Merge pull request #17526 from mattwire/frontendrequiredpaymentfrequency
[civicrm-core.git] / CRM / Utils / System / Joomla.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Joomla specific stuff goes here.
20 */
21 class CRM_Utils_System_Joomla extends CRM_Utils_System_Base {
22
23 /**
24 * Class constructor.
25 */
26 public function __construct() {
27 /**
28 * 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
29 * functions and leave the codebase oblivious to the type of CMS
30 * @deprecated
31 * @var bool
32 */
33 $this->is_drupal = FALSE;
34 }
35
36 /**
37 * @inheritDoc
38 */
39 public function createUser(&$params, $mail) {
40 $baseDir = JPATH_SITE;
41 require_once $baseDir . '/components/com_users/models/registration.php';
42
43 $userParams = JComponentHelper::getParams('com_users');
44 $model = new UsersModelRegistration();
45 $ufID = NULL;
46
47 // get the default usertype
48 $userType = $userParams->get('new_usertype');
49 if (!$userType) {
50 $userType = 2;
51 }
52
53 if (isset($params['name'])) {
54 $fullname = trim($params['name']);
55 }
56 elseif (isset($params['contactID'])) {
57 $fullname = trim(CRM_Contact_BAO_Contact::displayName($params['contactID']));
58 }
59 else {
60 $fullname = trim($params['cms_name']);
61 }
62
63 // Prepare the values for a new Joomla user.
64 $values = [];
65 $values['name'] = $fullname;
66 $values['username'] = trim($params['cms_name']);
67 $values['password1'] = $values['password2'] = $params['cms_pass'];
68 $values['email1'] = $values['email2'] = trim($params[$mail]);
69
70 $lang = JFactory::getLanguage();
71 $lang->load('com_users', $baseDir);
72
73 $register = $model->register($values);
74
75 $ufID = JUserHelper::getUserId($values['username']);
76 return $ufID;
77 }
78
79 /**
80 * @inheritDoc
81 */
82 public function updateCMSName($ufID, $ufName) {
83 $ufID = CRM_Utils_Type::escape($ufID, 'Integer');
84 $ufName = CRM_Utils_Type::escape($ufName, 'String');
85
86 $values = [];
87 $user = JUser::getInstance($ufID);
88
89 $values['email'] = $ufName;
90 $user->bind($values);
91
92 $user->save();
93 }
94
95 /**
96 * Check if username and email exists in the Joomla db.
97 *
98 * @param array $params
99 * Array of name and mail values.
100 * @param array $errors
101 * Array of errors.
102 * @param string $emailName
103 * Field label for the 'email'.
104 */
105 public function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') {
106 $config = CRM_Core_Config::singleton();
107
108 $dao = new CRM_Core_DAO();
109 $name = $dao->escape(CRM_Utils_Array::value('name', $params));
110 $email = $dao->escape(CRM_Utils_Array::value('mail', $params));
111 //don't allow the special characters and min. username length is two
112 //regex \\ to match a single backslash would become '/\\\\/'
113 $isNotValid = (bool) preg_match('/[\<|\>|\"|\'|\%|\;|\(|\)|\&|\\\\|\/]/im', $name);
114 if ($isNotValid || strlen($name) < 2) {
115 $errors['cms_name'] = ts('Your username contains invalid characters or is too short');
116 }
117
118 $JUserTable = &JTable::getInstance('User', 'JTable');
119
120 $db = $JUserTable->getDbo();
121 $query = $db->getQuery(TRUE);
122 $query->select('username, email');
123 $query->from($JUserTable->getTableName());
124
125 // LOWER in query below roughly translates to 'hurt my database without deriving any benefit' See CRM-19811.
126 $query->where('(LOWER(username) = LOWER(\'' . $name . '\')) OR (LOWER(email) = LOWER(\'' . $email . '\'))');
127 $db->setQuery($query, 0, 10);
128 $users = $db->loadAssocList();
129
130 $row = [];
131 if (count($users)) {
132 $row = $users[0];
133 }
134
135 if (!empty($row)) {
136 $dbName = $row['username'] ?? NULL;
137 $dbEmail = $row['email'] ?? NULL;
138 if (strtolower($dbName) == strtolower($name)) {
139 $errors['cms_name'] = ts('The username %1 is already taken. Please select another username.',
140 [1 => $name]
141 );
142 }
143 if (strtolower($dbEmail) == strtolower($email)) {
144 $resetUrl = str_replace('administrator/', '', $config->userFrameworkBaseURL) . 'index.php?option=com_users&view=reset';
145 $errors[$emailName] = ts('The email address %1 already has an account associated with it. <a href="%2">Have you forgotten your password?</a>',
146 [1 => $email, 2 => $resetUrl]
147 );
148 }
149 }
150 }
151
152 /**
153 * @inheritDoc
154 */
155 public function setTitle($title, $pageTitle = NULL) {
156 if (!$pageTitle) {
157 $pageTitle = $title;
158 }
159
160 $template = CRM_Core_Smarty::singleton();
161 $template->assign('pageTitle', $pageTitle);
162
163 $document = JFactory::getDocument();
164 $document->setTitle($title);
165 }
166
167 /**
168 * @inheritDoc
169 */
170 public function appendBreadCrumb($breadCrumbs) {
171 $template = CRM_Core_Smarty::singleton();
172 $bc = $template->get_template_vars('breadcrumb');
173
174 if (is_array($breadCrumbs)) {
175 foreach ($breadCrumbs as $crumbs) {
176 if (stripos($crumbs['url'], 'id%%')) {
177 $args = ['cid', 'mid'];
178 foreach ($args as $a) {
179 $val = CRM_Utils_Request::retrieve($a, 'Positive', CRM_Core_DAO::$_nullObject,
180 FALSE, NULL, $_GET
181 );
182 if ($val) {
183 $crumbs['url'] = str_ireplace("%%{$a}%%", $val, $crumbs['url']);
184 }
185 }
186 }
187 $bc[] = $crumbs;
188 }
189 }
190 $template->assign_by_ref('breadcrumb', $bc);
191 }
192
193 /**
194 * @inheritDoc
195 */
196 public function resetBreadCrumb() {
197 }
198
199 /**
200 * @inheritDoc
201 */
202 public function addHTMLHead($string = NULL) {
203 if ($string) {
204 $document = JFactory::getDocument();
205 $document->addCustomTag($string);
206 }
207 }
208
209 /**
210 * @inheritDoc
211 */
212 public function addStyleUrl($url, $region) {
213 if ($region == 'html-header') {
214 $document = JFactory::getDocument();
215 $document->addStyleSheet($url);
216 return TRUE;
217 }
218 return FALSE;
219 }
220
221 /**
222 * @inheritDoc
223 */
224 public function addStyle($code, $region) {
225 if ($region == 'html-header') {
226 $document = JFactory::getDocument();
227 $document->addStyleDeclaration($code);
228 return TRUE;
229 }
230 return FALSE;
231 }
232
233 /**
234 * @inheritDoc
235 */
236 public function url(
237 $path = NULL,
238 $query = NULL,
239 $absolute = FALSE,
240 $fragment = NULL,
241 $frontend = FALSE,
242 $forceBackend = FALSE,
243 $htmlize = TRUE
244 ) {
245 $config = CRM_Core_Config::singleton();
246 $separator = '&';
247 $Itemid = '';
248 $script = '';
249 $path = CRM_Utils_String::stripPathChars($path);
250
251 if ($config->userFrameworkFrontend) {
252 $script = 'index.php';
253
254 // Get Itemid using JInput::get()
255 $input = Joomla\CMS\Factory::getApplication()->input;
256 $itemIdNum = $input->get("Itemid");
257 if ($itemIdNum && (strpos($path, 'civicrm/payment/ipn') === FALSE)) {
258 $Itemid = "{$separator}Itemid=" . $itemIdNum;
259 }
260 }
261
262 if (isset($fragment)) {
263 $fragment = '#' . $fragment;
264 }
265
266 $base = $absolute ? $config->userFrameworkBaseURL : $config->useFrameworkRelativeBase;
267
268 if (!empty($query)) {
269 $url = "{$base}{$script}?option=com_civicrm{$separator}task={$path}{$Itemid}{$separator}{$query}{$fragment}";
270 }
271 else {
272 $url = "{$base}{$script}?option=com_civicrm{$separator}task={$path}{$Itemid}{$fragment}";
273 }
274
275 // gross hack for joomla, we are in the backend and want to send a frontend url
276 if ($frontend && $config->userFramework == 'Joomla') {
277 // handle both joomla v1.5 and v1.6, CRM-7939
278 $url = str_replace('/administrator/index2.php', '/index.php', $url);
279 $url = str_replace('/administrator/index.php', '/index.php', $url);
280
281 // CRM-8215
282 $url = str_replace('/administrator/', '/index.php', $url);
283 }
284 elseif ($forceBackend) {
285 if (defined('JVERSION')) {
286 $joomlaVersion = JVERSION;
287 }
288 else {
289 $jversion = new JVersion();
290 $joomlaVersion = $jversion->getShortVersion();
291 }
292
293 if (version_compare($joomlaVersion, '1.6') >= 0) {
294 $url = str_replace('/index.php', '/administrator/index.php', $url);
295 }
296 }
297 return $url;
298 }
299
300 /**
301 * Set the email address of the user.
302 *
303 * @param object $user
304 * Handle to the user object.
305 */
306 public function setEmail(&$user) {
307 global $database;
308 $query = $db->getQuery(TRUE);
309 $query->select($db->quoteName('email'))
310 ->from($db->quoteName('#__users'))
311 ->where($db->quoteName('id') . ' = ' . $user->id);
312 $database->setQuery($query);
313 $user->email = $database->loadResult();
314 }
315
316 /**
317 * @inheritDoc
318 */
319 public function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) {
320 require_once 'DB.php';
321
322 $config = CRM_Core_Config::singleton();
323 $user = NULL;
324
325 if ($loadCMSBootstrap) {
326 $bootStrapParams = [];
327 if ($name && $password) {
328 $bootStrapParams = [
329 'name' => $name,
330 'pass' => $password,
331 ];
332 }
333 CRM_Utils_System::loadBootStrap($bootStrapParams, TRUE, TRUE, FALSE);
334 }
335
336 jimport('joomla.application.component.helper');
337 jimport('joomla.database.table');
338 jimport('joomla.user.helper');
339
340 $JUserTable = JTable::getInstance('User', 'JTable');
341
342 $db = $JUserTable->getDbo();
343 $query = $db->getQuery(TRUE);
344 $query->select('id, name, username, email, password');
345 $query->from($JUserTable->getTableName());
346 $query->where('(LOWER(username) = LOWER(\'' . $name . '\')) AND (block = 0)');
347 $db->setQuery($query, 0, 0);
348 $users = $db->loadObjectList();
349
350 $row = [];
351 if (count($users)) {
352 $row = $users[0];
353 }
354
355 $joomlaBase = self::getBasePath();
356 self::getJVersion($joomlaBase);
357
358 if (!empty($row)) {
359 $dbPassword = $row->password;
360 $dbId = $row->id;
361 $dbEmail = $row->email;
362
363 if (version_compare(JVERSION, '2.5.18', 'lt') ||
364 (version_compare(JVERSION, '3.0', 'ge') && version_compare(JVERSION, '3.2.1', 'lt'))
365 ) {
366 // now check password
367 list($hash, $salt) = explode(':', $dbPassword);
368 $cryptpass = md5($password . $salt);
369 if ($hash != $cryptpass) {
370 return FALSE;
371 }
372 }
373 else {
374 if (!JUserHelper::verifyPassword($password, $dbPassword, $dbId)) {
375 return FALSE;
376 }
377
378 if (version_compare(JVERSION, '3.8.0', 'ge')) {
379 jimport('joomla.application.helper');
380 jimport('joomla.application.cms');
381 jimport('joomla.application.administrator');
382 }
383 //include additional files required by Joomla 3.2.1+
384 elseif (version_compare(JVERSION, '3.2.1', 'ge')) {
385 require_once $joomlaBase . '/libraries/cms/application/helper.php';
386 require_once $joomlaBase . '/libraries/cms/application/cms.php';
387 require_once $joomlaBase . '/libraries/cms/application/administrator.php';
388 }
389 }
390
391 CRM_Core_BAO_UFMatch::synchronizeUFMatch($row, $dbId, $dbEmail, 'Joomla');
392 $contactID = CRM_Core_BAO_UFMatch::getContactId($dbId);
393 if (!$contactID) {
394 return FALSE;
395 }
396 return [$contactID, $dbId, mt_rand()];
397 }
398
399 return FALSE;
400 }
401
402 /**
403 * Set a init session with user object.
404 *
405 * @param array $data
406 * Array with user specific data.
407 */
408 public function setUserSession($data) {
409 list($userID, $ufID) = $data;
410 $user = new JUser($ufID);
411 $session = JFactory::getSession();
412 $session->set('user', $user);
413
414 parent::setUserSession($data);
415 }
416
417 /**
418 * FIXME: Do something
419 *
420 * @param string $message
421 */
422 public function setMessage($message) {
423 }
424
425 /**
426 * @param \string $username
427 * @param \string $password
428 *
429 * @return bool
430 */
431 public function loadUser($username, $password = NULL) {
432 $uid = JUserHelper::getUserId($username);
433 if (empty($uid)) {
434 return FALSE;
435 }
436 $contactID = CRM_Core_BAO_UFMatch::getContactId($uid);
437 if (!empty($password)) {
438 $instance = JFactory::getApplication('site');
439 $params = [
440 'username' => $username,
441 'password' => $password,
442 ];
443 //perform the login action
444 $instance->login($params);
445 }
446
447 // Save details in Joomla session
448 $user = JFactory::getUser($uid);
449 $jsession = JFactory::getSession();
450 $jsession->set('user', $user);
451
452 // Save details in Civi session
453 $session = CRM_Core_Session::singleton();
454 $session->set('ufID', $uid);
455 $session->set('userID', $contactID);
456 return TRUE;
457 }
458
459 /**
460 * FIXME: Use CMS-native approach
461 * @throws \CRM_Core_Exception.
462 */
463 public function permissionDenied() {
464 throw new CRM_Core_Exception(ts('You do not have permission to access this page.'));
465 }
466
467 /**
468 * @inheritDoc
469 */
470 public function logout() {
471 session_destroy();
472 CRM_Utils_System::setHttpHeader("Location", "index.php");
473 }
474
475 /**
476 * @inheritDoc
477 */
478 public function getUFLocale() {
479 if (defined('_JEXEC')) {
480 $conf = JFactory::getConfig();
481 $locale = $conf->get('language');
482 return str_replace('-', '_', $locale);
483 }
484 return NULL;
485 }
486
487 /**
488 * @inheritDoc
489 */
490 public function setUFLocale($civicrm_language) {
491 // TODO
492 return TRUE;
493 }
494
495 /**
496 * @inheritDoc
497 */
498 public function getVersion() {
499 if (class_exists('JVersion')) {
500 $version = new JVersion();
501 return $version->getShortVersion();
502 }
503 else {
504 return 'Unknown';
505 }
506 }
507
508 public function getJVersion($joomlaBase) {
509 // Files may be in different places depending on Joomla version
510 if (!defined('JVERSION')) {
511 // Joomla 3.8.0+
512 $versionPhp = $joomlaBase . '/libraries/src/Version.php';
513 if (!file_exists($versionPhp)) {
514 // Joomla < 3.8.0
515 $versionPhp = $joomlaBase . '/libraries/cms/version/version.php';
516 }
517 require $versionPhp;
518 $jversion = new JVersion();
519 define('JVERSION', $jversion->getShortVersion());
520 }
521 }
522
523 /**
524 * Setup the base path related constant.
525 * @return mixed
526 */
527 public function getBasePath() {
528 global $civicrm_root;
529 $joomlaPath = explode(DIRECTORY_SEPARATOR . 'administrator', $civicrm_root);
530 $joomlaBase = $joomlaPath[0];
531 return $joomlaBase;
532 }
533
534 /**
535 * Load joomla bootstrap.
536 *
537 * @param array $params
538 * with uid or name and password.
539 * @param bool $loadUser
540 * load cms user?.
541 * @param bool|\throw $throwError throw error on failure?
542 * @param null $realPath
543 * @param bool $loadDefines
544 *
545 * @return bool
546 */
547 public function loadBootStrap($params = [], $loadUser = TRUE, $throwError = TRUE, $realPath = NULL, $loadDefines = TRUE) {
548 $joomlaBase = self::getBasePath();
549
550 // load BootStrap here if needed
551 // We are a valid Joomla entry point.
552 // dev/core#1384 Use DS to ensure a correct JPATH_BASE in Windows
553 if (!defined('_JEXEC') && $loadDefines) {
554 define('_JEXEC', 1);
555 define('DS', DIRECTORY_SEPARATOR);
556 define('JPATH_BASE', $joomlaBase . DS . 'administrator');
557 require $joomlaBase . '/administrator/includes/defines.php';
558 }
559
560 // Get the framework.
561 if (file_exists($joomlaBase . '/libraries/import.legacy.php')) {
562 require $joomlaBase . '/libraries/import.legacy.php';
563 }
564 require $joomlaBase . '/libraries/cms.php';
565 self::getJVersion($joomlaBase);
566
567 if (version_compare(JVERSION, '3.8', 'lt')) {
568 require $joomlaBase . '/libraries/import.php';
569 require $joomlaBase . '/libraries/joomla/event/dispatcher.php';
570 }
571
572 require_once $joomlaBase . '/configuration.php';
573
574 if (version_compare(JVERSION, '3.0', 'lt')) {
575 require $joomlaBase . '/libraries/joomla/environment/uri.php';
576 require $joomlaBase . '/libraries/joomla/application/component/helper.php';
577 }
578 elseif (version_compare(JVERSION, '3.8', 'lt')) {
579 jimport('joomla.environment.uri');
580 }
581
582 if (version_compare(JVERSION, '3.8', 'lt')) {
583 jimport('joomla.application.cli');
584 }
585
586 if (!defined('JDEBUG')) {
587 define('JDEBUG', FALSE);
588 }
589
590 // Set timezone for Joomla on Cron
591 $config = JFactory::getConfig();
592 $timezone = $config->get('offset');
593 if ($timezone) {
594 date_default_timezone_set($timezone);
595 CRM_Core_Config::singleton()->userSystem->setMySQLTimeZone();
596 }
597
598 // CRM-14281 Joomla wasn't available during bootstrap, so hook_civicrm_config never executes.
599 $config = CRM_Core_Config::singleton();
600 CRM_Utils_Hook::config($config);
601
602 return TRUE;
603 }
604
605 /**
606 * @inheritDoc
607 */
608 public function isUserLoggedIn() {
609 $user = JFactory::getUser();
610 return !$user->guest;
611 }
612
613 /**
614 * @inheritDoc
615 */
616 public function isUserRegistrationPermitted() {
617 $userParams = JComponentHelper::getParams('com_users');
618 if (!$userParams->get('allowUserRegistration')) {
619 return FALSE;
620 }
621 return TRUE;
622 }
623
624 /**
625 * @inheritDoc
626 */
627 public function isPasswordUserGenerated() {
628 return TRUE;
629 }
630
631 /**
632 * @inheritDoc
633 */
634 public function getLoggedInUfID() {
635 $user = JFactory::getUser();
636 return ($user->guest) ? NULL : $user->id;
637 }
638
639 /**
640 * @inheritDoc
641 */
642 public function getLoggedInUniqueIdentifier() {
643 $user = JFactory::getUser();
644 return $this->getUniqueIdentifierFromUserObject($user);
645 }
646
647 /**
648 * @inheritDoc
649 */
650 public function getUser($contactID) {
651 $user_details = parent::getUser($contactID);
652 $user = JFactory::getUser($user_details['id']);
653 $user_details['name'] = $user->name;
654 return $user_details;
655 }
656
657 /**
658 * @inheritDoc
659 */
660 public function getUserIDFromUserObject($user) {
661 return !empty($user->id) ? $user->id : NULL;
662 }
663
664 /**
665 * @inheritDoc
666 */
667 public function getUniqueIdentifierFromUserObject($user) {
668 return ($user->guest) ? NULL : $user->email;
669 }
670
671 /**
672 * @inheritDoc
673 */
674 public function getTimeZoneString() {
675 $timezone = JFactory::getConfig()->get('offset');
676 return !$timezone ? date_default_timezone_get() : $timezone;
677 }
678
679 /**
680 * Get a list of all installed modules, including enabled and disabled ones
681 *
682 * @return array
683 * CRM_Core_Module
684 */
685 public function getModules() {
686 $result = [];
687
688 $db = JFactory::getDbo();
689 $query = $db->getQuery(TRUE);
690 $query->select('type, folder, element, enabled')
691 ->from('#__extensions')
692 ->where('type =' . $db->Quote('plugin'));
693 $plugins = $db->setQuery($query)->loadAssocList();
694 foreach ($plugins as $plugin) {
695 // question: is the folder really a critical part of the plugin's name?
696 $name = implode('.', ['joomla', $plugin['type'], $plugin['folder'], $plugin['element']]);
697 $result[] = new CRM_Core_Module($name, !empty($plugin['enabled']));
698 }
699
700 return $result;
701 }
702
703 /**
704 * @inheritDoc
705 */
706 public function getLoginURL($destination = '') {
707 $config = CRM_Core_Config::singleton();
708 $loginURL = $config->userFrameworkBaseURL;
709 $loginURL = str_replace('administrator/', '', $loginURL);
710 $loginURL .= 'index.php?option=com_users&view=login';
711
712 //CRM-14872 append destination
713 if (!empty($destination)) {
714 $loginURL .= '&return=' . urlencode(base64_encode($destination));
715 }
716 return $loginURL;
717 }
718
719 /**
720 * @inheritDoc
721 */
722 public function getLoginDestination(&$form) {
723 $args = NULL;
724
725 $id = $form->get('id');
726 if ($id) {
727 $args .= "&id=$id";
728 }
729 else {
730 $gid = $form->get('gid');
731 if ($gid) {
732 $args .= "&gid=$gid";
733 }
734 else {
735 // Setup Personal Campaign Page link uses pageId
736 $pageId = $form->get('pageId');
737 if ($pageId) {
738 $component = $form->get('component');
739 $args .= "&pageId=$pageId&component=$component&action=add";
740 }
741 }
742 }
743
744 $destination = NULL;
745 if ($args) {
746 // append destination so user is returned to form they came from after login
747 $args = 'reset=1' . $args;
748 $destination = CRM_Utils_System::url(CRM_Utils_System::currentPath(), $args, TRUE, NULL, FALSE, TRUE);
749 }
750
751 return $destination;
752 }
753
754 /**
755 * Determine the location of the CMS root.
756 *
757 * @return string|NULL
758 * local file system path to CMS root, or NULL if it cannot be determined
759 */
760 public function cmsRootPath() {
761 global $civicrm_paths;
762 if (!empty($civicrm_paths['cms.root']['path'])) {
763 return $civicrm_paths['cms.root']['path'];
764 }
765
766 list($url, $siteName, $siteRoot) = $this->getDefaultSiteSettings();
767 if (file_exists("$siteRoot/administrator/index.php")) {
768 return $siteRoot;
769 }
770 return NULL;
771 }
772
773 /**
774 * @inheritDoc
775 */
776 public function getDefaultSiteSettings($dir = NULL) {
777 $config = CRM_Core_Config::singleton();
778 $url = preg_replace(
779 '|/administrator|',
780 '',
781 $config->userFrameworkBaseURL
782 );
783 // CRM-19453 revisited. Under Windows, the pattern wasn't recognised.
784 // This is the original pattern, but it doesn't work under Windows.
785 // By setting the pattern to the one used before the change first and only
786 // changing it means that the change code only affects Windows users.
787 $pattern = '|/media/civicrm/.*$|';
788 if (DIRECTORY_SEPARATOR == '\\') {
789 // This regular expression will handle Windows as well as Linux
790 // and any combination of forward and back slashes in directory
791 // separators. We only apply it if the directory separator is the one
792 // used by Windows.
793 $pattern = '|[\\\\/]media[\\\\/]civicrm[\\\\/].*$|';
794 }
795 $siteRoot = preg_replace(
796 $pattern,
797 '',
798 $config->imageUploadDir
799 );
800 return [$url, NULL, $siteRoot];
801 }
802
803 /**
804 * @inheritDoc
805 */
806 public function getUserRecordUrl($contactID) {
807 $uid = CRM_Core_BAO_UFMatch::getUFId($contactID);
808 $userRecordUrl = NULL;
809 // if logged in user has user edit access, then allow link to other users joomla profile
810 if (JFactory::getUser()->authorise('core.edit', 'com_users')) {
811 return CRM_Core_Config::singleton()->userFrameworkBaseURL . "index.php?option=com_users&view=user&task=user.edit&id=" . $uid;
812 }
813 elseif (CRM_Core_Session::singleton()->get('userID') == $contactID) {
814 return CRM_Core_Config::singleton()->userFrameworkBaseURL . "index.php?option=com_admin&view=profile&layout=edit&id=" . $uid;
815 }
816 }
817
818 /**
819 * @inheritDoc
820 */
821 public function checkPermissionAddUser() {
822 if (JFactory::getUser()->authorise('core.create', 'com_users')) {
823 return TRUE;
824 }
825 }
826
827 /**
828 * @inheritDoc
829 */
830 public function synchronizeUsers() {
831 $config = CRM_Core_Config::singleton();
832 if (PHP_SAPI != 'cli') {
833 set_time_limit(300);
834 }
835 $id = 'id';
836 $mail = 'email';
837 $name = 'name';
838
839 $JUserTable = &JTable::getInstance('User', 'JTable');
840
841 $db = $JUserTable->getDbo();
842 $query = $db->getQuery(TRUE);
843 $query->select($id . ', ' . $mail . ', ' . $name);
844 $query->from($JUserTable->getTableName());
845 $query->where($mail != '');
846
847 $db->setQuery($query);
848 $users = $db->loadObjectList();
849
850 $user = new StdClass();
851 $uf = $config->userFramework;
852 $contactCount = 0;
853 $contactCreated = 0;
854 $contactMatching = 0;
855 for ($i = 0; $i < count($users); $i++) {
856 $user->$id = $users[$i]->$id;
857 $user->$mail = $users[$i]->$mail;
858 $user->$name = $users[$i]->$name;
859 $contactCount++;
860 if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($user,
861 $users[$i]->$id,
862 $users[$i]->$mail,
863 $uf,
864 1,
865 'Individual',
866 TRUE
867 )
868 ) {
869 $contactCreated++;
870 }
871 else {
872 $contactMatching++;
873 }
874 }
875
876 return [
877 'contactCount' => $contactCount,
878 'contactMatching' => $contactMatching,
879 'contactCreated' => $contactCreated,
880 ];
881 }
882
883 /**
884 * Determine the location of the CiviCRM source tree.
885 *
886 * FIXME:
887 * 1. This was pulled out from a bigger function. It should be split
888 * into even smaller pieces and marked abstract.
889 * 2. This would be easier to compute by a calling a CMS API, but
890 * for whatever reason we take the hard way.
891 *
892 * @return array
893 * - url: string. ex: "http://example.com/sites/all/modules/civicrm"
894 * - path: string. ex: "/var/www/sites/all/modules/civicrm"
895 */
896 public function getCiviSourceStorage() {
897 global $civicrm_root;
898 if (!defined('CIVICRM_UF_BASEURL')) {
899 throw new RuntimeException('Undefined constant: CIVICRM_UF_BASEURL');
900 }
901 $baseURL = CRM_Utils_File::addTrailingSlash(CIVICRM_UF_BASEURL, '/');
902 if (CRM_Utils_System::isSSL()) {
903 $baseURL = str_replace('http://', 'https://', $baseURL);
904 }
905
906 // For Joomla CiviCRM Core files always live within the admistrator folder and $base_url is different on the frontend compared to the backend.
907 if (strpos($baseURL, 'administrator') === FALSE) {
908 $userFrameworkResourceURL = $baseURL . "administrator/components/com_civicrm/civicrm/";
909 }
910 else {
911 $userFrameworkResourceURL = $baseURL . "components/com_civicrm/civicrm/";
912 }
913
914 return [
915 'url' => CRM_Utils_File::addTrailingSlash($userFrameworkResourceURL, '/'),
916 'path' => CRM_Utils_File::addTrailingSlash($civicrm_root),
917 ];
918 }
919
920 }