Newer
Older
Alex Barth
committed
* Home of the FeedsFileFetcher and related classes.
/**
* Definition of the import batch object created on the fetching stage by
* FeedsFileFetcher.
*/
class FeedsFileBatch extends FeedsImportBatch {
protected $file_path;
/**
* Constructor.
*/
public function __construct($file_path) {
$this->file_path = $file_path;
parent::__construct();
}
/**
* Implementation of FeedsImportBatch::getRaw();
*/
public function getRaw() {
return file_get_contents(realpath($this->file_path));
}
/**
* Implementation of FeedsImportBatch::getFilePath().
*/
public function getFilePath() {
return $this->file_path;
}
}
/**
* Fetches data via HTTP.
*/
class FeedsFileFetcher extends FeedsFetcher {
Alex Barth
committed
/**
* Implementation of FeedsFetcher::fetch().
Alex Barth
committed
*/
public function fetch(FeedsSource $source) {
$source_config = $source->getConfigFor($this);
return new FeedsFileBatch($source_config['source']);
Alex Barth
committed
}
Alex Barth
committed
public function sourceForm($source_config) {
Alex Barth
committed
// When renaming, do not forget feeds_vews_handler_field_source class.
Alex Barth
committed
'#type' => 'textfield',
Alex Barth
committed
'#description' => t('Specify a file in the site\'s file system path or upload a file below.'),
'#default_value' => isset($source_config['source']) ? $source_config['source'] : '',
);
$form['upload'] = array(
'#type' => 'file',
'#title' => t('Upload'),
'#description' => t('Choose a file from your local computer.'),
);
return $form;
}
/**
Alex Barth
committed
* Override parent::sourceFormValidate().
Alex Barth
committed
public function sourceFormValidate(&$values) {
$feed_dir = file_directory_path() .'/feeds';
file_check_directory($feed_dir, TRUE);
// If there is a file uploaded, save it, otherwise validate input on
// file.
if ($file = file_save_upload('feeds', array(), $feed_dir)) {
file_set_status($file, FILE_STATUS_PERMANENT);
$values['source'] = $file->filepath;
}
elseif (empty($values['source'])) {
form_set_error('feeds][source', t('Upload a file first.'));
}
// If a file has not been uploaded and $values['source'] is not empty, make
// sure that this file is within Drupal's files directory as otherwise
// potentially any file that the web server has access could be exposed.
Alex Barth
committed
elseif (!file_check_location($values['source'], file_directory_path())) {
form_set_error('feeds][source', t('File needs to point to a file in your Drupal file system path.'));
}