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