Merge commit '762fe6d92bc' into 4.5-missing-prs
[civicrm-core.git] / CRM / Utils / System / DrupalBase.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
31 * @copyright CiviCRM LLC (c) 2004-2014
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 {
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;
47 /**
48 *
49 */
50 function __construct() {
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 */
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 */
69 function getDefaultSiteSettings($dir){
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 }
90
91 /**
92 * Check if a resource url is within the drupal directory and format appropriately
93 *
94 * @param url (reference)
95 *
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
98 */
99 function formatResourceUrl(&$url) {
100 $internal = FALSE;
101 $base = CRM_Core_Config::singleton()->resourceBase;
102 global $base_url;
103 // Handle absolute urls
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
106 if (strpos($url, $base_url) === 0) {
107 $internal = TRUE;
108 $url = trim(str_replace($base_url, '', $url), '/');
109 }
110 // Handle relative urls that are within the CiviCRM module directory
111 elseif (strpos($url, $base) === 0) {
112 $internal = TRUE;
113 $url = $this->appendCoreDirectoryToResourceBase(substr(drupal_get_path('module', 'civicrm'), 0, -6)) . trim(substr($url, strlen($base)), '/');
114 }
115 // Strip query string
116 $q = strpos($url, '?');
117 if ($q && $internal) {
118 $url = substr($url, 0, $q);
119 }
120 return $internal;
121 }
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 */
133 function appendCoreDirectoryToResourceBase($url) {
134 global $civicrm_root;
135 $lastDirectory = basename($civicrm_root);
136 if($lastDirectory != 'civicrm') {
137 return $url .= $lastDirectory . '/';
138 }
139 return $url;
140 }
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.
156 * @access public
157 *
158 */
159 function url($path = NULL, $query = NULL, $absolute = FALSE,
160 $fragment = NULL, $htmlize = TRUE,
161 $frontend = FALSE, $forceBackend = FALSE
162 ) {
163 $config = CRM_Core_Config::singleton();
164 $script = 'index.php';
165
166 $path = CRM_Utils_String::stripPathChars($path);
167
168 if (isset($fragment)) {
169 $fragment = '#' . $fragment;
170 }
171
172 if (!isset($config->useFrameworkRelativeBase)) {
173 $base = parse_url($config->userFrameworkBaseURL);
174 $config->useFrameworkRelativeBase = $base['path'];
175 }
176 $base = $absolute ? $config->userFrameworkBaseURL : $config->useFrameworkRelativeBase;
177
178 $separator = $htmlize ? '&amp;' : '&';
179
180 if (!$config->cleanURL) {
181 if (isset($path)) {
182 if (isset($query)) {
183 return $base . $script . '?q=' . $path . $separator . $query . $fragment;
184 }
185 else {
186 return $base . $script . '?q=' . $path . $fragment;
187 }
188 }
189 else {
190 if (isset($query)) {
191 return $base . $script . '?' . $query . $fragment;
192 }
193 else {
194 return $base . $fragment;
195 }
196 }
197 }
198 else {
199 if (isset($path)) {
200 if (isset($query)) {
201 return $base . $path . '?' . $query . $fragment;
202 }
203 else {
204 return $base . $path . $fragment;
205 }
206 }
207 else {
208 if (isset($query)) {
209 return $base . $script . '?' . $query . $fragment;
210 }
211 else {
212 return $base . $fragment;
213 }
214 }
215 }
216 }
217
218 /**
219 * Get User ID from UserFramework system (Drupal)
220 * @param object $user object as described by the CMS
221 * @return mixed <NULL, number>
222 */
223 function getUserIDFromUserObject($user) {
224 return !empty($user->uid) ? $user->uid : NULL;
225 }
226
227 /**
228 * Get Unique Identifier from UserFramework system (CMS)
229 * @param object $user object as described by the User Framework
230 * @return mixed $uniqueIdentifer Unique identifier from the user Framework system
231 *
232 */
233 function getUniqueIdentifierFromUserObject($user) {
234 return empty($user->mail) ? NULL : $user->mail;
235 }
236
237 /**
238 * Get currently logged in user unique identifier - this tends to be the email address or user name.
239 *
240 * @return string $userID logged in user unique identifier
241 */
242 function getLoggedInUniqueIdentifier() {
243 global $user;
244 return $this->getUniqueIdentifierFromUserObject($user);
245 }
246
247 /**
248 * Action to take when access is not permitted
249 */
250 function permissionDenied() {
251 drupal_access_denied();
252 }
253
254 /**
255 * Get Url to view user record
256 * @param integer $contactID Contact ID
257 *
258 * @return string
259 */
260 function getUserRecordUrl($contactID) {
261 $uid = CRM_Core_BAO_UFMatch::getUFId($contactID);
262 if (CRM_Core_Session::singleton()->get('userID') == $contactID || CRM_Core_Permission::checkAnyPerm(array('cms:administer users', 'cms:view user account'))) {
263 return CRM_Utils_System::url('user/' . $uid);
264 };
265 }
266
267 /**
268 * Is the current user permitted to add a user
269 * @return bool
270 */
271 function checkPermissionAddUser() {
272 if (CRM_Core_Permission::check('administer users')) {
273 return TRUE;
274 }
275 }
276
277
278 /**
279 * Log error to CMS
280 */
281 function logger($message) {
282 if (CRM_Core_Config::singleton()->userFrameworkLogging) {
283 watchdog('civicrm', '%message', array('%message' => $message), NULL, WATCHDOG_DEBUG);
284 }
285 }
286
287 /**
288 * Flush css/js caches
289 */
290 function clearResourceCache() {
291 _drupal_flush_css_js();
292 }
293
294 /**
295 * Append to coreResourcesList
296 */
297 function appendCoreResources(&$list) {
298 $list[] = 'js/crm.drupal.js';
299 }
300 }