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