Merge pull request #2843 from kurund/CRM-14427
[civicrm-core.git] / CRM / Utils / System / Drupal6.php
index b544245b1d2df98ce22b366c9048d11eb984f238..e16e61bbdee3bfc81bf99612063928036f754f9a 100644 (file)
@@ -174,16 +174,13 @@ class CRM_Utils_System_Drupal6 extends CRM_Utils_System_DrupalBase {
       FROM {users}
       WHERE (LOWER(name) = LOWER('$name')) OR (LOWER(mail) = LOWER('$email'))
     ";
-    $result = db_query($sql);
-    $rows = array();
 
-    if (count($result) > 0) {
-     $rows[] = db_fetch_array($result);
-    }
-    if(empty($rows)) {
+    $result = db_query($sql);
+    $row = db_fetch_array($result);
+    if (!$row) {
       return;
     }
-    $row = $rows[0];
+
     $user = NULL;
 
     if (!empty($row)) {
@@ -342,7 +339,7 @@ class CRM_Utils_System_Drupal6 extends CRM_Utils_System_DrupalBase {
         return FALSE;
     }
     // If the path is within the drupal directory we can add in the normal way
-    if (CRM_Utils_System_Drupal::formatResourceUrl($url)) {
+    if ($this->formatResourceUrl($url)) {
       drupal_add_js($url, 'module', $scope);
       return TRUE;
     }
@@ -387,7 +384,7 @@ class CRM_Utils_System_Drupal6 extends CRM_Utils_System_DrupalBase {
    * @access public
    */
   public function addStyleUrl($url, $region) {
-    if ($region != 'html-header' || !CRM_Utils_System_Drupal::formatResourceUrl($url)) {
+    if ($region != 'html-header' || !$this->formatResourceUrl($url)) {
       return FALSE;
     }
     drupal_add_css($url);
@@ -439,81 +436,6 @@ class CRM_Utils_System_Drupal6 extends CRM_Utils_System_DrupalBase {
     return $this->url($_GET['q']);
   }
 
-  /**
-   * Generate an internal CiviCRM URL (copied from DRUPAL/includes/common.inc#url)
-   *
-   * @param $path     string   The path being linked to, such as "civicrm/add"
-   * @param $query    string   A query string to append to the link.
-   * @param $absolute boolean  Whether to force the output to be an absolute link (beginning with http:).
-   *                           Useful for links that will be displayed outside the site, such as in an
-   *                           RSS feed.
-   * @param $fragment string   A fragment identifier (named anchor) to append to the link.
-   * @param $htmlize  boolean  whether to convert to html eqivalant
-   * @param $frontend boolean  a gross joomla hack
-   *
-   * @return string            an HTML string containing a link to the given path.
-   * @access public
-   *
-   */
-  function url($path = NULL, $query = NULL, $absolute = FALSE,
-    $fragment = NULL, $htmlize = TRUE,
-    $frontend = FALSE
-  ) {
-    $config = CRM_Core_Config::singleton();
-    $script = 'index.php';
-
-    $path = CRM_Utils_String::stripPathChars($path);
-
-    if (isset($fragment)) {
-      $fragment = '#' . $fragment;
-    }
-
-    if (!isset($config->useFrameworkRelativeBase)) {
-      $base = parse_url($config->userFrameworkBaseURL);
-      $config->useFrameworkRelativeBase = $base['path'];
-    }
-    $base = $absolute ? $config->userFrameworkBaseURL : $config->useFrameworkRelativeBase;
-
-    $separator = $htmlize ? '&' : '&';
-
-    if (!$config->cleanURL) {
-      if (isset($path)) {
-        if (isset($query)) {
-          return $base . $script . '?q=' . $path . $separator . $query . $fragment;
-        }
-        else {
-          return $base . $script . '?q=' . $path . $fragment;
-        }
-      }
-      else {
-        if (isset($query)) {
-          return $base . $script . '?' . $query . $fragment;
-        }
-        else {
-          return $base . $fragment;
-        }
-      }
-    }
-    else {
-      if (isset($path)) {
-        if (isset($query)) {
-          return $base . $path . '?' . $query . $fragment;
-        }
-        else {
-          return $base . $path . $fragment;
-        }
-      }
-      else {
-        if (isset($query)) {
-          return $base . $script . '?' . $query . $fragment;
-        }
-        else {
-          return $base . $fragment;
-        }
-      }
-    }
-  }
-
   /**
    * Authenticate the user against the drupal db
    *
@@ -528,6 +450,13 @@ class CRM_Utils_System_Drupal6 extends CRM_Utils_System_DrupalBase {
    * @access public
    */
   function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) {
+   //@todo this 'PEAR-y' stuff is only required when bookstrap is not being loaded which is rare
+   // if ever now.
+   // probably if bootstrap is loaded this call
+   // CRM_Utils_System::loadBootStrap($bootStrapParams, TRUE, TRUE, $realPath); would be
+   // sufficient to do what this fn does. It does exist as opposed to return which might need some hanky-panky to make
+   // safe in the unknown situation where authenticate might be called & it is important that
+   // false is returned
     require_once 'DB.php';
 
     $config = CRM_Core_Config::singleton();
@@ -736,7 +665,7 @@ class CRM_Utils_System_Drupal6 extends CRM_Utils_System_DrupalBase {
     if (!$loadUser) {
       return TRUE;
     }
-
+    global $user;
     // If $uid is passed in, authentication has been done already.
     $uid = CRM_Utils_Array::value('uid', $params);
     if (!$uid) {
@@ -745,21 +674,23 @@ class CRM_Utils_System_Drupal6 extends CRM_Utils_System_DrupalBase {
       $pass = CRM_Utils_Array::value('pass', $params, FALSE) ? $params['pass'] : trim(CRM_Utils_Array::value('pass', $_REQUEST));
 
       if ($name) {
-        $uid = user_authenticate(array('name' => $name, 'pass' => $pass));
-        if (!$uid) {
+        $user = user_authenticate(array('name' => $name, 'pass' => $pass));
+        if (!$user->uid) {
           if ($throwError) {
             echo '<br />Sorry, unrecognized username or password.';
             exit();
           }
           return FALSE;
         }
+        else {
+          return TRUE;
+        }
       }
     }
 
     if ($uid) {
       $account = user_load($uid);
       if ($account && $account->uid) {
-        global $user;
         $user = $account;
         return TRUE;
       }
@@ -1010,6 +941,29 @@ class CRM_Utils_System_Drupal6 extends CRM_Utils_System_DrupalBase {
       og_delete_subscription( $ogID, $drupalID );
   }
 
+  /**
+   * Get timezone from Drupal
+   * @return boolean|string
+   */
+  function getTimeZoneOffset(){
+    global $user;
+    if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) {
+      $timezone = $user->timezone;
+    } else {
+      $timezone = variable_get('date_default_timezone', null);
+    }
+    if(empty($timezone)){
+      return false;
+    }
+    $hour = $user->timezone / 3600;
+    $timeZoneOffset = sprintf("%02d:%02d", $timezone / 3600, abs(($timeZoneOffset/60)%60));
+    if($timeZoneOffset > 0){
+      $timeZoneOffset = '+' . $timeZoneOffset;
+    }
+    return $timeZoneOffset;
+  }
+
+
   /**
    * Reset any system caches that may be required for proper CiviCRM
    * integration.