|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* ownCloud - files_antivirus |
|
4
|
|
|
* |
|
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
|
6
|
|
|
* later. See the COPYING file. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Viktar Dubiniuk <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* @copyright Viktar Dubiniuk 2017-2018 |
|
11
|
|
|
* @license AGPL-3.0 |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace OCA\Files_Antivirus; |
|
15
|
|
|
|
|
16
|
|
|
use OC\Cache\CappedMemoryCache; |
|
17
|
|
|
use OC\Files\Storage\Storage; |
|
18
|
|
|
use \OCP\IRequest; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Used to detect the size of the uploaded file |
|
22
|
|
|
* |
|
23
|
|
|
* @package OCA\Files_Antivirus |
|
24
|
|
|
*/ |
|
25
|
|
|
class RequestHelper { |
|
26
|
|
|
/** |
|
27
|
|
|
* @var IRequest |
|
28
|
|
|
*/ |
|
29
|
|
|
private $request; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var CappedMemoryCache |
|
33
|
|
|
*/ |
|
34
|
|
|
private static $fileSizeCache; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* RequestHelper constructor. |
|
38
|
|
|
* |
|
39
|
|
|
* @param IRequest $request |
|
40
|
|
|
*/ |
|
41
|
5 |
|
public function __construct(IRequest $request) { |
|
42
|
5 |
|
$this->request = $request; |
|
43
|
5 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return CappedMemoryCache |
|
47
|
|
|
*/ |
|
48
|
4 |
|
public function getCache() { |
|
49
|
4 |
|
if (self::$fileSizeCache === null) { |
|
50
|
1 |
|
self::$fileSizeCache = new CappedMemoryCache(); |
|
51
|
|
|
} |
|
52
|
4 |
|
return self::$fileSizeCache; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $path |
|
57
|
|
|
* @param string $size |
|
58
|
|
|
* |
|
59
|
|
|
* @return void |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function setSizeForPath($path, $size) { |
|
62
|
1 |
|
$this->getCache()->set($path, $size); |
|
63
|
1 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get current upload size |
|
67
|
|
|
* returns null for chunks and when there is no upload |
|
68
|
|
|
* |
|
69
|
|
|
* @param Storage $storage |
|
70
|
|
|
* @param string $path |
|
71
|
|
|
* |
|
72
|
|
|
* @return int|null |
|
73
|
|
|
*/ |
|
74
|
4 |
|
public function getUploadSize(Storage $storage, $path) { |
|
75
|
|
|
$requestMethod = $this->request->getMethod(); |
|
76
|
4 |
|
|
|
77
|
4 |
|
// Handle MOVE first |
|
78
|
4 |
|
// the size is cached by the app dav plugin in this case |
|
79
|
4 |
|
if ($requestMethod === 'MOVE') { |
|
80
|
|
|
// remove .ocTransferId444531916.part from part files |
|
81
|
|
|
$cleanedPath = preg_replace( |
|
82
|
4 |
|
'|\.ocTransferId\d+\.part$|', |
|
83
|
4 |
|
'', |
|
84
|
4 |
|
$path |
|
85
|
4 |
|
); |
|
86
|
|
|
// cache uses dav path in /files/$user/path format |
|
87
|
4 |
|
$translatedPath = preg_replace( |
|
88
|
4 |
|
'|^files/|', |
|
89
|
1 |
|
'files/' . $storage->getOwner('/') . '/', |
|
90
|
|
|
$cleanedPath |
|
91
|
|
|
); |
|
92
|
3 |
|
$cachedSize = $this->getCache()->get($translatedPath); |
|
93
|
|
|
if ($cachedSize > 0) { |
|
94
|
3 |
|
return $cachedSize; |
|
95
|
1 |
|
} |
|
96
|
|
|
} |
|
97
|
2 |
|
|
|
98
|
2 |
|
// Are we uploading anything? |
|
99
|
2 |
|
if ($requestMethod !== 'PUT') { |
|
100
|
|
|
return null; |
|
101
|
|
|
} |
|
102
|
|
|
$isRemoteScript = $this->isScriptName('remote.php'); |
|
103
|
2 |
|
$isPublicScript = $this->isScriptName('public.php'); |
|
104
|
|
|
if (!$isRemoteScript && !$isPublicScript) { |
|
105
|
1 |
|
return null; |
|
106
|
1 |
|
} |
|
107
|
|
|
|
|
108
|
|
|
if ($isRemoteScript) { |
|
109
|
|
|
// v1 && v2 Chunks are not scanned |
|
110
|
|
|
if (\strpos($path, 'uploads/') === 0) { |
|
111
|
|
|
return null; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
if (\OC_FileChunking::isWebdavChunk() |
|
115
|
1 |
|
&& \strpos($path, 'cache/') === 0 |
|
116
|
|
|
) { |
|
117
|
1 |
|
return null; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
$uploadSize = (int)$this->request->getHeader('CONTENT_LENGTH'); |
|
121
|
|
|
|
|
122
|
|
|
return $uploadSize; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
2 |
|
* |
|
127
|
2 |
|
* @param string $string |
|
128
|
2 |
|
* |
|
129
|
|
|
* @return bool |
|
130
|
|
|
*/ |
|
131
|
|
|
public function isScriptName($string) { |
|
132
|
|
|
$pattern = \sprintf('|/%s|', \preg_quote($string)); |
|
133
|
|
|
return \preg_match($pattern, $this->request->getScriptName()) === 1; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|