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