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