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