You are here

function shurly_service_shorten in ShURLy 8

Same name and namespace in other branches
  1. 6 shurly_service/shurly_service.inc \shurly_service_shorten()
  2. 7 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 13
Link general search functionalities to services module.

Code

function shurly_service_shorten() {
  $defaults = [
    'format' => 'json',
    // 'domain' => NULL,
    'longUrl' => '',
    'shortUrl' => NULL,
    'apiKey' => NULL,
    // Function name for padded JSON.
    '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 ? \Drupal::entityTypeManager()
    ->getStorage('user')
    ->load($uid) : NULL;
  $access = $account
    ->hasPermission('Create short URLs');
  if ($access) {

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