Skip to content

Commit 5f082dd

Browse files
committed
fix: upload is not logging
1 parent 26523d6 commit 5f082dd

File tree

11 files changed

+145
-60
lines changed

11 files changed

+145
-60
lines changed

backend/app/Config.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ class Config
2121

2222
const VAR_PREFIX = 'bit_fm_';
2323

24-
const VERSION = '6.8';
24+
const VERSION = '6.8.1';
2525

26-
const VERSION_ID = 680;
26+
const VERSION_ID = 681;
2727

2828
const DB_VERSION = '1.0';
2929

backend/app/Http/Controllers/FileManagerController.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use BitApps\FM\Providers\FileManager\FileRoot;
1010
use BitApps\FM\Providers\FileManager\Options;
1111
use BitApps\FM\Vendor\BitApps\WPKit\Utils\Capabilities;
12+
use Error;
1213
use Exception;
1314

1415
final class FileManagerController
@@ -64,9 +65,14 @@ public function getFinderOptions()
6465
// 'zipdl.pre file.pre rename.pre put.pre upload.pre',
6566

6667
$finderOptions->setBind(
67-
'zipdl.pre file.pre rename.pre put.pre upload.pre rm.pre chmod.pre mkdir.pre mkfile.pre extract.pre',
68+
'zipdl.pre file.pre rename.pre put.pre rm.pre chmod.pre mkdir.pre mkfile.pre extract.pre',
6869
[Plugin::instance()->logger(), 'log']
6970
);
71+
72+
$finderOptions->setBind(
73+
'upload',
74+
[Plugin::instance()->logger(), 'logUpload']
75+
);
7076

7177
$allVolumes = $this->getFileRoots();
7278
$volumeCount = \count($allVolumes);

backend/app/Providers/AccessControlProvider.php

