-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathModule.php
303 lines (268 loc) · 11.3 KB
/
Module.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
namespace FileSideload;
use FileSideload\Form\ConfigForm;
use Laminas\EventManager\Event;
use Laminas\EventManager\SharedEventManagerInterface;
use Laminas\Mvc\Controller\AbstractController;
use Laminas\ServiceManager\ServiceLocatorInterface;
use Laminas\View\Renderer\PhpRenderer;
use Omeka\Module\AbstractModule;
use Omeka\Stdlib\Message;
class Module extends AbstractModule
{
protected $directory;
protected $deleteFile;
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function uninstall(ServiceLocatorInterface $serviceLocator)
{
$settings = $serviceLocator->get('Omeka\Settings');
$settings->delete('file_sideload_directory');
$settings->delete('file_sideload_delete_file');
$settings->delete('file_sideload_max_files');
$settings->delete('file_sideload_max_directories');
}
public function attachListeners(SharedEventManagerInterface $sharedEventManager): void
{
$sharedEventManager->attach(
\Omeka\Api\Adapter\ItemAdapter::class,
'api.hydrate.pre',
[$this, 'handleItemApiHydratePre']
);
}
public function getConfigForm(PhpRenderer $renderer)
{
$settings = $this->getServiceLocator()->get('Omeka\Settings');
$form = new ConfigForm;
$form->init();
$form->setData([
'directory' => $settings->get('file_sideload_directory'),
'delete_file' => $settings->get('file_sideload_delete_file', 'no'),
'filesideload_max_files' => $settings->get('file_sideload_max_files', 1000),
'filesideload_max_directories' => $settings->get('file_sideload_max_directories', 1000),
]);
return $renderer->formCollection($form, false);
}
public function handleConfigForm(AbstractController $controller)
{
$settings = $this->getServiceLocator()->get('Omeka\Settings');
$form = new ConfigForm;
$form->init();
$form->setData($controller->params()->fromPost());
if (!$form->isValid()) {
$controller->messenger()->addErrors($form->getMessages());
return false;
}
$formData = $form->getData();
$settings->set('file_sideload_directory', $formData['directory']);
$settings->set('file_sideload_delete_file', $formData['delete_file']);
$settings->set('file_sideload_max_files', (int) $formData['filesideload_max_files']);
$settings->set('file_sideload_max_directories', (int) $formData['filesideload_max_directories']);
return true;
}
public function handleItemApiHydratePre(Event $event)
{
static $isChecked;
/** @var \Omeka\Api\Request $request */
$request = $event->getParam('request');
$data = $request->getContent();
if (empty($data['o:media'])) {
return;
}
if (is_null($isChecked)) {
$isChecked = false;
$settings = $this->getServiceLocator()->get('Omeka\Settings');
$mainDir = (string) $settings->get('file_sideload_directory', '');
if (!strlen($mainDir)) {
return;
}
$mainDir = realpath($mainDir);
if ($mainDir === false) {
return;
}
$dir = new \SplFileInfo($mainDir);
if (!$dir->isDir() || !$dir->isReadable() || !$dir->isExecutable()) {
return;
}
$this->directory = $mainDir;
$this->deleteFile = $settings->get('file_sideload_delete_file') === 'yes';
$isChecked = true;
}
if (!$isChecked) {
return;
}
$errorStore = $event->getParam('errorStore');
$newDataMedias = [];
foreach ($data['o:media'] as $dataMedia) {
$newDataMedias[] = $dataMedia;
if (empty($dataMedia['o:ingester']) || $dataMedia['o:ingester'] !== 'sideload_dir') {
continue;
}
if (!array_key_exists('ingest_directory', $dataMedia)) {
$errorStore->addError('ingest_directory', 'No ingest directory specified.'); // @translate
continue;
}
$ingestDirectory = (string) $dataMedia['ingest_directory'];
// Some quick security checks are done here instead of ingester
// to simplify conversion into multiple media.
if (!strlen($ingestDirectory)) {
$errorStore->addError('ingest_directory', 'No ingest directory specified.'); // @translate
continue;
}
if ($ingestDirectory === '.' || $ingestDirectory === '..' || $ingestDirectory === '/') {
$errorStore->addError('ingest_directory', 'Illegal ingest directory specified.'); // @translate
continue;
}
$isAbsolutePathInsideDir = $this->directory && strpos($ingestDirectory, $this->directory) === 0;
$directory = $isAbsolutePathInsideDir
? $ingestDirectory
: $this->directory . DIRECTORY_SEPARATOR . $ingestDirectory;
$fileinfo = new \SplFileInfo($directory);
$directory = $this->verifyFileOrDir($fileinfo, true);
if (is_null($directory)) {
// Set a clearer message in some cases.
if ($this->deleteFile && !$fileinfo->getPathInfo()->isWritable()) {
$errorStore->addError('ingest_directory', new Message(
'Ingest directory "%s" is not writeable but the config requires deletion after upload.', // @translate
$ingestDirectory
));
} elseif (!$fileinfo->isDir()) {
$errorStore->addError('ingest_directory', new Message(
'Invalid ingest directory "%s" specified: not a directory', // @translate
$ingestDirectory
));
} else {
$errorStore->addError('ingest_directory', new Message(
'Invalid ingest directory "%s" specified: incorrect path or insufficient permissions', // @translate
$ingestDirectory
));
}
continue;
}
$listFiles = $this->listFiles($directory, !empty($dataMedia['ingest_directory_recursively']));
if (!count($listFiles)) {
$errorStore->addError('ingest_directory', new Message(
'Ingest directory "%s" is empty.', // @translate
$ingestDirectory
));
continue;
}
// Convert the media to a list of media for the item hydration.
// Remove the added media directory from list of media.
array_pop($newDataMedias);
foreach ($listFiles as $filepath) {
$dataMedia['ingest_filename'] = $filepath;
$newDataMedias[] = $dataMedia;
}
}
$data['o:media'] = $newDataMedias;
$request->setContent($data);
}
/**
* Get all files available to sideload from a directory inside the main dir.
*
* @return array List of filepaths relative to the main directory.
*/
protected function listFiles(string $directory, bool $recursive = false): array
{
$dir = new \SplFileInfo($directory);
if (!$dir->isDir() || !$dir->isReadable() || !$dir->isExecutable()) {
return [];
}
// Check if the dir is inside main directory: don't import root files.
$directory = $this->verifyFileOrDir($dir, true);
if (is_null($directory)) {
return [];
}
$listFiles = [];
// To simplify sort.
$listRootFiles = [];
$lengthDir = strlen($this->directory) + 1;
if ($recursive) {
$dir = new \RecursiveDirectoryIterator($directory);
// Prevent UnexpectedValueException "Permission denied" by excluding
// directories that are not executable or readable.
$dir = new \RecursiveCallbackFilterIterator($dir, function ($current, $key, $iterator) {
if ($iterator->isDir() && (!$iterator->isExecutable() || !$iterator->isReadable())) {
return false;
}
return true;
});
$iterator = new \RecursiveIteratorIterator($dir);
/** @var \SplFileInfo $file */
foreach ($iterator as $filepath => $file) {
if ($this->verifyFileOrDir($file)) {
// For security, don't display the full path to the user.
$relativePath = substr($filepath, $lengthDir);
// Use keys for quicker process on big directories.
$listFiles[$relativePath] = null;
if (pathinfo($filepath, PATHINFO_DIRNAME) === $directory) {
$listRootFiles[$relativePath] = null;
}
}
}
} else {
$iterator = new \DirectoryIterator($directory);
/** @var \DirectoryIterator $file */
foreach ($iterator as $file) {
$filepath = $this->verifyFileOrDir($file);
if (!is_null($filepath)) {
// For security, don't display the full path to the user.
$relativePath = substr($filepath, $lengthDir);
// Use keys for quicker process on big directories.
$listFiles[$relativePath] = null;
}
}
}
// Don't mix directories and files. List root files, then sub-directories.
$listFiles = array_keys($listFiles);
natcasesort($listFiles);
$listRootFiles = array_keys($listRootFiles);
natcasesort($listRootFiles);
return array_values(array_unique(array_merge($listRootFiles, $listFiles)));
}
/**
* Verify the passed file or directory.
*
* Working off the "real" base directory and "real" filepath: both must
* exist and have sufficient permissions; the filepath must begin with the
* base directory path to avoid problems with symlinks; the base directory
* must be server-writable to delete the file; and the file must be a
* readable regular file or directory.
*
* @param \SplFileInfo $fileinfo
* @return string|null The real file path or null if the file is invalid.
*/
protected function verifyFileOrDir(\SplFileInfo $fileinfo, bool $isDir = false): ?string
{
if (false === $this->directory) {
return null;
}
$realPath = $fileinfo->getRealPath();
if (false === $realPath) {
return null;
}
if ($realPath === $this->directory) {
return null;
}
if (0 !== strpos($realPath, $this->directory)) {
return null;
}
if ($this->deleteFile && !$fileinfo->getPathInfo()->isWritable()) {
return null;
}
if (!$fileinfo->isReadable()) {
return null;
}
if ($isDir) {
if (!$fileinfo->isDir() || !$fileinfo->isExecutable()) {
return null;
}
} elseif (!$fileinfo->isFile()) {
return null;
}
return $realPath;
}
}