3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Joomla specific stuff goes here.
21 class CRM_Utils_System_Joomla
extends CRM_Utils_System_Base
{
26 public function __construct() {
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
33 $this->is_drupal
= FALSE;
39 public function createUser(&$params, $mail) {
40 $baseDir = JPATH_SITE
;
41 require_once $baseDir . '/components/com_users/models/registration.php';
43 $userParams = JComponentHelper
::getParams('com_users');
44 $model = new UsersModelRegistration();
47 // get the default usertype
48 $userType = $userParams->get('new_usertype');
53 if (isset($params['name'])) {
54 $fullname = trim($params['name']);
56 elseif (isset($params['contactID'])) {
57 $fullname = trim(CRM_Contact_BAO_Contact
::displayName($params['contactID']));
60 $fullname = trim($params['cms_name']);
63 // Prepare the values for a new Joomla user.
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]);
70 $lang = JFactory
::getLanguage();
71 $lang->load('com_users', $baseDir);
73 $register = $model->register($values);
75 $ufID = JUserHelper
::getUserId($values['username']);
82 public function updateCMSName($ufID, $ufName) {
83 $ufID = CRM_Utils_Type
::escape($ufID, 'Integer');
84 $ufName = CRM_Utils_Type
::escape($ufName, 'String');
87 $user = JUser
::getInstance($ufID);
89 $values['email'] = $ufName;
96 * Check if username and email exists in the Joomla db.
98 * @param array $params
99 * Array of name and mail values.
100 * @param array $errors
102 * @param string $emailName
103 * Field label for the 'email'.
105 public function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') {
106 $config = CRM_Core_Config
::singleton();
108 $name = CRM_Utils_Array
::value('name', $params);
109 $email = CRM_Utils_Array
::value('mail', $params);
110 //don't allow the special characters and min. username length is two
111 //regex \\ to match a single backslash would become '/\\\\/'
112 $isNotValid = (bool) preg_match('/[\<|\>|\"|\'|\%|\;|\(|\)|\&|\\\\|\/]/im', $name);
113 if ($isNotValid ||
strlen($name) < 2) {
114 $errors['cms_name'] = ts('Your username contains invalid characters or is too short');
117 $JUserTable = &JTable
::getInstance('User', 'JTable');
119 $db = $JUserTable->getDbo();
120 $query = $db->getQuery(TRUE);
121 $query->select('username, email');
122 $query->from($JUserTable->getTableName());
124 // LOWER in query below roughly translates to 'hurt my database without deriving any benefit' See CRM-19811.
125 $query->where('(LOWER(username) = LOWER(' . $db->quote($name) . ')) OR (LOWER(email) = LOWER(' . $db->quote($email) . '))');
126 $db->setQuery($query, 0, 10);
127 $users = $db->loadAssocList();
135 $dbName = $row['username'] ??
NULL;
136 $dbEmail = $row['email'] ??
NULL;
137 if (strtolower($dbName) == strtolower($name)) {
138 $errors['cms_name'] = ts('The username %1 is already taken. Please select another username.',
142 if (strtolower($dbEmail) == strtolower($email)) {
143 $resetUrl = str_replace('administrator/', '', $config->userFrameworkBaseURL
) . 'index.php?option=com_users&view=reset';
144 $errors[$emailName] = ts('The email address %1 already has an account associated with it. <a href="%2">Have you forgotten your password?</a>',
145 [1 => $email, 2 => $resetUrl]
154 public function setTitle($title, $pageTitle = NULL) {
159 $template = CRM_Core_Smarty
::singleton();
160 $template->assign('pageTitle', $pageTitle);
162 $document = JFactory
::getDocument();
163 $document->setTitle($title);
169 public function appendBreadCrumb($breadCrumbs) {
170 $template = CRM_Core_Smarty
::singleton();
171 $bc = $template->get_template_vars('breadcrumb');
173 if (is_array($breadCrumbs)) {
174 foreach ($breadCrumbs as $crumbs) {
175 if (stripos($crumbs['url'], 'id%%')) {
176 $args = ['cid', 'mid'];
177 foreach ($args as $a) {
178 $val = CRM_Utils_Request
::retrieve($a, 'Positive', CRM_Core_DAO
::$_nullObject,
182 $crumbs['url'] = str_ireplace("%%{$a}%%", $val, $crumbs['url']);
189 $template->assign_by_ref('breadcrumb', $bc);
195 public function resetBreadCrumb() {
201 public function addHTMLHead($string = NULL) {
203 $document = JFactory
::getDocument();
204 $document->addCustomTag($string);
211 public function addStyleUrl($url, $region) {
212 if ($region == 'html-header') {
213 $document = JFactory
::getDocument();
214 $document->addStyleSheet($url);
223 public function addStyle($code, $region) {
224 if ($region == 'html-header') {
225 $document = JFactory
::getDocument();
226 $document->addStyleDeclaration($code);
241 $forceBackend = FALSE,
244 $config = CRM_Core_Config
::singleton();
248 $path = CRM_Utils_String
::stripPathChars($path);
250 if ($config->userFrameworkFrontend
) {
251 $script = 'index.php';
253 // Get Itemid using JInput::get()
254 $input = Joomla\CMS\Factory
::getApplication()->input
;
255 $itemIdNum = $input->get("Itemid");
256 if ($itemIdNum && (strpos($path, 'civicrm/payment/ipn') === FALSE)) {
257 $Itemid = "{$separator}Itemid=" . $itemIdNum;
261 if (isset($fragment)) {
262 $fragment = '#' . $fragment;
265 $base = $absolute ?
$config->userFrameworkBaseURL
: $config->useFrameworkRelativeBase
;
267 if (!empty($query)) {
268 $url = "{$base}{$script}?option=com_civicrm{$separator}task={$path}{$Itemid}{$separator}{$query}{$fragment}";
271 $url = "{$base}{$script}?option=com_civicrm{$separator}task={$path}{$Itemid}{$fragment}";
274 // gross hack for joomla, we are in the backend and want to send a frontend url
275 if ($frontend && $config->userFramework
== 'Joomla') {
276 // handle both joomla v1.5 and v1.6, CRM-7939
277 $url = str_replace('/administrator/index2.php', '/index.php', $url);
278 $url = str_replace('/administrator/index.php', '/index.php', $url);
281 $url = str_replace('/administrator/', '/index.php', $url);
283 elseif ($forceBackend) {
284 if (defined('JVERSION')) {
285 $joomlaVersion = JVERSION
;
288 $jversion = new JVersion();
289 $joomlaVersion = $jversion->getShortVersion();
292 if (version_compare($joomlaVersion, '1.6') >= 0) {
293 $url = str_replace('/index.php', '/administrator/index.php', $url);
300 * Set the email address of the user.
302 * @param object $user
303 * Handle to the user object.
305 public function setEmail(&$user) {
307 $query = $db->getQuery(TRUE);
308 $query->select($db->quoteName('email'))
309 ->from($db->quoteName('#__users'))
310 ->where($db->quoteName('id') . ' = ' . $user->id
);
311 $database->setQuery($query);
312 $user->email
= $database->loadResult();
318 public function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) {
319 require_once 'DB.php';
321 $config = CRM_Core_Config
::singleton();
324 if ($loadCMSBootstrap) {
325 $bootStrapParams = [];
326 if ($name && $password) {
332 CRM_Utils_System
::loadBootStrap($bootStrapParams, TRUE, TRUE, FALSE);
335 jimport('joomla.application.component.helper');
336 jimport('joomla.database.table');
337 jimport('joomla.user.helper');
339 $JUserTable = JTable
::getInstance('User', 'JTable');
341 $db = $JUserTable->getDbo();
342 $query = $db->getQuery(TRUE);
343 $query->select('id, name, username, email, password');
344 $query->from($JUserTable->getTableName());
345 $query->where('(LOWER(username) = LOWER(' . $db->quote($name) . ')) AND (block = 0)');
346 $db->setQuery($query, 0, 0);
347 $users = $db->loadObjectList();
354 $joomlaBase = self
::getBasePath();
355 self
::getJVersion($joomlaBase);
358 $dbPassword = $row->password
;
360 $dbEmail = $row->email
;
362 if (version_compare(JVERSION
, '2.5.18', 'lt') ||
363 (version_compare(JVERSION
, '3.0', 'ge') && version_compare(JVERSION
, '3.2.1', 'lt'))
365 // now check password
366 list($hash, $salt) = explode(':', $dbPassword);
367 $cryptpass = md5($password . $salt);
368 if ($hash != $cryptpass) {
373 if (!JUserHelper
::verifyPassword($password, $dbPassword, $dbId)) {
377 if (version_compare(JVERSION
, '3.8.0', 'ge')) {
378 jimport('joomla.application.helper');
379 jimport('joomla.application.cms');
380 jimport('joomla.application.administrator');
382 //include additional files required by Joomla 3.2.1+
383 elseif (version_compare(JVERSION
, '3.2.1', 'ge')) {
384 require_once $joomlaBase . '/libraries/cms/application/helper.php';
385 require_once $joomlaBase . '/libraries/cms/application/cms.php';
386 require_once $joomlaBase . '/libraries/cms/application/administrator.php';
390 CRM_Core_BAO_UFMatch
::synchronizeUFMatch($row, $dbId, $dbEmail, 'Joomla');
391 $contactID = CRM_Core_BAO_UFMatch
::getContactId($dbId);
395 return [$contactID, $dbId, mt_rand()];
402 * Set a init session with user object.
405 * Array with user specific data.
407 public function setUserSession($data) {
408 list($userID, $ufID) = $data;
409 $user = new JUser($ufID);
410 $session = JFactory
::getSession();
411 $session->set('user', $user);
413 parent
::setUserSession($data);
417 * FIXME: Do something
419 * @param string $message
421 public function setMessage($message) {
425 * @param \string $username
426 * @param \string $password
430 public function loadUser($username, $password = NULL) {
431 $uid = JUserHelper
::getUserId($username);
435 $contactID = CRM_Core_BAO_UFMatch
::getContactId($uid);
436 if (!empty($password)) {
437 $instance = JFactory
::getApplication('site');
439 'username' => $username,
440 'password' => $password,
442 //perform the login action
443 $instance->login($params);
446 // Save details in Joomla session
447 $user = JFactory
::getUser($uid);
448 $jsession = JFactory
::getSession();
449 $jsession->set('user', $user);
451 // Save details in Civi session
452 $session = CRM_Core_Session
::singleton();
453 $session->set('ufID', $uid);
454 $session->set('userID', $contactID);
458 public function getUfId($username) {
459 jimport('joomla.user.helper');
460 $uid = JUserHelper
::getUserId($username);
461 return empty($uid) ?
NULL : $uid;
465 * FIXME: Use CMS-native approach
466 * @throws \CRM_Core_Exception.
468 public function permissionDenied() {
469 throw new CRM_Core_Exception(ts('You do not have permission to access this page.'));
475 public function logout() {
477 CRM_Utils_System
::setHttpHeader("Location", "index.php");
483 public function getUFLocale() {
484 if (defined('_JEXEC')) {
485 $conf = JFactory
::getConfig();
486 $locale = $conf->get('language');
487 return str_replace('-', '_', $locale);
495 public function setUFLocale($civicrm_language) {
503 public function getVersion() {
504 if (class_exists('JVersion')) {
505 $version = new JVersion();
506 return $version->getShortVersion();
513 public function getJVersion($joomlaBase) {
514 // Files may be in different places depending on Joomla version
515 if (!defined('JVERSION')) {
517 $versionPhp = $joomlaBase . '/libraries/src/Version.php';
518 if (!file_exists($versionPhp)) {
520 $versionPhp = $joomlaBase . '/libraries/cms/version/version.php';
523 $jversion = new JVersion();
524 define('JVERSION', $jversion->getShortVersion());
529 * Setup the base path related constant.
532 public function getBasePath() {
533 global $civicrm_root;
534 $joomlaPath = explode(DIRECTORY_SEPARATOR
. 'administrator', $civicrm_root);
535 $joomlaBase = $joomlaPath[0];
540 * Load joomla bootstrap.
542 * @param array $params
543 * with uid or name and password.
544 * @param bool $loadUser
546 * @param bool|\throw $throwError throw error on failure?
547 * @param null $realPath
548 * @param bool $loadDefines
552 public function loadBootStrap($params = [], $loadUser = TRUE, $throwError = TRUE, $realPath = NULL, $loadDefines = TRUE) {
553 $joomlaBase = self
::getBasePath();
555 // load BootStrap here if needed
556 // We are a valid Joomla entry point.
557 // dev/core#1384 Use DS to ensure a correct JPATH_BASE in Windows
558 if (!defined('_JEXEC') && $loadDefines) {
560 define('DS', DIRECTORY_SEPARATOR
);
561 define('JPATH_BASE', $joomlaBase . DS
. 'administrator');
562 require $joomlaBase . '/administrator/includes/defines.php';
565 // Get the framework.
566 if (file_exists($joomlaBase . '/libraries/import.legacy.php')) {
567 require $joomlaBase . '/libraries/import.legacy.php';
569 require $joomlaBase . '/libraries/cms.php';
570 self
::getJVersion($joomlaBase);
572 if (version_compare(JVERSION
, '3.8', 'lt')) {
573 require $joomlaBase . '/libraries/import.php';
574 require $joomlaBase . '/libraries/joomla/event/dispatcher.php';
577 require_once $joomlaBase . '/configuration.php';
579 if (version_compare(JVERSION
, '3.0', 'lt')) {
580 require $joomlaBase . '/libraries/joomla/environment/uri.php';
581 require $joomlaBase . '/libraries/joomla/application/component/helper.php';
583 elseif (version_compare(JVERSION
, '3.8', 'lt')) {
584 jimport('joomla.environment.uri');
587 if (version_compare(JVERSION
, '3.8', 'lt')) {
588 jimport('joomla.application.cli');
591 if (!defined('JDEBUG')) {
592 define('JDEBUG', FALSE);
595 // Set timezone for Joomla on Cron
596 $config = JFactory
::getConfig();
597 $timezone = $config->get('offset');
599 date_default_timezone_set($timezone);
600 CRM_Core_Config
::singleton()->userSystem
->setMySQLTimeZone();
603 // CRM-14281 Joomla wasn't available during bootstrap, so hook_civicrm_config never executes.
604 $config = CRM_Core_Config
::singleton();
605 CRM_Utils_Hook
::config($config);
613 public function isUserLoggedIn() {
614 $user = JFactory
::getUser();
615 return !$user->guest
;
621 public function isUserRegistrationPermitted() {
622 $userParams = JComponentHelper
::getParams('com_users');
623 if (!$userParams->get('allowUserRegistration')) {
632 public function isPasswordUserGenerated() {
639 public function getLoggedInUfID() {
640 $user = JFactory
::getUser();
641 return ($user->guest
) ?
NULL : $user->id
;
647 public function getLoggedInUniqueIdentifier() {
648 $user = JFactory
::getUser();
649 return $this->getUniqueIdentifierFromUserObject($user);
655 public function getUser($contactID) {
656 $user_details = parent
::getUser($contactID);
657 $user = JFactory
::getUser($user_details['id']);
658 $user_details['name'] = $user->name
;
659 return $user_details;
665 public function getUserIDFromUserObject($user) {
666 return !empty($user->id
) ?
$user->id
: NULL;
672 public function getUniqueIdentifierFromUserObject($user) {
673 return ($user->guest
) ?
NULL : $user->email
;
679 public function getTimeZoneString() {
680 $timezone = JFactory
::getConfig()->get('offset');
681 return !$timezone ?
date_default_timezone_get() : $timezone;
685 * Get a list of all installed modules, including enabled and disabled ones
690 public function getModules() {
693 $db = JFactory
::getDbo();
694 $query = $db->getQuery(TRUE);
695 $query->select('type, folder, element, enabled')
696 ->from('#__extensions')
697 ->where('type =' . $db->Quote('plugin'));
698 $plugins = $db->setQuery($query)->loadAssocList();
699 foreach ($plugins as $plugin) {
700 // question: is the folder really a critical part of the plugin's name?
701 $name = implode('.', ['joomla', $plugin['type'], $plugin['folder'], $plugin['element']]);
702 $result[] = new CRM_Core_Module($name, !empty($plugin['enabled']));
711 public function getLoginURL($destination = '') {
712 $config = CRM_Core_Config
::singleton();
713 $loginURL = $config->userFrameworkBaseURL
;
714 $loginURL = str_replace('administrator/', '', $loginURL);
715 $loginURL .= 'index.php?option=com_users&view=login';
717 //CRM-14872 append destination
718 if (!empty($destination)) {
719 $loginURL .= '&return=' . urlencode(base64_encode($destination));
727 public function getLoginDestination(&$form) {
730 $id = $form->get('id');
735 $gid = $form->get('gid');
737 $args .= "&gid=$gid";
740 // Setup Personal Campaign Page link uses pageId
741 $pageId = $form->get('pageId');
743 $component = $form->get('component');
744 $args .= "&pageId=$pageId&component=$component&action=add";
751 // append destination so user is returned to form they came from after login
752 $args = 'reset=1' . $args;
753 $destination = CRM_Utils_System
::url(CRM_Utils_System
::currentPath(), $args, TRUE, NULL, FALSE, TRUE);
760 * Determine the location of the CMS root.
762 * @return string|NULL
763 * local file system path to CMS root, or NULL if it cannot be determined
765 public function cmsRootPath() {
766 global $civicrm_paths;
767 if (!empty($civicrm_paths['cms.root']['path'])) {
768 return $civicrm_paths['cms.root']['path'];
771 list($url, $siteName, $siteRoot) = $this->getDefaultSiteSettings();
772 if (file_exists("$siteRoot/administrator/index.php")) {
781 public function getDefaultSiteSettings($dir = NULL) {
782 $config = CRM_Core_Config
::singleton();
786 $config->userFrameworkBaseURL
788 // CRM-19453 revisited. Under Windows, the pattern wasn't recognised.
789 // This is the original pattern, but it doesn't work under Windows.
790 // By setting the pattern to the one used before the change first and only
791 // changing it means that the change code only affects Windows users.
792 $pattern = '|/media/civicrm/.*$|';
793 if (DIRECTORY_SEPARATOR
== '\\') {
794 // This regular expression will handle Windows as well as Linux
795 // and any combination of forward and back slashes in directory
796 // separators. We only apply it if the directory separator is the one
798 $pattern = '|[\\\\/]media[\\\\/]civicrm[\\\\/].*$|';
800 $siteRoot = preg_replace(
803 $config->imageUploadDir
805 return [$url, NULL, $siteRoot];
811 public function getUserRecordUrl($contactID) {
812 $uid = CRM_Core_BAO_UFMatch
::getUFId($contactID);
813 $userRecordUrl = NULL;
814 // if logged in user has user edit access, then allow link to other users joomla profile
815 if (JFactory
::getUser()->authorise('core.edit', 'com_users')) {
816 return CRM_Core_Config
::singleton()->userFrameworkBaseURL
. "index.php?option=com_users&view=user&task=user.edit&id=" . $uid;
818 elseif (CRM_Core_Session
::singleton()->get('userID') == $contactID) {
819 return CRM_Core_Config
::singleton()->userFrameworkBaseURL
. "index.php?option=com_admin&view=profile&layout=edit&id=" . $uid;
826 public function checkPermissionAddUser() {
827 if (JFactory
::getUser()->authorise('core.create', 'com_users')) {
835 public function synchronizeUsers() {
836 $config = CRM_Core_Config
::singleton();
837 if (PHP_SAPI
!= 'cli') {
844 $JUserTable = JTable
::getInstance('User', 'JTable');
846 $db = $JUserTable->getDbo();
847 $query = $db->getQuery(TRUE);
848 $query->select($id . ', ' . $mail . ', ' . $name);
849 $query->from($JUserTable->getTableName());
850 $query->where($mail != '');
852 $db->setQuery($query);
853 $users = $db->loadObjectList();
855 $user = new StdClass();
856 $uf = $config->userFramework
;
859 $contactMatching = 0;
860 for ($i = 0; $i < count($users); $i++
) {
861 $user->$id = $users[$i]->$id;
862 $user->$mail = $users[$i]->$mail;
863 $user->$name = $users[$i]->$name;
865 if ($match = CRM_Core_BAO_UFMatch
::synchronizeUFMatch($user,
882 'contactCount' => $contactCount,
883 'contactMatching' => $contactMatching,
884 'contactCreated' => $contactCreated,
889 * Determine the location of the CiviCRM source tree.
892 * 1. This was pulled out from a bigger function. It should be split
893 * into even smaller pieces and marked abstract.
894 * 2. This would be easier to compute by a calling a CMS API, but
895 * for whatever reason we take the hard way.
898 * - url: string. ex: "http://example.com/sites/all/modules/civicrm"
899 * - path: string. ex: "/var/www/sites/all/modules/civicrm"
901 public function getCiviSourceStorage() {
902 global $civicrm_root;
903 if (!defined('CIVICRM_UF_BASEURL')) {
904 throw new RuntimeException('Undefined constant: CIVICRM_UF_BASEURL');
906 $baseURL = CRM_Utils_File
::addTrailingSlash(CIVICRM_UF_BASEURL
, '/');
907 if (CRM_Utils_System
::isSSL()) {
908 $baseURL = str_replace('http://', 'https://', $baseURL);
911 // For Joomla CiviCRM Core files always live within the admistrator folder and $base_url is different on the frontend compared to the backend.
912 if (strpos($baseURL, 'administrator') === FALSE) {
913 $userFrameworkResourceURL = $baseURL . "administrator/components/com_civicrm/civicrm/";
916 $userFrameworkResourceURL = $baseURL . "components/com_civicrm/civicrm/";
920 'url' => CRM_Utils_File
::addTrailingSlash($userFrameworkResourceURL, '/'),
921 'path' => CRM_Utils_File
::addTrailingSlash($civicrm_root),
926 * Return the CMS-specific url for its permissions page
929 public function getCMSPermissionsUrlParams() {
932 $config = CRM_Core_Config
::singleton();
933 //condition based on Joomla version; <= 2.5 uses modal window; >= 3.0 uses full page with return value
934 if (version_compare(JVERSION
, '3.0', 'lt')) {
935 JHTML
::_('behavior.modal');
936 $ufAccessURL = $config->userFrameworkBaseURL
. 'index.php?option=com_config&view=component&component=com_civicrm&tmpl=component';
937 $jAccessParams = 'rel="{handler: \'iframe\', size: {x: 875, y: 550}, onClose: function() {}}" class="modal"';
940 $uri = (string) JUri
::getInstance();
941 $return = urlencode(base64_encode($uri));
942 $ufAccessURL = $config->userFrameworkBaseURL
. 'index.php?option=com_config&view=component&component=com_civicrm&return=' . $return;
945 'ufAccessURL' => $ufAccessURL,
946 'jAccessParams' => $jAccessParams,