function shurly_shorten in ShURLy 7
Same name and namespace in other branches
- 8 shurly.module \shurly_shorten()
- 6 shurly.module \shurly_shorten()
API function to shorten a URL @arg $long_url - the long URL to shorten @arg $custom - optional custom short URL
Return value
an array with the following keys 'success' => TRUE or FALSE 'error' => reason for for failure 'long_url' => the long url 'short_url' => the short url
2 calls to shurly_shorten()
- shurly_service_shorten in shurly_service/
shurly_service.inc - Callback for shurly/api/shorten
- _shurly_filter_process in ./
shurly.module - Process callback for shurly filter.
File
- ./
shurly.module, line 661 - description http://www.youtube.com/watch?v=Qo7qoonzTCE
Code
function shurly_shorten($long_url, $custom = NULL, $account = NULL) {
global $base_url;
$success = FALSE;
$account = $account ? $account : $GLOBALS['user'];
$error = '';
$no_save = FALSE;
$rate_limit = shurly_rate_limit_allowed($account);
if (!$rate_limit['allowed']) {
$error = t('Rate limit exceeded. You are limited to @rate requests per @time minute period.', array(
'@rate' => $rate_limit['rate'],
'@time' => $rate_limit['time'],
));
}
elseif (!shurly_validate_long($long_url)) {
$error = t('Invalid long URL.');
}
elseif (is_null($custom)) {
$latest = shurly_get_latest_short($long_url, $account->uid);
if ($latest) {
$no_save = TRUE;
$success = TRUE;
$short = $latest;
}
else {
$short = shurly_next_url();
}
}
else {
$short = $custom;
if (!shurly_validate_custom($short) || !shurly_path_available($short)) {
$error .= $error ? ' ' : '';
$error .= t('Invalid short URL.');
}
elseif (shurly_url_exists($short)) {
$error .= $error ? ' ' : '';
$error .= t('Existing short URL.');
}
}
if (!$error && !$no_save) {
if (shurly_save_url($long_url, $short, $account, $custom)) {
$success = TRUE;
}
else {
$error = t('Unknown database error.');
}
}
return array(
'success' => $success,
'error' => $error,
'longUrl' => $long_url,
'shortUrl' => isset($short) ? _surl($short, array(
'absolute' => TRUE,
'base_url' => variable_get('shurly_base', $base_url),
)) : '',
);
}