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