You are here

public function ShurlyCreateForm::validateForm in ShURLy 8

Form validation handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormBase::validateForm

File

src/Form/ShurlyCreateForm.php, line 89

Class

ShurlyCreateForm
ShurlyCreateForm.

Namespace

Drupal\shurly\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $rate_limit = shurly_rate_limit_allowed();
  if (!$rate_limit['allowed']) {
    $form_state
      ->setError('', $this
      ->t('Rate limit exceeded. You are limited to @rate requests per @time minute period.', [
      '@rate' => $rate_limit['rate'],
      '@time' => $rate_limit['time'],
    ]));
    return;
  }
  $form_state
    ->setValue('long_url', trim($form_state
    ->getValue('long_url')));
  $form_state
    ->setValue('short_url', trim($form_state
    ->getValue('short_url')));
  $vals = $form_state
    ->getValues();
  $long_url_name = 'wrapper][long_url';
  $short_url_name = 'wrapper][options][short_url';
  if ($vals['long_url']) {

    // Check that they've entered a URL.
    if (!preg_match('/^https?:\\/\\//', $vals['long_url'])) {
      $form_state
        ->setErrorByName($long_url_name, $this
        ->t('Please enter a web URL.'));
    }
    elseif (!shurly_validate_long($vals['long_url'])) {
      $form_state
        ->setErrorByName($long_url_name, t('Invalid URL.'));
    }
  }
  if ($vals['short_url']) {

    // A custom short URL has been entered.
    $form_state
      ->setValue('custom', [
      TRUE,
    ]);
    if (!shurly_validate_custom($vals['short_url'])) {
      $form_state
        ->setErrorByName($short_url_name, $this
        ->t('Short URL contains invalid characters.'));
    }
    elseif ($exists = shurly_url_exists($vals['short_url'], $vals['long_url'])) {
      $form_state
        ->setErrorByName($short_url_name, $this
        ->t('This short URL has already been used.'));
    }
    elseif (_surl($vals['short_url'], [
      'absolute' => TRUE,
    ]) == $vals['long_url'] || _surl($vals['short_url'], [
      'absolute' => TRUE,
      'base_url' => _shurly_get_shurly_base(),
    ]) == $vals['long_url']) {

      // Check that link isn't to itself (creating infinite loop)
      // problem - http vs https.
      $form_state
        ->setErrorByName($short_url_name, $this
        ->t('You cannot create links to themselves'));
    }
    elseif (!shurly_path_available($vals['short_url'])) {
      $form_state
        ->setErrorByName($short_url_name, $this
        ->t('This custom URL is reserved. Please choose another.'));
    }
  }
  else {

    // Custom short URL field is empty.
    $form_state
      ->setValue('custom', TRUE);
    if ($exist = shurly_get_latest_short($vals['long_url'], \Drupal::currentUser()
      ->id())) {
      $short = $exist;

      // We flag this as URL Exists so that it displays but doesn't get saved to the db.
      $form_state
        ->setValue('url_exists', TRUE);
    }
    else {
      $short = shurly_next_url();
    }
    $form_state
      ->setValue('short_url', $short);
    $form_state
      ->setStorage([
      'shurly' => [
        'short_url' => $short,
      ],
    ]);
  }

  // Check that the destination URL is "safe".
  if (\Drupal::config('shurly.settings')
    ->get('shurly_gsb')) {
    $gsb = shurly_gsb($vals['long_url']);
    if ($gsb) {
      $form_state
        ->setErrorByName($long_url_name, t('This URL is either phishing, malware, or both.'));
    }
  }
}