-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFileManagerController.php
More file actions
281 lines (228 loc) · 9.26 KB
/
Copy pathFileManagerController.php
File metadata and controls
281 lines (228 loc) · 9.26 KB
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
<?php
namespace BitApps\FM\Http\Controllers;
use BitApps\FM\Config;
use BitApps\FM\Exception\PreCommandException;
use function BitApps\FM\Functions\fileSystemAdapter;
use BitApps\FM\Plugin;
use BitApps\FM\Providers\FileManager\FileManagerProvider;
use BitApps\FM\Providers\FileManager\FileRoot;
use BitApps\FM\Providers\FileManager\Options;
use BitApps\FM\Vendor\BitApps\WPKit\Utils\Capabilities;
use Exception;
final class FileManagerController
{
/**
* File Manager connector function
*
* @throws Exception
*/
public function connector()
{
try {
Plugin::instance()->accessControl()->checkPermission(sanitize_key($_REQUEST['cmd']));
$finderProvider = new FileManagerProvider($this->getFinderOptions());
$finderProvider->getFinder()->run();
} catch (Exception $th) {
// phpcs:ignore
echo wp_json_encode(['error' => $th->getMessage()]);
}
wp_die();
}
public function getFinderOptions()
{
$finderOptions = new Options(is_user_logged_in() && \defined('WP_DEBUG') && WP_DEBUG);
$finderOptions->setBind(
'put.pre',
[
Plugin::instance()->fileEditValidator(),
'validate',
]
);
$finderOptions->setBind(
'get.pre file.pre archive.pre back.pre chmod.pre colwidth.pre copy.pre cut.pre duplicate.pre editor.pre
extract.pre forward.pre fullscreen.pre getfile.pre help.pre home.pre info.pre mkdir.pre mkfile.pre
netmount.pre netunmount.pre open.pre opendir.pre paste.pre places.pre quicklook.pre reload.pre
rename.pre resize.pre restore.pre rm.pre search.pre sort.pre up.pre upload.pre view.pre zipdl.pre
tree.pre parents.pre ls.pre tmb.pre size.pre dim.pre',
[
Plugin::instance()->accessControl(),
'checkPermission',
]
);
$finderOptions->setBind(
'upload',
[Plugin::instance()->mediaSyncs(), 'onFileUpload']
);
// 'zipdl.pre file.pre rename.pre put.pre upload.pre',
$finderOptions->setBind(
'zipdl.pre file.pre rename.pre put.pre rm.pre chmod.pre mkdir.pre mkfile.pre extract.pre',
[Plugin::instance()->logger(), 'log']
);
$finderOptions->setBind(
'upload',
[Plugin::instance()->logger(), 'logUpload']
);
$allVolumes = $this->getFileRoots();
$volumeCount = \count($allVolumes);
$invalidVolumeCount = 0;
foreach ($allVolumes as $root) {
if (!$root->isReadable()) {
$invalidVolumeCount++;
continue;
}
$finderOptions->setRoot($root);
}
if ($volumeCount === $invalidVolumeCount) {
throw new PreCommandException(esc_html__('There is no readable volume. Please select an readable folder from settings', 'file-manager'));
}
return $finderOptions;
}
public function getFileRoots()
{
if (!is_user_logged_in()) {
return $this->guestVolume();
} elseif (is_user_logged_in() && Plugin::instance()->permissions()->isRequestForAdminArea() && Plugin::instance()->permissions()->isDisabledForAdmin()) {
return $this->getDashboardVolumes();
}
return $this->getUserVolumes();
}
public function getUrlByPath($path)
{
return home_url(str_replace(ABSPATH, '', trailingslashit($path)));
}
/**
* Sets allowed mimetype for a volume/root
*
* @return void
*/
public function setAllowedFileType(FileRoot $volume)
{
$permissions = Plugin::instance()->permissions();
$mimes = $permissions->getEnabledFileType();
$maxUploadSize = $permissions->getMaximumUploadSize();
$volume->setUploadMaxSize($maxUploadSize == 0 ? 0 : $maxUploadSize . 'M');
$denyUploadType = array_diff(Plugin::instance()->mimes()->getTypes(), $mimes);
$isTextLikeEnabled = false; // is text like php,javascript, css is enabled or exists in $mimes then true else false
if (!\in_array('php', $mimes)) {
$denyUploadType[] = 'text/x-php';
} else {
$mimes[] = 'text/x-php';
$isTextLikeEnabled = true;
}
if (!\in_array('javascript', $mimes)) {
$denyUploadType[] = 'text/javascript';
} else {
$mimes[] = 'text/javascript';
$isTextLikeEnabled = true;
}
if (!\in_array('css', $mimes)) {
$denyUploadType[] = 'text/css';
} else {
$mimes[] = 'text/css';
$isTextLikeEnabled = true;
}
$allowedMimes = array_diff($mimes, $denyUploadType);
if ($isTextLikeEnabled && !\in_array('text', $allowedMimes)) {
$allowedMimes[] = 'text';
$denyUploadType = array_diff($denyUploadType, ['text']);
}
$volume->setUploadOrder(['allow', 'deny']);
$volume->setOption('uploadDeny', $denyUploadType);
$volume->setUploadAllow($allowedMimes);
}
private function getDashboardVolumes()
{
$mimes = Plugin::instance()->mimes()->getTypes();
$preferences = Plugin::instance()->preferences();
$accessControlProvider = Plugin::instance()->accessControl();
$permissions = Plugin::instance()->permissions();
$baseRoot = new FileRoot(
$preferences->getRootPath(),
$preferences->getRootUrl(),
$preferences->getRootVolumeName()
);
$baseRoot->setUploadAllow($mimes);
if ($permissions->currentUserRole() !== 'administrator') {
$this->setAllowedFileType($baseRoot);
}
if (fileSystemAdapter()->is_writable(stripslashes($preferences->getRootPath()) . DIRECTORY_SEPARATOR . '.tmbPath')) {
$baseRoot->setOption('tmbPath', '.tmb');
}
$baseRoot->setAccessControl([$accessControlProvider, 'control']);
$baseRoot->setAcceptedName([$accessControlProvider, 'validateName']);
$baseRoot->setDisabled([]);
$baseRoot->setWinHashFix(DIRECTORY_SEPARATOR !== '/');
if (Capabilities::filter(Config::VAR_PREFIX . 'user_can_manage_options')) {
$baseRoot->setAllowChmodReadOnly(true);
$baseRoot->setStatOwner(true);
$baseRoot->setUploadMaxSize(0);
}
$roots[] = $baseRoot;
return $roots;
}
private function getUserVolumes()
{
$permissions = Plugin::instance()->permissions();
$roots[] = $this->processFileRoot(
$permissions->getPathByFolderOption(),
'Public',
$this->getUrlByPath($permissions->getPathByFolderOption())
);
$permissionByRole = $permissions->getByRole($permissions->currentUserRole());
$roots[] = $this->processFileRoot(
$permissionByRole['path'],
$permissions->currentUserRole(),
$this->getUrlByPath($permissionByRole['path'])
);
$permissionByUser = $permissions->getByUser($permissions->currentUserID());
$roots[] = $this->processFileRoot(
$permissionByUser['path'],
$permissions->currentUser()->display_name,
$this->getUrlByPath($permissionByUser['path'])
);
return $roots;
}
private function guestVolume()
{
$permissions = Plugin::instance()->permissions();
$guestPermission = $permissions->getGuestPermissions();
$root = new FileRoot(
$guestPermission['path'],
$this->getUrlByPath($guestPermission['path']),
\array_key_exists('alias', $guestPermission)
? $guestPermission['alias'] : basename($guestPermission['path'])
);
$root->setDisabled(array_diff($permissions->allCommands(), $guestPermission['commands']));
return [$root];
}
/**
* Create Instance of FileRoot
*
* @param string $path
* @param string $alias
* @param string $url
*
* @return FileRoot
*/
private function processFileRoot($path, $alias, $url)
{
$permissions = Plugin::instance()->permissions();
$accessControlProvider = Plugin::instance()->accessControl();
$volume = new FileRoot(
$path,
Plugin::instance()->permissions()->currentUserCanRun('download') ? $url : '', // If a URL is provided, the file will be downloaded regardless of whether the download feature is disabled or not.
$alias
);
$this->setAllowedFileType($volume);
$volume->setAccessControl([$accessControlProvider, 'control']);
$volume->setAcceptedName([$accessControlProvider, 'validateName']);
$volume->setDisabled($permissions->getDisabledCommand());
$volume->setWinHashFix(DIRECTORY_SEPARATOR !== '/');
if (Capabilities::filter(Config::VAR_PREFIX . 'user_can_manage_options')) {
$volume->setAllowChmodReadOnly(true);
$volume->setStatOwner(true);
$volume->setUploadMaxSize(0);
}
return $volume;
}
}