/[drupal]/contributions/modules/gallery/gallery_user.inc
ViewVC logotype

Contents of /contributions/modules/gallery/gallery_user.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.11 - (show annotations) (download) (as text)
Fri Nov 23 11:20:33 2007 UTC (2 years ago) by profix898
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
Changes since 1.10: +40 -38 lines
File MIME type: text/x-php
- very early D6 version (Oct-01, 2007)
1 <?php
2 // $Id: gallery_user.inc,v 1.9.2.29 2007/09/10 20:11:16 profix898 Exp $
3
4 require_once(drupal_get_path('module', 'gallery') .'/gallery_groups.inc');
5
6 /**
7 * gallery.module : gallery_user.inc
8 * User Functions (insert, update, delete, view, details, ...)
9 */
10
11 define('GALLERY_MAP_UNKNOWN', 0);
12 define('GALLERY_MAP_USER_EXISTS', 1);
13 define('GALLERY_MAP_USER_EXISTS_BUT_NEEDS_MAPPING', 2);
14 define('GALLERY_MAP_USER_DOES_NOT_EXIST_BUT_IS_MAPPED', 3);
15 define('GALLERY_MAP_USER_DOES_NOT_EXIST', 4);
16
17 define('GALLERY_USERINFO_NOERROR', 1);
18 define('GALLERY_USERINFO_ERROR', 2);
19 define('GALLERY_USERINFO_ERROR_MISSING', 3);
20 define('GALLERY_USERINFO_ERROR_USERNAME', 4);
21 define('GALLERY_USERINFO_ERROR_FULLNAME', 5);
22 define('GALLERY_USERINFO_ERROR_FULLNAME_MISSING', 6);
23 define('GALLERY_USERINFO_ERROR_EMAIL', 7);
24 define('GALLERY_USERINFO_ERROR_PASSWORD', 8);
25 define('GALLERY_USERINFO_ERROR_HASHMETHOD', 9);
26 define('GALLERY_USERINFO_ERROR_GROUPS', 10);
27
28 define('GALLERY_IMPORT_CONFLICT_DUPLICATE', 1);
29 define('GALLERY_IMPORT_CONFLICT_INVALID', 2);
30
31 /**
32 * Function gallery_user_insert().
33 * (insert new user)
34 */
35 function gallery_user_insert(&$edit, $user) {
36 $user->roles = isset($edit['roles']) ? $edit['roles'] : $user->roles;
37 $user->status = isset($edit['status']) ? $edit['status'] : TRUE;
38
39 gallery_user_modify($user, 'create');
40 }
41
42 /**
43 * Function gallery_user_update().
44 * (update a user with new information)
45 */
46 function gallery_user_update(&$edit, $user) {
47 $user->language = isset($edit['language']) ? $edit['language'] : gallery_get_language($user);
48 $user->pass = !empty($edit['pass']) ? md5($edit['pass']) : $user->pass;
49 $user->status = isset($edit['status']) ? $edit['status'] : $user->status;
50 $user->mail = !empty($edit['mail']) ? $edit['mail'] : $user->mail;
51 $user->roles = isset($edit['roles']) ? $edit['roles'] : $user->roles;
52
53 // Fullname support
54 if (module_exists('profile') && variable_get('gallery_use_fullname', 0)) {
55 $fullname_field = variable_get('gallery_profile_fullname_field', 'profile_fullname');
56 $user->$fullname_field = isset($edit[$fullname_field]) ? $edit[$fullname_field] : $user->$fullname_field;
57 }
58
59 // Username is about to change
60 if ($namechange = (isset($edit['name']) && ($edit['name'] != $user->name))) {
61 // Make sure the original user is up to date
62 gallery_user_modify($user, 'update', TRUE);
63 }
64
65 $user->name = isset($edit['name']) ? $edit['name'] : $user->name;
66
67 if ($namechange) {
68 // Change username
69 gallery_user_modify($user, 'username');
70 }
71 else {
72 // Update user
73 gallery_user_modify($user, 'update', TRUE);
74 }
75 }
76
77 /**
78 * Function gallery_user_delete().
79 * (delete the user from the Gallery)
80 */
81 function gallery_user_delete($user) {
82 gallery_user_modify($user, 'delete');
83 }
84
85 /**
86 * Function gallery_user_modify().
87 * (modify (create/update/delete) a user)
88 */
89 function gallery_user_modify($user, $action = 'create', $groups = FALSE, $vars = NULL) {
90 if (!_gallery_init(TRUE, $vars)) {
91 return FALSE;
92 }
93
94 // Check for fullname support
95 $fullname_field = variable_get('gallery_profile_fullname_field', 'profile_fullname');
96 $usefullname = module_exists('profile') && variable_get('gallery_use_fullname', 0);
97 $fullname = $usefullname ? (isset($user->$fullname_field) ? $user->$fullname_field : '') : $user->name;
98
99 // Generate random password for G2 if user is blocked
100 $pass = ($user->status) ? $user->pass : user_password(20);
101
102 switch ($action) {
103 case 'username':
104 $ret = GalleryEmbed::updateUser($user->uid,
105 array('username' => $user->name,
106 'fullname' => $fullname,
107 'email' => $user->mail,
108 'language' => gallery_get_language($user),
109 'hashedpassword' => $pass,
110 'hashmethod' => 'md5'));
111 if ($ret) {
112 gallery_error(t('Error updating Gallery user (username changed)'), $ret);
113 return FALSE;
114 }
115 break;
116 case 'create' :
117 case 'update' :
118 // Get map state of the user
119 list($g2_user_state, $g2_user, $ret) = gallery_user_map_state($user);
120 if ($ret) {
121 gallery_error(t('Error determining user map state'), $ret);
122 return FALSE;
123 }
124
125 // Complete user mapping
126 switch ($g2_user_state) {
127 case GALLERY_MAP_USER_EXISTS_BUT_NEEDS_MAPPING:
128 // create map entry for the user
129 $ret = GalleryEmbed::addExternalIdMapEntry($user->uid, $g2_user->id, 'GalleryUser');
130 if ($ret) {
131 gallery_error(t('Error creating map entry (ExternlIdMapEntry)'), $ret);
132 return FALSE;
133 }
134 case GALLERY_MAP_USER_EXISTS:
135 // Update user (Drupal -> G2)
136 $ret = GalleryEmbed::updateUser($user->uid,
137 array('username' => $user->name,
138 'fullname' => $fullname,
139 'email' => $user->mail,
140 'language' => gallery_get_language($user),
141 'hashedpassword' => $pass,
142 'hashmethod' => 'md5'));
143 if ($ret) {
144 gallery_error(t('Error updating Gallery user'), $ret);
145 return FALSE;
146 }
147 break;
148 case GALLERY_MAP_USER_DOES_NOT_EXIST_BUT_IS_MAPPED:
149 // Remove mapping for non-existing user
150 // (also happens if gallery_user_modify() is called with a changed username)
151 $ret = GalleryCoreApi::removeMapEntry('ExternalIdMap', array('externalId' => $user->uid, 'entityType' => 'GalleryUser'));
152 if ($ret) {
153 gallery_error(t('Error removing map entry (ExternlIdMapEntry)'), $ret);
154 return FALSE;
155 }
156 case GALLERY_MAP_USER_DOES_NOT_EXIST:
157 // Create new user
158 if (!$user->uid)
159 return FALSE;
160 $ret = GalleryEmbed::createUser($user->uid,
161 array('username' => $user->name,
162 'email' => $user->mail,
163 'fullname' => $fullname,
164 'language' => gallery_get_language($user),
165 'hashedpassword' => $pass,
166 'hashmethod' => 'md5'));
167 if ($ret) {
168 gallery_error(t('Error creating Gallery user'), $ret);
169 return FALSE;
170 }
171 list($ret, $g2_user) = GalleryCoreApi::loadEntityByExternalId($user->uid, 'GalleryUser');
172 if ($ret) {
173 gallery_error(t('Error loading newly created Gallery user'), $ret);
174 return FALSE;
175 }
176 break;
177 }
178 // Update group info
179 _gallery_groups_user($user, $groups);
180 // Admin role mapping
181 $admin_role = variable_get('gallery_user_admin_role', 0);
182 if (($admin_role && in_array($admin_role, array_keys($user->roles))) || ($user->uid == 1)) {
183 // Get G2 admin group id
184 list($ret, $g2_admin_gid) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.adminGroup');
185 if ($ret) {
186 gallery_error(t('Error getting \'adminGroup\' id'), $ret);
187 return FALSE;
188 }
189 // Add user to admin group
190 $ret = GalleryCoreApi::addUserToGroup($g2_user->id, $g2_admin_gid);
191 if ($ret) {
192 gallery_error(t('Error adding user to Gallery group (:gid)',
193 array(':gid' => $g2_admin_gid)), $ret);
194 return FALSE;
195 }
196 }
197 break;
198 case 'delete' :
199 $ret = GalleryEmbed::deleteUser($user->uid);
200 if ($ret) {
201 gallery_error(t('Error deleting Gallery user'), $ret);
202 return FALSE;
203 }
204 break;
205 }
206
207 // Set the 'locked' property for the user
208 if (variable_get('gallery_user_locked', 0) && ($action != 'delete') && ($user->uid > 1)) {
209 list($ret, $g2_user) = GalleryCoreApi::loadEntityByExternalId($user->uid, 'GalleryUser');
210 if ($ret) {
211 gallery_error(t('Error loading Gallery user'), $ret);
212 return FALSE;
213 }
214 if (!$g2_user->locked) {
215 list($ret, $lock_id) = GalleryCoreApi::acquireWriteLock($g2_user->id);
216 if ($ret) {
217 gallery_error(t('Error acquiring write lock'), $ret);
218 return FALSE;
219 }
220 $g2_user->setLocked(TRUE);
221 if ($g2_user->save()) {
222 gallery_error(t('Locking user account failed'), $ret);
223 return FALSE;
224 }
225 }
226 }
227
228 GalleryEmbed::done();
229 return TRUE;
230 }
231
232 /**
233 * Function _gallery_user_sync().
234 * (sync user info for $uid)
235 */
236 function _gallery_user_sync($uid) {
237 $user = user_load(array('uid' => $uid));
238 gallery_user_modify($user, 'update', TRUE);
239 }
240
241 /**
242 * Function gallery_user_view().
243 * (view Gallery user details for a specific user)
244 */
245 function gallery_user_view($user) {
246 if (variable_get('gallery_user_hide_profile', 0) || !_gallery_init(TRUE)) {
247 return;
248 }
249 // User album status
250 if ($useralbum = gallery_user_useralbum($user->uid)) {
251 $form['gallery_view_user_album'] = array(
252 'value' => l(t('User Album'), $useralbum),
253 'class' => 'send-message');
254 }
255 else {
256 $form['gallery_view_user_album'] = array(
257 'value' => t('User has not created an album yet'),
258 'class' => 'send-message');
259 }
260 // Sync/Map status info
261 $g2_userinfo = gallery_user_map_info($user);
262 if (($g2_userinfo['status']) && (user_access('administer users'))) {
263 $form['gallery_view_user'] = array(
264 'title' => t('Gallery2-Drupal Sync Status'),
265 'value' => implode(',<br />', gallery_user_map_info_status($g2_userinfo['status']))
266 );
267 }
268
269 GalleryEmbed::done();
270 if (!empty($form)) {
271 return array(t('Gallery2') => $form);
272 }
273 }
274
275 /**
276 * Function gallery_user_useralbum().
277 * (create link to user album)
278 */
279 function gallery_user_useralbum($uid = NULL, $link = TRUE) {
280 if (!_gallery_init(TRUE)) {
281 return FALSE;
282 }
283 // Load G2 user
284 global $user;
285 list($ret, $g2_user) = GalleryCoreApi::loadEntityByExternalId(isset($uid) ? $uid : $user->uid, 'GalleryUser');
286 if ($ret) {
287 if (!($ret->getErrorCode() & ERROR_MISSING_OBJECT)) {
288 gallery_error(t('Error loading Gallery user'), $ret);
289 return FALSE;
290 }
291 }
292 // User album status
293 if ($g2_user && (gallery_single_plugin_status('useralbum') == GALLERY_PLUGIN_ENABLED)) {
294 // Fetch user album id
295 list($ret, $album_id) = GalleryCoreApi::getPluginParameter('module', 'useralbum', 'albumId', $g2_user->id);
296 if ($ret) {
297 gallery_error(t('Error fetching user album id'), $ret);
298 return FALSE;
299 }
300 // Generate link to user album
301 if (is_numeric($album_id) && $album_id > 0) {
302 return $link ? gallery_generate_url(array('itemId' => $album_id), FALSE) : $album_id;
303 }
304 }
305
306 return FALSE;
307 }
308
309 /**
310 * Function gallery_user_map_info().
311 * (get info about user map status)
312 */
313 function gallery_user_map_info($user, $noerror_status = TRUE) {
314 $g2_userinfo = array('g2_id' => -1, 'status' => array());
315 // User map entry
316 $ret = GalleryEmbed::isExternalIdMapped($user->uid, 'GalleryUser');
317 if ($ret && !($ret->getErrorCode() & ERROR_MISSING_OBJECT)) {
318 $g2_userinfo['status'][] = GALLERY_USERINFO_ERROR_MISSING;
319 return $g2_userinfo;
320 }
321 // But user not available
322 list($ret, $g2_user) = GalleryCoreApi::loadEntityByExternalId($user->uid, 'GalleryUser');
323 if ($ret) {
324 $g2_userinfo['status'][] = GALLERY_USERINFO_ERROR_MISSING;
325 return $g2_userinfo;
326 }
327 // Username
328 $g2_userinfo['g2_id'] = $g2_user->id;
329 if ($g2_user->userName != $user->name) {
330 $g2_userinfo['status'][] = GALLERY_USERINFO_ERROR_USERNAME;
331 }
332 // Fullname
333 if (module_exists('profile') && variable_get('gallery_use_fullname', 0)) {
334 $fullname_field = variable_get('gallery_profile_fullname_field', 'profile_fullname');
335 $fullname_result = db_query("SELECT v.value FROM {profile_values} v INNER JOIN {profile_fields} f ON v.fid = f.fid AND v.uid = %d WHERE f.name = '%s'", $user->uid, $fullname_field);
336 $fullname = db_fetch_object($fullname_result);
337 $fullname = $fullname->value;
338 if ($g2_user->fullName != $fullname) {
339 $g2_userinfo['status'][] = GALLERY_USERINFO_ERROR_FULLNAME;
340 }
341 elseif (!$fullname) {
342 $g2_userinfo['status'][] = GALLERY_USERINFO_ERROR_FULLNAME_MISSING;
343 }
344 }
345 // Email adress
346 if ($g2_user->email != $user->mail) {
347 $g2_userinfo['status'][] = GALLERY_USERINFO_ERROR_EMAIL;
348 }
349 // Password
350 if ($user->status && ($g2_user->hashedPassword != $user->pass)) {
351 if (strlen($g2_user->hashedPassword) != strlen($user->pass)) {
352 $g2_userinfo['status'][] = GALLERY_USERINFO_ERROR_HASHMETHOD;
353 }
354 $g2_userinfo['status'][] = GALLERY_USERINFO_ERROR_PASSWORD;
355 }
356 // Roles/Groups
357 if (!gallery_groups_map_info($g2_user, $user)) {
358 $g2_userinfo['status'][] = GALLERY_USERINFO_ERROR_GROUPS;
359 }
360 // Overall sync status
361 if ($noerror_status && !count($g2_userinfo['status'])) {
362 $g2_userinfo['status'][] = GALLERY_USERINFO_NOERROR;
363 }
364
365 return $g2_userinfo;
366 }
367
368 /**
369 * Function gallery_user_map_info_status().
370 * (get string representation of the use map status)
371 */
372 function gallery_user_map_info_status($info = array(), $format = TRUE) {
373 $info_map = array(
374 GALLERY_USERINFO_NOERROR => t('OK'),
375 GALLERY_USERINFO_ERROR => t('Any Error'),
376 GALLERY_USERINFO_ERROR_MISSING => t('Missing from G2'),
377 GALLERY_USERINFO_ERROR_USERNAME => t('Different Usernames'),
378 GALLERY_USERINFO_ERROR_FULLNAME => t('Different Full Names'),
379 GALLERY_USERINFO_ERROR_FULLNAME_MISSING => t('G2 Full Name missing'),
380 GALLERY_USERINFO_ERROR_EMAIL => t('Different E-Mails'),
381 GALLERY_USERINFO_ERROR_PASSWORD => t('Different Passwords'),
382 GALLERY_USERINFO_ERROR_HASHMETHOD => t('Imported/Different hash method'),
383 GALLERY_USERINFO_ERROR_GROUPS => t('Roles <> Groups')
384 );
385
386 if ($format) {
387 $status = array();
388 if (!count($info)) {
389 $info[] = GALLERY_USERINFO_NOERROR;
390 }
391 foreach ($info as $key => $value) {
392 $status[] = $info_map[$value];
393 }
394 return $status;
395 }
396
397 return $info_map;
398 }
399
400 /**
401 * Function gallery_user_map_state().
402 * (get state of user mapping)
403 */
404 function gallery_user_map_state($user) {
405 // See if user already exists in G2
406 list($ret, $g2_user) = GalleryCoreApi::fetchUserByUsername($user->name);
407 if (!$ret) {
408 // User is in G2, so map the user if needed
409 $ret2 = GalleryEmbed::isExternalIdMapped($user->uid, 'GalleryUser');
410 if ($ret2) {
411 if ($ret2->getErrorCode() & ERROR_MISSING_OBJECT) {
412 return array(GALLERY_MAP_USER_EXISTS_BUT_NEEDS_MAPPING, $g2_user, NULL);
413 }
414 else {
415 // Some other error
416 return array(GALLERY_MAP_UNKNOWN, $g2_user, $ret2);
417 }
418 }
419 else {
420 return array(GALLERY_MAP_USER_EXISTS, $g2_user, NULL);
421 }
422 }
423 elseif ($ret->getErrorCode() & ERROR_MISSING_OBJECT) {
424 // User does not yet exist in G2
425 // Check if the externalID was mapped (it should not be)
426 $ret2 = GalleryEmbed::isExternalIdMapped($user->uid, 'GalleryUser');
427 if ($ret2) {
428 if ($ret2->getErrorCode() & ERROR_MISSING_OBJECT) {
429 return array(GALLERY_MAP_USER_DOES_NOT_EXIST, $g2_user, NULL);
430 }
431 else {
432 // Some other error
433 return array(GALLERY_MAP_UNKNOWN, $g2_user, $ret2);
434 }
435 }
436 else {
437 // User is mapped
438 return array(GALLERY_MAP_USER_DOES_NOT_EXIST_BUT_IS_MAPPED, $g2_user, NULL);
439 }
440 }
441 else {
442 return array(NULL, $g2_user, $ret);
443 }
444 }
445
446 /**
447 * Function _gallery_user_import().
448 * (import Gallery users into Drupal)
449 */
450 function _gallery_user_import($g2_users, &$messages) {
451 $resolve_conflict = variable_get('gallery_user_import_conflict', array());
452 // Anonymous user id
453 list($ret, $guest) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.anonymousUser');
454 if (!$ret) {
455 unset($g2_users[$guest]);
456 }
457 if (($g2_groups_map = _gallery_groups_map()) === FALSE) {
458 return FALSE;
459 }
460 if (($g2_extIdMap = _gallery_user_map(array_keys($g2_users))) === FALSE) {
461 return FALSE;
462 }
463 gallery_debug($g2_users, t('G2 Users'));
464 gallery_debug($g2_extIdMap, t('G2 ExternalIdMap'));
465 // Iterate over G2 users
466 foreach ($g2_users as $g2_id => $g2_username) {
467 $new_user = !array_key_exists($g2_id, $g2_extIdMap);
468 list($ret, $g2_user) = GalleryCoreApi::fetchUserByUsername($g2_username);
469 if ($ret) {
470 gallery_error(t('Error fetching user by username (:name)',
471 array(':name' => $g2_username)), $ret);
472 return FALSE;
473 }
474 // Collect user details and validate the values (name, mail, ...)
475 $values = array();
476 $values['name'] = $g2_user->userName;
477 if ($error = user_validate_name($values['name'])) {
478 $messages[] = t('G2 User Import (uid: :uid, name: \':name\'): !error',
479 array(':uid' => $g2_id, ':name' => $values['name'], '!error' => $error));
480 continue;
481 }
482 $values['fullname'] = $g2_user->fullName;
483 $values['pass'] = user_password(20);
484 $password_hash = $g2_user->hashedPassword;
485 $values['mail'] = $g2_user->email;
486 if ($error = user_validate_mail($values['mail'])) {
487 if ($resolve_conflict[GALLERY_IMPORT_CONFLICT_INVALID]) {
488 _gallery_user_resolve_mail($values['mail'], $values['name'], $messages);
489 $error = user_validate_mail($values['mail']) ? $error : FALSE;
490 }
491 if ($error) {
492 $messages[] = t('G2 User Import (uid: :uid, name: \':name\'): !error',
493 array(':uid' => $g2_id, ':name' => $values['name'], '!error' => $error));
494 continue;
495 }
496 }
497 else if ($new_user && db_result(db_query("SELECT COUNT(uid) FROM {users} WHERE uid != 0 AND LOWER(mail) = LOWER('%s')", $values['mail'])) > 0) {
498 $error = TRUE;
499 if ($resolve_conflict[GALLERY_IMPORT_CONFLICT_DUPLICATE]) {
500 _gallery_user_resolve_mail($values['mail'], $values['name'], $messages);
501 $error = FALSE;
502 }
503 if ($error) {
504 $messages[] = t('G2 User Import (uid: :uid, name: \':name\'): The e-mail address :mail is already registered.',
505 array(':uid' => $g2_id, ':name' => $values['name'], ':mail' => $values['mail']));
506 continue;
507 }
508 }
509 $values['language'] = $g2_user->language;
510 $values['created'] = $g2_user->creationTimestamp;
511 $values['roles'] = array();
512 list($ret, $g2_groups) = GalleryCoreApi::fetchGroupsForUser($g2_user->id);
513 if ($ret) {
514 gallery_error(t('Error fetching groups for user (uid: :uid)',
515 array(':uid' => $g2_id)), $ret);
516 return FALSE;
517 }
518 foreach ($g2_groups as $g2_gid => $g2_groupname) {
519 if (isset($g2_groups_map[$g2_gid])) {
520 $values['roles'][$g2_groups_map[$g2_gid]] = $g2_groupname;
521 }
522 }
523 // Is the user blocked in G2
524 list($ret, $g2_user_blocked) = GalleryCoreApi::isDisabledUsername($g2_username);
525 if ($ret) {
526 gallery_error(t('Error calling isDisabledUsername() for \':name\'',
527 array(':name' => $g2_username)), $ret);
528 return FALSE;
529 }
530 $values['status'] = !$g2_user_blocked;
531 // Create new Drupal user (this will also override the G2 user
532 // during hook_user, but there is no 'clean' way to avoid this)
533 if ($new_user) {
534 $values['notify'] = FALSE;
535 if (!($user = user_save('', $values))) {
536 $messages[] = t('Error creating Drupal user for G2 user (uid: :uid)',
537 array(':uid' => $g2_id));
538 continue;
539 }
540 }
541 else {
542 $user = new stdClass();
543 $user->uid = $g2_extIdMap[$g2_id];
544 }
545 // Override user details if requested (or for new users)
546 if ($user->uid && ($new_user || variable_get('gallery_user_import_override', 0))) {
547 // Fullname support
548 if (module_exists('profile') && variable_get('gallery_use_fullname', 0)) {
549 $fullname_category = variable_get('gallery_profile_fullname_category', 'Personal Information');
550 $fullname_field = variable_get('gallery_profile_fullname_field', 'profile_fullname');
551 $values[$fullname_field] = $values['fullname'];
552 profile_save_profile($values, $user, $fullname_category);
553 }
554 // Do we have a md5 hash (unsalted hash)?
555 if (strlen($password_hash) == 32) {
556 db_query("UPDATE {users} SET pass = '%s' WHERE uid = %d", $password_hash, $user->uid);
557 unset($values['pass']);
558 }
559 // Update Drupal user with G2 user details (invokes hook_user)
560 user_save(user_load(array('uid' => $user->uid)), $values);
561 // Override/Restore password in G2 (in case we have a salted G2 hash)
562 // (Drupal hash will remain a dummy and user is authenticated in hook_auth against G2 directly)
563 if (strlen($password_hash) > 32) {
564 $ret = GalleryEmbed::updateUser($user->uid, array('hashedpassword' => $password_hash, 'hashmethod' => 'md5'));
565 if ($ret) {
566 gallery_error(t('Error updating Gallery user (password)'), $ret);
567 return FALSE;
568 }
569 }
570 }
571 }
572
573 return TRUE;
574 }
575
576 /**
577 * Function _gallery_user_resolve_mail()
578 */
579 function _gallery_user_resolve_mail(&$mail, $username, &$messages) {
580 // Replace all invalid chars in the username
581 $invalid_search = array(' ', '!', '#', '$', '%', '&', '*', '+', '/', '=', '?', '`', '|', '{', '}', '~', '\'');
582 $invalid_replace = array_fill(0, count($invalid_search), '_');
583 $user = str_replace($invalid_search, $invalid_replace, $username);
584 // Generate a new mail address using a simple 'username_#@drupaldomain' naming schema
585 // (=> username@drupaldomain, username_1@drupaldomain, username_2@drupaldomain, ...)
586 $i = 0;
587 do {
588 $newmail = $user . (($i > 0) ? ('_'. $i) : '') .'@'. $_SERVER['HTTP_HOST'];
589 $error = (db_result(db_query("SELECT COUNT(uid) FROM {users} WHERE uid != 0 AND LOWER(mail) = LOWER('%s')", $newmail)) > 0);
590 } while ($error && $i++ < 10);
591 // Do we have a new mail address?
592 if (!$error) {
593 $messages[] = t('G2 User Import (name: \':name\'): e-mail address :oldmail changed to :newmail.',
594 array(':name' => $username, ':oldmail' => $mail, ':newmail' => $newmail));
595 $mail = $newmail;
596 }
597 }
598
599 /**
600 * Function _gallery_user_map().
601 * (fetch 'GalleryUser' entries from G2 'ExternalIdMap')
602 */
603 function _gallery_user_map($ids = array(), $inverse = FALSE) {
604 // g2Id => externalId (default)
605 $ids = is_array($ids) ? $ids : array($ids);
606 $match = array('entityType' => 'GalleryUser');
607 if (count($ids) > 0) {
608 if ($inverse) {
609 $match['externalId'] = $ids;
610 }
611 else {
612 $match['entityId'] = $ids;
613 }
614 }
615 // Fetch the map entries
616 list($ret, $resultMap) = GalleryCoreApi::getMapEntry('ExternalIdMap', array('externalId', 'entityId'), $match);
617 if ($ret) {
618 gallery_error(t('Error fetching \'GalleryUser\' entries from \'ExternalIdMap\''), $ret);
619 return FALSE;
620 }
621 // Iterate over the results
622 $g2_extIdMap = array();
623 while (($row = $resultMap->nextResult()) !== FALSE) {
624 $g2_extIdMap[($inverse ? $row[0] : $row[1])] = ($inverse ? $row[1] : $row[0]);
625 }
626
627 return $g2_extIdMap;
628 }
629
630 /**
631 * Function _gallery_user_drupal_users().
632 * (fetch all existing Drupal users (uid => username))
633 */
634 function _gallery_user_drupal_users() {
635 $users = array();
636 $result = db_query("SELECT uid, name FROM {users} WHERE uid > 0");
637 while ($user = db_fetch_object($result)) {
638 $users[$user->uid] = $user->name;
639 }
640
641 return $users;
642 }

  ViewVC Help
Powered by ViewVC 1.1.2