1: <?php
2: 3: 4: 5:
6:
7: if (!defined('XOOPS_ROOT_PATH')) exit();
8:
9: require_once dirname(__FILE__)."/UserDataUploadAction.class.php";
10:
11: class User_UserDataUploadConfAction extends User_UserDataUploadAction
12: {
13:
14: function execute(&$controller, &$xoopsUser)
15: {
16:
17:
18: if (isset($_FILES['user_csv_file']) &&
19: $_FILES['user_csv_file']['error'] == 0){
20: return USER_FRAME_VIEW_SUCCESS;
21: }
22: return $this->getDefaultView($controller, $xoopsUser);
23: }
24:
25:
26:
27: function executeViewSuccess(&$controller, &$xoopsUser, &$render)
28: {
29:
30: $render->setTemplateName("user_data_upload_conf.html");
31:
32:
33: $fields = array();
34: $user_handler =& $this->_getHandler();
35: $user_tmp = $user_handler->create();
36: $user_key = array_keys($user_tmp->gets());
37: foreach ($user_key as $key){
38: $_f = '_MD_USER_LANG_'.strtoupper($key);
39: $fields[] = defined($_f) ? constant($_f) : $key ;
40: }
41: $render->setAttribute('user_fields', $fields);
42:
43:
44: $csv_data = array();
45: $csv_file = $_FILES['user_csv_file']['tmp_name'];
46: $csv_encoding = '';
47: $user_h =& $this->_getHandler();
48: if (function_exists('mb_detect_encoding')){
49: $_csv_contents = implode('', file($csv_file));
50: $csv_encoding = mb_detect_encoding($_csv_contents);
51: }
52:
53: foreach(file($csv_file) as $n=>$_data_line){
54: if ($csv_encoding){
55: mb_convert_variables(_CHARSET, $csv_encoding, $_data_line);
56: }
57: $_data = $this->explodeCSV($_data_line);
58: if (!$n || !implode('', $_data)){
59: continue;
60: }
61: $user_data = array(
62: 'error' => false,
63: 'update' => 0,
64: 'is_new' => true,
65: 'value' => array(),
66: );
67: if (count($_data) != count($user_key)){
68: $user_data['error'] = true;
69: }
70: if ($_data[0]){
71: $user =& $user_h->get($_data[0]);
72: if ($user){
73: for ($i=0; $i<count($user_key); $i++){
74: $csv_value = $_data[$i];
75: $user_value = $user->get($user_key[$i]);
76: $update = $user_value != $csv_value;
77: switch ($user_key[$i]){
78: case 'user_regdate':
79: case 'last_login':
80: $update = ($user_value || $csv_value) && strcmp(formatTimestamp($user_value, 'Y/n/j H:i'), $csv_value)!==0;
81: if ($update){
82: }
83: break;
84: case 'pass':
85: if (strlen($csv_value)!=32){
86: $update = $user_value != md5($csv_value);
87: $csv_value = md5($csv_value);
88: }
89: default:
90: }
91: $user_data['update'] = $user_data['update'] | $update;
92: $user_data['value'][] = array(
93: 'var' => $csv_value,
94: 'update' => $update,
95: );
96: }
97: $user_data['is_new'] = false;
98: }
99: }
100: if ($user_data['is_new'] == true){
101: for ($i=0; $i<count($user_key); $i++){
102: $var = isset($_data[$i]) && $_data[$i]!=='' ? $_data[$i] : $user_tmp->get($user_key[$i]);
103: switch ($user_key[$i]){
104: case 'user_regdate':
105: case 'last_login':
106: $var = formatTimestamp($var, 'Y/n/j H:i');
107: break;
108: }
109: $user_data['value'][] = array(
110: 'var' => $var,
111: 'update' => 0);
112: }
113: }
114: $csv_data[] = $user_data;
115: }
116:
117: $render->setAttribute('csv_data', $csv_data);
118: $_SESSION['user_csv_upload_data'] = $csv_data;
119: }
120:
121:
122:
123:
124: 125: 126: 127: 128: 129: 130: 131:
132: function explodeCSV($csv, $delimiter = ",")
133: {
134: $space_list = '';
135: foreach (array(" ", "\t", "\r", "\n") as $c) {
136: if ($c != $delimiter) {
137: $space_list .= $c;
138: }
139: }
140:
141: $line_end = "";
142: if (preg_match("/([$space_list]+)\$/sS", $csv, $match)) {
143: $line_end = $match[1];
144: }
145: $csv = substr($csv, 0, strlen($csv)-strlen($line_end));
146: $csv .= ' ';
147:
148: $field = '';
149: $retval = array();
150:
151: $index = 0;
152: $csv_len = strlen($csv);
153: do {
154:
155: if (preg_match("/^([$space_list]+)/sS", substr($csv, $index), $match)) {
156: $index += strlen($match[1]);
157: }
158: if ($index >= $csv_len) {
159: break;
160: }
161:
162:
163: if ($csv{$index} == '"') {
164:
165: $index++;
166: while ($index < $csv_len) {
167: if ($csv{$index} == '"') {
168:
169: if ($csv{$index+1} == '"') {
170: $field .= $csv{$index};
171: $index += 2;
172: } else {
173:
174: while ($csv{$index} != $delimiter && $index < $csv_len) {
175: $index++;
176: }
177: if ($csv{$index} == $delimiter) {
178: $index++;
179: }
180: break;
181: }
182: } else {
183:
184: if (preg_match("/^([^\"]*)/S", substr($csv, $index), $match)) {
185: $field .= $match[1];
186: $index += strlen($match[1]);
187: }
188:
189: if ($index == $csv_len) {
190: $field = substr($field, 0, strlen($field)-1);
191: $field .= $line_end;
192:
193:
194:
195: }
196: }
197: }
198: } else {
199:
200: if (preg_match("/^([^$delimiter]*)/S", substr($csv, $index), $match)) {
201: $field .= $match[1];
202: $index += strlen($match[1]);
203: }
204:
205:
206: $field = preg_replace("/[$space_list]+\$/S", '', $field);
207: if (isset($csv{$index}) && $csv{$index} == $delimiter) {
208: $index++;
209: }
210: }
211: $retval[] = $field;
212: $field = '';
213: } while ($index < $csv_len);
214:
215: return $retval;
216: }
217:
218: }
219:
220: ?>
221: