You are here

function path_redirect_edit_validate in Path redirect 5

File

./path_redirect.module, line 286

Code

function path_redirect_edit_validate($form_id, &$form_values, $form) {

  // Allow spaces in "from" path; encode everything but the query string
  $form_values['path'] = urlencode(preg_replace('/\\?.*/', '', $form_values['path'])) . (strstr($form_values['path'], '?') ? preg_replace('/.*(\\?)/', '$1', $form_values['path']) : '');
  form_set_value($form['path'], $form_values['path']);
  if (trim($form_values['path']) == '') {
    form_set_error('path', t('You must enter a <strong>from</strong> path.'));
  }
  else {
    $path_error = '';

    // The "from" path should not conflict with another redirect
    $result = path_redirect_load(NULL, $form_values['path']);
    if ($result && (!$form_values['rid'] || $form_values['rid'] !== $result['rid'])) {
      $path_error .= ' ' . t('The <strong>from</strong> path you entered is already redirected. You can <a href="@edit-page">edit this redirect</a> instead.', array(
        '@edit-page' => url('admin/build/path-redirect/edit/' . $result['rid']),
      ));
    }

    // Check that the "from" path is valid and contains no # fragment
    if (strpos($form_values['path'], '#') !== FALSE) {
      $path_error .= ' ' . t('You cannot redirect from a fragment anchor.');
    }

    // Make sure "from" has the form of a local Drupal path
    if (!valid_url($form_values['path'])) {
      $path_error .= ' ' . t('The redirect <strong>from</strong> path does not appear valid. This must be a local Drupal path.');
    }
    if (!empty($path_error)) {
      form_set_error('path', $path_error);
    }
  }
  if (!valid_url($form_values['redirect']) && !valid_url($form_values['redirect'], TRUE) && $form_values['redirect'] != '<front>') {
    form_set_error('redirect', t('The redirect <strong>to</strong> path does not appear valid.'));
  }
  if ($form_values['redirect'] == '<front>') {
    $form_values['redirect'] = variable_get('site_frontpage', 'node');
  }

  // check that there there are no redirect loops
  if ($form_values['path'] === $form_values['redirect']) {
    form_set_error('redirect', t('You are attempting to redirect the page to itself. This will result in an infinite loop.'));
  }
}