Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * Drupal specific stuff goes here | |
38 | */ | |
39 | class CRM_Utils_System_Drupal6 extends CRM_Utils_System_Base { | |
40 | function __construct() { | |
41 | $this->is_drupal = TRUE; | |
42 | $this->supports_form_extensions = TRUE; | |
43 | } | |
44 | ||
45 | /** | |
46 | * if we are using a theming system, invoke theme, else just print the | |
47 | * content | |
48 | * | |
49 | * @param string $content the content that will be themed | |
50 | * @param boolean $print are we displaying to the screen or bypassing theming? | |
51 | * @param boolean $maintenance for maintenance mode | |
52 | * | |
53 | * @return void prints content on stdout | |
54 | * @access public | |
55 | */ | |
56 | function theme(&$content, $print = FALSE, $maintenance = FALSE) { | |
57 | // TODO: Simplify; this was copied verbatim from CiviCRM 3.4's multi-UF theming function, but that's more complex than necessary | |
58 | if (function_exists('theme') && !$print) { | |
59 | if ($maintenance) { | |
60 | drupal_set_breadcrumb(''); | |
61 | drupal_maintenance_theme(); | |
62 | } | |
63 | ||
64 | // Arg 3 for D6 theme() is "show_blocks". Previously, we passed | |
65 | // through a badly named variable ("$args") which was almost always | |
66 | // TRUE (except on fatal error screen). However, this feature is | |
67 | // non-functional on D6 default themes, was purposefully removed from | |
68 | // D7, has no analog in other our other CMS's, and clutters the code. | |
69 | // Hard-wiring to TRUE should be OK. | |
70 | $out = theme('page', $content, TRUE); | |
71 | } | |
72 | else { | |
73 | $out = $content; | |
74 | } | |
75 | ||
76 | print $out; | |
77 | } | |
78 | ||
79 | /** | |
80 | * Function to create a user in Drupal. | |
81 | * | |
82 | * @param array $params associated array | |
83 | * @param string $mail email id for cms user | |
84 | * | |
85 | * @return uid if user exists, false otherwise | |
86 | * | |
87 | * @access public | |
88 | */ | |
89 | function createUser(&$params, $mail) { | |
90 | $form_state = array(); | |
91 | $form_state['values'] = array( | |
92 | 'name' => $params['cms_name'], | |
93 | 'mail' => $params[$mail], | |
94 | 'op' => 'Create new account', | |
95 | ); | |
a57957f7 KJ |
96 | |
97 | $admin = user_access('administer users'); | |
98 | if (!variable_get('user_email_verification', TRUE) || $admin) { | |
6a488035 TO |
99 | $form_state['values']['pass']['pass1'] = $params['cms_pass']; |
100 | $form_state['values']['pass']['pass2'] = $params['cms_pass']; | |
101 | } | |
102 | ||
103 | $config = CRM_Core_Config::singleton(); | |
104 | ||
105 | // we also need to redirect b | |
106 | $config->inCiviCRM = TRUE; | |
107 | ||
108 | $form = drupal_retrieve_form('user_register', $form_state); | |
109 | $form['#post'] = $form_state['values']; | |
110 | drupal_prepare_form('user_register', $form, $form_state); | |
111 | ||
112 | // remove the captcha element from the form prior to processing | |
113 | unset($form['captcha']); | |
114 | ||
115 | drupal_process_form('user_register', $form, $form_state); | |
116 | ||
117 | $config->inCiviCRM = FALSE; | |
118 | ||
119 | if (form_get_errors() || !isset($form_state['user'])) { | |
120 | return FALSE; | |
121 | } | |
6a488035 | 122 | return $form_state['user']->uid; |
6a488035 TO |
123 | } |
124 | ||
125 | /* | |
126 | * Change user name in host CMS | |
127 | * | |
128 | * @param integer $ufID User ID in CMS | |
129 | * @param string $ufName User name | |
130 | */ | |
131 | function updateCMSName($ufID, $ufName) { | |
132 | // CRM-5555 | |
133 | if (function_exists('user_load')) { | |
134 | $user = user_load(array('uid' => $ufID)); | |
135 | if ($user->mail != $ufName) { | |
136 | user_save($user, array('mail' => $ufName)); | |
137 | $user = user_load(array('uid' => $ufID)); | |
138 | } | |
139 | } | |
140 | } | |
141 | ||
142 | /** | |
143 | * Check if username and email exists in the drupal db | |
144 | * | |
145 | * @params $params array array of name and mail values | |
146 | * @params $errors array array of errors | |
147 | * @params $emailName string field label for the 'email' | |
148 | * | |
149 | * @return void | |
150 | */ | |
151 | function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') { | |
152 | $config = CRM_Core_Config::singleton(); | |
153 | ||
154 | $dao = new CRM_Core_DAO(); | |
155 | $name = $dao->escape(CRM_Utils_Array::value('name', $params)); | |
156 | $email = $dao->escape(CRM_Utils_Array::value('mail', $params)); | |
157 | _user_edit_validate(NULL, $params); | |
158 | $errors = form_get_errors(); | |
6a488035 TO |
159 | if ($errors) { |
160 | if (CRM_Utils_Array::value('name', $errors)) { | |
161 | $errors['cms_name'] = $errors['name']; | |
162 | } | |
163 | if (CRM_Utils_Array::value('mail', $errors)) { | |
164 | $errors[$emailName] = $errors['mail']; | |
165 | } | |
166 | // also unset drupal messages to avoid twice display of errors | |
167 | unset($_SESSION['messages']); | |
168 | } | |
169 | ||
a5ecff8d | 170 | // Do the name check manually. |
6a488035 TO |
171 | $nameError = user_validate_name($params['name']); |
172 | if ($nameError) { | |
173 | $errors['cms_name'] = $nameError; | |
174 | } | |
175 | ||
176 | $sql = " | |
177 | SELECT name, mail | |
178 | FROM {$config->userFrameworkUsersTableName} | |
179 | WHERE (LOWER(name) = LOWER('$name')) OR (LOWER(mail) = LOWER('$email'))"; | |
180 | ||
6a488035 TO |
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 | |
6a488035 TO |
425 | */ |
426 | function postURL($action) { | |
427 | if (!empty($action)) { | |
428 | return $action; | |
429 | } | |
430 | ||
431 | return $this->url($_GET['q']); | |
432 | } | |
433 | ||
434 | /** | |
435 | * Generate an internal CiviCRM URL (copied from DRUPAL/includes/common.inc#url) | |
436 | * | |
437 | * @param $path string The path being linked to, such as "civicrm/add" | |
438 | * @param $query string A query string to append to the link. | |
439 | * @param $absolute boolean Whether to force the output to be an absolute link (beginning with http:). | |
440 | * Useful for links that will be displayed outside the site, such as in an | |
441 | * RSS feed. | |
442 | * @param $fragment string A fragment identifier (named anchor) to append to the link. | |
443 | * @param $htmlize boolean whether to convert to html eqivalant | |
444 | * @param $frontend boolean a gross joomla hack | |
445 | * | |
446 | * @return string an HTML string containing a link to the given path. | |
447 | * @access public | |
448 | * | |
449 | */ | |
450 | function url($path = NULL, $query = NULL, $absolute = FALSE, | |
451 | $fragment = NULL, $htmlize = TRUE, | |
452 | $frontend = FALSE | |
453 | ) { | |
454 | $config = CRM_Core_Config::singleton(); | |
455 | $script = 'index.php'; | |
456 | ||
457 | $path = CRM_Utils_String::stripPathChars($path); | |
458 | ||
459 | if (isset($fragment)) { | |
460 | $fragment = '#' . $fragment; | |
461 | } | |
462 | ||
463 | if (!isset($config->useFrameworkRelativeBase)) { | |
464 | $base = parse_url($config->userFrameworkBaseURL); | |
465 | $config->useFrameworkRelativeBase = $base['path']; | |
466 | } | |
467 | $base = $absolute ? $config->userFrameworkBaseURL : $config->useFrameworkRelativeBase; | |
468 | ||
469 | $separator = $htmlize ? '&' : '&'; | |
470 | ||
471 | if (!$config->cleanURL) { | |
472 | if (isset($path)) { | |
473 | if (isset($query)) { | |
474 | return $base . $script . '?q=' . $path . $separator . $query . $fragment; | |
475 | } | |
476 | else { | |
477 | return $base . $script . '?q=' . $path . $fragment; | |
478 | } | |
479 | } | |
480 | else { | |
481 | if (isset($query)) { | |
482 | return $base . $script . '?' . $query . $fragment; | |
483 | } | |
484 | else { | |
485 | return $base . $fragment; | |
486 | } | |
487 | } | |
488 | } | |
489 | else { | |
490 | if (isset($path)) { | |
491 | if (isset($query)) { | |
492 | return $base . $path . '?' . $query . $fragment; | |
493 | } | |
494 | else { | |
495 | return $base . $path . $fragment; | |
496 | } | |
497 | } | |
498 | else { | |
499 | if (isset($query)) { | |
500 | return $base . $script . '?' . $query . $fragment; | |
501 | } | |
502 | else { | |
503 | return $base . $fragment; | |
504 | } | |
505 | } | |
506 | } | |
507 | } | |
508 | ||
509 | /** | |
510 | * Authenticate the user against the drupal db | |
511 | * | |
512 | * @param string $name the user name | |
513 | * @param string $password the password for the above user name | |
a5ecff8d CB |
514 | * @param boolean $loadCMSBootstrap load cms bootstrap? |
515 | * @param NULL|string $realPath filename of script | |
6a488035 TO |
516 | * |
517 | * @return mixed false if no auth | |
518 | * array( | |
519 | * contactID, ufID, unique string ) if success | |
520 | * @access public | |
521 | */ | |
522 | function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) { | |
523 | require_once 'DB.php'; | |
524 | ||
525 | $config = CRM_Core_Config::singleton(); | |
526 | ||
527 | $dbDrupal = DB::connect($config->userFrameworkDSN); | |
528 | if (DB::isError($dbDrupal)) { | |
529 | CRM_Core_Error::fatal("Cannot connect to drupal db via $config->userFrameworkDSN, " . $dbDrupal->getMessage()); | |
530 | } | |
531 | ||
532 | $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; | |
533 | $dbpassword = md5($password); | |
534 | $name = $dbDrupal->escapeSimple($strtolower($name)); | |
535 | $sql = 'SELECT u.* FROM ' . $config->userFrameworkUsersTableName . " u WHERE LOWER(u.name) = '$name' AND u.pass = '$dbpassword' AND u.status = 1"; | |
536 | $query = $dbDrupal->query($sql); | |
537 | ||
538 | $user = NULL; | |
539 | // need to change this to make sure we matched only one row | |
540 | while ($row = $query->fetchRow(DB_FETCHMODE_ASSOC)) { | |
541 | CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, $row['uid'], $row['mail'], 'Drupal'); | |
542 | $contactID = CRM_Core_BAO_UFMatch::getContactId($row['uid']); | |
543 | if (!$contactID) { | |
544 | return FALSE; | |
545 | } | |
546 | else{//success | |
547 | if ($loadCMSBootstrap) { | |
548 | $bootStrapParams = array(); | |
549 | if ($name && $password) { | |
550 | $bootStrapParams = array( | |
551 | 'name' => $name, | |
552 | 'pass' => $password, | |
553 | ); | |
554 | } | |
555 | CRM_Utils_System::loadBootStrap($bootStrapParams, TRUE, TRUE, $realPath); | |
556 | } | |
a5ecff8d CB |
557 | return array($contactID, $row['uid'], mt_rand()); |
558 | } | |
6a488035 TO |
559 | } |
560 | return FALSE; | |
561 | } | |
562 | ||
563 | /* | |
564 | * Load user into session | |
565 | */ | |
566 | function loadUser($username) { | |
567 | global $user; | |
568 | $user = user_load(array('name' => $username)); | |
569 | if (empty($user->uid)) { | |
570 | return FALSE; | |
571 | } | |
572 | ||
573 | $uid = $user->uid; | |
574 | $contact_id = CRM_Core_BAO_UFMatch::getContactId($uid); | |
575 | ||
576 | // lets store contact id and user id in session | |
577 | $session = CRM_Core_Session::singleton(); | |
578 | $session->set('ufID', $uid); | |
579 | $session->set('userID', $contact_id); | |
580 | return TRUE; | |
581 | } | |
582 | ||
583 | /** | |
584 | * Set a message in the UF to display to a user | |
585 | * | |
586 | * @param string $message the message to set | |
587 | * | |
588 | * @access public | |
589 | */ | |
590 | function setMessage($message) { | |
591 | drupal_set_message($message); | |
592 | } | |
593 | ||
594 | function permissionDenied() { | |
595 | drupal_access_denied(); | |
596 | } | |
597 | ||
598 | function logout() { | |
599 | module_load_include('inc', 'user', 'user.pages'); | |
600 | return user_logout(); | |
601 | } | |
602 | ||
603 | function updateCategories() { | |
604 | // copied this from profile.module. Seems a bit inefficient, but i dont know a better way | |
605 | // CRM-3600 | |
606 | cache_clear_all(); | |
607 | menu_rebuild(); | |
608 | } | |
609 | ||
610 | /** | |
611 | * Get the locale set in the hosting CMS | |
612 | * | |
613 | * @return string with the locale or null for none | |
614 | */ | |
615 | function getUFLocale() { | |
616 | // return CiviCRM’s xx_YY locale that either matches Drupal’s Chinese locale | |
617 | // (for CRM-6281), Drupal’s xx_YY or is retrieved based on Drupal’s xx | |
618 | global $language; | |
619 | switch (TRUE) { | |
620 | case $language->language == 'zh-hans': | |
621 | return 'zh_CN'; | |
622 | ||
623 | case $language->language == 'zh-hant': | |
624 | return 'zh_TW'; | |
625 | ||
626 | case preg_match('/^.._..$/', $language->language): | |
627 | return $language->language; | |
628 | ||
629 | default: | |
630 | return CRM_Core_I18n_PseudoConstant::longForShort(substr($language->language, 0, 2)); | |
631 | } | |
632 | } | |
633 | ||
634 | function getVersion() { | |
635 | return defined('VERSION') ? VERSION : 'Unknown'; | |
636 | } | |
637 | ||
638 | /** | |
639 | * load drupal bootstrap | |
640 | * | |
a5ecff8d CB |
641 | * @param array $params Either uid, or name & pass. |
642 | * @param boolean $loadUser boolean Require CMS user load. | |
643 | * @param boolean $throwError If true, print error on failure and exit. | |
644 | * @param boolean|string $realPath path to script | |
6a488035 | 645 | */ |
a5ecff8d | 646 | function loadBootStrap($params = array(), $loadUser = TRUE, $throwError = TRUE, $realPath = NULL) { |
6a488035 TO |
647 | $uid = CRM_Utils_Array::value('uid', $params); |
648 | $name = CRM_Utils_Array::value('name', $params, FALSE) ? $params['name'] : trim(CRM_Utils_Array::value('name', $_REQUEST)); | |
649 | $pass = CRM_Utils_Array::value('pass', $params, FALSE) ? $params['pass'] : trim(CRM_Utils_Array::value('pass', $_REQUEST)); | |
650 | ||
651 | //take the cms root path. | |
652 | $cmsPath = $this->cmsRootPath($realPath); | |
653 | if (!file_exists("$cmsPath/includes/bootstrap.inc")) { | |
654 | echo '<br />Sorry, unable to locate bootstrap.inc.'; | |
655 | exit(); | |
656 | } | |
657 | ||
658 | chdir($cmsPath); | |
659 | require_once 'includes/bootstrap.inc'; | |
660 | @drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); | |
661 | ||
a5ecff8d CB |
662 | if (!function_exists('module_exists') || !module_exists('civicrm')) { |
663 | echo '<br />Sorry, could not load drupal bootstrap.'; | |
6a488035 TO |
664 | exit(); |
665 | } | |
a5ecff8d | 666 | |
6a488035 TO |
667 | // lets also fix the clean url setting |
668 | // CRM-6948 | |
669 | $config->cleanURL = (int) variable_get('clean_url', '0'); | |
670 | ||
671 | // we need to call the config hook again, since we now know | |
672 | // all the modules that are listening on it, does not apply | |
673 | // to J! and WP as yet | |
674 | // CRM-8655 | |
675 | CRM_Utils_Hook::config($config); | |
676 | ||
677 | if (!$loadUser) { | |
678 | return TRUE; | |
679 | } | |
680 | //load user, we need to check drupal permissions. | |
681 | if ($name) { | |
682 | $user = user_authenticate(array('name' => $name, 'pass' => $pass)); | |
683 | if (empty($user->uid)) { | |
684 | echo '<br />Sorry, unrecognized username or password.'; | |
685 | exit(); | |
686 | } | |
687 | } | |
688 | elseif ($uid) { | |
689 | $account = user_load(array('uid' => $uid)); | |
690 | if (empty($account->uid)) { | |
691 | echo '<br />Sorry, unrecognized user id.'; | |
692 | exit(); | |
693 | } | |
694 | else { | |
695 | global $user; | |
696 | $user = $account; | |
697 | } | |
698 | } | |
699 | } | |
700 | ||
a5ecff8d CB |
701 | /** |
702 | * | |
703 | */ | |
6a488035 TO |
704 | function cmsRootPath($scriptFilename = NULL) { |
705 | $cmsRoot = $valid = NULL; | |
706 | ||
707 | if (!is_null($scriptFilename)) { | |
708 | $path = $scriptFilename; | |
709 | } | |
710 | else { | |
711 | $path = $_SERVER['SCRIPT_FILENAME']; | |
712 | } | |
a5ecff8d | 713 | |
6a488035 TO |
714 | if (function_exists('drush_get_context')) { |
715 | // drush anyway takes care of multisite install etc | |
716 | return drush_get_context('DRUSH_DRUPAL_ROOT'); | |
717 | } | |
718 | // CRM-7582 | |
719 | $pathVars = explode('/', | |
720 | str_replace('//', '/', | |
721 | str_replace('\\', '/', $path) | |
722 | ) | |
723 | ); | |
724 | ||
725 | //lets store first var, | |
726 | //need to get back for windows. | |
727 | $firstVar = array_shift($pathVars); | |
728 | ||
729 | //lets remove sript name to reduce one iteration. | |
730 | array_pop($pathVars); | |
731 | ||
732 | //CRM-7429 --do check for upper most 'includes' dir, | |
733 | //which would effectually work for multisite installation. | |
734 | do { | |
735 | $cmsRoot = $firstVar . '/' . implode('/', $pathVars); | |
736 | $cmsIncludePath = "$cmsRoot/includes"; | |
737 | //stop as we found bootstrap. | |
738 | if (@opendir($cmsIncludePath) && | |
739 | file_exists("$cmsIncludePath/bootstrap.inc") | |
740 | ) { | |
741 | $valid = TRUE; | |
742 | break; | |
743 | } | |
744 | //remove one directory level. | |
745 | array_pop($pathVars); | |
746 | } while (count($pathVars)); | |
747 | ||
748 | return ($valid) ? $cmsRoot : NULL; | |
749 | } | |
750 | ||
751 | /** | |
752 | * check is user logged in. | |
753 | * | |
754 | * @return boolean true/false. | |
755 | */ | |
756 | public function isUserLoggedIn() { | |
757 | $isloggedIn = FALSE; | |
758 | if (function_exists('user_is_logged_in')) { | |
759 | $isloggedIn = user_is_logged_in(); | |
760 | } | |
761 | ||
762 | return $isloggedIn; | |
763 | } | |
764 | ||
765 | /** | |
766 | * Get currently logged in user uf id. | |
767 | * | |
768 | * @return int $userID logged in user uf id. | |
769 | */ | |
770 | public function getLoggedInUfID() { | |
771 | $ufID = NULL; | |
772 | if (function_exists('user_is_logged_in') && | |
773 | user_is_logged_in() && | |
774 | function_exists('user_uid_optional_to_arg') | |
775 | ) { | |
776 | $ufID = user_uid_optional_to_arg(array()); | |
777 | } | |
778 | ||
779 | return $ufID; | |
780 | } | |
781 | ||
782 | /** | |
783 | * Format the url as per language Negotiation. | |
784 | * | |
785 | * @param string $url | |
786 | * | |
787 | * @return string $url, formatted url. | |
788 | * @static | |
789 | */ | |
a5ecff8d | 790 | function languageNegotiationURL($url, $addLanguagePart = TRUE, $removeLanguagePart = FALSE) { |
6a488035 TO |
791 | if (empty($url)) { |
792 | return $url; | |
793 | } | |
794 | ||
795 | //upto d6 only, already we have code in place for d7 | |
796 | $config = CRM_Core_Config::singleton(); | |
797 | if (function_exists('variable_get') && | |
798 | module_exists('locale') | |
799 | ) { | |
800 | global $language; | |
801 | ||
802 | //get the mode. | |
803 | $mode = variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE); | |
804 | ||
805 | //url prefix / path. | |
806 | if (isset($language->prefix) && | |
807 | $language->prefix && | |
808 | in_array($mode, array( | |
809 | LANGUAGE_NEGOTIATION_PATH, | |
810 | LANGUAGE_NEGOTIATION_PATH_DEFAULT, | |
811 | )) | |
812 | ) { | |
813 | ||
814 | if ($addLanguagePart) { | |
815 | $url .= $language->prefix . '/'; | |
816 | } | |
817 | if ($removeLanguagePart) { | |
818 | $url = str_replace("/{$language->prefix}/", '/', $url); | |
819 | } | |
820 | } | |
821 | if (isset($language->domain) && | |
822 | $language->domain && | |
823 | $mode == LANGUAGE_NEGOTIATION_DOMAIN | |
824 | ) { | |
825 | ||
826 | if ($addLanguagePart) { | |
827 | $url = CRM_Utils_File::addTrailingSlash($language->domain, '/'); | |
828 | } | |
829 | if ($removeLanguagePart && defined('CIVICRM_UF_BASEURL')) { | |
830 | $url = str_replace('\\', '/', $url); | |
831 | $parseUrl = parse_url($url); | |
832 | ||
833 | //kinda hackish but not sure how to do it right | |
834 | //hope http_build_url() will help at some point. | |
835 | if (is_array($parseUrl) && !empty($parseUrl)) { | |
836 | $urlParts = explode('/', $url); | |
837 | $hostKey = array_search($parseUrl['host'], $urlParts); | |
838 | $ufUrlParts = parse_url(CIVICRM_UF_BASEURL); | |
839 | $urlParts[$hostKey] = $ufUrlParts['host']; | |
840 | $url = implode('/', $urlParts); | |
841 | } | |
842 | } | |
843 | } | |
844 | } | |
845 | ||
846 | return $url; | |
847 | } | |
848 | ||
849 | /** | |
850 | * Find any users/roles/security-principals with the given permission | |
851 | * and replace it with one or more permissions. | |
852 | * | |
853 | * @param $oldPerm string | |
854 | * @param $newPerms array, strings | |
855 | * | |
856 | * @return void | |
857 | */ | |
858 | function replacePermission($oldPerm, $newPerms) { | |
859 | $roles = user_roles(FALSE, $oldPerm); | |
860 | foreach ($roles as $rid => $roleName) { | |
861 | $permList = db_result(db_query('SELECT perm FROM {permission} WHERE rid = %d', $rid)); | |
862 | $perms = drupal_map_assoc(explode(', ', $permList)); | |
863 | unset($perms[$oldPerm]); | |
864 | $perms = $perms + drupal_map_assoc($newPerms); | |
865 | $permList = implode(', ', $perms); | |
866 | db_query('UPDATE {permission} SET perm = "%s" WHERE rid = %d', $permList, $rid); | |
867 | /* | |
868 | if ( ! empty( $roles ) ) { | |
869 | $rids = implode(',', array_keys($roles)); | |
870 | db_query( 'UPDATE {permission} SET perm = CONCAT( perm, \', edit all events\') WHERE rid IN (' . implode(',', array_keys($roles)) . ')' ); | |
871 | db_query( "UPDATE {permission} SET perm = REPLACE( perm, '%s', '%s' ) WHERE rid IN ($rids)", | |
872 | $oldPerm, implode(', ', $newPerms) );*/ | |
873 | } | |
874 | } | |
875 | ||
876 | /** | |
877 | * Get a list of all installed modules, including enabled and disabled ones | |
878 | * | |
879 | * @return array CRM_Core_Module | |
880 | */ | |
881 | function getModules() { | |
882 | $result = array(); | |
883 | $q = db_query('SELECT name, status FROM {system} WHERE type = \'module\' AND schema_version <> -1'); | |
884 | while ($row = db_fetch_object($q)) { | |
885 | $result[] = new CRM_Core_Module('drupal.' . $row->name, ($row->status == 1) ? TRUE : FALSE); | |
886 | } | |
887 | return $result; | |
888 | } | |
889 | ||
890 | /** | |
891 | * Get user login URL for hosting CMS (method declared in each CMS system class) | |
892 | * | |
893 | * @param string $destination - if present, add destination to querystring (works for Drupal only) | |
894 | * | |
895 | * @return string - loginURL for the current CMS | |
896 | * @static | |
897 | */ | |
898 | public function getLoginURL($destination = '') { | |
899 | $config = CRM_Core_Config::singleton(); | |
900 | $loginURL = $config->userFrameworkBaseURL; | |
901 | $loginURL .= 'user'; | |
902 | if (!empty($destination)) { | |
903 | // append destination so user is returned to form they came from after login | |
904 | $loginURL .= '?destination=' . urlencode($destination); | |
905 | } | |
906 | return $loginURL; | |
907 | } | |
908 | ||
d761c4d8 | 909 | /** |
6a488035 | 910 | * Wrapper for og_membership creation |
d761c4d8 | 911 | * |
912 | * @param integer $ogID Organic Group ID | |
913 | * @param integer $drupalID drupal User ID | |
6a488035 TO |
914 | */ |
915 | function og_membership_create($ogID, $drupalID){ | |
d761c4d8 | 916 | og_save_subscription( $ogID, $drupalID, array( 'is_active' => 1 ) ); |
6a488035 TO |
917 | } |
918 | ||
919 | /** | |
920 | * Wrapper for og_membership deletion | |
d761c4d8 | 921 | * |
922 | * @param integer $ogID Organic Group ID | |
923 | * @param integer $drupalID drupal User ID | |
6a488035 TO |
924 | */ |
925 | function og_membership_delete($ogID, $drupalID) { | |
d761c4d8 | 926 | og_delete_subscription( $ogID, $drupalID ); |
6a488035 TO |
927 | } |
928 | ||
d8a4acc0 C |
929 | /** |
930 | * Reset any system caches that may be required for proper CiviCRM | |
931 | * integration. | |
932 | */ | |
933 | function flush() { | |
934 | drupal_flush_all_caches(); | |
935 | } | |
6a488035 | 936 | } |