3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
37 * Drupal specific stuff goes here
39 abstract class CRM_Utils_System_DrupalBase
extends CRM_Utils_System_Base
{
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
46 var $supports_UF_Logging = TRUE;
50 public function __construct() {
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
57 $this->is_drupal
= TRUE;
58 $this->supports_form_extensions
= TRUE;
64 public function getDefaultSiteSettings($dir) {
65 $config = CRM_Core_Config
::singleton();
66 $siteName = $siteRoot = NULL;
69 '|/sites/([\w\.\-\_]+)/|',
70 $config->templateCompileDir
,
73 $siteName = $matches[1];
75 $siteName = "/sites/$siteName/";
76 $siteNamePos = strpos($dir, $siteName);
77 if ($siteNamePos !== FALSE) {
78 $siteRoot = substr($dir, 0, $siteNamePos);
82 $url = $config->userFrameworkBaseURL
;
83 return array($url, $siteName, $siteRoot);
87 * Check if a resource url is within the drupal directory and format appropriately.
89 * @param $url (reference)
92 * TRUE for internal paths, FALSE for external. The drupal_add_js fn is able to add js more
93 * efficiently if it is known to be in the drupal site
95 public function formatResourceUrl(&$url) {
97 $base = CRM_Core_Config
::singleton()->resourceBase
;
99 // Handle absolute urls
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
102 if (strpos($url, $base_url) === 0) {
104 $url = trim(str_replace($base_url, '', $url), '/');
106 // Handle relative urls that are within the CiviCRM module directory
107 elseif (strpos($url, $base) === 0) {
109 $url = $this->appendCoreDirectoryToResourceBase(substr(drupal_get_path('module', 'civicrm'), 0, -6)) . trim(substr($url, strlen($base)), '/');
111 // Strip query string
112 $q = strpos($url, '?');
113 if ($q && $internal) {
114 $url = substr($url, 0, $q);
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.
127 * Potential resource url based on standard folder assumptions.
129 * with civicrm-core directory appended if not standard civi dir
131 public function appendCoreDirectoryToResourceBase($url) {
132 global $civicrm_root;
133 $lastDirectory = basename($civicrm_root);
134 if ($lastDirectory != 'civicrm') {
135 return $url .= $lastDirectory . '/';
141 * Generate an internal CiviCRM URL (copied from DRUPAL/includes/common.inc#url)
152 $forceBackend = FALSE
154 $config = CRM_Core_Config
::singleton();
155 $script = 'index.php';
157 $path = CRM_Utils_String
::stripPathChars($path);
159 if (isset($fragment)) {
160 $fragment = '#' . $fragment;
163 if (!isset($config->useFrameworkRelativeBase
)) {
164 $base = parse_url($config->userFrameworkBaseURL
);
165 $config->useFrameworkRelativeBase
= $base['path'];
167 $base = $absolute ?
$config->userFrameworkBaseURL
: $config->useFrameworkRelativeBase
;
169 $separator = $htmlize ?
'&' : '&';
171 if (!$config->cleanURL
) {
174 return $base . $script . '?q=' . $path . $separator . $query . $fragment;
177 return $base . $script . '?q=' . $path . $fragment;
182 return $base . $script . '?' . $query . $fragment;
185 return $base . $fragment;
192 return $base . $path . '?' . $query . $fragment;
195 return $base . $path . $fragment;
200 return $base . $script . '?' . $query . $fragment;
203 return $base . $fragment;
212 public function getUserIDFromUserObject($user) {
213 return !empty($user->uid
) ?
$user->uid
: NULL;
219 public function setMessage($message) {
220 drupal_set_message($message);
226 public function getUniqueIdentifierFromUserObject($user) {
227 return empty($user->mail
) ?
NULL : $user->mail
;
233 public function getLoggedInUniqueIdentifier() {
235 return $this->getUniqueIdentifierFromUserObject($user);
241 public function permissionDenied() {
242 drupal_access_denied();
248 public function getUserRecordUrl($contactID) {
249 $uid = CRM_Core_BAO_UFMatch
::getUFId($contactID);
250 if (CRM_Core_Session
::singleton()
251 ->get('userID') == $contactID || CRM_Core_Permission
::checkAnyPerm(array(
252 'cms:administer users',
253 'cms:view user account',
256 return CRM_Utils_System
::url('user/' . $uid);
263 public function checkPermissionAddUser() {
264 return CRM_Core_Permission
::check('administer users');
270 public function logger($message) {
271 if (CRM_Core_Config
::singleton()->userFrameworkLogging
) {
272 watchdog('civicrm', '%message', array('%message' => $message), NULL, WATCHDOG_DEBUG
);
279 public function clearResourceCache() {
280 _drupal_flush_css_js();
284 * Append Drupal js to coreResourcesList.
286 public function appendCoreResources(&$list) {
287 $list[] = 'js/crm.drupal.js';
293 public function flush() {
294 drupal_flush_all_caches();
300 public function getModules() {
302 $q = db_query('SELECT name, status FROM {system} WHERE type = \'module\' AND schema_version <> -1');
303 foreach ($q as $row) {
304 $result[] = new CRM_Core_Module('drupal.' . $row->name
, ($row->status
== 1) ?
TRUE : FALSE);
310 * Find any users/roles/security-principals with the given permission
311 * and replace it with one or more permissions.
313 * @param string $oldPerm
314 * @param array $newPerms
319 public function replacePermission($oldPerm, $newPerms) {
320 $roles = user_roles(FALSE, $oldPerm);
321 if (!empty($roles)) {
322 foreach (array_keys($roles) as $rid) {
323 user_role_revoke_permissions($rid, array($oldPerm));
324 user_role_grant_permissions($rid, $newPerms);
332 public function languageNegotiationURL($url, $addLanguagePart = TRUE, $removeLanguagePart = FALSE) {
337 //CRM-7803 -from d7 onward.
338 $config = CRM_Core_Config
::singleton();
339 if (function_exists('variable_get') &&
340 module_exists('locale') &&
341 function_exists('language_negotiation_get')
345 //does user configuration allow language
346 //support from the URL (Path prefix or domain)
347 if (language_negotiation_get('language') == 'locale-url') {
348 $urlType = variable_get('locale_language_negotiation_url_part');
351 if ($urlType == LOCALE_LANGUAGE_NEGOTIATION_URL_PREFIX
) {
352 if (isset($language->prefix
) && $language->prefix
) {
353 if ($addLanguagePart) {
354 $url .= $language->prefix
. '/';
356 if ($removeLanguagePart) {
357 $url = str_replace("/{$language->prefix}/", '/', $url);
362 if ($urlType == LOCALE_LANGUAGE_NEGOTIATION_URL_DOMAIN
) {
363 if (isset($language->domain
) && $language->domain
) {
364 if ($addLanguagePart) {
365 $url = (CRM_Utils_System
::isSSL() ?
'https' : 'http') . '://' . $language->domain
. base_path();
367 if ($removeLanguagePart && defined('CIVICRM_UF_BASEURL')) {
368 $url = str_replace('\\', '/', $url);
369 $parseUrl = parse_url($url);
371 //kinda hackish but not sure how to do it right
372 //hope http_build_url() will help at some point.
373 if (is_array($parseUrl) && !empty($parseUrl)) {
374 $urlParts = explode('/', $url);
375 $hostKey = array_search($parseUrl['host'], $urlParts);
376 $ufUrlParts = parse_url(CIVICRM_UF_BASEURL
);
377 $urlParts[$hostKey] = $ufUrlParts['host'];
378 $url = implode('/', $urlParts);
391 public function getVersion() {
392 return defined('VERSION') ? VERSION
: 'Unknown';
398 public function updateCategories() {
399 // copied this from profile.module. Seems a bit inefficient, but i don't know a better way
407 public function getUFLocale() {
408 // return CiviCRM’s xx_YY locale that either matches Drupal’s Chinese locale
409 // (for CRM-6281), Drupal’s xx_YY or is retrieved based on Drupal’s xx
410 // sometimes for CLI based on order called, this might not be set and/or empty
413 if (empty($language)) {
417 if ($language->language
== 'zh-hans') {
421 if ($language->language
== 'zh-hant') {
425 if (preg_match('/^.._..$/', $language->language
)) {
426 return $language->language
;
429 return CRM_Core_I18n_PseudoConstant
::longForShort(substr($language->language
, 0, 2));
433 * Perform any post login activities required by the UF -
434 * e.g. for drupal: records a watchdog message about the new session, saves the login timestamp,
435 * calls hook_user op 'login' and generates a new session.
437 * @param array $params
439 * FIXME: Document values accepted/required by $params
441 public function userLoginFinalize($params = array()) {
442 user_login_finalize($params);
448 public function getLoginDestination(&$form) {
451 $id = $form->get('id');
456 $gid = $form->get('gid');
458 $args .= "&gid=$gid";
461 // Setup Personal Campaign Page link uses pageId
462 $pageId = $form->get('pageId');
464 $component = $form->get('component');
465 $args .= "&pageId=$pageId&component=$component&action=add";
472 // append destination so user is returned to form they came from after login
473 $destination = CRM_Utils_System
::currentPath() . '?reset=1' . $args;
479 * Fixme: Why are we overriding the parent function? Seems inconsistent.
480 * This version supplies slightly different params to $this->url (not absolute and html encoded) but why?
482 public function postURL($action) {
483 if (!empty($action)) {
486 return $this->url($_GET['q']);