|
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
|
|
|
use gplcart\modules\file_manager\models\Scanner; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Provides methods to validate "copy" command |
|
17
|
|
|
*/ |
|
18
|
|
|
class Copy extends Element |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Scanner model class instance |
|
23
|
|
|
* @var \gplcart\modules\file_manager\models\Scanner $scanner |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $scanner; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Copy constructor. |
|
29
|
|
|
* @param Scanner $scanner |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(Scanner $scanner) |
|
32
|
|
|
{ |
|
33
|
|
|
parent::__construct(); |
|
34
|
|
|
|
|
35
|
|
|
$this->scanner = $scanner; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Validates an array of submitted command data |
|
40
|
|
|
* @param array $submitted |
|
41
|
|
|
* @param array $options |
|
42
|
|
|
* @return boolean|array |
|
43
|
|
|
*/ |
|
44
|
|
|
public function validateCopy(array &$submitted, array $options = array()) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->options = $options; |
|
47
|
|
|
$this->submitted = &$submitted; |
|
48
|
|
|
|
|
49
|
|
|
$this->validateDestinationNameCopy(); |
|
50
|
|
|
$this->validateDestinationDirectoryCopy(); |
|
51
|
|
|
$this->validateDestinationExistanceCopy(); |
|
52
|
|
|
|
|
53
|
|
|
return $this->getResult(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Validates a directory name |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function validateDestinationNameCopy() |
|
60
|
|
|
{ |
|
61
|
|
|
$destination = $this->getSubmitted('destination'); |
|
62
|
|
|
|
|
63
|
|
|
if ($destination !== '' && preg_match('/^[\w-]+[\w-\/]*[\w-]+$|^[\w-]$/', $destination) !== 1) { |
|
64
|
|
|
$this->setErrorInvalid('destination', $this->translation->text('Destination')); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Validates a destination directory |
|
70
|
|
|
* @return bool |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function validateDestinationDirectoryCopy() |
|
73
|
|
|
{ |
|
74
|
|
|
if ($this->isError()) { |
|
75
|
|
|
return null; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$initial_path = $this->scanner->getInitialPath(true); |
|
79
|
|
|
$directory = gplcart_path_normalize("$initial_path/" . $this->getSubmitted('destination')); |
|
80
|
|
|
|
|
81
|
|
|
if (!file_exists($directory) && !mkdir($directory, 0777, true)) { |
|
82
|
|
|
$this->setError('destination', $this->translation->text('Destination does not exist')); |
|
83
|
|
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if (!is_dir($directory)) { |
|
87
|
|
|
$this->setError('destination', $this->translation->text('Destination is not a directory')); |
|
88
|
|
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if (!is_writable($directory)) { |
|
92
|
|
|
$this->setError('destination', $this->translation->text('Directory is not writable')); |
|
93
|
|
|
return false; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$this->setSubmitted('directory', $directory); |
|
97
|
|
|
return true; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Validates file destinations |
|
102
|
|
|
* @return boolean |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function validateDestinationExistanceCopy() |
|
105
|
|
|
{ |
|
106
|
|
|
if ($this->isError()) { |
|
107
|
|
|
return null; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$destinations = array(); |
|
111
|
|
|
$directory = $this->getSubmitted('directory'); |
|
112
|
|
|
|
|
113
|
|
|
$error = null; |
|
114
|
|
|
foreach ((array) $this->getSubmitted('files') as $index => $file) { |
|
115
|
|
|
|
|
116
|
|
|
/* @var $file \SplFileInfo */ |
|
117
|
|
|
if ($file->isDir() && gplcart_path_normalize($file->getRealPath()) === $directory) { |
|
118
|
|
|
$error = $this->translation->text('Destination already exists'); |
|
119
|
|
|
continue; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$destination = "$directory/" . $file->getBasename(); |
|
123
|
|
|
|
|
124
|
|
|
if (file_exists($destination)) { |
|
125
|
|
|
$error = $this->translation->text('Destination already exists'); |
|
126
|
|
|
continue; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
$destinations[$index] = $destination; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
if (isset($error)) { |
|
133
|
|
|
$this->setError('destination', $error); |
|
134
|
|
|
} else { |
|
135
|
|
|
$this->setSubmitted('destinations', $destinations); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return true; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
|