3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
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) {
268 * Add an inline script
270 * @param $code: string, javascript code
271 * @param $region string, location within the document: 'html-header', 'page-header', 'page-footer'
273 * Note: This function is not to be called directly
274 * @see CRM_Core_Region::render()
276 * @return bool TRUE if we support this operation in this CMS, FALSE otherwise
279 public function addScript($code, $region) {
286 * @param $url: string, absolute path to file
287 * @param $region string, location within the document: 'html-header', 'page-header', 'page-footer'
289 * Note: This function is not to be called directly
290 * @see CRM_Core_Region::render()
292 * @return bool TRUE if we support this operation in this CMS, FALSE otherwise
295 public function addStyleUrl($url, $region) {
296 if ($region == 'html-header') {
297 $document = JFactory
::getDocument();
298 $document->addStyleSheet($url);
305 * Add an inline style
307 * @param $code: string, css code
308 * @param $region string, location within the document: 'html-header', 'page-header', 'page-footer'
310 * Note: This function is not to be called directly
311 * @see CRM_Core_Region::render()
313 * @return bool TRUE if we support this operation in this CMS, FALSE otherwise
316 public function addStyle($code, $region) {
317 if ($region == 'html-header') {
318 $document = JFactory
::getDocument();
319 $document->addStyleDeclaration($code);
326 * Generate an internal CiviCRM URL
328 * @param $path string The path being linked to, such as "civicrm/add"
329 * @param $query string A query string to append to the link.
330 * @param $absolute boolean Whether to force the output to be an absolute link (beginning with http:).
331 * Useful for links that will be displayed outside the site, such as in an
333 * @param $fragment string A fragment identifier (named anchor) to append to the link.
334 * @param $htmlize boolean whether to convert to html eqivalant
335 * @param $frontend boolean a gross joomla hack
337 * @return string an HTML string containing a link to the given path.
341 function url($path = NULL, $query = NULL, $absolute = TRUE,
342 $fragment = NULL, $htmlize = TRUE,
343 $frontend = FALSE, $forceBackend = FALSE
345 $config = CRM_Core_Config
::singleton();
346 $separator = $htmlize ?
'&' : '&';
349 $path = CRM_Utils_String
::stripPathChars($path);
351 if ($config->userFrameworkFrontend
) {
352 $script = 'index.php';
353 if (JRequest
::getVar("Itemid")) {
354 $Itemid = "{$separator}Itemid=" . JRequest
::getVar("Itemid");
358 if (isset($fragment)) {
359 $fragment = '#' . $fragment;
362 if (!isset($config->useFrameworkRelativeBase
)) {
363 $base = parse_url($config->userFrameworkBaseURL
);
364 $config->useFrameworkRelativeBase
= $base['path'];
366 $base = $absolute ?
$config->userFrameworkBaseURL
: $config->useFrameworkRelativeBase
;
368 if (!empty($query)) {
369 $url = "{$base}{$script}?option=com_civicrm{$separator}task={$path}{$Itemid}{$separator}{$query}{$fragment}";
372 $url = "{$base}{$script}?option=com_civicrm{$separator}task={$path}{$Itemid}{$fragment}";
375 // gross hack for joomla, we are in the backend and want to send a frontend url
376 if ($frontend && $config->userFramework
== 'Joomla') {
377 // handle both joomla v1.5 and v1.6, CRM-7939
378 $url = str_replace('/administrator/index2.php', '/index.php', $url);
379 $url = str_replace('/administrator/index.php', '/index.php', $url);
382 $url = str_replace('/administrator/', '/index.php', $url);
384 elseif ($forceBackend) {
385 if (defined('JVERSION')) {
386 $joomlaVersion = JVERSION
;
388 $jversion = new JVersion
;
389 $joomlaVersion = $jversion->getShortVersion();
392 if (version_compare($joomlaVersion, '1.6') >= 0) {
393 $url = str_replace('/index.php', '/administrator/index.php', $url);
400 * rewrite various system urls to https
405 function mapConfigToSSL() {
406 // dont need to do anything, let CMS handle their own switch to SSL
411 * figure out the post url for the form
413 * @param $action the default action if one is pre-specified
415 * @return string the url to post the form
418 function postURL($action) {
419 if (!empty($action)) {
423 return $this->url(CRM_Utils_Array
::value('task', $_GET),
424 NULL, TRUE, NULL, FALSE
429 * Function to set the email address of the user
431 * @param object $user handle to the user object
436 function setEmail(&$user) {
438 $query = "SELECT email FROM #__users WHERE id='$user->id'";
439 $database->setQuery($query);
440 $user->email
= $database->loadResult();
444 * Authenticate the user against the joomla db
446 * @param string $name the user name
447 * @param string $password the password for the above user name
448 * @param $loadCMSBootstrap boolean load cms bootstrap?
450 * @return mixed false if no auth
452 contactID, ufID, unique string ) if success
455 function authenticate($name, $password, $loadCMSBootstrap = FALSE) {
456 require_once 'DB.php';
458 $config = CRM_Core_Config
::singleton();
460 if ($loadCMSBootstrap) {
461 $bootStrapParams = array();
462 if ($name && $password) {
463 $bootStrapParams = array(
468 CRM_Utils_System
::loadBootStrap($bootStrapParams, TRUE, TRUE, FALSE);
471 jimport('joomla.application.component.helper');
472 jimport('joomla.database.table');
474 $JUserTable = JTable
::getInstance('User', 'JTable');
476 $db = $JUserTable->getDbo();
477 $query = $db->getQuery(TRUE);
478 $query->select('id, username, email, password');
479 $query->from($JUserTable->getTableName());
480 $query->where('(LOWER(username) = LOWER(\'' . $name . '\')) AND (block = 0)');
481 $db->setQuery($query, 0, 0);
482 $users = $db->loadAssocList();
491 $dbPassword = CRM_Utils_Array
::value('password', $row);
492 $dbId = CRM_Utils_Array
::value('id', $row);
493 $dbEmail = CRM_Utils_Array
::value('email', $row);
495 // now check password
496 if (strpos($dbPassword, ':') === FALSE) {
497 if ($dbPassword != md5($password)) {
502 list($hash, $salt) = explode(':', $dbPassword);
503 $cryptpass = md5($password . $salt);
504 if ($hash != $cryptpass) {
509 CRM_Core_BAO_UFMatch
::synchronizeUFMatch($user, $dbId, $dbEmail, 'Joomla');
510 $contactID = CRM_Core_BAO_UFMatch
::getContactId($dbId);
514 return array($contactID, $dbId, mt_rand());
520 * Set a init session with user object
522 * @param array $data array with user specific data
526 function setUserSession($data) {
527 list($userID, $ufID) = $data;
528 $user = new JUser( $ufID );
529 $session = JFactory
::getSession();
530 $session->set('user', $user);
532 parent
::setUserSession($data);
536 * Set a message in the UF to display to a user
538 * @param string $message the message to set
542 function setMessage($message) {
546 function loadUser($user) {
550 function permissionDenied() {
551 CRM_Core_Error
::fatal(ts('You do not have permission to access this page'));
556 header("Location:index.php");
560 * Get the locale set in the hosting CMS
562 * @return string the used locale or null for none
564 function getUFLocale() {
565 if (defined('_JEXEC')) {
566 $conf = JFactory
::getConfig();
567 $locale = $conf->get('language');
568 return str_replace('-', '_', $locale);
573 function getVersion() {
574 if (class_exists('JVersion')) {
575 $version = new JVersion
;
576 return $version->getShortVersion();
584 * load joomla bootstrap
586 * @param $params array with uid or name and password
587 * @param $loadUser boolean load cms user?
588 * @param $throwError throw error on failure?
590 function loadBootStrap($params = array(), $loadUser = TRUE, $throwError = TRUE, $realPath = NULL, $loadDefines = TRUE) {
591 // Setup the base path related constant.
592 $joomlaBase = dirname(dirname(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__
))))))));
594 // load BootStrap here if needed
595 // We are a valid Joomla entry point.
596 if ( ! defined( '_JEXEC' ) && $loadDefines ) {
598 define('DS', DIRECTORY_SEPARATOR
);
599 define('JPATH_BASE', $joomlaBase . '/administrator');
600 require $joomlaBase . '/administrator/includes/defines.php';
603 // Get the framework.
604 if (file_exists($joomlaBase . '/libraries/import.legacy.php')) {
605 require $joomlaBase . '/libraries/import.legacy.php';
607 require $joomlaBase . '/libraries/import.php';
608 require $joomlaBase . '/libraries/joomla/event/dispatcher.php';
609 require $joomlaBase . '/configuration.php';
611 // Files may be in different places depending on Joomla version
612 if ( !defined('JVERSION') ) {
613 require $joomlaBase . '/libraries/cms/version/version.php';
614 $jversion = new JVersion
;
615 define('JVERSION', $jversion->getShortVersion());
618 if( version_compare(JVERSION
, '3.0', 'lt') ) {
619 require $joomlaBase . '/libraries/joomla/environment/uri.php';
620 require $joomlaBase . '/libraries/joomla/application/component/helper.php';
623 require $joomlaBase . '/libraries/joomla/uri/uri.php';
626 jimport('joomla.application.cli');
632 * check is user logged in.
634 * @return boolean true/false.
636 public function isUserLoggedIn() {
637 $user = JFactory
::getUser();
638 return ($user->guest
) ?
FALSE : TRUE;
642 * Get currently logged in user uf id.
644 * @return int logged in user uf id.
646 public function getLoggedInUfID() {
647 $user = JFactory
::getUser();
648 return ($user->guest
) ?
NULL : $user->id
;
652 * Get a list of all installed modules, including enabled and disabled ones
654 * @return array CRM_Core_Module
656 function getModules() {
659 $db = JFactory
::getDbo();
660 $query = $db->getQuery(true);
661 $query->select('type, folder, element, enabled')
662 ->from('#__extensions')
663 ->where('type =' . $db->Quote('plugin'));
664 $plugins = $db->setQuery($query)->loadAssocList();
665 foreach ($plugins as $plugin) {
666 // question: is the folder really a critical part of the plugin's name?
667 $name = implode('.', array('joomla', $plugin['type'], $plugin['folder'], $plugin['element']));
668 $result[] = new CRM_Core_Module($name, $plugin['enabled'] ?
TRUE : FALSE);
675 * Get user login URL for hosting CMS (method declared in each CMS system class)
677 * @param string $destination - if present, add destination to querystring (works for Drupal only)
679 * @return string - loginURL for the current CMS
682 public function getLoginURL($destination = '') {
683 $config = CRM_Core_Config
::singleton();
684 $loginURL = $config->userFrameworkBaseURL
;
685 $loginURL = str_replace('administrator/', '', $loginURL);
686 $loginURL .= 'index.php?option=com_users&view=login';
690 public function getLoginDestination(&$form) {
695 * Return default Site Settings
696 * @return array array
697 * - $url, (Joomla - non admin url)
701 function getDefaultSiteSettings($dir){
702 $config = CRM_Core_Config
::singleton();
706 $config->userFrameworkBaseURL
708 $siteRoot = preg_replace(
709 '|/media/civicrm/.*$|',
711 $config->imageUploadDir
713 return array($url, NULL, $siteRoot);