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