1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package File manager |
5
|
|
|
* @author Iurii Makukh <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2017, Iurii Makukh <[email protected]> |
7
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+ |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace gplcart\modules\file_manager\handlers\validators; |
11
|
|
|
|
12
|
|
|
use gplcart\core\handlers\validator\Element; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Provides methods to validate "create" command |
16
|
|
|
*/ |
17
|
|
|
class Create extends Element |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Validates an array of submitted data while creating a new file |
22
|
|
|
* @param array $submitted |
23
|
|
|
* @param array $options |
24
|
|
|
* @return boolean|array |
25
|
|
|
*/ |
26
|
|
|
public function validateCreate(&$submitted, $options = array()) |
27
|
|
|
{ |
28
|
|
|
$this->options = $options; |
29
|
|
|
$this->submitted = &$submitted; |
30
|
|
|
|
31
|
|
|
$this->validateNameCreate(); |
32
|
|
|
$this->validateDestinationCreate(); |
33
|
|
|
|
34
|
|
|
return $this->getResult(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Validates a file name |
39
|
|
|
* @return boolean |
40
|
|
|
*/ |
41
|
|
|
protected function validateNameCreate() |
42
|
|
|
{ |
43
|
|
|
$name = $this->getSubmitted('name'); |
44
|
|
|
|
45
|
|
|
if (strlen($name) == 0) { |
46
|
|
|
$this->setErrorRequired('name', $this->translation->text('Name')); |
47
|
|
|
return false; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$pathinfo = pathinfo($name); |
51
|
|
|
|
52
|
|
|
if (!empty($pathinfo['extension'])) { |
53
|
|
|
// Validate filename. Allow only alphanumeric chars, underscores, dashes and dots |
54
|
|
|
$name = trim($pathinfo['dirname'], '.'); |
55
|
|
|
if (preg_match('/^[\w-.]+$/', $pathinfo['basename']) !== 1) { |
56
|
|
|
$this->setErrorInvalid('name', $this->translation->text('Name')); |
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// Validate directory path |
62
|
|
|
if ($name && preg_match('/^[\w-]+[\w-\/]*[\w-]+$|^[\w-]$/', $name) !== 1) { |
63
|
|
|
$this->setErrorInvalid('name', $this->translation->text('Name')); |
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Validates that the destination is unique |
72
|
|
|
* @return boolean |
73
|
|
|
*/ |
74
|
|
|
protected function validateDestinationCreate() |
75
|
|
|
{ |
76
|
|
|
if ($this->isError()) { |
77
|
|
|
return null; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$name = $this->getSubmitted('name'); |
81
|
|
|
$files = $this->getSubmitted('files'); |
82
|
|
|
|
83
|
|
|
/* @var $file \SplFileInfo */ |
84
|
|
|
$file = reset($files); |
85
|
|
|
$directory = $file->getRealPath(); |
86
|
|
|
|
87
|
|
|
if (file_exists("$directory/$name")) { |
88
|
|
|
$this->setError('name', $this->translation->text('Destination already exists')); |
89
|
|
|
return false; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$this->setSubmitted('destination', "$directory/$name"); |
93
|
|
|
return true; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|