CRM-14850 tidy up ufLogging by declaring the capability on the class
[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 $this->is_drupal = TRUE;
52 $this->supports_form_extensions = TRUE;
53 }
54
55 /**
56 * @param string dir base civicrm directory
57 * Return default Site Settings
58 * @return array array
59 * - $url, (Joomla - non admin url)
60 * - $siteName,
61 * - $siteRoot
62 */
63 function getDefaultSiteSettings($dir){
64 $config = CRM_Core_Config::singleton();
65 $siteName = $siteRoot = NULL;
66 $matches = array();
67 if (preg_match(
68 '|/sites/([\w\.\-\_]+)/|',
69 $config->templateCompileDir,
70 $matches
71 )) {
72 $siteName = $matches[1];
73 if ($siteName) {
74 $siteName = "/sites/$siteName/";
75 $siteNamePos = strpos($dir, $siteName);
76 if ($siteNamePos !== FALSE) {
77 $siteRoot = substr($dir, 0, $siteNamePos);
78 }
79 }
80 }
81 $url = $config->userFrameworkBaseURL;
82 return array($url, $siteName, $siteRoot);
83 }
84
85 /**
86 * Check if a resource url is within the drupal directory and format appropriately
87 *
88 * @param url (reference)
89 *
90 * @return bool: TRUE for internal paths, FALSE for external. The drupal_add_js fn is able to add js more
91 * efficiently if it is known to be in the drupal site
92 */
93 function formatResourceUrl(&$url) {
94 $internal = FALSE;
95 $base = CRM_Core_Config::singleton()->resourceBase;
96 global $base_url;
97 // Handle absolute urls
98 // 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)
99 // to see if the url is within our drupal dir, if it is we are able to treated it as an internal url
100 if (strpos($url, $base_url) === 0) {
101 $internal = TRUE;
102 $url = trim(str_replace($base_url, '', $url), '/');
103 }
104 // Handle relative urls that are within the CiviCRM module directory
105 elseif (strpos($url, $base) === 0) {
106 $internal = TRUE;
107 $url = $this->appendCoreDirectoryToResourceBase(substr(drupal_get_path('module', 'civicrm'), 0, -6)) . trim(substr($url, strlen($base)), '/');
108 }
109 // Strip query string
110 $q = strpos($url, '?');
111 if ($q && $internal) {
112 $url = substr($url, 0, $q);
113 }
114 return $internal;
115 }
116
117 /**
118 * In instance where civicrm folder has a drupal folder & a civicrm core folder @ the same level append the
119 * civicrm folder name to the url
120 * See CRM-13737 for discussion of how this allows implementers to alter the folder structure
121 * @todo - this only provides a limited amount of flexiblity - it still expects a 'civicrm' folder with a 'drupal' folder
122 * and is only flexible as to the name of the civicrm folder.
123 *
124 * @param string $url potential resource url based on standard folder assumptions
125 * @return string $url with civicrm-core directory appended if not standard civi dir
126 */
127 function appendCoreDirectoryToResourceBase($url) {
128 global $civicrm_root;
129 $lastDirectory = basename($civicrm_root);
130 if($lastDirectory != 'civicrm') {
131 return $url .= $lastDirectory . '/';
132 }
133 return $url;
134 }
135
136 /**
137 * Generate an internal CiviCRM URL (copied from DRUPAL/includes/common.inc#url)
138 *
139 * @param $path string The path being linked to, such as "civicrm/add"
140 * @param $query string A query string to append to the link.
141 * @param $absolute boolean Whether to force the output to be an absolute link (beginning with http:).
142 * Useful for links that will be displayed outside the site, such as in an
143 * RSS feed.
144 * @param $fragment string A fragment identifier (named anchor) to append to the link.
145 * @param $htmlize boolean whether to convert to html eqivalant
146 * @param $frontend boolean a gross joomla hack
147 * @param $forceBackend boolean a gross joomla hack
148 *
149 * @return string an HTML string containing a link to the given path.
150 * @access public
151 *
152 */
153 function url($path = NULL, $query = NULL, $absolute = FALSE,
154 $fragment = NULL, $htmlize = TRUE,
155 $frontend = FALSE, $forceBackend = FALSE
156 ) {
157 $config = CRM_Core_Config::singleton();
158 $script = 'index.php';
159
160 $path = CRM_Utils_String::stripPathChars($path);
161
162 if (isset($fragment)) {
163 $fragment = '#' . $fragment;
164 }
165
166 if (!isset($config->useFrameworkRelativeBase)) {
167 $base = parse_url($config->userFrameworkBaseURL);
168 $config->useFrameworkRelativeBase = $base['path'];
169 }
170 $base = $absolute ? $config->userFrameworkBaseURL : $config->useFrameworkRelativeBase;
171
172 $separator = $htmlize ? '&amp;' : '&';
173
174 if (!$config->cleanURL) {
175 if (isset($path)) {
176 if (isset($query)) {
177 return $base . $script . '?q=' . $path . $separator . $query . $fragment;
178 }
179 else {
180 return $base . $script . '?q=' . $path . $fragment;
181 }
182 }
183 else {
184 if (isset($query)) {
185 return $base . $script . '?' . $query . $fragment;
186 }
187 else {
188 return $base . $fragment;
189 }
190 }
191 }
192 else {
193 if (isset($path)) {
194 if (isset($query)) {
195 return $base . $path . '?' . $query . $fragment;
196 }
197 else {
198 return $base . $path . $fragment;
199 }
200 }
201 else {
202 if (isset($query)) {
203 return $base . $script . '?' . $query . $fragment;
204 }
205 else {
206 return $base . $fragment;
207 }
208 }
209 }
210 }
211
212 /**
213 * Get User ID from UserFramework system (Drupal)
214 * @param object $user object as described by the CMS
215 * @return mixed <NULL, number>
216 */
217 function getUserIDFromUserObject($user) {
218 return !empty($user->uid) ? $user->uid : NULL;
219 }
220
221 /**
222 * Get Unique Identifier from UserFramework system (CMS)
223 * @param object $user object as described by the User Framework
224 * @return mixed $uniqueIdentifer Unique identifier from the user Framework system
225 *
226 */
227 function getUniqueIdentifierFromUserObject($user) {
228 return empty($user->mail) ? NULL : $user->mail;
229 }
230
231 /**
232 * Get currently logged in user unique identifier - this tends to be the email address or user name.
233 *
234 * @return string $userID logged in user unique identifier
235 */
236 function getLoggedInUniqueIdentifier() {
237 global $user;
238 return $this->getUniqueIdentifierFromUserObject($user);
239 }
240
241 /**
242 * Action to take when access is not permitted
243 */
244 function permissionDenied() {
245 drupal_access_denied();
246 }
247
248 /**
249 * Get Url to view user record
250 * @param integer $contactID Contact ID
251 *
252 * @return string
253 */
254 function getUserRecordUrl($contactID) {
255 $uid = CRM_Core_BAO_UFMatch::getUFId($contactID);
256 if (CRM_Core_Session::singleton()->get('userID') == $contactID || CRM_Core_Permission::checkAnyPerm(array('cms:administer users', 'cms:view user account'))) {
257 return CRM_Utils_System::url('user/' . $uid);
258 };
259 }
260
261 /**
262 * Is the current user permitted to add a user
263 * @return bool
264 */
265 function checkPermissionAddUser() {
266 if (CRM_Core_Permission::check('administer users')) {
267 return TRUE;
268 }
269 }
270
271
272 /**
273 * Log error to CMS
274 */
275 function logger($message) {
276 if (CRM_Core_Config::singleton()->userFrameworkLogging) {
277 watchdog('civicrm', $message, NULL, WATCHDOG_DEBUG);
278 }
279 }
280 }