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