diff --git a/CRM/Utils/System/Drupal.php b/CRM/Utils/System/Drupal.php
index 0f7587c8d428a726c315d1b16d81ac9762a16332..be31ffaebf869f423b1eeac01c1b1993db687d4f 100644
--- a/CRM/Utils/System/Drupal.php
+++ b/CRM/Utils/System/Drupal.php
@@ -63,7 +63,7 @@ class CRM_Utils_System_Drupal extends CRM_Utils_System_Base {
 
     $admin = user_access('administer users');
     if (!variable_get('user_email_verification', TRUE) || $admin) {
-            $form_state['input']['pass'] = array('pass1'=>$params['cms_pass'],'pass2'=>$params['cms_pass']);
+      $form_state['input']['pass'] = array('pass1'=>$params['cms_pass'],'pass2'=>$params['cms_pass']);
     }
 
     if(!empty($params['notify'])){
@@ -98,9 +98,7 @@ class CRM_Utils_System_Drupal extends CRM_Utils_System_Base {
     if (form_get_errors()) {
       return FALSE;
     }
-    else {
-      return $form_state['user']->uid;
-  }
+    return $form_state['user']->uid;
   }
 
   /*
@@ -550,7 +548,8 @@ class CRM_Utils_System_Drupal extends CRM_Utils_System_Base {
    *
    * @param string $name     the user name
    * @param string $password the password for the above user name
-   * @param $loadCMSBootstrap boolean load cms bootstrap?
+   * @param boolean $loadCMSBootstrap load cms bootstrap?
+   * @param NULL|string $realPath filename of script
    *
    * @return mixed false if no auth
    *               array(
@@ -567,8 +566,6 @@ class CRM_Utils_System_Drupal extends CRM_Utils_System_Base {
       CRM_Core_Error::fatal("Cannot connect to drupal db via $config->userFrameworkDSN, " . $dbDrupal->getMessage());
     }
 
-
-
     $account = $userUid = $userMail = NULL;
     if ($loadCMSBootstrap) {
       $bootStrapParams = array();
@@ -738,11 +735,11 @@ AND    u.status = 1
   /**
    * load drupal bootstrap
    *
-   * @param $params array with uid or name and password
-   * @param $loadUser boolean load cms user?
-   * @param $throwError throw error on failure?
+   * @param array $params Either uid, or name & pass.
+   * @param boolean $loadUser boolean Require CMS user load.
+   * @param boolean $throwError If true, print error on failure and exit.
+   * @param boolean|string $realPath path to script
    */
-
   function loadBootStrap($params = array(), $loadUser = TRUE, $throwError = TRUE, $realPath = NULL) {
     //take the cms root path.
     $cmsPath = $this->cmsRootPath($realPath);
@@ -766,7 +763,8 @@ AND    u.status = 1
       }
     }
     require_once 'includes/bootstrap.inc';
-    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
+    // @ to suppress notices eg 'DRUPALFOO already defined'.
+    @drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
 
     // explicitly setting error reporting, since we cannot handle drupal related notices
     error_reporting(1);
@@ -839,8 +837,10 @@ AND    u.status = 1
     return FALSE;
   }
 
