1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2016 Joas Schilling <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @license GNU AGPL version 3 or any later version |
6
|
|
|
* |
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU Affero General Public License as |
9
|
|
|
* published by the Free Software Foundation, either version 3 of the |
10
|
|
|
* License, or (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace OCA\WorkflowEngine\Check; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
use OCA\WorkflowEngine\Entity\File; |
26
|
|
|
use OCP\IL10N; |
27
|
|
|
use OCP\IRequest; |
28
|
|
|
use OCP\Util; |
29
|
|
|
use OCP\WorkflowEngine\ICheck; |
30
|
|
|
use OCP\WorkflowEngine\IEntity; |
31
|
|
|
|
32
|
|
|
class FileSize implements ICheck { |
33
|
|
|
|
34
|
|
|
/** @var int */ |
35
|
|
|
protected $size; |
36
|
|
|
|
37
|
|
|
/** @var IL10N */ |
38
|
|
|
protected $l; |
39
|
|
|
|
40
|
|
|
/** @var IRequest */ |
41
|
|
|
protected $request; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param IL10N $l |
45
|
|
|
* @param IRequest $request |
46
|
|
|
*/ |
47
|
|
|
public function __construct(IL10N $l, IRequest $request) { |
48
|
|
|
$this->l = $l; |
49
|
|
|
$this->request = $request; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $operator |
54
|
|
|
* @param string $value |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
public function executeCheck($operator, $value) { |
58
|
|
|
$size = $this->getFileSizeFromHeader(); |
59
|
|
|
|
60
|
|
|
$value = Util::computerFileSize($value); |
61
|
|
|
if ($size !== false) { |
|
|
|
|
62
|
|
|
switch ($operator) { |
63
|
|
|
case 'less': |
64
|
|
|
return $size < $value; |
65
|
|
|
case '!less': |
66
|
|
|
return $size >= $value; |
67
|
|
|
case 'greater': |
68
|
|
|
return $size > $value; |
69
|
|
|
case '!greater': |
70
|
|
|
return $size <= $value; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $operator |
78
|
|
|
* @param string $value |
79
|
|
|
* @throws \UnexpectedValueException |
80
|
|
|
*/ |
81
|
|
|
public function validateCheck($operator, $value) { |
82
|
|
|
if (!in_array($operator, ['less', '!less', 'greater', '!greater'])) { |
83
|
|
|
throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (!preg_match('/^[0-9]+[ ]?[kmgt]?b$/i', $value)) { |
87
|
|
|
throw new \UnexpectedValueException($this->l->t('The given file size is invalid'), 2); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
protected function getFileSizeFromHeader() { |
95
|
|
|
if ($this->size !== null) { |
96
|
|
|
return $this->size; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$size = $this->request->getHeader('OC-Total-Length'); |
100
|
|
|
if ($size === '') { |
101
|
|
|
if (in_array($this->request->getMethod(), ['POST', 'PUT'])) { |
102
|
|
|
$size = $this->request->getHeader('Content-Length'); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if ($size === '') { |
107
|
|
|
$size = false; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$this->size = $size; |
|
|
|
|
111
|
|
|
return $this->size; |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function supportedEntities(): array { |
115
|
|
|
return [ File::class ]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function isAvailableForScope(int $scope): bool { |
119
|
|
|
return true; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|