3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
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) {
103 $file = trim(str_replace($base_url, '', $url), '/');
104 // CRM-18130: Custom CSS URL not working if aliased or rewritten
105 if (file_exists(DRUPAL_ROOT
. $file)) {
110 // Handle relative urls that are within the CiviCRM module directory
111 elseif (strpos($url, $base) === 0) {
113 $url = $this->appendCoreDirectoryToResourceBase(dirname(drupal_get_path('module', 'civicrm')) . '/') . trim(substr($url, strlen($base)), '/');
115 // Strip query string
116 $q = strpos($url, '?');
117 if ($q && $internal) {
118 $url = substr($url, 0, $q);
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.
131 * Potential resource url based on standard folder assumptions.
133 * with civicrm-core directory appended if not standard civi dir
135 public function appendCoreDirectoryToResourceBase($url) {
136 global $civicrm_root;
137 $lastDirectory = basename($civicrm_root);
138 if ($lastDirectory != 'civicrm') {
139 return $url .= $lastDirectory . '/';
145 * Generate an internal CiviCRM URL (copied from DRUPAL/includes/common.inc#url)
155 $forceBackend = FALSE
157 $config = CRM_Core_Config
::singleton();
158 $script = 'index.php';
160 $path = CRM_Utils_String
::stripPathChars($path);
162 if (isset($fragment)) {
163 $fragment = '#' . $fragment;
166 $base = $absolute ?
$config->userFrameworkBaseURL
: $config->useFrameworkRelativeBase
;
170 if (!$config->cleanURL
) {
173 return $base . $script . '?q=' . $path . $separator . $query . $fragment;
176 return $base . $script . '?q=' . $path . $fragment;
181 return $base . $script . '?' . $query . $fragment;
184 return $base . $fragment;
191 return $base . $path . '?' . $query . $fragment;
194 return $base . $path . $fragment;
199 return $base . $script . '?' . $query . $fragment;
202 return $base . $fragment;
211 public function getUserIDFromUserObject($user) {
212 return !empty($user->uid
) ?
$user->uid
: NULL;
218 public function setMessage($message) {
219 drupal_set_message($message);
225 public function getUniqueIdentifierFromUserObject($user) {
226 return empty($user->mail
) ?
NULL : $user->mail
;
232 public function getLoggedInUniqueIdentifier() {
234 return $this->getUniqueIdentifierFromUserObject($user);
240 public function permissionDenied() {
241 drupal_access_denied();
247 public function getUserRecordUrl($contactID) {
248 $uid = CRM_Core_BAO_UFMatch
::getUFId($contactID);
249 if (CRM_Core_Session
::singleton()
250 ->get('userID') == $contactID || CRM_Core_Permission
::checkAnyPerm(array(
251 'cms:administer users',
252 'cms:view user account',
255 return CRM_Utils_System
::url('user/' . $uid);
262 public function checkPermissionAddUser() {
263 return CRM_Core_Permission
::check('administer users');
269 public function logger($message) {
270 if (CRM_Core_Config
::singleton()->userFrameworkLogging
&& function_exists('watchdog')) {
271 watchdog('civicrm', '%message', array('%message' => $message), NULL, WATCHDOG_DEBUG
);
278 public function clearResourceCache() {
279 _drupal_flush_css_js();
283 * Append Drupal js to coreResourcesList.
287 public function appendCoreResources(&$list) {
288 $list[] = 'js/crm.drupal.js';
294 public function flush() {
295 drupal_flush_all_caches();
301 public function getModules() {
303 $q = db_query('SELECT name, status FROM {system} WHERE type = \'module\' AND schema_version <> -1');
304 foreach ($q as $row) {
305 $result[] = new CRM_Core_Module('drupal.' . $row->name
, ($row->status
== 1) ?
TRUE : FALSE);
311 * Find any users/roles/security-principals with the given permission
312 * and replace it with one or more permissions.
314 * @param string $oldPerm
315 * @param array $newPerms
320 public function replacePermission($oldPerm, $newPerms) {
321 $roles = user_roles(FALSE, $oldPerm);
322 if (!empty($roles)) {
323 foreach (array_keys($roles) as $rid) {
324 user_role_revoke_permissions($rid, array($oldPerm));
325 user_role_grant_permissions($rid, $newPerms);
333 public function languageNegotiationURL($url, $addLanguagePart = TRUE, $removeLanguagePart = FALSE) {
338 //CRM-7803 -from d7 onward.
339 $config = CRM_Core_Config
::singleton();
340 if (function_exists('variable_get') &&
341 module_exists('locale') &&
342 function_exists('language_negotiation_get')
346 //does user configuration allow language
347 //support from the URL (Path prefix or domain)
348 if (language_negotiation_get('language') == 'locale-url') {
349 $urlType = variable_get('locale_language_negotiation_url_part');
352 if ($urlType == LOCALE_LANGUAGE_NEGOTIATION_URL_PREFIX
) {
353 if (isset($language->prefix
) && $language->prefix
) {
354 if ($addLanguagePart) {
355 $url .= $language->prefix
. '/';
357 if ($removeLanguagePart) {
358 $url = str_replace("/{$language->prefix}/", '/', $url);
363 if ($urlType == LOCALE_LANGUAGE_NEGOTIATION_URL_DOMAIN
) {
364 if (isset($language->domain
) && $language->domain
) {
365 if ($addLanguagePart) {
366 $url = (CRM_Utils_System
::isSSL() ?
'https' : 'http') . '://' . $language->domain
. base_path();
368 if ($removeLanguagePart && defined('CIVICRM_UF_BASEURL')) {
369 $url = str_replace('\\', '/', $url);
370 $parseUrl = parse_url($url);
372 //kinda hackish but not sure how to do it right
373 //hope http_build_url() will help at some point.
374 if (is_array($parseUrl) && !empty($parseUrl)) {
375 $urlParts = explode('/', $url);
376 $hostKey = array_search($parseUrl['host'], $urlParts);
377 $ufUrlParts = parse_url(CIVICRM_UF_BASEURL
);
378 $urlParts[$hostKey] = $ufUrlParts['host'];
379 $url = implode('/', $urlParts);
392 public function getVersion() {
393 return defined('VERSION') ? VERSION
: 'Unknown';
399 public function updateCategories() {
400 // copied this from profile.module. Seems a bit inefficient, but i don't know a better way
408 public function getUFLocale() {
409 // return CiviCRM’s xx_YY locale that either matches Drupal’s Chinese locale
410 // (for CRM-6281), Drupal’s xx_YY or is retrieved based on Drupal’s xx
411 // sometimes for CLI based on order called, this might not be set and/or empty
414 if (empty($language)) {
418 if ($language->language
== 'zh-hans') {
422 if ($language->language
== 'zh-hant') {
426 if (preg_match('/^.._..$/', $language->language
)) {
427 return $language->language
;
430 return CRM_Core_I18n_PseudoConstant
::longForShort(substr($language->language
, 0, 2));
436 public function setUFLocale($civicrm_language) {
439 $langcode = substr($civicrm_language, 0, 2);
440 $languages = language_list();
442 if (isset($languages[$langcode])) {
443 $language = $languages[$langcode];
445 // Config must be re-initialized to reset the base URL
446 // otherwise links will have the wrong language prefix/domain.
447 $config = CRM_Core_Config
::singleton();
457 * Perform any post login activities required by the UF -
458 * e.g. for drupal: records a watchdog message about the new session, saves the login timestamp,
459 * calls hook_user op 'login' and generates a new session.
461 * @param array $params
463 * FIXME: Document values accepted/required by $params
465 public function userLoginFinalize($params = array()) {
466 user_login_finalize($params);
472 public function getLoginDestination(&$form) {
475 $id = $form->get('id');
480 $gid = $form->get('gid');
482 $args .= "&gid=$gid";
485 // Setup Personal Campaign Page link uses pageId
486 $pageId = $form->get('pageId');
488 $component = $form->get('component');
489 $args .= "&pageId=$pageId&component=$component&action=add";
496 // append destination so user is returned to form they came from after login
497 $destination = CRM_Utils_System
::currentPath() . '?reset=1' . $args;
503 * Fixme: Why are we overriding the parent function? Seems inconsistent.
504 * This version supplies slightly different params to $this->url (not absolute and html encoded) but why?
506 * @param string $action
510 public function postURL($action) {
511 if (!empty($action)) {
514 return $this->url($_GET['q']);
518 * Get an array of user details for a contact, containing at minimum the user ID & name.
520 * @param int $contactID
523 * CMS user details including
525 * - name (ie the system user name.
527 public function getUser($contactID) {
528 $userDetails = parent
::getUser($contactID);
529 $user = $this->getUserObject($userDetails['id']);
530 $userDetails['name'] = $user->name
;
531 $userDetails['email'] = $user->mail
;
536 * Load the user object.
538 * Note this function still works in drupal 6, 7 & 8 but is deprecated in Drupal 8.
544 public function getUserObject($userID) {
545 return user_load($userID);
548 public function parseDrupalSiteName($civicrm_root) {
550 if (strpos($civicrm_root,
551 DIRECTORY_SEPARATOR
. 'sites' . DIRECTORY_SEPARATOR
. 'all' . DIRECTORY_SEPARATOR
. 'modules'
554 $startPos = strpos($civicrm_root,
555 DIRECTORY_SEPARATOR
. 'sites' . DIRECTORY_SEPARATOR
557 $endPos = strpos($civicrm_root,
558 DIRECTORY_SEPARATOR
. 'modules' . DIRECTORY_SEPARATOR
560 if ($startPos && $endPos) {
561 // if component is in sites/SITENAME/modules
562 $siteName = substr($civicrm_root,
564 $endPos - $startPos - 7