Merge pull request #12137 from eileenmcnaughton/year_fiscal
[civicrm-core.git] / CRM / Utils / System / DrupalBase.php
index c0ce0f49c19672d9aca3f9d0de60f319fecc3eb5..6c1eebcd6b5351bf5eebfebdfbae4ecc4240879b 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.7                                                |
+ | CiviCRM version 5                                                  |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2017                                |
+ | Copyright CiviCRM LLC (c) 2004-2018                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2017
+ * @copyright CiviCRM LLC (c) 2004-2018
  * $Id$
  *
  */
@@ -58,6 +58,27 @@ abstract class CRM_Utils_System_DrupalBase extends CRM_Utils_System_Base {
     $this->supports_form_extensions = TRUE;
   }
 
+  /**
+   * @inheritdoc
+   */
+  public function getDefaultFileStorage() {
+    $config = CRM_Core_Config::singleton();
+    $baseURL = CRM_Utils_System::languageNegotiationURL($config->userFrameworkBaseURL, FALSE, TRUE);
+
+    $siteName = $this->parseDrupalSiteNameFromRequest('/files/civicrm');
+    if ($siteName) {
+      $filesURL = $baseURL . "sites/$siteName/files/civicrm/";
+    }
+    else {
+      $filesURL = $baseURL . "sites/default/files/civicrm/";
+    }
+
+    return array(
+      'url' => $filesURL,
+      'path' => CRM_Utils_File::baseFilePath(),
+    );
+  }
+
   /**
    * @inheritDoc
    */
@@ -393,6 +414,26 @@ abstract class CRM_Utils_System_DrupalBase extends CRM_Utils_System_Base {
     return defined('VERSION') ? VERSION : 'Unknown';
   }
 
+  /**
+   * @inheritDoc
+   */
+  public function isUserRegistrationPermitted() {
+    if (!variable_get('user_register', TRUE)) {
+      return FALSE;
+    }
+    return TRUE;
+  }
+
+  /**
+   * @inheritDoc
+   */
+  public function isPasswordUserGenerated() {
+    if (variable_get('user_email_verification', TRUE)) {
+      return FALSE;
+    }
+    return TRUE;
+  }
+
   /**
    * @inheritDoc
    */
@@ -409,25 +450,25 @@ abstract class CRM_Utils_System_DrupalBase extends CRM_Utils_System_Base {
     // 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;
+    $language = $this->getCurrentLanguage();
 
     if (empty($language)) {
       return NULL;
     }
 
-    if ($language->language == 'zh-hans') {
+    if ($language == 'zh-hans') {
       return 'zh_CN';
     }
 
-    if ($language->language == 'zh-hant') {
+    if ($language == 'zh-hant') {
       return 'zh_TW';
     }
 
-    if (preg_match('/^.._..$/', $language->language)) {
-      return $language->language;
+    if (preg_match('/^.._..$/', $language)) {
+      return $language;
     }
 
-    return CRM_Core_I18n_PseudoConstant::longForShort(substr($language->language, 0, 2));
+    return CRM_Core_I18n_PseudoConstant::longForShort(substr($language, 0, 2));
   }
 
   /**
@@ -551,8 +592,9 @@ abstract class CRM_Utils_System_DrupalBase extends CRM_Utils_System_Base {
    * @param string $civicrm_root
    *
    * @return null|string
+   * @deprecated
    */
-  public function parseDrupalSiteName($civicrm_root) {
+  public function parseDrupalSiteNameFromRoot($civicrm_root) {
     $siteName = NULL;
     if (strpos($civicrm_root,
         DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . 'all' . DIRECTORY_SEPARATOR . 'modules'
@@ -575,4 +617,61 @@ abstract class CRM_Utils_System_DrupalBase extends CRM_Utils_System_Base {
     return $siteName;
   }
 
+  /**
+   * Determine if Drupal multi-site applies to the current request -- and,
+   * specifically, determine the name of the multisite folder.
+   *
+   * @param string $flagFile
+   *   Check if $flagFile exists inside the site dir.
+   * @return null|string
+   *   string, e.g. `bar.example.com` if using multisite.
+   *   NULL if using the default site.
+   */
+  private function parseDrupalSiteNameFromRequest($flagFile = '') {
+    $phpSelf = array_key_exists('PHP_SELF', $_SERVER) ? $_SERVER['PHP_SELF'] : '';
+    $httpHost = array_key_exists('HTTP_HOST', $_SERVER) ? $_SERVER['HTTP_HOST'] : '';
+    if (empty($httpHost)) {
+      $httpHost = parse_url(CIVICRM_UF_BASEURL, PHP_URL_HOST);
+      if (parse_url(CIVICRM_UF_BASEURL, PHP_URL_PORT)) {
+        $httpHost .= ':' . parse_url(CIVICRM_UF_BASEURL, PHP_URL_PORT);
+      }
+    }
+
+    $confdir = $this->cmsRootPath() . '/sites';
+
+    if (file_exists($confdir . "/sites.php")) {
+      include $confdir . "/sites.php";
+    }
+    else {
+      $sites = array();
+    }
+
+    $uri = explode('/', $phpSelf);
+    $server = explode('.', implode('.', array_reverse(explode(':', rtrim($httpHost, '.')))));
+    for ($i = count($uri) - 1; $i > 0; $i--) {
+      for ($j = count($server); $j > 0; $j--) {
+        $dir = implode('.', array_slice($server, -$j)) . implode('.', array_slice($uri, 0, $i));
+        if (file_exists("$confdir/$dir" . $flagFile)) {
+          \Civi::$statics[__CLASS__]['drupalSiteName'] = $dir;
+          return \Civi::$statics[__CLASS__]['drupalSiteName'];
+        }
+        // check for alias
+        if (isset($sites[$dir]) && file_exists("$confdir/{$sites[$dir]}" . $flagFile)) {
+          \Civi::$statics[__CLASS__]['drupalSiteName'] = $sites[$dir];
+          return \Civi::$statics[__CLASS__]['drupalSiteName'];
+        }
+      }
+    }
+  }
+
+  /**
+   * Function to return current language of Drupal
+   *
+   * @return string
+   */
+  public function getCurrentLanguage() {
+    global $language;
+    return (!empty($language->language)) ? $language->language : $language;
+  }
+
 }