Stripping CVS keywords
[project/feeds.git] / plugins / FeedsFileFetcher.inc
CommitLineData
0ccaed76 1<?php
eabfe21e 2
0ccaed76
AB
3/**
4 * @file
967bf290 5 * Home of the FeedsFileFetcher and related classes.
0ccaed76
AB
6 */
7
8/**
967bf290
AB
9 * Definition of the import batch object created on the fetching stage by
10 * FeedsFileFetcher.
11 */
12class FeedsFileBatch extends FeedsImportBatch {
967bf290
AB
13 /**
14 * Constructor.
15 */
68613918 16 public function __construct($file_path, $feed_nid = 0) {
68613918 17 parent::__construct('', $feed_nid);
1da7a848 18 $this->file_path = $file_path;
967bf290
AB
19 }
20
21 /**
9f20738f 22 * Implements FeedsImportBatch::getRaw();
967bf290
AB
23 */
24 public function getRaw() {
1da7a848 25 return file_get_contents($this->file_path);
967bf290
AB
26 }
27
28 /**
9f20738f 29 * Implements FeedsImportBatch::getFilePath().
967bf290
AB
30 */
31 public function getFilePath() {
d364ce1d
AB
32 if (!file_exists($this->file_path)) {
33 throw new Exception(t('File @filepath is not accessible.', array('@filepath' => $this->file_path)));
34 }
967bf290
AB
35 return $this->file_path;
36 }
37}
38
39/**
0ccaed76
AB
40 * Fetches data via HTTP.
41 */
42class FeedsFileFetcher extends FeedsFetcher {
43
44 /**
9f20738f 45 * Implements FeedsFetcher::fetch().
eabfe21e
AB
46 */
47 public function fetch(FeedsSource $source) {
48 $source_config = $source->getConfigFor($this);
68613918 49 return new FeedsFileBatch($source_config['source'], $source->feed_nid);
eabfe21e
AB
50 }
51
52 /**
0ccaed76
AB
53 * Source form.
54 */
eabfe21e 55 public function sourceForm($source_config) {
725e6aeb
AB
56 $form = array();
57 $form['fid'] = array(
58 '#type' => 'value',
59 '#value' => empty($source_config['fid']) ? 0 : $source_config['fid'],
60 );
0ccaed76 61 $form['source'] = array(
b483268b 62 '#type' => empty($this->config['direct']) ? 'value' : 'textfield',
0ccaed76 63 '#title' => t('File'),
eabfe21e 64 '#description' => t('Specify a file in the site\'s file system path or upload a file below.'),
b483268b 65 '#default_value' => empty($source_config['source']) ? '' : $source_config['source'],
eabfe21e
AB
66 );
67 $form['upload'] = array(
68 '#type' => 'file',
b483268b
AB
69 '#title' => empty($this->config['direct']) ? t('File') : NULL,
70 '#description' => empty($source_config['source']) ? t('Select the file to be imported from your local system.') : t('Select a different file to be imported from your local system.'),
71 '#theme' => 'feeds_upload',
725e6aeb 72 '#file_info' => empty($source_config['fid']) ? NULL : file_load($source_config['fid']),
dffca3e2 73 '#size' => 10,
0ccaed76
AB
74 );
75 return $form;
76 }
77
78 /**
eabfe21e 79 * Override parent::sourceFormValidate().
0ccaed76 80 */
eabfe21e 81 public function sourceFormValidate(&$values) {
5ae1fd73
AB
82 $feed_dir = 'public://feeds';
83 file_prepare_directory($feed_dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
eabfe21e
AB
84
85 // If there is a file uploaded, save it, otherwise validate input on
86 // file.
725e6aeb 87 // @todo: Track usage of file, remove file when removing source.
94aae911
AB
88 if ($file = file_save_upload('feeds', array('file_validate_extensions' => array(0 => $this->config['allowed_extensions'])), $feed_dir)) {
89 $values['source'] = $file->uri;
725e6aeb 90 $values['file'] = $file;
eabfe21e
AB
91 }
92 elseif (empty($values['source'])) {
93 form_set_error('feeds][source', t('Upload a file first.'));
94 }
073c66a2
AB
95 // If a file has not been uploaded and $values['source'] is not empty, make
96 // sure that this file is within Drupal's files directory as otherwise
97 // potentially any file that the web server has access could be exposed.
94aae911
AB
98 elseif (strpos($values['source'], 'public://') !== 0) {
99 form_set_error('feeds][source', t('File needs to reside within the site\'s file directory, its path needs to start with public://.'));
eabfe21e 100 }
0ccaed76 101 }
b483268b
AB
102
103 /**
725e6aeb
AB
104 * Override parent::sourceSave().
105 */
106 public function sourceSave(FeedsSource $source) {
107 $source_config = $source->getConfigFor($this);
108
109 // If a new file is present, delete the old one and replace it with the new
110 // one.
111 if (isset($source_config['file'])) {
112 $file = $source_config['file'];
113 if (isset($source_config['fid'])) {
114 $this->deleteFile($source_config['fid'], $source->feed_nid);
115 }
116 $file->status = FILE_STATUS_PERMANENT;
117 file_save($file);
118 file_usage_add($file, 'feeds', get_class($this), $source->feed_nid);
119
120 $source_config['fid'] = $file->fid;
121 unset($source_config['file']);
122 $source->setConfigFor($this, $source_config);
123 }
124 }
125
126 /**
127 * Override parent::sourceDelete().
128 */
129 public function sourceDelete(FeedsSource $source) {
130 $source_config = $source->getConfigFor($this);
131 if (isset($source_config['fid'])) {
132 $this->deleteFile($source_config['fid'], $source->feed_nid);
133 }
134 }
135
136 /**
b483268b
AB
137 * Override parent::configDefaults().
138 */
139 public function configDefaults() {
140 return array(
94aae911 141 'allowed_extensions' => 'txt csv xml',
b483268b
AB
142 'direct' => FALSE,
143 );
144 }
145
146 /**
147 * Override parent::configForm().
148 */
149 public function configForm(&$form_state) {
150 $form = array();
94aae911
AB
151 $form['allowed_extensions'] = array(
152 '#type' =>'textfield',
153 '#title' => t('Allowed file extensions'),
154 '#description' => t('Allowed file extensions for upload.'),
155 '#default_value' => $this->config['allowed_extensions'],
156 );
b483268b
AB
157 $form['direct'] = array(
158 '#type' =>'checkbox',
159 '#title' => t('Supply path to file directly'),
160 '#description' => t('For experts. If checked users can specify a path to a file when importing rather than uploading a file. This is useful when files to be imported are already present on server.'),
161 '#default_value' => $this->config['direct'],
162 );
163 return $form;
164 }
725e6aeb
AB
165
166 /**
167 * Helper. Deletes a file.
168 */
169 protected function deleteFile($fid, $feed_nid) {
170 if ($file = file_load($fid)) {
171 file_usage_delete($file, 'feeds', get_class($this), $feed_nid);
172 file_delete($file);
173 }
174 }
5ab19479 175}