You are here

function shorten_url in Shorten URLs 6

Same name and namespace in other branches
  1. 8.2 shorten.module \shorten_url()
  2. 8 shorten.module \shorten_url()
  3. 7.2 shorten.module \shorten_url()
  4. 7 shorten.module \shorten_url()

Retrieves and beautifies the abbreviated URL. This is the main API function of this module.

Parameters

$original: The URL of the page for which to create the abbreviated URL. If not passed uses the current page.

$service: The service to use to abbreviate the URL. For services available by default, see shorten_shorten_service().

Return value

An abbreviated URL.

4 calls to shorten_url()
shorten_current in ./shorten.module
Builds a textfield to show the short URL for the current page.
shorten_form_shorten_submit in ./shorten.module
Submit function for the Shorten form.
shorten_token_values in ./shorten.module
Implementation of hook_token_values().
_shortener_url_behavior in shortener/shortener.module
Determines the link caption based on the filter behavior setting.

File

./shorten.module, line 124
Shortens URLs via external services.

Code

function shorten_url($original = '', $service = '') {
  if (!$original) {
    $original = url($_GET['q'], array(
      'absolute' => TRUE,
      'alias' => !variable_get('shorten_use_alias', 1),
    ));
  }
  if (!$service) {
    $service = variable_get('shorten_service', 'is.gd');
  }
  if ($service != 'Drupal ShortURL module' && $service != 'ShURLy') {

    // https://www.drupal.org/node/2324925
    $original = urlencode($original);
  }

  //First try to retrieve a value from the cache.
  $cached = cache_get($original, 'cache_shorten');
  if (!empty($cached->data) && time() < $cached->expire) {
    return $cached->data;
  }

  //We don't have anything cached for this URL, so try the primary service (or the one explicitly requested in the $service parameter).
  $services = module_invoke_all('shorten_service');
  if (isset($services[$service])) {
    $url = _shorten_get_url($original, $services[$service], $service);
  }

  //If the primary service fails, try the secondary service.
  if (empty($url)) {
    $service = variable_get('shorten_service_backup', 'TinyURL');
    if (isset($services[$service])) {
      $url = _shorten_get_url($original, $services[$service], $service);
    }

    //If the secondary service fails, use the original URL.
    if (empty($url)) {
      $url = $original;
    }
  }
  $url = trim($url);

  //Redundant for most services.

  //Replace "http://" with "www." if the URL is abbreviated because it's shorter.
  if ($url != $original && variable_get('shorten_www', TRUE)) {
    if (strpos($url, 'http://') === 0) {
      $url = drupal_substr($url, 7);
      if (strpos($url, 'www.') !== 0) {
        $url = 'www.' . $url;
      }
    }
    elseif (strpos($url, 'https://') === 0) {
      $url = drupal_substr($url, 8);
      if (strpos($url, 'www.') !== 0) {
        $url = 'www.' . $url;
      }
    }
  }

  //Cache the result for a limited time.
  $cache_duration = variable_get('shorten_cache_duration', 1814400);
  if ($url == $original) {
    $expire = time() + variable_get('shorten_cache_fail_duration', 1800);
  }
  elseif (is_numeric($cache_duration)) {
    $expire = time() + $cache_duration;
  }
  else {
    $expire = CACHE_PERMANENT;
  }
  cache_set($original, $url, 'cache_shorten', $expire);

  //Announce that we have a new short URL and return it.
  module_invoke_all('shorten_create', $original, $url, $service);
  return $url;
}