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