You are here

function shurly_create_form_validate in ShURLy 6

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

File

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

Code

function shurly_create_form_validate(&$form, &$form_state) {
  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_state['values']['short_url'])) {
    shurly_errors_to_form();
  }
  if ($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'));
    }
    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['values']['short_url'] = $short;
    $form_state['storage']['shurly']['short_url'] = $short;
  }
}