INFRA-132 - Remove extra newlines from the bottom of docblocks
[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 /**
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
9 * functions and leave the codebase oblivious to the type of CMS
10 *
11 * @deprecated
12 * @var bool
13 * TRUE, if the CMS is Drupal.
14 */
15 var $is_drupal = FALSE;
16
17 /**
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
19 * functions and leave the codebase oblivious to the type of CMS
20 *
21 * @deprecated
22 * @var bool
23 * TRUE, if the CMS is Joomla!.
24 */
25 var $is_joomla = FALSE;
26
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 */
35 var $is_wordpress = FALSE;
36
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
44 /**
45 * @var bool
46 * TRUE, if the CMS allows CMS forms to be extended by hooks.
47 */
48 var $supports_form_extensions = FALSE;
49
50 /**
51 * If we are using a theming system, invoke theme, else just print the
52 * content
53 *
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.
60 *
61 * @throws Exception
62 * @return string|null
63 * NULL, If $print is FALSE, and some other criteria match up.
64 * The themed string, otherwise.
65 *
66 * @todo The return value is inconsistent.
67 * @todo Better to always return, and never print.
68 */
69 public function theme(&$content, $print = FALSE, $maintenance = FALSE) {
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
74 $config = CRM_Core_Config::singleton();
75 if (
76 $config->userSystem->is_drupal &&
77 function_exists('theme') &&
78 !$print
79 ) {
80 if ($maintenance) {
81 drupal_set_breadcrumb('');
82 drupal_maintenance_theme();
83 if ($region = CRM_Core_Region::instance('html-header', FALSE)) {
84 CRM_Utils_System::addHTMLHead($region->render(''));
85 }
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();
94 if (
95 !$print &&
96 $config->userFramework == 'WordPress'
97 ) {
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 }
104 if (is_admin()) {
105 require_once ABSPATH . 'wp-admin/admin-header.php';
106 }
107 else {
108 // FIXME: we need to figure out to replace civicrm content on the frontend pages
109 }
110 }
111
112 if ($ret) {
113 return $out;
114 }
115 else {
116 print $out;
117 return NULL;
118 }
119 }
120
121 /**
122 * @return string
123 */
124 public function getDefaultBlockLocation() {
125 return 'left';
126 }
127
128 /**
129 * @return string
130 */
131 public function getVersion() {
132 return 'Unknown';
133 }
134
135 /**
136 * Format the url as per language Negotiation.
137 *
138 * @param string $url
139 * @param bool $addLanguagePart
140 * @param bool $removeLanguagePart
141 *
142 * @return string
143 * Formatted url.
144 * @static
145 */
146 function languageNegotiationURL(
147 $url,
148 $addLanguagePart = TRUE,
149 $removeLanguagePart = FALSE
150 ) {
151 return $url;
152 }
153
154 /**
155 * Determine the location of the CMS root.
156 *
157 * @return string|null
158 * Local file system path to CMS root, or NULL if it cannot be determined
159 */
160 public function cmsRootPath() {
161 return NULL;
162 }
163
164 /**
165 * Get user login URL for hosting CMS (method declared in each CMS system class)
166 *
167 * @param string $destination
168 * If present, add destination to querystring (works for Drupal only).
169 *
170 * @return string
171 * loginURL for the current CMS
172 * @static
173 */
174 public abstract function getLoginURL($destination = '');
175
176 /**
177 * Determine the native ID of the CMS user
178 *
179 * @param string $username
180 *
181 * @throws CRM_Core_Exception
182 * @return int|NULL
183 */
184 public function getUfId($username) {
185 $className = get_class($this);
186 throw new CRM_Core_Exception("Not implemented: {$className}->getUfId");
187 }
188
189 /**
190 * Set a init session with user object
191 *
192 * @param array $data
193 * Array with user specific data
194 */
195 public function setUserSession($data) {
196 list($userID, $ufID) = $data;
197 $session = CRM_Core_Session::singleton();
198 $session->set('ufID', $ufID);
199 $session->set('userID', $userID);
200 }
201
202 /**
203 * Reset any system caches that may be required for proper CiviCRM
204 * integration.
205 */
206 public function flush() {
207 // nullop by default
208 }
209
210 /**
211 * Flush css/js caches
212 */
213 public function clearResourceCache() {
214 // nullop by default
215 }
216
217 /**
218 * Return default Site Settings
219 *
220 * @param string $dir
221 *
222 * @return array
223 * - $url, (Joomla - non admin url)
224 * - $siteName,
225 * - $siteRoot
226 */
227 public function getDefaultSiteSettings($dir) {
228 $config = CRM_Core_Config::singleton();
229 $url = $config->userFrameworkBaseURL;
230 return array($url, NULL, NULL);
231 }
232
233 /**
234 * Perform any post login activities required by the CMS -
235 * e.g. for drupal: records a watchdog message about the new session, saves the login timestamp,
236 * calls hook_user op 'login' and generates a new session.
237 *
238 * @param array $params
239 *
240 * FIXME: Document values accepted/required by $params
241 */
242 public function userLoginFinalize($params = array()) {
243 }
244
245 /**
246 * Set timezone in mysql so that timestamp fields show the correct time
247 */
248 public function setMySQLTimeZone() {
249 $timeZoneOffset = $this->getTimeZoneOffset();
250 if ($timeZoneOffset) {
251 $sql = "SET time_zone = '$timeZoneOffset'";
252 CRM_Core_DAO::executequery($sql);
253 }
254 }
255
256
257 /**
258 * Get timezone from CMS
259 *
260 * @return string|false|null
261 */
262 public function getTimeZoneOffset() {
263 $timezone = $this->getTimeZoneString();
264 if ($timezone) {
265 $tzObj = new DateTimeZone($timezone);
266 $dateTime = new DateTime("now", $tzObj);
267 $tz = $tzObj->getOffset($dateTime);
268
269 if (empty($tz)) {
270 return FALSE;
271 }
272
273 $timeZoneOffset = sprintf("%02d:%02d", $tz / 3600, abs(($tz / 60) % 60));
274
275 if ($timeZoneOffset > 0) {
276 $timeZoneOffset = '+' . $timeZoneOffset;
277 }
278 return $timeZoneOffset;
279 }
280 return NULL;
281 }
282
283 /**
284 * Over-ridable function to get timezone as a string eg.
285 *
286 * @return string
287 * Time zone, e.g. 'America/Los_Angeles'
288 */
289 public function getTimeZoneString() {
290 return date_default_timezone_get();
291 }
292
293 /**
294 * Get Unique Identifier from UserFramework system (CMS)
295 * @param object $user
296 * Object as described by the User Framework.
297 * @return mixed $uniqueIdentifer Unique identifier from the user Framework system
298 */
299 public function getUniqueIdentifierFromUserObject($user) {
300 }
301
302 /**
303 * Get User ID from UserFramework system (CMS)
304 * @param object $user
305 * Object as described by the User Framework.
306 * @return mixed <NULL, number>
307 */
308 public function getUserIDFromUserObject($user) {
309 }
310
311 /**
312 * Get currently logged in user uf id.
313 *
314 * @return int
315 * $userID logged in user uf id.
316 */
317 public function getLoggedInUfID() {
318 }
319
320 /**
321 * Get currently logged in user unique identifier - this tends to be the email address or user name.
322 *
323 * @return string
324 * logged in user unique identifier
325 */
326 public function getLoggedInUniqueIdentifier() {
327 }
328
329 /**
330 * Return a UFID (user account ID from the UserFramework / CMS system being based on the user object
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
337 * @param object $user
338 * @return int
339 * $ufid - user ID of UF System
340 */
341 public function getBestUFID($user = NULL) {
342 if ($user) {
343 return $this->getUserIDFromUserObject($user);
344 }
345 return $this->getLoggedInUfID();
346 }
347
348 /**
349 * Return a unique identifier (usually an email address or username) from the UserFramework / CMS system being based on the user object
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
355 * @return string
356 * unique identifier from the UF System
357 */
358 public function getBestUFUniqueIdentifier($user = NULL) {
359 if ($user) {
360 return $this->getUniqueIdentifierFromUserObject($user);
361 }
362 return $this->getLoggedInUniqueIdentifier();
363 }
364
365 /**
366 * Get Url to view user record
367 * @param int $contactID
368 * Contact ID.
369 *
370 * @return string
371 */
372 public function getUserRecordUrl($contactID) {
373 return NULL;
374 }
375 /**
376 * Is the current user permitted to add a user
377 * @return bool
378 */
379 public function checkPermissionAddUser() {
380 return FALSE;
381 }
382
383 /**
384 * Output code from error function
385 * @param string $content
386 */
387 public function outputError($content) {
388 echo CRM_Utils_System::theme($content);
389 }
390
391 /**
392 * Log error to CMS
393 */
394 public function logger($message) {
395
396 }
397
398 /**
399 * Append to coreResourcesList
400 */
401 public function appendCoreResources(&$list) {
402 }
403 }