From ce939dbf84fc4e487d1ab80e25b2662f75149074 Mon Sep 17 00:00:00 2001
From: Bob Vincent <bobvin@pillars.net>
Date: Mon, 4 Apr 2011 13:10:47 -0400
Subject: [PATCH] #1113588 by pillarsdotnet: Provide url_is_local() and find_conf_path() functions.

---
 includes/bootstrap.inc |   26 ++++++++++++++++++++++-
 includes/common.inc    |   51 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 75 insertions(+), 2 deletions(-)

diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc
index fe1741d..5e999ec 100644
--- a/includes/bootstrap.inc
+++ b/includes/bootstrap.inc
@@ -381,6 +381,28 @@ function conf_path($require_settings = TRUE, $reset = FALSE) {
     return $conf;
   }
 
+  $script_name = $_SERVER['SCRIPT_NAME'] ? $_SERVER['SCRIPT_NAME'] : $_SERVER['SCRIPT_FILENAME'];
+  $http_host = $_SERVER['HTTP_HOST'];
+  $conf = find_conf_path($script_name, $http_host, $require_settings);
+  return $conf;
+}
+
+/**
+ * Find the appropriate configuration directory for a given host and path.
+ *
+ * @param $http_host
+ *   The hostname and optional port number, e.g. "www.example.com" or
+ *   "www.example.com:8080".
+ *
+ * @param $script_name
+ *   The part of the url following the hostname, including the leading slash.
+ *
+ * @return
+ *   The path of the matching directory.
+ *
+ * @see conf_path()
+ */
+function find_conf_path($http_host, $script_name, $require_settings = TRUE) {
   $confdir = 'sites';
 
   $sites = array();
@@ -389,8 +411,8 @@ function conf_path($require_settings = TRUE, $reset = FALSE) {
     include(DRUPAL_ROOT . '/' . $confdir . '/sites.php');
   }
 
-  $uri = explode('/', $_SERVER['SCRIPT_NAME'] ? $_SERVER['SCRIPT_NAME'] : $_SERVER['SCRIPT_FILENAME']);
-  $server = explode('.', implode('.', array_reverse(explode(':', rtrim($_SERVER['HTTP_HOST'], '.')))));
+  $uri = explode('/', $script_name);
+  $server = explode('.', implode('.', array_reverse(explode(':', rtrim($http_host, '.')))));
   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));
diff --git a/includes/common.inc b/includes/common.inc
index 0a3ed73..0f4d643 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -2197,6 +2197,57 @@ function url_is_external($path) {
 }
 
 /**
+ * Determines whether a given URL could be local to the current Drupal site.
+ *
+ * Finds the configuration directory matching the given URL, and compares it
+ * to the current configuration directory. Returns a relative URL path if they
+ * match, and FALSE if they don't.
+ *
+ * In a properly-configured multisite installation, this function helps answer
+ * the question, "Could this URL match a file or path within my site?"
+ *
+ * Whether the URL does in fact resolve to this site, or at all, cannot be
+ * determined within Drupal itself.  This is only a sanity check.
+ *
+ * For example, when looking for image URLs within an HTML Mail body for
+ * possible conversion to inline attachments, the following code might be used:
+ * @code
+ *   if ( !url_is_external($url)
+ *     && ($path = url_is_local($url))
+ *     && file_exists($path) ) {
+ *     // Attach $path and replace $url.
+ *     ...
+ *   }
+ * @endcode
+ *
+ * @param $path
+ *   The internal path or external URL being linked to, such as "node/34" or
+ *   "http://example.com/foo".
+ * @return
+ *   A relative path, or FALSE.
+ *
+ * @see conf_path()
+ */
+function url_is_local($path) {
+  $local = &drupal_static(__FUNCTION__, '');
+
+  $url = drupal_parse_url($path);
+  $http_host = $url[PHP_URL_HOST];
+  if (isset($url[PHP_URL_PORT])) {
+    $http_host .= ':' . $url[PHP_URL_PORT];
+  }
+  $script_name = $url[PHP_URL_PATH];
+  $path = $http_host . $script_name;
+
+  if (!isset($local[$path])) {
+    $local[$path] = (
+      conf_path() == find_conf_path($http_host, $script_name)
+    );
+  }
+  return $local[$path] ? $script_name : FALSE;
+}
+
+/**
  * Format an attribute string for a HTTP header.
  *
  * @param $attributes
-- 
1.7.1

