You are here

function shurly_create_form_validate in ShURLy 7

Same name and namespace in other branches
  1. 6 shurly.module \shurly_create_form_validate()

Validation of the main form

File

./shurly.module, line 484
description http://www.youtube.com/watch?v=Qo7qoonzTCE

Code

function shurly_create_form_validate($form, &$form_state) {
  global $base_url;
  if (!user_access('Create short URLs')) {
    form_set_error('', t('You do not have permission to create short URLs on this site'));
    return;
  }
  $rate_limit = shurly_rate_limit_allowed();
  if (!$rate_limit['allowed']) {
    form_set_error('', t('Rate limit exceeded. You are limited to @rate requests per @time minute period.', array(
      '@rate' => $rate_limit['rate'],
      '@time' => $rate_limit['time'],
    )));
    return;
  }
  $form_state['values']['long_url'] = trim($form_state['values']['long_url']);
  $form_state['values']['short_url'] = trim($form_state['values']['short_url']);
  $vals = $form_state['values'];

  // check that they've entered a URL
  if ($vals['long_url'] == '' || $vals['long_url'] == 'http://' || $vals['long_url'] == 'https://') {
    form_set_error('long_url', t('Please enter a web URL'));
  }
  elseif (!shurly_validate_long($form_state['values']['long_url'])) {
    form_set_error('long_url', t('Invalid URL'));
  }
  if (trim($vals['short_url']) != '') {

    // a custom short URL has been entered
    $form_state['custom'] = TRUE;
    if (!shurly_validate_custom($vals['short_url'])) {
      form_set_error('short_url', t('Short URL contains unallowed characters'));
    }
    elseif ($exists = shurly_url_exists($vals['short_url'], $vals['long_url'])) {
      form_set_error('short_url', t('This short URL has already been used'));

      //if ($exists == 'found') {

      //  form_set_error('short_url', t('This short URL is already used'));

      //}

      //else {

      //  $form_state['storage']['shurly']['final_url'] = url($vals['short_url'], array('absolute' => TRUE));
      //  $form_state['url_exists'] = TRUE;
      //  drupal_set_message(t('This URL pair already exists'), 'error');

      //}
    }
    elseif (_surl($vals['short_url'], array(
      'absolute' => TRUE,
    )) == $vals['long_url'] || _surl($vals['short_url'], array(
      'absolute' => TRUE,
      'base_url' => variable_get('shurly_base', $base_url),
    )) == $vals['long_url']) {

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

    // custom short URL field is empty
    $form_state['custom'] = FALSE;
    if ($exist = shurly_get_latest_short($vals['long_url'], $GLOBALS['user']->uid)) {
      $short = $exist;

      // we flag this as URL Exists so that it displays but doesn't get saved to the db
      $form_state['url_exists'] = TRUE;
    }
    else {
      $short = shurly_next_url();
      $form_state['url_exists'] = FALSE;
    }
    $form_state['values']['short_url'] = $short;
    $form_state['storage']['shurly']['short_url'] = $short;
  }

  // check that the destination URL is "safe"
  if (variable_get('shurly_gsb', NULL)) {
    $gsb = shurly_gsb($vals['long_url']);
    if ($gsb) {
      form_set_error('long_url', t('This URL is either phishing, malware, or both.'));
    }
  }
}