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