Merge pull request #12137 from eileenmcnaughton/year_fiscal
[civicrm-core.git] / CRM / Utils / System / DrupalBase.php
index bd5740c699549366b2ecb7a82a3a3f1aa3a7a249..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,30 +58,6 @@ abstract class CRM_Utils_System_DrupalBase extends CRM_Utils_System_Base {
     $this->supports_form_extensions = TRUE;
   }
 
-  /**
-   * @inheritDoc
-   */
-  public function getCiviSourceStorage() {
-    global $civicrm_root;
-
-    // Don't use $config->userFrameworkBaseURL; it has garbage on it.
-    // More generally, we shouldn't be using $config here.
-    if (!defined('CIVICRM_UF_BASEURL')) {
-      throw new RuntimeException('Undefined constant: CIVICRM_UF_BASEURL');
-    }
-
-    $cmsUrl = CIVICRM_UF_BASEURL;
-    if (CRM_Utils_System::isSSL()) {
-      $cmsUrl = str_replace('http://', 'https://', $cmsUrl);
-    }
-    $civiRelPath = CRM_Utils_File::relativize(realpath($civicrm_root), realpath($this->cmsRootPath()));
-    $civiUrl = rtrim($cmsUrl, '/') . '/' . ltrim($civiRelPath, ' /');
-    return array(
-      'url' => CRM_Utils_File::addTrailingSlash($civiUrl, '/'),
-      'path' => CRM_Utils_File::addTrailingSlash($civicrm_root),
-    );
-  }
-
   /**
    * @inheritdoc
    */
@@ -89,7 +65,7 @@ abstract class CRM_Utils_System_DrupalBase extends CRM_Utils_System_Base {
     $config = CRM_Core_Config::singleton();
     $baseURL = CRM_Utils_System::languageNegotiationURL($config->userFrameworkBaseURL, FALSE, TRUE);
 
-    $siteName = $this->parseDrupalSiteName('/files/civicrm');
+    $siteName = $this->parseDrupalSiteNameFromRequest('/files/civicrm');
     if ($siteName) {
       $filesURL = $baseURL . "sites/$siteName/files/civicrm/";
     }
@@ -438,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
    */
@@ -454,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));
   }
 
   /**
@@ -590,6 +586,37 @@ abstract class CRM_Utils_System_DrupalBase extends CRM_Utils_System_Base {
     return user_load($userID);
   }
 
+  /**
+   * Parse the name of the drupal site.
+   *
+   * @param string $civicrm_root
+   *
+   * @return null|string
+   * @deprecated
+   */
+  public function parseDrupalSiteNameFromRoot($civicrm_root) {
+    $siteName = NULL;
+    if (strpos($civicrm_root,
+        DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . 'all' . DIRECTORY_SEPARATOR . 'modules'
+      ) === FALSE
+    ) {
+      $startPos = strpos($civicrm_root,
+        DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR
+      );
+      $endPos = strpos($civicrm_root,
+        DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR
+      );
+      if ($startPos && $endPos) {
+        // if component is in sites/SITENAME/modules
+        $siteName = substr($civicrm_root,
+          $startPos + 7,
+          $endPos - $startPos - 7
+        );
+      }
+    }
+    return $siteName;
+  }
+
   /**
    * Determine if Drupal multi-site applies to the current request -- and,
    * specifically, determine the name of the multisite folder.
@@ -600,7 +627,7 @@ abstract class CRM_Utils_System_DrupalBase extends CRM_Utils_System_Base {
    *   string, e.g. `bar.example.com` if using multisite.
    *   NULL if using the default site.
    */
-  private function parseDrupalSiteName($flagFile = '') {
+  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)) {
@@ -637,4 +664,14 @@ abstract class CRM_Utils_System_DrupalBase extends CRM_Utils_System_Base {
     }
   }
 
+  /**
+   * Function to return current language of Drupal
+   *
+   * @return string
+   */
+  public function getCurrentLanguage() {
+    global $language;
+    return (!empty($language->language)) ? $language->language : $language;
+  }
+
 }