XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
📘  
Loading...
Searching...
No Matches
UserDataUploadConfAction.class.php
1<?php
7
8if (!defined('XOOPS_ROOT_PATH')) {
9 exit();
10}
11
12require_once __DIR__ . '/UserDataUploadAction.class.php';
13
15{
17 public function execute(&$controller, &$xoopsUser)
18 {
19
21 if (isset($_FILES['user_csv_file']) &&
22 0 == $_FILES['user_csv_file']['error']) {
23 return USER_FRAME_VIEW_SUCCESS;
24 }
25 return $this->getDefaultView($controller, $xoopsUser);
26 }
27
28
30 public function executeViewSuccess(&$controller, &$xoopsUser, &$render)
31 {
33 $render->setTemplateName('user_data_upload_conf.html');
34
35 // fields
36 $fields = [];
37 $user_handler =& $this->_getHandler();
38 $user_tmp = $user_handler->create();
39 $user_key = array_keys($user_tmp->gets());
40 foreach ($user_key as $key) {
41 $_f = '_MD_USER_LANG_'.strtoupper($key);
42 $fields[] = defined($_f) ? constant($_f) : $key ;
43 }
44 $render->setAttribute('user_fields', $fields);
45
47 $csv_data = [];
48 $csv_file = $_FILES['user_csv_file']['tmp_name'];
49 $csv_encoding = '';
50 $user_h =& $this->_getHandler();
51 if (function_exists('mb_detect_encoding')) {
52 $_csv_contents = implode('', file($csv_file));
53 $csv_encoding = mb_detect_encoding($_csv_contents);
54 }
55
56 if (false !== ($handle = fopen($csv_file, 'r'))) {
57 $current_locale = false;
58 if ('UTF-8' === $csv_encoding) {
59 $current_locale = setlocale(LC_ALL, '0');
60 setlocale(LC_ALL, 'ja_JP.UTF-8');
61 $bom = fread($handle, 3); // remove BOM
62 if (0xef !== ord($bom[0]) || 0xbb !== ord($bom[1]) || 0xbf !== ord($bom[2])) {
63 rewind($handle); // BOM not found then do rewind
64 }
65 }
66 $n = 0;
67 while (false !== ($_data = fgetcsv($handle))) {
68 if ($csv_encoding) {
69 mb_convert_variables(_CHARSET, $csv_encoding, $_data);
70 }
71 if (!$n++ || !implode('', $_data)) {
72 continue;
73 }
74 $user_data = [
75 'error' => false,
76 'update' => 0,
77 'is_new' => true,
78 'value' => [],
79 ];
80 if (count((array) $_data) != count($user_key)) {
81 $user_data['error'] = true;
82 }
83 if ($_data[0]) {
84 $user =& $user_h->get($_data[0]);
85 if ($user) {
86 for ($i=0; $i<count($user_key); $i++) {
87 $csv_value = $_data[$i];
88 $user_value = $user->get($user_key[$i]);
89 $update = $user_value != $csv_value;
90 switch ($user_key[$i]) {
91 case 'user_regdate':
92 case 'last_login':
93 $update = ($user_value || $csv_value) && 0 !== strcmp(formatTimestamp($user_value, 'Y/n/j H:i'), $csv_value);
94 if ($update) {
95 }
96 break;
97 case 'pass':
98 if (strlen($csv_value) < 32) {
99 $csv_value = User_Utils::encryptPassword($csv_value);
100 $update = $user_value !== $csv_value;
101 }
102 default:
103 }
104 $user_data['update'] = $user_data['update'] | $update;
105 $user_data['value'][] = [
106 'var' => $csv_value,
107 'update' => $update,
108 ];
109 }
110 $user_data['is_new'] = false;
111 }
112 }
113 if (true == $user_data['is_new']) {
114 for ($i=0; $i<count($user_key); $i++) {
115 $var = isset($_data[$i]) && '' !== $_data[$i] ? $_data[$i] : $user_tmp->get($user_key[$i]);
116 switch ($user_key[$i]) {
117 case 'user_regdate':
118 case 'last_login':
119 $var = formatTimestamp($var, 'Y/n/j H:i');
120 break;
121 }
122 $user_data['value'][] = [
123 'var' => $var,
124 'update' => 0
125 ];
126 }
127 }
128 $csv_data[] = $user_data;
129 }
130 if ($current_locale) {
131 setlocale(LC_ALL, $current_locale);
132 }
133 fclose($handle);
134 }
135
136 $render->setAttribute('csv_data', $csv_data);
137 $_SESSION['user_csv_upload_data'] = $csv_data;
138 }
139
140
141
142 // {{{ explodeCSV(Ethna_Util.php)
151 public function explodeCSV($csv, $delimiter = ',')
152 {
153 $space_list = '';
154 foreach ([' ', "\t", "\r", "\n"] as $c) {
155 if ($c != $delimiter) {
156 $space_list .= $c;
157 }
158 }
159
160 $line_end = '';
161 if (preg_match("/([$space_list]+)\$/sS", $csv, $match)) {
162 $line_end = $match[1];
163 }
164 $csv = substr($csv, 0, strlen($csv)-strlen($line_end));
165 $csv .= ' ';
166
167 $field = '';
168 $retval = [];
169
170 $index = 0;
171 $csv_len = strlen($csv);
172 do {
173 // 1. skip leading spaces
174 if (preg_match("/^([$space_list]+)/sS", substr($csv, $index), $match)) {
175 $index += strlen($match[1]);
176 }
177 if ($index >= $csv_len) {
178 break;
179 }
180
181 // 2. read field
182 if ('"' == $csv[$index]) {
183 // 2A. handle quote delimited field
184 $index++;
185 while ($index < $csv_len) {
186 if ('"' == $csv[$index]) {
187 // handle double quote
188 if ('"' == $csv[$index + 1]) {
189 $field .= $csv[$index];
190 $index += 2;
191 } else {
192 // must be end of string
193 while ($csv[$index] != $delimiter && $index < $csv_len) {
194 $index++;
195 }
196 if ($csv[$index] == $delimiter) {
197 $index++;
198 }
199 break;
200 }
201 } else {
202 // normal character
203 if (preg_match('/^([^"]*)/S', substr($csv, $index), $match)) {
204 $field .= $match[1];
205 $index += strlen($match[1]);
206 }
207
208 if ($index == $csv_len) {
209 $field = substr($field, 0, strlen($field)-1);
210 $field .= $line_end;
211
212 // request one more line
213// return Ethna::raiseNotice('CSV分割エラー(行継続)', E_UTIL_CSV_CONTINUE);
214 }
215 }
216 }
217 } else {
218 // 2B. handle non-quoted field
219 if (preg_match("/^([^$delimiter]*)/S", substr($csv, $index), $match)) {
220 $field .= $match[1];
221 $index += strlen($match[1]);
222 }
223
224 // remove trailing spaces
225 $field = preg_replace("/[$space_list]+\$/S", '', $field);
226 if (isset($csv[$index]) && $csv[$index] == $delimiter) {
227 $index++;
228 }
229 }
230 $retval[] = $field;
231 $field = '';
232 } while ($index < $csv_len);
233
234 return $retval;
235 }
236 // }}}
237}
execute(&$controller, &$xoopsUser)
アップされたCSVファイルを出力する
executeViewSuccess(&$controller, &$xoopsUser, &$render)
確認画面を表示
static encryptPassword($password)