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) { | |
66 | $form_state['input']['pass'] = array('pass1'=>$params['cms_pass'],'pass2'=>$params['cms_pass']); | |
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 | } | |
101 | else { | |
102 | return $form_state['user']->uid; | |
103 | } | |
104 | } | |
105 | ||
106 | /* | |
107 | * Change user name in host CMS | |
108 | * | |
109 | * @param integer $ufID User ID in CMS | |
110 | * @param string $ufName User name | |
111 | */ | |
112 | function updateCMSName($ufID, $ufName) { | |
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 | * | |
126 | * @params $params array array of name and mail values | |
127 | * @params $errors array array of errors | |
128 | * @params $emailName string field label for the 'email' | |
129 | * | |
130 | * @return void | |
131 | */ | |
132 | static function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') { | |
133 | $config = CRM_Core_Config::singleton(); | |
134 | ||
135 | $dao = new CRM_Core_DAO(); | |
136 | $name = $dao->escape(CRM_Utils_Array::value('name', $params)); | |
137 | $email = $dao->escape(CRM_Utils_Array::value('mail', $params)); | |
138 | $errors = form_get_errors(); | |
139 | if ($errors) { | |
140 | // unset drupal messages to avoid twice display of errors | |
141 | unset($_SESSION['messages']); | |
142 | } | |
143 | ||
144 | if (CRM_Utils_Array::value('name', $params)) { | |
145 | if ($nameError = user_validate_name($params['name'])) { | |
146 | $errors['cms_name'] = $nameError; | |
147 | } | |
148 | else { | |
149 | $uid = db_query( | |
150 | "SELECT uid FROM {users} WHERE name = :name", | |
151 | array(':name' => $params['name']) | |
152 | )->fetchField(); | |
153 | if ((bool) $uid) { | |
154 | $errors['cms_name'] = ts('The username %1 is already taken. Please select another username.', array(1 => $params['name'])); | |
155 | } | |
156 | } | |
157 | } | |
158 | ||
159 | if (CRM_Utils_Array::value('mail', $params)) { | |
160 | if ($emailError = user_validate_mail($params['mail'])) { | |
161 | $errors[$emailName] = $emailError; | |
162 | } | |
163 | else { | |
164 | $uid = db_query( | |
165 | "SELECT uid FROM {users} WHERE mail = :mail", | |
166 | array(':mail' => $params['mail']) | |
167 | )->fetchField(); | |
168 | if ((bool) $uid) { | |
169 | $resetUrl = $config->userFrameworkBaseURL . 'user/password'; | |
170 | $errors[$emailName] = ts('The email address %1 is already registered. <a href="%2">Have you forgotten your password?</a>', | |
171 | array(1 => $params['mail'], 2 => $resetUrl) | |
172 | ); | |
173 | } | |
174 | } | |
175 | } | |
176 | } | |
177 | ||
178 | /* | |
179 | * Function to get the drupal destination string. When this is passed in the | |
180 | * URL the user will be directed to it after filling in the drupal form | |
181 | * | |
182 | * @param object $form Form object representing the 'current' form - to which the user will be returned | |
183 | * @return string $destination destination value for URL | |
184 | * | |
185 | */ | |
186 | function getLoginDestination(&$form) { | |
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 | * | |
219 | * @param string $destination - if present, add destination to querystring (works for Drupal only) | |
220 | * | |
221 | * @return string - loginURL for the current CMS | |
222 | * @static | |
223 | */ | |
224 | public function getLoginURL($destination = '') { | |
225 | $config = CRM_Core_Config::singleton(); | |
226 | $loginURL = $config->userFrameworkBaseURL; | |
227 | $loginURL .= 'user'; | |
228 | if (!empty($destination)) { | |
229 | // append destination so user is returned to form they came from after login | |
230 | $loginURL .= '?destination=' . urlencode($destination); | |
231 | } | |
232 | return $loginURL; | |
233 | } | |
234 | ||
235 | ||
236 | /** | |
237 | * sets the title of the page | |
238 | * | |
239 | * @param string $title | |
240 | * @paqram string $pageTitle | |
241 | * | |
242 | * @return void | |
243 | * @access public | |
244 | */ | |
245 | function setTitle($title, $pageTitle = NULL) { | |
246 | if (arg(0) == 'civicrm') { | |
247 | if (!$pageTitle) { | |
248 | $pageTitle = $title; | |
249 | } | |
250 | ||
251 | drupal_set_title($pageTitle, PASS_THROUGH); | |
252 | } | |
253 | } | |
254 | ||
255 | /** | |
256 | * Append an additional breadcrumb tag to the existing breadcrumb | |
257 | * | |
258 | * @param string $title | |
259 | * @param string $url | |
260 | * | |
261 | * @return void | |
262 | * @access public | |
263 | */ | |
264 | function appendBreadCrumb($breadCrumbs) { | |
265 | $breadCrumb = drupal_get_breadcrumb(); | |
266 | ||
267 | if (is_array($breadCrumbs)) { | |
268 | foreach ($breadCrumbs as $crumbs) { | |
269 | if (stripos($crumbs['url'], 'id%%')) { | |
270 | $args = array('cid', 'mid'); | |
271 | foreach ($args as $a) { | |
272 | $val = CRM_Utils_Request::retrieve($a, 'Positive', CRM_Core_DAO::$_nullObject, | |
273 | FALSE, NULL, $_GET | |
274 | ); | |
275 | if ($val) { | |
276 | $crumbs['url'] = str_ireplace("%%{$a}%%", $val, $crumbs['url']); | |
277 | } | |
278 | } | |
279 | } | |
280 | $breadCrumb[] = "<a href=\"{$crumbs['url']}\">{$crumbs['title']}</a>"; | |
281 | } | |
282 | } | |
283 | drupal_set_breadcrumb($breadCrumb); | |
284 | } | |
285 | ||
286 | /** | |
287 | * Reset an additional breadcrumb tag to the existing breadcrumb | |
288 | * | |
289 | * @return void | |
290 | * @access public | |
291 | */ | |
292 | function resetBreadCrumb() { | |
293 | $bc = array(); | |
294 | drupal_set_breadcrumb($bc); | |
295 | } | |
296 | ||
297 | /** | |
298 | * Append a string to the head of the html file | |
299 | * | |
300 | * @param string $header the new string to be appended | |
301 | * | |
302 | * @return void | |
303 | * @access public | |
304 | */ | |
305 | function addHTMLHead($header) { | |
306 | static $count = 0; | |
307 | if (!empty($header)) { | |
308 | $key = 'civi_' . ++$count; | |
309 | $data = array( | |
310 | '#type' => 'markup', | |
311 | '#markup' => $header, | |
312 | ); | |
313 | drupal_add_html_head($data, $key); | |
314 | } | |
315 | } | |
316 | ||
317 | /** | |
318 | * Add a script file | |
319 | * | |
320 | * @param $url: string, absolute path to file | |
321 | * @param $region string, location within the document: 'html-header', 'page-header', 'page-footer' | |
322 | * | |
323 | * Note: This function is not to be called directly | |
324 | * @see CRM_Core_Region::render() | |
325 | * | |
326 | * @return bool TRUE if we support this operation in this CMS, FALSE otherwise | |
327 | * @access public | |
328 | */ | |
329 | public function addScriptUrl($url, $region) { | |
330 | $params = array('group' => JS_LIBRARY, 'weight' => 10); | |
331 | switch ($region) { | |
332 | case 'html-header': | |
333 | case 'page-footer': | |
334 | $params['scope'] = substr($region, 5); | |
335 | break; | |
336 | default: | |
337 | return FALSE; | |
338 | } | |
339 | // If the path is within the drupal directory we can use the more efficient 'file' setting | |
340 | $params['type'] = self::formatResourceUrl($url) ? 'file' : 'external'; | |
341 | drupal_add_js($url, $params); | |
342 | return TRUE; | |
343 | } | |
344 | ||
345 | /** | |
346 | * Add an inline script | |
347 | * | |
348 | * @param $code: string, javascript code | |
349 | * @param $region string, location within the document: 'html-header', 'page-header', 'page-footer' | |
350 | * | |
351 | * Note: This function is not to be called directly | |
352 | * @see CRM_Core_Region::render() | |
353 | * | |
354 | * @return bool TRUE if we support this operation in this CMS, FALSE otherwise | |
355 | * @access public | |
356 | */ | |
357 | public function addScript($code, $region) { | |
358 | $params = array('type' => 'inline', 'group' => JS_LIBRARY, 'weight' => 10); | |
359 | switch ($region) { | |
360 | case 'html-header': | |
361 | case 'page-footer': | |
362 | $params['scope'] = substr($region, 5); | |
363 | break; | |
364 | default: | |
365 | return FALSE; | |
366 | } | |
367 | drupal_add_js($code, $params); | |
368 | return TRUE; | |
369 | } | |
370 | ||
371 | /** | |
372 | * Add a css file | |
373 | * | |
374 | * @param $url: string, absolute path to file | |
375 | * @param $region string, location within the document: 'html-header', 'page-header', 'page-footer' | |
376 | * | |
377 | * Note: This function is not to be called directly | |
378 | * @see CRM_Core_Region::render() | |
379 | * | |
380 | * @return bool TRUE if we support this operation in this CMS, FALSE otherwise | |
381 | * @access public | |
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 | |
389 | $params['type'] = self::formatResourceUrl($url) ? 'file' : 'external'; | |
390 | drupal_add_css($url, $params); | |
391 | return TRUE; | |
392 | } | |
393 | ||
394 | /** | |
395 | * Add an inline style | |
396 | * | |
397 | * @param $code: string, css code | |
398 | * @param $region string, location within the document: 'html-header', 'page-header', 'page-footer' | |
399 | * | |
400 | * Note: This function is not to be called directly | |
401 | * @see CRM_Core_Region::render() | |
402 | * | |
403 | * @return bool TRUE if we support this operation in this CMS, FALSE otherwise | |
404 | * @access public | |
405 | */ | |
406 | public function addStyle($code, $region) { | |
407 | if ($region != 'html-header') { | |
408 | return FALSE; | |
409 | } | |
410 | $params = array('type' => 'inline'); | |
411 | drupal_add_css($code, $params); | |
412 | return TRUE; | |
413 | } | |
414 | ||
415 | /** | |
416 | * Check if a resource url is within the drupal directory and format appropriately | |
417 | * | |
418 | * @param url (reference) | |
419 | * | |
420 | * @return bool: TRUE for internal paths, FALSE for external | |
421 | */ | |
422 | static function formatResourceUrl(&$url) { | |
423 | $internal = FALSE; | |
424 | $base = CRM_Core_Config::singleton()->resourceBase; | |
425 | global $base_url; | |
426 | // Handle absolute urls | |
427 | if (strpos($url, $base_url) === 0) { | |
428 | $internal = TRUE; | |
429 | $url = trim(str_replace($base_url, '', $url), '/'); | |
430 | } | |
431 | // Handle relative urls | |
432 | elseif (strpos($url, $base) === 0) { | |
433 | $internal = TRUE; | |
434 | $url = substr(drupal_get_path('module', 'civicrm'), 0, -6) . trim(substr($url, strlen($base)), '/'); | |
435 | } | |
436 | // Strip query string | |
437 | $q = strpos($url, '?'); | |
438 | if ($q && $internal) { | |
439 | $url = substr($url, 0, $q); | |
440 | } | |
441 | return $internal; | |
442 | } | |
443 | ||
444 | /** | |
445 | * rewrite various system urls to https | |
446 | * | |
447 | * @param null | |
448 | * | |
449 | * @return void | |
450 | * @access public | |
451 | */ | |
452 | function mapConfigToSSL() { | |
453 | global $base_url; | |
454 | $base_url = str_replace('http://', 'https://', $base_url); | |
455 | } | |
456 | ||
457 | /** | |
458 | * figure out the post url for the form | |
459 | * | |
460 | * @param mix $action the default action if one is pre-specified | |
461 | * | |
462 | * @return string the url to post the form | |
463 | * @access public | |
464 | */ | |
465 | function postURL($action) { | |
466 | if (!empty($action)) { | |
467 | return $action; | |
468 | } | |
469 | ||
470 | return $this->url($_GET['q']); | |
471 | } | |
472 | ||
473 | /** | |
474 | * Generate an internal CiviCRM URL (copied from DRUPAL/includes/common.inc#url) | |
475 | * | |
476 | * @param $path string The path being linked to, such as "civicrm/add" | |
477 | * @param $query string A query string to append to the link. | |
478 | * @param $absolute boolean Whether to force the output to be an absolute link (beginning with http:). | |
479 | * Useful for links that will be displayed outside the site, such as in an | |
480 | * RSS feed. | |
481 | * @param $fragment string A fragment identifier (named anchor) to append to the link. | |
482 | * @param $htmlize boolean whether to convert to html eqivalant | |
483 | * @param $frontend boolean a gross joomla hack | |
484 | * | |
485 | * @return string an HTML string containing a link to the given path. | |
486 | * @access public | |
487 | * | |
488 | */ | |
489 | function url($path = NULL, $query = NULL, $absolute = FALSE, | |
490 | $fragment = NULL, $htmlize = TRUE, | |
491 | $frontend = FALSE, $forceBackend = FALSE | |
492 | ) { | |
493 | $config = CRM_Core_Config::singleton(); | |
494 | $script = 'index.php'; | |
495 | ||
496 | $path = CRM_Utils_String::stripPathChars($path); | |
497 | ||
498 | if (isset($fragment)) { | |
499 | $fragment = '#' . $fragment; | |
500 | } | |
501 | ||
502 | if (!isset($config->useFrameworkRelativeBase)) { | |
503 | $base = parse_url($config->userFrameworkBaseURL); | |
504 | $config->useFrameworkRelativeBase = $base['path']; | |
505 | } | |
506 | $base = $absolute ? $config->userFrameworkBaseURL : $config->useFrameworkRelativeBase; | |
507 | ||
508 | $separator = $htmlize ? '&' : '&'; | |
509 | ||
510 | if (!$config->cleanURL) { | |
511 | if (isset($path)) { | |
512 | if (isset($query)) { | |
513 | return $base . $script . '?q=' . $path . $separator . $query . $fragment; | |
514 | } | |
515 | else { | |
516 | return $base . $script . '?q=' . $path . $fragment; | |
517 | } | |
518 | } | |
519 | else { | |
520 | if (isset($query)) { | |
521 | return $base . $script . '?' . $query . $fragment; | |
522 | } | |
523 | else { | |
524 | return $base . $fragment; | |
525 | } | |
526 | } | |
527 | } | |
528 | else { | |
529 | if (isset($path)) { | |
530 | if (isset($query)) { | |
531 | return $base . $path . '?' . $query . $fragment; | |
532 | } | |
533 | else { | |
534 | return $base . $path . $fragment; | |
535 | } | |
536 | } | |
537 | else { | |
538 | if (isset($query)) { | |
539 | return $base . $script . '?' . $query . $fragment; | |
540 | } | |
541 | else { | |
542 | return $base . $fragment; | |
543 | } | |
544 | } | |
545 | } | |
546 | } | |
547 | ||
548 | /** | |
549 | * Authenticate the user against the drupal db | |
550 | * | |
551 | * @param string $name the user name | |
552 | * @param string $password the password for the above user name | |
553 | * @param $loadCMSBootstrap boolean load cms bootstrap? | |
554 | * | |
555 | * @return mixed false if no auth | |
556 | * array( | |
557 | * contactID, ufID, unique string ) if success | |
558 | * @access public | |
559 | */ | |
560 | static function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) { | |
561 | require_once 'DB.php'; | |
562 | ||
563 | $config = CRM_Core_Config::singleton(); | |
564 | ||
565 | $dbDrupal = DB::connect($config->userFrameworkDSN); | |
566 | if (DB::isError($dbDrupal)) { | |
567 | CRM_Core_Error::fatal("Cannot connect to drupal db via $config->userFrameworkDSN, " . $dbDrupal->getMessage()); | |
568 | } | |
569 | ||
0af0e4c9 DL |
570 | |
571 | ||
6a488035 TO |
572 | $account = $userUid = $userMail = NULL; |
573 | if ($loadCMSBootstrap) { | |
574 | $bootStrapParams = array(); | |
575 | if ($name && $password) { | |
576 | $bootStrapParams = array( | |
577 | 'name' => $name, | |
578 | 'pass' => $password, | |
579 | ); | |
580 | } | |
581 | CRM_Utils_System::loadBootStrap($bootStrapParams, TRUE, TRUE, $realPath); | |
582 | ||
583 | global $user; | |
584 | if ($user) { | |
585 | $userUid = $user->uid; | |
586 | $userMail = $user->mail; | |
587 | } | |
588 | } | |
589 | else { | |
590 | // CRM-8638 | |
591 | // SOAP cannot load drupal bootstrap and hence we do it the old way | |
592 | // Contact CiviSMTP folks if we run into issues with this :) | |
593 | $cmsPath = $config->userSystem->cmsRootPath($realPath); | |
594 | ||
595 | require_once ("$cmsPath/includes/bootstrap.inc"); | |
596 | require_once ("$cmsPath/includes/password.inc"); | |
597 | ||
598 | $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; | |
599 | $name = $dbDrupal->escapeSimple($strtolower($name)); | |
600 | $sql = " | |
601 | SELECT u.* | |
602 | FROM {$config->userFrameworkUsersTableName} u | |
603 | WHERE LOWER(u.name) = '$name' | |
604 | AND u.status = 1 | |
605 | "; | |
606 | ||
607 | $query = $dbDrupal->query($sql); | |
608 | $row = $query->fetchRow(DB_FETCHMODE_ASSOC); | |
609 | ||
610 | if ($row) { | |
611 | $fakeDrupalAccount = drupal_anonymous_user(); | |
612 | $fakeDrupalAccount->name = $name; | |
613 | $fakeDrupalAccount->pass = $row['pass']; | |
614 | $passwordCheck = user_check_password($password, $fakeDrupalAccount); | |
615 | if ($passwordCheck) { | |
616 | $userUid = $row['uid']; | |
617 | $userMail = $row['mail']; | |
618 | } | |
619 | } | |
620 | } | |
621 | ||
622 | if ($userUid && $userMail) { | |
623 | CRM_Core_BAO_UFMatch::synchronizeUFMatch($account, $userUid, $userMail, 'Drupal'); | |
624 | $contactID = CRM_Core_BAO_UFMatch::getContactId($userUid); | |
625 | if (!$contactID) { | |
626 | return FALSE; | |
627 | } | |
628 | return array($contactID, $userUid, mt_rand()); | |
629 | } | |
630 | return FALSE; | |
631 | } | |
632 | ||
633 | /* | |
634 | * Load user into session | |
635 | */ | |
636 | function loadUser($username) { | |
637 | global $user; | |
638 | ||
639 | $user = user_load_by_name($username); | |
640 | ||
641 | if (empty($user->uid)) { | |
642 | return FALSE; | |
643 | } | |
644 | ||
645 | $uid = $user->uid; | |
646 | $contact_id = CRM_Core_BAO_UFMatch::getContactId($uid); | |
647 | ||
648 | // lets store contact id and user id in session | |
649 | $session = CRM_Core_Session::singleton(); | |
650 | $session->set('ufID', $uid); | |
651 | $session->set('userID', $contact_id); | |
652 | return TRUE; | |
653 | } | |
654 | ||
655 | /** | |
656 | * Set a message in the UF to display to a user | |
657 | * | |
658 | * @param string $message the message to set | |
659 | * | |
660 | * @access public | |
661 | */ | |
662 | function setMessage($message) { | |
663 | drupal_set_message($message); | |
664 | } | |
665 | ||
666 | function permissionDenied() { | |
667 | drupal_access_denied(); | |
668 | } | |
669 | ||
670 | function logout() { | |
671 | module_load_include('inc', 'user', 'user.pages'); | |
672 | return user_logout(); | |
673 | } | |
674 | ||
675 | function updateCategories() { | |
676 | // copied this from profile.module. Seems a bit inefficient, but i dont know a better way | |
677 | // CRM-3600 | |
678 | cache_clear_all(); | |
679 | menu_rebuild(); | |
680 | } | |
681 | ||
682 | /** | |
683 | * Get the default location for CiviCRM blocks | |
684 | * | |
685 | * @return string | |
686 | */ | |
687 | function getDefaultBlockLocation() { | |
688 | return 'sidebar_first'; | |
689 | } | |
690 | ||
691 | /** | |
692 | * Get the locale set in the hosting CMS | |
693 | * | |
694 | * @return string with the locale or null for none | |
695 | */ | |
696 | function getUFLocale() { | |
697 | // return CiviCRM’s xx_YY locale that either matches Drupal’s Chinese locale | |
698 | // (for CRM-6281), Drupal’s xx_YY or is retrieved based on Drupal’s xx | |
1c9f455c | 699 | // sometimes for CLI based on order called, this might not be set and/or empty |
6a488035 | 700 | global $language; |
6a488035 | 701 | |
1c9f455c DL |
702 | if (empty($language)) { |
703 | return NULL; | |
704 | } | |
6a488035 | 705 | |
1c9f455c DL |
706 | if ($language->language == 'zh-hans') { |
707 | return 'zh_CN'; | |
708 | } | |
6a488035 | 709 | |
1c9f455c DL |
710 | if ($language->language == 'zh-hant') { |
711 | return 'zh_TW'; | |
6a488035 | 712 | } |
1c9f455c DL |
713 | |
714 | if (preg_match('/^.._..$/', $language->language)) { | |
715 | return $language->language; | |
716 | } | |
717 | ||
718 | return CRM_Core_I18n_PseudoConstant::longForShort(substr($language->language, 0, 2)); | |
6a488035 TO |
719 | } |
720 | ||
721 | function getVersion() { | |
722 | return defined('VERSION') ? VERSION : 'Unknown'; | |
723 | } | |
724 | ||
725 | /** | |
726 | * load drupal bootstrap | |
727 | * | |
728 | * @param $params array with uid or name and password | |
729 | * @param $loadUser boolean load cms user? | |
730 | * @param $throwError throw error on failure? | |
731 | */ | |
0af0e4c9 DL |
732 | |
733 | function loadBootStrap($params = array(), $loadUser = TRUE, $throwError = TRUE, $realPath = NULL) { | |
6a488035 TO |
734 | //take the cms root path. |
735 | $cmsPath = $this->cmsRootPath($realPath); | |
736 | ||
737 | if (!file_exists("$cmsPath/includes/bootstrap.inc")) { | |
738 | if ($throwError) { | |
739 | echo '<br />Sorry, could not locate bootstrap.inc\n'; | |
740 | exit(); | |
741 | } | |
742 | return FALSE; | |
743 | } | |
744 | // load drupal bootstrap | |
745 | chdir($cmsPath); | |
746 | define('DRUPAL_ROOT', $cmsPath); | |
747 | ||
748 | // For drupal multi-site CRM-11313 | |
749 | if ($realPath && strpos($realPath, 'sites/all/modules/') === FALSE) { | |
750 | preg_match('@sites/([^/]*)/modules@s', $realPath, $matches); | |
751 | if (!empty($matches[1])) { | |
752 | $_SERVER['HTTP_HOST'] = $matches[1]; | |
753 | } | |
754 | } | |
755 | require_once 'includes/bootstrap.inc'; | |
756 | drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); | |
757 | ||
758 | // explicitly setting error reporting, since we cannot handle drupal related notices | |
759 | error_reporting(1); | |
0af0e4c9 | 760 | if (!function_exists('module_exists') || !module_exists('civicrm')) { |
6a488035 TO |
761 | if ($throwError) { |
762 | echo '<br />Sorry, could not load drupal bootstrap.'; | |
763 | exit(); | |
764 | } | |
765 | return FALSE; | |
766 | } | |
767 | ||
768 | // seems like we've bootstrapped drupal | |
769 | $config = CRM_Core_Config::singleton(); | |
770 | ||
6a488035 TO |
771 | // lets also fix the clean url setting |
772 | // CRM-6948 | |
773 | $config->cleanURL = (int) variable_get('clean_url', '0'); | |
774 | ||
775 | // we need to call the config hook again, since we now know | |
776 | // all the modules that are listening on it, does not apply | |
777 | // to J! and WP as yet | |
778 | // CRM-8655 | |
779 | CRM_Utils_Hook::config($config); | |
780 | ||
781 | if (!$loadUser) { | |
782 | return TRUE; | |
783 | } | |
784 | ||
785 | $uid = CRM_Utils_Array::value('uid', $params); | |
786 | if (!$uid) { | |
787 | //load user, we need to check drupal permissions. | |
788 | $name = CRM_Utils_Array::value('name', $params, FALSE) ? $params['name'] : trim(CRM_Utils_Array::value('name', $_REQUEST)); | |
789 | $pass = CRM_Utils_Array::value('pass', $params, FALSE) ? $params['pass'] : trim(CRM_Utils_Array::value('pass', $_REQUEST)); | |
790 | ||
791 | if ($name) { | |
792 | $uid = user_authenticate($name, $pass); | |
793 | if (!$uid) { | |
794 | if ($throwError) { | |
795 | echo '<br />Sorry, unrecognized username or password.'; | |
796 | exit(); | |
797 | } | |
798 | return FALSE; | |
799 | } | |
800 | } | |
801 | } | |
802 | ||
803 | if ($uid) { | |
804 | $account = user_load($uid); | |
805 | if ($account && $account->uid) { | |
806 | global $user; | |
807 | $user = $account; | |
808 | return TRUE; | |
809 | } | |
810 | } | |
811 | ||
812 | if ($throwError) { | |
813 | echo '<br />Sorry, can not load CMS user account.'; | |
814 | exit(); | |
815 | } | |
816 | ||
0af0e4c9 DL |
817 | // CRM-6948: When using loadBootStrap, it's implicit that CiviCRM has already loaded its settings |
818 | // which means that define(CIVICRM_CLEANURL) was correctly set. | |
6a488035 TO |
819 | // So we correct it |
820 | $config = CRM_Core_Config::singleton(); | |
821 | $config->cleanURL = (int)variable_get('clean_url', '0'); | |
822 | ||
823 | // CRM-8655: Drupal wasn't available during bootstrap, so hook_civicrm_config never executes | |
824 | CRM_Utils_Hook::config($config); | |
825 | ||
826 | return FALSE; | |
827 | } | |
828 | ||
829 | function cmsRootPath($scriptFilename = NULL) { | |
830 | ||
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 | */ | |
0af0e4c9 DL |
916 | function languageNegotiationURL( |
917 | $url, | |
6a488035 TO |
918 | $addLanguagePart = TRUE, |
919 | $removeLanguagePart = FALSE | |
920 | ) { | |
921 | if (empty($url)) { | |
922 | return $url; | |
923 | } | |
924 | ||
925 | //CRM-7803 -from d7 onward. | |
926 | $config = CRM_Core_Config::singleton(); | |
927 | if (function_exists('variable_get') && | |
928 | module_exists('locale') && | |
929 | function_exists('language_negotiation_get') | |
930 | ) { | |
931 | global $language; | |
932 | ||
933 | //does user configuration allow language | |
934 | //support from the URL (Path prefix or domain) | |
935 | if (language_negotiation_get('language') == 'locale-url') { | |
936 | $urlType = variable_get('locale_language_negotiation_url_part'); | |
937 | ||
938 | //url prefix | |
939 | if ($urlType == LOCALE_LANGUAGE_NEGOTIATION_URL_PREFIX) { | |
940 | if (isset($language->prefix) && $language->prefix) { | |
941 | if ($addLanguagePart) { | |
942 | $url .= $language->prefix . '/'; | |
943 | } | |
944 | if ($removeLanguagePart) { | |
945 | $url = str_replace("/{$language->prefix}/", '/', $url); | |
946 | } | |
947 | } | |
948 | } | |
949 | //domain | |
950 | if ($urlType == LOCALE_LANGUAGE_NEGOTIATION_URL_DOMAIN) { | |
951 | if (isset($language->domain) && $language->domain) { | |
952 | if ($addLanguagePart) { | |
953 | $url = CRM_Utils_File::addTrailingSlash($language->domain, '/'); | |
954 | } | |
955 | if ($removeLanguagePart && defined('CIVICRM_UF_BASEURL')) { | |
956 | $url = str_replace('\\', '/', $url); | |
957 | $parseUrl = parse_url($url); | |
958 | ||
959 | //kinda hackish but not sure how to do it right | |
960 | //hope http_build_url() will help at some point. | |
961 | if (is_array($parseUrl) && !empty($parseUrl)) { | |
962 | $urlParts = explode('/', $url); | |
963 | $hostKey = array_search($parseUrl['host'], $urlParts); | |
964 | $ufUrlParts = parse_url(CIVICRM_UF_BASEURL); | |
965 | $urlParts[$hostKey] = $ufUrlParts['host']; | |
966 | $url = implode('/', $urlParts); | |
967 | } | |
968 | } | |
969 | } | |
970 | } | |
971 | } | |
972 | } | |
973 | ||
974 | return $url; | |
975 | } | |
976 | ||
977 | /** | |
978 | * Find any users/roles/security-principals with the given permission | |
979 | * and replace it with one or more permissions. | |
980 | * | |
981 | * @param $oldPerm string | |
982 | * @param $newPerms array, strings | |
983 | * | |
984 | * @return void | |
985 | */ | |
986 | function replacePermission($oldPerm, $newPerms) { | |
987 | $roles = user_roles(FALSE, $oldPerm); | |
988 | if (!empty($roles)) { | |
989 | foreach (array_keys($roles) as $rid) { | |
990 | user_role_revoke_permissions($rid, array($oldPerm)); | |
991 | user_role_grant_permissions($rid, $newPerms); | |
992 | } | |
993 | } | |
994 | } | |
995 | ||
996 | /** | |
997 | * Get a list of all installed modules, including enabled and disabled ones | |
998 | * | |
999 | * @return array CRM_Core_Module | |
1000 | */ | |
1001 | function getModules() { | |
1002 | $result = array(); | |
1003 | $q = db_query('SELECT name, status FROM {system} WHERE type = \'module\' AND schema_version <> -1'); | |
1004 | foreach ($q as $row) { | |
1005 | $result[] = new CRM_Core_Module('drupal.' . $row->name, ($row->status == 1) ? TRUE : FALSE); | |
1006 | } | |
1007 | return $result; | |
1008 | } | |
1009 | ||
1010 | /** | |
1011 | * Wrapper for og_membership creation | |
1012 | */ | |
1013 | function og_membership_create($ogID, $drupalID){ | |
1014 | if (function_exists('og_entity_query_alter')) { | |
1015 | // sort-of-randomly chose a function that only exists in the 7.x-2.x branch | |
1016 | // TODO: Find a more solid way to make this test | |
1017 | // Also, since we don't know how to get the entity type of the group, we'll assume it's 'node' | |
1018 | og_group('node', $ogID, array('entity' => user_load($drupalID))); | |
1019 | } | |
1020 | else { | |
1021 | // Works for the OG 7.x-1.x branch | |
1022 | og_group($ogID, array('entity' => user_load($drupalID))); | |
1023 | } | |
1024 | } | |
1025 | ||
1026 | /** | |
1027 | * Wrapper for og_membership deletion | |
1028 | */ | |
1029 | function og_membership_delete($ogID, $drupalID) { | |
1030 | if (function_exists('og_entity_query_alter')) { | |
1031 | // sort-of-randomly chose a function that only exists in the 7.x-2.x branch | |
1032 | // TODO: Find a more solid way to make this test | |
1033 | // Also, since we don't know how to get the entity type of the group, we'll assume it's 'node' | |
1034 | og_ungroup('node', $ogID, 'user', user_load($drupalID)); | |
1035 | } else { | |
1036 | // Works for the OG 7.x-1.x branch | |
1037 | og_ungroup($ogID, 'user', user_load($drupalID)); | |
1038 | } | |
1039 | } | |
d8a4acc0 C |
1040 | |
1041 | /** | |
1042 | * Reset any system caches that may be required for proper CiviCRM | |
1043 | * integration. | |
1044 | */ | |
1045 | function flush() { | |
1046 | drupal_flush_all_caches(); | |
1047 | } | |
6a488035 | 1048 | } |