Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
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 | /** | |
17f443df | 79 | * @inheritDoc |
6a488035 | 80 | */ |
00be9182 | 81 | public function createUser(&$params, $mail) { |
6a488035 TO |
82 | $form_state = array(); |
83 | $form_state['values'] = array( | |
84 | 'name' => $params['cms_name'], | |
85 | 'mail' => $params[$mail], | |
86 | 'op' => 'Create new account', | |
87 | ); | |
a57957f7 KJ |
88 | |
89 | $admin = user_access('administer users'); | |
90 | if (!variable_get('user_email_verification', TRUE) || $admin) { | |
6a488035 TO |
91 | $form_state['values']['pass']['pass1'] = $params['cms_pass']; |
92 | $form_state['values']['pass']['pass2'] = $params['cms_pass']; | |
93 | } | |
94 | ||
95 | $config = CRM_Core_Config::singleton(); | |
96 | ||
97 | // we also need to redirect b | |
98 | $config->inCiviCRM = TRUE; | |
99 | ||
100 | $form = drupal_retrieve_form('user_register', $form_state); | |
101 | $form['#post'] = $form_state['values']; | |
102 | drupal_prepare_form('user_register', $form, $form_state); | |
103 | ||
104 | // remove the captcha element from the form prior to processing | |
105 | unset($form['captcha']); | |
106 | ||
107 | drupal_process_form('user_register', $form, $form_state); | |
108 | ||
109 | $config->inCiviCRM = FALSE; | |
110 | ||
111 | if (form_get_errors() || !isset($form_state['user'])) { | |
112 | return FALSE; | |
113 | } | |
6a488035 | 114 | return $form_state['user']->uid; |
6a488035 TO |
115 | } |
116 | ||
bb3a214a | 117 | /** |
17f443df | 118 | * @inheritDoc |
bb3a214a | 119 | */ |
00be9182 | 120 | public function updateCMSName($ufID, $ufName) { |
6a488035 TO |
121 | // CRM-5555 |
122 | if (function_exists('user_load')) { | |
123 | $user = user_load(array('uid' => $ufID)); | |
124 | if ($user->mail != $ufName) { | |
125 | user_save($user, array('mail' => $ufName)); | |
126 | $user = user_load(array('uid' => $ufID)); | |
127 | } | |
128 | } | |
129 | } | |
130 | ||
131 | /** | |
fe482240 | 132 | * Check if username and email exists in the drupal db. |
6a488035 | 133 | * |
77855840 TO |
134 | * @param array $params |
135 | * Array of name and mail values. | |
136 | * @param array $errors | |
137 | * Array of errors. | |
138 | * @param string $emailName | |
139 | * Field label for the 'email'. | |
f4aaa82a | 140 | * |
6a488035 TO |
141 | * @return void |
142 | */ | |
00be9182 | 143 | public function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') { |
6a488035 TO |
144 | $config = CRM_Core_Config::singleton(); |
145 | ||
353ffa53 TO |
146 | $dao = new CRM_Core_DAO(); |
147 | $name = $dao->escape(CRM_Utils_Array::value('name', $params)); | |
6a488035 TO |
148 | $email = $dao->escape(CRM_Utils_Array::value('mail', $params)); |
149 | _user_edit_validate(NULL, $params); | |
150 | $errors = form_get_errors(); | |
6a488035 | 151 | if ($errors) { |
a7488080 | 152 | if (!empty($errors['name'])) { |
6a488035 TO |
153 | $errors['cms_name'] = $errors['name']; |
154 | } | |
a7488080 | 155 | if (!empty($errors['mail'])) { |
6a488035 TO |
156 | $errors[$emailName] = $errors['mail']; |
157 | } | |
158 | // also unset drupal messages to avoid twice display of errors | |
159 | unset($_SESSION['messages']); | |
160 | } | |
161 | ||
a5ecff8d | 162 | // Do the name check manually. |
6a488035 TO |
163 | $nameError = user_validate_name($params['name']); |
164 | if ($nameError) { | |
165 | $errors['cms_name'] = $nameError; | |
166 | } | |
167 | ||
168 | $sql = " | |
b67a6d82 E |
169 | SELECT name, mail |
170 | FROM {users} | |
171 | WHERE (LOWER(name) = LOWER('$name')) OR (LOWER(mail) = LOWER('$email')) | |
172 | "; | |
b67a6d82 | 173 | |
48f12f07 | 174 | $result = db_query($sql); |
8982d5f8 EM |
175 | $row = db_fetch_array($result); |
176 | if (!$row) { | |
b67a6d82 | 177 | return; |
6a488035 | 178 | } |
5a604d61 | 179 | |
b67a6d82 E |
180 | $user = NULL; |
181 | ||
6a488035 | 182 | if (!empty($row)) { |
b67a6d82 E |
183 | $dbName = CRM_Utils_Array::value('name', $row); |
184 | $dbEmail = CRM_Utils_Array::value('mail', $row); | |
6a488035 TO |
185 | if (strtolower($dbName) == strtolower($name)) { |
186 | $errors['cms_name'] = ts('The username %1 is already taken. Please select another username.', | |
187 | array(1 => $name) | |
188 | ); | |
189 | } | |
190 | if (strtolower($dbEmail) == strtolower($email)) { | |
22e263ad | 191 | if (empty($email)) { |
b67a6d82 E |
192 | $errors[$emailName] = ts('You cannot create an email account for a contact with no email', |
193 | array(1 => $email) | |
194 | ); | |
195 | } | |
92e4c2a5 | 196 | else { |
b67a6d82 E |
197 | $errors[$emailName] = ts('This email %1 is already registered. Please select another email.', |
198 | array(1 => $email) | |
199 | ); | |
200 | } | |
6a488035 TO |
201 | } |
202 | } | |
203 | } | |
204 | ||
f4aaa82a | 205 | /** |
17f443df | 206 | * @inheritDoc |
6a488035 | 207 | */ |
00be9182 | 208 | public function setTitle($title, $pageTitle = NULL) { |
6a488035 TO |
209 | if (!$pageTitle) { |
210 | $pageTitle = $title; | |
211 | } | |
212 | if (arg(0) == 'civicrm') { | |
213 | //set drupal title | |
214 | drupal_set_title($pageTitle); | |
215 | } | |
216 | } | |
217 | ||
218 | /** | |
17f443df | 219 | * @inheritDoc |
6a488035 | 220 | */ |
00be9182 | 221 | public function appendBreadCrumb($breadCrumbs) { |
6a488035 TO |
222 | $breadCrumb = drupal_get_breadcrumb(); |
223 | ||
224 | if (is_array($breadCrumbs)) { | |
225 | foreach ($breadCrumbs as $crumbs) { | |
226 | if (stripos($crumbs['url'], 'id%%')) { | |
227 | $args = array('cid', 'mid'); | |
228 | foreach ($args as $a) { | |
229 | $val = CRM_Utils_Request::retrieve($a, 'Positive', CRM_Core_DAO::$_nullObject, | |
230 | FALSE, NULL, $_GET | |
231 | ); | |
232 | if ($val) { | |
233 | $crumbs['url'] = str_ireplace("%%{$a}%%", $val, $crumbs['url']); | |
234 | } | |
235 | } | |
236 | } | |
237 | $breadCrumb[] = "<a href=\"{$crumbs['url']}\">{$crumbs['title']}</a>"; | |
238 | } | |
239 | } | |
240 | drupal_set_breadcrumb($breadCrumb); | |
241 | } | |
242 | ||
243 | /** | |
17f443df | 244 | * @inheritDoc |
6a488035 | 245 | */ |
00be9182 | 246 | public function resetBreadCrumb() { |
6a488035 TO |
247 | $bc = array(); |
248 | drupal_set_breadcrumb($bc); | |
249 | } | |
250 | ||
251 | /** | |
fe482240 | 252 | * Append a string to the head of the html file. |
6a488035 | 253 | * |
77855840 TO |
254 | * @param string $head |
255 | * The new string to be appended. | |
6a488035 TO |
256 | * |
257 | * @return void | |
6a488035 | 258 | */ |
00be9182 | 259 | public function addHTMLHead($head) { |
6a488035 TO |
260 | drupal_set_html_head($head); |
261 | } | |
262 | ||
6a488035 | 263 | /** |
fe482240 | 264 | * Add a css file. |
6a488035 | 265 | * |
353ffa53 | 266 | * @param $url : string, absolute path to file |
5a4f6742 CW |
267 | * @param string $region |
268 | * location within the document: 'html-header', 'page-header', 'page-footer'. | |
6a488035 TO |
269 | * |
270 | * Note: This function is not to be called directly | |
271 | * @see CRM_Core_Region::render() | |
272 | * | |
a6c01b45 CW |
273 | * @return bool |
274 | * TRUE if we support this operation in this CMS, FALSE otherwise | |
6a488035 TO |
275 | */ |
276 | public function addStyleUrl($url, $region) { | |
42e1a97c | 277 | if ($region != 'html-header' || !$this->formatResourceUrl($url)) { |
6a488035 TO |
278 | return FALSE; |
279 | } | |
280 | drupal_add_css($url); | |
281 | return TRUE; | |
282 | } | |
283 | ||
284 | /** | |
17f443df | 285 | * @inheritDoc |
6a488035 | 286 | */ |
00be9182 | 287 | public function mapConfigToSSL() { |
6a488035 TO |
288 | global $base_url; |
289 | $base_url = str_replace('http://', 'https://', $base_url); | |
290 | } | |
291 | ||
292 | /** | |
17f443df | 293 | * @inheritDoc |
6a488035 | 294 | */ |
00be9182 | 295 | public function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) { |
e7292422 TO |
296 | //@todo this 'PEAR-y' stuff is only required when bookstrap is not being loaded which is rare |
297 | // if ever now. | |
298 | // probably if bootstrap is loaded this call | |
299 | // CRM_Utils_System::loadBootStrap($bootStrapParams, TRUE, TRUE, $realPath); would be | |
300 | // sufficient to do what this fn does. It does exist as opposed to return which might need some hanky-panky to make | |
301 | // safe in the unknown situation where authenticate might be called & it is important that | |
302 | // false is returned | |
6a488035 TO |
303 | require_once 'DB.php'; |
304 | ||
305 | $config = CRM_Core_Config::singleton(); | |
306 | ||
307 | $dbDrupal = DB::connect($config->userFrameworkDSN); | |
308 | if (DB::isError($dbDrupal)) { | |
309 | CRM_Core_Error::fatal("Cannot connect to drupal db via $config->userFrameworkDSN, " . $dbDrupal->getMessage()); | |
310 | } | |
311 | ||
312 | $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; | |
353ffa53 TO |
313 | $dbpassword = md5($password); |
314 | $name = $dbDrupal->escapeSimple($strtolower($name)); | |
315 | $sql = 'SELECT u.* FROM ' . $config->userFrameworkUsersTableName . " u WHERE LOWER(u.name) = '$name' AND u.pass = '$dbpassword' AND u.status = 1"; | |
316 | $query = $dbDrupal->query($sql); | |
6a488035 TO |
317 | |
318 | $user = NULL; | |
319 | // need to change this to make sure we matched only one row | |
320 | while ($row = $query->fetchRow(DB_FETCHMODE_ASSOC)) { | |
321 | CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, $row['uid'], $row['mail'], 'Drupal'); | |
322 | $contactID = CRM_Core_BAO_UFMatch::getContactId($row['uid']); | |
323 | if (!$contactID) { | |
324 | return FALSE; | |
325 | } | |
ae5ffbb7 TO |
326 | else { |
327 | //success | |
6a488035 TO |
328 | if ($loadCMSBootstrap) { |
329 | $bootStrapParams = array(); | |
330 | if ($name && $password) { | |
331 | $bootStrapParams = array( | |
353ffa53 TO |
332 | 'name' => $name, |
333 | 'pass' => $password, | |
6a488035 TO |
334 | ); |
335 | } | |
336 | CRM_Utils_System::loadBootStrap($bootStrapParams, TRUE, TRUE, $realPath); | |
337 | } | |
a5ecff8d CB |
338 | return array($contactID, $row['uid'], mt_rand()); |
339 | } | |
6a488035 TO |
340 | } |
341 | return FALSE; | |
342 | } | |
343 | ||
f4aaa82a | 344 | /** |
17f443df | 345 | * @inheritDoc |
6a488035 | 346 | */ |
00be9182 | 347 | public function loadUser($username) { |
6a488035 TO |
348 | global $user; |
349 | $user = user_load(array('name' => $username)); | |
350 | if (empty($user->uid)) { | |
351 | return FALSE; | |
352 | } | |
353 | ||
354 | $uid = $user->uid; | |
355 | $contact_id = CRM_Core_BAO_UFMatch::getContactId($uid); | |
356 | ||
357 | // lets store contact id and user id in session | |
358 | $session = CRM_Core_Session::singleton(); | |
359 | $session->set('ufID', $uid); | |
360 | $session->set('userID', $contact_id); | |
361 | return TRUE; | |
362 | } | |
363 | ||
82d9c21e | 364 | /** |
53980972 | 365 | * Perform any post login activities required by the UF - |
366 | * e.g. for drupal : records a watchdog message about the new session, | |
367 | * saves the login timestamp, calls hook_user op 'login' and generates a new session. | |
e43cc689 | 368 | * |
ae5ffbb7 | 369 | * @param array $params |
95d68223 TO |
370 | * |
371 | * FIXME: Document values accepted/required by $params | |
53980972 | 372 | */ |
00be9182 | 373 | public function userLoginFinalize($params = array()) { |
53980972 | 374 | user_authenticate_finalize($params); |
82d9c21e | 375 | } |
376 | ||
46b6363c | 377 | /** |
fe482240 | 378 | * Determine the native ID of the CMS user. |
46b6363c | 379 | * |
100fef9d | 380 | * @param string $username |
46b6363c TO |
381 | * @return int|NULL |
382 | */ | |
00be9182 | 383 | public function getUfId($username) { |
46b6363c TO |
384 | $user = user_load(array('name' => $username)); |
385 | if (empty($user->uid)) { | |
386 | return NULL; | |
387 | } | |
388 | return $user->uid; | |
389 | } | |
390 | ||
6a488035 | 391 | /** |
17f443df | 392 | * @inheritDoc |
5bc392e6 | 393 | */ |
00be9182 | 394 | public function logout() { |
6a488035 TO |
395 | module_load_include('inc', 'user', 'user.pages'); |
396 | return user_logout(); | |
397 | } | |
398 | ||
6a488035 | 399 | /** |
fe482240 | 400 | * Load drupal bootstrap. |
6a488035 | 401 | * |
77855840 TO |
402 | * @param array $params |
403 | * Either uid, or name & pass. | |
404 | * @param bool $loadUser | |
405 | * Boolean Require CMS user load. | |
406 | * @param bool $throwError | |
407 | * If true, print error on failure and exit. | |
408 | * @param bool|string $realPath path to script | |
f4aaa82a EM |
409 | * |
410 | * @return bool | |
6a488035 | 411 | */ |
00be9182 | 412 | public function loadBootStrap($params = array(), $loadUser = TRUE, $throwError = TRUE, $realPath = NULL) { |
6a488035 TO |
413 | //take the cms root path. |
414 | $cmsPath = $this->cmsRootPath($realPath); | |
4459cd26 | 415 | |
6a488035 | 416 | if (!file_exists("$cmsPath/includes/bootstrap.inc")) { |
4459cd26 CB |
417 | if ($throwError) { |
418 | echo '<br />Sorry, could not locate bootstrap.inc\n'; | |
419 | exit(); | |
420 | } | |
421 | return FALSE; | |
6a488035 | 422 | } |
4459cd26 | 423 | // load drupal bootstrap |
6a488035 | 424 | chdir($cmsPath); |
4459cd26 CB |
425 | define('DRUPAL_ROOT', $cmsPath); |
426 | ||
427 | // For drupal multi-site CRM-11313 | |
428 | if ($realPath && strpos($realPath, 'sites/all/modules/') === FALSE) { | |
429 | preg_match('@sites/([^/]*)/modules@s', $realPath, $matches); | |
430 | if (!empty($matches[1])) { | |
431 | $_SERVER['HTTP_HOST'] = $matches[1]; | |
432 | } | |
433 | } | |
6a488035 | 434 | require_once 'includes/bootstrap.inc'; |
38507482 | 435 | // @ to suppress notices eg 'DRUPALFOO already defined'. |
6a488035 TO |
436 | @drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); |
437 | ||
4459cd26 CB |
438 | // explicitly setting error reporting, since we cannot handle drupal related notices |
439 | error_reporting(1); | |
a5ecff8d | 440 | if (!function_exists('module_exists') || !module_exists('civicrm')) { |
4459cd26 CB |
441 | if ($throwError) { |
442 | echo '<br />Sorry, could not load drupal bootstrap.'; | |
443 | exit(); | |
444 | } | |
445 | return FALSE; | |
6a488035 | 446 | } |
a5ecff8d | 447 | |
4459cd26 CB |
448 | // seems like we've bootstrapped drupal |
449 | $config = CRM_Core_Config::singleton(); | |
450 | ||
6a488035 TO |
451 | // lets also fix the clean url setting |
452 | // CRM-6948 | |
453 | $config->cleanURL = (int) variable_get('clean_url', '0'); | |
454 | ||
455 | // we need to call the config hook again, since we now know | |
456 | // all the modules that are listening on it, does not apply | |
457 | // to J! and WP as yet | |
458 | // CRM-8655 | |
459 | CRM_Utils_Hook::config($config); | |
460 | ||
461 | if (!$loadUser) { | |
462 | return TRUE; | |
463 | } | |
95915c38 | 464 | global $user; |
4459cd26 CB |
465 | // If $uid is passed in, authentication has been done already. |
466 | $uid = CRM_Utils_Array::value('uid', $params); | |
467 | if (!$uid) { | |
468 | //load user, we need to check drupal permissions. | |
469 | $name = CRM_Utils_Array::value('name', $params, FALSE) ? $params['name'] : trim(CRM_Utils_Array::value('name', $_REQUEST)); | |
470 | $pass = CRM_Utils_Array::value('pass', $params, FALSE) ? $params['pass'] : trim(CRM_Utils_Array::value('pass', $_REQUEST)); | |
471 | ||
472 | if ($name) { | |
95915c38 E |
473 | $user = user_authenticate(array('name' => $name, 'pass' => $pass)); |
474 | if (!$user->uid) { | |
4459cd26 CB |
475 | if ($throwError) { |
476 | echo '<br />Sorry, unrecognized username or password.'; | |
477 | exit(); | |
478 | } | |
479 | return FALSE; | |
480 | } | |
95915c38 E |
481 | else { |
482 | return TRUE; | |
483 | } | |
6a488035 TO |
484 | } |
485 | } | |
4459cd26 CB |
486 | |
487 | if ($uid) { | |
488 | $account = user_load($uid); | |
489 | if ($account && $account->uid) { | |
6a488035 | 490 | $user = $account; |
4459cd26 | 491 | return TRUE; |
6a488035 TO |
492 | } |
493 | } | |
4459cd26 CB |
494 | |
495 | if ($throwError) { | |
496 | echo '<br />Sorry, can not load CMS user account.'; | |
497 | exit(); | |
498 | } | |
499 | ||
500 | // CRM-6948: When using loadBootStrap, it's implicit that CiviCRM has already loaded its settings | |
501 | // which means that define(CIVICRM_CLEANURL) was correctly set. | |
502 | // So we correct it | |
503 | $config = CRM_Core_Config::singleton(); | |
e7292422 | 504 | $config->cleanURL = (int) variable_get('clean_url', '0'); |
4459cd26 CB |
505 | |
506 | // CRM-8655: Drupal wasn't available during bootstrap, so hook_civicrm_config never executes | |
507 | CRM_Utils_Hook::config($config); | |
508 | ||
509 | return FALSE; | |
6a488035 TO |
510 | } |
511 | ||
a5ecff8d | 512 | /** |
a5ecff8d | 513 | */ |
00be9182 | 514 | public function cmsRootPath($scriptFilename = NULL) { |
6a488035 TO |
515 | $cmsRoot = $valid = NULL; |
516 | ||
517 | if (!is_null($scriptFilename)) { | |
518 | $path = $scriptFilename; | |
519 | } | |
520 | else { | |
521 | $path = $_SERVER['SCRIPT_FILENAME']; | |
522 | } | |
a5ecff8d | 523 | |
6a488035 TO |
524 | if (function_exists('drush_get_context')) { |
525 | // drush anyway takes care of multisite install etc | |
526 | return drush_get_context('DRUSH_DRUPAL_ROOT'); | |
527 | } | |
528 | // CRM-7582 | |
529 | $pathVars = explode('/', | |
530 | str_replace('//', '/', | |
531 | str_replace('\\', '/', $path) | |
532 | ) | |
533 | ); | |
534 | ||
535 | //lets store first var, | |
536 | //need to get back for windows. | |
537 | $firstVar = array_shift($pathVars); | |
538 | ||
539 | //lets remove sript name to reduce one iteration. | |
540 | array_pop($pathVars); | |
541 | ||
542 | //CRM-7429 --do check for upper most 'includes' dir, | |
543 | //which would effectually work for multisite installation. | |
544 | do { | |
545 | $cmsRoot = $firstVar . '/' . implode('/', $pathVars); | |
546 | $cmsIncludePath = "$cmsRoot/includes"; | |
948d11bf | 547 | // Stop if we found bootstrap. |
f81c7606 | 548 | if (file_exists("$cmsIncludePath/bootstrap.inc")) { |
6a488035 TO |
549 | $valid = TRUE; |
550 | break; | |
551 | } | |
552 | //remove one directory level. | |
553 | array_pop($pathVars); | |
554 | } while (count($pathVars)); | |
555 | ||
556 | return ($valid) ? $cmsRoot : NULL; | |
557 | } | |
558 | ||
559 | /** | |
17f443df | 560 | * @inheritDoc |
6a488035 TO |
561 | */ |
562 | public function isUserLoggedIn() { | |
563 | $isloggedIn = FALSE; | |
564 | if (function_exists('user_is_logged_in')) { | |
565 | $isloggedIn = user_is_logged_in(); | |
566 | } | |
567 | ||
568 | return $isloggedIn; | |
569 | } | |
570 | ||
571 | /** | |
17f443df | 572 | * @inheritDoc |
6a488035 TO |
573 | */ |
574 | public function getLoggedInUfID() { | |
575 | $ufID = NULL; | |
576 | if (function_exists('user_is_logged_in') && | |
577 | user_is_logged_in() && | |
578 | function_exists('user_uid_optional_to_arg') | |
579 | ) { | |
580 | $ufID = user_uid_optional_to_arg(array()); | |
581 | } | |
582 | ||
583 | return $ufID; | |
584 | } | |
585 | ||
586 | /** | |
17f443df | 587 | * @inheritDoc |
6a488035 | 588 | */ |
00be9182 | 589 | public function languageNegotiationURL($url, $addLanguagePart = TRUE, $removeLanguagePart = FALSE) { |
6a488035 TO |
590 | if (empty($url)) { |
591 | return $url; | |
592 | } | |
593 | ||
594 | //upto d6 only, already we have code in place for d7 | |
595 | $config = CRM_Core_Config::singleton(); | |
596 | if (function_exists('variable_get') && | |
597 | module_exists('locale') | |
598 | ) { | |
599 | global $language; | |
600 | ||
601 | //get the mode. | |
602 | $mode = variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE); | |
603 | ||
604 | //url prefix / path. | |
605 | if (isset($language->prefix) && | |
606 | $language->prefix && | |
607 | in_array($mode, array( | |
608 | LANGUAGE_NEGOTIATION_PATH, | |
353ffa53 TO |
609 | LANGUAGE_NEGOTIATION_PATH_DEFAULT, |
610 | )) | |
6a488035 TO |
611 | ) { |
612 | ||
613 | if ($addLanguagePart) { | |
614 | $url .= $language->prefix . '/'; | |
615 | } | |
616 | if ($removeLanguagePart) { | |
617 | $url = str_replace("/{$language->prefix}/", '/', $url); | |
618 | } | |
619 | } | |
620 | if (isset($language->domain) && | |
621 | $language->domain && | |
622 | $mode == LANGUAGE_NEGOTIATION_DOMAIN | |
623 | ) { | |
624 | ||
625 | if ($addLanguagePart) { | |
626 | $url = CRM_Utils_File::addTrailingSlash($language->domain, '/'); | |
627 | } | |
628 | if ($removeLanguagePart && defined('CIVICRM_UF_BASEURL')) { | |
629 | $url = str_replace('\\', '/', $url); | |
630 | $parseUrl = parse_url($url); | |
631 | ||
632 | //kinda hackish but not sure how to do it right | |
633 | //hope http_build_url() will help at some point. | |
634 | if (is_array($parseUrl) && !empty($parseUrl)) { | |
353ffa53 TO |
635 | $urlParts = explode('/', $url); |
636 | $hostKey = array_search($parseUrl['host'], $urlParts); | |
637 | $ufUrlParts = parse_url(CIVICRM_UF_BASEURL); | |
6a488035 | 638 | $urlParts[$hostKey] = $ufUrlParts['host']; |
353ffa53 | 639 | $url = implode('/', $urlParts); |
6a488035 TO |
640 | } |
641 | } | |
642 | } | |
643 | } | |
644 | ||
645 | return $url; | |
646 | } | |
647 | ||
648 | /** | |
649 | * Find any users/roles/security-principals with the given permission | |
650 | * and replace it with one or more permissions. | |
651 | * | |
5a4f6742 CW |
652 | * @param string $oldPerm |
653 | * @param array $newPerms | |
77855840 | 654 | * Array, strings. |
6a488035 TO |
655 | * |
656 | * @return void | |
657 | */ | |
00be9182 | 658 | public function replacePermission($oldPerm, $newPerms) { |
6a488035 TO |
659 | $roles = user_roles(FALSE, $oldPerm); |
660 | foreach ($roles as $rid => $roleName) { | |
661 | $permList = db_result(db_query('SELECT perm FROM {permission} WHERE rid = %d', $rid)); | |
662 | $perms = drupal_map_assoc(explode(', ', $permList)); | |
663 | unset($perms[$oldPerm]); | |
664 | $perms = $perms + drupal_map_assoc($newPerms); | |
665 | $permList = implode(', ', $perms); | |
666 | db_query('UPDATE {permission} SET perm = "%s" WHERE rid = %d', $permList, $rid); | |
e70a7fc0 | 667 | /* @codingStandardsIgnoreStart |
6a488035 TO |
668 | if ( ! empty( $roles ) ) { |
669 | $rids = implode(',', array_keys($roles)); | |
670 | db_query( 'UPDATE {permission} SET perm = CONCAT( perm, \', edit all events\') WHERE rid IN (' . implode(',', array_keys($roles)) . ')' ); | |
671 | db_query( "UPDATE {permission} SET perm = REPLACE( perm, '%s', '%s' ) WHERE rid IN ($rids)", | |
e70a7fc0 TO |
672 | $oldPerm, implode(', ', $newPerms) ); |
673 | @codingStandardsIgnoreEnd */ | |
6a488035 TO |
674 | } |
675 | } | |
676 | ||
677 | /** | |
66e42142 | 678 | * @inheritDoc |
6a488035 | 679 | */ |
00be9182 | 680 | public function getModules() { |
6a488035 TO |
681 | $result = array(); |
682 | $q = db_query('SELECT name, status FROM {system} WHERE type = \'module\' AND schema_version <> -1'); | |
683 | while ($row = db_fetch_object($q)) { | |
684 | $result[] = new CRM_Core_Module('drupal.' . $row->name, ($row->status == 1) ? TRUE : FALSE); | |
685 | } | |
686 | return $result; | |
687 | } | |
688 | ||
689 | /** | |
17f443df | 690 | * @inheritDoc |
6a488035 TO |
691 | */ |
692 | public function getLoginURL($destination = '') { | |
693 | $config = CRM_Core_Config::singleton(); | |
694 | $loginURL = $config->userFrameworkBaseURL; | |
695 | $loginURL .= 'user'; | |
696 | if (!empty($destination)) { | |
697 | // append destination so user is returned to form they came from after login | |
698 | $loginURL .= '?destination=' . urlencode($destination); | |
699 | } | |
700 | return $loginURL; | |
701 | } | |
702 | ||
d761c4d8 | 703 | /** |
fe482240 | 704 | * Wrapper for og_membership creation. |
d761c4d8 | 705 | * |
77855840 TO |
706 | * @param int $ogID |
707 | * Organic Group ID. | |
708 | * @param int $drupalID | |
709 | * Drupal User ID. | |
6a488035 | 710 | */ |
9b873358 | 711 | public function og_membership_create($ogID, $drupalID) { |
481a74f4 | 712 | og_save_subscription($ogID, $drupalID, array('is_active' => 1)); |
6a488035 TO |
713 | } |
714 | ||
715 | /** | |
fe482240 | 716 | * Wrapper for og_membership deletion. |
d761c4d8 | 717 | * |
77855840 TO |
718 | * @param int $ogID |
719 | * Organic Group ID. | |
720 | * @param int $drupalID | |
721 | * Drupal User ID. | |
6a488035 | 722 | */ |
00be9182 | 723 | public function og_membership_delete($ogID, $drupalID) { |
481a74f4 | 724 | og_delete_subscription($ogID, $drupalID); |
6a488035 TO |
725 | } |
726 | ||
5a604d61 | 727 | /** |
17f443df | 728 | * @inheritDoc |
5a604d61 | 729 | */ |
00be9182 | 730 | public function getTimeZoneString() { |
5a604d61 E |
731 | global $user; |
732 | if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) { | |
733 | $timezone = $user->timezone; | |
0db6c3e1 TO |
734 | } |
735 | else { | |
e7292422 | 736 | $timezone = variable_get('date_default_timezone', NULL); |
5a604d61 | 737 | } |
48ec57ab TO |
738 | if (!$timezone) { |
739 | $timezone = parent::getTimeZoneString(); | |
5a604d61 | 740 | } |
48ec57ab | 741 | return $timezone; |
5a604d61 E |
742 | } |
743 | ||
6a488035 | 744 | } |