Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * Drupal specific stuff goes here | |
38 | */ | |
9977c6f5 | 39 | class CRM_Utils_System_Drupal extends CRM_Utils_System_DrupalBase { |
6a488035 TO |
40 | |
41 | /** | |
100fef9d | 42 | * Create a user in Drupal. |
6a488035 | 43 | * |
77855840 TO |
44 | * @param array $params |
45 | * Associated array. | |
46 | * @param string $mail | |
47 | * Email id for cms user. | |
6a488035 TO |
48 | * |
49 | * @return uid if user exists, false otherwise | |
50 | * | |
6a488035 TO |
51 | * |
52 | */ | |
00be9182 | 53 | public function createUser(&$params, $mail) { |
88b91745 | 54 | $form_state = form_state_defaults(); |
d0ffa3e4 | 55 | |
6a488035 TO |
56 | $form_state['input'] = array( |
57 | 'name' => $params['cms_name'], | |
58 | 'mail' => $params[$mail], | |
59 | 'op' => 'Create new account', | |
60 | ); | |
61 | ||
62 | $admin = user_access('administer users'); | |
63 | if (!variable_get('user_email_verification', TRUE) || $admin) { | |
e7292422 | 64 | $form_state['input']['pass'] = array('pass1' => $params['cms_pass'], 'pass2' => $params['cms_pass']); |
6a488035 TO |
65 | } |
66 | ||
9b873358 | 67 | if (!empty($params['notify'])) { |
6a488035 TO |
68 | $form_state['input']['notify'] = $params['notify']; |
69 | } | |
70 | ||
71 | $form_state['rebuild'] = FALSE; | |
72 | $form_state['programmed'] = TRUE; | |
805b0f6e | 73 | $form_state['complete form'] = FALSE; |
6a488035 TO |
74 | $form_state['method'] = 'post'; |
75 | $form_state['build_info']['args'] = array(); | |
41d551ad | 76 | /* |
77 | * if we want to submit this form more than once in a process (e.g. create more than one user) | |
78 | * we must force it to validate each time for this form. Otherwise it will not validate | |
79 | * subsequent submissions and the manner in which the password is passed in will be invalid | |
c490a46a | 80 | */ |
41d551ad | 81 | $form_state['must_validate'] = TRUE; |
6a488035 TO |
82 | $config = CRM_Core_Config::singleton(); |
83 | ||
84 | // we also need to redirect b | |
85 | $config->inCiviCRM = TRUE; | |
86 | ||
87 | $form = drupal_retrieve_form('user_register_form', $form_state); | |
88 | $form_state['process_input'] = 1; | |
89 | $form_state['submitted'] = 1; | |
805b0f6e | 90 | $form['#array_parents'] = array(); |
91 | $form['#tree'] = FALSE; | |
6a488035 TO |
92 | drupal_process_form('user_register_form', $form, $form_state); |
93 | ||
94 | $config->inCiviCRM = FALSE; | |
95 | ||
96 | if (form_get_errors()) { | |
97 | return FALSE; | |
98 | } | |
a5ecff8d | 99 | return $form_state['user']->uid; |
6a488035 TO |
100 | } |
101 | ||
102 | /* | |
103 | * Change user name in host CMS | |
104 | * | |
105 | * @param integer $ufID User ID in CMS | |
106 | * @param string $ufName User name | |
107 | */ | |
bb3a214a | 108 | /** |
100fef9d CW |
109 | * @param int $ufID |
110 | * @param string $ufName | |
bb3a214a | 111 | */ |
00be9182 | 112 | public function updateCMSName($ufID, $ufName) { |
6a488035 TO |
113 | // CRM-5555 |
114 | if (function_exists('user_load')) { | |
115 | $user = user_load($ufID); | |
116 | if ($user->mail != $ufName) { | |
117 | user_save($user, array('mail' => $ufName)); | |
118 | $user = user_load($ufID); | |
119 | } | |
120 | } | |
121 | } | |
122 | ||
123 | /** | |
124 | * Check if username and email exists in the drupal db | |
125 | * | |
77855840 TO |
126 | * @param array $params |
127 | * Array of name and mail values. | |
128 | * @param array $errors | |
129 | * Array of errors. | |
130 | * @param string $emailName | |
131 | * Field label for the 'email'. | |
f4aaa82a | 132 | * |
6a488035 TO |
133 | * @return void |
134 | */ | |
00be9182 | 135 | public static function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') { |
6a488035 TO |
136 | $config = CRM_Core_Config::singleton(); |
137 | ||
138 | $dao = new CRM_Core_DAO(); | |
139 | $name = $dao->escape(CRM_Utils_Array::value('name', $params)); | |
140 | $email = $dao->escape(CRM_Utils_Array::value('mail', $params)); | |
141 | $errors = form_get_errors(); | |
142 | if ($errors) { | |
143 | // unset drupal messages to avoid twice display of errors | |
144 | unset($_SESSION['messages']); | |
145 | } | |
146 | ||
a7488080 | 147 | if (!empty($params['name'])) { |
6a488035 TO |
148 | if ($nameError = user_validate_name($params['name'])) { |
149 | $errors['cms_name'] = $nameError; | |
150 | } | |
151 | else { | |
152 | $uid = db_query( | |
153 | "SELECT uid FROM {users} WHERE name = :name", | |
154 | array(':name' => $params['name']) | |
155 | )->fetchField(); | |
156 | if ((bool) $uid) { | |
157 | $errors['cms_name'] = ts('The username %1 is already taken. Please select another username.', array(1 => $params['name'])); | |
158 | } | |
159 | } | |
160 | } | |
161 | ||
a7488080 | 162 | if (!empty($params['mail'])) { |
6a488035 TO |
163 | if ($emailError = user_validate_mail($params['mail'])) { |
164 | $errors[$emailName] = $emailError; | |
165 | } | |
166 | else { | |
167 | $uid = db_query( | |
168 | "SELECT uid FROM {users} WHERE mail = :mail", | |
169 | array(':mail' => $params['mail']) | |
170 | )->fetchField(); | |
171 | if ((bool) $uid) { | |
172 | $resetUrl = $config->userFrameworkBaseURL . 'user/password'; | |
173 | $errors[$emailName] = ts('The email address %1 is already registered. <a href="%2">Have you forgotten your password?</a>', | |
174 | array(1 => $params['mail'], 2 => $resetUrl) | |
175 | ); | |
176 | } | |
177 | } | |
178 | } | |
179 | } | |
180 | ||
181 | /* | |
c490a46a | 182 | * Get the drupal destination string. When this is passed in the |
6a488035 TO |
183 | * URL the user will be directed to it after filling in the drupal form |
184 | * | |
77855840 TO |
185 | * @param CRM_Core_Form $form |
186 | * Form object representing the 'current' form - to which the user will be returned. | |
6a488035 TO |
187 | * @return string $destination destination value for URL |
188 | * | |
189 | */ | |
bb3a214a | 190 | /** |
c490a46a | 191 | * @param CRM_Core_Form $form |
bb3a214a EM |
192 | * |
193 | * @return null|string | |
194 | */ | |
00be9182 | 195 | public function getLoginDestination(&$form) { |
6a488035 TO |
196 | $args = NULL; |
197 | ||
198 | $id = $form->get('id'); | |
199 | if ($id) { | |
200 | $args .= "&id=$id"; | |
201 | } | |
202 | else { | |
203 | $gid = $form->get('gid'); | |
204 | if ($gid) { | |
205 | $args .= "&gid=$gid"; | |
206 | } | |
207 | else { | |
208 | // Setup Personal Campaign Page link uses pageId | |
209 | $pageId = $form->get('pageId'); | |
210 | if ($pageId) { | |
211 | $component = $form->get('component'); | |
212 | $args .= "&pageId=$pageId&component=$component&action=add"; | |
213 | } | |
214 | } | |
215 | } | |
216 | ||
217 | $destination = NULL; | |
218 | if ($args) { | |
219 | // append destination so user is returned to form they came from after login | |
220 | $destination = CRM_Utils_System::currentPath() . '?reset=1' . $args; | |
221 | } | |
222 | return $destination; | |
223 | } | |
224 | ||
225 | /** | |
226 | * Get user login URL for hosting CMS (method declared in each CMS system class) | |
227 | * | |
77855840 TO |
228 | * @param string $destination |
229 | * If present, add destination to querystring (works for Drupal only). | |
6a488035 TO |
230 | * |
231 | * @return string - loginURL for the current CMS | |
232 | * @static | |
233 | */ | |
234 | public function getLoginURL($destination = '') { | |
404815e3 EM |
235 | $query = $destination ? array('destination' => $destination) : array(); |
236 | return url('user', array('query' => $query)); | |
6a488035 TO |
237 | } |
238 | ||
239 | ||
240 | /** | |
100fef9d | 241 | * Sets the title of the page |
6a488035 TO |
242 | * |
243 | * @param string $title | |
f4aaa82a EM |
244 | * @param null $pageTitle |
245 | * | |
6a488035 TO |
246 | * @paqram string $pageTitle |
247 | * | |
248 | * @return void | |
6a488035 | 249 | */ |
00be9182 | 250 | public function setTitle($title, $pageTitle = NULL) { |
6a488035 TO |
251 | if (arg(0) == 'civicrm') { |
252 | if (!$pageTitle) { | |
253 | $pageTitle = $title; | |
254 | } | |
255 | ||
256 | drupal_set_title($pageTitle, PASS_THROUGH); | |
257 | } | |
258 | } | |
259 | ||
260 | /** | |
261 | * Append an additional breadcrumb tag to the existing breadcrumb | |
262 | * | |
c490a46a | 263 | * @param array $breadCrumbs |
f4aaa82a EM |
264 | * @internal param string $title |
265 | * @internal param string $url | |
6a488035 TO |
266 | * |
267 | * @return void | |
6a488035 | 268 | */ |
00be9182 | 269 | public function appendBreadCrumb($breadCrumbs) { |
6a488035 TO |
270 | $breadCrumb = drupal_get_breadcrumb(); |
271 | ||
272 | if (is_array($breadCrumbs)) { | |
273 | foreach ($breadCrumbs as $crumbs) { | |
274 | if (stripos($crumbs['url'], 'id%%')) { | |
275 | $args = array('cid', 'mid'); | |
276 | foreach ($args as $a) { | |
277 | $val = CRM_Utils_Request::retrieve($a, 'Positive', CRM_Core_DAO::$_nullObject, | |
278 | FALSE, NULL, $_GET | |
279 | ); | |
280 | if ($val) { | |
281 | $crumbs['url'] = str_ireplace("%%{$a}%%", $val, $crumbs['url']); | |
282 | } | |
283 | } | |
284 | } | |
285 | $breadCrumb[] = "<a href=\"{$crumbs['url']}\">{$crumbs['title']}</a>"; | |
286 | } | |
287 | } | |
288 | drupal_set_breadcrumb($breadCrumb); | |
289 | } | |
290 | ||
291 | /** | |
292 | * Reset an additional breadcrumb tag to the existing breadcrumb | |
293 | * | |
294 | * @return void | |
6a488035 | 295 | */ |
00be9182 | 296 | public function resetBreadCrumb() { |
6a488035 TO |
297 | $bc = array(); |
298 | drupal_set_breadcrumb($bc); | |
299 | } | |
300 | ||
301 | /** | |
302 | * Append a string to the head of the html file | |
303 | * | |
77855840 TO |
304 | * @param string $header |
305 | * The new string to be appended. | |
6a488035 TO |
306 | * |
307 | * @return void | |
6a488035 | 308 | */ |
00be9182 | 309 | public function addHTMLHead($header) { |
6a488035 TO |
310 | static $count = 0; |
311 | if (!empty($header)) { | |
312 | $key = 'civi_' . ++$count; | |
313 | $data = array( | |
314 | '#type' => 'markup', | |
315 | '#markup' => $header, | |
316 | ); | |
317 | drupal_add_html_head($data, $key); | |
318 | } | |
319 | } | |
320 | ||
321 | /** | |
322 | * Add a script file | |
323 | * | |
324 | * @param $url: string, absolute path to file | |
77855840 TO |
325 | * @param $region |
326 | * String, location within the document: 'html-header', 'page-header', 'page-footer'. | |
6a488035 TO |
327 | * |
328 | * Note: This function is not to be called directly | |
329 | * @see CRM_Core_Region::render() | |
330 | * | |
331 | * @return bool TRUE if we support this operation in this CMS, FALSE otherwise | |
6a488035 TO |
332 | */ |
333 | public function addScriptUrl($url, $region) { | |
334 | $params = array('group' => JS_LIBRARY, 'weight' => 10); | |
335 | switch ($region) { | |
336 | case 'html-header': | |
337 | case 'page-footer': | |
338 | $params['scope'] = substr($region, 5); | |
339 | break; | |
e7292422 | 340 | |
6a488035 TO |
341 | default: |
342 | return FALSE; | |
343 | } | |
344 | // If the path is within the drupal directory we can use the more efficient 'file' setting | |
42e1a97c | 345 | $params['type'] = $this->formatResourceUrl($url) ? 'file' : 'external'; |
6a488035 TO |
346 | drupal_add_js($url, $params); |
347 | return TRUE; | |
348 | } | |
349 | ||
350 | /** | |
351 | * Add an inline script | |
352 | * | |
353 | * @param $code: string, javascript code | |
77855840 TO |
354 | * @param $region |
355 | * String, location within the document: 'html-header', 'page-header', 'page-footer'. | |
6a488035 TO |
356 | * |
357 | * Note: This function is not to be called directly | |
358 | * @see CRM_Core_Region::render() | |
359 | * | |
360 | * @return bool TRUE if we support this operation in this CMS, FALSE otherwise | |
6a488035 TO |
361 | */ |
362 | public function addScript($code, $region) { | |
363 | $params = array('type' => 'inline', 'group' => JS_LIBRARY, 'weight' => 10); | |
364 | switch ($region) { | |
365 | case 'html-header': | |
366 | case 'page-footer': | |
367 | $params['scope'] = substr($region, 5); | |
368 | break; | |
e7292422 | 369 | |
6a488035 TO |
370 | default: |
371 | return FALSE; | |
372 | } | |
373 | drupal_add_js($code, $params); | |
374 | return TRUE; | |
375 | } | |
376 | ||
377 | /** | |
378 | * Add a css file | |
379 | * | |
380 | * @param $url: string, absolute path to file | |
77855840 TO |
381 | * @param $region |
382 | * String, location within the document: 'html-header', 'page-header', 'page-footer'. | |
6a488035 TO |
383 | * |
384 | * Note: This function is not to be called directly | |
385 | * @see CRM_Core_Region::render() | |
386 | * | |
387 | * @return bool TRUE if we support this operation in this CMS, FALSE otherwise | |
6a488035 TO |
388 | */ |
389 | public function addStyleUrl($url, $region) { | |
390 | if ($region != 'html-header') { | |
391 | return FALSE; | |
392 | } | |
393 | $params = array(); | |
394 | // If the path is within the drupal directory we can use the more efficient 'file' setting | |
42e1a97c | 395 | $params['type'] = $this->formatResourceUrl($url) ? 'file' : 'external'; |
6a488035 TO |
396 | drupal_add_css($url, $params); |
397 | return TRUE; | |
398 | } | |
399 | ||
400 | /** | |
401 | * Add an inline style | |
402 | * | |
403 | * @param $code: string, css code | |
77855840 TO |
404 | * @param $region |
405 | * String, location within the document: 'html-header', 'page-header', 'page-footer'. | |
6a488035 TO |
406 | * |
407 | * Note: This function is not to be called directly | |
408 | * @see CRM_Core_Region::render() | |
409 | * | |
410 | * @return bool TRUE if we support this operation in this CMS, FALSE otherwise | |
6a488035 TO |
411 | */ |
412 | public function addStyle($code, $region) { | |
413 | if ($region != 'html-header') { | |
414 | return FALSE; | |
415 | } | |
416 | $params = array('type' => 'inline'); | |
417 | drupal_add_css($code, $params); | |
418 | return TRUE; | |
419 | } | |
420 | ||
6a488035 | 421 | /** |
100fef9d | 422 | * Rewrite various system urls to https |
6a488035 TO |
423 | * |
424 | * @param null | |
425 | * | |
426 | * @return void | |
6a488035 | 427 | */ |
00be9182 | 428 | public function mapConfigToSSL() { |
6a488035 TO |
429 | global $base_url; |
430 | $base_url = str_replace('http://', 'https://', $base_url); | |
431 | } | |
432 | ||
433 | /** | |
100fef9d | 434 | * Figure out the post url for the form |
6a488035 | 435 | * |
77855840 TO |
436 | * @param mix $action |
437 | * The default action if one is pre-specified. | |
6a488035 TO |
438 | * |
439 | * @return string the url to post the form | |
6a488035 | 440 | */ |
00be9182 | 441 | public function postURL($action) { |
6a488035 TO |
442 | if (!empty($action)) { |
443 | return $action; | |
444 | } | |
445 | ||
446 | return $this->url($_GET['q']); | |
447 | } | |
448 | ||
6a488035 TO |
449 | |
450 | /** | |
451 | * Authenticate the user against the drupal db | |
452 | * | |
77855840 TO |
453 | * @param string $name |
454 | * The user name. | |
455 | * @param string $password | |
456 | * The password for the above user name. | |
457 | * @param bool $loadCMSBootstrap | |
458 | * Load cms bootstrap?. | |
a5ecff8d | 459 | * @param NULL|string $realPath filename of script |
6a488035 TO |
460 | * |
461 | * @return mixed false if no auth | |
462 | * array( | |
463 | * contactID, ufID, unique string ) if success | |
6a488035 | 464 | */ |
e7292422 | 465 | public static function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) { |
6a488035 TO |
466 | require_once 'DB.php'; |
467 | ||
468 | $config = CRM_Core_Config::singleton(); | |
469 | ||
470 | $dbDrupal = DB::connect($config->userFrameworkDSN); | |
471 | if (DB::isError($dbDrupal)) { | |
472 | CRM_Core_Error::fatal("Cannot connect to drupal db via $config->userFrameworkDSN, " . $dbDrupal->getMessage()); | |
473 | } | |
474 | ||
475 | $account = $userUid = $userMail = NULL; | |
476 | if ($loadCMSBootstrap) { | |
477 | $bootStrapParams = array(); | |
478 | if ($name && $password) { | |
479 | $bootStrapParams = array( | |
480 | 'name' => $name, | |
481 | 'pass' => $password, | |
482 | ); | |
483 | } | |
484 | CRM_Utils_System::loadBootStrap($bootStrapParams, TRUE, TRUE, $realPath); | |
485 | ||
486 | global $user; | |
487 | if ($user) { | |
488 | $userUid = $user->uid; | |
489 | $userMail = $user->mail; | |
490 | } | |
491 | } | |
492 | else { | |
493 | // CRM-8638 | |
494 | // SOAP cannot load drupal bootstrap and hence we do it the old way | |
495 | // Contact CiviSMTP folks if we run into issues with this :) | |
496 | $cmsPath = $config->userSystem->cmsRootPath($realPath); | |
497 | ||
e7292422 TO |
498 | require_once "$cmsPath/includes/bootstrap.inc"; |
499 | require_once "$cmsPath/includes/password.inc"; | |
6a488035 TO |
500 | |
501 | $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; | |
502 | $name = $dbDrupal->escapeSimple($strtolower($name)); | |
503 | $sql = " | |
504 | SELECT u.* | |
505 | FROM {$config->userFrameworkUsersTableName} u | |
506 | WHERE LOWER(u.name) = '$name' | |
507 | AND u.status = 1 | |
508 | "; | |
509 | ||
510 | $query = $dbDrupal->query($sql); | |
511 | $row = $query->fetchRow(DB_FETCHMODE_ASSOC); | |
512 | ||
513 | if ($row) { | |
514 | $fakeDrupalAccount = drupal_anonymous_user(); | |
515 | $fakeDrupalAccount->name = $name; | |
516 | $fakeDrupalAccount->pass = $row['pass']; | |
517 | $passwordCheck = user_check_password($password, $fakeDrupalAccount); | |
518 | if ($passwordCheck) { | |
519 | $userUid = $row['uid']; | |
520 | $userMail = $row['mail']; | |
521 | } | |
522 | } | |
523 | } | |
524 | ||
525 | if ($userUid && $userMail) { | |
526 | CRM_Core_BAO_UFMatch::synchronizeUFMatch($account, $userUid, $userMail, 'Drupal'); | |
527 | $contactID = CRM_Core_BAO_UFMatch::getContactId($userUid); | |
528 | if (!$contactID) { | |
529 | return FALSE; | |
530 | } | |
531 | return array($contactID, $userUid, mt_rand()); | |
532 | } | |
533 | return FALSE; | |
534 | } | |
535 | ||
536 | /* | |
537 | * Load user into session | |
538 | */ | |
bb3a214a | 539 | /** |
100fef9d | 540 | * @param string $username |
bb3a214a EM |
541 | * |
542 | * @return bool | |
543 | */ | |
00be9182 | 544 | public function loadUser($username) { |
6a488035 TO |
545 | global $user; |
546 | ||
547 | $user = user_load_by_name($username); | |
548 | ||
549 | if (empty($user->uid)) { | |
550 | return FALSE; | |
551 | } | |
552 | ||
553 | $uid = $user->uid; | |
554 | $contact_id = CRM_Core_BAO_UFMatch::getContactId($uid); | |
555 | ||
556 | // lets store contact id and user id in session | |
557 | $session = CRM_Core_Session::singleton(); | |
558 | $session->set('ufID', $uid); | |
559 | $session->set('userID', $contact_id); | |
560 | return TRUE; | |
561 | } | |
562 | ||
82d9c21e | 563 | /** |
53980972 | 564 | * Perform any post login activities required by the UF - |
565 | * e.g. for drupal: records a watchdog message about the new session, saves the login timestamp, | |
566 | * calls hook_user op 'login' and generates a new session. | |
e43cc689 | 567 | * |
95d68223 TO |
568 | * @param array params |
569 | * | |
570 | * FIXME: Document values accepted/required by $params | |
53980972 | 571 | */ |
9b873358 | 572 | public function userLoginFinalize($params = array()) { |
53980972 | 573 | user_login_finalize($params); |
82d9c21e | 574 | } |
575 | ||
46b6363c TO |
576 | /** |
577 | * Determine the native ID of the CMS user | |
578 | * | |
100fef9d | 579 | * @param string $username |
46b6363c TO |
580 | * @return int|NULL |
581 | */ | |
00be9182 | 582 | public function getUfId($username) { |
46b6363c TO |
583 | $user = user_load_by_name($username); |
584 | if (empty($user->uid)) { | |
585 | return NULL; | |
586 | } | |
587 | return $user->uid; | |
588 | } | |
589 | ||
6a488035 TO |
590 | /** |
591 | * Set a message in the UF to display to a user | |
592 | * | |
77855840 TO |
593 | * @param string $message |
594 | * The message to set. | |
6a488035 | 595 | * |
6a488035 | 596 | */ |
00be9182 | 597 | public function setMessage($message) { |
6a488035 TO |
598 | drupal_set_message($message); |
599 | } | |
600 | ||
bb3a214a EM |
601 | /** |
602 | * @return mixed | |
603 | */ | |
00be9182 | 604 | public function logout() { |
6a488035 TO |
605 | module_load_include('inc', 'user', 'user.pages'); |
606 | return user_logout(); | |
607 | } | |
608 | ||
00be9182 | 609 | public function updateCategories() { |
6a488035 TO |
610 | // copied this from profile.module. Seems a bit inefficient, but i dont know a better way |
611 | // CRM-3600 | |
612 | cache_clear_all(); | |
613 | menu_rebuild(); | |
614 | } | |
615 | ||
616 | /** | |
617 | * Get the default location for CiviCRM blocks | |
618 | * | |
619 | * @return string | |
620 | */ | |
00be9182 | 621 | public function getDefaultBlockLocation() { |
6a488035 TO |
622 | return 'sidebar_first'; |
623 | } | |
624 | ||
625 | /** | |
626 | * Get the locale set in the hosting CMS | |
627 | * | |
628 | * @return string with the locale or null for none | |
629 | */ | |
00be9182 | 630 | public function getUFLocale() { |
6a488035 TO |
631 | // return CiviCRM’s xx_YY locale that either matches Drupal’s Chinese locale |
632 | // (for CRM-6281), Drupal’s xx_YY or is retrieved based on Drupal’s xx | |
1c9f455c | 633 | // sometimes for CLI based on order called, this might not be set and/or empty |
6a488035 | 634 | global $language; |
6a488035 | 635 | |
1c9f455c DL |
636 | if (empty($language)) { |
637 | return NULL; | |
638 | } | |
6a488035 | 639 | |
1c9f455c DL |
640 | if ($language->language == 'zh-hans') { |
641 | return 'zh_CN'; | |
642 | } | |
6a488035 | 643 | |
1c9f455c DL |
644 | if ($language->language == 'zh-hant') { |
645 | return 'zh_TW'; | |
6a488035 | 646 | } |
1c9f455c DL |
647 | |
648 | if (preg_match('/^.._..$/', $language->language)) { | |
649 | return $language->language; | |
650 | } | |
651 | ||
652 | return CRM_Core_I18n_PseudoConstant::longForShort(substr($language->language, 0, 2)); | |
6a488035 TO |
653 | } |
654 | ||
bb3a214a EM |
655 | /** |
656 | * @return string | |
657 | */ | |
00be9182 | 658 | public function getVersion() { |
6a488035 TO |
659 | return defined('VERSION') ? VERSION : 'Unknown'; |
660 | } | |
661 | ||
662 | /** | |
100fef9d | 663 | * Load drupal bootstrap |
6a488035 | 664 | * |
77855840 TO |
665 | * @param array $params |
666 | * Either uid, or name & pass. | |
667 | * @param bool $loadUser | |
668 | * Boolean Require CMS user load. | |
669 | * @param bool $throwError | |
670 | * If true, print error on failure and exit. | |
671 | * @param bool|string $realPath path to script | |
f4aaa82a EM |
672 | * |
673 | * @return bool | |
6a488035 | 674 | */ |
00be9182 | 675 | public function loadBootStrap($params = array(), $loadUser = TRUE, $throwError = TRUE, $realPath = NULL) { |
6a488035 TO |
676 | //take the cms root path. |
677 | $cmsPath = $this->cmsRootPath($realPath); | |
678 | ||
679 | if (!file_exists("$cmsPath/includes/bootstrap.inc")) { | |
680 | if ($throwError) { | |
681 | echo '<br />Sorry, could not locate bootstrap.inc\n'; | |
682 | exit(); | |
683 | } | |
684 | return FALSE; | |
685 | } | |
686 | // load drupal bootstrap | |
687 | chdir($cmsPath); | |
688 | define('DRUPAL_ROOT', $cmsPath); | |
689 | ||
690 | // For drupal multi-site CRM-11313 | |
691 | if ($realPath && strpos($realPath, 'sites/all/modules/') === FALSE) { | |
692 | preg_match('@sites/([^/]*)/modules@s', $realPath, $matches); | |
693 | if (!empty($matches[1])) { | |
694 | $_SERVER['HTTP_HOST'] = $matches[1]; | |
695 | } | |
696 | } | |
697 | require_once 'includes/bootstrap.inc'; | |
38507482 CB |
698 | // @ to suppress notices eg 'DRUPALFOO already defined'. |
699 | @drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); | |
6a488035 TO |
700 | |
701 | // explicitly setting error reporting, since we cannot handle drupal related notices | |
702 | error_reporting(1); | |
0af0e4c9 | 703 | if (!function_exists('module_exists') || !module_exists('civicrm')) { |
6a488035 TO |
704 | if ($throwError) { |
705 | echo '<br />Sorry, could not load drupal bootstrap.'; | |
706 | exit(); | |
707 | } | |
708 | return FALSE; | |
709 | } | |
710 | ||
711 | // seems like we've bootstrapped drupal | |
712 | $config = CRM_Core_Config::singleton(); | |
713 | ||
6a488035 TO |
714 | // lets also fix the clean url setting |
715 | // CRM-6948 | |
716 | $config->cleanURL = (int) variable_get('clean_url', '0'); | |
717 | ||
718 | // we need to call the config hook again, since we now know | |
719 | // all the modules that are listening on it, does not apply | |
720 | // to J! and WP as yet | |
721 | // CRM-8655 | |
722 | CRM_Utils_Hook::config($config); | |
723 | ||
724 | if (!$loadUser) { | |
725 | return TRUE; | |
726 | } | |
727 | ||
728 | $uid = CRM_Utils_Array::value('uid', $params); | |
729 | if (!$uid) { | |
730 | //load user, we need to check drupal permissions. | |
731 | $name = CRM_Utils_Array::value('name', $params, FALSE) ? $params['name'] : trim(CRM_Utils_Array::value('name', $_REQUEST)); | |
732 | $pass = CRM_Utils_Array::value('pass', $params, FALSE) ? $params['pass'] : trim(CRM_Utils_Array::value('pass', $_REQUEST)); | |
733 | ||
734 | if ($name) { | |
735 | $uid = user_authenticate($name, $pass); | |
736 | if (!$uid) { | |
737 | if ($throwError) { | |
738 | echo '<br />Sorry, unrecognized username or password.'; | |
739 | exit(); | |
740 | } | |
741 | return FALSE; | |
742 | } | |
743 | } | |
744 | } | |
745 | ||
746 | if ($uid) { | |
747 | $account = user_load($uid); | |
748 | if ($account && $account->uid) { | |
749 | global $user; | |
750 | $user = $account; | |
751 | return TRUE; | |
752 | } | |
753 | } | |
754 | ||
755 | if ($throwError) { | |
756 | echo '<br />Sorry, can not load CMS user account.'; | |
757 | exit(); | |
758 | } | |
759 | ||
0af0e4c9 DL |
760 | // CRM-6948: When using loadBootStrap, it's implicit that CiviCRM has already loaded its settings |
761 | // which means that define(CIVICRM_CLEANURL) was correctly set. | |
6a488035 TO |
762 | // So we correct it |
763 | $config = CRM_Core_Config::singleton(); | |
e7292422 | 764 | $config->cleanURL = (int) variable_get('clean_url', '0'); |
6a488035 TO |
765 | |
766 | // CRM-8655: Drupal wasn't available during bootstrap, so hook_civicrm_config never executes | |
767 | CRM_Utils_Hook::config($config); | |
768 | ||
769 | return FALSE; | |
770 | } | |
771 | ||
a5ecff8d CB |
772 | /** |
773 | * | |
774 | */ | |
00be9182 | 775 | public function cmsRootPath($scriptFilename = NULL) { |
6a488035 TO |
776 | $cmsRoot = $valid = NULL; |
777 | ||
778 | if (!is_null($scriptFilename)) { | |
779 | $path = $scriptFilename; | |
780 | } | |
781 | else { | |
782 | $path = $_SERVER['SCRIPT_FILENAME']; | |
783 | } | |
784 | ||
785 | if (function_exists('drush_get_context')) { | |
786 | // drush anyway takes care of multisite install etc | |
787 | return drush_get_context('DRUSH_DRUPAL_ROOT'); | |
788 | } | |
789 | // CRM-7582 | |
790 | $pathVars = explode('/', | |
791 | str_replace('//', '/', | |
792 | str_replace('\\', '/', $path) | |
793 | ) | |
794 | ); | |
795 | ||
796 | //lets store first var, | |
797 | //need to get back for windows. | |
798 | $firstVar = array_shift($pathVars); | |
799 | ||
800 | //lets remove sript name to reduce one iteration. | |
801 | array_pop($pathVars); | |
802 | ||
948d11bf CB |
803 | // CRM-7429 -- do check for uppermost 'includes' dir, which would |
804 | // work for multisite installation. | |
6a488035 TO |
805 | do { |
806 | $cmsRoot = $firstVar . '/' . implode('/', $pathVars); | |
807 | $cmsIncludePath = "$cmsRoot/includes"; | |
948d11bf | 808 | // Stop if we find bootstrap. |
f81c7606 | 809 | if (file_exists("$cmsIncludePath/bootstrap.inc")) { |
6a488035 TO |
810 | $valid = TRUE; |
811 | break; | |
812 | } | |
813 | //remove one directory level. | |
814 | array_pop($pathVars); | |
815 | } while (count($pathVars)); | |
816 | ||
817 | return ($valid) ? $cmsRoot : NULL; | |
818 | } | |
819 | ||
820 | /** | |
100fef9d | 821 | * Check is user logged in. |
6a488035 TO |
822 | * |
823 | * @return boolean true/false. | |
824 | */ | |
825 | public function isUserLoggedIn() { | |
826 | $isloggedIn = FALSE; | |
827 | if (function_exists('user_is_logged_in')) { | |
828 | $isloggedIn = user_is_logged_in(); | |
829 | } | |
830 | ||
831 | return $isloggedIn; | |
832 | } | |
833 | ||
834 | /** | |
835 | * Get currently logged in user uf id. | |
836 | * | |
837 | * @return int $userID logged in user uf id. | |
838 | */ | |
839 | public function getLoggedInUfID() { | |
840 | $ufID = NULL; | |
841 | if (function_exists('user_is_logged_in') && | |
842 | user_is_logged_in() && | |
843 | function_exists('user_uid_optional_to_arg') | |
844 | ) { | |
845 | $ufID = user_uid_optional_to_arg(array()); | |
846 | } | |
847 | ||
848 | return $ufID; | |
849 | } | |
850 | ||
851 | /** | |
852 | * Format the url as per language Negotiation. | |
853 | * | |
854 | * @param string $url | |
855 | * | |
f4aaa82a EM |
856 | * @param bool $addLanguagePart |
857 | * @param bool $removeLanguagePart | |
858 | * | |
6a488035 TO |
859 | * @return string $url, formatted url. |
860 | * @static | |
861 | */ | |
00be9182 | 862 | public function languageNegotiationURL($url, $addLanguagePart = TRUE, $removeLanguagePart = FALSE) { |
6a488035 TO |
863 | if (empty($url)) { |
864 | return $url; | |
865 | } | |
866 | ||
867 | //CRM-7803 -from d7 onward. | |
868 | $config = CRM_Core_Config::singleton(); | |
869 | if (function_exists('variable_get') && | |
870 | module_exists('locale') && | |
871 | function_exists('language_negotiation_get') | |
872 | ) { | |
873 | global $language; | |
874 | ||
875 | //does user configuration allow language | |
876 | //support from the URL (Path prefix or domain) | |
877 | if (language_negotiation_get('language') == 'locale-url') { | |
878 | $urlType = variable_get('locale_language_negotiation_url_part'); | |
879 | ||
880 | //url prefix | |
881 | if ($urlType == LOCALE_LANGUAGE_NEGOTIATION_URL_PREFIX) { | |
882 | if (isset($language->prefix) && $language->prefix) { | |
883 | if ($addLanguagePart) { | |
3fe61451 | 884 | $url .= $language->prefix . '/'; |
6a488035 TO |
885 | } |
886 | if ($removeLanguagePart) { | |
887 | $url = str_replace("/{$language->prefix}/", '/', $url); | |
888 | } | |
889 | } | |
890 | } | |
891 | //domain | |
892 | if ($urlType == LOCALE_LANGUAGE_NEGOTIATION_URL_DOMAIN) { | |
893 | if (isset($language->domain) && $language->domain) { | |
894 | if ($addLanguagePart) { | |
b276048c | 895 | $cleanedUrl = preg_replace('#^https?://#', '', $language->domain); |
ea54c1ae | 896 | // drupal function base_path() adds a "/" to the beginning and end of the returned path |
34020e33 | 897 | if (substr($cleanedUrl, -1) == '/') { |
ea54c1ae LS |
898 | $cleanedUrl = substr($cleanedUrl, 0, -1); |
899 | } | |
b276048c | 900 | $url = (CRM_Utils_System::isSSL() ? 'https' : 'http') . '://' . $cleanedUrl . base_path(); |
6a488035 TO |
901 | } |
902 | if ($removeLanguagePart && defined('CIVICRM_UF_BASEURL')) { | |
903 | $url = str_replace('\\', '/', $url); | |
904 | $parseUrl = parse_url($url); | |
905 | ||
906 | //kinda hackish but not sure how to do it right | |
907 | //hope http_build_url() will help at some point. | |
908 | if (is_array($parseUrl) && !empty($parseUrl)) { | |
909 | $urlParts = explode('/', $url); | |
910 | $hostKey = array_search($parseUrl['host'], $urlParts); | |
911 | $ufUrlParts = parse_url(CIVICRM_UF_BASEURL); | |
912 | $urlParts[$hostKey] = $ufUrlParts['host']; | |
913 | $url = implode('/', $urlParts); | |
914 | } | |
915 | } | |
916 | } | |
917 | } | |
918 | } | |
919 | } | |
920 | ||
921 | return $url; | |
922 | } | |
923 | ||
924 | /** | |
925 | * Find any users/roles/security-principals with the given permission | |
926 | * and replace it with one or more permissions. | |
927 | * | |
77855840 TO |
928 | * @param $oldPerm |
929 | * String. | |
930 | * @param $newPerms | |
931 | * Array, strings. | |
6a488035 TO |
932 | * |
933 | * @return void | |
934 | */ | |
00be9182 | 935 | public function replacePermission($oldPerm, $newPerms) { |
6a488035 TO |
936 | $roles = user_roles(FALSE, $oldPerm); |
937 | if (!empty($roles)) { | |
938 | foreach (array_keys($roles) as $rid) { | |
939 | user_role_revoke_permissions($rid, array($oldPerm)); | |
940 | user_role_grant_permissions($rid, $newPerms); | |
941 | } | |
942 | } | |
943 | } | |
944 | ||
945 | /** | |
946 | * Get a list of all installed modules, including enabled and disabled ones | |
947 | * | |
948 | * @return array CRM_Core_Module | |
949 | */ | |
00be9182 | 950 | public function getModules() { |
6a488035 TO |
951 | $result = array(); |
952 | $q = db_query('SELECT name, status FROM {system} WHERE type = \'module\' AND schema_version <> -1'); | |
953 | foreach ($q as $row) { | |
954 | $result[] = new CRM_Core_Module('drupal.' . $row->name, ($row->status == 1) ? TRUE : FALSE); | |
955 | } | |
956 | return $result; | |
957 | } | |
958 | ||
959 | /** | |
960 | * Wrapper for og_membership creation | |
a5ecff8d | 961 | * |
77855840 TO |
962 | * @param int $ogID |
963 | * Organic Group ID. | |
964 | * @param int $drupalID | |
965 | * Drupal User ID. | |
6a488035 | 966 | */ |
9b873358 | 967 | public function og_membership_create($ogID, $drupalID) { |
6a488035 | 968 | if (function_exists('og_entity_query_alter')) { |
a5ecff8d CB |
969 | // sort-of-randomly chose a function that only exists in the // 7.x-2.x branch |
970 | // | |
971 | // @TODO Find more solid way to check - try system_get_info('module', 'og'). | |
972 | // | |
973 | // Also, since we don't know how to get the entity type of the // group, we'll assume it's 'node' | |
6a488035 TO |
974 | og_group('node', $ogID, array('entity' => user_load($drupalID))); |
975 | } | |
976 | else { | |
977 | // Works for the OG 7.x-1.x branch | |
978 | og_group($ogID, array('entity' => user_load($drupalID))); | |
979 | } | |
980 | } | |
981 | ||
982 | /** | |
983 | * Wrapper for og_membership deletion | |
a5ecff8d | 984 | * |
77855840 TO |
985 | * @param int $ogID |
986 | * Organic Group ID. | |
987 | * @param int $drupalID | |
988 | * Drupal User ID. | |
6a488035 | 989 | */ |
00be9182 | 990 | public function og_membership_delete($ogID, $drupalID) { |
6a488035 TO |
991 | if (function_exists('og_entity_query_alter')) { |
992 | // sort-of-randomly chose a function that only exists in the 7.x-2.x branch | |
993 | // TODO: Find a more solid way to make this test | |
994 | // Also, since we don't know how to get the entity type of the group, we'll assume it's 'node' | |
995 | og_ungroup('node', $ogID, 'user', user_load($drupalID)); | |
0db6c3e1 TO |
996 | } |
997 | else { | |
6a488035 TO |
998 | // Works for the OG 7.x-1.x branch |
999 | og_ungroup($ogID, 'user', user_load($drupalID)); | |
1000 | } | |
1001 | } | |
d8a4acc0 | 1002 | |
5a604d61 | 1003 | /** |
48ec57ab TO |
1004 | * Over-ridable function to get timezone as a string eg. |
1005 | * @return string Timezone e.g. 'America/Los_Angeles' | |
5a604d61 | 1006 | */ |
00be9182 | 1007 | public function getTimeZoneString() { |
5a604d61 E |
1008 | global $user; |
1009 | if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) { | |
1010 | $timezone = $user->timezone; | |
0db6c3e1 TO |
1011 | } |
1012 | else { | |
e7292422 | 1013 | $timezone = variable_get('date_default_timezone', NULL); |
5a604d61 | 1014 | } |
48ec57ab TO |
1015 | if (!$timezone) { |
1016 | $timezone = parent::getTimeZoneString(); | |
5a604d61 | 1017 | } |
48ec57ab | 1018 | return $timezone; |
5a604d61 | 1019 | } |
d8a4acc0 C |
1020 | /** |
1021 | * Reset any system caches that may be required for proper CiviCRM | |
1022 | * integration. | |
1023 | */ | |
00be9182 | 1024 | public function flush() { |
d8a4acc0 C |
1025 | drupal_flush_all_caches(); |
1026 | } | |
6a488035 | 1027 | } |