Various phpdoc fixes
[civicrm-core.git] / CRM / Utils / System / Drupal6.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Drupal specific stuff goes here.
20 */
21 class CRM_Utils_System_Drupal6 extends CRM_Utils_System_DrupalBase {
22
23 /**
24 * Theme output.
25 *
26 * If we are using a theming system, invoke theme, else just print the content.
27 *
28 * @param string $content
29 * The content that will be themed.
30 * @param bool $print
31 * Are we displaying to the screen or bypassing theming?.
32 * @param bool $maintenance
33 * For maintenance mode.
34 *
35 * @return null|string
36 * prints content on stdout
37 */
38 public function theme(&$content, $print = FALSE, $maintenance = FALSE) {
39 // TODO: Simplify; this was copied verbatim from CiviCRM 3.4's multi-UF theming function, but that's more complex than necessary
40 if (function_exists('theme') && !$print) {
41 if ($maintenance) {
42 drupal_set_breadcrumb('');
43 drupal_maintenance_theme();
44 }
45
46 // Arg 3 for D6 theme() is "show_blocks". Previously, we passed
47 // through a badly named variable ("$args") which was almost always
48 // TRUE (except on fatal error screen). However, this feature is
49 // non-functional on D6 default themes, was purposefully removed from
50 // D7, has no analog in other our other CMS's, and clutters the code.
51 // Hard-wiring to TRUE should be OK.
52 $out = theme('page', $content, TRUE);
53 }
54 else {
55 $out = $content;
56 }
57
58 print $out;
59 return NULL;
60 }
61
62 /**
63 * Create user.
64 *
65 * @inheritDoc
66 */
67 public function createUser(&$params, $mail) {
68 $form_state = [];
69 $form_state['values'] = [
70 'name' => $params['cms_name'],
71 'mail' => $params[$mail],
72 'op' => 'Create new account',
73 ];
74
75 $admin = user_access('administer users');
76 if (!variable_get('user_email_verification', TRUE) || $admin) {
77 $form_state['values']['pass']['pass1'] = $params['cms_pass'];
78 $form_state['values']['pass']['pass2'] = $params['cms_pass'];
79 }
80
81 $config = CRM_Core_Config::singleton();
82
83 // we also need to redirect b
84 $config->inCiviCRM = TRUE;
85
86 $form = drupal_retrieve_form('user_register', $form_state);
87 $form['#post'] = $form_state['values'];
88 drupal_prepare_form('user_register', $form, $form_state);
89
90 // remove the captcha element from the form prior to processing
91 unset($form['captcha']);
92
93 drupal_process_form('user_register', $form, $form_state);
94
95 $config->inCiviCRM = FALSE;
96
97 if (form_get_errors() || !isset($form_state['user'])) {
98 return FALSE;
99 }
100 return $form_state['user']->uid;
101 }
102
103 /**
104 * @inheritDoc
105 */
106 public function updateCMSName($ufID, $ufName) {
107 // CRM-5555
108 if (function_exists('user_load')) {
109 $user = user_load(['uid' => $ufID]);
110 if ($user->mail != $ufName) {
111 user_save($user, ['mail' => $ufName]);
112 $user = user_load(['uid' => $ufID]);
113 }
114 }
115 }
116
117 /**
118 * Check if username and email exists in the drupal db.
119 *
120 * @param array $params
121 * Array of name and mail values.
122 * @param array $errors
123 * Array of errors.
124 * @param string $emailName
125 * Field label for the 'email'.
126 */
127 public function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') {
128 $config = CRM_Core_Config::singleton();
129
130 $dao = new CRM_Core_DAO();
131 $name = $dao->escape(CRM_Utils_Array::value('name', $params));
132 $email = $dao->escape(CRM_Utils_Array::value('mail', $params));
133 _user_edit_validate(NULL, $params);
134 $errors = form_get_errors();
135 if ($errors) {
136 if (!empty($errors['name'])) {
137 $errors['cms_name'] = $errors['name'];
138 }
139 if (!empty($errors['mail'])) {
140 $errors[$emailName] = $errors['mail'];
141 }
142 // also unset drupal messages to avoid twice display of errors
143 unset($_SESSION['messages']);
144 }
145
146 // Do the name check manually.
147 $nameError = user_validate_name($params['name']);
148 if ($nameError) {
149 $errors['cms_name'] = $nameError;
150 }
151
152 // LOWER in query below roughly translates to 'hurt my database without deriving any benefit' See CRM-19811.
153 $sql = "
154 SELECT name, mail
155 FROM {users}
156 WHERE (LOWER(name) = LOWER('$name')) OR (LOWER(mail) = LOWER('$email'))
157 ";
158
159 $result = db_query($sql);
160 $row = db_fetch_array($result);
161 if (!$row) {
162 return;
163 }
164
165 $user = NULL;
166
167 if (!empty($row)) {
168 $dbName = $row['name'] ?? NULL;
169 $dbEmail = $row['mail'] ?? NULL;
170 if (strtolower($dbName) == strtolower($name)) {
171 $errors['cms_name'] = ts('The username %1 is already taken. Please select another username.',
172 [1 => $name]
173 );
174 }
175 if (strtolower($dbEmail) == strtolower($email)) {
176 if (empty($email)) {
177 $errors[$emailName] = ts('You cannot create an email account for a contact with no email',
178 [1 => $email]
179 );
180 }
181 else {
182 $errors[$emailName] = ts('This email %1 already has an account associated with it. Please select another email.',
183 [1 => $email]
184 );
185 }
186 }
187 }
188 }
189
190 /**
191 * @inheritDoc
192 */
193 public function setTitle($title, $pageTitle = NULL) {
194 if (!$pageTitle) {
195 $pageTitle = $title;
196 }
197 if (arg(0) == 'civicrm') {
198 //set drupal title
199 drupal_set_title($pageTitle);
200 }
201 }
202
203 /**
204 * @inheritDoc
205 */
206 public function appendBreadCrumb($breadCrumbs) {
207 $breadCrumb = drupal_get_breadcrumb();
208
209 if (is_array($breadCrumbs)) {
210 foreach ($breadCrumbs as $crumbs) {
211 if (stripos($crumbs['url'], 'id%%')) {
212 $args = ['cid', 'mid'];
213 foreach ($args as $a) {
214 $val = CRM_Utils_Request::retrieve($a, 'Positive', CRM_Core_DAO::$_nullObject,
215 FALSE, NULL, $_GET
216 );
217 if ($val) {
218 $crumbs['url'] = str_ireplace("%%{$a}%%", $val, $crumbs['url']);
219 }
220 }
221 }
222 $breadCrumb[] = "<a href=\"{$crumbs['url']}\">{$crumbs['title']}</a>";
223 }
224 }
225 drupal_set_breadcrumb($breadCrumb);
226 }
227
228 /**
229 * @inheritDoc
230 */
231 public function resetBreadCrumb() {
232 $bc = [];
233 drupal_set_breadcrumb($bc);
234 }
235
236 /**
237 * Append a string to the head of the html file.
238 *
239 * @param string $head
240 * The new string to be appended.
241 */
242 public function addHTMLHead($head) {
243 drupal_set_html_head($head);
244 }
245
246 /**
247 * Add a css file.
248 *
249 * @param string $url
250 * absolute path to file
251 * @param string $region
252 * location within the document: 'html-header', 'page-header', 'page-footer'.
253 *
254 * Note: This function is not to be called directly
255 * @see CRM_Core_Region::render()
256 *
257 * @return bool
258 * TRUE if we support this operation in this CMS, FALSE otherwise
259 */
260 public function addStyleUrl($url, $region) {
261 if ($region != 'html-header' || !$this->formatResourceUrl($url)) {
262 return FALSE;
263 }
264 drupal_add_css($url);
265 return TRUE;
266 }
267
268 /**
269 * @inheritDoc
270 */
271 public function mapConfigToSSL() {
272 global $base_url;
273 $base_url = str_replace('http://', 'https://', $base_url);
274 }
275
276 /**
277 * Get the name of the table that stores the user details.
278 *
279 * @return string
280 */
281 protected function getUsersTableName() {
282 $userFrameworkUsersTableName = Civi::settings()->get('userFrameworkUsersTableName');
283 if (empty($userFrameworkUsersTableName)) {
284 $userFrameworkUsersTableName = 'users';
285 }
286 return $userFrameworkUsersTableName;
287 }
288
289 /**
290 * @inheritDoc
291 */
292 public function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) {
293 //@todo this 'PEAR-y' stuff is only required when bookstrap is not being loaded which is rare
294 // if ever now.
295 // probably if bootstrap is loaded this call
296 // CRM_Utils_System::loadBootStrap($bootStrapParams, TRUE, TRUE, $realPath); would be
297 // sufficient to do what this fn does. It does exist as opposed to return which might need some hanky-panky to make
298 // safe in the unknown situation where authenticate might be called & it is important that
299 // false is returned
300 require_once 'DB.php';
301
302 $config = CRM_Core_Config::singleton();
303
304 $ufDSN = CRM_Utils_SQL::autoSwitchDSN($config->userFrameworkDSN);
305 try {
306 $dbDrupal = DB::connect($ufDSN);
307 }
308 catch (Exception $e) {
309 throw new CRM_Core_Exception("Cannot connect to drupal db via $ufDSN, " . $e->getMessage());
310 }
311
312 $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower';
313 $dbpassword = md5($password);
314 $name = $dbDrupal->escapeSimple($strtolower($name));
315 $userFrameworkUsersTableName = $this->getUsersTableName();
316 $sql = 'SELECT u.* FROM ' . $userFrameworkUsersTableName . " u WHERE LOWER(u.name) = '$name' AND u.pass = '$dbpassword' AND u.status = 1";
317 $query = $dbDrupal->query($sql);
318
319 $user = NULL;
320 // need to change this to make sure we matched only one row
321 while ($row = $query->fetchRow(DB_FETCHMODE_ASSOC)) {
322 CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, $row['uid'], $row['mail'], 'Drupal');
323 $contactID = CRM_Core_BAO_UFMatch::getContactId($row['uid']);
324 if (!$contactID) {
325 return FALSE;
326 }
327 else {
328 //success
329 if ($loadCMSBootstrap) {
330 $bootStrapParams = [];
331 if ($name && $password) {
332 $bootStrapParams = [
333 'name' => $name,
334 'pass' => $password,
335 ];
336 }
337 CRM_Utils_System::loadBootStrap($bootStrapParams, TRUE, TRUE, $realPath);
338 }
339 return [$contactID, $row['uid'], mt_rand()];
340 }
341 }
342 return FALSE;
343 }
344
345 /**
346 * @inheritDoc
347 */
348 public function loadUser($username) {
349 global $user;
350 $user = user_load(['name' => $username]);
351 if (empty($user->uid)) {
352 return FALSE;
353 }
354
355 $uid = $user->uid;
356 $contact_id = CRM_Core_BAO_UFMatch::getContactId($uid);
357
358 // lets store contact id and user id in session
359 $session = CRM_Core_Session::singleton();
360 $session->set('ufID', $uid);
361 $session->set('userID', $contact_id);
362 return TRUE;
363 }
364
365 /**
366 * Perform any post login activities required by the UF -
367 * e.g. for drupal : records a watchdog message about the new session,
368 * saves the login timestamp, calls hook_user op 'login' and generates a new session.
369 *
370 * @param array $params
371 *
372 * FIXME: Document values accepted/required by $params
373 */
374 public function userLoginFinalize($params = []) {
375 user_authenticate_finalize($params);
376 }
377
378 /**
379 * Determine the native ID of the CMS user.
380 *
381 * @param string $username
382 * @return int|null
383 */
384 public function getUfId($username) {
385 $user = user_load(['name' => $username]);
386 if (empty($user->uid)) {
387 return NULL;
388 }
389 return $user->uid;
390 }
391
392 /**
393 * @inheritDoc
394 */
395 public function logout() {
396 module_load_include('inc', 'user', 'user.pages');
397 return user_logout();
398 }
399
400 /**
401 * Load drupal bootstrap.
402 *
403 * @param array $params
404 * Either uid, or name & pass.
405 * @param bool $loadUser
406 * Boolean Require CMS user load.
407 * @param bool $throwError
408 * If true, print error on failure and exit.
409 * @param bool|string $realPath path to script
410 *
411 * @return bool
412 */
413 public function loadBootStrap($params = [], $loadUser = TRUE, $throwError = TRUE, $realPath = NULL) {
414 //take the cms root path.
415 $cmsPath = $this->cmsRootPath($realPath);
416
417 if (!file_exists("$cmsPath/includes/bootstrap.inc")) {
418 if ($throwError) {
419 echo '<br />Sorry, could not locate bootstrap.inc\n';
420 exit();
421 }
422 return FALSE;
423 }
424 // load drupal bootstrap
425 chdir($cmsPath);
426 define('DRUPAL_ROOT', $cmsPath);
427
428 // For drupal multi-site CRM-11313
429 if ($realPath && strpos($realPath, 'sites/all/modules/') === FALSE) {
430 preg_match('@sites/([^/]*)/modules@s', $realPath, $matches);
431 if (!empty($matches[1])) {
432 $_SERVER['HTTP_HOST'] = $matches[1];
433 }
434 }
435 require_once 'includes/bootstrap.inc';
436 // @ to suppress notices eg 'DRUPALFOO already defined'.
437 @drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
438
439 // explicitly setting error reporting, since we cannot handle drupal related notices
440 error_reporting(1);
441 if (!function_exists('module_exists') || !module_exists('civicrm')) {
442 if ($throwError) {
443 echo '<br />Sorry, could not load drupal bootstrap.';
444 exit();
445 }
446 return FALSE;
447 }
448
449 // seems like we've bootstrapped drupal
450 $config = CRM_Core_Config::singleton();
451
452 // lets also fix the clean url setting
453 // CRM-6948
454 $config->cleanURL = (int) variable_get('clean_url', '0');
455
456 // we need to call the config hook again, since we now know
457 // all the modules that are listening on it, does not apply
458 // to J! and WP as yet
459 // CRM-8655
460 CRM_Utils_Hook::config($config);
461
462 if (!$loadUser) {
463 return TRUE;
464 }
465 global $user;
466 // If $uid is passed in, authentication has been done already.
467 $uid = $params['uid'] ?? NULL;
468 if (!$uid) {
469 //load user, we need to check drupal permissions.
470 $name = CRM_Utils_Array::value('name', $params, FALSE) ? $params['name'] : trim(CRM_Utils_Array::value('name', $_REQUEST));
471 $pass = CRM_Utils_Array::value('pass', $params, FALSE) ? $params['pass'] : trim(CRM_Utils_Array::value('pass', $_REQUEST));
472
473 if ($name) {
474 $user = user_authenticate(['name' => $name, 'pass' => $pass]);
475 if (!$user->uid) {
476 if ($throwError) {
477 echo '<br />Sorry, unrecognized username or password.';
478 exit();
479 }
480 return FALSE;
481 }
482 else {
483 return TRUE;
484 }
485 }
486 }
487
488 if ($uid) {
489 $account = user_load($uid);
490 if ($account && $account->uid) {
491 $user = $account;
492 return TRUE;
493 }
494 }
495
496 if ($throwError) {
497 echo '<br />Sorry, can not load CMS user account.';
498 exit();
499 }
500
501 // CRM-6948: When using loadBootStrap, it's implicit that CiviCRM has already loaded its settings
502 // which means that define(CIVICRM_CLEANURL) was correctly set.
503 // So we correct it
504 $config = CRM_Core_Config::singleton();
505 $config->cleanURL = (int) variable_get('clean_url', '0');
506
507 // CRM-8655: Drupal wasn't available during bootstrap, so hook_civicrm_config never executes
508 CRM_Utils_Hook::config($config);
509
510 return FALSE;
511 }
512
513 /**
514 * Get CMS root path.
515 *
516 * @param string $scriptFilename
517 *
518 * @return null|string
519 */
520 public function cmsRootPath($scriptFilename = NULL) {
521 $cmsRoot = $valid = NULL;
522
523 if (!is_null($scriptFilename)) {
524 $path = $scriptFilename;
525 }
526 else {
527 $path = $_SERVER['SCRIPT_FILENAME'];
528 }
529
530 if (function_exists('drush_get_context')) {
531 // drush anyway takes care of multisite install etc
532 return drush_get_context('DRUSH_DRUPAL_ROOT');
533 }
534
535 global $civicrm_paths;
536 if (!empty($civicrm_paths['cms.root']['path'])) {
537 return $civicrm_paths['cms.root']['path'];
538 }
539
540 // CRM-7582
541 $pathVars = explode('/',
542 str_replace('//', '/',
543 str_replace('\\', '/', $path)
544 )
545 );
546
547 //lets store first var,
548 //need to get back for windows.
549 $firstVar = array_shift($pathVars);
550
551 //lets remove sript name to reduce one iteration.
552 array_pop($pathVars);
553
554 //CRM-7429 --do check for upper most 'includes' dir,
555 //which would effectually work for multisite installation.
556 do {
557 $cmsRoot = $firstVar . '/' . implode('/', $pathVars);
558 $cmsIncludePath = "$cmsRoot/includes";
559 // Stop if we found bootstrap.
560 if (file_exists("$cmsIncludePath/bootstrap.inc")) {
561 $valid = TRUE;
562 break;
563 }
564 //remove one directory level.
565 array_pop($pathVars);
566 } while (count($pathVars));
567
568 return ($valid) ? $cmsRoot : NULL;
569 }
570
571 /**
572 * @inheritDoc
573 */
574 public function isUserLoggedIn() {
575 $isloggedIn = FALSE;
576 if (function_exists('user_is_logged_in')) {
577 $isloggedIn = user_is_logged_in();
578 }
579
580 return $isloggedIn;
581 }
582
583 /**
584 * @inheritDoc
585 */
586 public function getLoggedInUfID() {
587 $ufID = NULL;
588 if (function_exists('user_is_logged_in') &&
589 user_is_logged_in() &&
590 function_exists('user_uid_optional_to_arg')
591 ) {
592 $ufID = user_uid_optional_to_arg([]);
593 }
594
595 return $ufID;
596 }
597
598 /**
599 * @inheritDoc
600 */
601 public function languageNegotiationURL($url, $addLanguagePart = TRUE, $removeLanguagePart = FALSE) {
602 if (empty($url)) {
603 return $url;
604 }
605
606 //up to d6 only, already we have code in place for d7
607 $config = CRM_Core_Config::singleton();
608 if (function_exists('variable_get') &&
609 module_exists('locale')
610 ) {
611 global $language;
612
613 //get the mode.
614 $mode = variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE);
615
616 //url prefix / path.
617 if (isset($language->prefix) &&
618 $language->prefix &&
619 in_array($mode, [
620 LANGUAGE_NEGOTIATION_PATH,
621 LANGUAGE_NEGOTIATION_PATH_DEFAULT,
622 ])
623 ) {
624
625 if ($addLanguagePart) {
626 $url .= $language->prefix . '/';
627 }
628 if ($removeLanguagePart) {
629 $url = str_replace("/{$language->prefix}/", '/', $url);
630 }
631 }
632 if (isset($language->domain) &&
633 $language->domain &&
634 $mode == LANGUAGE_NEGOTIATION_DOMAIN
635 ) {
636
637 if ($addLanguagePart) {
638 $url = CRM_Utils_File::addTrailingSlash($language->domain, '/');
639 }
640 if ($removeLanguagePart && defined('CIVICRM_UF_BASEURL')) {
641 $url = str_replace('\\', '/', $url);
642 $parseUrl = parse_url($url);
643
644 //kinda hackish but not sure how to do it right
645 //hope http_build_url() will help at some point.
646 if (is_array($parseUrl) && !empty($parseUrl)) {
647 $urlParts = explode('/', $url);
648 $hostKey = array_search($parseUrl['host'], $urlParts);
649 $ufUrlParts = parse_url(CIVICRM_UF_BASEURL);
650 $urlParts[$hostKey] = $ufUrlParts['host'];
651 $url = implode('/', $urlParts);
652 }
653 }
654 }
655 }
656
657 return $url;
658 }
659
660 /**
661 * Find any users/roles/security-principals with the given permission
662 * and replace it with one or more permissions.
663 *
664 * @param string $oldPerm
665 * @param array $newPerms
666 * Array, strings.
667 */
668 public function replacePermission($oldPerm, $newPerms) {
669 $roles = user_roles(FALSE, $oldPerm);
670 foreach ($roles as $rid => $roleName) {
671 $permList = db_result(db_query('SELECT perm FROM {permission} WHERE rid = %d', $rid));
672 $perms = drupal_map_assoc(explode(', ', $permList));
673 unset($perms[$oldPerm]);
674 $perms = $perms + drupal_map_assoc($newPerms);
675 $permList = implode(', ', $perms);
676 db_query('UPDATE {permission} SET perm = "%s" WHERE rid = %d', $permList, $rid);
677 /* @codingStandardsIgnoreStart
678 if ( ! empty( $roles ) ) {
679 $rids = implode(',', array_keys($roles));
680 db_query( 'UPDATE {permission} SET perm = CONCAT( perm, \', edit all events\') WHERE rid IN (' . implode(',', array_keys($roles)) . ')' );
681 db_query( "UPDATE {permission} SET perm = REPLACE( perm, '%s', '%s' ) WHERE rid IN ($rids)",
682 $oldPerm, implode(', ', $newPerms) );
683 @codingStandardsIgnoreEnd */
684 }
685 }
686
687 /**
688 * @inheritDoc
689 */
690 public function getModules() {
691 $result = [];
692 $q = db_query('SELECT name, status FROM {system} WHERE type = \'module\' AND schema_version <> -1');
693 while ($row = db_fetch_object($q)) {
694 $result[] = new CRM_Core_Module('drupal.' . $row->name, $row->status == 1);
695 }
696 return $result;
697 }
698
699 /**
700 * @inheritDoc
701 */
702 public function getLoginURL($destination = '') {
703 $config = CRM_Core_Config::singleton();
704 $loginURL = $config->userFrameworkBaseURL;
705 $loginURL .= 'user';
706 if (!empty($destination)) {
707 // append destination so user is returned to form they came from after login
708 $loginURL .= '?destination=' . urlencode($destination);
709 }
710 return $loginURL;
711 }
712
713 /**
714 * Wrapper for og_membership creation.
715 *
716 * @param int $ogID
717 * Organic Group ID.
718 * @param int $drupalID
719 * Drupal User ID.
720 */
721 public function og_membership_create($ogID, $drupalID) {
722 og_save_subscription($ogID, $drupalID, ['is_active' => 1]);
723 }
724
725 /**
726 * Wrapper for og_membership deletion.
727 *
728 * @param int $ogID
729 * Organic Group ID.
730 * @param int $drupalID
731 * Drupal User ID.
732 */
733 public function og_membership_delete($ogID, $drupalID) {
734 og_delete_subscription($ogID, $drupalID);
735 }
736
737 /**
738 * @inheritDoc
739 */
740 public function getTimeZoneString() {
741 global $user;
742 // Note that 0 is a valid timezone (GMT) so we use strlen not empty to check.
743 if (variable_get('configurable_timezones', 1) && $user->uid && isset($user->timezone) && strlen($user->timezone)) {
744 $timezone = $user->timezone;
745 }
746 else {
747 $timezone = variable_get('date_default_timezone', NULL);
748 }
749
750 // Retrieved timezone will be represented as GMT offset in seconds but, according
751 // to the doc for the overridden method, ought to be returned as a region string
752 // (e.g., America/Havana).
753 if (strlen($timezone)) {
754 $timezone = timezone_name_from_abbr("", (int) $timezone);
755 }
756
757 if (!$timezone) {
758 $timezone = parent::getTimeZoneString();
759 }
760
761 return $timezone;
762 }
763
764 /**
765 * @inheritDoc
766 */
767 public function setHttpHeader($name, $value) {
768 drupal_set_header("$name: $value");
769 }
770
771 /**
772 * @inheritDoc
773 */
774 public function synchronizeUsers() {
775 $config = CRM_Core_Config::singleton();
776 if (PHP_SAPI != 'cli') {
777 set_time_limit(300);
778 }
779 $rows = [];
780 $id = 'uid';
781 $mail = 'mail';
782 $name = 'name';
783
784 $result = db_query("SELECT uid, mail, name FROM {users} where mail != ''");
785
786 while ($row = db_fetch_array($result)) {
787 $rows[] = $row;
788 }
789
790 $user = new StdClass();
791 $uf = $config->userFramework;
792 $contactCount = 0;
793 $contactCreated = 0;
794 $contactMatching = 0;
795 foreach ($rows as $row) {
796 $user->$id = $row[$id];
797 $user->$mail = $row[$mail];
798 $user->$name = $row[$name];
799 $contactCount++;
800 if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, $row[$id], $row[$mail], $uf, 1, 'Individual', TRUE)) {
801 $contactCreated++;
802 }
803 else {
804 $contactMatching++;
805 }
806 }
807
808 return [
809 'contactCount' => $contactCount,
810 'contactMatching' => $contactMatching,
811 'contactCreated' => $contactCreated,
812 ];
813 }
814
815 /**
816 * Return the CMS-specific url for its permissions page
817 * @return array
818 */
819 public function getCMSPermissionsUrlParams() {
820 return ['ufAccessURL' => url('admin/user/permissions')];
821 }
822
823 }