user_import.import.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. function _user_import_process($settings) {
  3. // Load supported modules
  4. user_import_load_supported();
  5. $remaining_data = FALSE;
  6. $line_max = variable_get('user_import_line_max', 1000);
  7. $import_max = variable_get('user_import_max', 250);
  8. $field_match = _user_import_unconcatenate_field_match($settings['field_match']);
  9. $update_setting = _user_import_update_user_check($settings['options']['update_user']);
  10. $update_setting_per_module = $settings['options']['update_user'];
  11. $username_data = array();
  12. $username_order = array();
  13. $username_abbreviate = array();
  14. $first_line_skip = 0;
  15. $delimiter = isset($settings['delimiter']) && !empty($settings['delimiter']) ? $settings['delimiter'] : ',';
  16. ini_set('auto_detect_line_endings', TRUE);
  17. $handle = @fopen($settings['filepath'], "r");
  18. // move pointer to where test/import last finished
  19. if ($settings['pointer'] != 0) fseek($handle, $settings['pointer']);
  20. // start count of imports on this cron run
  21. $processed_counter = 0;
  22. while ($data = fgetcsv($handle, $line_max, $delimiter)) {
  23. $errors = user_import_errors(FALSE, TRUE);
  24. // if importing, check we are not over max number of imports per cron
  25. if ($settings['setting'] == 'import' && $processed_counter >= $import_max) {
  26. $remaining_data = TRUE;
  27. break;
  28. }
  29. // don't process empty lines
  30. $line_filled = (count($data) == 1 && drupal_strlen($data[0]) == 0) ? FALSE : TRUE;
  31. if ($line_filled) {
  32. // check if this is first line - if so should we skip?
  33. if (!empty($settings['first_line_skip']) && $settings['processed'] == 0) {
  34. // reset to false on second process
  35. $first_line_skip = ($first_line_skip === 0) ? TRUE : FALSE;
  36. }
  37. if (!$first_line_skip) {
  38. unset($errors, $fields);
  39. reset($field_match);
  40. $password = '';
  41. // Process data cell.
  42. foreach ($field_match as $column_id => $column_settings) {
  43. $type = $column_settings['type'];
  44. $field_id = $column_settings['field_id'];
  45. // Skip if this is a field used as part of a username but
  46. // not otherwise mapped for import.
  47. if ($type != 'username_part') {
  48. $fields[$type][$field_id] = module_invoke_all('user_import_data', $settings, $update_setting, $column_settings, $type, $field_id, $data, $column_id);
  49. }
  50. // Read in data if present for concatenating a user name.
  51. if ($column_settings['username'] > 0) {
  52. $username_data[$column_id] = $data[$column_id];
  53. $username_order[$column_id] = $column_settings['username'];
  54. $username_abbreviate[$column_id] = $column_settings['abbreviate'];
  55. }
  56. }
  57. $errors = user_import_errors();
  58. $account = array();
  59. $existing_account = FALSE;
  60. $updated = FALSE;
  61. // if we update existing users matched by email (and therefore passed validation even if this email already exists)
  62. // look for and use an existing account.
  63. if ($update_setting && !empty($fields['user']['email'][0])) {
  64. $existing_account = user_load_by_mail($fields['user']['email'][0]);
  65. if ($existing_account) $account = (array)$existing_account;
  66. }
  67. // if $account['uid'] is not empty then we can assume the account is being updated
  68. $account_additions = module_invoke_all('user_import_pre_save', $settings, $account, $fields, $errors, $update_setting_per_module);
  69. foreach ($account_additions as $field_name => $value) {
  70. $account[$field_name] = $value;
  71. }
  72. if (empty($errors)) {
  73. if ($settings['setting'] == 'import') {
  74. if ($existing_account) {
  75. $account = user_save($existing_account, $account);
  76. $updated = TRUE;
  77. }
  78. else {
  79. // Only set a user name if we are not updating an existing record.
  80. $account['name'] = _user_import_create_username($username_order, $username_data, $username_abbreviate, $settings['username_space']);
  81. $password = $account['pass'];
  82. $account = user_save('', $account);
  83. }
  84. module_invoke_all('user_import_after_save', $settings, $account, $password, $fields, $updated, $update_setting_per_module);
  85. $processed_counter++;
  86. }
  87. $settings['valid']++;
  88. }
  89. // If first line is skipped it doesn't count as processed.
  90. $settings['processed']++;
  91. }
  92. $settings['pointer'] = ftell($handle);
  93. // save lines that have fatal errors
  94. if (!empty($errors)) {
  95. $account_email = isset($account['email']) ? $account['email'] : '';
  96. _user_import_errors_display_save($settings['import_id'], $fields, $account_email, $errors);
  97. }
  98. }
  99. $settings['setting'] = _user_import_save_progress($settings['setting'], $remaining_data, $settings['pointer'], $settings['processed'], $settings['valid'], $settings['import_id']);
  100. }
  101. // Save progress.
  102. $settings['setting'] = _user_import_save_progress($settings['setting'], $remaining_data, $settings['pointer'], $settings['processed'], $settings['valid'], $settings['import_id'], TRUE);
  103. if ($settings['setting'] == 'imported') {
  104. module_invoke_all('user_import_imported', $settings['import_id'], $settings);
  105. }
  106. fclose($handle);
  107. return $settings;
  108. }
  109. // errors for user being imported
  110. function user_import_errors($error = FALSE, $clear = FALSE) {
  111. static $errors = array();
  112. if ($clear) $errors = array();
  113. if ($error) $errors[] = $error;
  114. return $errors;
  115. }
  116. function _user_import_create_username($order, $data, $abbreviate, $username_space) {
  117. $username = '';
  118. if (is_array($order)) {
  119. asort($order);
  120. //reset($order);
  121. //while (list ($file_column, $sequence) = each ($order)) {
  122. foreach ($order as $file_column => $sequence) {
  123. if (!empty($username) && !empty($username_space)) {
  124. $username .= ' ';
  125. }
  126. if ($abbreviate[$file_column] == 1) {
  127. //$username .= trim(drupal_strtoupper(chr(ord($data[$file_column]))));
  128. $first_character = trim($data[$file_column]);
  129. $first_character = drupal_substr($first_character, 0, 1);
  130. $username .= drupal_strtoupper($first_character);
  131. }
  132. else {
  133. $username .= trim($data[$file_column]);
  134. }
  135. }
  136. }
  137. if (empty($username)) $username = _user_import_random_username();
  138. $username = _user_import_sanitise_username($username);
  139. $username = _user_import_unique_username($username, TRUE);
  140. return $username;
  141. }
  142. /**
  143. * conform to Drupal username rules
  144. */
  145. function _user_import_sanitise_username($username) {
  146. // username cannot contain an illegal character
  147. $username = preg_replace('/[^\x80-\xF7 [:alnum:]@_.-]/', '', $username);
  148. $username = preg_replace(
  149. '/[\x{80}-\x{A0}' . // Non-printable ISO-8859-1 + NBSP
  150. '\x{AD}' . // Soft-hyphen
  151. '\x{2000}-\x{200F}' . // Various space characters
  152. '\x{2028}-\x{202F}' . // Bidirectional text overrides
  153. '\x{205F}-\x{206F}' . // Various text hinting characters
  154. '\x{FEFF}' . // Byte order mark
  155. '\x{FF01}-\x{FF60}' . // Full-width latin
  156. '\x{FFF9}-\x{FFFD}' . // Replacement characters
  157. '\x{0}]/u',
  158. '', $username);
  159. // username cannot contain multiple spaces in a row
  160. $username = preg_replace('/[ ]+/', ' ', $username);
  161. // username must be less than 56 characters
  162. $username = substr($username, 0, 56);
  163. // username cannot begin or end with a space
  164. $username = trim($username);
  165. return $username;
  166. }
  167. /**
  168. * deal with duplicate usernames
  169. */
  170. function _user_import_unique_username($username, $start = FALSE) {
  171. static $suffix = 1;
  172. if ($start) $suffix = 1;
  173. if ($suffix < 2) {
  174. $duplicate = db_query_range('SELECT uid from {users} where name = :name', 0, 1, array(':name' => $username))->fetchField();
  175. }
  176. else {
  177. $duplicate = db_query_range('SELECT uid from {users} where name = :name', 0, 1, array(':name' => "$username $suffix"))->fetchField();
  178. }
  179. // loop until name is valid
  180. if (!empty($duplicate)) {
  181. $suffix++;
  182. // If we loop to many times PHP will kill the script,
  183. // for large user bases that might be a problem with popular names.
  184. if ($suffix > 10) {
  185. $suffix = $suffix * mt_rand(10, 99);
  186. }
  187. _user_import_unique_username($username);
  188. }
  189. // add number at end of username if it already exists
  190. $username = ($suffix < 2) ? $username : "$username $suffix";
  191. return $username;
  192. }
  193. // Update settings for existing import
  194. function _user_import_settings_update($pointer, $processed, $valid, $setting, $import_id) {
  195. if (empty($import_id)) return;
  196. db_update('user_import')
  197. ->fields(array(
  198. 'pointer' => $pointer,
  199. 'processed' => $processed,
  200. 'valid' => $valid,
  201. 'setting' => $setting
  202. ))
  203. ->condition('import_id', $import_id)
  204. ->execute();
  205. }
  206. function _user_import_random_username() {
  207. $username = '';
  208. $vowels = 'aoueiy';
  209. $consonants = 'bcdfghjklmnpqrstvwxz';
  210. $length = 8;
  211. mt_srand((double)microtime() * 10000000);
  212. $next_vowel = 0;
  213. for ($count = 0; $count <= $length; $count++) {
  214. if ($next_vowel) {
  215. $rand = mt_rand(0, 5);
  216. $username .= $vowels{$rand};
  217. $next_vowel = 0;
  218. }
  219. else {
  220. $rand = mt_rand(0, 19);
  221. $username .= $consonants{$rand};
  222. $next_vowel = 1;
  223. }
  224. }
  225. return $username;
  226. }
  227. // check if any updates are to be made
  228. function _user_import_update_user_check($settings) {
  229. foreach ($settings as $setting) {
  230. if ($setting != UPDATE_NONE) return TRUE;
  231. }
  232. return FALSE;
  233. }
  234. function _user_import_errors_display_save($import_id, $data, $email, $errors) {
  235. $data['email'] = $email;
  236. $id = db_insert('user_import_errors')
  237. ->fields(array(
  238. 'import_id' => $import_id,
  239. 'data' => serialize($data),
  240. 'errors' => serialize($errors),
  241. ))
  242. ->execute();
  243. return;
  244. }
  245. /**
  246. * Save progress status and counter of the import.
  247. */
  248. function _user_import_save_progress($status, $remaining_data, $pointer, $processed, $valid, $import_id, $status_check = FALSE) {
  249. if ($status_check) {
  250. if ($status == 'import' && !$remaining_data) $status = 'imported';
  251. if ($status == 'test') $status = 'tested';
  252. }
  253. _user_import_settings_update($pointer, $processed, $valid, $status, $import_id);
  254. return $status;
  255. }