-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_import.import.inc
387 lines (308 loc) · 10.9 KB
/
user_import.import.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?php
function _user_import_process($settings) {
$config = config('user_import.settings');
// Load supported modules.
user_import_load_supported();
$remaining_data = FALSE;
$line_max = $config->get('line_max');
$import_max = $config->get('user_import_max');
$field_match = _user_import_unconcatenate_field_match($settings['field_match']);
$update_setting = _user_import_update_user_check($settings['options']['update_user']);
$update_setting_per_module = $settings['options']['update_user'];
$username_data = array();
$username_order = array();
$username_abbreviate = array();
$first_line_skip = 0;
$delimiter = isset($settings['delimiter']) && !empty($settings['delimiter']) ? $settings['delimiter'] : ',';
$sencoding = isset($settings['encoding']) && !empty($settings['encoding']) ? $settings['encoding'] : 'UTF-8';
ini_set('auto_detect_line_endings', TRUE);
$handle = @fopen($settings['filepath'], "r");
// Move pointer to where test/import last finished.
if ($settings['pointer'] != 0) {
fseek($handle, $settings['pointer']);
}
// Start count of imports on this cron run.
$processed_counter = 0;
while ($data = fgetcsv($handle, $line_max, $delimiter)) {
// Reset error array prior to testing.
$errors = user_import_errors(FALSE, TRUE);
$ii = 0;
foreach ($data as $field) {
if ('UTF-8' != $sencoding) {
$field = mb_convert_encoding($field, 'UTF-8', $sencoding);
$data[$ii] = $field;
}
$encoding = mb_check_encoding($field, 'UTF-8');
if (!$encoding) {
user_import_errors(t('record contains an illegal character'));
$data[$ii] = '-???-';
}
$ii++;
}
// If importing, check we are not over max number of imports per cron.
if ($settings['setting'] == 'import' && $processed_counter >= $import_max) {
$remaining_data = TRUE;
break;
}
// Don't process empty lines.
$line_filled = (count($data) == 1 && backdrop_strlen($data[0]) == 0) ? FALSE : TRUE;
if ($line_filled) {
// Check if this is first line - if so should we skip?
if (!empty($settings['first_line_skip']) && $settings['processed'] == 0) {
// Reset to false on second process.
$first_line_skip = ($first_line_skip === 0) ? TRUE : FALSE;
}
if (!$first_line_skip) {
unset($errors, $fields);
reset($field_match);
$password = '';
// Process data cell.
foreach ($field_match as $column_id => $column_settings) {
$type = $column_settings['type'];
$field_id = $column_settings['field_id'];
// Skip if this is a field used as part of a username but
// not otherwise mapped for import.
if ($type != 'username_part') {
$fields[$type][$field_id] = module_invoke_all('user_import_data', $settings, $update_setting, $column_settings, $type, $field_id, $data, $column_id);
}
// Read in data if present for concatenating a user name.
if ($column_settings['username'] > 0) {
$username_data[$column_id] = $data[$column_id];
$username_order[$column_id] = $column_settings['username'];
$username_abbreviate[$column_id] = $column_settings['abbreviate'];
}
}
$errors = user_import_errors();
$updated = FALSE;
// Look for and use an existing account if set to update users.
if ($update_setting && !empty($fields['user']['email'][0])) {
$account = user_load_by_mail($fields['user']['email'][0]);
}
if (empty($account)) {
$account = entity_create('user', array());
}
$account_additions = module_invoke_all('user_import_pre_save', $settings, $account, $fields, $errors, $update_setting_per_module);
foreach ($account_additions as $field_name => $value) {
$account->$field_name = $value;
}
if (empty($errors)) {
if ($settings['setting'] == 'import') {
// If $account->uid is not empty then we can assume the account is being updated.
if ($account && $account->uid) {
user_save($account);
$updated = TRUE;
}
else {
// Only set a user name if we are not updating an existing record.
$account->name = _user_import_create_username($username_order, $username_data, $username_abbreviate, $settings['username_space']);
$password = $account->pass;
user_save($account);
}
module_invoke_all('user_import_after_save', $settings, $account, $password, $fields, $updated, $update_setting_per_module);
$processed_counter++;
}
$settings['valid']++;
}
// If first line is skipped it doesn't count as processed.
$settings['processed']++;
}
$settings['pointer'] = ftell($handle);
// Save lines that have fatal errors.
if (!empty($errors)) {
$account_email = isset($account->email) ? $account->email : '';
_user_import_errors_display_save($settings['import_id'], $fields, $account_email, $errors);
}
}
$settings['setting'] = _user_import_save_progress($settings['setting'], $remaining_data, $settings['pointer'], $settings['processed'], $settings['valid'], $settings['import_id']);
unset($account);
}
// Save progress.
$settings['setting'] = _user_import_save_progress($settings['setting'], $remaining_data, $settings['pointer'], $settings['processed'], $settings['valid'], $settings['import_id'], TRUE);
if ($settings['setting'] == 'imported') {
module_invoke_all('user_import_imported', $settings['import_id'], $settings);
}
fclose($handle);
return $settings;
}
/**
* Function to accumulate an array accumulating errors for import.
*
* @param string|bool $error
* A string describing the error, or FALSE.
* @param bool $clear
* TRUE if the $errors array shall be cleared.
*
* @return array
* The array with all the errors.
*/
function user_import_errors($error = FALSE, $clear = FALSE) {
static $errors = array();
if ($clear) {
$errors = array();
}
if ($error) {
$errors[] = $error;
}
return $errors;
}
function _user_import_create_username($order, $data, $abbreviate, $username_space) {
$username = '';
if (is_array($order)) {
asort($order);
foreach ($order as $file_column => $sequence) {
if (!empty($username) && !empty($username_space)) {
$username .= ' ';
}
if ($abbreviate[$file_column] == 1) {
$first_character = trim($data[$file_column]);
$first_character = backdrop_substr($first_character, 0, 1);
$username .= backdrop_strtoupper($first_character);
}
else {
$username .= trim($data[$file_column]);
}
}
}
if (empty($username)) {
$username = _user_import_random_username();
}
$username = _user_import_sanitise_username($username);
$username = _user_import_unique_username($username, TRUE);
return $username;
}
/**
* Conform to Backdrop username rules.
*/
function _user_import_sanitise_username($username) {
// Username cannot contain an illegal character.
$username = preg_replace('/[^\x80-\xF7 [:alnum:]@_.-]/', '', $username);
$username = preg_replace(
// Non-printable ISO-8859-1 + NBSP
'/[\x{80}-\x{A0}' .
// Soft-hyphen
'\x{AD}' .
// Various space characters
'\x{2000}-\x{200F}' .
// Bidirectional text overrides
'\x{2028}-\x{202F}' .
// Various text hinting characters
'\x{205F}-\x{206F}' .
// Byte order mark
'\x{FEFF}' .
// Full-width latin
'\x{FF01}-\x{FF60}' .
// Replacement characters
'\x{FFF9}-\x{FFFD}' .
'\x{0}]/u',
'', $username);
// username cannot contain multiple spaces in a row
$username = preg_replace('/[ ]+/', ' ', $username);
// username must be less than 56 characters
$username = substr($username, 0, 56);
// username cannot begin or end with a space
$username = trim($username);
return $username;
}
/**
* deal with duplicate usernames
*/
function _user_import_unique_username($username, $start = FALSE) {
static $suffix = 1;
if ($start) {
$suffix = 1;
}
if ($suffix < 2) {
$duplicate = db_query_range('SELECT uid from {users} where name = :name', 0, 1, array(':name' => $username))->fetchField();
}
else {
$duplicate = db_query_range('SELECT uid from {users} where name = :name', 0, 1, array(':name' => "$username $suffix"))->fetchField();
}
// loop until name is valid
if (!empty($duplicate)) {
$suffix++;
// If we loop to many times PHP will kill the script,
// for large user bases that might be a problem with popular names.
if ($suffix > 10) {
$suffix = $suffix * mt_rand(10, 99);
}
_user_import_unique_username($username);
}
// add number at end of username if it already exists
$username = ($suffix < 2) ? $username : "$username $suffix";
return $username;
}
/**
* Update settings for existing import
*/
function _user_import_settings_update($pointer, $processed, $valid, $setting, $import_id) {
if (empty($import_id)) {
return;
}
db_update('user_import')
->fields(array(
'pointer' => $pointer,
'processed' => $processed,
'valid' => $valid,
'setting' => $setting,
))
->condition('import_id', $import_id)
->execute();
}
function _user_import_random_username() {
$username = '';
$vowels = 'aoueiy';
$consonants = 'bcdfghjklmnpqrstvwxz';
$length = 8;
mt_srand((double) microtime() * 10000000);
$next_vowel = 0;
for ($count = 0; $count <= $length; $count++) {
if ($next_vowel) {
$rand = mt_rand(0, 5);
$username .= $vowels[$rand];
$next_vowel = 0;
}
else {
$rand = mt_rand(0, 19);
$username .= $consonants[$rand];
$next_vowel = 1;
}
}
return $username;
}
/**
* check if any updates are to be made
*/
function _user_import_update_user_check($settings) {
foreach ($settings as $setting) {
if ($setting != UPDATE_NONE) {
return TRUE;
}
}
return FALSE;
}
function _user_import_errors_display_save($import_id, $data, $email, $errors) {
$data['email'] = $email;
$id = db_insert('user_import_errors')
->fields(array(
'import_id' => $import_id,
'data' => serialize($data),
'errors' => serialize($errors),
))
->execute();
return;
}
/**
* Save progress status and counter of the import.
*/
function _user_import_save_progress($status, $remaining_data, $pointer, $processed, $valid, $import_id, $status_check = FALSE) {
if ($status_check) {
if ($status == 'import' && !$remaining_data) {
$status = 'imported';
}
if ($status == 'test') {
$status = 'tested';
}
}
_user_import_settings_update($pointer, $processed, $valid, $status, $import_id);
return $status;
}