Commit | Line | Data |
---|---|---|
9977c6f5 | 1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
9977c6f5 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
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 | /** | |
87 | * Check if a resource url is within the drupal directory and format appropriately | |
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 E |
102 | if (strpos($url, $base_url) === 0) { |
103 | $internal = TRUE; | |
104 | $url = trim(str_replace($base_url, '', $url), '/'); | |
105 | } | |
abfa8a7d | 106 | // Handle relative urls that are within the CiviCRM module directory |
42e1a97c E |
107 | elseif (strpos($url, $base) === 0) { |
108 | $internal = TRUE; | |
168be77d | 109 | $url = $this->appendCoreDirectoryToResourceBase(substr(drupal_get_path('module', 'civicrm'), 0, -6)) . trim(substr($url, strlen($base)), '/'); |
42e1a97c E |
110 | } |
111 | // Strip query string | |
112 | $q = strpos($url, '?'); | |
113 | if ($q && $internal) { | |
114 | $url = substr($url, 0, $q); | |
115 | } | |
116 | return $internal; | |
117 | } | |
168be77d E |
118 | |
119 | /** | |
120 | * In instance where civicrm folder has a drupal folder & a civicrm core folder @ the same level append the | |
121 | * civicrm folder name to the url | |
122 | * See CRM-13737 for discussion of how this allows implementers to alter the folder structure | |
123 | * @todo - this only provides a limited amount of flexiblity - it still expects a 'civicrm' folder with a 'drupal' folder | |
124 | * and is only flexible as to the name of the civicrm folder. | |
125 | * | |
77855840 TO |
126 | * @param string $url |
127 | * Potential resource url based on standard folder assumptions. | |
a6c01b45 CW |
128 | * @return string |
129 | * with civicrm-core directory appended if not standard civi dir | |
168be77d | 130 | */ |
00be9182 | 131 | public function appendCoreDirectoryToResourceBase($url) { |
168be77d | 132 | global $civicrm_root; |
2102546d | 133 | $lastDirectory = basename($civicrm_root); |
22e263ad | 134 | if ($lastDirectory != 'civicrm') { |
168be77d E |
135 | return $url .= $lastDirectory . '/'; |
136 | } | |
137 | return $url; | |
138 | } | |
72b140cf E |
139 | |
140 | /** | |
141 | * Generate an internal CiviCRM URL (copied from DRUPAL/includes/common.inc#url) | |
142 | * | |
17f443df | 143 | * @inheritDoc |
72b140cf | 144 | */ |
3bdca100 | 145 | public function url( |
17f443df CW |
146 | $path = NULL, |
147 | $query = NULL, | |
148 | $absolute = FALSE, | |
149 | $fragment = NULL, | |
150 | $htmlize = TRUE, | |
151 | $frontend = FALSE, | |
152 | $forceBackend = FALSE | |
72b140cf E |
153 | ) { |
154 | $config = CRM_Core_Config::singleton(); | |
155 | $script = 'index.php'; | |
156 | ||
157 | $path = CRM_Utils_String::stripPathChars($path); | |
158 | ||
159 | if (isset($fragment)) { | |
160 | $fragment = '#' . $fragment; | |
161 | } | |
162 | ||
163 | if (!isset($config->useFrameworkRelativeBase)) { | |
164 | $base = parse_url($config->userFrameworkBaseURL); | |
165 | $config->useFrameworkRelativeBase = $base['path']; | |
166 | } | |
167 | $base = $absolute ? $config->userFrameworkBaseURL : $config->useFrameworkRelativeBase; | |
168 | ||
169 | $separator = $htmlize ? '&' : '&'; | |
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 TO |
250 | if (CRM_Core_Session::singleton() |
251 | ->get('userID') == $contactID || CRM_Core_Permission::checkAnyPerm(array( | |
252 | 'cms:administer users', | |
3bdca100 | 253 | 'cms:view user account', |
353ffa53 TO |
254 | )) |
255 | ) { | |
59f97da6 EM |
256 | return CRM_Utils_System::url('user/' . $uid); |
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) { |
e0dd98a5 EM |
271 | if (CRM_Core_Config::singleton()->userFrameworkLogging) { |
272 | watchdog('civicrm', $message, NULL, WATCHDOG_DEBUG); | |
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 CW |
282 | |
283 | /** | |
17f443df | 284 | * Append Drupal js to coreResourcesList |
f9f361d0 | 285 | */ |
00be9182 | 286 | public function appendCoreResources(&$list) { |
f9f361d0 CW |
287 | $list[] = 'js/crm.drupal.js'; |
288 | } | |
7e9cadcf EM |
289 | |
290 | /** | |
17f443df | 291 | * @inheritDoc |
7e9cadcf | 292 | */ |
00be9182 | 293 | public function flush() { |
7e9cadcf EM |
294 | drupal_flush_all_caches(); |
295 | } | |
296 | ||
297 | /** | |
298 | * Get a list of all installed modules, including enabled and disabled ones | |
299 | * | |
a6c01b45 CW |
300 | * @return array |
301 | * CRM_Core_Module | |
7e9cadcf | 302 | */ |
00be9182 | 303 | public function getModules() { |
7e9cadcf EM |
304 | $result = array(); |
305 | $q = db_query('SELECT name, status FROM {system} WHERE type = \'module\' AND schema_version <> -1'); | |
306 | foreach ($q as $row) { | |
307 | $result[] = new CRM_Core_Module('drupal.' . $row->name, ($row->status == 1) ? TRUE : FALSE); | |
308 | } | |
309 | return $result; | |
310 | } | |
311 | ||
312 | /** | |
313 | * Find any users/roles/security-principals with the given permission | |
314 | * and replace it with one or more permissions. | |
315 | * | |
5a4f6742 CW |
316 | * @param string $oldPerm |
317 | * @param array $newPerms | |
77855840 | 318 | * Array, strings. |
7e9cadcf EM |
319 | * |
320 | * @return void | |
321 | */ | |
00be9182 | 322 | public function replacePermission($oldPerm, $newPerms) { |
7e9cadcf EM |
323 | $roles = user_roles(FALSE, $oldPerm); |
324 | if (!empty($roles)) { | |
325 | foreach (array_keys($roles) as $rid) { | |
326 | user_role_revoke_permissions($rid, array($oldPerm)); | |
327 | user_role_grant_permissions($rid, $newPerms); | |
328 | } | |
329 | } | |
330 | } | |
353ffa53 | 331 | |
7e9cadcf | 332 | /** |
17f443df | 333 | * @inheritDoc |
7e9cadcf | 334 | */ |
00be9182 | 335 | public function languageNegotiationURL($url, $addLanguagePart = TRUE, $removeLanguagePart = FALSE) { |
7e9cadcf EM |
336 | if (empty($url)) { |
337 | return $url; | |
338 | } | |
339 | ||
340 | //CRM-7803 -from d7 onward. | |
341 | $config = CRM_Core_Config::singleton(); | |
342 | if (function_exists('variable_get') && | |
343 | module_exists('locale') && | |
344 | function_exists('language_negotiation_get') | |
345 | ) { | |
346 | global $language; | |
347 | ||
348 | //does user configuration allow language | |
349 | //support from the URL (Path prefix or domain) | |
350 | if (language_negotiation_get('language') == 'locale-url') { | |
351 | $urlType = variable_get('locale_language_negotiation_url_part'); | |
352 | ||
353 | //url prefix | |
354 | if ($urlType == LOCALE_LANGUAGE_NEGOTIATION_URL_PREFIX) { | |
355 | if (isset($language->prefix) && $language->prefix) { | |
356 | if ($addLanguagePart) { | |
357 | $url .= $language->prefix . '/'; | |
358 | } | |
359 | if ($removeLanguagePart) { | |
360 | $url = str_replace("/{$language->prefix}/", '/', $url); | |
361 | } | |
362 | } | |
363 | } | |
364 | //domain | |
365 | if ($urlType == LOCALE_LANGUAGE_NEGOTIATION_URL_DOMAIN) { | |
366 | if (isset($language->domain) && $language->domain) { | |
367 | if ($addLanguagePart) { | |
368 | $url = (CRM_Utils_System::isSSL() ? 'https' : 'http') . '://' . $language->domain . base_path(); | |
369 | } | |
370 | if ($removeLanguagePart && defined('CIVICRM_UF_BASEURL')) { | |
371 | $url = str_replace('\\', '/', $url); | |
372 | $parseUrl = parse_url($url); | |
373 | ||
374 | //kinda hackish but not sure how to do it right | |
375 | //hope http_build_url() will help at some point. | |
376 | if (is_array($parseUrl) && !empty($parseUrl)) { | |
353ffa53 TO |
377 | $urlParts = explode('/', $url); |
378 | $hostKey = array_search($parseUrl['host'], $urlParts); | |
379 | $ufUrlParts = parse_url(CIVICRM_UF_BASEURL); | |
7e9cadcf | 380 | $urlParts[$hostKey] = $ufUrlParts['host']; |
353ffa53 | 381 | $url = implode('/', $urlParts); |
7e9cadcf EM |
382 | } |
383 | } | |
384 | } | |
385 | } | |
386 | } | |
387 | } | |
388 | return $url; | |
389 | } | |
390 | ||
391 | /** | |
17f443df | 392 | * @inheritDoc |
7e9cadcf | 393 | */ |
00be9182 | 394 | public function getVersion() { |
7e9cadcf EM |
395 | return defined('VERSION') ? VERSION : 'Unknown'; |
396 | } | |
397 | ||
398 | /** | |
17f443df | 399 | * @inheritDoc |
7e9cadcf | 400 | */ |
00be9182 | 401 | public function updateCategories() { |
17f443df | 402 | // copied this from profile.module. Seems a bit inefficient, but i don't know a better way |
7e9cadcf EM |
403 | cache_clear_all(); |
404 | menu_rebuild(); | |
405 | } | |
406 | ||
7e9cadcf | 407 | /** |
17f443df | 408 | * @inheritDoc |
7e9cadcf | 409 | */ |
00be9182 | 410 | public function getUFLocale() { |
7e9cadcf EM |
411 | // return CiviCRM’s xx_YY locale that either matches Drupal’s Chinese locale |
412 | // (for CRM-6281), Drupal’s xx_YY or is retrieved based on Drupal’s xx | |
413 | // sometimes for CLI based on order called, this might not be set and/or empty | |
414 | global $language; | |
415 | ||
416 | if (empty($language)) { | |
417 | return NULL; | |
418 | } | |
419 | ||
420 | if ($language->language == 'zh-hans') { | |
421 | return 'zh_CN'; | |
422 | } | |
423 | ||
424 | if ($language->language == 'zh-hant') { | |
425 | return 'zh_TW'; | |
426 | } | |
427 | ||
428 | if (preg_match('/^.._..$/', $language->language)) { | |
429 | return $language->language; | |
430 | } | |
431 | ||
432 | return CRM_Core_I18n_PseudoConstant::longForShort(substr($language->language, 0, 2)); | |
433 | } | |
353ffa53 | 434 | |
7e9cadcf EM |
435 | /** |
436 | * Perform any post login activities required by the UF - | |
437 | * e.g. for drupal: records a watchdog message about the new session, saves the login timestamp, | |
438 | * calls hook_user op 'login' and generates a new session. | |
439 | * | |
3bdca100 | 440 | * @param array $params |
7e9cadcf EM |
441 | * |
442 | * FIXME: Document values accepted/required by $params | |
7e9cadcf | 443 | */ |
9b873358 | 444 | public function userLoginFinalize($params = array()) { |
7e9cadcf EM |
445 | user_login_finalize($params); |
446 | } | |
447 | ||
448 | /** | |
17f443df CW |
449 | * @inheritDoc |
450 | */ | |
451 | public function getLoginDestination(&$form) { | |
452 | $args = NULL; | |
453 | ||
454 | $id = $form->get('id'); | |
455 | if ($id) { | |
456 | $args .= "&id=$id"; | |
457 | } | |
458 | else { | |
459 | $gid = $form->get('gid'); | |
460 | if ($gid) { | |
461 | $args .= "&gid=$gid"; | |
462 | } | |
463 | else { | |
464 | // Setup Personal Campaign Page link uses pageId | |
465 | $pageId = $form->get('pageId'); | |
466 | if ($pageId) { | |
467 | $component = $form->get('component'); | |
468 | $args .= "&pageId=$pageId&component=$component&action=add"; | |
469 | } | |
470 | } | |
471 | } | |
472 | ||
473 | $destination = NULL; | |
474 | if ($args) { | |
475 | // append destination so user is returned to form they came from after login | |
476 | $destination = CRM_Utils_System::currentPath() . '?reset=1' . $args; | |
477 | } | |
478 | return $destination; | |
479 | } | |
480 | ||
481 | /** | |
482 | * Fixme: Why are we overriding the parent function? Seems inconsistent. | |
483 | * This version supplies slightly different params to $this->url (not absolute and html encoded) but why? | |
7e9cadcf | 484 | */ |
00be9182 | 485 | public function postURL($action) { |
7e9cadcf EM |
486 | if (!empty($action)) { |
487 | return $action; | |
488 | } | |
489 | return $this->url($_GET['q']); | |
490 | } | |
96025800 | 491 | |
232624b1 | 492 | } |