CRM-14850 tidy up ufLogging by declaring the capability on the class
[civicrm-core.git] / CRM / Utils / System / Base.php
1 <?php
2
3 /**
4 * Base class for UF system integrations
5 */
6 abstract class CRM_Utils_System_Base {
7 var $is_drupal = FALSE;
8 var $is_joomla = FALSE;
9 var $is_wordpress = FALSE;
10
11 /**
12 * Does this CMS / UF support a CMS specific logging mechanism?
13 * @todo - we should think about offering up logging mechanisms in a way that is also extensible by extensions
14 * @var bool
15 */
16 var $supports_UF_Logging = FALSE;
17
18 /*
19 * Does the CMS allow CMS forms to be extended by hooks
20 */
21 var $supports_form_extensions = FALSE;
22
23 /**
24 * if we are using a theming system, invoke theme, else just print the
25 * content
26 *
27 * @param string $content the content that will be themed
28 * @param boolean $print are we displaying to the screen or bypassing theming?
29 * @param boolean $maintenance for maintenance mode
30 *
31 * @return void prints content on stdout
32 * @access public
33 */
34 function theme(&$content, $print = FALSE, $maintenance = FALSE) {
35 $ret = FALSE;
36
37 // TODO: Split up; this was copied verbatim from CiviCRM 4.0's multi-UF theming function
38 // but the parts should be copied into cleaner subclass implementations
39 $config = CRM_Core_Config::singleton();
40 if (
41 $config->userSystem->is_drupal &&
42 function_exists('theme') &&
43 !$print
44 ) {
45 if ($maintenance) {
46 drupal_set_breadcrumb('');
47 drupal_maintenance_theme();
48 if ($region = CRM_Core_Region::instance('html-header', FALSE)) {
49 CRM_Utils_System::addHTMLHead($region->render(''));
50 }
51 print theme('maintenance_page', array('content' => $content));
52 exit();
53 }
54 $ret = TRUE; // TODO: Figure out why D7 returns but everyone else prints
55 }
56 $out = $content;
57
58 $config = &CRM_Core_Config::singleton();
59 if (
60 !$print &&
61 $config->userFramework == 'WordPress'
62 ) {
63 if (is_admin()) {
64 require_once (ABSPATH . 'wp-admin/admin-header.php');
65 }
66 else {
67 // FIX ME: we need to figure out to replace civicrm content on the frontend pages
68 }
69 }
70
71 if ($ret) {
72 return $out;
73 }
74 else {
75 print $out;
76 }
77 }
78
79 /**
80 * @return string
81 */
82 function getDefaultBlockLocation() {
83 return 'left';
84 }
85
86 /**
87 * @return string
88 */
89 function getVersion() {
90 return 'Unknown';
91 }
92
93 /**
94 * Format the url as per language Negotiation.
95 *
96 * @param string $url
97 *
98 * @param bool $addLanguagePart
99 * @param bool $removeLanguagePart
100 *
101 * @return string $url, formatted url.
102 * @static
103 */
104 function languageNegotiationURL(
105 $url,
106 $addLanguagePart = TRUE,
107 $removeLanguagePart = FALSE
108 ) {
109 return $url;
110 }
111
112 /**
113 * Determine the location of the CMS root.
114 *
115 * @return string|NULL local file system path to CMS root, or NULL if it cannot be determined
116 */
117 function cmsRootPath() {
118 return NULL;
119 }
120
121 /**
122 * Get user login URL for hosting CMS (method declared in each CMS system class)
123 *
124 * @param string $destination - if present, add destination to querystring (works for Drupal only)
125 *
126 * @return string - loginURL for the current CMS
127 * @static
128 */
129 public abstract function getLoginURL($destination = '');
130
131 /**
132 * Determine the native ID of the CMS user
133 *
134 * @param $username
135 *
136 * @throws CRM_Core_Exception
137 * @return int|NULL
138 */
139 function getUfId($username) {
140 $className = get_class($this);
141 throw new CRM_Core_Exception("Not implemented: {$className}->getUfId");
142 }
143
144 /**
145 * Set a init session with user object
146 *
147 * @param array $data array with user specific data
148 *
149 * @access public
150 */
151 function setUserSession($data) {
152 list($userID, $ufID) = $data;
153 $session = CRM_Core_Session::singleton();
154 $session->set('ufID', $ufID);
155 $session->set('userID', $userID);
156 }
157
158 /**
159 * Reset any system caches that may be required for proper CiviCRM
160 * integration.
161 */
162 function flush() {
163 // nullop by default
164 }
165
166 /**
167 * Return default Site Settings
168 *
169 * @param $dir
170 *
171 * @return array array
172 * - $url, (Joomla - non admin url)
173 * - $siteName,
174 * - $siteRoot
175 */
176 function getDefaultSiteSettings($dir) {
177 $config = CRM_Core_Config::singleton();
178 $url = $config->userFrameworkBaseURL;
179 return array($url, NULL, NULL);
180 }
181
182 /**
183 * Perform any post login activities required by the CMS -
184 * e.g. for drupal: records a watchdog message about the new session, saves the login timestamp,
185 * calls hook_user op 'login' and generates a new session.
186 *
187 * @param array params
188 *
189 * FIXME: Document values accepted/required by $params
190 */
191 function userLoginFinalize($params = array()){
192 }
193
194 /**
195 * Set timezone in mysql so that timestamp fields show the correct time
196 */
197 function setMySQLTimeZone(){
198 $timeZoneOffset = $this->getTimeZoneOffset();
199 if($timeZoneOffset){
200 $sql = "SET time_zone = '$timeZoneOffset'";
201 CRM_Core_DAO::executequery($sql);
202 }
203 }
204
205
206 /**
207 * Get timezone from CMS
208 * @return boolean|string
209 */
210 /**
211 * Get timezone from Drupal
212 * @return boolean|string
213 */
214 function getTimeZoneOffset(){
215 $timezone = $this->getTimeZoneString();
216 if($timezone){
217 $tzObj = new DateTimeZone($timezone);
218 $dateTime = new DateTime("now", $tzObj);
219 $tz = $tzObj->getOffset($dateTime);
220
221 if(empty($tz)){
222 return false;
223 }
224
225 $timeZoneOffset = sprintf("%02d:%02d", $tz / 3600, abs(($tz/60)%60));
226
227 if($timeZoneOffset > 0){
228 $timeZoneOffset = '+' . $timeZoneOffset;
229 }
230 return $timeZoneOffset;
231 }
232 }
233
234 /**
235 * Over-ridable function to get timezone as a string eg.
236 * @return string Timezone e.g. 'America/Los_Angeles'
237 */
238 function getTimeZoneString() {
239 return date_default_timezone_get();
240 }
241
242 /**
243 * Get Unique Identifier from UserFramework system (CMS)
244 * @param object $user object as described by the User Framework
245 * @return mixed $uniqueIdentifer Unique identifier from the user Framework system
246 *
247 */
248 function getUniqueIdentifierFromUserObject($user) {}
249
250 /**
251 * Get User ID from UserFramework system (CMS)
252 * @param object $user object as described by the User Framework
253 * @return mixed <NULL, number>
254 *
255 */
256 function getUserIDFromUserObject($user) {}
257
258 /**
259 * Get currently logged in user uf id.
260 *
261 * @return int $userID logged in user uf id.
262 */
263 function getLoggedInUfID() {}
264
265 /**
266 * Get currently logged in user unique identifier - this tends to be the email address or user name.
267 *
268 * @return string $userID logged in user unique identifier
269 */
270 function getLoggedInUniqueIdentifier() {}
271
272 /**
273 * return a UFID (user account ID from the UserFramework / CMS system being based on the user object
274 * passed, defaulting to the logged in user if not passed. Note that ambiguous situation occurs
275 * in CRM_Core_BAO_UFMatch::synchronize - a cleaner approach would seem to be resolving the user id before calling
276 * the function
277 *
278 * Note there is already a function getUFId which takes $username as a param - we could add $user
279 * as a second param to it but it seems messy - just overloading it because the name is taken
280 * @param object $user
281 * @return int $ufid - user ID of UF System
282 */
283 function getBestUFID($user = NULL) {
284 if($user) {
285 return $this->getUserIDFromUserObject($user);
286 }
287 return $this->getLoggedInUfID();
288 }
289
290 /**
291 * return a unique identifier (usually an email address or username) from the UserFramework / CMS system being based on the user object
292 * passed, defaulting to the logged in user if not passed. Note that ambiguous situation occurs
293 * in CRM_Core_BAO_UFMatch::synchronize - a cleaner approach would seem to be resolving the unique identifier before calling
294 * the function
295 *
296 * @param object $user
297 * @return string $uniqueIdentifier - unique identifier from the UF System
298 */
299 function getBestUFUniqueIdentifier($user = NULL) {
300 if($user) {
301 return $this->getUniqueIdentifierFromUserObject($user);
302 }
303 return $this->getLoggedInUniqueIdentifier();
304 }
305
306 /**
307 * Get Url to view user record
308 * @param integer $contactID Contact ID
309 *
310 * @return string
311 */
312 function getUserRecordUrl($contactID) {
313 return NULL;
314 }
315 /**
316 * Is the current user permitted to add a user
317 * @return bool
318 */
319 function checkPermissionAddUser() {
320 return FALSE;
321 }
322
323 /**
324 * output code from error function
325 * @param string $content
326 */
327 function outputError($content) {
328 echo CRM_Utils_System::theme($content);
329 }
330
331 /**
332 * Log error to CMS
333 */
334 function logger($message) {
335
336 }
337 }
338