You are here

function shurly_service_shorten in ShURLy 7

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

Callback for shurly/api/shorten

1 string reference to 'shurly_service_shorten'
shurly_service_menu in shurly_service/shurly_service.module
Implements hook_menu().

File

shurly_service/shurly_service.inc, line 11
Link general search functionalities to services module.

Code

function shurly_service_shorten() {
  $defaults = array(
    'format' => 'json',
    // 'domain' => NULL,
    'longUrl' => '',
    'shortUrl' => NULL,
    'apiKey' => NULL,
    'func' => 'urlData',
  );
  $input = $_GET + $defaults;
  module_load_include('inc', 'shurly_service', 'shurly_api_keys');
  $uid = isset($input['apiKey']) ? shurly_get_uid($input['apiKey']) : NULL;
  $account = $uid ? user_load($uid) : NULL;
  $access = user_access('Create short URLs', $account);
  if ($access) {

    // If the user doesn't have access to request a custom short URL from the
    // service, reset it to NULL.
    if (!user_access('Request custom short URL', $account)) {
      $input['shortUrl'] = NULL;
    }
    $data = shurly_shorten($input['longUrl'], $input['shortUrl'], $account);
  }
  else {
    $data = array(
      'success' => FALSE,
      'error' => t('Invalid API key'),
    );
  }
  shurly_service_output($data, $input);
}