3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
37 * Joomla specific stuff goes here
39 class CRM_Utils_System_Joomla
extends CRM_Utils_System_Base
{
40 function __construct() {
41 $this->is_drupal
= FALSE;
45 * Function to create a user of Joomla.
47 * @param array $params associated array
48 * @param string $mail email id for cms user
50 * @return uid if user exists, false otherwise
54 function createUser(&$params, $mail) {
55 $baseDir = JPATH_SITE
;
56 require_once $baseDir . '/components/com_users/models/registration.php';
58 $userParams = JComponentHelper
::getParams('com_users');
59 $model = new UsersModelRegistration();
62 // get the default usertype
63 $userType = $userParams->get('new_usertype');
68 if (isset($params['name'])) {
69 $fullname = trim($params['name']);
71 elseif (isset($params['contactID'])) {
72 $fullname = trim(CRM_Contact_BAO_Contact
::displayName($params['contactID']));
75 $fullname = trim($params['cms_name']);
78 // Prepare the values for a new Joomla user.
80 $values['name'] = $fullname;
81 $values['username'] = trim($params['cms_name']);
82 $values['password1'] = $values['password2'] = $params['cms_pass'];
83 $values['email1'] = $values['email2'] = trim($params[$mail]);
85 $lang = JFactory
::getLanguage();
86 $lang->load('com_users', $baseDir);
88 $register = $model->register($values);
90 $ufID = JUserHelper
::getUserId($values['username']);
95 * Change user name in host CMS
97 * @param integer $ufID User ID in CMS
98 * @param string $ufName User name
100 function updateCMSName($ufID, $ufName) {
101 $ufID = CRM_Utils_Type
::escape($ufID, 'Integer');
102 $ufName = CRM_Utils_Type
::escape($ufName, 'String');
105 $user = &JUser
::getInstance($ufID);
107 $values['email'] = $ufName;
108 $user->bind($values);
114 * Check if username and email exists in the Joomla! db
116 * @params $params array array of name and mail values
117 * @params $errors array array of errors
118 * @params $emailName string field label for the 'email'
122 function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') {
123 $config = CRM_Core_Config
::singleton();
125 $dao = new CRM_Core_DAO();
126 $name = $dao->escape(CRM_Utils_Array
::value('name', $params));
127 $email = $dao->escape(CRM_Utils_Array
::value('mail', $params));
128 //don't allow the special characters and min. username length is two
129 //regex \\ to match a single backslash would become '/\\\\/'
130 $isNotValid = (bool) preg_match('/[\<|\>|\"|\'|\%|\;|\(|\)|\&|\\\\|\/]/im', $name);
131 if ($isNotValid ||
strlen($name) < 2) {
132 $errors['cms_name'] = ts('Your username contains invalid characters or is too short');
136 $JUserTable = &JTable
::getInstance('User', 'JTable');
138 $db = $JUserTable->getDbo();
139 $query = $db->getQuery(TRUE);
140 $query->select('username, email');
141 $query->from($JUserTable->getTableName());
142 $query->where('(LOWER(username) = LOWER(\'' . $name . '\')) OR (LOWER(email) = LOWER(\'' . $email . '\'))');
143 $db->setQuery($query, 0, 10);
144 $users = $db->loadAssocList();
152 $dbName = CRM_Utils_Array
::value('username', $row);
153 $dbEmail = CRM_Utils_Array
::value('email', $row);
154 if (strtolower($dbName) == strtolower($name)) {
155 $errors['cms_name'] = ts('The username %1 is already taken. Please select another username.',
159 if (strtolower($dbEmail) == strtolower($email)) {
160 $resetUrl = str_replace('administrator/', '', $config->userFrameworkBaseURL
) . 'index.php?option=com_users&view=reset';
161 $errors[$emailName] = ts('The email address %1 is already registered. <a href="%2">Have you forgotten your password?</a>',
162 array(1 => $email, 2 => $resetUrl)
169 * sets the title of the page
171 * @param string $title title to set
172 * @param string $pageTitle
177 function setTitle($title, $pageTitle = NULL) {
182 $template = CRM_Core_Smarty
::singleton();
183 $template->assign('pageTitle', $pageTitle);
185 $document = JFactory
::getDocument();
186 $document->setTitle($title);
192 * Append an additional breadcrumb tag to the existing breadcrumb
194 * @param string $title
200 function appendBreadCrumb($breadCrumbs) {
201 $template = CRM_Core_Smarty
::singleton();
202 $bc = $template->get_template_vars('breadcrumb');
204 if (is_array($breadCrumbs)) {
205 foreach ($breadCrumbs as $crumbs) {
206 if (stripos($crumbs['url'], 'id%%')) {
207 $args = array('cid', 'mid');
208 foreach ($args as $a) {
209 $val = CRM_Utils_Request
::retrieve($a, 'Positive', CRM_Core_DAO
::$_nullObject,
213 $crumbs['url'] = str_ireplace("%%{$a}%%", $val, $crumbs['url']);
220 $template->assign_by_ref('breadcrumb', $bc);
225 * Reset an additional breadcrumb tag to the existing breadcrumb
227 * @param string $bc the new breadcrumb to be appended
232 function resetBreadCrumb() {
237 * Append a string to the head of the html file
239 * @param string $head the new string to be appended
244 static function addHTMLHead($string = NULL) {
246 $document = JFactory
::getDocument();
247 $document->addCustomTag($string);
254 * @param $url: string, absolute path to file
255 * @param $region string, location within the document: 'html-header', 'page-header', 'page-footer'
257 * Note: This function is not to be called directly
258 * @see CRM_Core_Region::render()
260 * @return bool TRUE if we support this operation in this CMS, FALSE otherwise
263 public function addScriptUrl($url, $region) {
264 if ($region == 'html-header') {
265 $document = JFactory
::getDocument();
266 $document->addScript($url);
273 * Add an inline script
275 * @param $code: string, javascript code
276 * @param $region string, location within the document: 'html-header', 'page-header', 'page-footer'
278 * Note: This function is not to be called directly
279 * @see CRM_Core_Region::render()
281 * @return bool TRUE if we support this operation in this CMS, FALSE otherwise
284 public function addScript($code, $region) {
285 if ($region == 'html-header') {
286 $document = JFactory
::getDocument();
287 $document->addScriptDeclaration($code);
296 * @param $url: string, absolute path to file
297 * @param $region string, location within the document: 'html-header', 'page-header', 'page-footer'
299 * Note: This function is not to be called directly
300 * @see CRM_Core_Region::render()
302 * @return bool TRUE if we support this operation in this CMS, FALSE otherwise
305 public function addStyleUrl($url, $region) {
306 if ($region == 'html-header') {
307 $document = JFactory
::getDocument();
308 $document->addStyleSheet($url);
315 * Add an inline style
317 * @param $code: string, css code
318 * @param $region string, location within the document: 'html-header', 'page-header', 'page-footer'
320 * Note: This function is not to be called directly
321 * @see CRM_Core_Region::render()
323 * @return bool TRUE if we support this operation in this CMS, FALSE otherwise
326 public function addStyle($code, $region) {
327 if ($region == 'html-header') {
328 $document = JFactory
::getDocument();
329 $document->addStyleDeclaration($code);
336 * Generate an internal CiviCRM URL
338 * @param $path string The path being linked to, such as "civicrm/add"
339 * @param $query string A query string to append to the link.
340 * @param $absolute boolean Whether to force the output to be an absolute link (beginning with http:).
341 * Useful for links that will be displayed outside the site, such as in an
343 * @param $fragment string A fragment identifier (named anchor) to append to the link.
344 * @param $htmlize boolean whether to convert to html eqivalant
345 * @param $frontend boolean a gross joomla hack
347 * @return string an HTML string containing a link to the given path.
351 function url($path = NULL, $query = NULL, $absolute = TRUE,
352 $fragment = NULL, $htmlize = TRUE,
353 $frontend = FALSE, $forceBackend = FALSE
355 $config = CRM_Core_Config
::singleton();
356 $separator = $htmlize ?
'&' : '&';
359 $path = CRM_Utils_String
::stripPathChars($path);
361 if ($config->userFrameworkFrontend
) {
362 $script = 'index.php';
363 if (JRequest
::getVar("Itemid")) {
364 $Itemid = "{$separator}Itemid=" . JRequest
::getVar("Itemid");
368 if (isset($fragment)) {
369 $fragment = '#' . $fragment;
372 if (!isset($config->useFrameworkRelativeBase
)) {
373 $base = parse_url($config->userFrameworkBaseURL
);
374 $config->useFrameworkRelativeBase
= $base['path'];
376 $base = $absolute ?
$config->userFrameworkBaseURL
: $config->useFrameworkRelativeBase
;
378 if (!empty($query)) {
379 $url = "{$base}{$script}?option=com_civicrm{$separator}task={$path}{$Itemid}{$separator}{$query}{$fragment}";
382 $url = "{$base}{$script}?option=com_civicrm{$separator}task={$path}{$Itemid}{$fragment}";
385 // gross hack for joomla, we are in the backend and want to send a frontend url
386 if ($frontend && $config->userFramework
== 'Joomla') {
387 // handle both joomla v1.5 and v1.6, CRM-7939
388 $url = str_replace('/administrator/index2.php', '/index.php', $url);
389 $url = str_replace('/administrator/index.php', '/index.php', $url);
392 $url = str_replace('/administrator/', '/index.php', $url);
394 elseif ($forceBackend) {
395 if (defined('JVERSION')) {
396 $joomlaVersion = JVERSION
;
398 $jversion = new JVersion
;
399 $joomlaVersion = $jversion->getShortVersion();
402 if (version_compare($joomlaVersion, '1.6') >= 0) {
403 $url = str_replace('/index.php', '/administrator/index.php', $url);
410 * rewrite various system urls to https
415 function mapConfigToSSL() {
416 // dont need to do anything, let CMS handle their own switch to SSL
421 * figure out the post url for the form
423 * @param $action the default action if one is pre-specified
425 * @return string the url to post the form
428 function postURL($action) {
429 if (!empty($action)) {
433 return $this->url(CRM_Utils_Array
::value('task', $_GET),
434 NULL, TRUE, NULL, FALSE
439 * Function to set the email address of the user
441 * @param object $user handle to the user object
446 function setEmail(&$user) {
448 $query = "SELECT email FROM #__users WHERE id='$user->id'";
449 $database->setQuery($query);
450 $user->email
= $database->loadResult();
454 * Authenticate the user against the joomla db
456 * @param string $name the user name
457 * @param string $password the password for the above user name
458 * @param $loadCMSBootstrap boolean load cms bootstrap?
460 * @return mixed false if no auth
462 contactID, ufID, unique string ) if success
465 function authenticate($name, $password, $loadCMSBootstrap = FALSE) {
466 require_once 'DB.php';
468 $config = CRM_Core_Config
::singleton();
470 if ($loadCMSBootstrap) {
471 $bootStrapParams = array();
472 if ($name && $password) {
473 $bootStrapParams = array(
478 CRM_Utils_System
::loadBootStrap($bootStrapParams, TRUE, TRUE, FALSE);
481 jimport('joomla.application.component.helper');
482 jimport('joomla.database.table');
484 $JUserTable = &JTable
::getInstance('User', 'JTable');
486 $db = $JUserTable->getDbo();
487 $query = $db->getQuery(TRUE);
488 $query->select('id, username, email, password');
489 $query->from($JUserTable->getTableName());
490 $query->where('(LOWER(username) = LOWER(\'' . $name . '\')) AND (block = 0)');
491 $db->setQuery($query, 0, 0);
492 $users = $db->loadAssocList();
501 $dbPassword = CRM_Utils_Array
::value('password', $row);
502 $dbId = CRM_Utils_Array
::value('id', $row);
503 $dbEmail = CRM_Utils_Array
::value('email', $row);
505 // now check password
506 if (strpos($dbPassword, ':') === FALSE) {
507 if ($dbPassword != md5($password)) {
512 list($hash, $salt) = explode(':', $dbPassword);
513 $cryptpass = md5($password . $salt);
514 if ($hash != $cryptpass) {
519 CRM_Core_BAO_UFMatch
::synchronizeUFMatch($user, $dbId, $dbEmail, 'Joomla');
520 $contactID = CRM_Core_BAO_UFMatch
::getContactId($dbId);
524 return array($contactID, $dbId, mt_rand());
530 * Set a init session with user object
532 * @param array $data array with user specific data
536 function setUserSession($data) {
537 list($userID, $ufID) = $data;
538 $user = new JUser( $ufID );
539 $session = &JFactory
::getSession();
540 $session->set('user', $user);
542 parent
::setUserSession($data);
546 * Set a message in the UF to display to a user
548 * @param string $message the message to set
552 function setMessage($message) {
556 function loadUser($user) {
560 function permissionDenied() {
561 CRM_Core_Error
::fatal(ts('You do not have permission to access this page'));
566 header("Location:index.php");
570 * Get the locale set in the hosting CMS
572 * @return string the used locale or null for none
574 function getUFLocale() {
575 if (defined('_JEXEC')) {
576 $conf = JFactory
::getConfig();
577 $locale = $conf->getValue('config.language');
578 return str_replace('-', '_', $locale);
583 function getVersion() {
584 if (class_exists('JVersion')) {
585 $version = new JVersion
;
586 return $version->getShortVersion();
594 * load joomla bootstrap
596 * @param $params array with uid or name and password
597 * @param $loadUser boolean load cms user?
598 * @param $throwError throw error on failure?
600 function loadBootStrap($params = array(), $loadUser = TRUE, $throwError = TRUE, $loadDefines = TRUE) {
601 // Setup the base path related constant.
602 $joomlaBase = dirname(dirname(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__
))))))));
604 // load BootStrap here if needed
605 // We are a valid Joomla entry point.
606 if ( ! defined( '_JEXEC' ) && $loadDefines ) {
608 define('DS', DIRECTORY_SEPARATOR
);
609 define('JPATH_BASE', $joomlaBase . '/administrator');
610 require $joomlaBase . '/administrator/includes/defines.php';
613 // Get the framework.
614 require $joomlaBase . '/libraries/import.php';
615 require $joomlaBase . '/libraries/joomla/event/dispatcher.php';
616 require $joomlaBase . '/libraries/joomla/environment/uri.php';
617 require $joomlaBase . '/libraries/joomla/application/component/helper.php';
618 require $joomlaBase . '/configuration.php';
620 jimport('joomla.application.cli');
626 * check is user logged in.
628 * @return boolean true/false.
630 public function isUserLoggedIn() {
631 $user = JFactory
::getUser();
632 return ($user->guest
) ?
FALSE : TRUE;
636 * Get currently logged in user uf id.
638 * @return int logged in user uf id.
640 public function getLoggedInUfID() {
641 $user = JFactory
::getUser();
642 return ($user->guest
) ?
NULL : $user->id
;
646 * Get a list of all installed modules, including enabled and disabled ones
648 * @return array CRM_Core_Module
650 function getModules() {
653 $db = JFactory
::getDbo();
654 $query = $db->getQuery(true);
655 $query->select('type, folder, element, enabled')
656 ->from('#__extensions')
657 ->where('type =' . $db->Quote('plugin'));
658 $plugins = $db->setQuery($query)->loadAssocList();
659 foreach ($plugins as $plugin) {
660 // question: is the folder really a critical part of the plugin's name?
661 $name = implode('.', array('joomla', $plugin['type'], $plugin['folder'], $plugin['element']));
662 $result[] = new CRM_Core_Module($name, $plugin['enabled'] ?
TRUE : FALSE);
669 * Get user login URL for hosting CMS (method declared in each CMS system class)
671 * @param string $destination - if present, add destination to querystring (works for Drupal only)
673 * @return string - loginURL for the current CMS
676 public function getLoginURL($destination = '') {
677 $config = CRM_Core_Config
::singleton();
678 $loginURL = $config->userFrameworkBaseURL
;
679 $loginURL = str_replace('administrator/', '', $loginURL);
680 $loginURL .= 'index.php?option=com_users&view=login';
684 public function getLoginDestination(&$form) {