Merge pull request #2638 from colemanw/CRM-14196
[civicrm-core.git] / CRM / Utils / System / Joomla.php
index fc7eb8f71b557864a2cbfa38db2dffefe77d558d..4db4125cf13618f277b3b657b9fdf7f38a01a50f 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
@@ -456,6 +456,7 @@ class CRM_Utils_System_Joomla extends CRM_Utils_System_Base {
     require_once 'DB.php';
 
     $config = CRM_Core_Config::singleton();
+    $user = NULL;
 
     if ($loadCMSBootstrap) {
       $bootStrapParams = array();
@@ -470,49 +471,64 @@ class CRM_Utils_System_Joomla extends CRM_Utils_System_Base {
 
     jimport('joomla.application.component.helper');
     jimport('joomla.database.table');
+    jimport('joomla.user.helper');
 
-    $JUserTable = &JTable::getInstance('User', 'JTable');
+    $JUserTable = JTable::getInstance('User', 'JTable');
 
     $db = $JUserTable->getDbo();
     $query = $db->getQuery(TRUE);
-    $query->select('id, username, email, password');
+    $query->select('id, name, username, email, password');
     $query->from($JUserTable->getTableName());
     $query->where('(LOWER(username) = LOWER(\'' . $name . '\')) AND (block = 0)');
     $db->setQuery($query, 0, 0);
-    $users = $db->loadAssocList();
+    $users = $db->loadObjectList();
 
-    $row = array();;
+    $row = array();
     if (count($users)) {
       $row = $users[0];
     }
 
-    $user = NULL;
+    if ( !defined('JVERSION') ) {
+      $joomlaBase = dirname(dirname(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__))))))));
+      require $joomlaBase . '/libraries/cms/version/version.php';
+      $jversion = new JVersion;
+      define('JVERSION', $jversion->getShortVersion());
+    }
+
     if (!empty($row)) {
-      $dbPassword = CRM_Utils_Array::value('password', $row);
-      $dbId       = CRM_Utils_Array::value('id', $row);
-      $dbEmail    = CRM_Utils_Array::value('email', $row);
-
-      // now check password
-      if (strpos($dbPassword, ':') === FALSE) {
-        if ($dbPassword != md5($password)) {
-          return FALSE;
+      $dbPassword = $row->password;
+      $dbId = $row->id;
+      $dbEmail = $row->email;
+
+      if ( version_compare(JVERSION, '2.5.18', 'lt') ||
+        ( version_compare(JVERSION, '3.0', 'ge') && version_compare(JVERSION, '3.2.1', 'lt') )
+      ) {
+        // now check password
+        if (strpos($dbPassword, ':') === FALSE) {
+          if ($dbPassword != md5($password)) {
+            return FALSE;
+          }
+        }
+        else {
+          list($hash, $salt) = explode(':', $dbPassword);
+          $cryptpass = md5($password . $salt);
+          if ($hash != $cryptpass) {
+            return FALSE;
+          }
         }
       }
       else {
-        list($hash, $salt) = explode(':', $dbPassword);
-        $cryptpass = md5($password . $salt);
-        if ($hash != $cryptpass) {
-          return FALSE;
-        }
+        if (!JUserHelper::verifyPassword($password, $dbPassword, $dbId)) return FALSE;
       }
 
-      CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, $dbId, $dbEmail, 'Joomla');
+      CRM_Core_BAO_UFMatch::synchronizeUFMatch($row, $dbId, $dbEmail, 'Joomla');
       $contactID = CRM_Core_BAO_UFMatch::getContactId($dbId);
       if (!$contactID) {
         return FALSE;
       }
       return array($contactID, $dbId, mt_rand());
     }
+
     return FALSE;
   }
 
@@ -526,7 +542,7 @@ class CRM_Utils_System_Joomla extends CRM_Utils_System_Base {
   function setUserSession($data) {
     list($userID, $ufID) = $data;
     $user = new JUser( $ufID );
-    $session = &JFactory::getSession();
+    $session = JFactory::getSession();
     $session->set('user', $user);
 
     parent::setUserSession($data);
@@ -564,7 +580,7 @@ class CRM_Utils_System_Joomla extends CRM_Utils_System_Base {
   function getUFLocale() {
     if (defined('_JEXEC')) {
       $conf = JFactory::getConfig();
-      $locale = $conf->getValue('config.language');
+      $locale = $conf->get('language');
       return str_replace('-', '_', $locale);
     }
     return NULL;
@@ -587,7 +603,7 @@ class CRM_Utils_System_Joomla extends CRM_Utils_System_Base {
    * @param $loadUser boolean load cms user?
    * @param $throwError throw error on failure?
    */
-  function loadBootStrap($params = array(), $loadUser = TRUE, $throwError = TRUE, $loadDefines = TRUE) {
+  function loadBootStrap($params = array(), $loadUser = TRUE, $throwError = TRUE, $realPath = NULL, $loadDefines = TRUE) {
     // Setup the base path related constant.
     $joomlaBase = dirname(dirname(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__))))))));
 
@@ -601,6 +617,9 @@ class CRM_Utils_System_Joomla extends CRM_Utils_System_Base {
     }
 
     // Get the framework.
+    if (file_exists($joomlaBase . '/libraries/import.legacy.php')) {
+      require $joomlaBase . '/libraries/import.legacy.php';
+    }
     require $joomlaBase . '/libraries/import.php';
     require $joomlaBase . '/libraries/joomla/event/dispatcher.php';
     require $joomlaBase . '/configuration.php';
@@ -618,7 +637,6 @@ class CRM_Utils_System_Joomla extends CRM_Utils_System_Base {
     }
     else {
       require $joomlaBase . '/libraries/joomla/uri/uri.php';
-      require $joomlaBase . '/libraries/legacy/component/helper.php';
     }
 
     jimport('joomla.application.cli');
@@ -684,9 +702,31 @@ class CRM_Utils_System_Joomla extends CRM_Utils_System_Base {
     $loginURL .= 'index.php?option=com_users&view=login';
     return $loginURL;
   }
-  
+
   public function getLoginDestination(&$form) {
     return;
   }
+
+  /**
+   * Return default Site Settings
+   * @return array array
+   * - $url, (Joomla - non admin url)
+   * - $siteName,
+   * - $siteRoot
+   */
+  function getDefaultSiteSettings($dir){
+    $config = CRM_Core_Config::singleton();
+    $url = preg_replace(
+      '|/administrator|',
+      '',
+      $config->userFrameworkBaseURL
+    );
+    $siteRoot = preg_replace(
+      '|/media/civicrm/.*$|',
+      '',
+      $config->imageUploadDir
+    );
+    return array($url, NULL, $siteRoot);
+  }
 }