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\commands; |
11
|
|
|
|
12
|
|
|
use DirectoryIterator; |
13
|
|
|
use gplcart\core\Handler, |
14
|
|
|
gplcart\core\Container; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Base handler class |
18
|
|
|
*/ |
19
|
|
|
class Base extends Handler |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Config class instance |
24
|
|
|
* @var \gplcart\core\Config $config |
25
|
|
|
*/ |
26
|
|
|
protected $config; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Translation UI model class instance |
30
|
|
|
* @var \gplcart\core\models\Translation $translation |
31
|
|
|
*/ |
32
|
|
|
protected $translation; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Command model class instance |
36
|
|
|
* @var \gplcart\modules\file_manager\models\Command $command |
37
|
|
|
*/ |
38
|
|
|
protected $command; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Scanner model class instance |
42
|
|
|
* @var \gplcart\modules\file_manager\models\Scanner $scanner |
43
|
|
|
*/ |
44
|
|
|
protected $scanner; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Constructor |
48
|
|
|
*/ |
49
|
|
|
public function __construct() |
50
|
|
|
{ |
51
|
|
|
$this->config = Container::get('gplcart\\core\\Config'); |
52
|
|
|
$this->translation = Container::get('gplcart\\core\\models\\Translation'); |
53
|
|
|
$this->command = Container::get('gplcart\\modules\\file_manager\\models\\Command'); |
54
|
|
|
$this->scanner = Container::get('gplcart\\modules\\file_manager\\models\\Scanner'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Sets a property |
59
|
|
|
* @param string $name |
60
|
|
|
* @param mixed $value |
61
|
|
|
*/ |
62
|
|
|
public function setProperty($name, $value) |
63
|
|
|
{ |
64
|
|
|
$this->{$name} = $value; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Returns a total number of scanned files |
69
|
|
|
* @param string $directory |
70
|
|
|
* @param array $options |
71
|
|
|
* @return integer |
72
|
|
|
*/ |
73
|
|
|
protected function getTotal($directory, array $options = array()) |
74
|
|
|
{ |
75
|
|
|
$options['count'] = true; |
76
|
|
|
return (int) $this->scanner->scan($directory, $options); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns an array of scanned files |
81
|
|
|
* @param string $directory |
82
|
|
|
* @param array $options |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
protected function getFiles($directory, array $options = array()) |
86
|
|
|
{ |
87
|
|
|
return (array) $this->scanner->scan($directory, $options); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns a normalized relative path for the file |
92
|
|
|
* @param string $file |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
protected function getRelativeFilePath($file) |
96
|
|
|
{ |
97
|
|
|
return gplcart_path_normalize(gplcart_file_relative($file)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Returns a relative file path or initial directory |
102
|
|
|
* @param null|string $path |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
protected function getRelativePath($path = null) |
106
|
|
|
{ |
107
|
|
|
if (!isset($path)) { |
108
|
|
|
$path = $this->scanner->getInitialPath(true); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return gplcart_path_normalize(gplcart_path_relative($path)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns a relative directory path for the file |
116
|
|
|
* @param string $file |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
|
|
protected function getRelativeDirectory($file) |
120
|
|
|
{ |
121
|
|
|
return trim(dirname($this->getRelativeFilePath($file)), '.'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Moves a file to a new destination |
126
|
|
|
* @param string $src |
127
|
|
|
* @param string $dest |
128
|
|
|
* @param int $errors |
129
|
|
|
* @param int $success |
130
|
|
|
* @return bool |
131
|
|
|
*/ |
132
|
|
|
protected function move($src, $dest, &$errors = 0, &$success = 0) |
133
|
|
|
{ |
134
|
|
|
$this->copy($src, $dest, $errors, $success); |
135
|
|
|
|
136
|
|
|
if (empty($errors)) { |
137
|
|
|
gplcart_file_delete_recursive($src, $errors); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return empty($errors); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Copy a file / directory |
145
|
|
|
* @param string $src |
146
|
|
|
* @param string $dest |
147
|
|
|
* @param int $errors |
148
|
|
|
* @param int $success |
149
|
|
|
* @return boolean |
150
|
|
|
*/ |
151
|
|
|
protected function copy($src, $dest, &$errors = 0, &$success = 0) |
152
|
|
|
{ |
153
|
|
|
if (is_file($src)) { |
154
|
|
|
if (copy($src, $dest)) { |
155
|
|
|
$success++; |
156
|
|
|
return true; |
157
|
|
|
} |
158
|
|
|
$errors++; |
159
|
|
|
return false; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
if (!is_dir($dest) && !mkdir($dest)) { |
163
|
|
|
$errors++; |
164
|
|
|
return false; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
foreach (new DirectoryIterator($src) as $file) { |
168
|
|
|
|
169
|
|
|
$result = null; |
170
|
|
|
$copyto = "$dest/" . $file->getBasename(); |
171
|
|
|
if ($file->isFile() || (!$file->isDot() && $file->isDir())) { |
172
|
|
|
$result = $this->copy($file->getRealPath(), $copyto, $errors, $success); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
if (!isset($result)) { |
176
|
|
|
continue; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
if ($result) { |
180
|
|
|
$success++; |
181
|
|
|
} else { |
182
|
|
|
$errors++; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$success++; |
187
|
|
|
return true; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Whether the current file is the initial file manager path |
192
|
|
|
* @param \SplFileInfo $file |
193
|
|
|
* @return bool |
194
|
|
|
*/ |
195
|
|
|
protected function isInitialPath($file) |
196
|
|
|
{ |
197
|
|
|
$current_path = gplcart_path_normalize($file->getRealPath()); |
198
|
|
|
$initial_path = gplcart_path_normalize($this->scanner->getInitialPath(true)); |
199
|
|
|
return $current_path === $initial_path; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
} |
203
|
|
|
|