Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
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_Drupal6 extends CRM_Utils_System_DrupalBase { |
6a488035 TO |
40 | |
41 | /** | |
42 | * if we are using a theming system, invoke theme, else just print the | |
43 | * content | |
44 | * | |
45 | * @param string $content the content that will be themed | |
46 | * @param boolean $print are we displaying to the screen or bypassing theming? | |
47 | * @param boolean $maintenance for maintenance mode | |
48 | * | |
49 | * @return void prints content on stdout | |
50 | * @access public | |
51 | */ | |
52 | function theme(&$content, $print = FALSE, $maintenance = FALSE) { | |
53 | // TODO: Simplify; this was copied verbatim from CiviCRM 3.4's multi-UF theming function, but that's more complex than necessary | |
54 | if (function_exists('theme') && !$print) { | |
55 | if ($maintenance) { | |
56 | drupal_set_breadcrumb(''); | |
57 | drupal_maintenance_theme(); | |
58 | } | |
59 | ||
60 | // Arg 3 for D6 theme() is "show_blocks". Previously, we passed | |
61 | // through a badly named variable ("$args") which was almost always | |
62 | // TRUE (except on fatal error screen). However, this feature is | |
63 | // non-functional on D6 default themes, was purposefully removed from | |
64 | // D7, has no analog in other our other CMS's, and clutters the code. | |
65 | // Hard-wiring to TRUE should be OK. | |
66 | $out = theme('page', $content, TRUE); | |
67 | } | |
68 | else { | |
69 | $out = $content; | |
70 | } | |
71 | ||
72 | print $out; | |
73 | } | |
74 | ||
75 | /** | |
76 | * Function to create a user in Drupal. | |
77 | * | |
78 | * @param array $params associated array | |
79 | * @param string $mail email id for cms user | |
80 | * | |
81 | * @return uid if user exists, false otherwise | |
82 | * | |
83 | * @access public | |
84 | */ | |
85 | function createUser(&$params, $mail) { | |
86 | $form_state = array(); | |
87 | $form_state['values'] = array( | |
88 | 'name' => $params['cms_name'], | |
89 | 'mail' => $params[$mail], | |
90 | 'op' => 'Create new account', | |
91 | ); | |
a57957f7 KJ |
92 | |
93 | $admin = user_access('administer users'); | |
94 | if (!variable_get('user_email_verification', TRUE) || $admin) { | |
6a488035 TO |
95 | $form_state['values']['pass']['pass1'] = $params['cms_pass']; |
96 | $form_state['values']['pass']['pass2'] = $params['cms_pass']; | |
97 | } | |
98 | ||
99 | $config = CRM_Core_Config::singleton(); | |
100 | ||
101 | // we also need to redirect b | |
102 | $config->inCiviCRM = TRUE; | |
103 | ||
104 | $form = drupal_retrieve_form('user_register', $form_state); | |
105 | $form['#post'] = $form_state['values']; | |
106 | drupal_prepare_form('user_register', $form, $form_state); | |
107 | ||
108 | // remove the captcha element from the form prior to processing | |
109 | unset($form['captcha']); | |
110 | ||
111 | drupal_process_form('user_register', $form, $form_state); | |
112 | ||
113 | $config->inCiviCRM = FALSE; | |
114 | ||
115 | if (form_get_errors() || !isset($form_state['user'])) { | |
116 | return FALSE; | |
117 | } | |
6a488035 | 118 | return $form_state['user']->uid; |
6a488035 TO |
119 | } |
120 | ||
121 | /* | |
122 | * Change user name in host CMS | |
123 | * | |
124 | * @param integer $ufID User ID in CMS | |
125 | * @param string $ufName User name | |
126 | */ | |
127 | function updateCMSName($ufID, $ufName) { | |
128 | // CRM-5555 | |
129 | if (function_exists('user_load')) { | |
130 | $user = user_load(array('uid' => $ufID)); | |
131 | if ($user->mail != $ufName) { | |
132 | user_save($user, array('mail' => $ufName)); | |
133 | $user = user_load(array('uid' => $ufID)); | |
134 | } | |
135 | } | |
136 | } | |
137 | ||
138 | /** | |
139 | * Check if username and email exists in the drupal db | |
140 | * | |
141 | * @params $params array array of name and mail values | |
142 | * @params $errors array array of errors | |
143 | * @params $emailName string field label for the 'email' | |
144 | * | |
145 | * @return void | |
146 | */ | |
147 | function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') { | |
148 | $config = CRM_Core_Config::singleton(); | |
149 | ||
150 | $dao = new CRM_Core_DAO(); | |
151 | $name = $dao->escape(CRM_Utils_Array::value('name', $params)); | |
152 | $email = $dao->escape(CRM_Utils_Array::value('mail', $params)); | |
153 | _user_edit_validate(NULL, $params); | |
154 | $errors = form_get_errors(); | |
6a488035 | 155 | if ($errors) { |
a7488080 | 156 | if (!empty($errors['name'])) { |
6a488035 TO |
157 | $errors['cms_name'] = $errors['name']; |
158 | } | |
a7488080 | 159 | if (!empty($errors['mail'])) { |
6a488035 TO |
160 | $errors[$emailName] = $errors['mail']; |
161 | } | |
162 | // also unset drupal messages to avoid twice display of errors | |
163 | unset($_SESSION['messages']); | |
164 | } | |
165 | ||
a5ecff8d | 166 | // Do the name check manually. |
6a488035 TO |
167 | $nameError = user_validate_name($params['name']); |
168 | if ($nameError) { | |
169 | $errors['cms_name'] = $nameError; | |
170 | } | |
171 | ||
172 | $sql = " | |
b67a6d82 E |
173 | SELECT name, mail |
174 | FROM {users} | |
175 | WHERE (LOWER(name) = LOWER('$name')) OR (LOWER(mail) = LOWER('$email')) | |
176 | "; | |
b67a6d82 | 177 | |
48f12f07 | 178 | $result = db_query($sql); |
8982d5f8 EM |
179 | $row = db_fetch_array($result); |
180 | if (!$row) { | |
b67a6d82 | 181 | return; |
6a488035 | 182 | } |
5a604d61 | 183 | |
b67a6d82 E |
184 | $user = NULL; |
185 | ||
6a488035 | 186 | if (!empty($row)) { |
b67a6d82 E |
187 | $dbName = CRM_Utils_Array::value('name', $row); |
188 | $dbEmail = CRM_Utils_Array::value('mail', $row); | |
6a488035 TO |
189 | if (strtolower($dbName) == strtolower($name)) { |
190 | $errors['cms_name'] = ts('The username %1 is already taken. Please select another username.', | |
191 | array(1 => $name) | |
192 | ); | |
193 | } | |
194 | if (strtolower($dbEmail) == strtolower($email)) { | |
b67a6d82 E |
195 | if(empty($email)) { |
196 | $errors[$emailName] = ts('You cannot create an email account for a contact with no email', | |
197 | array(1 => $email) | |
198 | ); | |
199 | } | |
200 | else{ | |
201 | $errors[$emailName] = ts('This email %1 is already registered. Please select another email.', | |
202 | array(1 => $email) | |
203 | ); | |
204 | } | |
6a488035 TO |
205 | } |
206 | } | |
207 | } | |
208 | ||
209 | /* | |
210 | * Function to get the drupal destination string. When this is passed in the | |
211 | * URL the user will be directed to it after filling in the drupal form | |
212 | * | |
213 | * @param object $form Form object representing the 'current' form - to which the user will be returned | |
214 | * @return string $destination destination value for URL | |
215 | * | |
216 | */ | |
217 | function getLoginDestination(&$form) { | |
218 | $args = NULL; | |
219 | ||
220 | $id = $form->get('id'); | |
221 | if ($id) { | |
222 | $args .= "&id=$id"; | |
223 | } | |
224 | else { | |
225 | $gid = $form->get('gid'); | |
226 | if ($gid) { | |
227 | $args .= "&gid=$gid"; | |
228 | } | |
229 | else { | |
230 | // Setup Personal Campaign Page link uses pageId | |
231 | $pageId = $form->get('pageId'); | |
232 | if ($pageId) { | |
233 | $component = $form->get('component'); | |
234 | $args .= "&pageId=$pageId&component=$component&action=add"; | |
235 | } | |
236 | } | |
237 | } | |
238 | ||
239 | $destination = NULL; | |
240 | if ($args) { | |
241 | // append destination so user is returned to form they came from after login | |
242 | $destination = CRM_Utils_System::currentPath() . '?reset=1' . $args; | |
243 | } | |
244 | return $destination; | |
245 | } | |
246 | ||
247 | /** | |
248 | * sets the title of the page | |
249 | * | |
250 | * @param string $title | |
251 | * @paqram string $pageTitle | |
252 | * | |
253 | * @return void | |
254 | * @access public | |
255 | */ | |
256 | function setTitle($title, $pageTitle = NULL) { | |
257 | if (!$pageTitle) { | |
258 | $pageTitle = $title; | |
259 | } | |
260 | if (arg(0) == 'civicrm') { | |
261 | //set drupal title | |
262 | drupal_set_title($pageTitle); | |
263 | } | |
264 | } | |
265 | ||
266 | /** | |
267 | * Append an additional breadcrumb tag to the existing breadcrumb | |
268 | * | |
269 | * @param string $title | |
270 | * @param string $url | |
271 | * | |
272 | * @return void | |
273 | * @access public | |
274 | */ | |
275 | function appendBreadCrumb($breadCrumbs) { | |
276 | $breadCrumb = drupal_get_breadcrumb(); | |
277 | ||
278 | if (is_array($breadCrumbs)) { | |
279 | foreach ($breadCrumbs as $crumbs) { | |
280 | if (stripos($crumbs['url'], 'id%%')) { | |
281 | $args = array('cid', 'mid'); | |
282 | foreach ($args as $a) { | |
283 | $val = CRM_Utils_Request::retrieve($a, 'Positive', CRM_Core_DAO::$_nullObject, | |
284 | FALSE, NULL, $_GET | |
285 | ); | |
286 | if ($val) { | |
287 | $crumbs['url'] = str_ireplace("%%{$a}%%", $val, $crumbs['url']); | |
288 | } | |
289 | } | |
290 | } | |
291 | $breadCrumb[] = "<a href=\"{$crumbs['url']}\">{$crumbs['title']}</a>"; | |
292 | } | |
293 | } | |
294 | drupal_set_breadcrumb($breadCrumb); | |
295 | } | |
296 | ||
297 | /** | |
298 | * Reset an additional breadcrumb tag to the existing breadcrumb | |
299 | * | |
300 | * @return void | |
301 | * @access public | |
302 | */ | |
303 | function resetBreadCrumb() { | |
304 | $bc = array(); | |
305 | drupal_set_breadcrumb($bc); | |
306 | } | |
307 | ||
308 | /** | |
309 | * Append a string to the head of the html file | |
310 | * | |
311 | * @param string $head the new string to be appended | |
312 | * | |
313 | * @return void | |
314 | * @access public | |
315 | */ | |
316 | function addHTMLHead($head) { | |
317 | drupal_set_html_head($head); | |
318 | } | |
319 | ||
320 | /** | |
321 | * Add a script file | |
322 | * | |
323 | * @param $url: string, absolute path to file | |
324 | * @param $region string, location within the document: 'html-header', 'page-header', 'page-footer' | |
325 | * | |
326 | * Note: This function is not to be called directly | |
327 | * @see CRM_Core_Region::render() | |
328 | * | |
329 | * @return bool TRUE if we support this operation in this CMS, FALSE otherwise | |
330 | * @access public | |
331 | */ | |
332 | public function addScriptUrl($url, $region) { | |
333 | switch ($region) { | |
334 | case 'html-header': | |
335 | case 'page-footer': | |
336 | $scope = substr($region, 5); | |
337 | break; | |
338 | default: | |
339 | return FALSE; | |
340 | } | |
341 | // If the path is within the drupal directory we can add in the normal way | |
42e1a97c | 342 | if ($this->formatResourceUrl($url)) { |
6a488035 | 343 | drupal_add_js($url, 'module', $scope); |
a03524aa | 344 | return TRUE; |
6a488035 | 345 | } |
a03524aa | 346 | return FALSE; |
6a488035 TO |
347 | } |
348 | ||
349 | /** | |
350 | * Add an inline script | |
351 | * | |
352 | * @param $code: string, javascript code | |
353 | * @param $region string, location within the document: 'html-header', 'page-header', 'page-footer' | |
354 | * | |
355 | * Note: This function is not to be called directly | |
356 | * @see CRM_Core_Region::render() | |
357 | * | |
358 | * @return bool TRUE if we support this operation in this CMS, FALSE otherwise | |
359 | * @access public | |
360 | */ | |
361 | public function addScript($code, $region) { | |
362 | switch ($region) { | |
363 | case 'html-header': | |
364 | case 'page-footer': | |
365 | $scope = substr($region, 5); | |
366 | break; | |
367 | default: | |
368 | return FALSE; | |
369 | } | |
370 | drupal_add_js($code, 'inline', $scope); | |
371 | return TRUE; | |
372 | } | |
373 | ||
374 | /** | |
375 | * Add a css file | |
376 | * | |
377 | * @param $url: string, absolute path to file | |
378 | * @param $region string, location within the document: 'html-header', 'page-header', 'page-footer' | |
379 | * | |
380 | * Note: This function is not to be called directly | |
381 | * @see CRM_Core_Region::render() | |
382 | * | |
383 | * @return bool TRUE if we support this operation in this CMS, FALSE otherwise | |
384 | * @access public | |
385 | */ | |
386 | public function addStyleUrl($url, $region) { | |
42e1a97c | 387 | if ($region != 'html-header' || !$this->formatResourceUrl($url)) { |
6a488035 TO |
388 | return FALSE; |
389 | } | |
390 | drupal_add_css($url); | |
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 | return FALSE; | |
408 | } | |
409 | ||
410 | /** | |
411 | * rewrite various system urls to https | |
412 | * | |
413 | * @param null | |
414 | * | |
415 | * @return void | |
416 | * @access public | |
417 | */ | |
418 | function mapConfigToSSL() { | |
419 | global $base_url; | |
420 | $base_url = str_replace('http://', 'https://', $base_url); | |
421 | } | |
422 | ||
423 | /** | |
424 | * figure out the post url for the form | |
425 | * | |
426 | * @param mix $action the default action if one is pre-specified | |
427 | * | |
428 | * @return string the url to post the form | |
429 | * @access public | |
6a488035 TO |
430 | */ |
431 | function postURL($action) { | |
432 | if (!empty($action)) { | |
433 | return $action; | |
434 | } | |
435 | ||
436 | return $this->url($_GET['q']); | |
437 | } | |
438 | ||
6a488035 TO |
439 | /** |
440 | * Authenticate the user against the drupal db | |
441 | * | |
442 | * @param string $name the user name | |
443 | * @param string $password the password for the above user name | |
a5ecff8d CB |
444 | * @param boolean $loadCMSBootstrap load cms bootstrap? |
445 | * @param NULL|string $realPath filename of script | |
6a488035 TO |
446 | * |
447 | * @return mixed false if no auth | |
448 | * array( | |
449 | * contactID, ufID, unique string ) if success | |
450 | * @access public | |
451 | */ | |
452 | function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) { | |
bc2f844f E |
453 | //@todo this 'PEAR-y' stuff is only required when bookstrap is not being loaded which is rare |
454 | // if ever now. | |
455 | // probably if bootstrap is loaded this call | |
456 | // CRM_Utils_System::loadBootStrap($bootStrapParams, TRUE, TRUE, $realPath); would be | |
457 | // sufficient to do what this fn does. It does exist as opposed to return which might need some hanky-panky to make | |
458 | // safe in the unknown situation where authenticate might be called & it is important that | |
459 | // false is returned | |
6a488035 TO |
460 | require_once 'DB.php'; |
461 | ||
462 | $config = CRM_Core_Config::singleton(); | |
463 | ||
464 | $dbDrupal = DB::connect($config->userFrameworkDSN); | |
465 | if (DB::isError($dbDrupal)) { | |
466 | CRM_Core_Error::fatal("Cannot connect to drupal db via $config->userFrameworkDSN, " . $dbDrupal->getMessage()); | |
467 | } | |
468 | ||
469 | $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; | |
470 | $dbpassword = md5($password); | |
471 | $name = $dbDrupal->escapeSimple($strtolower($name)); | |
b67a6d82 | 472 | $sql = 'SELECT u.* FROM ' . $config->userFrameworkUsersTableName . " u WHERE LOWER(u.name) = '$name' AND u.pass = '$dbpassword' AND u.status = 1"; |
6a488035 TO |
473 | $query = $dbDrupal->query($sql); |
474 | ||
475 | $user = NULL; | |
476 | // need to change this to make sure we matched only one row | |
477 | while ($row = $query->fetchRow(DB_FETCHMODE_ASSOC)) { | |
478 | CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, $row['uid'], $row['mail'], 'Drupal'); | |
479 | $contactID = CRM_Core_BAO_UFMatch::getContactId($row['uid']); | |
480 | if (!$contactID) { | |
481 | return FALSE; | |
482 | } | |
483 | else{//success | |
484 | if ($loadCMSBootstrap) { | |
485 | $bootStrapParams = array(); | |
486 | if ($name && $password) { | |
487 | $bootStrapParams = array( | |
488 | 'name' => $name, | |
489 | 'pass' => $password, | |
490 | ); | |
491 | } | |
492 | CRM_Utils_System::loadBootStrap($bootStrapParams, TRUE, TRUE, $realPath); | |
493 | } | |
a5ecff8d CB |
494 | return array($contactID, $row['uid'], mt_rand()); |
495 | } | |
6a488035 TO |
496 | } |
497 | return FALSE; | |
498 | } | |
499 | ||
500 | /* | |
501 | * Load user into session | |
502 | */ | |
503 | function loadUser($username) { | |
504 | global $user; | |
505 | $user = user_load(array('name' => $username)); | |
506 | if (empty($user->uid)) { | |
507 | return FALSE; | |
508 | } | |
509 | ||
510 | $uid = $user->uid; | |
511 | $contact_id = CRM_Core_BAO_UFMatch::getContactId($uid); | |
512 | ||
513 | // lets store contact id and user id in session | |
514 | $session = CRM_Core_Session::singleton(); | |
515 | $session->set('ufID', $uid); | |
516 | $session->set('userID', $contact_id); | |
517 | return TRUE; | |
518 | } | |
519 | ||
82d9c21e | 520 | /** |
53980972 | 521 | * Perform any post login activities required by the UF - |
522 | * e.g. for drupal : records a watchdog message about the new session, | |
523 | * saves the login timestamp, calls hook_user op 'login' and generates a new session. | |
e43cc689 | 524 | * |
95d68223 TO |
525 | * @param array params |
526 | * | |
527 | * FIXME: Document values accepted/required by $params | |
53980972 | 528 | */ |
529 | function userLoginFinalize($params = array()) { | |
530 | user_authenticate_finalize($params); | |
82d9c21e | 531 | } |
532 | ||
46b6363c TO |
533 | /** |
534 | * Determine the native ID of the CMS user | |
535 | * | |
536 | * @param $username | |
537 | * @return int|NULL | |
538 | */ | |
539 | function getUfId($username) { | |
540 | $user = user_load(array('name' => $username)); | |
541 | if (empty($user->uid)) { | |
542 | return NULL; | |
543 | } | |
544 | return $user->uid; | |
545 | } | |
546 | ||
6a488035 TO |
547 | /** |
548 | * Set a message in the UF to display to a user | |
549 | * | |
550 | * @param string $message the message to set | |
551 | * | |
552 | * @access public | |
553 | */ | |
554 | function setMessage($message) { | |
555 | drupal_set_message($message); | |
556 | } | |
557 | ||
6a488035 TO |
558 | function logout() { |
559 | module_load_include('inc', 'user', 'user.pages'); | |
560 | return user_logout(); | |
561 | } | |
562 | ||
563 | function updateCategories() { | |
564 | // copied this from profile.module. Seems a bit inefficient, but i dont know a better way | |
565 | // CRM-3600 | |
566 | cache_clear_all(); | |
567 | menu_rebuild(); | |
568 | } | |
569 | ||
570 | /** | |
571 | * Get the locale set in the hosting CMS | |
572 | * | |
573 | * @return string with the locale or null for none | |
574 | */ | |
575 | function getUFLocale() { | |
576 | // return CiviCRM’s xx_YY locale that either matches Drupal’s Chinese locale | |
577 | // (for CRM-6281), Drupal’s xx_YY or is retrieved based on Drupal’s xx | |
cbb2e021 | 578 | // sometimes for CLI based on order called, this might not be set and/or empty |
6a488035 | 579 | global $language; |
6a488035 | 580 | |
cbb2e021 CB |
581 | if (empty($language)) { |
582 | return NULL; | |
583 | } | |
6a488035 | 584 | |
cbb2e021 CB |
585 | if ($language->language == 'zh-hans') { |
586 | return 'zh_CN'; | |
587 | } | |
6a488035 | 588 | |
cbb2e021 CB |
589 | if ($language->language == 'zh-hant') { |
590 | return 'zh_TW'; | |
6a488035 | 591 | } |
cbb2e021 CB |
592 | |
593 | if (preg_match('/^.._..$/', $language->language)) { | |
594 | return $language->language; | |
595 | } | |
596 | ||
597 | return CRM_Core_I18n_PseudoConstant::longForShort(substr($language->language, 0, 2)); | |
6a488035 TO |
598 | } |
599 | ||
600 | function getVersion() { | |
601 | return defined('VERSION') ? VERSION : 'Unknown'; | |
602 | } | |
603 | ||
604 | /** | |
605 | * load drupal bootstrap | |
606 | * | |
a5ecff8d CB |
607 | * @param array $params Either uid, or name & pass. |
608 | * @param boolean $loadUser boolean Require CMS user load. | |
609 | * @param boolean $throwError If true, print error on failure and exit. | |
610 | * @param boolean|string $realPath path to script | |
6a488035 | 611 | */ |
a5ecff8d | 612 | function loadBootStrap($params = array(), $loadUser = TRUE, $throwError = TRUE, $realPath = NULL) { |
6a488035 TO |
613 | //take the cms root path. |
614 | $cmsPath = $this->cmsRootPath($realPath); | |
4459cd26 | 615 | |
6a488035 | 616 | if (!file_exists("$cmsPath/includes/bootstrap.inc")) { |
4459cd26 CB |
617 | if ($throwError) { |
618 | echo '<br />Sorry, could not locate bootstrap.inc\n'; | |
619 | exit(); | |
620 | } | |
621 | return FALSE; | |
6a488035 | 622 | } |
4459cd26 | 623 | // load drupal bootstrap |
6a488035 | 624 | chdir($cmsPath); |
4459cd26 CB |
625 | define('DRUPAL_ROOT', $cmsPath); |
626 | ||
627 | // For drupal multi-site CRM-11313 | |
628 | if ($realPath && strpos($realPath, 'sites/all/modules/') === FALSE) { | |
629 | preg_match('@sites/([^/]*)/modules@s', $realPath, $matches); | |
630 | if (!empty($matches[1])) { | |
631 | $_SERVER['HTTP_HOST'] = $matches[1]; | |
632 | } | |
633 | } | |
6a488035 | 634 | require_once 'includes/bootstrap.inc'; |
38507482 | 635 | // @ to suppress notices eg 'DRUPALFOO already defined'. |
6a488035 TO |
636 | @drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); |
637 | ||
4459cd26 CB |
638 | // explicitly setting error reporting, since we cannot handle drupal related notices |
639 | error_reporting(1); | |
a5ecff8d | 640 | if (!function_exists('module_exists') || !module_exists('civicrm')) { |
4459cd26 CB |
641 | if ($throwError) { |
642 | echo '<br />Sorry, could not load drupal bootstrap.'; | |
643 | exit(); | |
644 | } | |
645 | return FALSE; | |
6a488035 | 646 | } |
a5ecff8d | 647 | |
4459cd26 CB |
648 | // seems like we've bootstrapped drupal |
649 | $config = CRM_Core_Config::singleton(); | |
650 | ||
6a488035 TO |
651 | // lets also fix the clean url setting |
652 | // CRM-6948 | |
653 | $config->cleanURL = (int) variable_get('clean_url', '0'); | |
654 | ||
655 | // we need to call the config hook again, since we now know | |
656 | // all the modules that are listening on it, does not apply | |
657 | // to J! and WP as yet | |
658 | // CRM-8655 | |
659 | CRM_Utils_Hook::config($config); | |
660 | ||
661 | if (!$loadUser) { | |
662 | return TRUE; | |
663 | } | |
95915c38 | 664 | global $user; |
4459cd26 CB |
665 | // If $uid is passed in, authentication has been done already. |
666 | $uid = CRM_Utils_Array::value('uid', $params); | |
667 | if (!$uid) { | |
668 | //load user, we need to check drupal permissions. | |
669 | $name = CRM_Utils_Array::value('name', $params, FALSE) ? $params['name'] : trim(CRM_Utils_Array::value('name', $_REQUEST)); | |
670 | $pass = CRM_Utils_Array::value('pass', $params, FALSE) ? $params['pass'] : trim(CRM_Utils_Array::value('pass', $_REQUEST)); | |
671 | ||
672 | if ($name) { | |
95915c38 E |
673 | $user = user_authenticate(array('name' => $name, 'pass' => $pass)); |
674 | if (!$user->uid) { | |
4459cd26 CB |
675 | if ($throwError) { |
676 | echo '<br />Sorry, unrecognized username or password.'; | |
677 | exit(); | |
678 | } | |
679 | return FALSE; | |
680 | } | |
95915c38 E |
681 | else { |
682 | return TRUE; | |
683 | } | |
6a488035 TO |
684 | } |
685 | } | |
4459cd26 CB |
686 | |
687 | if ($uid) { | |
688 | $account = user_load($uid); | |
689 | if ($account && $account->uid) { | |
6a488035 | 690 | $user = $account; |
4459cd26 | 691 | return TRUE; |
6a488035 TO |
692 | } |
693 | } | |
4459cd26 CB |
694 | |
695 | if ($throwError) { | |
696 | echo '<br />Sorry, can not load CMS user account.'; | |
697 | exit(); | |
698 | } | |
699 | ||
700 | // CRM-6948: When using loadBootStrap, it's implicit that CiviCRM has already loaded its settings | |
701 | // which means that define(CIVICRM_CLEANURL) was correctly set. | |
702 | // So we correct it | |
703 | $config = CRM_Core_Config::singleton(); | |
704 | $config->cleanURL = (int)variable_get('clean_url', '0'); | |
705 | ||
706 | // CRM-8655: Drupal wasn't available during bootstrap, so hook_civicrm_config never executes | |
707 | CRM_Utils_Hook::config($config); | |
708 | ||
709 | return FALSE; | |
6a488035 TO |
710 | } |
711 | ||
a5ecff8d CB |
712 | /** |
713 | * | |
714 | */ | |
6a488035 TO |
715 | function cmsRootPath($scriptFilename = NULL) { |
716 | $cmsRoot = $valid = NULL; | |
717 | ||
718 | if (!is_null($scriptFilename)) { | |
719 | $path = $scriptFilename; | |
720 | } | |
721 | else { | |
722 | $path = $_SERVER['SCRIPT_FILENAME']; | |
723 | } | |
a5ecff8d | 724 | |
6a488035 TO |
725 | if (function_exists('drush_get_context')) { |
726 | // drush anyway takes care of multisite install etc | |
727 | return drush_get_context('DRUSH_DRUPAL_ROOT'); | |
728 | } | |
729 | // CRM-7582 | |
730 | $pathVars = explode('/', | |
731 | str_replace('//', '/', | |
732 | str_replace('\\', '/', $path) | |
733 | ) | |
734 | ); | |
735 | ||
736 | //lets store first var, | |
737 | //need to get back for windows. | |
738 | $firstVar = array_shift($pathVars); | |
739 | ||
740 | //lets remove sript name to reduce one iteration. | |
741 | array_pop($pathVars); | |
742 | ||
743 | //CRM-7429 --do check for upper most 'includes' dir, | |
744 | //which would effectually work for multisite installation. | |
745 | do { | |
746 | $cmsRoot = $firstVar . '/' . implode('/', $pathVars); | |
747 | $cmsIncludePath = "$cmsRoot/includes"; | |
748 | //stop as we found bootstrap. | |
749 | if (@opendir($cmsIncludePath) && | |
750 | file_exists("$cmsIncludePath/bootstrap.inc") | |
751 | ) { | |
752 | $valid = TRUE; | |
753 | break; | |
754 | } | |
755 | //remove one directory level. | |
756 | array_pop($pathVars); | |
757 | } while (count($pathVars)); | |
758 | ||
759 | return ($valid) ? $cmsRoot : NULL; | |
760 | } | |
761 | ||
762 | /** | |
763 | * check is user logged in. | |
764 | * | |
765 | * @return boolean true/false. | |
766 | */ | |
767 | public function isUserLoggedIn() { | |
768 | $isloggedIn = FALSE; | |
769 | if (function_exists('user_is_logged_in')) { | |
770 | $isloggedIn = user_is_logged_in(); | |
771 | } | |
772 | ||
773 | return $isloggedIn; | |
774 | } | |
775 | ||
776 | /** | |
777 | * Get currently logged in user uf id. | |
778 | * | |
779 | * @return int $userID logged in user uf id. | |
780 | */ | |
781 | public function getLoggedInUfID() { | |
782 | $ufID = NULL; | |
783 | if (function_exists('user_is_logged_in') && | |
784 | user_is_logged_in() && | |
785 | function_exists('user_uid_optional_to_arg') | |
786 | ) { | |
787 | $ufID = user_uid_optional_to_arg(array()); | |
788 | } | |
789 | ||
790 | return $ufID; | |
791 | } | |
792 | ||
793 | /** | |
794 | * Format the url as per language Negotiation. | |
795 | * | |
796 | * @param string $url | |
797 | * | |
798 | * @return string $url, formatted url. | |
799 | * @static | |
800 | */ | |
a5ecff8d | 801 | function languageNegotiationURL($url, $addLanguagePart = TRUE, $removeLanguagePart = FALSE) { |
6a488035 TO |
802 | if (empty($url)) { |
803 | return $url; | |
804 | } | |
805 | ||
806 | //upto d6 only, already we have code in place for d7 | |
807 | $config = CRM_Core_Config::singleton(); | |
808 | if (function_exists('variable_get') && | |
809 | module_exists('locale') | |
810 | ) { | |
811 | global $language; | |
812 | ||
813 | //get the mode. | |
814 | $mode = variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE); | |
815 | ||
816 | //url prefix / path. | |
817 | if (isset($language->prefix) && | |
818 | $language->prefix && | |
819 | in_array($mode, array( | |
820 | LANGUAGE_NEGOTIATION_PATH, | |
821 | LANGUAGE_NEGOTIATION_PATH_DEFAULT, | |
822 | )) | |
823 | ) { | |
824 | ||
825 | if ($addLanguagePart) { | |
826 | $url .= $language->prefix . '/'; | |
827 | } | |
828 | if ($removeLanguagePart) { | |
829 | $url = str_replace("/{$language->prefix}/", '/', $url); | |
830 | } | |
831 | } | |
832 | if (isset($language->domain) && | |
833 | $language->domain && | |
834 | $mode == LANGUAGE_NEGOTIATION_DOMAIN | |
835 | ) { | |
836 | ||
837 | if ($addLanguagePart) { | |
838 | $url = CRM_Utils_File::addTrailingSlash($language->domain, '/'); | |
839 | } | |
840 | if ($removeLanguagePart && defined('CIVICRM_UF_BASEURL')) { | |
841 | $url = str_replace('\\', '/', $url); | |
842 | $parseUrl = parse_url($url); | |
843 | ||
844 | //kinda hackish but not sure how to do it right | |
845 | //hope http_build_url() will help at some point. | |
846 | if (is_array($parseUrl) && !empty($parseUrl)) { | |
847 | $urlParts = explode('/', $url); | |
848 | $hostKey = array_search($parseUrl['host'], $urlParts); | |
849 | $ufUrlParts = parse_url(CIVICRM_UF_BASEURL); | |
850 | $urlParts[$hostKey] = $ufUrlParts['host']; | |
851 | $url = implode('/', $urlParts); | |
852 | } | |
853 | } | |
854 | } | |
855 | } | |
856 | ||
857 | return $url; | |
858 | } | |
859 | ||
860 | /** | |
861 | * Find any users/roles/security-principals with the given permission | |
862 | * and replace it with one or more permissions. | |
863 | * | |
864 | * @param $oldPerm string | |
865 | * @param $newPerms array, strings | |
866 | * | |
867 | * @return void | |
868 | */ | |
869 | function replacePermission($oldPerm, $newPerms) { | |
870 | $roles = user_roles(FALSE, $oldPerm); | |
871 | foreach ($roles as $rid => $roleName) { | |
872 | $permList = db_result(db_query('SELECT perm FROM {permission} WHERE rid = %d', $rid)); | |
873 | $perms = drupal_map_assoc(explode(', ', $permList)); | |
874 | unset($perms[$oldPerm]); | |
875 | $perms = $perms + drupal_map_assoc($newPerms); | |
876 | $permList = implode(', ', $perms); | |
877 | db_query('UPDATE {permission} SET perm = "%s" WHERE rid = %d', $permList, $rid); | |
878 | /* | |
879 | if ( ! empty( $roles ) ) { | |
880 | $rids = implode(',', array_keys($roles)); | |
881 | db_query( 'UPDATE {permission} SET perm = CONCAT( perm, \', edit all events\') WHERE rid IN (' . implode(',', array_keys($roles)) . ')' ); | |
882 | db_query( "UPDATE {permission} SET perm = REPLACE( perm, '%s', '%s' ) WHERE rid IN ($rids)", | |
883 | $oldPerm, implode(', ', $newPerms) );*/ | |
884 | } | |
885 | } | |
886 | ||
887 | /** | |
888 | * Get a list of all installed modules, including enabled and disabled ones | |
889 | * | |
890 | * @return array CRM_Core_Module | |
891 | */ | |
892 | function getModules() { | |
893 | $result = array(); | |
894 | $q = db_query('SELECT name, status FROM {system} WHERE type = \'module\' AND schema_version <> -1'); | |
895 | while ($row = db_fetch_object($q)) { | |
896 | $result[] = new CRM_Core_Module('drupal.' . $row->name, ($row->status == 1) ? TRUE : FALSE); | |
897 | } | |
898 | return $result; | |
899 | } | |
900 | ||
901 | /** | |
902 | * Get user login URL for hosting CMS (method declared in each CMS system class) | |
903 | * | |
904 | * @param string $destination - if present, add destination to querystring (works for Drupal only) | |
905 | * | |
906 | * @return string - loginURL for the current CMS | |
907 | * @static | |
908 | */ | |
909 | public function getLoginURL($destination = '') { | |
910 | $config = CRM_Core_Config::singleton(); | |
911 | $loginURL = $config->userFrameworkBaseURL; | |
912 | $loginURL .= 'user'; | |
913 | if (!empty($destination)) { | |
914 | // append destination so user is returned to form they came from after login | |
915 | $loginURL .= '?destination=' . urlencode($destination); | |
916 | } | |
917 | return $loginURL; | |
918 | } | |
919 | ||
d761c4d8 | 920 | /** |
6a488035 | 921 | * Wrapper for og_membership creation |
d761c4d8 | 922 | * |
923 | * @param integer $ogID Organic Group ID | |
924 | * @param integer $drupalID drupal User ID | |
6a488035 TO |
925 | */ |
926 | function og_membership_create($ogID, $drupalID){ | |
d761c4d8 | 927 | og_save_subscription( $ogID, $drupalID, array( 'is_active' => 1 ) ); |
6a488035 TO |
928 | } |
929 | ||
930 | /** | |
931 | * Wrapper for og_membership deletion | |
d761c4d8 | 932 | * |
933 | * @param integer $ogID Organic Group ID | |
934 | * @param integer $drupalID drupal User ID | |
6a488035 TO |
935 | */ |
936 | function og_membership_delete($ogID, $drupalID) { | |
d761c4d8 | 937 | og_delete_subscription( $ogID, $drupalID ); |
6a488035 TO |
938 | } |
939 | ||
5a604d61 E |
940 | /** |
941 | * Get timezone from Drupal | |
942 | * @return boolean|string | |
943 | */ | |
944 | function getTimeZoneOffset(){ | |
945 | global $user; | |
946 | if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) { | |
947 | $timezone = $user->timezone; | |
948 | } else { | |
949 | $timezone = variable_get('date_default_timezone', null); | |
950 | } | |
951 | if(empty($timezone)){ | |
952 | return false; | |
953 | } | |
954 | $hour = $user->timezone / 3600; | |
955 | $timeZoneOffset = sprintf("%02d:%02d", $timezone / 3600, ($timezone/60)%60 ); | |
956 | if($timeZoneOffset > 0){ | |
957 | $timeZoneOffset = '+' . $timeZoneOffset; | |
958 | } | |
959 | return $timeZoneOffset; | |
960 | } | |
961 | ||
962 | ||
d8a4acc0 C |
963 | /** |
964 | * Reset any system caches that may be required for proper CiviCRM | |
965 | * integration. | |
966 | */ | |
967 | function flush() { | |
968 | drupal_flush_all_caches(); | |
969 | } | |
6a488035 | 970 | } |