Merge pull request #2843 from kurund/CRM-14427
[civicrm-core.git] / CRM / Utils / System / DrupalBase.php
index 5550afc9fb8b0959790607c830f856d5785dc69e..e8b5a82375d8548858f01e7035177860a077fcfe 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
@@ -71,4 +71,131 @@ abstract class CRM_Utils_System_DrupalBase extends CRM_Utils_System_Base {
     $url = $config->userFrameworkBaseURL;
     return array($url, $siteName, $siteRoot);
   }
-}
\ No newline at end of file
+
+  /**
+   * 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. The drupal_add_js fn is able to add js more
+   * efficiently if it is known to be in the drupal site
+   */
+  function formatResourceUrl(&$url) {
+    $internal = FALSE;
+    $base = CRM_Core_Config::singleton()->resourceBase;
+    global $base_url;
+    // Handle absolute urls
+    // 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), '/');
+    }
+    // Handle relative urls that are within the CiviCRM module directory
+    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;
+  }
+
+  /**
+   * Generate an internal CiviCRM URL (copied from DRUPAL/includes/common.inc#url)
+   *
+   * @param $path     string   The path being linked to, such as "civicrm/add"
+   * @param $query    string   A query string to append to the link.
+   * @param $absolute boolean  Whether to force the output to be an absolute link (beginning with http:).
+   *                           Useful for links that will be displayed outside the site, such as in an
+   *                           RSS feed.
+   * @param $fragment string   A fragment identifier (named anchor) to append to the link.
+   * @param $htmlize  boolean  whether to convert to html eqivalant
+   * @param $frontend boolean  a gross joomla hack
+   * @param $forceBackend boolean  a gross joomla hack
+   *
+   * @return string an HTML string containing a link to the given path.
+   * @access public
+   *
+   */
+  function url($path = NULL, $query = NULL, $absolute = FALSE,
+    $fragment = NULL, $htmlize = TRUE,
+    $frontend = FALSE, $forceBackend = FALSE
+  ) {
+    $config = CRM_Core_Config::singleton();
+    $script = 'index.php';
+
+    $path = CRM_Utils_String::stripPathChars($path);
+
+    if (isset($fragment)) {
+      $fragment = '#' . $fragment;
+    }
+
+    if (!isset($config->useFrameworkRelativeBase)) {
+      $base = parse_url($config->userFrameworkBaseURL);
+      $config->useFrameworkRelativeBase = $base['path'];
+    }
+    $base = $absolute ? $config->userFrameworkBaseURL : $config->useFrameworkRelativeBase;
+
+    $separator = $htmlize ? '&amp;' : '&';
+
+    if (!$config->cleanURL) {
+      if (isset($path)) {
+        if (isset($query)) {
+          return $base . $script . '?q=' . $path . $separator . $query . $fragment;
+        }
+        else {
+          return $base . $script . '?q=' . $path . $fragment;
+        }
+      }
+      else {
+        if (isset($query)) {
+          return $base . $script . '?' . $query . $fragment;
+        }
+        else {
+          return $base . $fragment;
+        }
+      }
+    }
+    else {
+      if (isset($path)) {
+        if (isset($query)) {
+          return $base . $path . '?' . $query . $fragment;
+        }
+        else {
+          return $base . $path . $fragment;
+        }
+      }
+      else {
+        if (isset($query)) {
+          return $base . $script . '?' . $query . $fragment;
+        }
+        else {
+          return $base . $fragment;
+        }
+      }
+    }
+  }
+}