Commit | Line | Data |
---|---|---|
84fce21c TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.7 | | |
5 | +--------------------------------------------------------------------+ | |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
84fce21c 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 | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
84fce21c TO |
32 | */ |
33 | ||
34 | /** | |
ddd5c06f | 35 | * Backdrop-specific logic that differs from Drupal. |
84fce21c TO |
36 | */ |
37 | class CRM_Utils_System_Backdrop extends CRM_Utils_System_DrupalBase { | |
38 | ||
39 | /** | |
40 | * @inheritDoc | |
41 | */ | |
42 | public function createUser(&$params, $mail) { | |
43 | $form_state = form_state_defaults(); | |
44 | ||
45 | $form_state['input'] = array( | |
46 | 'name' => $params['cms_name'], | |
47 | 'mail' => $params[$mail], | |
48 | 'op' => 'Create new account', | |
49 | ); | |
50 | ||
51 | $admin = user_access('administer users'); | |
ddd5c06f | 52 | if (!config_get('system.core', 'user_email_verification') || $admin) { |
84fce21c TO |
53 | $form_state['input']['pass'] = array('pass1' => $params['cms_pass'], 'pass2' => $params['cms_pass']); |
54 | } | |
55 | ||
56 | if (!empty($params['notify'])) { | |
57 | $form_state['input']['notify'] = $params['notify']; | |
58 | } | |
59 | ||
60 | $form_state['rebuild'] = FALSE; | |
61 | $form_state['programmed'] = TRUE; | |
62 | $form_state['complete form'] = FALSE; | |
63 | $form_state['method'] = 'post'; | |
64 | $form_state['build_info']['args'] = array(); | |
65 | /* | |
66 | * if we want to submit this form more than once in a process (e.g. create more than one user) | |
67 | * we must force it to validate each time for this form. Otherwise it will not validate | |
68 | * subsequent submissions and the manner in which the password is passed in will be invalid | |
69 | */ | |
70 | $form_state['must_validate'] = TRUE; | |
71 | $config = CRM_Core_Config::singleton(); | |
72 | ||
73 | // we also need to redirect b | |
74 | $config->inCiviCRM = TRUE; | |
75 | ||
84da17e3 | 76 | $form = backdrop_retrieve_form('user_register_form', $form_state); |
84fce21c TO |
77 | $form_state['process_input'] = 1; |
78 | $form_state['submitted'] = 1; | |
79 | $form['#array_parents'] = array(); | |
80 | $form['#tree'] = FALSE; | |
84da17e3 | 81 | backdrop_process_form('user_register_form', $form, $form_state); |
84fce21c TO |
82 | |
83 | $config->inCiviCRM = FALSE; | |
84 | ||
85 | if (form_get_errors()) { | |
86 | return FALSE; | |
87 | } | |
88 | return $form_state['user']->uid; | |
89 | } | |
90 | ||
91 | /** | |
92 | * @inheritDoc | |
93 | */ | |
ddd5c06f | 94 | public function updateCMSName($ufID, $email) { |
84fce21c TO |
95 | // CRM-5555 |
96 | if (function_exists('user_load')) { | |
97 | $user = user_load($ufID); | |
ddd5c06f NH |
98 | if ($user->mail != $email) { |
99 | $user->mail = $email; | |
100 | $user->save(); | |
84fce21c TO |
101 | } |
102 | } | |
103 | } | |
104 | ||
105 | /** | |
84da17e3 | 106 | * Check if username and email exists in the Backdrop db. |
84fce21c TO |
107 | * |
108 | * @param array $params | |
109 | * Array of name and mail values. | |
110 | * @param array $errors | |
111 | * Array of errors. | |
112 | * @param string $emailName | |
113 | * Field label for the 'email'. | |
114 | */ | |
115 | public static function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') { | |
84fce21c TO |
116 | $errors = form_get_errors(); |
117 | if ($errors) { | |
84da17e3 | 118 | // unset Backdrop messages to avoid twice display of errors |
84fce21c TO |
119 | unset($_SESSION['messages']); |
120 | } | |
121 | ||
122 | if (!empty($params['name'])) { | |
123 | if ($nameError = user_validate_name($params['name'])) { | |
124 | $errors['cms_name'] = $nameError; | |
125 | } | |
126 | else { | |
ddd5c06f | 127 | $uid = db_query("SELECT uid FROM {users} WHERE name = :name", array(':name' => $params['name']))->fetchField(); |
84fce21c TO |
128 | if ((bool) $uid) { |
129 | $errors['cms_name'] = ts('The username %1 is already taken. Please select another username.', array(1 => $params['name'])); | |
130 | } | |
131 | } | |
132 | } | |
133 | ||
134 | if (!empty($params['mail'])) { | |
ddd5c06f NH |
135 | if (!valid_email_address($params['mail'])) { |
136 | $errors[$emailName] = ts('The e-mail address %1 is not valid.', array('%1' => $params['mail'])); | |
84fce21c TO |
137 | } |
138 | else { | |
ddd5c06f | 139 | $uid = db_query("SELECT uid FROM {users} WHERE mail = :mail", array(':mail' => $params['mail']))->fetchField(); |
84fce21c | 140 | if ((bool) $uid) { |
ddd5c06f | 141 | $resetUrl = url('user/password'); |
84fce21c TO |
142 | $errors[$emailName] = ts('The email address %1 already has an account associated with it. <a href="%2">Have you forgotten your password?</a>', |
143 | array(1 => $params['mail'], 2 => $resetUrl) | |
144 | ); | |
145 | } | |
146 | } | |
147 | } | |
148 | } | |
149 | ||
150 | /** | |
151 | * @inheritDoc | |
152 | */ | |
153 | public function getLoginURL($destination = '') { | |
154 | $query = $destination ? array('destination' => $destination) : array(); | |
ddd5c06f | 155 | return url('user', array('query' => $query, 'absolute' => TRUE)); |
84fce21c TO |
156 | } |
157 | ||
158 | /** | |
159 | * @inheritDoc | |
160 | */ | |
161 | public function setTitle($title, $pageTitle = NULL) { | |
162 | if (arg(0) == 'civicrm') { | |
163 | if (!$pageTitle) { | |
164 | $pageTitle = $title; | |
165 | } | |
166 | ||
84da17e3 | 167 | backdrop_set_title($pageTitle, PASS_THROUGH); |
84fce21c TO |
168 | } |
169 | } | |
170 | ||
171 | /** | |
172 | * @inheritDoc | |
173 | */ | |
174 | public function appendBreadCrumb($breadCrumbs) { | |
84da17e3 | 175 | $breadCrumb = backdrop_get_breadcrumb(); |
84fce21c TO |
176 | |
177 | if (is_array($breadCrumbs)) { | |
178 | foreach ($breadCrumbs as $crumbs) { | |
179 | if (stripos($crumbs['url'], 'id%%')) { | |
180 | $args = array('cid', 'mid'); | |
181 | foreach ($args as $a) { | |
182 | $val = CRM_Utils_Request::retrieve($a, 'Positive', CRM_Core_DAO::$_nullObject, | |
183 | FALSE, NULL, $_GET | |
184 | ); | |
185 | if ($val) { | |
186 | $crumbs['url'] = str_ireplace("%%{$a}%%", $val, $crumbs['url']); | |
187 | } | |
188 | } | |
189 | } | |
190 | $breadCrumb[] = "<a href=\"{$crumbs['url']}\">{$crumbs['title']}</a>"; | |
191 | } | |
192 | } | |
84da17e3 | 193 | backdrop_set_breadcrumb($breadCrumb); |
84fce21c TO |
194 | } |
195 | ||
196 | /** | |
197 | * @inheritDoc | |
198 | */ | |
199 | public function resetBreadCrumb() { | |
200 | $bc = array(); | |
84da17e3 | 201 | backdrop_set_breadcrumb($bc); |
84fce21c TO |
202 | } |
203 | ||
204 | /** | |
205 | * @inheritDoc | |
206 | */ | |
207 | public function addHTMLHead($header) { | |
208 | static $count = 0; | |
209 | if (!empty($header)) { | |
210 | $key = 'civi_' . ++$count; | |
211 | $data = array( | |
212 | '#type' => 'markup', | |
213 | '#markup' => $header, | |
214 | ); | |
84da17e3 | 215 | backdrop_add_html_head($data, $key); |
84fce21c TO |
216 | } |
217 | } | |
218 | ||
219 | /** | |
220 | * @inheritDoc | |
221 | */ | |
222 | public function addScriptUrl($url, $region) { | |
223 | $params = array('group' => JS_LIBRARY, 'weight' => 10); | |
224 | switch ($region) { | |
225 | case 'html-header': | |
226 | case 'page-footer': | |
227 | $params['scope'] = substr($region, 5); | |
228 | break; | |
229 | ||
230 | default: | |
231 | return FALSE; | |
232 | } | |
84da17e3 | 233 | // If the path is within the Backdrop directory we can use the more efficient 'file' setting |
84fce21c | 234 | $params['type'] = $this->formatResourceUrl($url) ? 'file' : 'external'; |
84da17e3 | 235 | backdrop_add_js($url, $params); |
84fce21c TO |
236 | return TRUE; |
237 | } | |
238 | ||
239 | /** | |
240 | * @inheritDoc | |
241 | */ | |
242 | public function addScript($code, $region) { | |
243 | $params = array('type' => 'inline', 'group' => JS_LIBRARY, 'weight' => 10); | |
244 | switch ($region) { | |
245 | case 'html-header': | |
246 | case 'page-footer': | |
247 | $params['scope'] = substr($region, 5); | |
248 | break; | |
249 | ||
250 | default: | |
251 | return FALSE; | |
252 | } | |
84da17e3 | 253 | backdrop_add_js($code, $params); |
84fce21c TO |
254 | return TRUE; |
255 | } | |
256 | ||
257 | /** | |
258 | * @inheritDoc | |
259 | */ | |
260 | public function addStyleUrl($url, $region) { | |
261 | if ($region != 'html-header') { | |
262 | return FALSE; | |
263 | } | |
264 | $params = array(); | |
84da17e3 | 265 | // If the path is within the Backdrop directory we can use the more efficient 'file' setting |
84fce21c | 266 | $params['type'] = $this->formatResourceUrl($url) ? 'file' : 'external'; |
84da17e3 | 267 | backdrop_add_css($url, $params); |
84fce21c TO |
268 | return TRUE; |
269 | } | |
270 | ||
271 | /** | |
272 | * @inheritDoc | |
273 | */ | |
274 | public function addStyle($code, $region) { | |
275 | if ($region != 'html-header') { | |
276 | return FALSE; | |
277 | } | |
278 | $params = array('type' => 'inline'); | |
84da17e3 | 279 | backdrop_add_css($code, $params); |
84fce21c TO |
280 | return TRUE; |
281 | } | |
282 | ||
283 | /** | |
284 | * @inheritDoc | |
285 | */ | |
286 | public function mapConfigToSSL() { | |
287 | global $base_url; | |
288 | $base_url = str_replace('http://', 'https://', $base_url); | |
289 | } | |
290 | ||
8246bca4 | 291 | /** |
292 | * Get the name of the table that stores the user details. | |
293 | * | |
294 | * @return string | |
295 | */ | |
84fce21c TO |
296 | protected function getUsersTableName() { |
297 | $userFrameworkUsersTableName = Civi::settings()->get('userFrameworkUsersTableName'); | |
298 | if (empty($userFrameworkUsersTableName)) { | |
299 | $userFrameworkUsersTableName = 'users'; | |
300 | } | |
301 | return $userFrameworkUsersTableName; | |
302 | } | |
303 | ||
304 | /** | |
305 | * @inheritDoc | |
306 | */ | |
307 | public function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) { | |
84fce21c TO |
308 | $config = CRM_Core_Config::singleton(); |
309 | ||
ddd5c06f NH |
310 | $dbBackdrop = DB::connect($config->userFrameworkDSN); |
311 | if (DB::isError($dbBackdrop)) { | |
312 | CRM_Core_Error::fatal("Cannot connect to Backdrop database via $config->userFrameworkDSN, " . $dbBackdrop->getMessage()); | |
84fce21c TO |
313 | } |
314 | ||
315 | $account = $userUid = $userMail = NULL; | |
316 | if ($loadCMSBootstrap) { | |
317 | $bootStrapParams = array(); | |
318 | if ($name && $password) { | |
319 | $bootStrapParams = array( | |
320 | 'name' => $name, | |
321 | 'pass' => $password, | |
322 | ); | |
323 | } | |
324 | CRM_Utils_System::loadBootStrap($bootStrapParams, TRUE, TRUE, $realPath); | |
325 | ||
326 | global $user; | |
327 | if ($user) { | |
328 | $userUid = $user->uid; | |
329 | $userMail = $user->mail; | |
330 | } | |
331 | } | |
332 | else { | |
333 | // CRM-8638 | |
84da17e3 | 334 | // SOAP cannot load Backdrop bootstrap and hence we do it the old way |
84fce21c | 335 | // Contact CiviSMTP folks if we run into issues with this :) |
ddd5c06f | 336 | $cmsPath = $this->cmsRootPath(); |
1cee22f0 | 337 | if (!defined('BACKDROP_ROOT')) { |
338 | define(BACKDROP_ROOT, $cmsPath); | |
339 | } | |
5c6ebf4c TO |
340 | require_once "$cmsPath/core/includes/bootstrap.inc"; |
341 | require_once "$cmsPath/core/includes/password.inc"; | |
84fce21c TO |
342 | |
343 | $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; | |
ddd5c06f | 344 | $name = $dbBackdrop->escapeSimple($strtolower($name)); |
84fce21c | 345 | $userFrameworkUsersTableName = $this->getUsersTableName(); |
b27d1855 | 346 | |
347 | // LOWER in query below roughly translates to 'hurt my database without deriving any benefit' See CRM-19811. | |
84fce21c TO |
348 | $sql = " |
349 | SELECT u.* | |
350 | FROM {$userFrameworkUsersTableName} u | |
351 | WHERE LOWER(u.name) = '$name' | |
352 | AND u.status = 1 | |
353 | "; | |
354 | ||
ddd5c06f | 355 | $query = $dbBackdrop->query($sql); |
84fce21c TO |
356 | $row = $query->fetchRow(DB_FETCHMODE_ASSOC); |
357 | ||
358 | if ($row) { | |
ddd5c06f NH |
359 | $fakeAccount = backdrop_anonymous_user(); |
360 | $fakeAccount->name = $name; | |
361 | $fakeAccount->pass = $row['pass']; | |
362 | $passwordCheck = user_check_password($password, $fakeAccount); | |
84fce21c TO |
363 | if ($passwordCheck) { |
364 | $userUid = $row['uid']; | |
365 | $userMail = $row['mail']; | |
366 | } | |
367 | } | |
368 | } | |
369 | ||
370 | if ($userUid && $userMail) { | |
ddd5c06f | 371 | CRM_Core_BAO_UFMatch::synchronizeUFMatch($account, $userUid, $userMail, 'Backdrop'); |
84fce21c TO |
372 | $contactID = CRM_Core_BAO_UFMatch::getContactId($userUid); |
373 | if (!$contactID) { | |
374 | return FALSE; | |
375 | } | |
376 | return array($contactID, $userUid, mt_rand()); | |
377 | } | |
378 | return FALSE; | |
379 | } | |
380 | ||
381 | /** | |
382 | * @inheritDoc | |
383 | */ | |
384 | public function loadUser($username) { | |
385 | global $user; | |
386 | ||
387 | $user = user_load_by_name($username); | |
388 | ||
389 | if (empty($user->uid)) { | |
390 | return FALSE; | |
391 | } | |
392 | ||
393 | $uid = $user->uid; | |
394 | $contact_id = CRM_Core_BAO_UFMatch::getContactId($uid); | |
395 | ||
396 | // lets store contact id and user id in session | |
397 | $session = CRM_Core_Session::singleton(); | |
398 | $session->set('ufID', $uid); | |
399 | $session->set('userID', $contact_id); | |
400 | return TRUE; | |
401 | } | |
402 | ||
403 | /** | |
404 | * Perform any post login activities required by the UF - | |
ddd5c06f NH |
405 | * For Backdrop this could mean recording a watchdog message about the new |
406 | * session, saving the login timestamp, calling hook_user_login(), etc. | |
84fce21c TO |
407 | * |
408 | * @param array $params | |
ddd5c06f | 409 | * The array of form values submitted by the user. |
84fce21c TO |
410 | */ |
411 | public function userLoginFinalize($params = array()) { | |
412 | user_login_finalize($params); | |
413 | } | |
414 | ||
974c6b58 HD |
415 | /** |
416 | * @inheritDoc | |
417 | */ | |
418 | public function getUFLocale() { | |
84da17e3 | 419 | // return CiviCRM’s xx_YY locale that either matches Backdrop’s Chinese locale |
420 | // (for CRM-6281), Backdrop’s xx_YY or is retrieved based on Backdrop’s xx | |
974c6b58 HD |
421 | // sometimes for CLI based on order called, this might not be set and/or empty |
422 | global $language; | |
423 | ||
424 | if (empty($language)) { | |
425 | return NULL; | |
426 | } | |
427 | ||
428 | if ($language->langcode == 'zh-hans') { | |
429 | return 'zh_CN'; | |
430 | } | |
431 | ||
432 | if ($language->langcode == 'zh-hant') { | |
433 | return 'zh_TW'; | |
434 | } | |
435 | ||
436 | if (preg_match('/^.._..$/', $language->langcode)) { | |
437 | return $language->langcode; | |
438 | } | |
439 | ||
440 | return CRM_Core_I18n_PseudoConstant::longForShort(substr($language->langcode, 0, 2)); | |
441 | } | |
442 | ||
443 | /** | |
444 | * @inheritDoc | |
445 | */ | |
446 | public function setUFLocale($civicrm_language) { | |
447 | global $language; | |
448 | ||
449 | $langcode = substr($civicrm_language, 0, 2); | |
450 | $languages = language_list(FALSE, TRUE); | |
451 | ||
452 | if (isset($languages[$langcode])) { | |
453 | $language = $languages[$langcode]; | |
454 | ||
455 | // Config must be re-initialized to reset the base URL | |
456 | // otherwise links will have the wrong language prefix/domain. | |
457 | $config = CRM_Core_Config::singleton(); | |
458 | $config->free(); | |
459 | ||
460 | return TRUE; | |
461 | } | |
462 | ||
463 | return FALSE; | |
464 | } | |
465 | ||
84fce21c TO |
466 | /** |
467 | * Determine the native ID of the CMS user. | |
468 | * | |
469 | * @param string $username | |
470 | * @return int|NULL | |
471 | */ | |
472 | public function getUfId($username) { | |
473 | $user = user_load_by_name($username); | |
474 | if (empty($user->uid)) { | |
475 | return NULL; | |
476 | } | |
477 | return $user->uid; | |
478 | } | |
479 | ||
480 | /** | |
481 | * @inheritDoc | |
482 | */ | |
483 | public function logout() { | |
484 | module_load_include('inc', 'user', 'user.pages'); | |
ddd5c06f | 485 | user_logout(); |
84fce21c TO |
486 | } |
487 | ||
488 | /** | |
489 | * Get the default location for CiviCRM blocks. | |
490 | * | |
491 | * @return string | |
492 | */ | |
493 | public function getDefaultBlockLocation() { | |
494 | return 'sidebar_first'; | |
495 | } | |
496 | ||
497 | /** | |
ddd5c06f | 498 | * Load Backdrop bootstrap. |
84fce21c TO |
499 | * |
500 | * @param array $params | |
501 | * Either uid, or name & pass. | |
502 | * @param bool $loadUser | |
503 | * Boolean Require CMS user load. | |
504 | * @param bool $throwError | |
505 | * If true, print error on failure and exit. | |
506 | * @param bool|string $realPath path to script | |
507 | * | |
508 | * @return bool | |
509 | */ | |
510 | public function loadBootStrap($params = array(), $loadUser = TRUE, $throwError = TRUE, $realPath = NULL) { | |
84fce21c TO |
511 | $cmsPath = $this->cmsRootPath($realPath); |
512 | ||
5c6ebf4c | 513 | if (!file_exists("$cmsPath/core/includes/bootstrap.inc")) { |
84fce21c TO |
514 | if ($throwError) { |
515 | echo '<br />Sorry, could not locate bootstrap.inc\n'; | |
516 | exit(); | |
517 | } | |
518 | return FALSE; | |
519 | } | |
84da17e3 | 520 | // load Backdrop bootstrap |
84fce21c | 521 | chdir($cmsPath); |
5c6ebf4c | 522 | define('BACKDROP_ROOT', $cmsPath); |
84fce21c | 523 | |
84da17e3 | 524 | // For Backdrop multi-site CRM-11313 |
84fce21c TO |
525 | if ($realPath && strpos($realPath, 'sites/all/modules/') === FALSE) { |
526 | preg_match('@sites/([^/]*)/modules@s', $realPath, $matches); | |
527 | if (!empty($matches[1])) { | |
528 | $_SERVER['HTTP_HOST'] = $matches[1]; | |
529 | } | |
530 | } | |
ddd5c06f | 531 | require_once "$cmsPath/core/includes/bootstrap.inc"; |
a620adeb | 532 | require_once "$cmsPath/core/includes/config.inc"; |
ddd5c06f | 533 | backdrop_bootstrap(BACKDROP_BOOTSTRAP_FULL); |
84fce21c | 534 | |
ddd5c06f NH |
535 | // Explicitly setting error reporting, since we cannot handle Backdrop |
536 | // related notices. | |
84fce21c TO |
537 | error_reporting(1); |
538 | if (!function_exists('module_exists') || !module_exists('civicrm')) { | |
539 | if ($throwError) { | |
ddd5c06f | 540 | echo '<br />Sorry, could not load Backdrop bootstrap.'; |
84fce21c TO |
541 | exit(); |
542 | } | |
543 | return FALSE; | |
544 | } | |
545 | ||
ddd5c06f | 546 | // Backdrop successfully bootstrapped. |
84fce21c TO |
547 | $config = CRM_Core_Config::singleton(); |
548 | ||
549 | // lets also fix the clean url setting | |
550 | // CRM-6948 | |
ddd5c06f | 551 | $config->cleanURL = (int) config_get('system.core', 'clean_url'); |
84fce21c TO |
552 | |
553 | // we need to call the config hook again, since we now know | |
554 | // all the modules that are listening on it, does not apply | |
555 | // to J! and WP as yet | |
556 | // CRM-8655 | |
557 | CRM_Utils_Hook::config($config); | |
558 | ||
559 | if (!$loadUser) { | |
560 | return TRUE; | |
561 | } | |
562 | ||
563 | $uid = CRM_Utils_Array::value('uid', $params); | |
564 | if (!$uid) { | |
ddd5c06f | 565 | // Load the user we need to check Backdrop permissions. |
84fce21c TO |
566 | $name = CRM_Utils_Array::value('name', $params, FALSE) ? $params['name'] : trim(CRM_Utils_Array::value('name', $_REQUEST)); |
567 | $pass = CRM_Utils_Array::value('pass', $params, FALSE) ? $params['pass'] : trim(CRM_Utils_Array::value('pass', $_REQUEST)); | |
568 | ||
569 | if ($name) { | |
570 | $uid = user_authenticate($name, $pass); | |
571 | if (!$uid) { | |
572 | if ($throwError) { | |
573 | echo '<br />Sorry, unrecognized username or password.'; | |
574 | exit(); | |
575 | } | |
576 | return FALSE; | |
577 | } | |
578 | } | |
579 | } | |
580 | ||
581 | if ($uid) { | |
582 | $account = user_load($uid); | |
583 | if ($account && $account->uid) { | |
584 | global $user; | |
585 | $user = $account; | |
586 | return TRUE; | |
587 | } | |
588 | } | |
589 | ||
590 | if ($throwError) { | |
591 | echo '<br />Sorry, can not load CMS user account.'; | |
592 | exit(); | |
593 | } | |
594 | ||
595 | // CRM-6948: When using loadBootStrap, it's implicit that CiviCRM has already loaded its settings | |
596 | // which means that define(CIVICRM_CLEANURL) was correctly set. | |
597 | // So we correct it | |
598 | $config = CRM_Core_Config::singleton(); | |
ddd5c06f | 599 | $config->cleanURL = (int) config_get('system.core', 'clean_url'); |
84fce21c | 600 | |
ddd5c06f NH |
601 | // CRM-8655: Backdrop wasn't available during bootstrap, so |
602 | // hook_civicrm_config() never executes. | |
84fce21c TO |
603 | CRM_Utils_Hook::config($config); |
604 | ||
605 | return FALSE; | |
606 | } | |
607 | ||
608 | /** | |
ddd5c06f | 609 | * @inheritDoc |
84fce21c TO |
610 | */ |
611 | public function cmsRootPath($scriptFilename = NULL) { | |
a93a0366 TO |
612 | global $civicrm_paths; |
613 | if (!empty($civicrm_paths['cms.root']['path'])) { | |
614 | return $civicrm_paths['cms.root']['path']; | |
615 | } | |
616 | ||
ddd5c06f NH |
617 | $cmsRoot = NULL; |
618 | $valid = NULL; | |
84fce21c TO |
619 | |
620 | if (!is_null($scriptFilename)) { | |
621 | $path = $scriptFilename; | |
622 | } | |
623 | else { | |
624 | $path = $_SERVER['SCRIPT_FILENAME']; | |
625 | } | |
626 | ||
84fce21c TO |
627 | // CRM-7582 |
628 | $pathVars = explode('/', | |
629 | str_replace('//', '/', | |
630 | str_replace('\\', '/', $path) | |
631 | ) | |
632 | ); | |
633 | ||
ddd5c06f | 634 | // Keep the first directory name for later. |
84fce21c TO |
635 | $firstVar = array_shift($pathVars); |
636 | ||
ddd5c06f | 637 | // Remove script name to reduce one iteration. |
84fce21c TO |
638 | array_pop($pathVars); |
639 | ||
640 | // CRM-7429 -- do check for uppermost 'includes' dir, which would | |
641 | // work for multisite installation. | |
642 | do { | |
643 | $cmsRoot = $firstVar . '/' . implode('/', $pathVars); | |
5c6ebf4c TO |
644 | // Stop if we find backdrop signature file. |
645 | if (file_exists("$cmsRoot/core/misc/backdrop.js")) { | |
84fce21c TO |
646 | $valid = TRUE; |
647 | break; | |
648 | } | |
ddd5c06f | 649 | // Remove one directory level. |
84fce21c TO |
650 | array_pop($pathVars); |
651 | } while (count($pathVars)); | |
652 | ||
653 | return ($valid) ? $cmsRoot : NULL; | |
654 | } | |
655 | ||
656 | /** | |
657 | * @inheritDoc | |
658 | */ | |
659 | public function isUserLoggedIn() { | |
660 | $isloggedIn = FALSE; | |
661 | if (function_exists('user_is_logged_in')) { | |
662 | $isloggedIn = user_is_logged_in(); | |
663 | } | |
664 | ||
665 | return $isloggedIn; | |
666 | } | |
667 | ||
668 | /** | |
669 | * @inheritDoc | |
670 | */ | |
671 | public function getLoggedInUfID() { | |
672 | $ufID = NULL; | |
673 | if (function_exists('user_is_logged_in') && | |
674 | user_is_logged_in() && | |
675 | function_exists('user_uid_optional_to_arg') | |
676 | ) { | |
677 | $ufID = user_uid_optional_to_arg(array()); | |
678 | } | |
679 | ||
680 | return $ufID; | |
681 | } | |
682 | ||
683 | /** | |
684 | * @inheritDoc | |
685 | */ | |
686 | public function languageNegotiationURL($url, $addLanguagePart = TRUE, $removeLanguagePart = FALSE) { | |
687 | if (empty($url)) { | |
688 | return $url; | |
689 | } | |
690 | ||
ddd5c06f | 691 | if (function_exists('config_get') && |
84fce21c TO |
692 | module_exists('locale') && |
693 | function_exists('language_negotiation_get') | |
694 | ) { | |
695 | global $language; | |
696 | ||
ddd5c06f | 697 | // Check if language support from the URL (Path prefix or domain) is set. |
84fce21c | 698 | if (language_negotiation_get('language') == 'locale-url') { |
ddd5c06f | 699 | $urlType = config_get('locale.settings', 'locale_language_negotiation_url_part'); |
84fce21c | 700 | |
ddd5c06f NH |
701 | // URL prefix negotiation. |
702 | if ($urlType == LANGUAGE_NEGOTIATION_URL_PREFIX) { | |
84fce21c TO |
703 | if (isset($language->prefix) && $language->prefix) { |
704 | if ($addLanguagePart) { | |
705 | $url .= $language->prefix . '/'; | |
706 | } | |
707 | if ($removeLanguagePart) { | |
708 | $url = str_replace("/{$language->prefix}/", '/', $url); | |
709 | } | |
710 | } | |
711 | } | |
ddd5c06f NH |
712 | // Domain negotiation. |
713 | if ($urlType == LANGUAGE_NEGOTIATION_URL_DOMAIN) { | |
84fce21c TO |
714 | if (isset($language->domain) && $language->domain) { |
715 | if ($addLanguagePart) { | |
716 | $cleanedUrl = preg_replace('#^https?://#', '', $language->domain); | |
ddd5c06f NH |
717 | // Backdrop function base_path() adds a "/" to the beginning and |
718 | // end of the returned path. | |
84fce21c TO |
719 | if (substr($cleanedUrl, -1) == '/') { |
720 | $cleanedUrl = substr($cleanedUrl, 0, -1); | |
721 | } | |
722 | $url = (CRM_Utils_System::isSSL() ? 'https' : 'http') . '://' . $cleanedUrl . base_path(); | |
723 | } | |
724 | if ($removeLanguagePart && defined('CIVICRM_UF_BASEURL')) { | |
725 | $url = str_replace('\\', '/', $url); | |
726 | $parseUrl = parse_url($url); | |
727 | ||
728 | //kinda hackish but not sure how to do it right | |
729 | //hope http_build_url() will help at some point. | |
730 | if (is_array($parseUrl) && !empty($parseUrl)) { | |
731 | $urlParts = explode('/', $url); | |
732 | $hostKey = array_search($parseUrl['host'], $urlParts); | |
733 | $ufUrlParts = parse_url(CIVICRM_UF_BASEURL); | |
734 | $urlParts[$hostKey] = $ufUrlParts['host']; | |
735 | $url = implode('/', $urlParts); | |
736 | } | |
737 | } | |
738 | } | |
739 | } | |
740 | } | |
741 | } | |
742 | ||
743 | return $url; | |
744 | } | |
745 | ||
746 | /** | |
747 | * Find any users/roles/security-principals with the given permission | |
748 | * and replace it with one or more permissions. | |
749 | * | |
750 | * @param string $oldPerm | |
751 | * @param array $newPerms | |
752 | * Array, strings. | |
753 | */ | |
754 | public function replacePermission($oldPerm, $newPerms) { | |
755 | $roles = user_roles(FALSE, $oldPerm); | |
756 | if (!empty($roles)) { | |
757 | foreach (array_keys($roles) as $rid) { | |
758 | user_role_revoke_permissions($rid, array($oldPerm)); | |
759 | user_role_grant_permissions($rid, $newPerms); | |
760 | } | |
761 | } | |
762 | } | |
763 | ||
764 | /** | |
765 | * Wrapper for og_membership creation. | |
766 | * | |
767 | * @param int $ogID | |
768 | * Organic Group ID. | |
ddd5c06f NH |
769 | * @param int $userID |
770 | * Backdrop User ID. | |
84fce21c | 771 | */ |
ddd5c06f | 772 | public function og_membership_create($ogID, $userID) { |
84fce21c TO |
773 | if (function_exists('og_entity_query_alter')) { |
774 | // sort-of-randomly chose a function that only exists in the // 7.x-2.x branch | |
775 | // | |
776 | // @TODO Find more solid way to check - try system_get_info('module', 'og'). | |
777 | // | |
778 | // Also, since we don't know how to get the entity type of the // group, we'll assume it's 'node' | |
ddd5c06f | 779 | og_group('node', $ogID, array('entity' => user_load($userID))); |
84fce21c TO |
780 | } |
781 | else { | |
782 | // Works for the OG 7.x-1.x branch | |
ddd5c06f | 783 | og_group($ogID, array('entity' => user_load($userID))); |
84fce21c TO |
784 | } |
785 | } | |
786 | ||
787 | /** | |
788 | * Wrapper for og_membership deletion. | |
789 | * | |
790 | * @param int $ogID | |
791 | * Organic Group ID. | |
ddd5c06f NH |
792 | * @param int $userID |
793 | * Backdrop User ID. | |
84fce21c | 794 | */ |
ddd5c06f | 795 | public function og_membership_delete($ogID, $userID) { |
84fce21c TO |
796 | if (function_exists('og_entity_query_alter')) { |
797 | // sort-of-randomly chose a function that only exists in the 7.x-2.x branch | |
798 | // TODO: Find a more solid way to make this test | |
799 | // Also, since we don't know how to get the entity type of the group, we'll assume it's 'node' | |
ddd5c06f | 800 | og_ungroup('node', $ogID, 'user', user_load($userID)); |
84fce21c TO |
801 | } |
802 | else { | |
803 | // Works for the OG 7.x-1.x branch | |
ddd5c06f | 804 | og_ungroup($ogID, 'user', user_load($userID)); |
84fce21c TO |
805 | } |
806 | } | |
807 | ||
808 | /** | |
809 | * @inheritDoc | |
810 | */ | |
811 | public function getTimeZoneString() { | |
812 | global $user; | |
813 | // Note that 0 is a valid timezone (GMT) so we use strlen not empty to check. | |
ddd5c06f | 814 | if (config_get('system.date', 'user_configurable_timezones') && $user->uid && isset($user->timezone) && strlen($user->timezone)) { |
84fce21c TO |
815 | $timezone = $user->timezone; |
816 | } | |
817 | else { | |
ddd5c06f | 818 | $timezone = config_get('system.date', 'default_timezone'); |
84fce21c TO |
819 | } |
820 | if (!$timezone) { | |
821 | $timezone = parent::getTimeZoneString(); | |
822 | } | |
823 | return $timezone; | |
824 | } | |
825 | ||
826 | /** | |
827 | * @inheritDoc | |
828 | */ | |
829 | public function setHttpHeader($name, $value) { | |
ddd5c06f | 830 | backdrop_add_http_header($name, $value); |
84fce21c TO |
831 | } |
832 | ||
833 | /** | |
834 | * @inheritDoc | |
835 | */ | |
836 | public function synchronizeUsers() { | |
837 | $config = CRM_Core_Config::singleton(); | |
838 | if (PHP_SAPI != 'cli') { | |
839 | set_time_limit(300); | |
840 | } | |
841 | $id = 'uid'; | |
842 | $mail = 'mail'; | |
843 | $name = 'name'; | |
844 | ||
845 | $result = db_query("SELECT uid, mail, name FROM {users} where mail != ''"); | |
846 | ||
847 | $user = new StdClass(); | |
848 | $uf = $config->userFramework; | |
849 | $contactCount = 0; | |
850 | $contactCreated = 0; | |
851 | $contactMatching = 0; | |
852 | foreach ($result as $row) { | |
853 | $user->$id = $row->$id; | |
854 | $user->$mail = $row->$mail; | |
855 | $user->$name = $row->$name; | |
856 | $contactCount++; | |
857 | if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, $row->$id, $row->$mail, $uf, 1, 'Individual', TRUE)) { | |
858 | $contactCreated++; | |
859 | } | |
860 | else { | |
861 | $contactMatching++; | |
862 | } | |
863 | if (is_object($match)) { | |
864 | $match->free(); | |
865 | } | |
866 | } | |
867 | ||
868 | return array( | |
869 | 'contactCount' => $contactCount, | |
870 | 'contactMatching' => $contactMatching, | |
871 | 'contactCreated' => $contactCreated, | |
872 | ); | |
873 | } | |
874 | ||
ec95d32c TO |
875 | /** |
876 | * @inheritDoc | |
877 | */ | |
878 | public function clearResourceCache() { | |
879 | _backdrop_flush_css_js(); | |
880 | } | |
5c6ebf4c TO |
881 | |
882 | /** | |
883 | * Get all the contact emails for users that have a specific permission. | |
884 | * | |
885 | * @param string $permissionName | |
886 | * Name of the permission we are interested in. | |
887 | * | |
888 | * @return string | |
889 | * a comma separated list of email addresses | |
890 | */ | |
891 | public function permissionEmails($permissionName) { | |
892 | // FIXME!!!! | |
893 | return array(); | |
894 | } | |
895 | ||
84da17e3 | 896 | /** |
897 | * @inheritdoc | |
898 | */ | |
899 | public function getDefaultFileStorage() { | |
900 | $config = CRM_Core_Config::singleton(); | |
901 | $baseURL = CRM_Utils_System::languageNegotiationURL($config->userFrameworkBaseURL, FALSE, TRUE); | |
902 | ||
903 | $siteName = $this->parseBackdropSiteNameFromRequest('/files/civicrm'); | |
904 | if ($siteName) { | |
905 | $filesURL = $baseURL . "sites/$siteName/files/civicrm/"; | |
906 | } | |
907 | else { | |
908 | $filesURL = $baseURL . "files/civicrm/"; | |
909 | } | |
910 | ||
911 | return array( | |
912 | 'url' => $filesURL, | |
913 | 'path' => CRM_Utils_File::baseFilePath(), | |
914 | ); | |
915 | } | |
916 | ||
917 | /** | |
918 | * Check if a resource url is within the Backdrop directory and format appropriately. | |
919 | * | |
920 | * @param $url (reference) | |
921 | * | |
922 | * @return bool | |
923 | * TRUE for internal paths, FALSE for external. The backdrop_add_js fn is able to add js more | |
924 | * efficiently if it is known to be in the Backdrop site | |
925 | */ | |
926 | public function formatResourceUrl(&$url) { | |
927 | $internal = FALSE; | |
928 | $base = CRM_Core_Config::singleton()->resourceBase; | |
929 | global $base_url; | |
930 | // Handle absolute urls | |
931 | // compares $url (which is some unknown/untrusted value from a third-party dev) to the CMS's base url (which is independent of civi's url) | |
932 | // to see if the url is within our Backdrop dir, if it is we are able to treated it as an internal url | |
933 | if (strpos($url, $base_url) === 0) { | |
934 | $file = trim(str_replace($base_url, '', $url), '/'); | |
935 | // CRM-18130: Custom CSS URL not working if aliased or rewritten | |
936 | if (file_exists(BACKDROP_ROOT . $file)) { | |
937 | $url = $file; | |
938 | $internal = TRUE; | |
939 | } | |
940 | } | |
941 | // Handle relative urls that are within the CiviCRM module directory | |
942 | elseif (strpos($url, $base) === 0) { | |
943 | $internal = TRUE; | |
944 | $url = $this->appendCoreDirectoryToResourceBase(dirname(backdrop_get_path('module', 'civicrm')) . '/') . trim(substr($url, strlen($base)), '/'); | |
945 | } | |
946 | // Strip query string | |
947 | $q = strpos($url, '?'); | |
948 | if ($q && $internal) { | |
949 | $url = substr($url, 0, $q); | |
950 | } | |
951 | return $internal; | |
952 | } | |
953 | ||
954 | /** | |
955 | * @inheritDoc | |
956 | */ | |
957 | public function setMessage($message) { | |
958 | backdrop_set_message($message); | |
959 | } | |
960 | ||
961 | /** | |
962 | * @inheritDoc | |
963 | */ | |
964 | public function permissionDenied() { | |
965 | backdrop_access_denied(); | |
966 | } | |
967 | ||
968 | /** | |
969 | * @inheritDoc | |
970 | */ | |
971 | public function flush() { | |
972 | backdrop_flush_all_caches(); | |
973 | } | |
974 | ||
975 | /** | |
976 | * Determine if Backdrop multi-site applies to the current request -- and, | |
977 | * specifically, determine the name of the multisite folder. | |
978 | * | |
979 | * @param string $flagFile | |
980 | * Check if $flagFile exists inside the site dir. | |
981 | * @return null|string | |
982 | * string, e.g. `bar.example.com` if using multisite. | |
983 | * NULL if using the default site. | |
984 | */ | |
985 | private function parseBackdropSiteNameFromRequest($flagFile = '') { | |
986 | $phpSelf = array_key_exists('PHP_SELF', $_SERVER) ? $_SERVER['PHP_SELF'] : ''; | |
987 | $httpHost = array_key_exists('HTTP_HOST', $_SERVER) ? $_SERVER['HTTP_HOST'] : ''; | |
988 | if (empty($httpHost)) { | |
989 | $httpHost = parse_url(CIVICRM_UF_BASEURL, PHP_URL_HOST); | |
990 | if (parse_url(CIVICRM_UF_BASEURL, PHP_URL_PORT)) { | |
991 | $httpHost .= ':' . parse_url(CIVICRM_UF_BASEURL, PHP_URL_PORT); | |
992 | } | |
993 | } | |
994 | ||
995 | $confdir = $this->cmsRootPath() . '/sites'; | |
996 | ||
997 | if (file_exists($confdir . "/sites.php")) { | |
998 | include $confdir . "/sites.php"; | |
999 | } | |
1000 | else { | |
1001 | $sites = array(); | |
1002 | } | |
1003 | ||
1004 | $uri = explode('/', $phpSelf); | |
1005 | $server = explode('.', implode('.', array_reverse(explode(':', rtrim($httpHost, '.'))))); | |
1006 | for ($i = count($uri) - 1; $i > 0; $i--) { | |
1007 | for ($j = count($server); $j > 0; $j--) { | |
1008 | $dir = implode('.', array_slice($server, -$j)) . implode('.', array_slice($uri, 0, $i)); | |
1009 | if (file_exists("$confdir/$dir" . $flagFile)) { | |
1010 | \Civi::$statics[__CLASS__]['drupalSiteName'] = $dir; | |
1011 | return \Civi::$statics[__CLASS__]['drupalSiteName']; | |
1012 | } | |
1013 | // check for alias | |
1014 | if (isset($sites[$dir]) && file_exists("$confdir/{$sites[$dir]}" . $flagFile)) { | |
1015 | \Civi::$statics[__CLASS__]['drupalSiteName'] = $sites[$dir]; | |
1016 | return \Civi::$statics[__CLASS__]['drupalSiteName']; | |
1017 | } | |
1018 | } | |
1019 | } | |
1020 | } | |
1021 | ||
88764dbe | 1022 | /** |
8a09f4aa | 1023 | * Append Backdrop CSS and JS to coreResourcesList. |
88764dbe | 1024 | * |
1025 | * @param array $list | |
1026 | */ | |
1027 | public function appendCoreResources(&$list) { | |
1028 | $list[] = 'css/backdrop.css'; | |
8a09f4aa | 1029 | $list[] = 'js/crm.backdrop.js'; |
88764dbe | 1030 | } |
fb3c1356 | 1031 | |
84fce21c | 1032 | } |