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 "rename" command |
16
|
|
|
*/ |
17
|
|
|
class Rename extends Element |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Validates an array of submitted data while renaming a file |
22
|
|
|
* @param array $submitted |
23
|
|
|
* @param array $options |
24
|
|
|
* @return boolean|array |
25
|
|
|
*/ |
26
|
|
|
public function validateRename(&$submitted, $options = array()) |
27
|
|
|
{ |
28
|
|
|
$this->options = $options; |
29
|
|
|
$this->submitted = &$submitted; |
30
|
|
|
|
31
|
|
|
$this->validateNameRename(); |
32
|
|
|
$this->validateDestinationRename(); |
33
|
|
|
|
34
|
|
|
return $this->getResult(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Validates a file name |
39
|
|
|
* @return boolean |
40
|
|
|
*/ |
41
|
|
|
protected function validateNameRename() |
42
|
|
|
{ |
43
|
|
|
$files = $this->getSubmitted('files'); |
44
|
|
|
|
45
|
|
|
/* @var $file \SplFileInfo */ |
46
|
|
|
$file = reset($files); |
47
|
|
|
$name = $this->getSubmitted('name'); |
48
|
|
|
|
49
|
|
|
if (strlen($name) == 0) { |
50
|
|
|
$this->setErrorRequired('name', $this->translation->text('Name')); |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$pattern = $file->isFile() ? '/^[\w-.]+$/' : '/^[\w-]+$/'; |
55
|
|
|
|
56
|
|
|
if (preg_match($pattern, $name) !== 1) { |
57
|
|
|
$this->setErrorInvalid('name', $this->translation->text('Name')); |
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return true; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Validates that the destination is unique |
66
|
|
|
* @return boolean |
67
|
|
|
*/ |
68
|
|
|
protected function validateDestinationRename() |
69
|
|
|
{ |
70
|
|
|
if ($this->isError()) { |
71
|
|
|
return null; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$files = $this->getSubmitted('files'); |
75
|
|
|
|
76
|
|
|
/* @var $file \SplFileInfo */ |
77
|
|
|
$file = reset($files); |
78
|
|
|
$name = $this->getSubmitted('name'); |
79
|
|
|
|
80
|
|
|
$directory = dirname($file->getRealPath()); |
81
|
|
|
$destination = gplcart_path_normalize("$directory/$name"); |
82
|
|
|
|
83
|
|
|
if (file_exists($destination)) { |
84
|
|
|
$this->setError('name', $this->translation->text('Destination already exists')); |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$this->setSubmitted('destination', $destination); |
89
|
|
|
return true; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
} |
93
|
|
|
|