Add in unit test of searching when price field value label has changed
[civicrm-core.git] / CRM / Utils / System / DrupalBase.php
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 * $Id$
33 *
34 */
35
36 /**
37 * Drupal specific stuff goes here
38 */
39 abstract class CRM_Utils_System_DrupalBase extends CRM_Utils_System_Base {
40
41 /**
42 * Does this CMS / UF support a CMS specific logging mechanism?
43 * @var bool
44 * @todo - we should think about offering up logging mechanisms in a way that is also extensible by extensions
45 */
46 public $supports_UF_Logging = TRUE;
47
48 /**
49 */
50 public function __construct() {
51 /**
52 * deprecated property to check if this is a drupal install. The correct method is to have functions on the UF classes for all UF specific
53 * functions and leave the codebase oblivious to the type of CMS
54 * @deprecated
55 * @var bool
56 */
57 $this->is_drupal = TRUE;
58 $this->supports_form_extensions = TRUE;
59 }
60
61 /**
62 * @inheritdoc
63 */
64 public function getDefaultFileStorage() {
65 $config = CRM_Core_Config::singleton();
66 $baseURL = CRM_Utils_System::languageNegotiationURL($config->userFrameworkBaseURL, FALSE, TRUE);
67
68 $siteName = $this->parseDrupalSiteNameFromRequest('/files/civicrm');
69 if ($siteName) {
70 $filesURL = $baseURL . "sites/$siteName/files/civicrm/";
71 }
72 else {
73 $filesURL = $baseURL . "sites/default/files/civicrm/";
74 }
75
76 return [
77 'url' => $filesURL,
78 'path' => CRM_Utils_File::baseFilePath(),
79 ];
80 }
81
82 /**
83 * @inheritDoc
84 */
85 public function getDefaultSiteSettings($dir) {
86 $config = CRM_Core_Config::singleton();
87 $siteName = $siteRoot = NULL;
88 $matches = [];
89 if (preg_match(
90 '|/sites/([\w\.\-\_]+)/|',
91 $config->templateCompileDir,
92 $matches
93 )) {
94 $siteName = $matches[1];
95 if ($siteName) {
96 $siteName = "/sites/$siteName/";
97 $siteNamePos = strpos($dir, $siteName);
98 if ($siteNamePos !== FALSE) {
99 $siteRoot = substr($dir, 0, $siteNamePos);
100 }
101 }
102 }
103 $url = $config->userFrameworkBaseURL;
104 return [$url, $siteName, $siteRoot];
105 }
106
107 /**
108 * Check if a resource url is within the drupal directory and format appropriately.
109 *
110 * @param $url (reference)
111 *
112 * @return bool
113 * TRUE for internal paths, FALSE for external. The drupal_add_js fn is able to add js more
114 * efficiently if it is known to be in the drupal site
115 */
116 public function formatResourceUrl(&$url) {
117 $internal = FALSE;
118 $base = CRM_Core_Config::singleton()->resourceBase;
119 global $base_url;
120 // Strip query string
121 $q = strpos($url, '?');
122 $url_path = $q ? substr($url, 0, $q) : $url;
123 // Handle absolute urls
124 // 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)
125 // to see if the url is within our drupal dir, if it is we are able to treated it as an internal url
126 if (strpos($url_path, $base_url) === 0) {
127 $file = trim(str_replace($base_url, '', $url_path), '/');
128 // CRM-18130: Custom CSS URL not working if aliased or rewritten
129 if (file_exists(DRUPAL_ROOT . '/' . $file)) {
130 $url = $file;
131 $internal = TRUE;
132 }
133 }
134 // Handle relative urls that are within the CiviCRM module directory
135 elseif (strpos($url_path, $base) === 0) {
136 $internal = TRUE;
137 $url = $this->appendCoreDirectoryToResourceBase(dirname(drupal_get_path('module', 'civicrm')) . '/') . trim(substr($url_path, strlen($base)), '/');
138 }
139 return $internal;
140 }
141
142 /**
143 * In instance where civicrm folder has a drupal folder & a civicrm core folder @ the same level append the
144 * civicrm folder name to the url
145 * See CRM-13737 for discussion of how this allows implementers to alter the folder structure
146 * @todo - this only provides a limited amount of flexiblity - it still expects a 'civicrm' folder with a 'drupal' folder
147 * and is only flexible as to the name of the civicrm folder.
148 *
149 * @param string $url
150 * Potential resource url based on standard folder assumptions.
151 * @return string
152 * with civicrm-core directory appended if not standard civi dir
153 */
154 public function appendCoreDirectoryToResourceBase($url) {
155 global $civicrm_root;
156 $lastDirectory = basename($civicrm_root);
157 if ($lastDirectory != 'civicrm') {
158 return $url .= $lastDirectory . '/';
159 }
160 return $url;
161 }
162
163 /**
164 * Generate an internal CiviCRM URL (copied from DRUPAL/includes/common.inc#url)
165 *
166 * @inheritDoc
167 */
168 public function url(
169 $path = NULL,
170 $query = NULL,
171 $absolute = FALSE,
172 $fragment = NULL,
173 $frontend = FALSE,
174 $forceBackend = FALSE
175 ) {
176 $config = CRM_Core_Config::singleton();
177 $script = 'index.php';
178
179 $path = CRM_Utils_String::stripPathChars($path);
180
181 if (isset($fragment)) {
182 $fragment = '#' . $fragment;
183 }
184
185 $base = $absolute ? $config->userFrameworkBaseURL : $config->useFrameworkRelativeBase;
186
187 $separator = '&';
188
189 if (!$config->cleanURL) {
190 if (isset($path)) {
191 if (isset($query)) {
192 return $base . $script . '?q=' . $path . $separator . $query . $fragment;
193 }
194 else {
195 return $base . $script . '?q=' . $path . $fragment;
196 }
197 }
198 else {
199 if (isset($query)) {
200 return $base . $script . '?' . $query . $fragment;
201 }
202 else {
203 return $base . $fragment;
204 }
205 }
206 }
207 else {
208 if (isset($path)) {
209 if (isset($query)) {
210 return $base . $path . '?' . $query . $fragment;
211 }
212 else {
213 return $base . $path . $fragment;
214 }
215 }
216 else {
217 if (isset($query)) {
218 return $base . $script . '?' . $query . $fragment;
219 }
220 else {
221 return $base . $fragment;
222 }
223 }
224 }
225 }
226
227 /**
228 * @inheritDoc
229 */
230 public function getUserIDFromUserObject($user) {
231 return !empty($user->uid) ? $user->uid : NULL;
232 }
233
234 /**
235 * @inheritDoc
236 */
237 public function setMessage($message) {
238 drupal_set_message($message);
239 }
240
241 /**
242 * @inheritDoc
243 */
244 public function getUniqueIdentifierFromUserObject($user) {
245 return empty($user->mail) ? NULL : $user->mail;
246 }
247
248 /**
249 * @inheritDoc
250 */
251 public function getLoggedInUniqueIdentifier() {
252 global $user;
253 return $this->getUniqueIdentifierFromUserObject($user);
254 }
255
256 /**
257 * @inheritDoc
258 */
259 public function permissionDenied() {
260 drupal_access_denied();
261 }
262
263 /**
264 * @inheritDoc
265 */
266 public function getUserRecordUrl($contactID) {
267 $uid = CRM_Core_BAO_UFMatch::getUFId($contactID);
268 if (CRM_Core_Session::singleton()
269 ->get('userID') == $contactID || CRM_Core_Permission::checkAnyPerm([
270 'cms:administer users',
271 'cms:view user account',
272 ])
273 ) {
274 return $this->url('user/' . $uid);
275 };
276 }
277
278 /**
279 * @inheritDoc
280 */
281 public function checkPermissionAddUser() {
282 return CRM_Core_Permission::check('administer users');
283 }
284
285 /**
286 * @inheritDoc
287 */
288 public function logger($message) {
289 if (CRM_Core_Config::singleton()->userFrameworkLogging && function_exists('watchdog')) {
290 watchdog('civicrm', '%message', ['%message' => $message], NULL, WATCHDOG_DEBUG);
291 }
292 }
293
294 /**
295 * @inheritDoc
296 */
297 public function clearResourceCache() {
298 _drupal_flush_css_js();
299 }
300
301 /**
302 * @inheritDoc
303 */
304 public function flush() {
305 drupal_flush_all_caches();
306 }
307
308 /**
309 * @inheritDoc
310 */
311 public function getModules() {
312 $result = [];
313 $q = db_query('SELECT name, status FROM {system} WHERE type = \'module\' AND schema_version <> -1');
314 foreach ($q as $row) {
315 $result[] = new CRM_Core_Module('drupal.' . $row->name, ($row->status == 1) ? TRUE : FALSE);
316 }
317 return $result;
318 }
319
320 /**
321 * Find any users/roles/security-principals with the given permission
322 * and replace it with one or more permissions.
323 *
324 * @param string $oldPerm
325 * @param array $newPerms
326 * Array, strings.
327 *
328 * @return void
329 */
330 public function replacePermission($oldPerm, $newPerms) {
331 $roles = user_roles(FALSE, $oldPerm);
332 if (!empty($roles)) {
333 foreach (array_keys($roles) as $rid) {
334 user_role_revoke_permissions($rid, [$oldPerm]);
335 user_role_grant_permissions($rid, $newPerms);
336 }
337 }
338 }
339
340 /**
341 * @inheritDoc
342 */
343 public function languageNegotiationURL($url, $addLanguagePart = TRUE, $removeLanguagePart = FALSE) {
344 if (empty($url)) {
345 return $url;
346 }
347
348 //CRM-7803 -from d7 onward.
349 $config = CRM_Core_Config::singleton();
350 if (function_exists('variable_get') &&
351 module_exists('locale') &&
352 function_exists('language_negotiation_get')
353 ) {
354 global $language;
355
356 //does user configuration allow language
357 //support from the URL (Path prefix or domain)
358 if (language_negotiation_get('language') == 'locale-url') {
359 $urlType = variable_get('locale_language_negotiation_url_part');
360
361 //url prefix
362 if ($urlType == LOCALE_LANGUAGE_NEGOTIATION_URL_PREFIX) {
363 if (isset($language->prefix) && $language->prefix) {
364 if ($addLanguagePart) {
365 $url .= $language->prefix . '/';
366 }
367 if ($removeLanguagePart) {
368 $url = str_replace("/{$language->prefix}/", '/', $url);
369 }
370 }
371 }
372 //domain
373 if ($urlType == LOCALE_LANGUAGE_NEGOTIATION_URL_DOMAIN) {
374 if (isset($language->domain) && $language->domain) {
375 if ($addLanguagePart) {
376 $url = (CRM_Utils_System::isSSL() ? 'https' : 'http') . '://' . $language->domain . base_path();
377 }
378 if ($removeLanguagePart && defined('CIVICRM_UF_BASEURL')) {
379 $url = str_replace('\\', '/', $url);
380 $parseUrl = parse_url($url);
381
382 //kinda hackish but not sure how to do it right
383 //hope http_build_url() will help at some point.
384 if (is_array($parseUrl) && !empty($parseUrl)) {
385 $urlParts = explode('/', $url);
386 $hostKey = array_search($parseUrl['host'], $urlParts);
387 $ufUrlParts = parse_url(CIVICRM_UF_BASEURL);
388 $urlParts[$hostKey] = $ufUrlParts['host'];
389 $url = implode('/', $urlParts);
390 }
391 }
392 }
393 }
394 }
395 }
396 return $url;
397 }
398
399 /**
400 * @inheritDoc
401 */
402 public function getVersion() {
403 return defined('VERSION') ? VERSION : 'Unknown';
404 }
405
406 /**
407 * @inheritDoc
408 */
409 public function isUserRegistrationPermitted() {
410 if (!variable_get('user_register', TRUE)) {
411 return FALSE;
412 }
413 return TRUE;
414 }
415
416 /**
417 * @inheritDoc
418 */
419 public function isPasswordUserGenerated() {
420 if (variable_get('user_email_verification', TRUE)) {
421 return FALSE;
422 }
423 return TRUE;
424 }
425
426 /**
427 * @inheritDoc
428 */
429 public function updateCategories() {
430 // copied this from profile.module. Seems a bit inefficient, but i don't know a better way
431 cache_clear_all();
432 menu_rebuild();
433 }
434
435 /**
436 * @inheritDoc
437 */
438 public function getUFLocale() {
439 // return CiviCRM’s xx_YY locale that either matches Drupal’s Chinese locale
440 // (for CRM-6281), Drupal’s xx_YY or is retrieved based on Drupal’s xx
441 // sometimes for CLI based on order called, this might not be set and/or empty
442 $language = $this->getCurrentLanguage();
443
444 if (empty($language)) {
445 return NULL;
446 }
447
448 if ($language == 'zh-hans') {
449 return 'zh_CN';
450 }
451
452 if ($language == 'zh-hant') {
453 return 'zh_TW';
454 }
455
456 if (preg_match('/^.._..$/', $language)) {
457 return $language;
458 }
459
460 return CRM_Core_I18n_PseudoConstant::longForShort(substr($language, 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();
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 * Perform any post login activities required by the UF -
488 * e.g. for drupal: records a watchdog message about the new session, saves the login timestamp,
489 * calls hook_user op 'login' and generates a new session.
490 *
491 * @param array $params
492 *
493 * FIXME: Document values accepted/required by $params
494 */
495 public function userLoginFinalize($params = []) {
496 user_login_finalize($params);
497 }
498
499 /**
500 * @inheritDoc
501 */
502 public function getLoginDestination(&$form) {
503 $args = NULL;
504
505 $id = $form->get('id');
506 if ($id) {
507 $args .= "&id=$id";
508 }
509 else {
510 $gid = $form->get('gid');
511 if ($gid) {
512 $args .= "&gid=$gid";
513 }
514 else {
515 // Setup Personal Campaign Page link uses pageId
516 $pageId = $form->get('pageId');
517 if ($pageId) {
518 $component = $form->get('component');
519 $args .= "&pageId=$pageId&component=$component&action=add";
520 }
521 }
522 }
523
524 $destination = NULL;
525 if ($args) {
526 // append destination so user is returned to form they came from after login
527 $destination = CRM_Utils_System::currentPath() . '?reset=1' . $args;
528 }
529 return $destination;
530 }
531
532 /**
533 * Fixme: Why are we overriding the parent function? Seems inconsistent.
534 * This version supplies slightly different params to $this->url (not absolute and html encoded) but why?
535 *
536 * @param string $action
537 *
538 * @return string
539 */
540 public function postURL($action) {
541 if (!empty($action)) {
542 return $action;
543 }
544 return $this->url($_GET['q']);
545 }
546
547 /**
548 * Get an array of user details for a contact, containing at minimum the user ID & name.
549 *
550 * @param int $contactID
551 *
552 * @return array
553 * CMS user details including
554 * - id
555 * - name (ie the system user name.
556 */
557 public function getUser($contactID) {
558 $userDetails = parent::getUser($contactID);
559 $user = $this->getUserObject($userDetails['id']);
560 $userDetails['name'] = $user->name;
561 $userDetails['email'] = $user->mail;
562 return $userDetails;
563 }
564
565 /**
566 * Load the user object.
567 *
568 * Note this function still works in drupal 6, 7 & 8 but is deprecated in Drupal 8.
569 *
570 * @param $userID
571 *
572 * @return object
573 */
574 public function getUserObject($userID) {
575 return user_load($userID);
576 }
577
578 /**
579 * Parse the name of the drupal site.
580 *
581 * @param string $civicrm_root
582 *
583 * @return null|string
584 * @deprecated
585 */
586 public function parseDrupalSiteNameFromRoot($civicrm_root) {
587 $siteName = NULL;
588 if (strpos($civicrm_root,
589 DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . 'all' . DIRECTORY_SEPARATOR . 'modules'
590 ) === FALSE
591 ) {
592 $startPos = strpos($civicrm_root,
593 DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR
594 );
595 $endPos = strpos($civicrm_root,
596 DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR
597 );
598 if ($startPos && $endPos) {
599 // if component is in sites/SITENAME/modules
600 $siteName = substr($civicrm_root,
601 $startPos + 7,
602 $endPos - $startPos - 7
603 );
604 }
605 }
606 return $siteName;
607 }
608
609 /**
610 * Determine if Drupal multi-site applies to the current request -- and,
611 * specifically, determine the name of the multisite folder.
612 *
613 * @param string $flagFile
614 * Check if $flagFile exists inside the site dir.
615 * @return null|string
616 * string, e.g. `bar.example.com` if using multisite.
617 * NULL if using the default site.
618 */
619 private function parseDrupalSiteNameFromRequest($flagFile = '') {
620 $phpSelf = array_key_exists('PHP_SELF', $_SERVER) ? $_SERVER['PHP_SELF'] : '';
621 $httpHost = array_key_exists('HTTP_HOST', $_SERVER) ? $_SERVER['HTTP_HOST'] : '';
622 if (empty($httpHost)) {
623 $httpHost = parse_url(CIVICRM_UF_BASEURL, PHP_URL_HOST);
624 if (parse_url(CIVICRM_UF_BASEURL, PHP_URL_PORT)) {
625 $httpHost .= ':' . parse_url(CIVICRM_UF_BASEURL, PHP_URL_PORT);
626 }
627 }
628
629 $confdir = $this->cmsRootPath() . '/sites';
630
631 if (file_exists($confdir . "/sites.php")) {
632 include $confdir . "/sites.php";
633 }
634 else {
635 $sites = [];
636 }
637
638 $uri = explode('/', $phpSelf);
639 $server = explode('.', implode('.', array_reverse(explode(':', rtrim($httpHost, '.')))));
640 for ($i = count($uri) - 1; $i > 0; $i--) {
641 for ($j = count($server); $j > 0; $j--) {
642 $dir = implode('.', array_slice($server, -$j)) . implode('.', array_slice($uri, 0, $i));
643 if (file_exists("$confdir/$dir" . $flagFile)) {
644 \Civi::$statics[__CLASS__]['drupalSiteName'] = $dir;
645 return \Civi::$statics[__CLASS__]['drupalSiteName'];
646 }
647 // check for alias
648 if (isset($sites[$dir]) && file_exists("$confdir/{$sites[$dir]}" . $flagFile)) {
649 \Civi::$statics[__CLASS__]['drupalSiteName'] = $sites[$dir];
650 return \Civi::$statics[__CLASS__]['drupalSiteName'];
651 }
652 }
653 }
654 }
655
656 /**
657 * Function to return current language of Drupal
658 *
659 * @return string
660 */
661 public function getCurrentLanguage() {
662 global $language;
663 return (!empty($language->language)) ? $language->language : $language;
664 }
665
666 /**
667 * Is a front end page being accessed.
668 *
669 * Generally this would be a contribution form or other public page as opposed to a backoffice page (like contact edit).
670 *
671 * See https://github.com/civicrm/civicrm-drupal/pull/546/files
672 *
673 * @return bool
674 */
675 public function isFrontEndPage() {
676 // Get the menu items.
677 $args = explode('?', $_GET['q']);
678 $path = $args[0];
679
680 // Get the menu for above URL.
681 $item = CRM_Core_Menu::get($path);
682 if (!empty(CRM_Utils_Array::value('is_public', $item))) {
683 return TRUE;
684 }
685 return FALSE;
686 }
687
688 }