CRM-19649 - Fix wysiwyg defaults
[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 * @inheritDoc
408 */
409 public function getUFLocale() {
410 // return CiviCRM’s xx_YY locale that either matches Drupal’s Chinese locale
411 // (for CRM-6281), Drupal’s xx_YY or is retrieved based on Drupal’s xx
412 // sometimes for CLI based on order called, this might not be set and/or empty
413 global $language;
414
415 if (empty($language)) {
416 return NULL;
417 }
418
419 if ($language->langcode == 'zh-hans') {
420 return 'zh_CN';
421 }
422
423 if ($language->langcode == 'zh-hant') {
424 return 'zh_TW';
425 }
426
427 if (preg_match('/^.._..$/', $language->langcode)) {
428 return $language->langcode;
429 }
430
431 return CRM_Core_I18n_PseudoConstant::longForShort(substr($language->langcode, 0, 2));
432 }
433
434 /**
435 * @inheritDoc
436 */
437 public function setUFLocale($civicrm_language) {
438 global $language;
439
440 $langcode = substr($civicrm_language, 0, 2);
441 $languages = language_list(FALSE, TRUE);
442
443 if (isset($languages[$langcode])) {
444 $language = $languages[$langcode];
445
446 // Config must be re-initialized to reset the base URL
447 // otherwise links will have the wrong language prefix/domain.
448 $config = CRM_Core_Config::singleton();
449 $config->free();
450
451 return TRUE;
452 }
453
454 return FALSE;
455 }
456
457 /**
458 * Determine the native ID of the CMS user.
459 *
460 * @param string $username
461 * @return int|NULL
462 */
463 public function getUfId($username) {
464 $user = user_load_by_name($username);
465 if (empty($user->uid)) {
466 return NULL;
467 }
468 return $user->uid;
469 }
470
471 /**
472 * @inheritDoc
473 */
474 public function logout() {
475 module_load_include('inc', 'user', 'user.pages');
476 user_logout();
477 }
478
479 /**
480 * Get the default location for CiviCRM blocks.
481 *
482 * @return string
483 */
484 public function getDefaultBlockLocation() {
485 return 'sidebar_first';
486 }
487
488 /**
489 * Load Backdrop bootstrap.
490 *
491 * @param array $params
492 * Either uid, or name & pass.
493 * @param bool $loadUser
494 * Boolean Require CMS user load.
495 * @param bool $throwError
496 * If true, print error on failure and exit.
497 * @param bool|string $realPath path to script
498 *
499 * @return bool
500 */
501 public function loadBootStrap($params = array(), $loadUser = TRUE, $throwError = TRUE, $realPath = NULL) {
502 $cmsPath = $this->cmsRootPath($realPath);
503
504 if (!file_exists("$cmsPath/core/includes/bootstrap.inc")) {
505 if ($throwError) {
506 echo '<br />Sorry, could not locate bootstrap.inc\n';
507 exit();
508 }
509 return FALSE;
510 }
511 // load drupal bootstrap
512 chdir($cmsPath);
513 define('BACKDROP_ROOT', $cmsPath);
514
515 // For drupal multi-site CRM-11313
516 if ($realPath && strpos($realPath, 'sites/all/modules/') === FALSE) {
517 preg_match('@sites/([^/]*)/modules@s', $realPath, $matches);
518 if (!empty($matches[1])) {
519 $_SERVER['HTTP_HOST'] = $matches[1];
520 }
521 }
522 require_once "$cmsPath/core/includes/bootstrap.inc";
523 backdrop_bootstrap(BACKDROP_BOOTSTRAP_FULL);
524
525 // Explicitly setting error reporting, since we cannot handle Backdrop
526 // related notices.
527 error_reporting(1);
528 if (!function_exists('module_exists') || !module_exists('civicrm')) {
529 if ($throwError) {
530 echo '<br />Sorry, could not load Backdrop bootstrap.';
531 exit();
532 }
533 return FALSE;
534 }
535
536 // Backdrop successfully bootstrapped.
537 $config = CRM_Core_Config::singleton();
538
539 // lets also fix the clean url setting
540 // CRM-6948
541 $config->cleanURL = (int) config_get('system.core', 'clean_url');
542
543 // we need to call the config hook again, since we now know
544 // all the modules that are listening on it, does not apply
545 // to J! and WP as yet
546 // CRM-8655
547 CRM_Utils_Hook::config($config);
548
549 if (!$loadUser) {
550 return TRUE;
551 }
552
553 $uid = CRM_Utils_Array::value('uid', $params);
554 if (!$uid) {
555 // Load the user we need to check Backdrop permissions.
556 $name = CRM_Utils_Array::value('name', $params, FALSE) ? $params['name'] : trim(CRM_Utils_Array::value('name', $_REQUEST));
557 $pass = CRM_Utils_Array::value('pass', $params, FALSE) ? $params['pass'] : trim(CRM_Utils_Array::value('pass', $_REQUEST));
558
559 if ($name) {
560 $uid = user_authenticate($name, $pass);
561 if (!$uid) {
562 if ($throwError) {
563 echo '<br />Sorry, unrecognized username or password.';
564 exit();
565 }
566 return FALSE;
567 }
568 }
569 }
570
571 if ($uid) {
572 $account = user_load($uid);
573 if ($account && $account->uid) {
574 global $user;
575 $user = $account;
576 return TRUE;
577 }
578 }
579
580 if ($throwError) {
581 echo '<br />Sorry, can not load CMS user account.';
582 exit();
583 }
584
585 // CRM-6948: When using loadBootStrap, it's implicit that CiviCRM has already loaded its settings
586 // which means that define(CIVICRM_CLEANURL) was correctly set.
587 // So we correct it
588 $config = CRM_Core_Config::singleton();
589 $config->cleanURL = (int) config_get('system.core', 'clean_url');
590
591 // CRM-8655: Backdrop wasn't available during bootstrap, so
592 // hook_civicrm_config() never executes.
593 CRM_Utils_Hook::config($config);
594
595 return FALSE;
596 }
597
598 /**
599 * @inheritDoc
600 */
601 public function cmsRootPath($scriptFilename = NULL) {
602 $cmsRoot = NULL;
603 $valid = NULL;
604
605 if (!is_null($scriptFilename)) {
606 $path = $scriptFilename;
607 }
608 else {
609 $path = $_SERVER['SCRIPT_FILENAME'];
610 }
611
612 // CRM-7582
613 $pathVars = explode('/',
614 str_replace('//', '/',
615 str_replace('\\', '/', $path)
616 )
617 );
618
619 // Keep the first directory name for later.
620 $firstVar = array_shift($pathVars);
621
622 // Remove script name to reduce one iteration.
623 array_pop($pathVars);
624
625 // CRM-7429 -- do check for uppermost 'includes' dir, which would
626 // work for multisite installation.
627 do {
628 $cmsRoot = $firstVar . '/' . implode('/', $pathVars);
629 // Stop if we find backdrop signature file.
630 if (file_exists("$cmsRoot/core/misc/backdrop.js")) {
631 $valid = TRUE;
632 break;
633 }
634 // Remove one directory level.
635 array_pop($pathVars);
636 } while (count($pathVars));
637
638 return ($valid) ? $cmsRoot : NULL;
639 }
640
641 /**
642 * @inheritDoc
643 */
644 public function isUserLoggedIn() {
645 $isloggedIn = FALSE;
646 if (function_exists('user_is_logged_in')) {
647 $isloggedIn = user_is_logged_in();
648 }
649
650 return $isloggedIn;
651 }
652
653 /**
654 * @inheritDoc
655 */
656 public function getLoggedInUfID() {
657 $ufID = NULL;
658 if (function_exists('user_is_logged_in') &&
659 user_is_logged_in() &&
660 function_exists('user_uid_optional_to_arg')
661 ) {
662 $ufID = user_uid_optional_to_arg(array());
663 }
664
665 return $ufID;
666 }
667
668 /**
669 * @inheritDoc
670 */
671 public function languageNegotiationURL($url, $addLanguagePart = TRUE, $removeLanguagePart = FALSE) {
672 if (empty($url)) {
673 return $url;
674 }
675
676 if (function_exists('config_get') &&
677 module_exists('locale') &&
678 function_exists('language_negotiation_get')
679 ) {
680 global $language;
681
682 // Check if language support from the URL (Path prefix or domain) is set.
683 if (language_negotiation_get('language') == 'locale-url') {
684 $urlType = config_get('locale.settings', 'locale_language_negotiation_url_part');
685
686 // URL prefix negotiation.
687 if ($urlType == LANGUAGE_NEGOTIATION_URL_PREFIX) {
688 if (isset($language->prefix) && $language->prefix) {
689 if ($addLanguagePart) {
690 $url .= $language->prefix . '/';
691 }
692 if ($removeLanguagePart) {
693 $url = str_replace("/{$language->prefix}/", '/', $url);
694 }
695 }
696 }
697 // Domain negotiation.
698 if ($urlType == LANGUAGE_NEGOTIATION_URL_DOMAIN) {
699 if (isset($language->domain) && $language->domain) {
700 if ($addLanguagePart) {
701 $cleanedUrl = preg_replace('#^https?://#', '', $language->domain);
702 // Backdrop function base_path() adds a "/" to the beginning and
703 // end of the returned path.
704 if (substr($cleanedUrl, -1) == '/') {
705 $cleanedUrl = substr($cleanedUrl, 0, -1);
706 }
707 $url = (CRM_Utils_System::isSSL() ? 'https' : 'http') . '://' . $cleanedUrl . base_path();
708 }
709 if ($removeLanguagePart && defined('CIVICRM_UF_BASEURL')) {
710 $url = str_replace('\\', '/', $url);
711 $parseUrl = parse_url($url);
712
713 //kinda hackish but not sure how to do it right
714 //hope http_build_url() will help at some point.
715 if (is_array($parseUrl) && !empty($parseUrl)) {
716 $urlParts = explode('/', $url);
717 $hostKey = array_search($parseUrl['host'], $urlParts);
718 $ufUrlParts = parse_url(CIVICRM_UF_BASEURL);
719 $urlParts[$hostKey] = $ufUrlParts['host'];
720 $url = implode('/', $urlParts);
721 }
722 }
723 }
724 }
725 }
726 }
727
728 return $url;
729 }
730
731 /**
732 * Find any users/roles/security-principals with the given permission
733 * and replace it with one or more permissions.
734 *
735 * @param string $oldPerm
736 * @param array $newPerms
737 * Array, strings.
738 */
739 public function replacePermission($oldPerm, $newPerms) {
740 $roles = user_roles(FALSE, $oldPerm);
741 if (!empty($roles)) {
742 foreach (array_keys($roles) as $rid) {
743 user_role_revoke_permissions($rid, array($oldPerm));
744 user_role_grant_permissions($rid, $newPerms);
745 }
746 }
747 }
748
749 /**
750 * Wrapper for og_membership creation.
751 *
752 * @param int $ogID
753 * Organic Group ID.
754 * @param int $userID
755 * Backdrop User ID.
756 */
757 public function og_membership_create($ogID, $userID) {
758 if (function_exists('og_entity_query_alter')) {
759 // sort-of-randomly chose a function that only exists in the // 7.x-2.x branch
760 //
761 // @TODO Find more solid way to check - try system_get_info('module', 'og').
762 //
763 // Also, since we don't know how to get the entity type of the // group, we'll assume it's 'node'
764 og_group('node', $ogID, array('entity' => user_load($userID)));
765 }
766 else {
767 // Works for the OG 7.x-1.x branch
768 og_group($ogID, array('entity' => user_load($userID)));
769 }
770 }
771
772 /**
773 * Wrapper for og_membership deletion.
774 *
775 * @param int $ogID
776 * Organic Group ID.
777 * @param int $userID
778 * Backdrop User ID.
779 */
780 public function og_membership_delete($ogID, $userID) {
781 if (function_exists('og_entity_query_alter')) {
782 // sort-of-randomly chose a function that only exists in the 7.x-2.x branch
783 // TODO: Find a more solid way to make this test
784 // Also, since we don't know how to get the entity type of the group, we'll assume it's 'node'
785 og_ungroup('node', $ogID, 'user', user_load($userID));
786 }
787 else {
788 // Works for the OG 7.x-1.x branch
789 og_ungroup($ogID, 'user', user_load($userID));
790 }
791 }
792
793 /**
794 * @inheritDoc
795 */
796 public function getTimeZoneString() {
797 global $user;
798 // Note that 0 is a valid timezone (GMT) so we use strlen not empty to check.
799 if (config_get('system.date', 'user_configurable_timezones') && $user->uid && isset($user->timezone) && strlen($user->timezone)) {
800 $timezone = $user->timezone;
801 }
802 else {
803 $timezone = config_get('system.date', 'default_timezone');
804 }
805 if (!$timezone) {
806 $timezone = parent::getTimeZoneString();
807 }
808 return $timezone;
809 }
810
811 /**
812 * @inheritDoc
813 */
814 public function setHttpHeader($name, $value) {
815 backdrop_add_http_header($name, $value);
816 }
817
818 /**
819 * @inheritDoc
820 */
821 public function synchronizeUsers() {
822 $config = CRM_Core_Config::singleton();
823 if (PHP_SAPI != 'cli') {
824 set_time_limit(300);
825 }
826 $id = 'uid';
827 $mail = 'mail';
828 $name = 'name';
829
830 $result = db_query("SELECT uid, mail, name FROM {users} where mail != ''");
831
832 $user = new StdClass();
833 $uf = $config->userFramework;
834 $contactCount = 0;
835 $contactCreated = 0;
836 $contactMatching = 0;
837 foreach ($result as $row) {
838 $user->$id = $row->$id;
839 $user->$mail = $row->$mail;
840 $user->$name = $row->$name;
841 $contactCount++;
842 if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, $row->$id, $row->$mail, $uf, 1, 'Individual', TRUE)) {
843 $contactCreated++;
844 }
845 else {
846 $contactMatching++;
847 }
848 if (is_object($match)) {
849 $match->free();
850 }
851 }
852
853 return array(
854 'contactCount' => $contactCount,
855 'contactMatching' => $contactMatching,
856 'contactCreated' => $contactCreated,
857 );
858 }
859
860 /**
861 * @inheritDoc
862 */
863 public function clearResourceCache() {
864 _backdrop_flush_css_js();
865 }
866
867 /**
868 * Get all the contact emails for users that have a specific permission.
869 *
870 * @param string $permissionName
871 * Name of the permission we are interested in.
872 *
873 * @return string
874 * a comma separated list of email addresses
875 */
876 public function permissionEmails($permissionName) {
877 // FIXME!!!!
878 return array();
879 }
880
881 }