+  /**
+   *
+   */
   function cmsRootPath($scriptFilename = NULL) {
-
     $cmsRoot = $valid = NULL;
 
     if (!is_null($scriptFilename)) {
@@ -926,11 +926,7 @@ AND    u.status = 1
    * @return string $url, formatted url.
    * @static
    */
-  function languageNegotiationURL(
-    $url,
-    $addLanguagePart = TRUE,
-    $removeLanguagePart = FALSE
-  ) {
+  function languageNegotiationURL($url, $addLanguagePart = TRUE, $removeLanguagePart = FALSE) {
     if (empty($url)) {
       return $url;
     }
@@ -1022,12 +1018,17 @@ AND    u.status = 1
 
   /**
    * Wrapper for og_membership creation
+   *
+   * @param integer $ogID Organic Group ID
+   * @param integer $drupalID drupal User ID
    */
   function og_membership_create($ogID, $drupalID){
     if (function_exists('og_entity_query_alter')) {
-      // sort-of-randomly chose a function that only exists in the 7.x-2.x branch
-      // TODO: Find a more solid way to make this test
-      // Also, since we don't know how to get the entity type of the group, we'll assume it's 'node'
+      // sort-of-randomly chose a function that only exists in the // 7.x-2.x branch
+      //
+      // @TODO Find more solid way to check - try system_get_info('module', 'og').
+      //
+      // Also, since we don't know how to get the entity type of the // group, we'll assume it's 'node'
       og_group('node', $ogID, array('entity' => user_load($drupalID)));
     }
     else {
@@ -1038,6 +1039,9 @@ AND    u.status = 1
 
   /**
    * Wrapper for og_membership deletion
+   *
+   * @param integer $ogID Organic Group ID
+   * @param integer $drupalID drupal User ID
    */
   function og_membership_delete($ogID, $drupalID) {
     if (function_exists('og_entity_query_alter')) {
diff --git a/CRM/Utils/System/Drupal6.php b/CRM/Utils/System/Drupal6.php
index cc840c67a50c7bbe39d1836efa1b2cf3ec98bca4..e77a06710825fffd408158c3a8da0def0abdd49a 100644
--- a/CRM/Utils/System/Drupal6.php
+++ b/CRM/Utils/System/Drupal6.php
@@ -119,9 +119,7 @@ class CRM_Utils_System_Drupal6 extends CRM_Utils_System_Base {
     if (form_get_errors() || !isset($form_state['user'])) {
       return FALSE;
     }
-
     return $form_state['user']->uid;
-
   }
 
   /*
@@ -158,7 +156,6 @@ class CRM_Utils_System_Drupal6 extends CRM_Utils_System_Base {
     $email = $dao->escape(CRM_Utils_Array::value('mail', $params));
     _user_edit_validate(NULL, $params);
     $errors = form_get_errors();
-
     if ($errors) {
       if (CRM_Utils_Array::value('name', $errors)) {
         $errors['cms_name'] = $errors['name'];
@@ -170,7 +167,7 @@ class CRM_Utils_System_Drupal6 extends CRM_Utils_System_Base {
       unset($_SESSION['messages']);
     }
 
-    // drupal api sucks do the name check manually
+    // Do the name check manually.
     $nameError = user_validate_name($params['name']);
     if ($nameError) {
       $errors['cms_name'] = $nameError;
@@ -181,7 +178,6 @@ SELECT name, mail
   FROM {$config->userFrameworkUsersTableName}
  WHERE (LOWER(name) = LOWER('$name')) OR (LOWER(mail) = LOWER('$email'))";
 
-
     $db_cms = DB::connect($config->userFrameworkDSN);
     if (DB::isError($db_cms)) {
       die("Cannot connect to UF db via $dsn, " . $db_cms->getMessage());
@@ -426,7 +422,6 @@ SELECT name, mail
    *
    * @return string the url to post the form
    * @access public
-
    */
   function postURL($action) {
     if (!empty($action)) {
@@ -516,6 +511,8 @@ SELECT name, mail
    *
    * @param string $name     the user name
    * @param string $password the password for the above user name
+   * @param boolean $loadCMSBootstrap load cms bootstrap?
+   * @param NULL|string $realPath filename of script
    *
    * @return mixed false if no auth
    *               array(
@@ -557,8 +554,8 @@ SELECT name, mail
           }
           CRM_Utils_System::loadBootStrap($bootStrapParams, TRUE, TRUE, $realPath);
         }
-      return array($contactID, $row['uid'], mt_rand());
-    }
+        return array($contactID, $row['uid'], mt_rand());
+      }
     }
     return FALSE;
   }
@@ -631,20 +628,26 @@ SELECT name, mail
   function getUFLocale() {
     // return CiviCRM’s xx_YY locale that either matches Drupal’s Chinese locale
     // (for CRM-6281), Drupal’s xx_YY or is retrieved based on Drupal’s xx
+    // sometimes for CLI based on order called, this might not be set and/or empty
     global $language;
-    switch (TRUE) {
-      case $language->language == 'zh-hans':
-        return 'zh_CN';
 
-      case $language->language == 'zh-hant':
-        return 'zh_TW';
+    if (empty($language)) {
+      return NULL;
+    }
 
-      case preg_match('/^.._..$/', $language->language):
-        return $language->language;
+    if ($language->language == 'zh-hans') {
+      return 'zh_CN';
+    }
 
-      default:
-        return CRM_Core_I18n_PseudoConstant::longForShort(substr($language->language, 0, 2));
+    if ($language->language == 'zh-hant') {
+      return 'zh_TW';
     }
+
+    if (preg_match('/^.._..$/', $language->language)) {
+      return $language->language;
+    }
+
+    return CRM_Core_I18n_PseudoConstant::longForShort(substr($language->language, 0, 2));
   }
 
   function getVersion() {
@@ -654,32 +657,50 @@ SELECT name, mail
   /**
    * load drupal bootstrap
    *
-   * @param $name string  optional username for login
-   * @param $pass string  optional password for login
+   * @param array $params Either uid, or name & pass.
+   * @param boolean $loadUser boolean Require CMS user load.
+   * @param boolean $throwError If true, print error on failure and exit.
+   * @param boolean|string $realPath path to script
    */
-  function loadBootStrap($params = array(
-    ), $loadUser = TRUE, $throwError = TRUE, $realPath = NULL) {
-    $uid  = CRM_Utils_Array::value('uid', $params);
-    $name = CRM_Utils_Array::value('name', $params, FALSE) ? $params['name'] : trim(CRM_Utils_Array::value('name', $_REQUEST));
-    $pass = CRM_Utils_Array::value('pass', $params, FALSE) ? $params['pass'] : trim(CRM_Utils_Array::value('pass', $_REQUEST));
-
+  function loadBootStrap($params = array(), $loadUser = TRUE, $throwError = TRUE, $realPath = NULL) {
     //take the cms root path.
     $cmsPath = $this->cmsRootPath($realPath);
+
     if (!file_exists("$cmsPath/includes/bootstrap.inc")) {
-      echo '<br />Sorry, unable to locate bootstrap.inc.';
-      exit();
+      if ($throwError) {
+        echo '<br />Sorry, could not locate bootstrap.inc\n';
+        exit();
+      }
+      return FALSE;
     }
-
+    // load drupal bootstrap
     chdir($cmsPath);
+    define('DRUPAL_ROOT', $cmsPath);
+
+    // For drupal multi-site CRM-11313
+    if ($realPath && strpos($realPath, 'sites/all/modules/') === FALSE) {
+      preg_match('@sites/([^/]*)/modules@s', $realPath, $matches);
+      if (!empty($matches[1])) {
+        $_SERVER['HTTP_HOST'] = $matches[1];
+      }
+    }
     require_once 'includes/bootstrap.inc';
+    // @ to suppress notices eg 'DRUPALFOO already defined'.
     @drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
 
-    if (!function_exists('module_exists') ||
-      !module_exists('civicrm')
-    ) {
-      echo '<br />Sorry, could not able to load drupal bootstrap.';
-      exit();
+    // explicitly setting error reporting, since we cannot handle drupal related notices
+    error_reporting(1);
+    if (!function_exists('module_exists') || !module_exists('civicrm')) {
+      if ($throwError) {
+        echo '<br />Sorry, could not load drupal bootstrap.';
+        exit();
+      }
+      return FALSE;
     }
+
+    // seems like we've bootstrapped drupal
+    $config = CRM_Core_Config::singleton();
+
     // lets also fix the clean url setting
     // CRM-6948
     $config->cleanURL = (int) variable_get('clean_url', '0');
@@ -693,27 +714,55 @@ SELECT name, mail
     if (!$loadUser) {
       return TRUE;
     }
-    //load user, we need to check drupal permissions.
-    if ($name) {
-      $user = user_authenticate(array('name' => $name, 'pass' => $pass));
-      if (empty($user->uid)) {
-        echo '<br />Sorry, unrecognized username or password.';
-        exit();
+
+    // If $uid is passed in, authentication has been done already.
+    $uid = CRM_Utils_Array::value('uid', $params);
+    if (!$uid) {
+      //load user, we need to check drupal permissions.
+      $name = CRM_Utils_Array::value('name', $params, FALSE) ? $params['name'] : trim(CRM_Utils_Array::value('name', $_REQUEST));
+      $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) {
+          if ($throwError) {
+            echo '<br />Sorry, unrecognized username or password.';
+            exit();
+          }
+          return FALSE;
+        }
       }
     }
-    elseif ($uid) {
-      $account = user_load(array('uid' => $uid));
-      if (empty($account->uid)) {
-        echo '<br />Sorry, unrecognized user id.';
-        exit();
-      }
-      else {
+
+    if ($uid) {
+      $account = user_load($uid);
+      if ($account && $account->uid) {
         global $user;
         $user = $account;
+        return TRUE;
       }
     }
+
+    if ($throwError) {
+      echo '<br />Sorry, can not load CMS user account.';
+      exit();
+    }
+
+    // CRM-6948: When using loadBootStrap, it's implicit that CiviCRM has already loaded its settings
+    // which means that define(CIVICRM_CLEANURL) was correctly set.
+    // So we correct it
+    $config = CRM_Core_Config::singleton();
+    $config->cleanURL = (int)variable_get('clean_url', '0');
+
+    // CRM-8655: Drupal wasn't available during bootstrap, so hook_civicrm_config never executes
+    CRM_Utils_Hook::config($config);
+
+    return FALSE;
   }
 
+  /**
+   *
+   */
   function cmsRootPath($scriptFilename = NULL) {
     $cmsRoot = $valid = NULL;
 
@@ -723,6 +772,7 @@ SELECT name, mail
     else {
       $path = $_SERVER['SCRIPT_FILENAME'];
     }
+
     if (function_exists('drush_get_context')) {
       // drush anyway takes care of multisite install etc
       return drush_get_context('DRUSH_DRUPAL_ROOT');
@@ -799,10 +849,7 @@ SELECT name, mail
    * @return string $url, formatted url.
    * @static
    */
-  function languageNegotiationURL($url,
-    $addLanguagePart = TRUE,
-    $removeLanguagePart = FALSE
-  ) {
+  function languageNegotiationURL($url, $addLanguagePart = TRUE, $removeLanguagePart = FALSE) {
     if (empty($url)) {
       return $url;
     }
@@ -949,4 +996,3 @@ SELECT name, mail
     drupal_flush_all_caches();
   }
 }
-