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 | +--------------------------------------------------------------------+ | |
26 | */ | |
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; | |
bb3a214a EM |
47 | /** |
48 | * | |
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 | /** | |
62 | * @param string dir base civicrm directory | |
63 | * Return default Site Settings | |
64 | * @return array array | |
65 | * - $url, (Joomla - non admin url) | |
66 | * - $siteName, | |
67 | * - $siteRoot | |
68 | */ | |
9b873358 | 69 | public function getDefaultSiteSettings($dir) { |
9977c6f5 | 70 | $config = CRM_Core_Config::singleton(); |
71 | $siteName = $siteRoot = NULL; | |
72 | $matches = array(); | |
73 | if (preg_match( | |
74 | '|/sites/([\w\.\-\_]+)/|', | |
75 | $config->templateCompileDir, | |
76 | $matches | |
77 | )) { | |
78 | $siteName = $matches[1]; | |
79 | if ($siteName) { | |
80 | $siteName = "/sites/$siteName/"; | |
81 | $siteNamePos = strpos($dir, $siteName); | |
82 | if ($siteNamePos !== FALSE) { | |
83 | $siteRoot = substr($dir, 0, $siteNamePos); | |
84 | } | |
85 | } | |
86 | } | |
87 | $url = $config->userFrameworkBaseURL; | |
88 | return array($url, $siteName, $siteRoot); | |
89 | } | |
42e1a97c E |
90 | |
91 | /** | |
92 | * Check if a resource url is within the drupal directory and format appropriately | |
93 | * | |
94 | * @param url (reference) | |
95 | * | |
abfa8a7d EM |
96 | * @return bool: TRUE for internal paths, FALSE for external. The drupal_add_js fn is able to add js more |
97 | * efficiently if it is known to be in the drupal site | |
42e1a97c | 98 | */ |
00be9182 | 99 | public function formatResourceUrl(&$url) { |
42e1a97c E |
100 | $internal = FALSE; |
101 | $base = CRM_Core_Config::singleton()->resourceBase; | |
102 | global $base_url; | |
103 | // Handle absolute urls | |
abfa8a7d EM |
104 | // 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) |
105 | // 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 |
106 | if (strpos($url, $base_url) === 0) { |
107 | $internal = TRUE; | |
108 | $url = trim(str_replace($base_url, '', $url), '/'); | |
109 | } | |
abfa8a7d | 110 | // Handle relative urls that are within the CiviCRM module directory |
42e1a97c E |
111 | elseif (strpos($url, $base) === 0) { |
112 | $internal = TRUE; | |
168be77d | 113 | $url = $this->appendCoreDirectoryToResourceBase(substr(drupal_get_path('module', 'civicrm'), 0, -6)) . 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. | |
168be77d E |
132 | * @return string $url with civicrm-core directory appended if not standard civi dir |
133 | */ | |
00be9182 | 134 | public function appendCoreDirectoryToResourceBase($url) { |
168be77d | 135 | global $civicrm_root; |
2102546d | 136 | $lastDirectory = basename($civicrm_root); |
22e263ad | 137 | if ($lastDirectory != 'civicrm') { |
168be77d E |
138 | return $url .= $lastDirectory . '/'; |
139 | } | |
140 | return $url; | |
141 | } | |
72b140cf E |
142 | |
143 | /** | |
144 | * Generate an internal CiviCRM URL (copied from DRUPAL/includes/common.inc#url) | |
145 | * | |
77855840 TO |
146 | * @param $path |
147 | * String The path being linked to, such as "civicrm/add". | |
148 | * @param $query | |
149 | * String A query string to append to the link. | |
150 | * @param $absolute | |
151 | * Boolean Whether to force the output to be an absolute link (beginning with http:). | |
72b140cf E |
152 | * Useful for links that will be displayed outside the site, such as in an |
153 | * RSS feed. | |
77855840 TO |
154 | * @param $fragment |
155 | * String A fragment identifier (named anchor) to append to the link. | |
156 | * @param $htmlize | |
157 | * Boolean whether to convert to html eqivalant. | |
158 | * @param $frontend | |
159 | * Boolean a gross joomla hack. | |
160 | * @param $forceBackend | |
161 | * Boolean a gross joomla hack. | |
72b140cf E |
162 | * |
163 | * @return string an HTML string containing a link to the given path. | |
72b140cf E |
164 | * |
165 | */ | |
a3e55d9c TO |
166 | function url( |
167 | $path = NULL, $query = NULL, $absolute = FALSE, | |
72b140cf E |
168 | $fragment = NULL, $htmlize = TRUE, |
169 | $frontend = FALSE, $forceBackend = FALSE | |
170 | ) { | |
171 | $config = CRM_Core_Config::singleton(); | |
172 | $script = 'index.php'; | |
173 | ||
174 | $path = CRM_Utils_String::stripPathChars($path); | |
175 | ||
176 | if (isset($fragment)) { | |
177 | $fragment = '#' . $fragment; | |
178 | } | |
179 | ||
180 | if (!isset($config->useFrameworkRelativeBase)) { | |
181 | $base = parse_url($config->userFrameworkBaseURL); | |
182 | $config->useFrameworkRelativeBase = $base['path']; | |
183 | } | |
184 | $base = $absolute ? $config->userFrameworkBaseURL : $config->useFrameworkRelativeBase; | |
185 | ||
186 | $separator = $htmlize ? '&' : '&'; | |
187 | ||
188 | if (!$config->cleanURL) { | |
189 | if (isset($path)) { | |
190 | if (isset($query)) { | |
191 | return $base . $script . '?q=' . $path . $separator . $query . $fragment; | |
192 | } | |
193 | else { | |
194 | return $base . $script . '?q=' . $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 | else { | |
207 | if (isset($path)) { | |
208 | if (isset($query)) { | |
209 | return $base . $path . '?' . $query . $fragment; | |
210 | } | |
211 | else { | |
212 | return $base . $path . $fragment; | |
213 | } | |
214 | } | |
215 | else { | |
216 | if (isset($query)) { | |
217 | return $base . $script . '?' . $query . $fragment; | |
218 | } | |
219 | else { | |
220 | return $base . $fragment; | |
221 | } | |
222 | } | |
223 | } | |
224 | } | |
32998c82 EM |
225 | |
226 | /** | |
227 | * Get User ID from UserFramework system (Drupal) | |
77855840 TO |
228 | * @param object $user |
229 | * Object as described by the CMS. | |
32998c82 EM |
230 | * @return mixed <NULL, number> |
231 | */ | |
00be9182 | 232 | public function getUserIDFromUserObject($user) { |
32998c82 EM |
233 | return !empty($user->uid) ? $user->uid : NULL; |
234 | } | |
2b617cb0 EM |
235 | |
236 | /** | |
237 | * Get Unique Identifier from UserFramework system (CMS) | |
77855840 TO |
238 | * @param object $user |
239 | * Object as described by the User Framework. | |
2b617cb0 EM |
240 | * @return mixed $uniqueIdentifer Unique identifier from the user Framework system |
241 | * | |
242 | */ | |
00be9182 | 243 | public function getUniqueIdentifierFromUserObject($user) { |
2b617cb0 EM |
244 | return empty($user->mail) ? NULL : $user->mail; |
245 | } | |
246 | ||
247 | /** | |
248 | * Get currently logged in user unique identifier - this tends to be the email address or user name. | |
249 | * | |
250 | * @return string $userID logged in user unique identifier | |
251 | */ | |
00be9182 | 252 | public function getLoggedInUniqueIdentifier() { |
2b617cb0 EM |
253 | global $user; |
254 | return $this->getUniqueIdentifierFromUserObject($user); | |
255 | } | |
d0ffa3e4 EM |
256 | |
257 | /** | |
258 | * Action to take when access is not permitted | |
259 | */ | |
00be9182 | 260 | public function permissionDenied() { |
d0ffa3e4 EM |
261 | drupal_access_denied(); |
262 | } | |
59f97da6 EM |
263 | |
264 | /** | |
265 | * Get Url to view user record | |
77855840 TO |
266 | * @param int $contactID |
267 | * Contact ID. | |
59f97da6 EM |
268 | * |
269 | * @return string | |
270 | */ | |
00be9182 | 271 | public function getUserRecordUrl($contactID) { |
59f97da6 EM |
272 | $uid = CRM_Core_BAO_UFMatch::getUFId($contactID); |
273 | if (CRM_Core_Session::singleton()->get('userID') == $contactID || CRM_Core_Permission::checkAnyPerm(array('cms:administer users', 'cms:view user account'))) { | |
274 | return CRM_Utils_System::url('user/' . $uid); | |
275 | }; | |
276 | } | |
277 | ||
278 | /** | |
279 | * Is the current user permitted to add a user | |
280 | * @return bool | |
281 | */ | |
00be9182 | 282 | public function checkPermissionAddUser() { |
59f97da6 EM |
283 | if (CRM_Core_Permission::check('administer users')) { |
284 | return TRUE; | |
285 | } | |
286 | } | |
e0dd98a5 EM |
287 | |
288 | ||
289 | /** | |
290 | * Log error to CMS | |
291 | */ | |
00be9182 | 292 | public function logger($message) { |
e0dd98a5 EM |
293 | if (CRM_Core_Config::singleton()->userFrameworkLogging) { |
294 | watchdog('civicrm', $message, NULL, WATCHDOG_DEBUG); | |
295 | } | |
296 | } | |
f091327b CW |
297 | |
298 | /** | |
299 | * Flush css/js caches | |
300 | */ | |
00be9182 | 301 | public function clearResourceCache() { |
f091327b CW |
302 | _drupal_flush_css_js(); |
303 | } | |
f9f361d0 CW |
304 | |
305 | /** | |
306 | * Append to coreResourcesList | |
307 | */ | |
00be9182 | 308 | public function appendCoreResources(&$list) { |
f9f361d0 CW |
309 | $list[] = 'js/crm.drupal.js'; |
310 | } | |
7e9cadcf EM |
311 | |
312 | /** | |
313 | * Reset any system caches that may be required for proper CiviCRM | |
314 | * integration. | |
315 | */ | |
00be9182 | 316 | public function flush() { |
7e9cadcf EM |
317 | drupal_flush_all_caches(); |
318 | } | |
319 | ||
320 | /** | |
321 | * Get a list of all installed modules, including enabled and disabled ones | |
322 | * | |
323 | * @return array CRM_Core_Module | |
324 | * | |
325 | */ | |
00be9182 | 326 | public function getModules() { |
7e9cadcf EM |
327 | $result = array(); |
328 | $q = db_query('SELECT name, status FROM {system} WHERE type = \'module\' AND schema_version <> -1'); | |
329 | foreach ($q as $row) { | |
330 | $result[] = new CRM_Core_Module('drupal.' . $row->name, ($row->status == 1) ? TRUE : FALSE); | |
331 | } | |
332 | return $result; | |
333 | } | |
334 | ||
335 | /** | |
336 | * Find any users/roles/security-principals with the given permission | |
337 | * and replace it with one or more permissions. | |
338 | * | |
77855840 TO |
339 | * @param $oldPerm |
340 | * String. | |
341 | * @param $newPerms | |
342 | * Array, strings. | |
7e9cadcf EM |
343 | * |
344 | * @return void | |
345 | */ | |
00be9182 | 346 | public function replacePermission($oldPerm, $newPerms) { |
7e9cadcf EM |
347 | $roles = user_roles(FALSE, $oldPerm); |
348 | if (!empty($roles)) { | |
349 | foreach (array_keys($roles) as $rid) { | |
350 | user_role_revoke_permissions($rid, array($oldPerm)); | |
351 | user_role_grant_permissions($rid, $newPerms); | |
352 | } | |
353 | } | |
354 | } | |
355 | /** | |
356 | * Format the url as per language Negotiation. | |
357 | * | |
358 | * @param string $url | |
359 | * | |
360 | * @return string $url, formatted url. | |
361 | * @static | |
362 | */ | |
00be9182 | 363 | public function languageNegotiationURL($url, $addLanguagePart = TRUE, $removeLanguagePart = FALSE) { |
7e9cadcf EM |
364 | if (empty($url)) { |
365 | return $url; | |
366 | } | |
367 | ||
368 | //CRM-7803 -from d7 onward. | |
369 | $config = CRM_Core_Config::singleton(); | |
370 | if (function_exists('variable_get') && | |
371 | module_exists('locale') && | |
372 | function_exists('language_negotiation_get') | |
373 | ) { | |
374 | global $language; | |
375 | ||
376 | //does user configuration allow language | |
377 | //support from the URL (Path prefix or domain) | |
378 | if (language_negotiation_get('language') == 'locale-url') { | |
379 | $urlType = variable_get('locale_language_negotiation_url_part'); | |
380 | ||
381 | //url prefix | |
382 | if ($urlType == LOCALE_LANGUAGE_NEGOTIATION_URL_PREFIX) { | |
383 | if (isset($language->prefix) && $language->prefix) { | |
384 | if ($addLanguagePart) { | |
385 | $url .= $language->prefix . '/'; | |
386 | } | |
387 | if ($removeLanguagePart) { | |
388 | $url = str_replace("/{$language->prefix}/", '/', $url); | |
389 | } | |
390 | } | |
391 | } | |
392 | //domain | |
393 | if ($urlType == LOCALE_LANGUAGE_NEGOTIATION_URL_DOMAIN) { | |
394 | if (isset($language->domain) && $language->domain) { | |
395 | if ($addLanguagePart) { | |
396 | $url = (CRM_Utils_System::isSSL() ? 'https' : 'http') . '://' . $language->domain . base_path(); | |
397 | } | |
398 | if ($removeLanguagePart && defined('CIVICRM_UF_BASEURL')) { | |
399 | $url = str_replace('\\', '/', $url); | |
400 | $parseUrl = parse_url($url); | |
401 | ||
402 | //kinda hackish but not sure how to do it right | |
403 | //hope http_build_url() will help at some point. | |
404 | if (is_array($parseUrl) && !empty($parseUrl)) { | |
405 | $urlParts = explode('/', $url); | |
406 | $hostKey = array_search($parseUrl['host'], $urlParts); | |
407 | $ufUrlParts = parse_url(CIVICRM_UF_BASEURL); | |
408 | $urlParts[$hostKey] = $ufUrlParts['host']; | |
409 | $url = implode('/', $urlParts); | |
410 | } | |
411 | } | |
412 | } | |
413 | } | |
414 | } | |
415 | } | |
416 | return $url; | |
417 | } | |
418 | ||
419 | /** | |
420 | * GET CMS Version | |
421 | * @return string | |
422 | */ | |
00be9182 | 423 | public function getVersion() { |
7e9cadcf EM |
424 | return defined('VERSION') ? VERSION : 'Unknown'; |
425 | } | |
426 | ||
427 | /** | |
428 | */ | |
00be9182 | 429 | public function updateCategories() { |
7e9cadcf EM |
430 | // copied this from profile.module. Seems a bit inefficient, but i dont know a better way |
431 | // CRM-3600 | |
432 | cache_clear_all(); | |
433 | menu_rebuild(); | |
434 | } | |
435 | ||
7e9cadcf EM |
436 | /** |
437 | * Get the locale set in the hosting CMS | |
438 | * | |
439 | * @return string with the locale or null for none | |
440 | * | |
441 | */ | |
00be9182 | 442 | public function getUFLocale() { |
7e9cadcf EM |
443 | // return CiviCRM’s xx_YY locale that either matches Drupal’s Chinese locale |
444 | // (for CRM-6281), Drupal’s xx_YY or is retrieved based on Drupal’s xx | |
445 | // sometimes for CLI based on order called, this might not be set and/or empty | |
446 | global $language; | |
447 | ||
448 | if (empty($language)) { | |
449 | return NULL; | |
450 | } | |
451 | ||
452 | if ($language->language == 'zh-hans') { | |
453 | return 'zh_CN'; | |
454 | } | |
455 | ||
456 | if ($language->language == 'zh-hant') { | |
457 | return 'zh_TW'; | |
458 | } | |
459 | ||
460 | if (preg_match('/^.._..$/', $language->language)) { | |
461 | return $language->language; | |
462 | } | |
463 | ||
464 | return CRM_Core_I18n_PseudoConstant::longForShort(substr($language->language, 0, 2)); | |
465 | } | |
466 | /** | |
467 | * Perform any post login activities required by the UF - | |
468 | * e.g. for drupal: records a watchdog message about the new session, saves the login timestamp, | |
469 | * calls hook_user op 'login' and generates a new session. | |
470 | * | |
471 | * @param array params | |
472 | * | |
473 | * FIXME: Document values accepted/required by $params | |
474 | * | |
475 | */ | |
9b873358 | 476 | public function userLoginFinalize($params = array()) { |
7e9cadcf EM |
477 | user_login_finalize($params); |
478 | } | |
479 | ||
480 | /** | |
100fef9d | 481 | * Figure out the post url for the form |
7e9cadcf | 482 | * |
77855840 TO |
483 | * @param mix $action |
484 | * The default action if one is pre-specified. | |
7e9cadcf EM |
485 | * |
486 | * @return string the url to post the form | |
7e9cadcf | 487 | */ |
00be9182 | 488 | public function postURL($action) { |
7e9cadcf EM |
489 | if (!empty($action)) { |
490 | return $action; | |
491 | } | |
492 | return $this->url($_GET['q']); | |
493 | } | |
232624b1 | 494 | } |