Merge pull request #9425 from colemanw/CRM-19649
[civicrm-core.git] / CRM / Utils / System / DrupalBase.php
index 923994fbfadcb27d17f2fe703d3dc050bfbf4314..943ea51905247840453eab19211210a66a22bf69 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.7                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2015                                |
+ | Copyright CiviCRM LLC (c) 2004-2016                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2015
+ * @copyright CiviCRM LLC (c) 2004-2016
  * $Id$
  *
  */
@@ -100,8 +100,12 @@ abstract class CRM_Utils_System_DrupalBase extends CRM_Utils_System_Base {
     // compares $url (which is some unknown/untrusted value from a third-party dev) to the CMS's base url (which is independent of civi's url)
     // to see if the url is within our drupal dir, if it is we are able to treated it as an internal url
     if (strpos($url, $base_url) === 0) {
-      $internal = TRUE;
-      $url = trim(str_replace($base_url, '', $url), '/');
+      $file = trim(str_replace($base_url, '', $url), '/');
+      // CRM-18130: Custom CSS URL not working if aliased or rewritten
+      if (file_exists(DRUPAL_ROOT . $file)) {
+        $url = $file;
+        $internal = TRUE;
+      }
     }
     // Handle relative urls that are within the CiviCRM module directory
     elseif (strpos($url, $base) === 0) {
@@ -248,7 +252,7 @@ abstract class CRM_Utils_System_DrupalBase extends CRM_Utils_System_Base {
           'cms:view user account',
         ))
     ) {
-      return CRM_Utils_System::url('user/' . $uid);
+      return url('user/' . $uid);
     };
   }
 
@@ -564,4 +568,92 @@ abstract class CRM_Utils_System_DrupalBase extends CRM_Utils_System_Base {
     return $siteName;
   }
 
+  /**
+   * @var $basepath String cached basepath to prevent having to parse it repeatedly.
+   */
+  protected $basepath;
+
+  /**
+   * @var $filesUrl String holds resolved path.
+   */
+  protected $filesUrl;
+
+  /**
+   *  checkBasePath - Returns root directory with respect to $civicrm_root
+   *
+   * @param $root String
+   * @param $seek String
+   */
+  public function checkBasePath($root, $seek = "/sites/") {
+    if (!isset($this->basepath)) {
+      $this->basepath = substr($root, 0, stripos($root, $seek) + 1);
+    }
+
+    return $this->basepath;
+  }
+
+  /**
+   * check if files exist in path.  Just a simple helper function for viewing
+   * existence of sites.
+   *
+   * @param $basepath string
+   * @param $basepath string
+   */
+  private function checkFilesExists($basepath, $folder) {
+    return file_exists("{$basepath}sites/$folder/files/civicrm/");
+  }
+
+  /**
+   * Returns the concatenated url for existing path.
+   *
+   * @param $baseUrl string
+   * @param $folder string
+   */
+  private function getUrl($baseUrl, $folder) {
+    return "{$baseUrl}sites/$folder/files/civicrm/";
+  }
+
+  /**
+   * Returns the location of /sites/SITENAME/files/civicrm depending
+   * on system configuration.
+   *
+   * @fixed CRM-19303
+   * @param $root string
+   * @param $baseUrl string
+   * @param $default string
+   */
+  public function checkMultisite($root, $baseUrl, $default = "default") {
+    if (isset($this->filesUrl)) {
+      return $this->filesUrl;
+    }
+
+    $basepath = $this->checkBasePath($root);
+    $correct = NULL;
+    if ($this->checkFilesExists($root, $default)) {
+      $correct = $default;
+    }
+    else {
+      //Check for any other directories if default doesn't exist.
+      $folders = scandir($basepath . 'sites/');
+      foreach ($folders as $folder) {
+        //Ignore hidden directories/files...
+        if (strpos($folder, '.') === 0 || $folder == 'all') {
+          continue;
+        }
+        //Check if it is a directory
+        if (!is_dir($basepath . 'sites/' . $folder)) {
+          continue;
+        }
+
+        //Check if files path exists...
+        if ($this->checkFilesExists($basepath, $folder) && $folder == $_SERVER['HTTP_HOST']) {
+          $correct = $folder;
+          break;
+        }
+      }
+    }
+    $this->filesUrl = self::getUrl($baseUrl, $correct);
+    return $this->filesUrl;
+  }
+
 }