Merge pull request #6069 from eileenmcnaughton/CRM-16627
[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
17f443df 50 /**
fe482240 51 * Append an additional breadcrumb tag to the existing breadcrumb.
17f443df
CW
52 *
53 * @param array $breadCrumbs
54 */
55 public function appendBreadCrumb($breadCrumbs) {
56 }
57
58 /**
fe482240 59 * Reset an additional breadcrumb tag to the existing breadcrumb.
17f443df
CW
60 */
61 public function resetBreadCrumb() {
62 }
63
64 /**
fe482240 65 * Append a string to the head of the html file.
17f443df
CW
66 *
67 * @param string $head
68 * The new string to be appended.
69 */
70 public function addHTMLHead($head) {
71 }
72
73 /**
fe482240 74 * Rewrite various system urls to https.
17f443df
CW
75 */
76 public function mapConfigToSSL() {
77 // dont need to do anything, let CMS handle their own switch to SSL
78 }
79
80 /**
fe482240 81 * Figure out the post url for QuickForm.
17f443df
CW
82 *
83 * @param string $action
84 * The default url if one is pre-specified.
85 *
86 * @return string
87 * The url to post the form.
88 */
89 public function postURL($action) {
90 $config = CRM_Core_Config::singleton();
91 if (!empty($action)) {
92 return $action;
93 }
94
95 return $this->url(CRM_Utils_Array::value($config->userFrameworkURLVar, $_GET),
96 NULL, TRUE, NULL, FALSE
97 );
98 }
99
100 /**
101 * Generate the url string to a CiviCRM path.
102 *
103 * @param string $path
104 * The path being linked to, such as "civicrm/add".
105 * @param string $query
106 * A query string to append to the link.
107 * @param bool $absolute
108 * Whether to force the output to be an absolute link (beginning with http).
109 * Useful for links that will be displayed outside the site, such as in an RSS feed.
110 * @param string $fragment
111 * A fragment identifier (named anchor) to append to the link.
112 * @param bool $htmlize
113 * Whether to encode special html characters such as &.
114 * @param bool $frontend
115 * This link should be to the CMS front end (applies to WP & Joomla).
116 * @param bool $forceBackend
117 * This link should be to the CMS back end (applies to WP & Joomla).
118 *
119 * @return string
120 */
121 public function url(
122 $path = NULL,
123 $query = NULL,
124 $absolute = FALSE,
125 $fragment = NULL,
126 $htmlize = TRUE,
127 $frontend = FALSE,
128 $forceBackend = FALSE
129 ) {
130 return NULL;
131 }
132
133 /**
fe482240 134 * Authenticate the user against the CMS db.
17f443df
CW
135 *
136 * @param string $name
137 * The user name.
138 * @param string $password
139 * The password for the above user.
140 * @param bool $loadCMSBootstrap
141 * Load cms bootstrap?.
142 * @param string $realPath
143 * Filename of script
144 *
145 * @return array|bool
146 * [contactID, ufID, unique string] else false if no auth
147 */
148 public function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) {
149 return FALSE;
150 }
151
152 /**
fe482240 153 * Set a message in the CMS to display to a user.
17f443df
CW
154 *
155 * @param string $message
156 * The message to set.
157 */
158 public function setMessage($message) {
159 }
160
161 /**
fe482240 162 * Load user into session.
17f443df
CW
163 *
164 * @param $user
165 *
166 * @return bool
167 */
168 public function loadUser($user) {
169 return TRUE;
170 }
171
172 /**
173 * Immediately stop script execution and display a 401 "Access Denied" page
174 */
175 public function permissionDenied() {
176 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
177 }
178
179 /**
180 * Immediately stop script execution, log out the user and redirect to the home page
181 *
182 * @deprecated
183 * This function should be removed in favor of linking to the CMS's logout page
184 */
185 public function logout() {
186 }
187
188 /**
189 * Clear CMS caches related to the user registration/profile forms.
190 * Used when updating/embedding profiles on CMS user forms.
191 * @see CRM-3600
192 */
193 public function updateCategories() {
194 }
195
196 /**
fe482240 197 * Get the locale set in the CMS.
17f443df
CW
198 *
199 * @return string|null
200 * Locale or null for none
201 */
202 public function getUFLocale() {
203 return NULL;
204 }
205
6a488035 206 /**
100fef9d 207 * If we are using a theming system, invoke theme, else just print the
6a488035
TO
208 * content
209 *
77855840
TO
210 * @param string $content
211 * The content that will be themed.
212 * @param bool $print
213 * Are we displaying to the screen or bypassing theming?.
214 * @param bool $maintenance
215 * For maintenance mode.
6a488035 216 *
7cdf0f5b
AH
217 * @throws Exception
218 * @return string|null
219 * NULL, If $print is FALSE, and some other criteria match up.
220 * The themed string, otherwise.
26659f0c
AH
221 *
222 * @todo The return value is inconsistent.
223 * @todo Better to always return, and never print.
6a488035 224 */
00be9182 225 public function theme(&$content, $print = FALSE, $maintenance = FALSE) {
6a488035
TO
226 $ret = FALSE;
227
228 // TODO: Split up; this was copied verbatim from CiviCRM 4.0's multi-UF theming function
229 // but the parts should be copied into cleaner subclass implementations
1e305b0b
DL
230 $config = CRM_Core_Config::singleton();
231 if (
232 $config->userSystem->is_drupal &&
233 function_exists('theme') &&
234 !$print
235 ) {
6a488035
TO
236 if ($maintenance) {
237 drupal_set_breadcrumb('');
238 drupal_maintenance_theme();
10221f8a
TO
239 if ($region = CRM_Core_Region::instance('html-header', FALSE)) {
240 CRM_Utils_System::addHTMLHead($region->render(''));
241 }
6a488035
TO
242 print theme('maintenance_page', array('content' => $content));
243 exit();
244 }
245 $ret = TRUE; // TODO: Figure out why D7 returns but everyone else prints
246 }
247 $out = $content;
248
249 $config = &CRM_Core_Config::singleton();
1e305b0b
DL
250 if (
251 !$print &&
6a488035
TO
252 $config->userFramework == 'WordPress'
253 ) {
aecede9a
AH
254 if (!function_exists('is_admin')) {
255 throw new \Exception('Function "is_admin()" is missing, even though WordPress is the user framework.');
256 }
257 if (!defined('ABSPATH')) {
258 throw new \Exception('Constant "ABSPATH" is not defined, even though WordPress is the user framework.');
259 }
6a488035 260 if (is_admin()) {
e7292422 261 require_once ABSPATH . 'wp-admin/admin-header.php';
6a488035
TO
262 }
263 else {
7cdf0f5b 264 // FIXME: we need to figure out to replace civicrm content on the frontend pages
6a488035
TO
265 }
266 }
267
268 if ($ret) {
269 return $out;
270 }
271 else {
272 print $out;
85c5f34c 273 return NULL;
6a488035
TO
274 }
275 }
276
bb3a214a
EM
277 /**
278 * @return string
279 */
00be9182 280 public function getDefaultBlockLocation() {
6a488035
TO
281 return 'left';
282 }
283
bb3a214a 284 /**
fe482240 285 * Get CMS Version.
17f443df 286 *
bb3a214a
EM
287 * @return string
288 */
00be9182 289 public function getVersion() {
6a488035
TO
290 return 'Unknown';
291 }
292
293 /**
294 * Format the url as per language Negotiation.
295 *
296 * @param string $url
77b97be7
EM
297 * @param bool $addLanguagePart
298 * @param bool $removeLanguagePart
299 *
7cdf0f5b
AH
300 * @return string
301 * Formatted url.
6a488035 302 */
d5cc0fc2 303 public function languageNegotiationURL(
6a488035
TO
304 $url,
305 $addLanguagePart = TRUE,
306 $removeLanguagePart = FALSE
307 ) {
308 return $url;
309 }
310
e29aefb4
TO
311 /**
312 * Determine the location of the CMS root.
313 *
7cdf0f5b
AH
314 * @return string|null
315 * Local file system path to CMS root, or NULL if it cannot be determined
6a488035 316 */
00be9182 317 public function cmsRootPath() {
e29aefb4 318 return NULL;
6a488035
TO
319 }
320
17f443df
CW
321 /**
322 * Create a user in the CMS.
323 *
324 * @param array $params
325 * @param string $mail
326 * Email id for cms user.
327 *
328 * @return int|bool
329 * uid if user exists, false otherwise
330 */
331 public function createUser(&$params, $mail) {
332 return FALSE;
333 }
334
335 /**
336 * Update a user's email address in the CMS.
337 *
338 * @param int $ufID
339 * User ID in CMS.
340 * @param string $email
341 * Primary contact email address.
342 */
343 public function updateCMSName($ufID, $email) {
344 }
345
346 /**
347 * Check if user is logged in to the CMS.
348 *
349 * @return bool
350 */
351 public function isUserLoggedIn() {
352 return FALSE;
353 }
354
6a488035
TO
355 /**
356 * Get user login URL for hosting CMS (method declared in each CMS system class)
357 *
77855840
TO
358 * @param string $destination
359 * If present, add destination to querystring (works for Drupal only).
6a488035 360 *
a6c01b45
CW
361 * @return string
362 * loginURL for the current CMS
6a488035
TO
363 */
364 public abstract function getLoginURL($destination = '');
365
17f443df
CW
366 /**
367 * Get the login destination string. When this is passed in the
368 * URL the user will be directed to it after filling in the CMS form
369 *
370 * @param CRM_Core_Form $form
371 * Form object representing the 'current' form - to which the user will be returned.
372 * @return string|NULL
373 * destination value for URL
374 */
375 public function getLoginDestination(&$form) {
376 return NULL;
377 }
378
46b6363c 379 /**
fe482240 380 * Determine the native ID of the CMS user.
46b6363c 381 *
100fef9d 382 * @param string $username
f4aaa82a
EM
383 *
384 * @throws CRM_Core_Exception
46b6363c 385 */
00be9182 386 public function getUfId($username) {
46b6363c
TO
387 $className = get_class($this);
388 throw new CRM_Core_Exception("Not implemented: {$className}->getUfId");
389 }
390
5d0eb86b 391 /**
fe482240 392 * Set a init session with user object.
5d0eb86b 393 *
7cdf0f5b
AH
394 * @param array $data
395 * Array with user specific data
5d0eb86b 396 */
00be9182 397 public function setUserSession($data) {
5d0eb86b
BS
398 list($userID, $ufID) = $data;
399 $session = CRM_Core_Session::singleton();
400 $session->set('ufID', $ufID);
401 $session->set('userID', $userID);
402 }
d8a4acc0
C
403
404 /**
405 * Reset any system caches that may be required for proper CiviCRM
406 * integration.
407 */
00be9182 408 public function flush() {
d8a4acc0
C
409 // nullop by default
410 }
82d9c21e 411
f091327b
CW
412 /**
413 * Flush css/js caches
414 */
00be9182 415 public function clearResourceCache() {
f091327b
CW
416 // nullop by default
417 }
418
17f443df 419 /**
fe482240 420 * Add a script file.
17f443df
CW
421 *
422 * Note: This function is not to be called directly
423 * @see CRM_Core_Region::render()
424 *
425 * @param $url : string, absolute path to file
426 * @param string $region
427 * location within the document: 'html-header', 'page-header', 'page-footer'.
428 *
429 * @return bool
430 * TRUE if we support this operation in this CMS, FALSE otherwise
431 */
432 public function addScriptUrl($url, $region) {
433 return FALSE;
434 }
435
436 /**
fe482240 437 * Add an inline script.
17f443df
CW
438 *
439 * Note: This function is not to be called directly
440 * @see CRM_Core_Region::render()
441 *
442 * @param $code : string, javascript code
443 * @param string $region
444 * location within the document: 'html-header', 'page-header', 'page-footer'.
445 *
446 * @return bool
447 * TRUE if we support this operation in this CMS, FALSE otherwise
448 */
449 public function addScript($code, $region) {
450 return FALSE;
451 }
452
453 /**
fe482240 454 * Add a css file.
17f443df
CW
455 *
456 * Note: This function is not to be called directly
457 * @see CRM_Core_Region::render()
458 *
459 * @param $url : string, absolute path to file
460 * @param string $region
461 * location within the document: 'html-header', 'page-header', 'page-footer'.
462 *
463 * @return bool
464 * TRUE if we support this operation in this CMS, FALSE otherwise
465 */
466 public function addStyleUrl($url, $region) {
467 return FALSE;
468 }
469
470 /**
fe482240 471 * Add an inline style.
17f443df
CW
472 *
473 * Note: This function is not to be called directly
474 * @see CRM_Core_Region::render()
475 *
476 * @param $code : string, css code
477 * @param string $region
478 * location within the document: 'html-header', 'page-header', 'page-footer'.
479 *
480 * @return bool
481 * TRUE if we support this operation in this CMS, FALSE otherwise
482 */
483 public function addStyle($code, $region) {
484 return FALSE;
485 }
486
487 /**
fe482240 488 * Sets the title of the page.
17f443df
CW
489 *
490 * @param string $title
491 * Title to set in html header
492 * @param string|null $pageTitle
493 * Title to set in html body (if different)
494 */
495 public function setTitle($title, $pageTitle = NULL) {
496 }
497
c8950569 498 /**
fe482240 499 * Return default Site Settings.
f4aaa82a 500 *
7cdf0f5b 501 * @param string $dir
f4aaa82a 502 *
7cdf0f5b 503 * @return array
d5cc0fc2 504 * - $url, (Joomla - non admin url)
505 * - $siteName,
506 * - $siteRoot
9977c6f5 507 */
00be9182 508 public function getDefaultSiteSettings($dir) {
9977c6f5 509 $config = CRM_Core_Config::singleton();
510 $url = $config->userFrameworkBaseURL;
511 return array($url, NULL, NULL);
512 }
c8950569 513
82d9c21e 514 /**
95d68223 515 * Perform any post login activities required by the CMS -
53980972 516 * e.g. for drupal: records a watchdog message about the new session, saves the login timestamp,
517 * calls hook_user op 'login' and generates a new session.
e43cc689 518 *
7cdf0f5b 519 * @param array $params
95d68223
TO
520 *
521 * FIXME: Document values accepted/required by $params
c8950569 522 */
9b873358 523 public function userLoginFinalize($params = array()) {
82d9c21e 524 }
5a604d61
E
525
526 /**
fe482240 527 * Set timezone in mysql so that timestamp fields show the correct time.
5a604d61 528 */
9b873358 529 public function setMySQLTimeZone() {
5a604d61 530 $timeZoneOffset = $this->getTimeZoneOffset();
9b873358 531 if ($timeZoneOffset) {
5a604d61
E
532 $sql = "SET time_zone = '$timeZoneOffset'";
533 CRM_Core_DAO::executequery($sql);
534 }
535 }
536
6491539b 537
5a604d61 538 /**
fe482240 539 * Get timezone from CMS.
7cdf0f5b
AH
540 *
541 * @return string|false|null
6491539b 542 */
9b873358 543 public function getTimeZoneOffset() {
6491539b 544 $timezone = $this->getTimeZoneString();
8b647c3a 545 if ($timezone) {
6491539b
DL
546 $tzObj = new DateTimeZone($timezone);
547 $dateTime = new DateTime("now", $tzObj);
548 $tz = $tzObj->getOffset($dateTime);
549
8b647c3a
AH
550 if (empty($tz)) {
551 return FALSE;
6491539b
DL
552 }
553
e7292422 554 $timeZoneOffset = sprintf("%02d:%02d", $tz / 3600, abs(($tz / 60) % 60));
6491539b 555
8b647c3a 556 if ($timeZoneOffset > 0) {
6491539b
DL
557 $timeZoneOffset = '+' . $timeZoneOffset;
558 }
559 return $timeZoneOffset;
560 }
85c5f34c 561 return NULL;
6491539b
DL
562 }
563
564 /**
fe482240 565 * Get timezone as a string.
7cdf0f5b 566 * @return string
17f443df 567 * Timezone string e.g. 'America/Los_Angeles'
6491539b 568 */
00be9182 569 public function getTimeZoneString() {
48ec57ab 570 return date_default_timezone_get();
5a604d61 571 }
2b617cb0
EM
572
573 /**
574 * Get Unique Identifier from UserFramework system (CMS)
77855840
TO
575 * @param object $user
576 * Object as described by the User Framework.
72b3a70c
CW
577 * @return mixed
578 * $uniqueIdentifer Unique identifier from the user Framework system
2b617cb0 579 */
e7292422 580 public function getUniqueIdentifierFromUserObject($user) {
17f443df 581 return NULL;
e7292422 582 }
2b617cb0 583
32998c82
EM
584 /**
585 * Get User ID from UserFramework system (CMS)
77855840
TO
586 * @param object $user
587 * Object as described by the User Framework.
17f443df 588 * @return null|int
32998c82 589 */
e7292422 590 public function getUserIDFromUserObject($user) {
17f443df 591 return NULL;
e7292422 592 }
32998c82
EM
593
594 /**
595 * Get currently logged in user uf id.
596 *
17f443df
CW
597 * @return int|null
598 * logged in user uf id.
32998c82 599 */
e7292422 600 public function getLoggedInUfID() {
17f443df 601 return NULL;
e7292422 602 }
32998c82 603
2b617cb0
EM
604 /**
605 * Get currently logged in user unique identifier - this tends to be the email address or user name.
606 *
17f443df 607 * @return string|null
a6c01b45 608 * logged in user unique identifier
2b617cb0 609 */
e7292422 610 public function getLoggedInUniqueIdentifier() {
17f443df 611 return NULL;
e7292422 612 }
2b617cb0 613
32998c82 614 /**
100fef9d 615 * Return a UFID (user account ID from the UserFramework / CMS system being based on the user object
32998c82
EM
616 * passed, defaulting to the logged in user if not passed. Note that ambiguous situation occurs
617 * in CRM_Core_BAO_UFMatch::synchronize - a cleaner approach would seem to be resolving the user id before calling
618 * the function
619 *
620 * Note there is already a function getUFId which takes $username as a param - we could add $user
621 * as a second param to it but it seems messy - just overloading it because the name is taken
2b617cb0 622 * @param object $user
a6c01b45
CW
623 * @return int
624 * $ufid - user ID of UF System
32998c82 625 */
00be9182 626 public function getBestUFID($user = NULL) {
22e263ad 627 if ($user) {
32998c82
EM
628 return $this->getUserIDFromUserObject($user);
629 }
630 return $this->getLoggedInUfID();
631 }
2b617cb0
EM
632
633 /**
100fef9d 634 * Return a unique identifier (usually an email address or username) from the UserFramework / CMS system being based on the user object
2b617cb0
EM
635 * passed, defaulting to the logged in user if not passed. Note that ambiguous situation occurs
636 * in CRM_Core_BAO_UFMatch::synchronize - a cleaner approach would seem to be resolving the unique identifier before calling
637 * the function
638 *
639 * @param object $user
a6c01b45
CW
640 * @return string
641 * unique identifier from the UF System
2b617cb0 642 */
00be9182 643 public function getBestUFUniqueIdentifier($user = NULL) {
22e263ad 644 if ($user) {
2b617cb0
EM
645 return $this->getUniqueIdentifierFromUserObject($user);
646 }
647 return $this->getLoggedInUniqueIdentifier();
648 }
59f97da6 649
66e42142
CW
650 /**
651 * List modules installed in the CMS, including enabled and disabled ones.
652 *
653 * @return array
654 * [CRM_Core_Module]
655 */
656 public function getModules() {
657 return array();
658 }
659
59f97da6 660 /**
fe482240 661 * Get Url to view user record.
77855840
TO
662 * @param int $contactID
663 * Contact ID.
59f97da6 664 *
17f443df 665 * @return string|null
59f97da6 666 */
00be9182 667 public function getUserRecordUrl($contactID) {
59f97da6
EM
668 return NULL;
669 }
353ffa53 670
59f97da6 671 /**
fe482240 672 * Is the current user permitted to add a user.
59f97da6
EM
673 * @return bool
674 */
00be9182 675 public function checkPermissionAddUser() {
59f97da6
EM
676 return FALSE;
677 }
f85b1d20
EM
678
679 /**
fe482240 680 * Output code from error function.
f85b1d20
EM
681 * @param string $content
682 */
00be9182 683 public function outputError($content) {
f85b1d20
EM
684 echo CRM_Utils_System::theme($content);
685 }
e0dd98a5
EM
686
687 /**
fe482240 688 * Log error to CMS.
17f443df
CW
689 *
690 * $param string $message
e0dd98a5 691 */
00be9182 692 public function logger($message) {
e0dd98a5 693 }
f9f361d0
CW
694
695 /**
fe482240 696 * Append to coreResourcesList.
17f443df
CW
697 *
698 * @param array $list
f9f361d0 699 */
e7292422
TO
700 public function appendCoreResources(&$list) {
701 }
96025800 702
6a488035 703}