function shorten_url in Shorten URLs 7
Same name and namespace in other branches
- 8.2 shorten.module \shorten_url()
- 8 shorten.module \shorten_url()
- 6 shorten.module \shorten_url()
- 7.2 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_tokens in ./
shorten.module - Implements hook_tokens().
- _shortener_url_behavior in shortener/
shortener.module - Determines the link caption based on the filter behavior setting.
File
- ./
shorten.module, line 133 - 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),
));
}
$original_for_caching = urlencode($original);
// https://www.drupal.org/node/2324925
if (!$service) {
$service = variable_get('shorten_service', 'is.gd');
}
$cached = cache_get($original_for_caching, 'cache_shorten');
if (!empty($cached->data) && REQUEST_TIME < $cached->expire) {
return $cached->data;
}
$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', FALSE)) {
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_duration = variable_get('shorten_cache_duration', 1814400);
// Only cache failed retrievals for a limited amount of time.
if ($url == $original) {
$expire = REQUEST_TIME + variable_get('shorten_cache_fail_duration', 1800);
}
elseif (is_numeric($cache_duration)) {
$expire = REQUEST_TIME + $cache_duration;
}
else {
$expire = CACHE_PERMANENT;
}
cache_set($original_for_caching, $url, 'cache_shorten', $expire);
module_invoke_all('shorten_create', $original, $url, $service);
return $url;
}