You are here

public function DefaultController::shurly_service_shorten in ShURLy 8

1 string reference to 'DefaultController::shurly_service_shorten'
shurly_service.routing.yml in shurly_service/shurly_service.routing.yml
shurly_service/shurly_service.routing.yml

File

shurly_service/src/Controller/DefaultController.php, line 23

Class

DefaultController
Default controller for the shurly_service module.

Namespace

Drupal\shurly_service\Controller

Code

public function shurly_service_shorten() {
  $defaults = [
    '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 ? \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);
}