CRM-13737 drupal integration - Use basename fn to extract last dir in the url as...
[civicrm-core.git] / CRM / Utils / System / DrupalBase.php
index 82b79dd3e89ee2a06c1d841b359f47b846a79e5a..efd4b6036e83b56d4a1260bbda89cc69dff2eee6 100644 (file)
@@ -71,4 +71,53 @@ abstract class CRM_Utils_System_DrupalBase extends CRM_Utils_System_Base {
     $url = $config->userFrameworkBaseURL;
     return array($url, $siteName, $siteRoot);
   }
+
+  /**
+   * Check if a resource url is within the drupal directory and format appropriately
+   *
+   * @param url (reference)
+   *
+   * @return bool: TRUE for internal paths, FALSE for external
+   */
+  function formatResourceUrl(&$url) {
+    $internal = FALSE;
+    $base = CRM_Core_Config::singleton()->resourceBase;
+    global $base_url;
+    // Handle absolute urls
+    if (strpos($url, $base_url) === 0) {
+      $internal = TRUE;
+      $url = $this->appendCoreDirectoryToResourceBase($url);
+      $url = trim(str_replace($base_url, '', $url), '/');
+    }
+    // Handle relative urls
+    elseif (strpos($url, $base) === 0) {
+      $internal = TRUE;
+      $url = $this->appendCoreDirectoryToResourceBase(substr(drupal_get_path('module', 'civicrm'), 0, -6)) . trim(substr($url, strlen($base)), '/');
+    }
+    // Strip query string
+    $q = strpos($url, '?');
+    if ($q && $internal) {
+      $url = substr($url, 0, $q);
+    }
+    return $internal;
+  }
+
+  /**
+   * In instance where civicrm folder has a drupal folder & a civicrm core folder @ the same level append the
+   * civicrm folder name to the url
+   * See CRM-13737 for discussion of how this allows implementers to alter the folder structure
+   * @todo - this only provides a limited amount of flexiblity - it still expects a 'civicrm' folder with a 'drupal' folder
+   * and is only flexible as to the name of the civicrm folder.
+   *
+   * @param string $url potential resource url based on standard folder assumptions
+   * @return string $url with civicrm-core directory appended if not standard civi dir
+   */
+  function appendCoreDirectoryToResourceBase($url) {
+    global $civicrm_root;
+    $lastDirectory = basename($civicrm_root);
+    if(!$lastDirectory != 'civicrm') {
+      return $url .= $lastDirectory . '/';
+    }
+    return $url;
+  }
 }