You are here

function url_to_path in Emogrifier 7

Same name and namespace in other branches
  1. 8 url_to_path.inc \url_to_path()
  2. 6 url_to_path.inc \url_to_path()

Returns the local relative path corresponding to a given URL, if possible.

Finds the configuration directory matching the given URL, and compares it to the current configuration directory. Returns a path relative to the drupal install if they match, and FALSE if they don't.

In a properly-configured multisite installation, this function helps answer the question, "Could a given 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.

Parameters

$url: The internal path or external URL being linked to, such as "drupal/node/34" or "http://example.com/drupal/foo".

Return value

FALSE if $url contains a host/port/path that does not match the current site, or else a drupal path such as "node/34" or "foo".

See also

conf_path(), find_conf_path()

1 call to url_to_path()
url_to_realpath in ./url_to_path.inc
Returns an absolute local path corresponding to a given URL, if possible.
1 string reference to 'url_to_path'
emogrifier.module in ./emogrifier.module

File

./url_to_path.inc, line 73
Provide a url_to_path() function by refactoring and repurposing conf_path().

Code

function url_to_path($url) {
  global $base_path;
  $local =& drupal_static('__FUNCTION__', array());
  $base_len = strlen($base_path);
  $parts = parse_url($url);
  $http_host = isset($parts['host']) ? $parts['host'] : $_SERVER['HTTP_HOST'];
  if (isset($parts['port'])) {
    $http_host .= ':' . $parts['port'];
  }
  $script_name = '/';
  if (isset($parts['path'])) {
    $script_name .= ltrim($parts['path'], '/');
  }
  $path = $http_host . $script_name;
  if (!isset($local[$path])) {
    $local[$path] = conf_path() == find_conf_path($http_host, $script_name) && !strncmp($script_name, $base_path, $base_len);
  }
  return $local[$path] ? substr($script_name, $base_len) : FALSE;
}