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