From 24914c2719e59a090ea4e9868b4ed0159d54bfe7 Mon Sep 17 00:00:00 2001
From: Bob Vincent <bobvin@pillars.net>
Date: Fri, 1 Apr 2011 14:59:56 -0400
Subject: [PATCH] Issue #1113460 by pillarsdotnet: provide url_is_local() function.

---
 includes/bootstrap.inc |   12 ++++++++++--
 includes/common.inc    |   27 +++++++++++++++++++++++++++
 2 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc
index de9a175..8bc5035 100644
--- a/includes/bootstrap.inc
+++ b/includes/bootstrap.inc
@@ -381,6 +381,14 @@ 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'];
+  return ($conf = _conf_path($script_name, $http_host, $require_settings));
+}
+/**
+ * Helper function for conf_path()
+ */
+function _conf_path($http_host, $script_name, $require_settings = TRUE) {
   $confdir = 'sites';
 
   $sites = array();
@@ -389,8 +397,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 00b2e71..b38b5ac 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -2197,6 +2197,33 @@ function url_is_external($path) {
   return $colonpos !== FALSE && !preg_match('![/?#]!', substr($path, 0, $colonpos)) && drupal_strip_dangerous_protocols($path) == $path;
 }
 
+
+/**
+ * Return TRUE if a path is local to the current drupal instance.
+ *
+ * @param $path
+ *   The internal path or external URL being linked to, such as "node/34" or
+ *   "http://example.com/foo".
+ * @return
+ *   Boolean TRUE or FALSE, where TRUE indicates a local path.
+ */
+function url_is_local($path) {
+  $local = &drupal_static(__FUNCTION__, '');
+
+  $url = parse_url($path, PHP_URL_HOST);
+  $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() == _conf_path($http_host, $script_name));
+  }
+  return $local[$path];
+}
+
 /**
  * Format an attribute string for a HTTP header.
  *
-- 
1.7.1

