INFRA-132 - Batch #7
[civicrm-core.git] / CRM / Utils / System / Base.php
CommitLineData
6a488035
TO
1<?php
2
3/**
4 * Base class for UF system integrations
5 */
6abstract class CRM_Utils_System_Base {
4caaa696 7 /**
100fef9d 8 * 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
4caaa696 9 * functions and leave the codebase oblivious to the type of CMS
7cdf0f5b 10 *
4caaa696
EM
11 * @deprecated
12 * @var bool
7cdf0f5b 13 * TRUE, if the CMS is Drupal.
4caaa696 14 */
6a488035 15 var $is_drupal = FALSE;
4caaa696
EM
16
17 /**
100fef9d 18 * Deprecated property to check if this is a joomla install. The correct method is to have functions on the UF classes for all UF specific
4caaa696 19 * functions and leave the codebase oblivious to the type of CMS
7cdf0f5b 20 *
4caaa696
EM
21 * @deprecated
22 * @var bool
7cdf0f5b 23 * TRUE, if the CMS is Joomla!.
4caaa696 24 */
6a488035 25 var $is_joomla = FALSE;
4caaa696 26
7cdf0f5b
AH
27 /**
28 * deprecated property to check if this is a wordpress install. The correct method is to have functions on the UF classes for all UF specific
29 * functions and leave the codebase oblivious to the type of CMS
30 *
31 * @deprecated
32 * @var bool
33 * TRUE, if the CMS is WordPress.
34 */
6a488035
TO
35 var $is_wordpress = FALSE;
36
e0dd98a5
EM
37 /**
38 * Does this CMS / UF support a CMS specific logging mechanism?
39 * @todo - we should think about offering up logging mechanisms in a way that is also extensible by extensions
40 * @var bool
41 */
42 var $supports_UF_Logging = FALSE;
43
7cdf0f5b
AH
44 /**
45 * @var bool
46 * TRUE, if the CMS allows CMS forms to be extended by hooks.
6a488035
TO
47 */
48 var $supports_form_extensions = FALSE;
49
50 /**
100fef9d 51 * If we are using a theming system, invoke theme, else just print the
6a488035
TO
52 * content
53 *
77855840
TO
54 * @param string $content
55 * The content that will be themed.
56 * @param bool $print
57 * Are we displaying to the screen or bypassing theming?.
58 * @param bool $maintenance
59 * For maintenance mode.
6a488035 60 *
7cdf0f5b
AH
61 * @throws Exception
62 * @return string|null
63 * NULL, If $print is FALSE, and some other criteria match up.
64 * The themed string, otherwise.
26659f0c
AH
65 *
66 * @todo The return value is inconsistent.
67 * @todo Better to always return, and never print.
6a488035 68 */
00be9182 69 public function theme(&$content, $print = FALSE, $maintenance = FALSE) {
6a488035
TO
70 $ret = FALSE;
71
72 // TODO: Split up; this was copied verbatim from CiviCRM 4.0's multi-UF theming function
73 // but the parts should be copied into cleaner subclass implementations
1e305b0b
DL
74 $config = CRM_Core_Config::singleton();
75 if (
76 $config->userSystem->is_drupal &&
77 function_exists('theme') &&
78 !$print
79 ) {
6a488035
TO
80 if ($maintenance) {
81 drupal_set_breadcrumb('');
82 drupal_maintenance_theme();
10221f8a
TO
83 if ($region = CRM_Core_Region::instance('html-header', FALSE)) {
84 CRM_Utils_System::addHTMLHead($region->render(''));
85 }
6a488035
TO
86 print theme('maintenance_page', array('content' => $content));
87 exit();
88 }
89 $ret = TRUE; // TODO: Figure out why D7 returns but everyone else prints
90 }
91 $out = $content;
92
93 $config = &CRM_Core_Config::singleton();
1e305b0b
DL
94 if (
95 !$print &&
6a488035
TO
96 $config->userFramework == 'WordPress'
97 ) {
aecede9a
AH
98 if (!function_exists('is_admin')) {
99 throw new \Exception('Function "is_admin()" is missing, even though WordPress is the user framework.');
100 }
101 if (!defined('ABSPATH')) {
102 throw new \Exception('Constant "ABSPATH" is not defined, even though WordPress is the user framework.');
103 }
6a488035 104 if (is_admin()) {
e7292422 105 require_once ABSPATH . 'wp-admin/admin-header.php';
6a488035
TO
106 }
107 else {
7cdf0f5b 108 // FIXME: we need to figure out to replace civicrm content on the frontend pages
6a488035
TO
109 }
110 }
111
112 if ($ret) {
113 return $out;
114 }
115 else {
116 print $out;
85c5f34c 117 return NULL;
6a488035
TO
118 }
119 }
120
bb3a214a
EM
121 /**
122 * @return string
123 */
00be9182 124 public function getDefaultBlockLocation() {
6a488035
TO
125 return 'left';
126 }
127
bb3a214a
EM
128 /**
129 * @return string
130 */
00be9182 131 public function getVersion() {
6a488035
TO
132 return 'Unknown';
133 }
134
135 /**
136 * Format the url as per language Negotiation.
137 *
138 * @param string $url
77b97be7
EM
139 * @param bool $addLanguagePart
140 * @param bool $removeLanguagePart
141 *
7cdf0f5b
AH
142 * @return string
143 * Formatted url.
6a488035
TO
144 */
145 function languageNegotiationURL(
146 $url,
147 $addLanguagePart = TRUE,
148 $removeLanguagePart = FALSE
149 ) {
150 return $url;
151 }
152
e29aefb4
TO
153 /**
154 * Determine the location of the CMS root.
155 *
7cdf0f5b
AH
156 * @return string|null
157 * Local file system path to CMS root, or NULL if it cannot be determined
6a488035 158 */
00be9182 159 public function cmsRootPath() {
e29aefb4 160 return NULL;
6a488035
TO
161 }
162
163 /**
164 * Get user login URL for hosting CMS (method declared in each CMS system class)
165 *
77855840
TO
166 * @param string $destination
167 * If present, add destination to querystring (works for Drupal only).
6a488035 168 *
a6c01b45
CW
169 * @return string
170 * loginURL for the current CMS
6a488035
TO
171 */
172 public abstract function getLoginURL($destination = '');
173
46b6363c
TO
174 /**
175 * Determine the native ID of the CMS user
176 *
100fef9d 177 * @param string $username
f4aaa82a
EM
178 *
179 * @throws CRM_Core_Exception
46b6363c
TO
180 * @return int|NULL
181 */
00be9182 182 public function getUfId($username) {
46b6363c
TO
183 $className = get_class($this);
184 throw new CRM_Core_Exception("Not implemented: {$className}->getUfId");
185 }
186
5d0eb86b
BS
187 /**
188 * Set a init session with user object
189 *
7cdf0f5b
AH
190 * @param array $data
191 * Array with user specific data
5d0eb86b 192 */
00be9182 193 public function setUserSession($data) {
5d0eb86b
BS
194 list($userID, $ufID) = $data;
195 $session = CRM_Core_Session::singleton();
196 $session->set('ufID', $ufID);
197 $session->set('userID', $userID);
198 }
d8a4acc0
C
199
200 /**
201 * Reset any system caches that may be required for proper CiviCRM
202 * integration.
203 */
00be9182 204 public function flush() {
d8a4acc0
C
205 // nullop by default
206 }
82d9c21e 207
f091327b
CW
208 /**
209 * Flush css/js caches
210 */
00be9182 211 public function clearResourceCache() {
f091327b
CW
212 // nullop by default
213 }
214
c8950569 215 /**
9977c6f5 216 * Return default Site Settings
f4aaa82a 217 *
7cdf0f5b 218 * @param string $dir
f4aaa82a 219 *
7cdf0f5b 220 * @return array
9977c6f5 221 * - $url, (Joomla - non admin url)
222 * - $siteName,
223 * - $siteRoot
224 */
00be9182 225 public function getDefaultSiteSettings($dir) {
9977c6f5 226 $config = CRM_Core_Config::singleton();
227 $url = $config->userFrameworkBaseURL;
228 return array($url, NULL, NULL);
229 }
c8950569 230
82d9c21e 231 /**
95d68223 232 * Perform any post login activities required by the CMS -
53980972 233 * e.g. for drupal: records a watchdog message about the new session, saves the login timestamp,
234 * calls hook_user op 'login' and generates a new session.
e43cc689 235 *
7cdf0f5b 236 * @param array $params
95d68223
TO
237 *
238 * FIXME: Document values accepted/required by $params
c8950569 239 */
9b873358 240 public function userLoginFinalize($params = array()) {
82d9c21e 241 }
5a604d61
E
242
243 /**
244 * Set timezone in mysql so that timestamp fields show the correct time
245 */
9b873358 246 public function setMySQLTimeZone() {
5a604d61 247 $timeZoneOffset = $this->getTimeZoneOffset();
9b873358 248 if ($timeZoneOffset) {
5a604d61
E
249 $sql = "SET time_zone = '$timeZoneOffset'";
250 CRM_Core_DAO::executequery($sql);
251 }
252 }
253
6491539b 254
5a604d61
E
255 /**
256 * Get timezone from CMS
7cdf0f5b
AH
257 *
258 * @return string|false|null
6491539b 259 */
9b873358 260 public function getTimeZoneOffset() {
6491539b 261 $timezone = $this->getTimeZoneString();
8b647c3a 262 if ($timezone) {
6491539b
DL
263 $tzObj = new DateTimeZone($timezone);
264 $dateTime = new DateTime("now", $tzObj);
265 $tz = $tzObj->getOffset($dateTime);
266
8b647c3a
AH
267 if (empty($tz)) {
268 return FALSE;
6491539b
DL
269 }
270
e7292422 271 $timeZoneOffset = sprintf("%02d:%02d", $tz / 3600, abs(($tz / 60) % 60));
6491539b 272
8b647c3a 273 if ($timeZoneOffset > 0) {
6491539b
DL
274 $timeZoneOffset = '+' . $timeZoneOffset;
275 }
276 return $timeZoneOffset;
277 }
85c5f34c 278 return NULL;
6491539b
DL
279 }
280
281 /**
282 * Over-ridable function to get timezone as a string eg.
7cdf0f5b
AH
283 *
284 * @return string
285 * Time zone, e.g. 'America/Los_Angeles'
6491539b 286 */
00be9182 287 public function getTimeZoneString() {
48ec57ab 288 return date_default_timezone_get();
5a604d61 289 }
2b617cb0
EM
290
291 /**
292 * Get Unique Identifier from UserFramework system (CMS)
77855840
TO
293 * @param object $user
294 * Object as described by the User Framework.
72b3a70c
CW
295 * @return mixed
296 * $uniqueIdentifer Unique identifier from the user Framework system
2b617cb0 297 */
e7292422
TO
298 public function getUniqueIdentifierFromUserObject($user) {
299 }
2b617cb0 300
32998c82
EM
301 /**
302 * Get User ID from UserFramework system (CMS)
77855840
TO
303 * @param object $user
304 * Object as described by the User Framework.
72b3a70c
CW
305 * @return mixed
306 * <NULL, number>
32998c82 307 */
e7292422
TO
308 public function getUserIDFromUserObject($user) {
309 }
32998c82
EM
310
311 /**
312 * Get currently logged in user uf id.
313 *
a6c01b45
CW
314 * @return int
315 * $userID logged in user uf id.
32998c82 316 */
e7292422
TO
317 public function getLoggedInUfID() {
318 }
32998c82 319
2b617cb0
EM
320 /**
321 * Get currently logged in user unique identifier - this tends to be the email address or user name.
322 *
a6c01b45
CW
323 * @return string
324 * logged in user unique identifier
2b617cb0 325 */
e7292422
TO
326 public function getLoggedInUniqueIdentifier() {
327 }
2b617cb0 328
32998c82 329 /**
100fef9d 330 * Return a UFID (user account ID from the UserFramework / CMS system being based on the user object
32998c82
EM
331 * passed, defaulting to the logged in user if not passed. Note that ambiguous situation occurs
332 * in CRM_Core_BAO_UFMatch::synchronize - a cleaner approach would seem to be resolving the user id before calling
333 * the function
334 *
335 * Note there is already a function getUFId which takes $username as a param - we could add $user
336 * as a second param to it but it seems messy - just overloading it because the name is taken
2b617cb0 337 * @param object $user
a6c01b45
CW
338 * @return int
339 * $ufid - user ID of UF System
32998c82 340 */
00be9182 341 public function getBestUFID($user = NULL) {
22e263ad 342 if ($user) {
32998c82
EM
343 return $this->getUserIDFromUserObject($user);
344 }
345 return $this->getLoggedInUfID();
346 }
2b617cb0
EM
347
348 /**
100fef9d 349 * Return a unique identifier (usually an email address or username) from the UserFramework / CMS system being based on the user object
2b617cb0
EM
350 * passed, defaulting to the logged in user if not passed. Note that ambiguous situation occurs
351 * in CRM_Core_BAO_UFMatch::synchronize - a cleaner approach would seem to be resolving the unique identifier before calling
352 * the function
353 *
354 * @param object $user
a6c01b45
CW
355 * @return string
356 * unique identifier from the UF System
2b617cb0 357 */
00be9182 358 public function getBestUFUniqueIdentifier($user = NULL) {
22e263ad 359 if ($user) {
2b617cb0
EM
360 return $this->getUniqueIdentifierFromUserObject($user);
361 }
362 return $this->getLoggedInUniqueIdentifier();
363 }
59f97da6
EM
364
365 /**
366 * Get Url to view user record
77855840
TO
367 * @param int $contactID
368 * Contact ID.
59f97da6
EM
369 *
370 * @return string
371 */
00be9182 372 public function getUserRecordUrl($contactID) {
59f97da6
EM
373 return NULL;
374 }
353ffa53 375
59f97da6
EM
376 /**
377 * Is the current user permitted to add a user
378 * @return bool
379 */
00be9182 380 public function checkPermissionAddUser() {
59f97da6
EM
381 return FALSE;
382 }
f85b1d20
EM
383
384 /**
100fef9d 385 * Output code from error function
f85b1d20
EM
386 * @param string $content
387 */
00be9182 388 public function outputError($content) {
f85b1d20
EM
389 echo CRM_Utils_System::theme($content);
390 }
e0dd98a5
EM
391
392 /**
393 * Log error to CMS
394 */
00be9182 395 public function logger($message) {
e0dd98a5
EM
396
397 }
f9f361d0
CW
398
399 /**
400 * Append to coreResourcesList
401 */
e7292422
TO
402 public function appendCoreResources(&$list) {
403 }
6a488035 404}