Merge pull request #8092 from eileenmcnaughton/CRM-18368
[civicrm-core.git] / CRM / Utils / System / Backdrop.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2016
32 */
33
34 /**
35 * Backdrop-specific logic that differs from Drupal.
36 */
37 class CRM_Utils_System_Backdrop extends CRM_Utils_System_DrupalBase {
38
39 /**
40 * @inheritDoc
41 */
42 public function createUser(&$params, $mail) {
43 $form_state = form_state_defaults();
44
45 $form_state['input'] = array(
46 'name' => $params['cms_name'],
47 'mail' => $params[$mail],
48 'op' => 'Create new account',
49 );
50
51 $admin = user_access('administer users');
52 if (!config_get('system.core', 'user_email_verification') || $admin) {
53 $form_state['input']['pass'] = array('pass1' => $params['cms_pass'], 'pass2' => $params['cms_pass']);
54 }
55
56 if (!empty($params['notify'])) {
57 $form_state['input']['notify'] = $params['notify'];
58 }
59
60 $form_state['rebuild'] = FALSE;
61 $form_state['programmed'] = TRUE;
62 $form_state['complete form'] = FALSE;
63 $form_state['method'] = 'post';
64 $form_state['build_info']['args'] = array();
65 /*
66 * if we want to submit this form more than once in a process (e.g. create more than one user)
67 * we must force it to validate each time for this form. Otherwise it will not validate
68 * subsequent submissions and the manner in which the password is passed in will be invalid
69 */
70 $form_state['must_validate'] = TRUE;
71 $config = CRM_Core_Config::singleton();
72
73 // we also need to redirect b
74 $config->inCiviCRM = TRUE;
75
76 $form = drupal_retrieve_form('user_register_form', $form_state);
77 $form_state['process_input'] = 1;
78 $form_state['submitted'] = 1;
79 $form['#array_parents'] = array();
80 $form['#tree'] = FALSE;
81 drupal_process_form('user_register_form', $form, $form_state);
82
83 $config->inCiviCRM = FALSE;
84
85 if (form_get_errors()) {
86 return FALSE;
87 }
88 return $form_state['user']->uid;
89 }
90
91 /**
92 * @inheritDoc
93 */
94 public function updateCMSName($ufID, $email) {
95 // CRM-5555
96 if (function_exists('user_load')) {
97 $user = user_load($ufID);
98 if ($user->mail != $email) {
99 $user->mail = $email;
100 $user->save();
101 }
102 }
103 }
104
105 /**
106 * Check if username and email exists in the drupal db.
107 *
108 * @param array $params
109 * Array of name and mail values.
110 * @param array $errors
111 * Array of errors.
112 * @param string $emailName
113 * Field label for the 'email'.
114 */
115 public static function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') {
116 $errors = form_get_errors();
117 if ($errors) {
118 // unset drupal messages to avoid twice display of errors
119 unset($_SESSION['messages']);
120 }
121
122 if (!empty($params['name'])) {
123 if ($nameError = user_validate_name($params['name'])) {
124 $errors['cms_name'] = $nameError;
125 }
126 else {
127 $uid = db_query("SELECT uid FROM {users} WHERE name = :name", array(':name' => $params['name']))->fetchField();
128 if ((bool) $uid) {
129 $errors['cms_name'] = ts('The username %1 is already taken. Please select another username.', array(1 => $params['name']));
130 }
131 }
132 }
133
134 if (!empty($params['mail'])) {
135 if (!valid_email_address($params['mail'])) {
136 $errors[$emailName] = ts('The e-mail address %1 is not valid.', array('%1' => $params['mail']));
137 }
138 else {
139 $uid = db_query("SELECT uid FROM {users} WHERE mail = :mail", array(':mail' => $params['mail']))->fetchField();
140 if ((bool) $uid) {
141 $resetUrl = url('user/password');
142 $errors[$emailName] = ts('The email address %1 already has an account associated with it. <a href="%2">Have you forgotten your password?</a>',
143 array(1 => $params['mail'], 2 => $resetUrl)
144 );
145 }
146 }
147 }
148 }
149
150 /**
151 * @inheritDoc
152 */
153 public function getLoginURL($destination = '') {
154 $query = $destination ? array('destination' => $destination) : array();
155 return url('user', array('query' => $query, 'absolute' => TRUE));
156 }
157
158 /**
159 * @inheritDoc
160 */
161 public function setTitle($title, $pageTitle = NULL) {
162 if (arg(0) == 'civicrm') {
163 if (!$pageTitle) {
164 $pageTitle = $title;
165 }
166
167 drupal_set_title($pageTitle, PASS_THROUGH);
168 }
169 }
170
171 /**
172 * @inheritDoc
173 */
174 public function appendBreadCrumb($breadCrumbs) {
175 $breadCrumb = drupal_get_breadcrumb();
176
177 if (is_array($breadCrumbs)) {
178 foreach ($breadCrumbs as $crumbs) {
179 if (stripos($crumbs['url'], 'id%%')) {
180 $args = array('cid', 'mid');
181 foreach ($args as $a) {
182 $val = CRM_Utils_Request::retrieve($a, 'Positive', CRM_Core_DAO::$_nullObject,
183 FALSE, NULL, $_GET
184 );
185 if ($val) {
186 $crumbs['url'] = str_ireplace("%%{$a}%%", $val, $crumbs['url']);
187 }
188 }
189 }
190 $breadCrumb[] = "<a href=\"{$crumbs['url']}\">{$crumbs['title']}</a>";
191 }
192 }
193 drupal_set_breadcrumb($breadCrumb);
194 }
195
196 /**
197 * @inheritDoc
198 */
199 public function resetBreadCrumb() {
200 $bc = array();
201 drupal_set_breadcrumb($bc);
202 }
203
204 /**
205 * @inheritDoc
206 */
207 public function addHTMLHead($header) {
208 static $count = 0;
209 if (!empty($header)) {
210 $key = 'civi_' . ++$count;
211 $data = array(
212 '#type' => 'markup',
213 '#markup' => $header,
214 );
215 drupal_add_html_head($data, $key);
216 }
217 }
218
219 /**
220 * @inheritDoc
221 */
222 public function addScriptUrl($url, $region) {
223 $params = array('group' => JS_LIBRARY, 'weight' => 10);
224 switch ($region) {
225 case 'html-header':
226 case 'page-footer':
227 $params['scope'] = substr($region, 5);
228 break;
229
230 default:
231 return FALSE;
232 }
233 // If the path is within the drupal directory we can use the more efficient 'file' setting
234 $params['type'] = $this->formatResourceUrl($url) ? 'file' : 'external';
235 drupal_add_js($url, $params);
236 return TRUE;
237 }
238
239 /**
240 * @inheritDoc
241 */
242 public function addScript($code, $region) {
243 $params = array('type' => 'inline', 'group' => JS_LIBRARY, 'weight' => 10);
244 switch ($region) {
245 case 'html-header':
246 case 'page-footer':
247 $params['scope'] = substr($region, 5);
248 break;
249
250 default:
251 return FALSE;
252 }
253 drupal_add_js($code, $params);
254 return TRUE;
255 }
256
257 /**
258 * @inheritDoc
259 */
260 public function addStyleUrl($url, $region) {
261 if ($region != 'html-header') {
262 return FALSE;
263 }
264 $params = array();
265 // If the path is within the drupal directory we can use the more efficient 'file' setting
266 $params['type'] = $this->formatResourceUrl($url) ? 'file' : 'external';
267 drupal_add_css($url, $params);
268 return TRUE;
269 }
270
271 /**
272 * @inheritDoc
273 */
274 public function addStyle($code, $region) {
275 if ($region != 'html-header') {
276 return FALSE;
277 }
278 $params = array('type' => 'inline');
279 drupal_add_css($code, $params);
280 return TRUE;
281 }
282
283 /**
284 * @inheritDoc
285 */
286 public function mapConfigToSSL() {
287 global $base_url;
288 $base_url = str_replace('http://', 'https://', $base_url);
289 }
290
291 protected function getUsersTableName() {
292 $userFrameworkUsersTableName = Civi::settings()->get('userFrameworkUsersTableName');
293 if (empty($userFrameworkUsersTableName)) {
294 $userFrameworkUsersTableName = 'users';
295 }
296 return $userFrameworkUsersTableName;
297 }
298
299 /**
300 * @inheritDoc
301 */
302 public function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) {
303 $config = CRM_Core_Config::singleton();
304
305 $dbBackdrop = DB::connect($config->userFrameworkDSN);
306 if (DB::isError($dbBackdrop)) {
307 CRM_Core_Error::fatal("Cannot connect to Backdrop database via $config->userFrameworkDSN, " . $dbBackdrop->getMessage());
308 }
309
310 $account = $userUid = $userMail = NULL;
311 if ($loadCMSBootstrap) {
312 $bootStrapParams = array();
313 if ($name && $password) {
314 $bootStrapParams = array(
315 'name' => $name,
316 'pass' => $password,
317 );
318 }
319 CRM_Utils_System::loadBootStrap($bootStrapParams, TRUE, TRUE, $realPath);
320
321 global $user;
322 if ($user) {
323 $userUid = $user->uid;
324 $userMail = $user->mail;
325 }
326 }
327 else {
328 // CRM-8638
329 // SOAP cannot load drupal bootstrap and hence we do it the old way
330 // Contact CiviSMTP folks if we run into issues with this :)
331 $cmsPath = $this->cmsRootPath();
332
333 require_once "$cmsPath/core/includes/bootstrap.inc";
334 require_once "$cmsPath/core/includes/password.inc";
335
336 $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower';
337 $name = $dbBackdrop->escapeSimple($strtolower($name));
338 $userFrameworkUsersTableName = $this->getUsersTableName();
339 $sql = "
340 SELECT u.*
341 FROM {$userFrameworkUsersTableName} u
342 WHERE LOWER(u.name) = '$name'
343 AND u.status = 1
344 ";
345
346 $query = $dbBackdrop->query($sql);
347 $row = $query->fetchRow(DB_FETCHMODE_ASSOC);
348
349 if ($row) {
350 $fakeAccount = backdrop_anonymous_user();
351 $fakeAccount->name = $name;
352 $fakeAccount->pass = $row['pass'];
353 $passwordCheck = user_check_password($password, $fakeAccount);
354 if ($passwordCheck) {
355 $userUid = $row['uid'];
356 $userMail = $row['mail'];
357 }
358 }
359 }
360
361 if ($userUid && $userMail) {
362 CRM_Core_BAO_UFMatch::synchronizeUFMatch($account, $userUid, $userMail, 'Backdrop');
363 $contactID = CRM_Core_BAO_UFMatch::getContactId($userUid);
364 if (!$contactID) {
365 return FALSE;
366 }
367 return array($contactID, $userUid, mt_rand());
368 }
369 return FALSE;
370 }
371
372 /**
373 * @inheritDoc
374 */
375 public function loadUser($username) {
376 global $user;
377
378 $user = user_load_by_name($username);
379
380 if (empty($user->uid)) {
381 return FALSE;
382 }
383
384 $uid = $user->uid;
385 $contact_id = CRM_Core_BAO_UFMatch::getContactId($uid);
386
387 // lets store contact id and user id in session
388 $session = CRM_Core_Session::singleton();
389 $session->set('ufID', $uid);
390 $session->set('userID', $contact_id);
391 return TRUE;
392 }
393
394 /**
395 * Perform any post login activities required by the UF -
396 * For Backdrop this could mean recording a watchdog message about the new
397 * session, saving the login timestamp, calling hook_user_login(), etc.
398 *
399 * @param array $params
400 * The array of form values submitted by the user.
401 */
402 public function userLoginFinalize($params = array()) {
403 user_login_finalize($params);
404 }
405
406 /**
407 * Determine the native ID of the CMS user.
408 *
409 * @param string $username
410 * @return int|NULL
411 */
412 public function getUfId($username) {
413 $user = user_load_by_name($username);
414 if (empty($user->uid)) {
415 return NULL;
416 }
417 return $user->uid;
418 }
419
420 /**
421 * @inheritDoc
422 */
423 public function logout() {
424 module_load_include('inc', 'user', 'user.pages');
425 user_logout();
426 }
427
428 /**
429 * Get the default location for CiviCRM blocks.
430 *
431 * @return string
432 */
433 public function getDefaultBlockLocation() {
434 return 'sidebar_first';
435 }
436
437 /**
438 * Load Backdrop bootstrap.
439 *
440 * @param array $params
441 * Either uid, or name & pass.
442 * @param bool $loadUser
443 * Boolean Require CMS user load.
444 * @param bool $throwError
445 * If true, print error on failure and exit.
446 * @param bool|string $realPath path to script
447 *
448 * @return bool
449 */
450 public function loadBootStrap($params = array(), $loadUser = TRUE, $throwError = TRUE, $realPath = NULL) {
451 $cmsPath = $this->cmsRootPath($realPath);
452
453 if (!file_exists("$cmsPath/core/includes/bootstrap.inc")) {
454 if ($throwError) {
455 echo '<br />Sorry, could not locate bootstrap.inc\n';
456 exit();
457 }
458 return FALSE;
459 }
460 // load drupal bootstrap
461 chdir($cmsPath);
462 define('BACKDROP_ROOT', $cmsPath);
463
464 // For drupal multi-site CRM-11313
465 if ($realPath && strpos($realPath, 'sites/all/modules/') === FALSE) {
466 preg_match('@sites/([^/]*)/modules@s', $realPath, $matches);
467 if (!empty($matches[1])) {
468 $_SERVER['HTTP_HOST'] = $matches[1];
469 }
470 }
471 require_once "$cmsPath/core/includes/bootstrap.inc";
472 backdrop_bootstrap(BACKDROP_BOOTSTRAP_FULL);
473
474 // Explicitly setting error reporting, since we cannot handle Backdrop
475 // related notices.
476 error_reporting(1);
477 if (!function_exists('module_exists') || !module_exists('civicrm')) {
478 if ($throwError) {
479 echo '<br />Sorry, could not load Backdrop bootstrap.';
480 exit();
481 }
482 return FALSE;
483 }
484
485 // Backdrop successfully bootstrapped.
486 $config = CRM_Core_Config::singleton();
487
488 // lets also fix the clean url setting
489 // CRM-6948
490 $config->cleanURL = (int) config_get('system.core', 'clean_url');
491
492 // we need to call the config hook again, since we now know
493 // all the modules that are listening on it, does not apply
494 // to J! and WP as yet
495 // CRM-8655
496 CRM_Utils_Hook::config($config);
497
498 if (!$loadUser) {
499 return TRUE;
500 }
501
502 $uid = CRM_Utils_Array::value('uid', $params);
503 if (!$uid) {
504 // Load the user we need to check Backdrop permissions.
505 $name = CRM_Utils_Array::value('name', $params, FALSE) ? $params['name'] : trim(CRM_Utils_Array::value('name', $_REQUEST));
506 $pass = CRM_Utils_Array::value('pass', $params, FALSE) ? $params['pass'] : trim(CRM_Utils_Array::value('pass', $_REQUEST));
507
508 if ($name) {
509 $uid = user_authenticate($name, $pass);
510 if (!$uid) {
511 if ($throwError) {
512 echo '<br />Sorry, unrecognized username or password.';
513 exit();
514 }
515 return FALSE;
516 }
517 }
518 }
519
520 if ($uid) {
521 $account = user_load($uid);
522 if ($account && $account->uid) {
523 global $user;
524 $user = $account;
525 return TRUE;
526 }
527 }
528
529 if ($throwError) {
530 echo '<br />Sorry, can not load CMS user account.';
531 exit();
532 }
533
534 // CRM-6948: When using loadBootStrap, it's implicit that CiviCRM has already loaded its settings
535 // which means that define(CIVICRM_CLEANURL) was correctly set.
536 // So we correct it
537 $config = CRM_Core_Config::singleton();
538 $config->cleanURL = (int) config_get('system.core', 'clean_url');
539
540 // CRM-8655: Backdrop wasn't available during bootstrap, so
541 // hook_civicrm_config() never executes.
542 CRM_Utils_Hook::config($config);
543
544 return FALSE;
545 }
546
547 /**
548 * @inheritDoc
549 */
550 public function cmsRootPath($scriptFilename = NULL) {
551 $cmsRoot = NULL;
552 $valid = NULL;
553
554 if (!is_null($scriptFilename)) {
555 $path = $scriptFilename;
556 }
557 else {
558 $path = $_SERVER['SCRIPT_FILENAME'];
559 }
560
561 // CRM-7582
562 $pathVars = explode('/',
563 str_replace('//', '/',
564 str_replace('\\', '/', $path)
565 )
566 );
567
568 // Keep the first directory name for later.
569 $firstVar = array_shift($pathVars);
570
571 // Remove script name to reduce one iteration.
572 array_pop($pathVars);
573
574 // CRM-7429 -- do check for uppermost 'includes' dir, which would
575 // work for multisite installation.
576 do {
577 $cmsRoot = $firstVar . '/' . implode('/', $pathVars);
578 // Stop if we find backdrop signature file.
579 if (file_exists("$cmsRoot/core/misc/backdrop.js")) {
580 $valid = TRUE;
581 break;
582 }
583 // Remove one directory level.
584 array_pop($pathVars);
585 } while (count($pathVars));
586
587 return ($valid) ? $cmsRoot : NULL;
588 }
589
590 /**
591 * @inheritDoc
592 */
593 public function isUserLoggedIn() {
594 $isloggedIn = FALSE;
595 if (function_exists('user_is_logged_in')) {
596 $isloggedIn = user_is_logged_in();
597 }
598
599 return $isloggedIn;
600 }
601
602 /**
603 * @inheritDoc
604 */
605 public function getLoggedInUfID() {
606 $ufID = NULL;
607 if (function_exists('user_is_logged_in') &&
608 user_is_logged_in() &&
609 function_exists('user_uid_optional_to_arg')
610 ) {
611 $ufID = user_uid_optional_to_arg(array());
612 }
613
614 return $ufID;
615 }
616
617 /**
618 * @inheritDoc
619 */
620 public function languageNegotiationURL($url, $addLanguagePart = TRUE, $removeLanguagePart = FALSE) {
621 if (empty($url)) {
622 return $url;
623 }
624
625 if (function_exists('config_get') &&
626 module_exists('locale') &&
627 function_exists('language_negotiation_get')
628 ) {
629 global $language;
630
631 // Check if language support from the URL (Path prefix or domain) is set.
632 if (language_negotiation_get('language') == 'locale-url') {
633 $urlType = config_get('locale.settings', 'locale_language_negotiation_url_part');
634
635 // URL prefix negotiation.
636 if ($urlType == LANGUAGE_NEGOTIATION_URL_PREFIX) {
637 if (isset($language->prefix) && $language->prefix) {
638 if ($addLanguagePart) {
639 $url .= $language->prefix . '/';
640 }
641 if ($removeLanguagePart) {
642 $url = str_replace("/{$language->prefix}/", '/', $url);
643 }
644 }
645 }
646 // Domain negotiation.
647 if ($urlType == LANGUAGE_NEGOTIATION_URL_DOMAIN) {
648 if (isset($language->domain) && $language->domain) {
649 if ($addLanguagePart) {
650 $cleanedUrl = preg_replace('#^https?://#', '', $language->domain);
651 // Backdrop function base_path() adds a "/" to the beginning and
652 // end of the returned path.
653 if (substr($cleanedUrl, -1) == '/') {
654 $cleanedUrl = substr($cleanedUrl, 0, -1);
655 }
656 $url = (CRM_Utils_System::isSSL() ? 'https' : 'http') . '://' . $cleanedUrl . base_path();
657 }
658 if ($removeLanguagePart && defined('CIVICRM_UF_BASEURL')) {
659 $url = str_replace('\\', '/', $url);
660 $parseUrl = parse_url($url);
661
662 //kinda hackish but not sure how to do it right
663 //hope http_build_url() will help at some point.
664 if (is_array($parseUrl) && !empty($parseUrl)) {
665 $urlParts = explode('/', $url);
666 $hostKey = array_search($parseUrl['host'], $urlParts);
667 $ufUrlParts = parse_url(CIVICRM_UF_BASEURL);
668 $urlParts[$hostKey] = $ufUrlParts['host'];
669 $url = implode('/', $urlParts);
670 }
671 }
672 }
673 }
674 }
675 }
676
677 return $url;
678 }
679
680 /**
681 * Find any users/roles/security-principals with the given permission
682 * and replace it with one or more permissions.
683 *
684 * @param string $oldPerm
685 * @param array $newPerms
686 * Array, strings.
687 */
688 public function replacePermission($oldPerm, $newPerms) {
689 $roles = user_roles(FALSE, $oldPerm);
690 if (!empty($roles)) {
691 foreach (array_keys($roles) as $rid) {
692 user_role_revoke_permissions($rid, array($oldPerm));
693 user_role_grant_permissions($rid, $newPerms);
694 }
695 }
696 }
697
698 /**
699 * Wrapper for og_membership creation.
700 *
701 * @param int $ogID
702 * Organic Group ID.
703 * @param int $userID
704 * Backdrop User ID.
705 */
706 public function og_membership_create($ogID, $userID) {
707 if (function_exists('og_entity_query_alter')) {
708 // sort-of-randomly chose a function that only exists in the // 7.x-2.x branch
709 //
710 // @TODO Find more solid way to check - try system_get_info('module', 'og').
711 //
712 // Also, since we don't know how to get the entity type of the // group, we'll assume it's 'node'
713 og_group('node', $ogID, array('entity' => user_load($userID)));
714 }
715 else {
716 // Works for the OG 7.x-1.x branch
717 og_group($ogID, array('entity' => user_load($userID)));
718 }
719 }
720
721 /**
722 * Wrapper for og_membership deletion.
723 *
724 * @param int $ogID
725 * Organic Group ID.
726 * @param int $userID
727 * Backdrop User ID.
728 */
729 public function og_membership_delete($ogID, $userID) {
730 if (function_exists('og_entity_query_alter')) {
731 // sort-of-randomly chose a function that only exists in the 7.x-2.x branch
732 // TODO: Find a more solid way to make this test
733 // Also, since we don't know how to get the entity type of the group, we'll assume it's 'node'
734 og_ungroup('node', $ogID, 'user', user_load($userID));
735 }
736 else {
737 // Works for the OG 7.x-1.x branch
738 og_ungroup($ogID, 'user', user_load($userID));
739 }
740 }
741
742 /**
743 * @inheritDoc
744 */
745 public function getTimeZoneString() {
746 global $user;
747 // Note that 0 is a valid timezone (GMT) so we use strlen not empty to check.
748 if (config_get('system.date', 'user_configurable_timezones') && $user->uid && isset($user->timezone) && strlen($user->timezone)) {
749 $timezone = $user->timezone;
750 }
751 else {
752 $timezone = config_get('system.date', 'default_timezone');
753 }
754 if (!$timezone) {
755 $timezone = parent::getTimeZoneString();
756 }
757 return $timezone;
758 }
759
760 /**
761 * @inheritDoc
762 */
763 public function setHttpHeader($name, $value) {
764 backdrop_add_http_header($name, $value);
765 }
766
767 /**
768 * @inheritDoc
769 */
770 public function synchronizeUsers() {
771 $config = CRM_Core_Config::singleton();
772 if (PHP_SAPI != 'cli') {
773 set_time_limit(300);
774 }
775 $id = 'uid';
776 $mail = 'mail';
777 $name = 'name';
778
779 $result = db_query("SELECT uid, mail, name FROM {users} where mail != ''");
780
781 $user = new StdClass();
782 $uf = $config->userFramework;
783 $contactCount = 0;
784 $contactCreated = 0;
785 $contactMatching = 0;
786 foreach ($result as $row) {
787 $user->$id = $row->$id;
788 $user->$mail = $row->$mail;
789 $user->$name = $row->$name;
790 $contactCount++;
791 if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, $row->$id, $row->$mail, $uf, 1, 'Individual', TRUE)) {
792 $contactCreated++;
793 }
794 else {
795 $contactMatching++;
796 }
797 if (is_object($match)) {
798 $match->free();
799 }
800 }
801
802 return array(
803 'contactCount' => $contactCount,
804 'contactMatching' => $contactMatching,
805 'contactCreated' => $contactCreated,
806 );
807 }
808
809 /**
810 * @inheritDoc
811 */
812 public function clearResourceCache() {
813 _backdrop_flush_css_js();
814 }
815
816 /**
817 * Get all the contact emails for users that have a specific permission.
818 *
819 * @param string $permissionName
820 * Name of the permission we are interested in.
821 *
822 * @return string
823 * a comma separated list of email addresses
824 */
825 public function permissionEmails($permissionName) {
826 // FIXME!!!!
827 return array();
828 }
829
830 }