You are here

function shurly_service_expand in ShURLy 8

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

Callback for shurly/api/shorten.

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

File

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

Code

function shurly_service_expand() {
  $defaults = [
    'format' => 'json',
    // 'domain' => NULL,
    // 'longUrl' => '',
    'shortUrl' => '',
    // 'login' => 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('Expand short URLs');
  if ($access) {

    // Only works with clean URLs.
    $path = array_pop(explode('/', parse_url($input['shortUrl'], PHP_URL_PATH)));
    $data = shurly_expand($path, $account);
  }
  else {
    $data = [
      'success' => FALSE,
      'error' => t('Invalid API key'),
    ];
  }
  shurly_service_output($data, $input);
}