Merge pull request #17516 from eileenmcnaughton/pf_field
[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 {
2b0e7d03 7
4caaa696 8 /**
2b0e7d03
EM
9 * Deprecated property to check if this is a drupal install.
10 *
11 * The correct method is to have functions on the UF classes for all UF specific
4caaa696 12 * functions and leave the codebase oblivious to the type of CMS
7cdf0f5b 13 *
4caaa696 14 * @var bool
6714d8d2 15 * @deprecated
7cdf0f5b 16 * TRUE, if the CMS is Drupal.
4caaa696 17 */
6714d8d2 18 public $is_drupal = FALSE;
4caaa696
EM
19
20 /**
100fef9d 21 * 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 22 * functions and leave the codebase oblivious to the type of CMS
7cdf0f5b 23 *
4caaa696 24 * @var bool
6714d8d2 25 * @deprecated
7cdf0f5b 26 * TRUE, if the CMS is Joomla!.
4caaa696 27 */
6714d8d2 28 public $is_joomla = FALSE;
4caaa696 29
7cdf0f5b
AH
30 /**
31 * 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
32 * functions and leave the codebase oblivious to the type of CMS
33 *
7cdf0f5b 34 * @var bool
6714d8d2 35 * @deprecated
7cdf0f5b
AH
36 * TRUE, if the CMS is WordPress.
37 */
6714d8d2 38 public $is_wordpress = FALSE;
6a488035 39
e0dd98a5
EM
40 /**
41 * Does this CMS / UF support a CMS specific logging mechanism?
e0dd98a5 42 * @var bool
6714d8d2 43 * @todo - we should think about offering up logging mechanisms in a way that is also extensible by extensions
e0dd98a5 44 */
6714d8d2 45 public $supports_UF_Logging = FALSE;
e0dd98a5 46
7cdf0f5b
AH
47 /**
48 * @var bool
49 * TRUE, if the CMS allows CMS forms to be extended by hooks.
6a488035 50 */
6714d8d2 51 public $supports_form_extensions = FALSE;
6a488035 52
d4330c62
TO
53 public function initialize() {
54 if (\CRM_Utils_System::isSSL()) {
55 $this->mapConfigToSSL();
56 }
57 }
58
6714d8d2 59 abstract public function loadBootStrap($params = [], $loadUser = TRUE, $throwError = TRUE, $realPath = NULL);
9ba02e3e 60
17f443df 61 /**
fe482240 62 * Append an additional breadcrumb tag to the existing breadcrumb.
17f443df
CW
63 *
64 * @param array $breadCrumbs
65 */
66 public function appendBreadCrumb($breadCrumbs) {
67 }
68
69 /**
fe482240 70 * Reset an additional breadcrumb tag to the existing breadcrumb.
17f443df
CW
71 */
72 public function resetBreadCrumb() {
73 }
74
75 /**
fe482240 76 * Append a string to the head of the html file.
17f443df
CW
77 *
78 * @param string $head
79 * The new string to be appended.
80 */
81 public function addHTMLHead($head) {
82 }
83
84 /**
fe482240 85 * Rewrite various system urls to https.
17f443df
CW
86 */
87 public function mapConfigToSSL() {
88 // dont need to do anything, let CMS handle their own switch to SSL
89 }
90
91 /**
fe482240 92 * Figure out the post url for QuickForm.
17f443df
CW
93 *
94 * @param string $action
95 * The default url if one is pre-specified.
96 *
97 * @return string
98 * The url to post the form.
99 */
100 public function postURL($action) {
101 $config = CRM_Core_Config::singleton();
102 if (!empty($action)) {
103 return $action;
104 }
105
106 return $this->url(CRM_Utils_Array::value($config->userFrameworkURLVar, $_GET),
107 NULL, TRUE, NULL, FALSE
108 );
109 }
110
111 /**
112 * Generate the url string to a CiviCRM path.
113 *
114 * @param string $path
115 * The path being linked to, such as "civicrm/add".
116 * @param string $query
117 * A query string to append to the link.
118 * @param bool $absolute
119 * Whether to force the output to be an absolute link (beginning with http).
120 * Useful for links that will be displayed outside the site, such as in an RSS feed.
121 * @param string $fragment
122 * A fragment identifier (named anchor) to append to the link.
17f443df
CW
123 * @param bool $frontend
124 * This link should be to the CMS front end (applies to WP & Joomla).
125 * @param bool $forceBackend
126 * This link should be to the CMS back end (applies to WP & Joomla).
127 *
128 * @return string
129 */
130 public function url(
131 $path = NULL,
132 $query = NULL,
133 $absolute = FALSE,
134 $fragment = NULL,
17f443df
CW
135 $frontend = FALSE,
136 $forceBackend = FALSE
137 ) {
138 return NULL;
139 }
140
141 /**
fe482240 142 * Authenticate the user against the CMS db.
17f443df
CW
143 *
144 * @param string $name
145 * The user name.
146 * @param string $password
147 * The password for the above user.
148 * @param bool $loadCMSBootstrap
149 * Load cms bootstrap?.
150 * @param string $realPath
151 * Filename of script
152 *
153 * @return array|bool
154 * [contactID, ufID, unique string] else false if no auth
309310bf 155 * @throws \CRM_Core_Exception.
17f443df
CW
156 */
157 public function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) {
158 return FALSE;
159 }
160
161 /**
fe482240 162 * Set a message in the CMS to display to a user.
17f443df
CW
163 *
164 * @param string $message
165 * The message to set.
166 */
167 public function setMessage($message) {
168 }
169
170 /**
fe482240 171 * Load user into session.
17f443df 172 *
2b0e7d03 173 * @param obj $user
17f443df
CW
174 *
175 * @return bool
176 */
177 public function loadUser($user) {
178 return TRUE;
179 }
180
181 /**
2b0e7d03 182 * Immediately stop script execution and display a 401 "Access Denied" page.
309310bf 183 * @throws \CRM_Core_Exception
17f443df
CW
184 */
185 public function permissionDenied() {
309310bf 186 throw new CRM_Core_Exception(ts('You do not have permission to access this page.'));
17f443df
CW
187 }
188
189 /**
2b0e7d03 190 * Immediately stop script execution, log out the user and redirect to the home page.
17f443df
CW
191 *
192 * @deprecated
193 * This function should be removed in favor of linking to the CMS's logout page
194 */
195 public function logout() {
196 }
197
198 /**
199 * Clear CMS caches related to the user registration/profile forms.
200 * Used when updating/embedding profiles on CMS user forms.
201 * @see CRM-3600
202 */
203 public function updateCategories() {
204 }
205
206 /**
fe482240 207 * Get the locale set in the CMS.
17f443df
CW
208 *
209 * @return string|null
210 * Locale or null for none
211 */
212 public function getUFLocale() {
213 return NULL;
214 }
215
6a488035 216 /**
2b0e7d03 217 * If we are using a theming system, invoke theme, else just print the content.
6a488035 218 *
77855840
TO
219 * @param string $content
220 * The content that will be themed.
221 * @param bool $print
222 * Are we displaying to the screen or bypassing theming?.
223 * @param bool $maintenance
224 * For maintenance mode.
6a488035 225 *
7cdf0f5b
AH
226 * @throws Exception
227 * @return string|null
228 * NULL, If $print is FALSE, and some other criteria match up.
229 * The themed string, otherwise.
26659f0c
AH
230 *
231 * @todo The return value is inconsistent.
232 * @todo Better to always return, and never print.
6a488035 233 */
00be9182 234 public function theme(&$content, $print = FALSE, $maintenance = FALSE) {
6a488035
TO
235 $ret = FALSE;
236
237 // TODO: Split up; this was copied verbatim from CiviCRM 4.0's multi-UF theming function
238 // but the parts should be copied into cleaner subclass implementations
1e305b0b
DL
239 $config = CRM_Core_Config::singleton();
240 if (
241 $config->userSystem->is_drupal &&
242 function_exists('theme') &&
243 !$print
244 ) {
6a488035
TO
245 if ($maintenance) {
246 drupal_set_breadcrumb('');
247 drupal_maintenance_theme();
10221f8a
TO
248 if ($region = CRM_Core_Region::instance('html-header', FALSE)) {
249 CRM_Utils_System::addHTMLHead($region->render(''));
250 }
be2fb01f 251 print theme('maintenance_page', ['content' => $content]);
6a488035
TO
252 exit();
253 }
2b0e7d03
EM
254 // TODO: Figure out why D7 returns but everyone else prints
255 $ret = TRUE;
6a488035
TO
256 }
257 $out = $content;
258
1e305b0b
DL
259 if (
260 !$print &&
f3a87cf4 261 CRM_Core_Config::singleton()->userFramework == 'WordPress'
6a488035 262 ) {
aecede9a
AH
263 if (!function_exists('is_admin')) {
264 throw new \Exception('Function "is_admin()" is missing, even though WordPress is the user framework.');
265 }
266 if (!defined('ABSPATH')) {
267 throw new \Exception('Constant "ABSPATH" is not defined, even though WordPress is the user framework.');
268 }
6a488035 269 if (is_admin()) {
e7292422 270 require_once ABSPATH . 'wp-admin/admin-header.php';
6a488035
TO
271 }
272 else {
7cdf0f5b 273 // FIXME: we need to figure out to replace civicrm content on the frontend pages
6a488035
TO
274 }
275 }
276
277 if ($ret) {
278 return $out;
279 }
280 else {
281 print $out;
85c5f34c 282 return NULL;
6a488035
TO
283 }
284 }
285
bb3a214a
EM
286 /**
287 * @return string
288 */
00be9182 289 public function getDefaultBlockLocation() {
6a488035
TO
290 return 'left';
291 }
292
8246bca4 293 /**
294 * Get the absolute path to the site's base url.
295 *
296 * @return bool|mixed|string
297 */
d4330c62
TO
298 public function getAbsoluteBaseURL() {
299 if (!defined('CIVICRM_UF_BASEURL')) {
300 return FALSE;
301 }
302
303 $url = CRM_Utils_File::addTrailingSlash(CIVICRM_UF_BASEURL, '/');
304
305 //format url for language negotiation, CRM-7803
306 $url = $this->languageNegotiationURL($url);
307
308 if (CRM_Utils_System::isSSL()) {
309 $url = str_replace('http://', 'https://', $url);
310 }
311
312 return $url;
313 }
314
8246bca4 315 /**
316 * Get the relative path to the sites base url.
317 *
318 * @return bool
319 */
d4330c62
TO
320 public function getRelativeBaseURL() {
321 $absoluteBaseURL = $this->getAbsoluteBaseURL();
322 if ($absoluteBaseURL === FALSE) {
323 return FALSE;
324 }
325 $parts = parse_url($absoluteBaseURL);
326 return $parts['path'];
327 //$this->useFrameworkRelativeBase = empty($base['path']) ? '/' : $base['path'];
328 }
329
bb3a214a 330 /**
fe482240 331 * Get CMS Version.
17f443df 332 *
bb3a214a
EM
333 * @return string
334 */
00be9182 335 public function getVersion() {
6a488035
TO
336 return 'Unknown';
337 }
338
339 /**
340 * Format the url as per language Negotiation.
341 *
342 * @param string $url
77b97be7
EM
343 * @param bool $addLanguagePart
344 * @param bool $removeLanguagePart
345 *
7cdf0f5b
AH
346 * @return string
347 * Formatted url.
6a488035 348 */
d5cc0fc2 349 public function languageNegotiationURL(
6a488035
TO
350 $url,
351 $addLanguagePart = TRUE,
352 $removeLanguagePart = FALSE
353 ) {
354 return $url;
355 }
356
e29aefb4
TO
357 /**
358 * Determine the location of the CMS root.
359 *
7cdf0f5b
AH
360 * @return string|null
361 * Local file system path to CMS root, or NULL if it cannot be determined
6a488035 362 */
00be9182 363 public function cmsRootPath() {
e29aefb4 364 return NULL;
6a488035
TO
365 }
366
17f443df
CW
367 /**
368 * Create a user in the CMS.
369 *
370 * @param array $params
371 * @param string $mail
372 * Email id for cms user.
373 *
374 * @return int|bool
375 * uid if user exists, false otherwise
376 */
377 public function createUser(&$params, $mail) {
378 return FALSE;
379 }
380
381 /**
382 * Update a user's email address in the CMS.
383 *
384 * @param int $ufID
385 * User ID in CMS.
386 * @param string $email
387 * Primary contact email address.
388 */
389 public function updateCMSName($ufID, $email) {
390 }
391
392 /**
393 * Check if user is logged in to the CMS.
394 *
395 * @return bool
396 */
397 public function isUserLoggedIn() {
398 return FALSE;
399 }
400
8caad0ce 401 /**
402 * Check if user registration is permitted.
403 *
404 * @return bool
405 */
406 public function isUserRegistrationPermitted() {
407 return FALSE;
408 }
409
63df6889
HD
410 /**
411 * Check if user can create passwords or is initially assigned a system-generated one.
412 *
413 * @return bool
414 */
1a6630be 415 public function isPasswordUserGenerated() {
63df6889
HD
416 return FALSE;
417 }
418
68e3bc34 419 /**
420 * Is a front end page being accessed.
421 *
422 * Generally this would be a contribution form or other public page as opposed to a backoffice page (like contact edit).
423 *
424 * @todo Drupal uses the is_public setting - clarify & rationalise. See https://github.com/civicrm/civicrm-drupal/pull/546/files
425 *
426 * @return bool
427 */
428 public function isFrontEndPage() {
429 return CRM_Core_Config::singleton()->userFrameworkFrontend;
430 }
431
6a488035
TO
432 /**
433 * Get user login URL for hosting CMS (method declared in each CMS system class)
434 *
77855840
TO
435 * @param string $destination
436 * If present, add destination to querystring (works for Drupal only).
6a488035 437 *
a6c01b45
CW
438 * @return string
439 * loginURL for the current CMS
6a488035 440 */
6714d8d2 441 abstract public function getLoginURL($destination = '');
6a488035 442
17f443df 443 /**
2b0e7d03
EM
444 * Get the login destination string.
445 *
446 * When this is passed in the URL the user will be directed to it after filling in the CMS form.
17f443df
CW
447 *
448 * @param CRM_Core_Form $form
449 * Form object representing the 'current' form - to which the user will be returned.
2b0e7d03 450 *
17f443df
CW
451 * @return string|NULL
452 * destination value for URL
453 */
454 public function getLoginDestination(&$form) {
455 return NULL;
456 }
457
46b6363c 458 /**
fe482240 459 * Determine the native ID of the CMS user.
46b6363c 460 *
100fef9d 461 * @param string $username
f4aaa82a
EM
462 *
463 * @throws CRM_Core_Exception
46b6363c 464 */
00be9182 465 public function getUfId($username) {
46b6363c
TO
466 $className = get_class($this);
467 throw new CRM_Core_Exception("Not implemented: {$className}->getUfId");
468 }
469
bc854509 470 /**
471 * Set the localisation from the user framework.
472 *
473 * @param string $civicrm_language
474 *
475 * @return bool
476 */
a7c57397
TO
477 public function setUFLocale($civicrm_language) {
478 return TRUE;
479 }
480
5d0eb86b 481 /**
fe482240 482 * Set a init session with user object.
5d0eb86b 483 *
7cdf0f5b
AH
484 * @param array $data
485 * Array with user specific data
5d0eb86b 486 */
00be9182 487 public function setUserSession($data) {
5d0eb86b
BS
488 list($userID, $ufID) = $data;
489 $session = CRM_Core_Session::singleton();
490 $session->set('ufID', $ufID);
491 $session->set('userID', $userID);
492 }
d8a4acc0
C
493
494 /**
2b0e7d03 495 * Reset any system caches that may be required for proper CiviCRM integration.
d8a4acc0 496 */
00be9182 497 public function flush() {
d8a4acc0
C
498 // nullop by default
499 }
82d9c21e 500
f091327b 501 /**
2b0e7d03 502 * Flush css/js caches.
f091327b 503 */
00be9182 504 public function clearResourceCache() {
f091327b
CW
505 // nullop by default
506 }
507
17f443df 508 /**
fe482240 509 * Add a script file.
17f443df
CW
510 *
511 * Note: This function is not to be called directly
512 * @see CRM_Core_Region::render()
513 *
2b0e7d03 514 * @param string $url absolute path to file
17f443df
CW
515 * @param string $region
516 * location within the document: 'html-header', 'page-header', 'page-footer'.
517 *
518 * @return bool
519 * TRUE if we support this operation in this CMS, FALSE otherwise
520 */
521 public function addScriptUrl($url, $region) {
522 return FALSE;
523 }
524
525 /**
fe482240 526 * Add an inline script.
17f443df
CW
527 *
528 * Note: This function is not to be called directly
529 * @see CRM_Core_Region::render()
530 *
2b0e7d03 531 * @param string $code javascript code
17f443df
CW
532 * @param string $region
533 * location within the document: 'html-header', 'page-header', 'page-footer'.
534 *
535 * @return bool
536 * TRUE if we support this operation in this CMS, FALSE otherwise
537 */
538 public function addScript($code, $region) {
539 return FALSE;
540 }
541
542 /**
fe482240 543 * Add a css file.
17f443df
CW
544 *
545 * Note: This function is not to be called directly
546 * @see CRM_Core_Region::render()
547 *
2b0e7d03 548 * @param string $url absolute path to file
17f443df
CW
549 * @param string $region
550 * location within the document: 'html-header', 'page-header', 'page-footer'.
551 *
552 * @return bool
553 * TRUE if we support this operation in this CMS, FALSE otherwise
554 */
555 public function addStyleUrl($url, $region) {
556 return FALSE;
557 }
558
559 /**
fe482240 560 * Add an inline style.
17f443df
CW
561 *
562 * Note: This function is not to be called directly
563 * @see CRM_Core_Region::render()
564 *
2b0e7d03 565 * @param string $code css code
17f443df
CW
566 * @param string $region
567 * location within the document: 'html-header', 'page-header', 'page-footer'.
568 *
569 * @return bool
570 * TRUE if we support this operation in this CMS, FALSE otherwise
571 */
572 public function addStyle($code, $region) {
573 return FALSE;
574 }
575
576 /**
fe482240 577 * Sets the title of the page.
17f443df
CW
578 *
579 * @param string $title
580 * Title to set in html header
581 * @param string|null $pageTitle
582 * Title to set in html body (if different)
583 */
584 public function setTitle($title, $pageTitle = NULL) {
585 }
586
c8950569 587 /**
fe482240 588 * Return default Site Settings.
f4aaa82a 589 *
7cdf0f5b 590 * @param string $dir
f4aaa82a 591 *
7cdf0f5b 592 * @return array
d5cc0fc2 593 * - $url, (Joomla - non admin url)
594 * - $siteName,
595 * - $siteRoot
9977c6f5 596 */
00be9182 597 public function getDefaultSiteSettings($dir) {
9977c6f5 598 $config = CRM_Core_Config::singleton();
599 $url = $config->userFrameworkBaseURL;
be2fb01f 600 return [$url, NULL, NULL];
9977c6f5 601 }
c8950569 602
f6f958e4
TO
603 /**
604 * Determine the default location for file storage.
605 *
606 * FIXME:
607 * 1. This was pulled out from a bigger function. It should be split
608 * into even smaller pieces and marked abstract.
609 * 2. This would be easier to compute by a calling a CMS API, but
610 * for whatever reason Civi gets it from config data.
611 *
612 * @return array
613 * - url: string. ex: "http://example.com/sites/foo.com/files/civicrm"
614 * - path: string. ex: "/var/www/sites/foo.com/files/civicrm"
615 */
616 public function getDefaultFileStorage() {
617 global $civicrm_root;
618 $config = CRM_Core_Config::singleton();
619 $baseURL = CRM_Utils_System::languageNegotiationURL($config->userFrameworkBaseURL, FALSE, TRUE);
620
621 $filesURL = NULL;
622 $filesPath = NULL;
623
624 if ($config->userFramework == 'Joomla') {
625 // gross hack
626 // we need to remove the administrator/ from the end
627 $tempURL = str_replace("/administrator/", "/", $baseURL);
628 $filesURL = $tempURL . "media/civicrm/";
629 }
f6f958e4
TO
630 elseif ($config->userFramework == 'UnitTests') {
631 $filesURL = $baseURL . "sites/default/files/civicrm/";
632 }
633 else {
634 throw new CRM_Core_Exception("Failed to locate default file storage ($config->userFramework)");
635 }
636
be2fb01f 637 return [
f6f958e4
TO
638 'url' => $filesURL,
639 'path' => CRM_Utils_File::baseFilePath(),
be2fb01f 640 ];
f6f958e4
TO
641 }
642
643 /**
644 * Determine the location of the CiviCRM source tree.
645 *
646 * FIXME:
647 * 1. This was pulled out from a bigger function. It should be split
648 * into even smaller pieces and marked abstract.
649 * 2. This would be easier to compute by a calling a CMS API, but
650 * for whatever reason we take the hard way.
651 *
652 * @return array
653 * - url: string. ex: "http://example.com/sites/all/modules/civicrm"
654 * - path: string. ex: "/var/www/sites/all/modules/civicrm"
655 */
656 public function getCiviSourceStorage() {
657 global $civicrm_root;
658 $config = CRM_Core_Config::singleton();
659
660 // Don't use $config->userFrameworkBaseURL; it has garbage on it.
661 // More generally, w shouldn't be using $config here.
662 if (!defined('CIVICRM_UF_BASEURL')) {
663 throw new RuntimeException('Undefined constant: CIVICRM_UF_BASEURL');
664 }
665 $baseURL = CRM_Utils_File::addTrailingSlash(CIVICRM_UF_BASEURL, '/');
666 if (CRM_Utils_System::isSSL()) {
667 $baseURL = str_replace('http://', 'https://', $baseURL);
668 }
669
38dab72a 670 // @todo this function is only called / code is only reached when is_drupal is true - move this to the drupal classes
671 // and don't implement here.
672 if ($this->is_drupal) {
3403e54b
TO
673 // Drupal setting
674 // check and see if we are installed in sites/all (for D5 and above)
675 // we dont use checkURL since drupal generates an error page and throws
676 // the system for a loop on lobo's macosx box
677 // or in modules
678 $cmsPath = $config->userSystem->cmsRootPath();
679 $userFrameworkResourceURL = $baseURL . str_replace("$cmsPath/", '',
680 str_replace('\\', '/', $civicrm_root)
681 );
682
683 $siteName = $config->userSystem->parseDrupalSiteNameFromRoot($civicrm_root);
684 if ($siteName) {
685 $civicrmDirName = trim(basename($civicrm_root));
686 $userFrameworkResourceURL = $baseURL . "sites/$siteName/modules/$civicrmDirName/";
687 }
688 }
f6f958e4
TO
689 else {
690 $userFrameworkResourceURL = NULL;
691 }
692
be2fb01f 693 return [
6ee26d90 694 'url' => CRM_Utils_File::addTrailingSlash($userFrameworkResourceURL, '/'),
f6f958e4 695 'path' => CRM_Utils_File::addTrailingSlash($civicrm_root),
be2fb01f 696 ];
f6f958e4
TO
697 }
698
82d9c21e 699 /**
2b0e7d03
EM
700 * Perform any post login activities required by the CMS.
701 *
53980972 702 * e.g. for drupal: records a watchdog message about the new session, saves the login timestamp,
703 * calls hook_user op 'login' and generates a new session.
e43cc689 704 *
7cdf0f5b 705 * @param array $params
95d68223
TO
706 *
707 * FIXME: Document values accepted/required by $params
c8950569 708 */
be2fb01f 709 public function userLoginFinalize($params = []) {
82d9c21e 710 }
5a604d61
E
711
712 /**
fe482240 713 * Set timezone in mysql so that timestamp fields show the correct time.
5a604d61 714 */
9b873358 715 public function setMySQLTimeZone() {
5a604d61 716 $timeZoneOffset = $this->getTimeZoneOffset();
9b873358 717 if ($timeZoneOffset) {
5a604d61
E
718 $sql = "SET time_zone = '$timeZoneOffset'";
719 CRM_Core_DAO::executequery($sql);
720 }
721 }
722
723 /**
fe482240 724 * Get timezone from CMS.
7cdf0f5b
AH
725 *
726 * @return string|false|null
6491539b 727 */
9b873358 728 public function getTimeZoneOffset() {
6491539b 729 $timezone = $this->getTimeZoneString();
8b647c3a 730 if ($timezone) {
2451c06a 731 if ($timezone == 'UTC' || $timezone == 'Etc/UTC') {
030113f0 732 // CRM-17072 Let's short-circuit all the zero handling & return it here!
733 return '+00:00';
734 }
6491539b
DL
735 $tzObj = new DateTimeZone($timezone);
736 $dateTime = new DateTime("now", $tzObj);
737 $tz = $tzObj->getOffset($dateTime);
738
f2173d64 739 if ($tz === 0) {
278460dd
AS
740 // CRM-21422
741 return '+00:00';
742 }
743
8b647c3a
AH
744 if (empty($tz)) {
745 return FALSE;
6491539b
DL
746 }
747
e7292422 748 $timeZoneOffset = sprintf("%02d:%02d", $tz / 3600, abs(($tz / 60) % 60));
6491539b 749
8b647c3a 750 if ($timeZoneOffset > 0) {
6491539b
DL
751 $timeZoneOffset = '+' . $timeZoneOffset;
752 }
753 return $timeZoneOffset;
754 }
85c5f34c 755 return NULL;
6491539b
DL
756 }
757
758 /**
fe482240 759 * Get timezone as a string.
7cdf0f5b 760 * @return string
17f443df 761 * Timezone string e.g. 'America/Los_Angeles'
6491539b 762 */
00be9182 763 public function getTimeZoneString() {
48ec57ab 764 return date_default_timezone_get();
5a604d61 765 }
2b617cb0
EM
766
767 /**
2b0e7d03
EM
768 * Get Unique Identifier from UserFramework system (CMS).
769 *
77855840
TO
770 * @param object $user
771 * Object as described by the User Framework.
2b0e7d03 772 *
72b3a70c 773 * @return mixed
2b0e7d03 774 * Unique identifier from the user Framework system
2b617cb0 775 */
e7292422 776 public function getUniqueIdentifierFromUserObject($user) {
17f443df 777 return NULL;
e7292422 778 }
2b617cb0 779
32998c82 780 /**
2b0e7d03
EM
781 * Get User ID from UserFramework system (CMS).
782 *
77855840 783 * @param object $user
2b0e7d03 784 *
77855840 785 * Object as described by the User Framework.
17f443df 786 * @return null|int
32998c82 787 */
e7292422 788 public function getUserIDFromUserObject($user) {
17f443df 789 return NULL;
e7292422 790 }
32998c82 791
2b0e7d03
EM
792 /**
793 * Get an array of user details for a contact, containing at minimum the user ID & name.
794 *
795 * @param int $contactID
796 *
797 * @return array
798 * CMS user details including
799 * - id
800 * - name (ie the system user name.
801 */
802 public function getUser($contactID) {
be2fb01f 803 $ufMatch = civicrm_api3('UFMatch', 'getsingle', [
2b0e7d03
EM
804 'contact_id' => $contactID,
805 'domain_id' => CRM_Core_Config::domainID(),
be2fb01f
CW
806 ]);
807 return [
2b0e7d03
EM
808 'id' => $ufMatch['uf_id'],
809 'name' => $ufMatch['uf_name'],
be2fb01f 810 ];
2b0e7d03
EM
811 }
812
32998c82
EM
813 /**
814 * Get currently logged in user uf id.
815 *
17f443df
CW
816 * @return int|null
817 * logged in user uf id.
32998c82 818 */
e7292422 819 public function getLoggedInUfID() {
17f443df 820 return NULL;
e7292422 821 }
32998c82 822
2b617cb0
EM
823 /**
824 * Get currently logged in user unique identifier - this tends to be the email address or user name.
825 *
17f443df 826 * @return string|null
a6c01b45 827 * logged in user unique identifier
2b617cb0 828 */
e7292422 829 public function getLoggedInUniqueIdentifier() {
17f443df 830 return NULL;
e7292422 831 }
2b617cb0 832
32998c82 833 /**
2b0e7d03
EM
834 * Return a UFID (user account ID from the UserFramework / CMS system.
835 *
836 * ID is based on the user object passed, defaulting to the logged in user if not passed.
837 *
838 * Note that ambiguous situation occurs in CRM_Core_BAO_UFMatch::synchronize - a cleaner approach would
839 * seem to be resolving the user id before calling the function.
32998c82
EM
840 *
841 * Note there is already a function getUFId which takes $username as a param - we could add $user
2b0e7d03
EM
842 * as a second param to it but it seems messy - just overloading it because the name is taken.
843 *
2b617cb0 844 * @param object $user
2b0e7d03 845 *
a6c01b45 846 * @return int
2b0e7d03 847 * User ID of UF System
32998c82 848 */
00be9182 849 public function getBestUFID($user = NULL) {
22e263ad 850 if ($user) {
32998c82
EM
851 return $this->getUserIDFromUserObject($user);
852 }
853 return $this->getLoggedInUfID();
854 }
2b617cb0
EM
855
856 /**
2b0e7d03
EM
857 * Return a unique identifier (usually an email address or username) from the UserFramework / CMS system.
858 *
859 * This is based on the user object passed, defaulting to the logged in user if not passed.
860 *
861 * Note that ambiguous situation occurs in CRM_Core_BAO_UFMatch::synchronize - a cleaner approach would seem to be
862 * resolving the unique identifier before calling the function.
2b617cb0
EM
863 *
864 * @param object $user
2b0e7d03 865 *
a6c01b45
CW
866 * @return string
867 * unique identifier from the UF System
2b617cb0 868 */
00be9182 869 public function getBestUFUniqueIdentifier($user = NULL) {
22e263ad 870 if ($user) {
2b617cb0
EM
871 return $this->getUniqueIdentifierFromUserObject($user);
872 }
873 return $this->getLoggedInUniqueIdentifier();
874 }
59f97da6 875
66e42142
CW
876 /**
877 * List modules installed in the CMS, including enabled and disabled ones.
878 *
879 * @return array
880 * [CRM_Core_Module]
881 */
882 public function getModules() {
be2fb01f 883 return [];
66e42142
CW
884 }
885
59f97da6 886 /**
fe482240 887 * Get Url to view user record.
2b0e7d03 888 *
77855840
TO
889 * @param int $contactID
890 * Contact ID.
59f97da6 891 *
17f443df 892 * @return string|null
59f97da6 893 */
00be9182 894 public function getUserRecordUrl($contactID) {
59f97da6
EM
895 return NULL;
896 }
353ffa53 897
59f97da6 898 /**
fe482240 899 * Is the current user permitted to add a user.
2b0e7d03 900 *
59f97da6
EM
901 * @return bool
902 */
00be9182 903 public function checkPermissionAddUser() {
59f97da6
EM
904 return FALSE;
905 }
f85b1d20
EM
906
907 /**
fe482240 908 * Output code from error function.
2b0e7d03 909 *
f85b1d20
EM
910 * @param string $content
911 */
00be9182 912 public function outputError($content) {
f85b1d20
EM
913 echo CRM_Utils_System::theme($content);
914 }
e0dd98a5
EM
915
916 /**
fe482240 917 * Log error to CMS.
17f443df 918 *
ad37ac8e 919 * @param string $message
e0dd98a5 920 */
00be9182 921 public function logger($message) {
e0dd98a5 922 }
f9f361d0
CW
923
924 /**
fe482240 925 * Append to coreResourcesList.
17f443df 926 *
303017a1 927 * @param \Civi\Core\Event\GenericHookEvent $e
f9f361d0 928 */
303017a1 929 public function appendCoreResources(\Civi\Core\Event\GenericHookEvent $e) {
e7292422 930 }
96025800 931
62c20d1e
CW
932 /**
933 * Modify dynamic assets.
934 *
935 * @param \Civi\Core\Event\GenericHookEvent $e
936 */
937 public function alterAssetUrl(\Civi\Core\Event\GenericHookEvent $e) {
938 }
939
d42a224c
CW
940 /**
941 * @param string $name
942 * @param string $value
943 */
944 public function setHttpHeader($name, $value) {
945 header("$name: $value");
946 }
947
03d5592a
CW
948 /**
949 * Create CRM contacts for all existing CMS users
950 *
951 * @return array
952 * @throws \Exception
953 */
954 public function synchronizeUsers() {
955 throw new Exception('CMS user creation not supported for this framework');
be2fb01f 956 return [];
03d5592a
CW
957 }
958
79dd7fe9 959 /**
46dddc5c
SL
960 * Send an HTTP Response base on PSR HTTP RespnseInterface response.
961 *
962 * @param \Psr\Http\Message\ResponseInterface $response
79dd7fe9 963 */
46dddc5c 964 public function sendResponse(\Psr\Http\Message\ResponseInterface $response) {
fcfcd0f7 965 http_response_code($response->getStatusCode());
90f6c8df
SL
966 foreach ($response->getHeaders() as $name => $values) {
967 CRM_Utils_System::setHttpHeader($name, implode(', ', (array) $values));
79dd7fe9 968 }
46dddc5c
SL
969 echo $response->getBody();
970 CRM_Utils_System::civiExit();
79dd7fe9
SL
971 }
972
671f655b 973 /**
974 * Start a new session.
975 */
976 public function sessionStart() {
977 session_start();
978 }
979
08d5b4df
JP
980 /**
981 * Get role names
982 *
983 * @return array|null
984 */
985 public function getRoleNames() {
986 return NULL;
987 }
988
9a8fe710
SL
989 /**
990 * Determine if the Views module exists.
991 *
992 * @return bool
993 */
994 public function viewsExists() {
995 return FALSE;
996 }
997
6a488035 998}