You are here

function linkit_parse_url in Linkit 7.3

Retrieve relevant information about a URL. Specifically this function is usable for internal (absolute) URL:s, but it also works for external URL:s.

Parameters

$url: The search string (URL) that should be scanned.

$parts: An array of URL parts from parse_url().

Return value

An associative array containing:

  • url: The same as the argument $url, untouched.
  • target: Either "internal" or "external".
  • requested_path: If internal, the path requested relative to Drupal root. The only exception is when frontpage is referred directly, then it will be whatever the frontpage is set to.
  • system_path: If internal and the path is valid, the Drupal system path, e.g. "node/23".
  • query_fragment: If internal, the query and fragment of the url. Typically it is not needed for searching and is just reappended back when processing of the path is done. It could e.g. look like "?foo=bar#anchor".
1 call to linkit_parse_url()
linkit_autocomplete_absolute_url in ./linkit.module
Retrieve the result object from an absolute URL. Both internal and external paths work.

File

./linkit.module, line 832
Main file for Linkit module.

Code

function linkit_parse_url($url, $parts) {
  global $base_url;

  // Make a new array, this will hold the components from parse_url() and our
  // own "Linkit" components.
  $path_info = array();

  // Append the original components from parse_url() to our array.
  $path_info += $parts;

  // Save the whole URL.
  $path_info['url'] = $url;
  if (!isset($path_info['query'])) {
    $path_info['query'] = '';
  }

  // Convert the query string to an array as Drupal can only handle querys as
  // arrays.
  // @see http://api.drupal.org/drupal_http_build_query
  parse_str($path_info['query'], $path_info['query']);

  // The 'q' parameter contains the path of the current page if clean URLs are
  // disabled. It overrides the 'path' of the URL when present, even if clean
  // URLs are enabled, due to how Apache rewriting rules work.
  if (isset($path_info['query']['q'])) {
    $path_info['path'] = $path_info['query']['q'];
    unset($path_info['query']['q']);
  }

  // Load all local stream wrappers. The $path_info['scheme'] will be tested
  // against this later to ensure it is local.
  $local_stream_wrappers = file_get_stream_wrappers(STREAM_WRAPPERS_LOCAL);

  // Internal URL.
  // We can not use the url_is_external() as it treats all absolute links as
  // external and treats all stream wrappers as internal.
  $local_hosts = array(
    $base_url,
  );

  // Give other modules a chance to add/remove/alter the local hosts.
  drupal_alter('linkit_local_hosts', $local_hosts);
  $local_url = isset($path_info['scheme']) && isset($path_info['host']) && in_array(trim($path_info['scheme'] . '://' . $path_info['host'] . base_path(), '/'), $local_hosts);
  $local_stream_wrapper = isset($local_stream_wrappers[$path_info['scheme']]);

  //@TODO: maybe file_valid_uri() ?
  if ($local_url || $local_stream_wrapper) {

    // Set target as internal.
    $path_info['target'] = 'internal';

    // If this is seems to be a valid local stream wrapper string, force the
    // $path_info['path'] to be set to the file_url.
    if ($local_stream_wrapper) {
      $path_info = array_merge($path_info, parse_url(file_create_url($path_info['url'])));
    }

    // Trim the path from slashes.
    $path_info['path'] = trim($path_info['path'], '/');

    // If we have an empty path, and an internal target, we can assume that the
    // URL should go the the frontpage.
    if (empty($path_info['path'])) {
      $path_info['frontpage'] = TRUE;
      $path_info['path'] = variable_get('site_frontpage', 'node');
    }

    // Try converting the path to an internal Drupal path.
    $internal_url = drupal_get_normal_path($path_info['path']);

    // Add the "real" system path (not the alias) if the current user have
    // access to the URL.
    $path_info['system_path'] = drupal_valid_path($internal_url) ? $internal_url : FALSE;
    $menu_item = menu_get_item($path_info['system_path']);
    if ($menu_item) {
      $path_info['menu']['path'] = $path_info['system_path'];
      $path_info['menu']['description'] = check_plain($menu_item['description']);
      $path_info['menu']['title'] = check_plain($menu_item['title']);
    }

    // If we have a valid stream wrapper URL, find out the internal url.
    if ($local_stream_wrapper) {
      $path_info['system_path'] = $path_info['path'];
      $path_info['menu']['path'] = $path_info['path'];
      $path_info['menu']['description'] = 'This funciton is not fully integrated yet.';
      $path_info['menu']['title'] = 'This funciton is not fully integrated yet.';
    }
  }
  else {

    // Set target as external.
    $path_info['target'] = 'external';
  }
  return $path_info;
}