Commit | Line | Data |
---|---|---|
84fce21c TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
84fce21c | 5 | +--------------------------------------------------------------------+ |
f299f7db | 6 | | Copyright CiviCRM LLC (c) 2004-2020 | |
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 | |
f299f7db | 31 | * @copyright CiviCRM LLC (c) 2004-2020 |
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 | ||
be2fb01f | 45 | $form_state['input'] = [ |
84fce21c TO |
46 | 'name' => $params['cms_name'], |
47 | 'mail' => $params[$mail], | |
48 | 'op' => 'Create new account', | |
be2fb01f | 49 | ]; |
84fce21c TO |
50 | |
51 | $admin = user_access('administer users'); | |
ddd5c06f | 52 | if (!config_get('system.core', 'user_email_verification') || $admin) { |
be2fb01f | 53 | $form_state['input']['pass'] = ['pass1' => $params['cms_pass'], 'pass2' => $params['cms_pass']]; |
84fce21c TO |
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'; | |
be2fb01f | 64 | $form_state['build_info']['args'] = []; |
84fce21c TO |
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; | |
be2fb01f | 79 | $form['#array_parents'] = []; |
84fce21c | 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 { | |
be2fb01f | 127 | $uid = db_query("SELECT uid FROM {users} WHERE name = :name", [':name' => $params['name']])->fetchField(); |
84fce21c | 128 | if ((bool) $uid) { |
be2fb01f | 129 | $errors['cms_name'] = ts('The username %1 is already taken. Please select another username.', [1 => $params['name']]); |
84fce21c TO |
130 | } |
131 | } | |
132 | } | |
133 | ||
134 | if (!empty($params['mail'])) { | |
ddd5c06f | 135 | if (!valid_email_address($params['mail'])) { |
6dabf459 | 136 | $errors[$emailName] = ts('The e-mail address %1 is not valid.', [1 => $params['mail']]); |
84fce21c TO |
137 | } |
138 | else { | |
be2fb01f | 139 | $uid = db_query("SELECT uid FROM {users} WHERE mail = :mail", [':mail' => $params['mail']])->fetchField(); |
84fce21c | 140 | if ((bool) $uid) { |
ddd5c06f | 141 | $resetUrl = url('user/password'); |
84fce21c | 142 | $errors[$emailName] = ts('The email address %1 already has an account associated with it. <a href="%2">Have you forgotten your password?</a>', |
be2fb01f | 143 | [1 => $params['mail'], 2 => $resetUrl] |
84fce21c TO |
144 | ); |
145 | } | |
146 | } | |
147 | } | |
148 | } | |
149 | ||
150 | /** | |
151 | * @inheritDoc | |
152 | */ | |
153 | public function getLoginURL($destination = '') { | |
be2fb01f | 154 | $query = $destination ? ['destination' => $destination] : []; |
8f76d5aa | 155 | return url('user/login', ['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%%')) { | |
be2fb01f | 180 | $args = ['cid', 'mid']; |
84fce21c TO |
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() { | |
be2fb01f | 200 | $bc = []; |
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; | |
be2fb01f | 211 | $data = [ |
84fce21c TO |
212 | '#type' => 'markup', |
213 | '#markup' => $header, | |
be2fb01f | 214 | ]; |
84da17e3 | 215 | backdrop_add_html_head($data, $key); |
84fce21c TO |
216 | } |
217 | } | |
218 | ||
219 | /** | |
220 | * @inheritDoc | |
221 | */ | |
222 | public function addScriptUrl($url, $region) { | |
be2fb01f | 223 | $params = ['group' => JS_LIBRARY, 'weight' => 10]; |
84fce21c TO |
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) { | |
be2fb01f | 243 | $params = ['type' => 'inline', 'group' => JS_LIBRARY, 'weight' => 10]; |
84fce21c TO |
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 | } | |
be2fb01f | 264 | $params = []; |
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 | } | |
be2fb01f | 278 | $params = ['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)) { | |
309310bf | 312 | throw new CRM_Core_Exception("Cannot connect to Backdrop database via $config->userFrameworkDSN, " . $dbBackdrop->getMessage()); |
84fce21c TO |
313 | } |
314 | ||
315 | $account = $userUid = $userMail = NULL; | |
316 | if ($loadCMSBootstrap) { | |
be2fb01f | 317 | $bootStrapParams = []; |
84fce21c | 318 | if ($name && $password) { |
be2fb01f | 319 | $bootStrapParams = [ |
84fce21c TO |
320 | 'name' => $name, |
321 | 'pass' => $password, | |
be2fb01f | 322 | ]; |
84fce21c TO |
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 | } | |
be2fb01f | 376 | return [$contactID, $userUid, mt_rand()]; |
84fce21c TO |
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 | 410 | */ |
be2fb01f | 411 | public function userLoginFinalize($params = []) { |
84fce21c TO |
412 | user_login_finalize($params); |
413 | } | |
414 | ||
cfb37063 HD |
415 | /** |
416 | * @inheritDoc | |
417 | */ | |
418 | public function isUserRegistrationPermitted() { | |
419 | if (config_get('system.core', 'user_register') == 'admin_only') { | |
420 | return FALSE; | |
421 | } | |
422 | return TRUE; | |
423 | } | |
424 | ||
63df6889 HD |
425 | /** |
426 | * @inheritDoc | |
427 | */ | |
1a6630be | 428 | public function isPasswordUserGenerated() { |
63df6889 HD |
429 | if (config_get('system.core', 'user_email_verification') == TRUE) { |
430 | return FALSE; | |
431 | } | |
432 | return TRUE; | |
433 | } | |
434 | ||
974c6b58 HD |
435 | /** |
436 | * @inheritDoc | |
437 | */ | |
438 | public function getUFLocale() { | |
84da17e3 | 439 | // return CiviCRM’s xx_YY locale that either matches Backdrop’s Chinese locale |
440 | // (for CRM-6281), Backdrop’s xx_YY or is retrieved based on Backdrop’s xx | |
974c6b58 HD |
441 | // sometimes for CLI based on order called, this might not be set and/or empty |
442 | global $language; | |
443 | ||
444 | if (empty($language)) { | |
445 | return NULL; | |
446 | } | |
447 | ||
448 | if ($language->langcode == 'zh-hans') { | |
449 | return 'zh_CN'; | |
450 | } | |
451 | ||
452 | if ($language->langcode == 'zh-hant') { | |
453 | return 'zh_TW'; | |
454 | } | |
455 | ||
456 | if (preg_match('/^.._..$/', $language->langcode)) { | |
457 | return $language->langcode; | |
458 | } | |
459 | ||
460 | return CRM_Core_I18n_PseudoConstant::longForShort(substr($language->langcode, 0, 2)); | |
461 | } | |
462 | ||
463 | /** | |
464 | * @inheritDoc | |
465 | */ | |
466 | public function setUFLocale($civicrm_language) { | |
467 | global $language; | |
468 | ||
469 | $langcode = substr($civicrm_language, 0, 2); | |
470 | $languages = language_list(FALSE, TRUE); | |
471 | ||
472 | if (isset($languages[$langcode])) { | |
473 | $language = $languages[$langcode]; | |
474 | ||
475 | // Config must be re-initialized to reset the base URL | |
476 | // otherwise links will have the wrong language prefix/domain. | |
477 | $config = CRM_Core_Config::singleton(); | |
478 | $config->free(); | |
479 | ||
480 | return TRUE; | |
481 | } | |
482 | ||
483 | return FALSE; | |
484 | } | |
485 | ||
84fce21c TO |
486 | /** |
487 | * Determine the native ID of the CMS user. | |
488 | * | |
489 | * @param string $username | |
e97c66ff | 490 | * @return int|null |
84fce21c TO |
491 | */ |
492 | public function getUfId($username) { | |
493 | $user = user_load_by_name($username); | |
494 | if (empty($user->uid)) { | |
495 | return NULL; | |
496 | } | |
497 | return $user->uid; | |
498 | } | |
499 | ||
500 | /** | |
501 | * @inheritDoc | |
502 | */ | |
503 | public function logout() { | |
504 | module_load_include('inc', 'user', 'user.pages'); | |
ddd5c06f | 505 | user_logout(); |
84fce21c TO |
506 | } |
507 | ||
508 | /** | |
509 | * Get the default location for CiviCRM blocks. | |
510 | * | |
511 | * @return string | |
512 | */ | |
513 | public function getDefaultBlockLocation() { | |
514 | return 'sidebar_first'; | |
515 | } | |
516 | ||
517 | /** | |
ddd5c06f | 518 | * Load Backdrop bootstrap. |
84fce21c TO |
519 | * |
520 | * @param array $params | |
521 | * Either uid, or name & pass. | |
522 | * @param bool $loadUser | |
523 | * Boolean Require CMS user load. | |
524 | * @param bool $throwError | |
525 | * If true, print error on failure and exit. | |
526 | * @param bool|string $realPath path to script | |
527 | * | |
528 | * @return bool | |
529 | */ | |
be2fb01f | 530 | public function loadBootStrap($params = [], $loadUser = TRUE, $throwError = TRUE, $realPath = NULL) { |
84fce21c TO |
531 | $cmsPath = $this->cmsRootPath($realPath); |
532 | ||
5c6ebf4c | 533 | if (!file_exists("$cmsPath/core/includes/bootstrap.inc")) { |
84fce21c TO |
534 | if ($throwError) { |
535 | echo '<br />Sorry, could not locate bootstrap.inc\n'; | |
536 | exit(); | |
537 | } | |
538 | return FALSE; | |
539 | } | |
84da17e3 | 540 | // load Backdrop bootstrap |
84fce21c | 541 | chdir($cmsPath); |
5c6ebf4c | 542 | define('BACKDROP_ROOT', $cmsPath); |
84fce21c | 543 | |
84da17e3 | 544 | // For Backdrop multi-site CRM-11313 |
84fce21c TO |
545 | if ($realPath && strpos($realPath, 'sites/all/modules/') === FALSE) { |
546 | preg_match('@sites/([^/]*)/modules@s', $realPath, $matches); | |
547 | if (!empty($matches[1])) { | |
548 | $_SERVER['HTTP_HOST'] = $matches[1]; | |
549 | } | |
550 | } | |
ddd5c06f | 551 | require_once "$cmsPath/core/includes/bootstrap.inc"; |
a620adeb | 552 | require_once "$cmsPath/core/includes/config.inc"; |
ddd5c06f | 553 | backdrop_bootstrap(BACKDROP_BOOTSTRAP_FULL); |
84fce21c | 554 | |
ddd5c06f NH |
555 | // Explicitly setting error reporting, since we cannot handle Backdrop |
556 | // related notices. | |
84fce21c TO |
557 | error_reporting(1); |
558 | if (!function_exists('module_exists') || !module_exists('civicrm')) { | |
559 | if ($throwError) { | |
ddd5c06f | 560 | echo '<br />Sorry, could not load Backdrop bootstrap.'; |
84fce21c TO |
561 | exit(); |
562 | } | |
563 | return FALSE; | |
564 | } | |
565 | ||
ddd5c06f | 566 | // Backdrop successfully bootstrapped. |
84fce21c TO |
567 | $config = CRM_Core_Config::singleton(); |
568 | ||
569 | // lets also fix the clean url setting | |
570 | // CRM-6948 | |
ddd5c06f | 571 | $config->cleanURL = (int) config_get('system.core', 'clean_url'); |
84fce21c TO |
572 | |
573 | // we need to call the config hook again, since we now know | |
574 | // all the modules that are listening on it, does not apply | |
575 | // to J! and WP as yet | |
576 | // CRM-8655 | |
577 | CRM_Utils_Hook::config($config); | |
578 | ||
579 | if (!$loadUser) { | |
580 | return TRUE; | |
581 | } | |
582 | ||
583 | $uid = CRM_Utils_Array::value('uid', $params); | |
584 | if (!$uid) { | |
ddd5c06f | 585 | // Load the user we need to check Backdrop permissions. |
84fce21c TO |
586 | $name = CRM_Utils_Array::value('name', $params, FALSE) ? $params['name'] : trim(CRM_Utils_Array::value('name', $_REQUEST)); |
587 | $pass = CRM_Utils_Array::value('pass', $params, FALSE) ? $params['pass'] : trim(CRM_Utils_Array::value('pass', $_REQUEST)); | |
588 | ||
589 | if ($name) { | |
590 | $uid = user_authenticate($name, $pass); | |
591 | if (!$uid) { | |
592 | if ($throwError) { | |
593 | echo '<br />Sorry, unrecognized username or password.'; | |
594 | exit(); | |
595 | } | |
596 | return FALSE; | |
597 | } | |
598 | } | |
599 | } | |
600 | ||
601 | if ($uid) { | |
602 | $account = user_load($uid); | |
603 | if ($account && $account->uid) { | |
604 | global $user; | |
605 | $user = $account; | |
606 | return TRUE; | |
607 | } | |
608 | } | |
609 | ||
610 | if ($throwError) { | |
611 | echo '<br />Sorry, can not load CMS user account.'; | |
612 | exit(); | |
613 | } | |
614 | ||
615 | // CRM-6948: When using loadBootStrap, it's implicit that CiviCRM has already loaded its settings | |
616 | // which means that define(CIVICRM_CLEANURL) was correctly set. | |
617 | // So we correct it | |
618 | $config = CRM_Core_Config::singleton(); | |
ddd5c06f | 619 | $config->cleanURL = (int) config_get('system.core', 'clean_url'); |
84fce21c | 620 | |
ddd5c06f NH |
621 | // CRM-8655: Backdrop wasn't available during bootstrap, so |
622 | // hook_civicrm_config() never executes. | |
84fce21c TO |
623 | CRM_Utils_Hook::config($config); |
624 | ||
625 | return FALSE; | |
626 | } | |
627 | ||
628 | /** | |
ddd5c06f | 629 | * @inheritDoc |
84fce21c TO |
630 | */ |
631 | public function cmsRootPath($scriptFilename = NULL) { | |
a93a0366 TO |
632 | global $civicrm_paths; |
633 | if (!empty($civicrm_paths['cms.root']['path'])) { | |
634 | return $civicrm_paths['cms.root']['path']; | |
635 | } | |
636 | ||
ddd5c06f NH |
637 | $cmsRoot = NULL; |
638 | $valid = NULL; | |
84fce21c TO |
639 | |
640 | if (!is_null($scriptFilename)) { | |
641 | $path = $scriptFilename; | |
642 | } | |
643 | else { | |
644 | $path = $_SERVER['SCRIPT_FILENAME']; | |
645 | } | |
646 | ||
84fce21c TO |
647 | // CRM-7582 |
648 | $pathVars = explode('/', | |
649 | str_replace('//', '/', | |
650 | str_replace('\\', '/', $path) | |
651 | ) | |
652 | ); | |
653 | ||
ddd5c06f | 654 | // Keep the first directory name for later. |
84fce21c TO |
655 | $firstVar = array_shift($pathVars); |
656 | ||
ddd5c06f | 657 | // Remove script name to reduce one iteration. |
84fce21c TO |
658 | array_pop($pathVars); |
659 | ||
660 | // CRM-7429 -- do check for uppermost 'includes' dir, which would | |
661 | // work for multisite installation. | |
662 | do { | |
663 | $cmsRoot = $firstVar . '/' . implode('/', $pathVars); | |
5c6ebf4c TO |
664 | // Stop if we find backdrop signature file. |
665 | if (file_exists("$cmsRoot/core/misc/backdrop.js")) { | |
84fce21c TO |
666 | $valid = TRUE; |
667 | break; | |
668 | } | |
ddd5c06f | 669 | // Remove one directory level. |
84fce21c TO |
670 | array_pop($pathVars); |
671 | } while (count($pathVars)); | |
672 | ||
673 | return ($valid) ? $cmsRoot : NULL; | |
674 | } | |
675 | ||
676 | /** | |
677 | * @inheritDoc | |
678 | */ | |
679 | public function isUserLoggedIn() { | |
680 | $isloggedIn = FALSE; | |
681 | if (function_exists('user_is_logged_in')) { | |
682 | $isloggedIn = user_is_logged_in(); | |
683 | } | |
684 | ||
685 | return $isloggedIn; | |
686 | } | |
687 | ||
688 | /** | |
689 | * @inheritDoc | |
690 | */ | |
691 | public function getLoggedInUfID() { | |
692 | $ufID = NULL; | |
693 | if (function_exists('user_is_logged_in') && | |
694 | user_is_logged_in() && | |
695 | function_exists('user_uid_optional_to_arg') | |
696 | ) { | |
be2fb01f | 697 | $ufID = user_uid_optional_to_arg([]); |
84fce21c TO |
698 | } |
699 | ||
700 | return $ufID; | |
701 | } | |
702 | ||
703 | /** | |
704 | * @inheritDoc | |
705 | */ | |
706 | public function languageNegotiationURL($url, $addLanguagePart = TRUE, $removeLanguagePart = FALSE) { | |
707 | if (empty($url)) { | |
708 | return $url; | |
709 | } | |
710 | ||
ddd5c06f | 711 | if (function_exists('config_get') && |
84fce21c TO |
712 | module_exists('locale') && |
713 | function_exists('language_negotiation_get') | |
714 | ) { | |
715 | global $language; | |
716 | ||
ddd5c06f | 717 | // Check if language support from the URL (Path prefix or domain) is set. |
84fce21c | 718 | if (language_negotiation_get('language') == 'locale-url') { |
ddd5c06f | 719 | $urlType = config_get('locale.settings', 'locale_language_negotiation_url_part'); |
84fce21c | 720 | |
ddd5c06f NH |
721 | // URL prefix negotiation. |
722 | if ($urlType == LANGUAGE_NEGOTIATION_URL_PREFIX) { | |
84fce21c TO |
723 | if (isset($language->prefix) && $language->prefix) { |
724 | if ($addLanguagePart) { | |
725 | $url .= $language->prefix . '/'; | |
726 | } | |
727 | if ($removeLanguagePart) { | |
728 | $url = str_replace("/{$language->prefix}/", '/', $url); | |
729 | } | |
730 | } | |
731 | } | |
ddd5c06f NH |
732 | // Domain negotiation. |
733 | if ($urlType == LANGUAGE_NEGOTIATION_URL_DOMAIN) { | |
84fce21c TO |
734 | if (isset($language->domain) && $language->domain) { |
735 | if ($addLanguagePart) { | |
736 | $cleanedUrl = preg_replace('#^https?://#', '', $language->domain); | |
ddd5c06f NH |
737 | // Backdrop function base_path() adds a "/" to the beginning and |
738 | // end of the returned path. | |
84fce21c TO |
739 | if (substr($cleanedUrl, -1) == '/') { |
740 | $cleanedUrl = substr($cleanedUrl, 0, -1); | |
741 | } | |
742 | $url = (CRM_Utils_System::isSSL() ? 'https' : 'http') . '://' . $cleanedUrl . base_path(); | |
743 | } | |
744 | if ($removeLanguagePart && defined('CIVICRM_UF_BASEURL')) { | |
745 | $url = str_replace('\\', '/', $url); | |
746 | $parseUrl = parse_url($url); | |
747 | ||
748 | //kinda hackish but not sure how to do it right | |
749 | //hope http_build_url() will help at some point. | |
750 | if (is_array($parseUrl) && !empty($parseUrl)) { | |
751 | $urlParts = explode('/', $url); | |
752 | $hostKey = array_search($parseUrl['host'], $urlParts); | |
753 | $ufUrlParts = parse_url(CIVICRM_UF_BASEURL); | |
754 | $urlParts[$hostKey] = $ufUrlParts['host']; | |
755 | $url = implode('/', $urlParts); | |
756 | } | |
757 | } | |
758 | } | |
759 | } | |
760 | } | |
761 | } | |
762 | ||
763 | return $url; | |
764 | } | |
765 | ||
766 | /** | |
767 | * Find any users/roles/security-principals with the given permission | |
768 | * and replace it with one or more permissions. | |
769 | * | |
770 | * @param string $oldPerm | |
771 | * @param array $newPerms | |
772 | * Array, strings. | |
773 | */ | |
774 | public function replacePermission($oldPerm, $newPerms) { | |
775 | $roles = user_roles(FALSE, $oldPerm); | |
776 | if (!empty($roles)) { | |
777 | foreach (array_keys($roles) as $rid) { | |
be2fb01f | 778 | user_role_revoke_permissions($rid, [$oldPerm]); |
84fce21c TO |
779 | user_role_grant_permissions($rid, $newPerms); |
780 | } | |
781 | } | |
782 | } | |
783 | ||
784 | /** | |
785 | * Wrapper for og_membership creation. | |
786 | * | |
787 | * @param int $ogID | |
788 | * Organic Group ID. | |
ddd5c06f NH |
789 | * @param int $userID |
790 | * Backdrop User ID. | |
84fce21c | 791 | */ |
ddd5c06f | 792 | public function og_membership_create($ogID, $userID) { |
84fce21c TO |
793 | if (function_exists('og_entity_query_alter')) { |
794 | // sort-of-randomly chose a function that only exists in the // 7.x-2.x branch | |
795 | // | |
796 | // @TODO Find more solid way to check - try system_get_info('module', 'og'). | |
797 | // | |
798 | // Also, since we don't know how to get the entity type of the // group, we'll assume it's 'node' | |
be2fb01f | 799 | og_group('node', $ogID, ['entity' => user_load($userID)]); |
84fce21c TO |
800 | } |
801 | else { | |
802 | // Works for the OG 7.x-1.x branch | |
be2fb01f | 803 | og_group($ogID, ['entity' => user_load($userID)]); |
84fce21c TO |
804 | } |
805 | } | |
806 | ||
807 | /** | |
808 | * Wrapper for og_membership deletion. | |
809 | * | |
810 | * @param int $ogID | |
811 | * Organic Group ID. | |
ddd5c06f NH |
812 | * @param int $userID |
813 | * Backdrop User ID. | |
84fce21c | 814 | */ |
ddd5c06f | 815 | public function og_membership_delete($ogID, $userID) { |
84fce21c TO |
816 | if (function_exists('og_entity_query_alter')) { |
817 | // sort-of-randomly chose a function that only exists in the 7.x-2.x branch | |
818 | // TODO: Find a more solid way to make this test | |
819 | // Also, since we don't know how to get the entity type of the group, we'll assume it's 'node' | |
ddd5c06f | 820 | og_ungroup('node', $ogID, 'user', user_load($userID)); |
84fce21c TO |
821 | } |
822 | else { | |
823 | // Works for the OG 7.x-1.x branch | |
ddd5c06f | 824 | og_ungroup($ogID, 'user', user_load($userID)); |
84fce21c TO |
825 | } |
826 | } | |
827 | ||
828 | /** | |
829 | * @inheritDoc | |
830 | */ | |
831 | public function getTimeZoneString() { | |
832 | global $user; | |
833 | // Note that 0 is a valid timezone (GMT) so we use strlen not empty to check. | |
ddd5c06f | 834 | if (config_get('system.date', 'user_configurable_timezones') && $user->uid && isset($user->timezone) && strlen($user->timezone)) { |
84fce21c TO |
835 | $timezone = $user->timezone; |
836 | } | |
837 | else { | |
ddd5c06f | 838 | $timezone = config_get('system.date', 'default_timezone'); |
84fce21c TO |
839 | } |
840 | if (!$timezone) { | |
841 | $timezone = parent::getTimeZoneString(); | |
842 | } | |
843 | return $timezone; | |
844 | } | |
845 | ||
846 | /** | |
847 | * @inheritDoc | |
848 | */ | |
849 | public function setHttpHeader($name, $value) { | |
ddd5c06f | 850 | backdrop_add_http_header($name, $value); |
84fce21c TO |
851 | } |
852 | ||
853 | /** | |
854 | * @inheritDoc | |
855 | */ | |
856 | public function synchronizeUsers() { | |
857 | $config = CRM_Core_Config::singleton(); | |
858 | if (PHP_SAPI != 'cli') { | |
859 | set_time_limit(300); | |
860 | } | |
861 | $id = 'uid'; | |
862 | $mail = 'mail'; | |
863 | $name = 'name'; | |
864 | ||
865 | $result = db_query("SELECT uid, mail, name FROM {users} where mail != ''"); | |
866 | ||
867 | $user = new StdClass(); | |
868 | $uf = $config->userFramework; | |
869 | $contactCount = 0; | |
870 | $contactCreated = 0; | |
871 | $contactMatching = 0; | |
872 | foreach ($result as $row) { | |
873 | $user->$id = $row->$id; | |
874 | $user->$mail = $row->$mail; | |
875 | $user->$name = $row->$name; | |
876 | $contactCount++; | |
877 | if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, $row->$id, $row->$mail, $uf, 1, 'Individual', TRUE)) { | |
878 | $contactCreated++; | |
879 | } | |
880 | else { | |
881 | $contactMatching++; | |
882 | } | |
84fce21c TO |
883 | } |
884 | ||
be2fb01f | 885 | return [ |
84fce21c TO |
886 | 'contactCount' => $contactCount, |
887 | 'contactMatching' => $contactMatching, | |
888 | 'contactCreated' => $contactCreated, | |
be2fb01f | 889 | ]; |
84fce21c TO |
890 | } |
891 | ||
ec95d32c TO |
892 | /** |
893 | * @inheritDoc | |
894 | */ | |
895 | public function clearResourceCache() { | |
896 | _backdrop_flush_css_js(); | |
897 | } | |
5c6ebf4c TO |
898 | |
899 | /** | |
900 | * Get all the contact emails for users that have a specific permission. | |
901 | * | |
902 | * @param string $permissionName | |
903 | * Name of the permission we are interested in. | |
904 | * | |
905 | * @return string | |
906 | * a comma separated list of email addresses | |
907 | */ | |
908 | public function permissionEmails($permissionName) { | |
909 | // FIXME!!!! | |
be2fb01f | 910 | return []; |
5c6ebf4c TO |
911 | } |
912 | ||
84da17e3 | 913 | /** |
914 | * @inheritdoc | |
915 | */ | |
916 | public function getDefaultFileStorage() { | |
917 | $config = CRM_Core_Config::singleton(); | |
918 | $baseURL = CRM_Utils_System::languageNegotiationURL($config->userFrameworkBaseURL, FALSE, TRUE); | |
919 | ||
920 | $siteName = $this->parseBackdropSiteNameFromRequest('/files/civicrm'); | |
921 | if ($siteName) { | |
922 | $filesURL = $baseURL . "sites/$siteName/files/civicrm/"; | |
923 | } | |
924 | else { | |
925 | $filesURL = $baseURL . "files/civicrm/"; | |
926 | } | |
927 | ||
be2fb01f | 928 | return [ |
84da17e3 | 929 | 'url' => $filesURL, |
930 | 'path' => CRM_Utils_File::baseFilePath(), | |
be2fb01f | 931 | ]; |
84da17e3 | 932 | } |
933 | ||
934 | /** | |
935 | * Check if a resource url is within the Backdrop directory and format appropriately. | |
936 | * | |
937 | * @param $url (reference) | |
938 | * | |
939 | * @return bool | |
940 | * TRUE for internal paths, FALSE for external. The backdrop_add_js fn is able to add js more | |
941 | * efficiently if it is known to be in the Backdrop site | |
942 | */ | |
943 | public function formatResourceUrl(&$url) { | |
944 | $internal = FALSE; | |
945 | $base = CRM_Core_Config::singleton()->resourceBase; | |
946 | global $base_url; | |
947 | // Handle absolute urls | |
948 | // 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) | |
949 | // to see if the url is within our Backdrop dir, if it is we are able to treated it as an internal url | |
950 | if (strpos($url, $base_url) === 0) { | |
951 | $file = trim(str_replace($base_url, '', $url), '/'); | |
952 | // CRM-18130: Custom CSS URL not working if aliased or rewritten | |
953 | if (file_exists(BACKDROP_ROOT . $file)) { | |
954 | $url = $file; | |
955 | $internal = TRUE; | |
956 | } | |
957 | } | |
958 | // Handle relative urls that are within the CiviCRM module directory | |
959 | elseif (strpos($url, $base) === 0) { | |
960 | $internal = TRUE; | |
961 | $url = $this->appendCoreDirectoryToResourceBase(dirname(backdrop_get_path('module', 'civicrm')) . '/') . trim(substr($url, strlen($base)), '/'); | |
962 | } | |
963 | // Strip query string | |
964 | $q = strpos($url, '?'); | |
965 | if ($q && $internal) { | |
966 | $url = substr($url, 0, $q); | |
967 | } | |
968 | return $internal; | |
969 | } | |
970 | ||
971 | /** | |
972 | * @inheritDoc | |
973 | */ | |
974 | public function setMessage($message) { | |
975 | backdrop_set_message($message); | |
976 | } | |
977 | ||
978 | /** | |
979 | * @inheritDoc | |
980 | */ | |
981 | public function permissionDenied() { | |
982 | backdrop_access_denied(); | |
983 | } | |
984 | ||
985 | /** | |
986 | * @inheritDoc | |
987 | */ | |
988 | public function flush() { | |
989 | backdrop_flush_all_caches(); | |
990 | } | |
991 | ||
992 | /** | |
993 | * Determine if Backdrop multi-site applies to the current request -- and, | |
994 | * specifically, determine the name of the multisite folder. | |
995 | * | |
996 | * @param string $flagFile | |
997 | * Check if $flagFile exists inside the site dir. | |
998 | * @return null|string | |
999 | * string, e.g. `bar.example.com` if using multisite. | |
1000 | * NULL if using the default site. | |
1001 | */ | |
1002 | private function parseBackdropSiteNameFromRequest($flagFile = '') { | |
1003 | $phpSelf = array_key_exists('PHP_SELF', $_SERVER) ? $_SERVER['PHP_SELF'] : ''; | |
1004 | $httpHost = array_key_exists('HTTP_HOST', $_SERVER) ? $_SERVER['HTTP_HOST'] : ''; | |
1005 | if (empty($httpHost)) { | |
1006 | $httpHost = parse_url(CIVICRM_UF_BASEURL, PHP_URL_HOST); | |
1007 | if (parse_url(CIVICRM_UF_BASEURL, PHP_URL_PORT)) { | |
1008 | $httpHost .= ':' . parse_url(CIVICRM_UF_BASEURL, PHP_URL_PORT); | |
1009 | } | |
1010 | } | |
1011 | ||
1012 | $confdir = $this->cmsRootPath() . '/sites'; | |
1013 | ||
1014 | if (file_exists($confdir . "/sites.php")) { | |
1015 | include $confdir . "/sites.php"; | |
1016 | } | |
1017 | else { | |
be2fb01f | 1018 | $sites = []; |
84da17e3 | 1019 | } |
1020 | ||
1021 | $uri = explode('/', $phpSelf); | |
1022 | $server = explode('.', implode('.', array_reverse(explode(':', rtrim($httpHost, '.'))))); | |
1023 | for ($i = count($uri) - 1; $i > 0; $i--) { | |
1024 | for ($j = count($server); $j > 0; $j--) { | |
1025 | $dir = implode('.', array_slice($server, -$j)) . implode('.', array_slice($uri, 0, $i)); | |
1026 | if (file_exists("$confdir/$dir" . $flagFile)) { | |
1027 | \Civi::$statics[__CLASS__]['drupalSiteName'] = $dir; | |
1028 | return \Civi::$statics[__CLASS__]['drupalSiteName']; | |
1029 | } | |
1030 | // check for alias | |
1031 | if (isset($sites[$dir]) && file_exists("$confdir/{$sites[$dir]}" . $flagFile)) { | |
1032 | \Civi::$statics[__CLASS__]['drupalSiteName'] = $sites[$dir]; | |
1033 | return \Civi::$statics[__CLASS__]['drupalSiteName']; | |
1034 | } | |
1035 | } | |
1036 | } | |
1037 | } | |
1038 | ||
88764dbe | 1039 | /** |
8a09f4aa | 1040 | * Append Backdrop CSS and JS to coreResourcesList. |
88764dbe | 1041 | * |
303017a1 | 1042 | * @param \Civi\Core\Event\GenericHookEvent $e |
88764dbe | 1043 | */ |
303017a1 CW |
1044 | public function appendCoreResources(\Civi\Core\Event\GenericHookEvent $e) { |
1045 | $e->list[] = 'css/backdrop.css'; | |
1046 | $e->list[] = 'js/crm.backdrop.js'; | |
88764dbe | 1047 | } |
fb3c1356 | 1048 | |
84fce21c | 1049 | } |