You are here

public function FeedsFileFetcher::sourceFormValidate in Feeds 7.2

Same name and namespace in other branches
  1. 6 plugins/FeedsFileFetcher.inc \FeedsFileFetcher::sourceFormValidate()
  2. 7 plugins/FeedsFileFetcher.inc \FeedsFileFetcher::sourceFormValidate()

Overrides parent::sourceFormValidate().

Overrides FeedsPlugin::sourceFormValidate

File

plugins/FeedsFileFetcher.inc, line 131
Home of the FeedsFileFetcher and related classes.

Class

FeedsFileFetcher
Fetches data via HTTP.

Code

public function sourceFormValidate(&$values) {
  $values['source'] = trim($values['source']);
  if (empty($this->config['direct'])) {
    $feed_dir = $this->config['directory'];
    if (!file_prepare_directory($feed_dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
      if (user_access('administer feeds')) {
        $plugin_key = feeds_importer($this->id)->config[$this
          ->pluginType()]['plugin_key'];
        $link = url('admin/structure/feeds/' . $this->id . '/settings/' . $plugin_key);
        form_set_error('feeds][FeedsFileFetcher][source', t('Upload failed. Please check the upload <a href="@link">settings.</a>', array(
          '@link' => $link,
        )));
      }
      else {
        form_set_error('feeds][FeedsFileFetcher][source', t('Upload failed. Please contact your site administrator.'));
      }
      watchdog('feeds', 'The upload directory %directory required by a feed could not be created or is not accessible. A newly uploaded file could not be saved in this directory as a consequence, and the upload was canceled.', array(
        '%directory' => $feed_dir,
      ));
    }
    elseif ($file = file_save_upload('feeds', array(
      'file_validate_extensions' => array(
        0 => $this->config['allowed_extensions'],
      ),
    ), $feed_dir)) {
      $values['source'] = $file->uri;
      $values['file'] = $file;
    }
    elseif (empty($values['source'])) {
      form_set_error('feeds][FeedsFileFetcher][source', t('Please upload a file.'));
    }
    else {

      // File present from previous upload. Nothing to validate.
    }
  }
  else {

    // Check if chosen url scheme is allowed.
    $scheme = file_uri_scheme($values['source']);
    if (!$scheme || !in_array($scheme, $this->config['allowed_schemes'])) {
      form_set_error('feeds][FeedsFileFetcher][source', t("The file needs to reside within the site's files directory, its path needs to start with scheme://. Available schemes: @schemes.", array(
        '@schemes' => implode(', ', $this->config['allowed_schemes']),
      )));
    }
    elseif (!is_readable($values['source'])) {
      form_set_error('feeds][FeedsFileFetcher][source', t('The specified file or directory does not exist.'));
    }
  }
}