Ian province abbreviation patch - issue 724
[civicrm-core.git] / CRM / Utils / System / Drupal.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 */
33
34/**
35 * Drupal specific stuff goes here
36 */
9977c6f5 37class CRM_Utils_System_Drupal extends CRM_Utils_System_DrupalBase {
6a488035
TO
38
39 /**
17f443df 40 * @inheritDoc
6a488035 41 */
00be9182 42 public function createUser(&$params, $mail) {
88b91745 43 $form_state = form_state_defaults();
d0ffa3e4 44
6a488035
TO
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 (!variable_get('user_email_verification', TRUE) || $admin) {
e7292422 53 $form_state['input']['pass'] = array('pass1' => $params['cms_pass'], 'pass2' => $params['cms_pass']);
6a488035
TO
54 }
55
9b873358 56 if (!empty($params['notify'])) {
6a488035
TO
57 $form_state['input']['notify'] = $params['notify'];
58 }
59
60 $form_state['rebuild'] = FALSE;
61 $form_state['programmed'] = TRUE;
805b0f6e 62 $form_state['complete form'] = FALSE;
6a488035
TO
63 $form_state['method'] = 'post';
64 $form_state['build_info']['args'] = array();
41d551ad 65 /*
e70a7fc0
TO
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 */
41d551ad 70 $form_state['must_validate'] = TRUE;
6a488035
TO
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;
805b0f6e 79 $form['#array_parents'] = array();
80 $form['#tree'] = FALSE;
6a488035
TO
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 }
a5ecff8d 88 return $form_state['user']->uid;
6a488035
TO
89 }
90
bb3a214a 91 /**
17f443df 92 * @inheritDoc
bb3a214a 93 */
00be9182 94 public function updateCMSName($ufID, $ufName) {
6a488035
TO
95 // CRM-5555
96 if (function_exists('user_load')) {
97 $user = user_load($ufID);
98 if ($user->mail != $ufName) {
99 user_save($user, array('mail' => $ufName));
100 $user = user_load($ufID);
101 }
102 }
103 }
104
105 /**
fe482240 106 * Check if username and email exists in the drupal db.
6a488035 107 *
77855840
TO
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'.
6a488035 114 */
00be9182 115 public static function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') {
6a488035
TO
116 $config = CRM_Core_Config::singleton();
117
353ffa53
TO
118 $dao = new CRM_Core_DAO();
119 $name = $dao->escape(CRM_Utils_Array::value('name', $params));
120 $email = $dao->escape(CRM_Utils_Array::value('mail', $params));
6a488035
TO
121 $errors = form_get_errors();
122 if ($errors) {
123 // unset drupal messages to avoid twice display of errors
124 unset($_SESSION['messages']);
125 }
126
a7488080 127 if (!empty($params['name'])) {
6a488035
TO
128 if ($nameError = user_validate_name($params['name'])) {
129 $errors['cms_name'] = $nameError;
130 }
131 else {
132 $uid = db_query(
133 "SELECT uid FROM {users} WHERE name = :name",
134 array(':name' => $params['name'])
135 )->fetchField();
136 if ((bool) $uid) {
137 $errors['cms_name'] = ts('The username %1 is already taken. Please select another username.', array(1 => $params['name']));
138 }
139 }
140 }
141
a7488080 142 if (!empty($params['mail'])) {
6a488035
TO
143 if ($emailError = user_validate_mail($params['mail'])) {
144 $errors[$emailName] = $emailError;
145 }
146 else {
147 $uid = db_query(
148 "SELECT uid FROM {users} WHERE mail = :mail",
149 array(':mail' => $params['mail'])
150 )->fetchField();
151 if ((bool) $uid) {
152 $resetUrl = $config->userFrameworkBaseURL . 'user/password';
153 $errors[$emailName] = ts('The email address %1 is already registered. <a href="%2">Have you forgotten your password?</a>',
154 array(1 => $params['mail'], 2 => $resetUrl)
155 );
156 }
157 }
158 }
159 }
160
d424ffde 161 /**
17f443df 162 * @inheritDoc
6a488035
TO
163 */
164 public function getLoginURL($destination = '') {
404815e3 165 $query = $destination ? array('destination' => $destination) : array();
4dddcf7f 166 return url('user', array('query' => $query), TRUE);
6a488035
TO
167 }
168
6a488035 169 /**
17f443df 170 * @inheritDoc
6a488035 171 */
00be9182 172 public function setTitle($title, $pageTitle = NULL) {
6a488035
TO
173 if (arg(0) == 'civicrm') {
174 if (!$pageTitle) {
175 $pageTitle = $title;
176 }
177
178 drupal_set_title($pageTitle, PASS_THROUGH);
179 }
180 }
181
182 /**
17f443df 183 * @inheritDoc
6a488035 184 */
00be9182 185 public function appendBreadCrumb($breadCrumbs) {
6a488035
TO
186 $breadCrumb = drupal_get_breadcrumb();
187
188 if (is_array($breadCrumbs)) {
189 foreach ($breadCrumbs as $crumbs) {
190 if (stripos($crumbs['url'], 'id%%')) {
191 $args = array('cid', 'mid');
192 foreach ($args as $a) {
193 $val = CRM_Utils_Request::retrieve($a, 'Positive', CRM_Core_DAO::$_nullObject,
194 FALSE, NULL, $_GET
195 );
196 if ($val) {
197 $crumbs['url'] = str_ireplace("%%{$a}%%", $val, $crumbs['url']);
198 }
199 }
200 }
201 $breadCrumb[] = "<a href=\"{$crumbs['url']}\">{$crumbs['title']}</a>";
202 }
203 }
204 drupal_set_breadcrumb($breadCrumb);
205 }
206
207 /**
17f443df 208 * @inheritDoc
6a488035 209 */
00be9182 210 public function resetBreadCrumb() {
6a488035
TO
211 $bc = array();
212 drupal_set_breadcrumb($bc);
213 }
214
215 /**
17f443df 216 * @inheritDoc
6a488035 217 */
00be9182 218 public function addHTMLHead($header) {
6a488035
TO
219 static $count = 0;
220 if (!empty($header)) {
221 $key = 'civi_' . ++$count;
222 $data = array(
223 '#type' => 'markup',
224 '#markup' => $header,
225 );
226 drupal_add_html_head($data, $key);
227 }
228 }
229
230 /**
17f443df 231 * @inheritDoc
6a488035
TO
232 */
233 public function addScriptUrl($url, $region) {
234 $params = array('group' => JS_LIBRARY, 'weight' => 10);
235 switch ($region) {
236 case 'html-header':
237 case 'page-footer':
238 $params['scope'] = substr($region, 5);
239 break;
e7292422 240
6a488035
TO
241 default:
242 return FALSE;
243 }
244 // If the path is within the drupal directory we can use the more efficient 'file' setting
42e1a97c 245 $params['type'] = $this->formatResourceUrl($url) ? 'file' : 'external';
6a488035
TO
246 drupal_add_js($url, $params);
247 return TRUE;
248 }
249
250 /**
17f443df 251 * @inheritDoc
6a488035
TO
252 */
253 public function addScript($code, $region) {
254 $params = array('type' => 'inline', 'group' => JS_LIBRARY, 'weight' => 10);
255 switch ($region) {
256 case 'html-header':
257 case 'page-footer':
258 $params['scope'] = substr($region, 5);
259 break;
e7292422 260
6a488035
TO
261 default:
262 return FALSE;
263 }
264 drupal_add_js($code, $params);
265 return TRUE;
266 }
267
268 /**
17f443df 269 * @inheritDoc
6a488035
TO
270 */
271 public function addStyleUrl($url, $region) {
272 if ($region != 'html-header') {
273 return FALSE;
274 }
275 $params = array();
276 // If the path is within the drupal directory we can use the more efficient 'file' setting
42e1a97c 277 $params['type'] = $this->formatResourceUrl($url) ? 'file' : 'external';
6a488035
TO
278 drupal_add_css($url, $params);
279 return TRUE;
280 }
281
282 /**
17f443df 283 * @inheritDoc
6a488035
TO
284 */
285 public function addStyle($code, $region) {
286 if ($region != 'html-header') {
287 return FALSE;
288 }
289 $params = array('type' => 'inline');
290 drupal_add_css($code, $params);
291 return TRUE;
292 }
293
6a488035 294 /**
17f443df 295 * @inheritDoc
6a488035 296 */
00be9182 297 public function mapConfigToSSL() {
6a488035
TO
298 global $base_url;
299 $base_url = str_replace('http://', 'https://', $base_url);
300 }
301
302 /**
17f443df 303 * @inheritDoc
6a488035 304 */
17f443df 305 public function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) {
6a488035
TO
306 require_once 'DB.php';
307
308 $config = CRM_Core_Config::singleton();
309
310 $dbDrupal = DB::connect($config->userFrameworkDSN);
311 if (DB::isError($dbDrupal)) {
312 CRM_Core_Error::fatal("Cannot connect to drupal db via $config->userFrameworkDSN, " . $dbDrupal->getMessage());
313 }
314
315 $account = $userUid = $userMail = NULL;
316 if ($loadCMSBootstrap) {
317 $bootStrapParams = array();
318 if ($name && $password) {
319 $bootStrapParams = array(
320 'name' => $name,
321 'pass' => $password,
322 );
323 }
324 CRM_Utils_System::loadBootStrap($bootStrapParams, TRUE, TRUE, $realPath);
325
326 global $user;
327 if ($user) {
328 $userUid = $user->uid;
329 $userMail = $user->mail;
330 }
331 }
332 else {
333 // CRM-8638
334 // SOAP cannot load drupal bootstrap and hence we do it the old way
335 // Contact CiviSMTP folks if we run into issues with this :)
336 $cmsPath = $config->userSystem->cmsRootPath($realPath);
337
e7292422
TO
338 require_once "$cmsPath/includes/bootstrap.inc";
339 require_once "$cmsPath/includes/password.inc";
6a488035
TO
340
341 $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower';
353ffa53
TO
342 $name = $dbDrupal->escapeSimple($strtolower($name));
343 $sql = "
6a488035
TO
344SELECT u.*
345FROM {$config->userFrameworkUsersTableName} u
346WHERE LOWER(u.name) = '$name'
347AND u.status = 1
348";
349
350 $query = $dbDrupal->query($sql);
351 $row = $query->fetchRow(DB_FETCHMODE_ASSOC);
352
353 if ($row) {
354 $fakeDrupalAccount = drupal_anonymous_user();
355 $fakeDrupalAccount->name = $name;
356 $fakeDrupalAccount->pass = $row['pass'];
357 $passwordCheck = user_check_password($password, $fakeDrupalAccount);
358 if ($passwordCheck) {
359 $userUid = $row['uid'];
360 $userMail = $row['mail'];
361 }
362 }
363 }
364
365 if ($userUid && $userMail) {
366 CRM_Core_BAO_UFMatch::synchronizeUFMatch($account, $userUid, $userMail, 'Drupal');
367 $contactID = CRM_Core_BAO_UFMatch::getContactId($userUid);
368 if (!$contactID) {
369 return FALSE;
370 }
371 return array($contactID, $userUid, mt_rand());
372 }
373 return FALSE;
374 }
375
bb3a214a 376 /**
17f443df 377 * @inheritDoc
bb3a214a 378 */
00be9182 379 public function loadUser($username) {
6a488035
TO
380 global $user;
381
382 $user = user_load_by_name($username);
383
384 if (empty($user->uid)) {
385 return FALSE;
386 }
387
388 $uid = $user->uid;
389 $contact_id = CRM_Core_BAO_UFMatch::getContactId($uid);
390
391 // lets store contact id and user id in session
392 $session = CRM_Core_Session::singleton();
393 $session->set('ufID', $uid);
394 $session->set('userID', $contact_id);
395 return TRUE;
396 }
397
82d9c21e 398 /**
53980972 399 * Perform any post login activities required by the UF -
400 * e.g. for drupal: records a watchdog message about the new session, saves the login timestamp,
401 * calls hook_user op 'login' and generates a new session.
e43cc689 402 *
c301f76e 403 * @param array $params
95d68223
TO
404 *
405 * FIXME: Document values accepted/required by $params
53980972 406 */
9b873358 407 public function userLoginFinalize($params = array()) {
53980972 408 user_login_finalize($params);
82d9c21e 409 }
410
46b6363c 411 /**
fe482240 412 * Determine the native ID of the CMS user.
46b6363c 413 *
100fef9d 414 * @param string $username
46b6363c
TO
415 * @return int|NULL
416 */
00be9182 417 public function getUfId($username) {
46b6363c
TO
418 $user = user_load_by_name($username);
419 if (empty($user->uid)) {
420 return NULL;
421 }
422 return $user->uid;
423 }
424
6a488035 425 /**
17f443df 426 * @inheritDoc
bb3a214a 427 */
00be9182 428 public function logout() {
6a488035
TO
429 module_load_include('inc', 'user', 'user.pages');
430 return user_logout();
431 }
432
6a488035 433 /**
fe482240 434 * Get the default location for CiviCRM blocks.
6a488035
TO
435 *
436 * @return string
437 */
00be9182 438 public function getDefaultBlockLocation() {
6a488035
TO
439 return 'sidebar_first';
440 }
441
6a488035 442 /**
fe482240 443 * Load drupal bootstrap.
6a488035 444 *
77855840
TO
445 * @param array $params
446 * Either uid, or name & pass.
447 * @param bool $loadUser
448 * Boolean Require CMS user load.
449 * @param bool $throwError
450 * If true, print error on failure and exit.
451 * @param bool|string $realPath path to script
f4aaa82a
EM
452 *
453 * @return bool
6a488035 454 */
00be9182 455 public function loadBootStrap($params = array(), $loadUser = TRUE, $throwError = TRUE, $realPath = NULL) {
6a488035
TO
456 //take the cms root path.
457 $cmsPath = $this->cmsRootPath($realPath);
458
459 if (!file_exists("$cmsPath/includes/bootstrap.inc")) {
460 if ($throwError) {
461 echo '<br />Sorry, could not locate bootstrap.inc\n';
462 exit();
463 }
464 return FALSE;
465 }
466 // load drupal bootstrap
467 chdir($cmsPath);
468 define('DRUPAL_ROOT', $cmsPath);
469
470 // For drupal multi-site CRM-11313
471 if ($realPath && strpos($realPath, 'sites/all/modules/') === FALSE) {
472 preg_match('@sites/([^/]*)/modules@s', $realPath, $matches);
473 if (!empty($matches[1])) {
474 $_SERVER['HTTP_HOST'] = $matches[1];
475 }
476 }
477 require_once 'includes/bootstrap.inc';
38507482
CB
478 // @ to suppress notices eg 'DRUPALFOO already defined'.
479 @drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
6a488035
TO
480
481 // explicitly setting error reporting, since we cannot handle drupal related notices
482 error_reporting(1);
0af0e4c9 483 if (!function_exists('module_exists') || !module_exists('civicrm')) {
6a488035
TO
484 if ($throwError) {
485 echo '<br />Sorry, could not load drupal bootstrap.';
486 exit();
487 }
488 return FALSE;
489 }
490
491 // seems like we've bootstrapped drupal
492 $config = CRM_Core_Config::singleton();
493
6a488035
TO
494 // lets also fix the clean url setting
495 // CRM-6948
496 $config->cleanURL = (int) variable_get('clean_url', '0');
497
498 // we need to call the config hook again, since we now know
499 // all the modules that are listening on it, does not apply
500 // to J! and WP as yet
501 // CRM-8655
502 CRM_Utils_Hook::config($config);
503
504 if (!$loadUser) {
505 return TRUE;
506 }
507
508 $uid = CRM_Utils_Array::value('uid', $params);
509 if (!$uid) {
510 //load user, we need to check drupal permissions.
511 $name = CRM_Utils_Array::value('name', $params, FALSE) ? $params['name'] : trim(CRM_Utils_Array::value('name', $_REQUEST));
512 $pass = CRM_Utils_Array::value('pass', $params, FALSE) ? $params['pass'] : trim(CRM_Utils_Array::value('pass', $_REQUEST));
513
514 if ($name) {
515 $uid = user_authenticate($name, $pass);
516 if (!$uid) {
517 if ($throwError) {
518 echo '<br />Sorry, unrecognized username or password.';
519 exit();
520 }
521 return FALSE;
522 }
523 }
524 }
525
526 if ($uid) {
527 $account = user_load($uid);
528 if ($account && $account->uid) {
529 global $user;
530 $user = $account;
531 return TRUE;
532 }
533 }
534
535 if ($throwError) {
536 echo '<br />Sorry, can not load CMS user account.';
537 exit();
538 }
539
0af0e4c9
DL
540 // CRM-6948: When using loadBootStrap, it's implicit that CiviCRM has already loaded its settings
541 // which means that define(CIVICRM_CLEANURL) was correctly set.
6a488035
TO
542 // So we correct it
543 $config = CRM_Core_Config::singleton();
e7292422 544 $config->cleanURL = (int) variable_get('clean_url', '0');
6a488035
TO
545
546 // CRM-8655: Drupal wasn't available during bootstrap, so hook_civicrm_config never executes
547 CRM_Utils_Hook::config($config);
548
549 return FALSE;
550 }
551
a5ecff8d 552 /**
a5ecff8d 553 */
00be9182 554 public function cmsRootPath($scriptFilename = NULL) {
6a488035
TO
555 $cmsRoot = $valid = NULL;
556
557 if (!is_null($scriptFilename)) {
558 $path = $scriptFilename;
559 }
560 else {
561 $path = $_SERVER['SCRIPT_FILENAME'];
562 }
563
564 if (function_exists('drush_get_context')) {
565 // drush anyway takes care of multisite install etc
566 return drush_get_context('DRUSH_DRUPAL_ROOT');
567 }
568 // CRM-7582
569 $pathVars = explode('/',
570 str_replace('//', '/',
571 str_replace('\\', '/', $path)
572 )
573 );
574
575 //lets store first var,
576 //need to get back for windows.
577 $firstVar = array_shift($pathVars);
578
579 //lets remove sript name to reduce one iteration.
580 array_pop($pathVars);
581
948d11bf
CB
582 // CRM-7429 -- do check for uppermost 'includes' dir, which would
583 // work for multisite installation.
6a488035
TO
584 do {
585 $cmsRoot = $firstVar . '/' . implode('/', $pathVars);
586 $cmsIncludePath = "$cmsRoot/includes";
948d11bf 587 // Stop if we find bootstrap.
f81c7606 588 if (file_exists("$cmsIncludePath/bootstrap.inc")) {
6a488035
TO
589 $valid = TRUE;
590 break;
591 }
592 //remove one directory level.
593 array_pop($pathVars);
594 } while (count($pathVars));
595
596 return ($valid) ? $cmsRoot : NULL;
597 }
598
599 /**
17f443df 600 * @inheritDoc
6a488035
TO
601 */
602 public function isUserLoggedIn() {
603 $isloggedIn = FALSE;
604 if (function_exists('user_is_logged_in')) {
605 $isloggedIn = user_is_logged_in();
606 }
607
608 return $isloggedIn;
609 }
610
611 /**
17f443df 612 * @inheritDoc
6a488035
TO
613 */
614 public function getLoggedInUfID() {
615 $ufID = NULL;
616 if (function_exists('user_is_logged_in') &&
617 user_is_logged_in() &&
618 function_exists('user_uid_optional_to_arg')
619 ) {
620 $ufID = user_uid_optional_to_arg(array());
621 }
622
623 return $ufID;
624 }
625
626 /**
17f443df 627 * @inheritDoc
6a488035 628 */
00be9182 629 public function languageNegotiationURL($url, $addLanguagePart = TRUE, $removeLanguagePart = FALSE) {
6a488035
TO
630 if (empty($url)) {
631 return $url;
632 }
633
634 //CRM-7803 -from d7 onward.
635 $config = CRM_Core_Config::singleton();
636 if (function_exists('variable_get') &&
637 module_exists('locale') &&
638 function_exists('language_negotiation_get')
639 ) {
640 global $language;
641
642 //does user configuration allow language
643 //support from the URL (Path prefix or domain)
644 if (language_negotiation_get('language') == 'locale-url') {
645 $urlType = variable_get('locale_language_negotiation_url_part');
646
647 //url prefix
648 if ($urlType == LOCALE_LANGUAGE_NEGOTIATION_URL_PREFIX) {
649 if (isset($language->prefix) && $language->prefix) {
650 if ($addLanguagePart) {
3fe61451 651 $url .= $language->prefix . '/';
6a488035
TO
652 }
653 if ($removeLanguagePart) {
654 $url = str_replace("/{$language->prefix}/", '/', $url);
655 }
656 }
657 }
658 //domain
659 if ($urlType == LOCALE_LANGUAGE_NEGOTIATION_URL_DOMAIN) {
660 if (isset($language->domain) && $language->domain) {
661 if ($addLanguagePart) {
b276048c 662 $cleanedUrl = preg_replace('#^https?://#', '', $language->domain);
ea54c1ae 663 // drupal function base_path() adds a "/" to the beginning and end of the returned path
34020e33 664 if (substr($cleanedUrl, -1) == '/') {
ea54c1ae
LS
665 $cleanedUrl = substr($cleanedUrl, 0, -1);
666 }
b276048c 667 $url = (CRM_Utils_System::isSSL() ? 'https' : 'http') . '://' . $cleanedUrl . base_path();
6a488035
TO
668 }
669 if ($removeLanguagePart && defined('CIVICRM_UF_BASEURL')) {
670 $url = str_replace('\\', '/', $url);
671 $parseUrl = parse_url($url);
672
673 //kinda hackish but not sure how to do it right
674 //hope http_build_url() will help at some point.
675 if (is_array($parseUrl) && !empty($parseUrl)) {
353ffa53
TO
676 $urlParts = explode('/', $url);
677 $hostKey = array_search($parseUrl['host'], $urlParts);
678 $ufUrlParts = parse_url(CIVICRM_UF_BASEURL);
6a488035 679 $urlParts[$hostKey] = $ufUrlParts['host'];
353ffa53 680 $url = implode('/', $urlParts);
6a488035
TO
681 }
682 }
683 }
684 }
685 }
686 }
687
688 return $url;
689 }
690
691 /**
692 * Find any users/roles/security-principals with the given permission
693 * and replace it with one or more permissions.
694 *
5a4f6742
CW
695 * @param string $oldPerm
696 * @param array $newPerms
77855840 697 * Array, strings.
6a488035 698 */
00be9182 699 public function replacePermission($oldPerm, $newPerms) {
6a488035
TO
700 $roles = user_roles(FALSE, $oldPerm);
701 if (!empty($roles)) {
702 foreach (array_keys($roles) as $rid) {
703 user_role_revoke_permissions($rid, array($oldPerm));
704 user_role_grant_permissions($rid, $newPerms);
705 }
706 }
707 }
708
6a488035 709 /**
fe482240 710 * Wrapper for og_membership creation.
a5ecff8d 711 *
77855840
TO
712 * @param int $ogID
713 * Organic Group ID.
714 * @param int $drupalID
715 * Drupal User ID.
6a488035 716 */
9b873358 717 public function og_membership_create($ogID, $drupalID) {
6a488035 718 if (function_exists('og_entity_query_alter')) {
a5ecff8d
CB
719 // sort-of-randomly chose a function that only exists in the // 7.x-2.x branch
720 //
721 // @TODO Find more solid way to check - try system_get_info('module', 'og').
722 //
723 // Also, since we don't know how to get the entity type of the // group, we'll assume it's 'node'
6a488035
TO
724 og_group('node', $ogID, array('entity' => user_load($drupalID)));
725 }
726 else {
727 // Works for the OG 7.x-1.x branch
728 og_group($ogID, array('entity' => user_load($drupalID)));
729 }
730 }
731
732 /**
fe482240 733 * Wrapper for og_membership deletion.
a5ecff8d 734 *
77855840
TO
735 * @param int $ogID
736 * Organic Group ID.
737 * @param int $drupalID
738 * Drupal User ID.
6a488035 739 */
00be9182 740 public function og_membership_delete($ogID, $drupalID) {
6a488035
TO
741 if (function_exists('og_entity_query_alter')) {
742 // sort-of-randomly chose a function that only exists in the 7.x-2.x branch
743 // TODO: Find a more solid way to make this test
744 // Also, since we don't know how to get the entity type of the group, we'll assume it's 'node'
745 og_ungroup('node', $ogID, 'user', user_load($drupalID));
0db6c3e1
TO
746 }
747 else {
6a488035
TO
748 // Works for the OG 7.x-1.x branch
749 og_ungroup($ogID, 'user', user_load($drupalID));
750 }
751 }
d8a4acc0 752
5a604d61 753 /**
17f443df 754 * @inheritDoc
5a604d61 755 */
00be9182 756 public function getTimeZoneString() {
5a604d61
E
757 global $user;
758 if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) {
759 $timezone = $user->timezone;
0db6c3e1
TO
760 }
761 else {
e7292422 762 $timezone = variable_get('date_default_timezone', NULL);
5a604d61 763 }
48ec57ab
TO
764 if (!$timezone) {
765 $timezone = parent::getTimeZoneString();
5a604d61 766 }
48ec57ab 767 return $timezone;
5a604d61 768 }
353ffa53 769
d42a224c
CW
770 /**
771 * @inheritDoc
772 */
773 public function setHttpHeader($name, $value) {
774 drupal_add_http_header($name, $value);
775 }
776
6a488035 777}