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 OCP\Files\Storage\IStorage; |
26
|
|
|
use OCP\IL10N; |
27
|
|
|
use OCP\IRequest; |
28
|
|
|
use OCP\WorkflowEngine\ICheck; |
29
|
|
|
|
30
|
|
|
class RequestRemoteAddress implements ICheck { |
31
|
|
|
|
32
|
|
|
/** @var IL10N */ |
33
|
|
|
protected $l; |
34
|
|
|
|
35
|
|
|
/** @var IRequest */ |
36
|
|
|
protected $request; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param IL10N $l |
40
|
|
|
* @param IRequest $request |
41
|
|
|
*/ |
42
|
|
|
public function __construct(IL10N $l, IRequest $request) { |
43
|
|
|
$this->l = $l; |
44
|
|
|
$this->request = $request; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param IStorage $storage |
49
|
|
|
* @param string $path |
50
|
|
|
*/ |
51
|
|
|
public function setFileInfo(IStorage $storage, $path) { |
52
|
|
|
// A different path doesn't change time, so nothing to do here. |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $operator |
57
|
|
|
* @param string $value |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
public function executeCheck($operator, $value) { |
61
|
|
|
$actualValue = $this->request->getRemoteAddress(); |
62
|
|
|
$decodedValue = explode('/', $value); |
63
|
|
|
|
64
|
|
|
if ($operator === 'matchesIPv4') { |
65
|
|
|
return $this->matchIPv4($actualValue, $decodedValue[0], $decodedValue[1]); |
66
|
|
|
} else if ($operator === '!matchesIPv4') { |
67
|
|
|
return !$this->matchIPv4($actualValue, $decodedValue[0], $decodedValue[1]); |
68
|
|
|
} else if ($operator === 'matchesIPv6') { |
69
|
|
|
return $this->matchIPv6($actualValue, $decodedValue[0], $decodedValue[1]); |
70
|
|
|
} else { |
71
|
|
|
return !$this->matchIPv6($actualValue, $decodedValue[0], $decodedValue[1]); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $operator |
77
|
|
|
* @param string $value |
78
|
|
|
* @throws \UnexpectedValueException |
79
|
|
|
*/ |
80
|
|
|
public function validateCheck($operator, $value) { |
81
|
|
|
if (!in_array($operator, ['matchesIPv4', '!matchesIPv4', 'matchesIPv6', '!matchesIPv6'])) { |
82
|
|
|
throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$decodedValue = explode('/', $value); |
86
|
|
|
if (sizeof($decodedValue) !== 2) { |
87
|
|
|
throw new \UnexpectedValueException($this->l->t('The given IP range is invalid'), 2); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (in_array($operator, ['matchesIPv4', '!matchesIPv4'])) { |
91
|
|
View Code Duplication |
if (!filter_var($decodedValue[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
|
|
|
|
92
|
|
|
throw new \UnexpectedValueException($this->l->t('The given IP range is not valid for IPv4'), 3); |
93
|
|
|
} |
94
|
|
View Code Duplication |
if ($decodedValue[1] > 32 || $decodedValue[1] <= 0) { |
|
|
|
|
95
|
|
|
throw new \UnexpectedValueException($this->l->t('The given IP range is not valid for IPv4'), 4); |
96
|
|
|
} |
97
|
|
|
} else { |
98
|
|
View Code Duplication |
if (!filter_var($decodedValue[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { |
|
|
|
|
99
|
|
|
throw new \UnexpectedValueException($this->l->t('The given IP range is not valid for IPv6'), 3); |
100
|
|
|
} |
101
|
|
View Code Duplication |
if ($decodedValue[1] > 128 || $decodedValue[1] <= 0) { |
|
|
|
|
102
|
|
|
throw new \UnexpectedValueException($this->l->t('The given IP range is not valid for IPv6'), 4); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Based on http://stackoverflow.com/a/594134 |
109
|
|
|
* @param string $ip |
110
|
|
|
* @param string $rangeIp |
111
|
|
|
* @param int $bits |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
|
|
protected function matchIPv4($ip, $rangeIp, $bits) { |
115
|
|
|
$rangeDecimal = ip2long($rangeIp); |
116
|
|
|
$ipDecimal = ip2long($ip); |
117
|
|
|
$mask = -1 << (32 - $bits); |
118
|
|
|
return ($ipDecimal & $mask) === ($rangeDecimal & $mask); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Based on http://stackoverflow.com/a/7951507 |
123
|
|
|
* @param string $ip |
124
|
|
|
* @param string $rangeIp |
125
|
|
|
* @param int $bits |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
|
|
protected function matchIPv6($ip, $rangeIp, $bits) { |
129
|
|
|
$ipNet = inet_pton($ip); |
130
|
|
|
$binaryIp = $this->ipv6ToBits($ipNet); |
131
|
|
|
$ipNetBits = substr($binaryIp, 0, $bits); |
132
|
|
|
|
133
|
|
|
$rangeNet = inet_pton($rangeIp); |
134
|
|
|
$binaryRange = $this->ipv6ToBits($rangeNet); |
135
|
|
|
$rangeNetBits = substr($binaryRange, 0, $bits); |
136
|
|
|
|
137
|
|
|
return $ipNetBits === $rangeNetBits; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Based on http://stackoverflow.com/a/7951507 |
142
|
|
|
* @param string $packedIp |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
|
|
protected function ipv6ToBits($packedIp) { |
146
|
|
|
$unpackedIp = unpack('A16', $packedIp); |
147
|
|
|
$unpackedIp = str_split($unpackedIp[1]); |
148
|
|
|
$binaryIp = ''; |
149
|
|
|
foreach ($unpackedIp as $char) { |
150
|
|
|
$binaryIp .= str_pad(decbin(ord($char)), 8, '0', STR_PAD_LEFT); |
151
|
|
|
} |
152
|
|
|
return str_pad($binaryIp, 128, '0', STR_PAD_RIGHT); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.