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