Lines changed: 64 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ class AccessControlProvider
1313
{
1414
public $settings;
1515

16+
private $maliciousPatterns = [
17+
'/<script.*?>.*?<\/script>/is',
18+
'/onload=["\'].*?["\']/is',
19+
'/<.*?javascript:.*?>/is',
20+
'/<.*?on\w+=[^>]+>/is',
21+
'/\/S \/JavaScript \/JS /is',
22+
];
23+
private $scannedResult = [];
24+
1625
public function __construct()
1726
{
1827
$this->settings = Plugin::instance()->preferences();
@@ -165,54 +174,78 @@ public function scanFile($command, $args)
165174
if (!\in_array($command, ['put', 'upload']) || \in_array('javascript', Plugin::instance()->permissions()->getEnabledFileType())) {
166175
return;
167176
}
168-
$content = '';
169177

170-
if ($command === 'upload' && isset($args[0]['FILES']['upload']['tmp_name'])) {
178+
if (isset($args[0]['chunk']) && !empty($args[0]['chunk'])) {
179+
return;
180+
}
181+
182+
if (
183+
$command === 'upload' &&
184+
!empty($args[0]['FILES']['upload']['tmp_name']) &&
185+
is_array($args[0]['FILES']['upload']['tmp_name'])
186+
) {
171187
$filePath = '';
172188
$fileName = '';
173-
$filePath = $args[0]['FILES']['upload']['tmp_name'][0];
174-
$fileName = $args[0]['FILES']['upload']['name'][0];
175-
$fileTypeAndExt = wp_check_filetype_and_ext($filePath, $fileName, $args[0]['FILES']['upload']['type'][0]);
176-
177-
if (
178-
isset($fileTypeAndExt['ext'], $fileTypeAndExt['type'])
179-
&& (strpos($fileTypeAndExt['type'], 'text') !== false || strpos($fileTypeAndExt['type'], 'pdf') !== false)
180-
|| current_user_can('administrator')
181-
) {
182-
$content = file_get_contents($filePath);
183-
} else {
184-
throw new PreCommandException(__('Failed to process the file', 'file-manager'));
189+
$uploadedFiles = $args[0]['FILES']['upload']['tmp_name'];
190+
error_log(print_r($args[0]['FILES']['upload'], true));
191+
foreach ($uploadedFiles as $index => $tmpName) {
192+
$content = '';
193+
$filePath = $args[0]['FILES']['upload']['tmp_name'][$index];
194+
$fileName = $args[0]['FILES']['upload']['name'][$index];
195+
if (empty($filePath)) {
196+
continue;
197+
}
198+
$fileTypeAndExt = wp_check_filetype_and_ext($filePath, $fileName);
199+
error_log(print_r($fileTypeAndExt, true));
200+
if (!empty($fileTypeAndExt['type'])) {
201+
if (stripos($fileTypeAndExt['type'], 'javascript') !== false) {
202+
$this->scannedResult[] = sprintf(__('This file %s type is not allowed', 'file-manager'), $fileName);
203+
}
204+
if (
205+
stripos($fileTypeAndExt['type'], 'text') !== false ||
206+
stripos($fileTypeAndExt['type'], 'pdf') !== false
207+
) {
208+
$content = file_get_contents($filePath);
209+
}
210+
} else {
211+
try {
212+
$content = file_get_contents($filePath);
213+
} catch (\Exception $e) {
214+
$this->scannedResult[] = sprintf(__('Failed to process this file %s', 'file-manager'), $fileName);
215+
}
216+
}
217+
218+
if (!empty($content)) {
219+
$this->scanForPattern($content, $fileName);
220+
}
185221
}
186222
} elseif (isset($_REQUEST['content'])) {
187-
$content = $_REQUEST['content'];
223+
$this->scanForPattern($_REQUEST['content'], '');
188224
}
189-
if (empty($content)) {
190-
return;
225+
226+
if (count($this->scannedResult) > 0) {
227+
throw new PreCommandException(
228+
implode('. >> ', $this->scannedResult)
229+
);
191230
}
192231

193-
$containsJs = false;
232+
}
194233

195-
$maliciousPatterns = [
196-
'/<script.*?>.*?<\/script>/is',
197-
'/onload=["\'].*?["\']/is',
198-
'/<.*?javascript:.*?>/is',
199-
'/<.*?on\w+=[^>]+>/is',
200-
'/\/S \/JavaScript \/JS /is',
201-
];
202-
203-
foreach ($maliciousPatterns as $pattern) {
234+
private function scanForPattern($content, $fileName)
235+
{
236+
$containsJs = false;
237+
foreach ($this->maliciousPatterns as $pattern) {
204238
if (preg_match($pattern, $content)) {
205239
$containsJs = true;
206240

207241
break;
208242
}
209243
}
210244

211-
212-
213245
if ($containsJs) {
214-
215-
throw new PreCommandException(__('The file contains JS code. Please remove the code and try again. Or allow js mimetype', 'file-manager'));
246+
$this->scannedResult[] = sprintf(__('This file %s contains JS code. Please remove the code and try again. Or allow js mimetype', 'file-manager'), $fileName);
216247
}
217248
}
218249
}
250+
251+

backend/app/Providers/Logger.php

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,21 @@ public function log($command, $target, $finder, $volume)
4646
* update: put
4747
*/
4848
$commandDetails = [];
49-
if ($command === 'upload') {
49+
$commandDetails = $this->processFileHash($command, $target, $volume);
50+
51+
if (isset($commandDetails['files'])) {
52+
$this->_logger->save($command, $commandDetails);
53+
}
54+
}
55+
56+
public function logUpload($command, $status, $target, $finder, $volume)
57+
{
58+
$commandDetails = [];
5059
$commandDetails = $this->processFileHashForUpload($target, $volume);
51-
} else {
52-
$commandDetails = $this->processFileHash($command, $target, $volume);
60+
61+
if (isset($commandDetails['files'])) {
62+
$this->_logger->save($command, $commandDetails);
5363
}
54-
55-
$this->_logger->save($command, $commandDetails);
5664
}
5765

5866
/**
@@ -85,15 +93,26 @@ private function processFileHash($command, $target, $volume)
8593
return $details;
8694
}
8795

96+
/**
97+
* Process targeted file hash to path for upload command
98+
*
99+
* @param array $target
100+
* @param elFinderVolumeDriver | elFinderVolumeLocalFileSystem $volume
101+
*
102+
* @return array
103+
*/
88104
private function processFileHashForUpload($target, $volume)
89105
{
90-
$details = [];
106+
if(!empty($target['chunk'])) {
107+
return [];
108+
}
109+
$details['driver'] = \get_class($volume);
110+
$details['folder'] = [
111+
'path' => str_replace(ABSPATH, '', $volume->getPath($target['target'])),
112+
'hash' => $target['target'],
113+
];
114+
91115
if (!empty($target['upload_path'])) {
92-
$details['driver'] = \get_class($volume);
93-
$details['folder'] = [
94-
'path' => str_replace(ABSPATH, '', $volume->getPath($target['target'])),
95-
'hash' => $target['target'],
96-
];
97116
foreach ($target['upload_path'] as $index => $file) {
98117
$details['files'][] = [
99118
'path' => str_replace(ABSPATH, '', $volume->getPath($file)),
@@ -103,6 +122,19 @@ private function processFileHashForUpload($target, $volume)
103122
break;
104123
}
105124
}
125+
} else if (isset($target["FILES"]["upload"]["full_path"])) {
126+
$uploadBase = $details['folder']['path'];
127+
$files = $target["FILES"]["upload"]["full_path"];
128+
foreach ($files as $index => $file) {
129+
if ($index > 300 || $file === 'blob') {
130+
break;
131+
}
132+
$details['files'][] = [
133+
'path' => $uploadBase . DIRECTORY_SEPARATOR . $file,
134+
'hash' => '',
135+
];
136+
}
137+
106138
}
107139

108140
return $details;

file-manager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Plugin URI: https://bitapps.pro/bit-file-manager
66
* Author: File Manager by Bit Form Team
77
* Author URI: https://bitapps.pro
8-
* Version: 6.8
8+
* Version: 6.8.1
99
* Requires at least: 5.0
1010
* Requires PHP: 7.4
1111
* Text domain: file-manager

frontend/src/pages/Permissions/ui/Permissions.tsx

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ function Permissions() {
214214
<Button loading={isUserPermissionDeleting && delInProgressId === user.ID}>
215215
<DeleteFilled />
216216
</Button>
217-
placeholder=" placeholder=" placeholder=" placeholder=" placeholder=" placeholder="
218217
</Popconfirm>
219218
}
220219
>
@@ -248,16 +247,10 @@ function Permissions() {
248247
<Form.Item
249248
name={['guest', 'path']}
250249
label={__('Path')}
251-
/* rules={[">">
252-
">
253-
">
254-
">
255-
">
256-
{
250+
/* rules={[
257251
// eslint-disable-next-line no-useless-escape
258252
pattern: new RegExp(`^${wpRoot}?(?:\/[^\/]+)*\/?$`),
259253
message: __('Folder Path Must be within WordPress root directory')
260-
}
261254
]} */
262255
>
263256
<Input placeholder={__('Root Folder Path')} />

frontend/src/pages/root/Root.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ export default function Root() {
7575
setIsOpening(false)
7676
})
7777

78+
finder.bind('uploadfail', () => {
79+
finder.toast({
80+
mode: 'error',
81+
msg: __('Something went wrong while uploading files.'),
82+
hideDuration: 5000
83+
})
84+
})
85+
7886
finder.bind('viewchange', () => {
7987
changeViewState(finder)
8088
})

frontend/src/types/finder.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,14 @@ declare module 'elfinder' {
2929
addCommand(commandName: string, commandOptions?: CommandOptions): void
3030
removeCommand(commandName: string): void
3131
exec(cmd: string, files?: Array<File> | string, opts?, dstHash?): void
32+
toast({
33+
mode,
34+
msg,
35+
hideDuration
36+
}: {
37+
mode: 'error' | 'warnning' | 'success'
38+
msg: string
39+
hideDuration: number
40+
}): void
3241
}
3342
}

libs/elFinder/php/elFinder.class.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ public function __construct($opts)
606606
$this->version = (string)self::$ApiVersion;
607607

608608
// set error handler of WARNING, NOTICE
609-
$errLevel = E_WARNING | E_NOTICE | E_USER_WARNING | E_USER_NOTICE | E_STRICT | E_RECOVERABLE_ERROR;
609+
$errLevel = E_WARNING | E_NOTICE | E_USER_WARNING | E_USER_NOTICE | E_RECOVERABLE_ERROR;
610610
if (defined('E_DEPRECATED')) {
611611
$errLevel |= E_DEPRECATED | E_USER_DEPRECATED;
612612
}
@@ -3528,7 +3528,7 @@ protected function upload($args)
35283528
}
35293529
if (!$_target || ($file = $volume->upload($fp, $_target, $name, $tmpname, ($_target === $target) ? $hashes : array())) === false) {
35303530
$errors = array_merge($errors, $this->error(self::ERROR_UPLOAD_FILE, $name, $volume->error()));
3531-
fclose($fp);
3531+
is_resource($fp) && fclose($fp);
35323532
if (!is_uploaded_file($tmpname) && unlink($tmpname)) {
35333533
unset($GLOBALS['elFinderTempFiles'][$tmpname]);
35343534
}
@@ -4467,7 +4467,7 @@ protected function itemLock($hashes, $autoUnlock = true)
44674467
foreach ($hashes as $hash) {
44684468
$lock = elFinder::$commonTempPath . DIRECTORY_SEPARATOR . self::filenameDecontaminate($hash) . '.lock';
44694469
if ($this->itemLocked($hash)) {
4470-
$cnt = file_get_contents($lock) + 1;
4470+
$cnt = (int) file_get_contents($lock) + 1;
44714471
} else {
44724472
$cnt = 1;
44734473
}
@@ -4492,7 +4492,7 @@ protected function itemUnlock($hash)
44924492
return true;
44934493
}
44944494
$lock = elFinder::$commonTempPath . DIRECTORY_SEPARATOR . $hash . '.lock';
4495-
$cnt = file_get_contents($lock);
4495+
$cnt = (int)file_get_contents($lock);
44964496
if (--$cnt < 1) {
44974497
unlink($lock);
44984498
return true;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "file-manager",
3-
"version": "6.8",
3+
"version": "6.8.1",
44
"description": "Manage your file the way you like. You can upload, delete, copy, move, rename, compress, extract files. You don't need to worry about ftp. It is really simple and easy to use.",
55
"author": "Bit Apps",
66
"license": "GPL-2.0-or-later",

0 commit comments

Comments
 (0)