| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
| 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 |
| 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
| 32 | */ |
| 33 | |
| 34 | /** |
| 35 | * Backdrop-specific logic that differs from Drupal. |
| 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'] = [ |
| 46 | 'name' => $params['cms_name'], |
| 47 | 'mail' => $params[$mail], |
| 48 | 'op' => 'Create new account', |
| 49 | ]; |
| 50 | |
| 51 | $admin = user_access('administer users'); |
| 52 | if (!config_get('system.core', 'user_email_verification') || $admin) { |
| 53 | $form_state['input']['pass'] = ['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'] = []; |
| 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 | |
| 76 | $form = backdrop_retrieve_form('user_register_form', $form_state); |
| 77 | $form_state['process_input'] = 1; |
| 78 | $form_state['submitted'] = 1; |
| 79 | $form['#array_parents'] = []; |
| 80 | $form['#tree'] = FALSE; |
| 81 | backdrop_process_form('user_register_form', $form, $form_state); |
| 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 | */ |
| 94 | public function updateCMSName($ufID, $email) { |
| 95 | // CRM-5555 |
| 96 | if (function_exists('user_load')) { |
| 97 | $user = user_load($ufID); |
| 98 | if ($user->mail != $email) { |
| 99 | $user->mail = $email; |
| 100 | $user->save(); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Check if username and email exists in the Backdrop db. |
| 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') { |
| 116 | $errors = form_get_errors(); |
| 117 | if ($errors) { |
| 118 | // unset Backdrop messages to avoid twice display of errors |
| 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 { |
| 127 | $uid = db_query("SELECT uid FROM {users} WHERE name = :name", [':name' => $params['name']])->fetchField(); |
| 128 | if ((bool) $uid) { |
| 129 | $errors['cms_name'] = ts('The username %1 is already taken. Please select another username.', [1 => $params['name']]); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | if (!empty($params['mail'])) { |
| 135 | if (!valid_email_address($params['mail'])) { |
| 136 | $errors[$emailName] = ts('The e-mail address %1 is not valid.', [1 => $params['mail']]); |
| 137 | } |
| 138 | else { |
| 139 | $uid = db_query("SELECT uid FROM {users} WHERE mail = :mail", [':mail' => $params['mail']])->fetchField(); |
| 140 | if ((bool) $uid) { |
| 141 | $resetUrl = url('user/password'); |
| 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 | [1 => $params['mail'], 2 => $resetUrl] |
| 144 | ); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * @inheritDoc |
| 152 | */ |
| 153 | public function getLoginURL($destination = '') { |
| 154 | $query = $destination ? ['destination' => $destination] : []; |
| 155 | return url('user/login', ['query' => $query, 'absolute' => TRUE]); |
| 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 | |
| 167 | backdrop_set_title($pageTitle, PASS_THROUGH); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * @inheritDoc |
| 173 | */ |
| 174 | public function appendBreadCrumb($breadCrumbs) { |
| 175 | $breadCrumb = backdrop_get_breadcrumb(); |
| 176 | |
| 177 | if (is_array($breadCrumbs)) { |
| 178 | foreach ($breadCrumbs as $crumbs) { |
| 179 | if (stripos($crumbs['url'], 'id%%')) { |
| 180 | $args = ['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 | } |
| 193 | backdrop_set_breadcrumb($breadCrumb); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * @inheritDoc |
| 198 | */ |
| 199 | public function resetBreadCrumb() { |
| 200 | $bc = []; |
| 201 | backdrop_set_breadcrumb($bc); |
| 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 = [ |
| 212 | '#type' => 'markup', |
| 213 | '#markup' => $header, |
| 214 | ]; |
| 215 | backdrop_add_html_head($data, $key); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * @inheritDoc |
| 221 | */ |
| 222 | public function addScriptUrl($url, $region) { |
| 223 | $params = ['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 | } |
| 233 | // If the path is within the Backdrop directory we can use the more efficient 'file' setting |
| 234 | $params['type'] = $this->formatResourceUrl($url) ? 'file' : 'external'; |
| 235 | backdrop_add_js($url, $params); |
| 236 | return TRUE; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * @inheritDoc |
| 241 | */ |
| 242 | public function addScript($code, $region) { |
| 243 | $params = ['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 | } |
| 253 | backdrop_add_js($code, $params); |
| 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 = []; |
| 265 | // If the path is within the Backdrop directory we can use the more efficient 'file' setting |
| 266 | $params['type'] = $this->formatResourceUrl($url) ? 'file' : 'external'; |
| 267 | backdrop_add_css($url, $params); |
| 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 = ['type' => 'inline']; |
| 279 | backdrop_add_css($code, $params); |
| 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 | |
| 291 | /** |
| 292 | * Get the name of the table that stores the user details. |
| 293 | * |
| 294 | * @return string |
| 295 | */ |
| 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) { |
| 308 | $config = CRM_Core_Config::singleton(); |
| 309 | |
| 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()); |
| 313 | } |
| 314 | |
| 315 | $account = $userUid = $userMail = NULL; |
| 316 | if ($loadCMSBootstrap) { |
| 317 | $bootStrapParams = []; |
| 318 | if ($name && $password) { |
| 319 | $bootStrapParams = [ |
| 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 |
| 334 | // SOAP cannot load Backdrop bootstrap and hence we do it the old way |
| 335 | // Contact CiviSMTP folks if we run into issues with this :) |
| 336 | $cmsPath = $this->cmsRootPath(); |
| 337 | if (!defined('BACKDROP_ROOT')) { |
| 338 | define(BACKDROP_ROOT, $cmsPath); |
| 339 | } |
| 340 | require_once "$cmsPath/core/includes/bootstrap.inc"; |
| 341 | require_once "$cmsPath/core/includes/password.inc"; |
| 342 | |
| 343 | $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; |
| 344 | $name = $dbBackdrop->escapeSimple($strtolower($name)); |
| 345 | $userFrameworkUsersTableName = $this->getUsersTableName(); |
| 346 | |
| 347 | // LOWER in query below roughly translates to 'hurt my database without deriving any benefit' See CRM-19811. |
| 348 | $sql = " |
| 349 | SELECT u.* |
| 350 | FROM {$userFrameworkUsersTableName} u |
| 351 | WHERE LOWER(u.name) = '$name' |
| 352 | AND u.status = 1 |
| 353 | "; |
| 354 | |
| 355 | $query = $dbBackdrop->query($sql); |
| 356 | $row = $query->fetchRow(DB_FETCHMODE_ASSOC); |
| 357 | |
| 358 | if ($row) { |
| 359 | $fakeAccount = backdrop_anonymous_user(); |
| 360 | $fakeAccount->name = $name; |
| 361 | $fakeAccount->pass = $row['pass']; |
| 362 | $passwordCheck = user_check_password($password, $fakeAccount); |
| 363 | if ($passwordCheck) { |
| 364 | $userUid = $row['uid']; |
| 365 | $userMail = $row['mail']; |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | if ($userUid && $userMail) { |
| 371 | CRM_Core_BAO_UFMatch::synchronizeUFMatch($account, $userUid, $userMail, 'Backdrop'); |
| 372 | $contactID = CRM_Core_BAO_UFMatch::getContactId($userUid); |
| 373 | if (!$contactID) { |
| 374 | return FALSE; |
| 375 | } |
| 376 | return [$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 - |
| 405 | * For Backdrop this could mean recording a watchdog message about the new |
| 406 | * session, saving the login timestamp, calling hook_user_login(), etc. |
| 407 | * |
| 408 | * @param array $params |
| 409 | * The array of form values submitted by the user. |
| 410 | */ |
| 411 | public function userLoginFinalize($params = []) { |
| 412 | user_login_finalize($params); |
| 413 | } |
| 414 | |
| 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 | |
| 425 | /** |
| 426 | * @inheritDoc |
| 427 | */ |
| 428 | public function isPasswordUserGenerated() { |
| 429 | if (config_get('system.core', 'user_email_verification') == TRUE) { |
| 430 | return FALSE; |
| 431 | } |
| 432 | return TRUE; |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * @inheritDoc |
| 437 | */ |
| 438 | public function getUFLocale() { |
| 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 |
| 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 | |
| 486 | /** |
| 487 | * Determine the native ID of the CMS user. |
| 488 | * |
| 489 | * @param string $username |
| 490 | * @return int|null |
| 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'); |
| 505 | user_logout(); |
| 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 | /** |
| 518 | * Load Backdrop bootstrap. |
| 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 | */ |
| 530 | public function loadBootStrap($params = [], $loadUser = TRUE, $throwError = TRUE, $realPath = NULL) { |
| 531 | $cmsPath = $this->cmsRootPath($realPath); |
| 532 | |
| 533 | if (!file_exists("$cmsPath/core/includes/bootstrap.inc")) { |
| 534 | if ($throwError) { |
| 535 | echo '<br />Sorry, could not locate bootstrap.inc\n'; |
| 536 | exit(); |
| 537 | } |
| 538 | return FALSE; |
| 539 | } |
| 540 | // load Backdrop bootstrap |
| 541 | chdir($cmsPath); |
| 542 | define('BACKDROP_ROOT', $cmsPath); |
| 543 | |
| 544 | // For Backdrop multi-site CRM-11313 |
| 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 | } |
| 551 | require_once "$cmsPath/core/includes/bootstrap.inc"; |
| 552 | require_once "$cmsPath/core/includes/config.inc"; |
| 553 | backdrop_bootstrap(BACKDROP_BOOTSTRAP_FULL); |
| 554 | |
| 555 | // Explicitly setting error reporting, since we cannot handle Backdrop |
| 556 | // related notices. |
| 557 | error_reporting(1); |
| 558 | if (!function_exists('module_exists') || !module_exists('civicrm')) { |
| 559 | if ($throwError) { |
| 560 | echo '<br />Sorry, could not load Backdrop bootstrap.'; |
| 561 | exit(); |
| 562 | } |
| 563 | return FALSE; |
| 564 | } |
| 565 | |
| 566 | // Backdrop successfully bootstrapped. |
| 567 | $config = CRM_Core_Config::singleton(); |
| 568 | |
| 569 | // lets also fix the clean url setting |
| 570 | // CRM-6948 |
| 571 | $config->cleanURL = (int) config_get('system.core', 'clean_url'); |
| 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) { |
| 585 | // Load the user we need to check Backdrop permissions. |
| 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(); |
| 619 | $config->cleanURL = (int) config_get('system.core', 'clean_url'); |
| 620 | |
| 621 | // CRM-8655: Backdrop wasn't available during bootstrap, so |
| 622 | // hook_civicrm_config() never executes. |
| 623 | CRM_Utils_Hook::config($config); |
| 624 | |
| 625 | return FALSE; |
| 626 | } |
| 627 | |
| 628 | /** |
| 629 | * @inheritDoc |
| 630 | */ |
| 631 | public function cmsRootPath($scriptFilename = NULL) { |
| 632 | global $civicrm_paths; |
| 633 | if (!empty($civicrm_paths['cms.root']['path'])) { |
| 634 | return $civicrm_paths['cms.root']['path']; |
| 635 | } |
| 636 | |
| 637 | $cmsRoot = NULL; |
| 638 | $valid = NULL; |
| 639 | |
| 640 | if (!is_null($scriptFilename)) { |
| 641 | $path = $scriptFilename; |
| 642 | } |
| 643 | else { |
| 644 | $path = $_SERVER['SCRIPT_FILENAME']; |
| 645 | } |
| 646 | |
| 647 | // CRM-7582 |
| 648 | $pathVars = explode('/', |
| 649 | str_replace('//', '/', |
| 650 | str_replace('\\', '/', $path) |
| 651 | ) |
| 652 | ); |
| 653 | |
| 654 | // Keep the first directory name for later. |
| 655 | $firstVar = array_shift($pathVars); |
| 656 | |
| 657 | // Remove script name to reduce one iteration. |
| 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); |
| 664 | // Stop if we find backdrop signature file. |
| 665 | if (file_exists("$cmsRoot/core/misc/backdrop.js")) { |
| 666 | $valid = TRUE; |
| 667 | break; |
| 668 | } |
| 669 | // Remove one directory level. |
| 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 | ) { |
| 697 | $ufID = user_uid_optional_to_arg([]); |
| 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 | |
| 711 | if (function_exists('config_get') && |
| 712 | module_exists('locale') && |
| 713 | function_exists('language_negotiation_get') |
| 714 | ) { |
| 715 | global $language; |
| 716 | |
| 717 | // Check if language support from the URL (Path prefix or domain) is set. |
| 718 | if (language_negotiation_get('language') == 'locale-url') { |
| 719 | $urlType = config_get('locale.settings', 'locale_language_negotiation_url_part'); |
| 720 | |
| 721 | // URL prefix negotiation. |
| 722 | if ($urlType == LANGUAGE_NEGOTIATION_URL_PREFIX) { |
| 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 | } |
| 732 | // Domain negotiation. |
| 733 | if ($urlType == LANGUAGE_NEGOTIATION_URL_DOMAIN) { |
| 734 | if (isset($language->domain) && $language->domain) { |
| 735 | if ($addLanguagePart) { |
| 736 | $cleanedUrl = preg_replace('#^https?://#', '', $language->domain); |
| 737 | // Backdrop function base_path() adds a "/" to the beginning and |
| 738 | // end of the returned path. |
| 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) { |
| 778 | user_role_revoke_permissions($rid, [$oldPerm]); |
| 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. |
| 789 | * @param int $userID |
| 790 | * Backdrop User ID. |
| 791 | */ |
| 792 | public function og_membership_create($ogID, $userID) { |
| 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' |
| 799 | og_group('node', $ogID, ['entity' => user_load($userID)]); |
| 800 | } |
| 801 | else { |
| 802 | // Works for the OG 7.x-1.x branch |
| 803 | og_group($ogID, ['entity' => user_load($userID)]); |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | /** |
| 808 | * Wrapper for og_membership deletion. |
| 809 | * |
| 810 | * @param int $ogID |
| 811 | * Organic Group ID. |
| 812 | * @param int $userID |
| 813 | * Backdrop User ID. |
| 814 | */ |
| 815 | public function og_membership_delete($ogID, $userID) { |
| 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' |
| 820 | og_ungroup('node', $ogID, 'user', user_load($userID)); |
| 821 | } |
| 822 | else { |
| 823 | // Works for the OG 7.x-1.x branch |
| 824 | og_ungroup($ogID, 'user', user_load($userID)); |
| 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. |
| 834 | if (config_get('system.date', 'user_configurable_timezones') && $user->uid && isset($user->timezone) && strlen($user->timezone)) { |
| 835 | $timezone = $user->timezone; |
| 836 | } |
| 837 | else { |
| 838 | $timezone = config_get('system.date', 'default_timezone'); |
| 839 | } |
| 840 | if (!$timezone) { |
| 841 | $timezone = parent::getTimeZoneString(); |
| 842 | } |
| 843 | return $timezone; |
| 844 | } |
| 845 | |
| 846 | /** |
| 847 | * @inheritDoc |
| 848 | */ |
| 849 | public function setHttpHeader($name, $value) { |
| 850 | backdrop_add_http_header($name, $value); |
| 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 | } |
| 883 | } |
| 884 | |
| 885 | return [ |
| 886 | 'contactCount' => $contactCount, |
| 887 | 'contactMatching' => $contactMatching, |
| 888 | 'contactCreated' => $contactCreated, |
| 889 | ]; |
| 890 | } |
| 891 | |
| 892 | /** |
| 893 | * @inheritDoc |
| 894 | */ |
| 895 | public function clearResourceCache() { |
| 896 | _backdrop_flush_css_js(); |
| 897 | } |
| 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!!!! |
| 910 | return []; |
| 911 | } |
| 912 | |
| 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 | |
| 928 | return [ |
| 929 | 'url' => $filesURL, |
| 930 | 'path' => CRM_Utils_File::baseFilePath(), |
| 931 | ]; |
| 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 { |
| 1018 | $sites = []; |
| 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 | |
| 1039 | /** |
| 1040 | * Append Backdrop CSS and JS to coreResourcesList. |
| 1041 | * |
| 1042 | * @param \Civi\Core\Event\GenericHookEvent $e |
| 1043 | */ |
| 1044 | public function appendCoreResources(\Civi\Core\Event\GenericHookEvent $e) { |
| 1045 | $e->list[] = 'css/backdrop.css'; |
| 1046 | $e->list[] = 'js/crm.backdrop.js'; |
| 1047 | } |
| 1048 | |
| 1049 | } |