123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?php
- namespace OCA\WorkflowEngine\Check;
- use OCP\IL10N;
- use OCP\IRequest;
- use OCP\WorkflowEngine\ICheck;
- class RequestRemoteAddress implements ICheck {
-
- protected $l;
-
- protected $request;
-
- public function __construct(IL10N $l, IRequest $request) {
- $this->l = $l;
- $this->request = $request;
- }
-
- public function executeCheck($operator, $value) {
- $actualValue = $this->request->getRemoteAddress();
- $decodedValue = explode('/', $value);
- if ($operator === 'matchesIPv4') {
- return $this->matchIPv4($actualValue, $decodedValue[0], $decodedValue[1]);
- } elseif ($operator === '!matchesIPv4') {
- return !$this->matchIPv4($actualValue, $decodedValue[0], $decodedValue[1]);
- } elseif ($operator === 'matchesIPv6') {
- return $this->matchIPv6($actualValue, $decodedValue[0], $decodedValue[1]);
- } else {
- return !$this->matchIPv6($actualValue, $decodedValue[0], $decodedValue[1]);
- }
- }
-
- public function validateCheck($operator, $value) {
- if (!in_array($operator, ['matchesIPv4', '!matchesIPv4', 'matchesIPv6', '!matchesIPv6'])) {
- throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1);
- }
- $decodedValue = explode('/', $value);
- if (count($decodedValue) !== 2) {
- throw new \UnexpectedValueException($this->l->t('The given IP range is invalid'), 2);
- }
- if (in_array($operator, ['matchesIPv4', '!matchesIPv4'])) {
- if (!filter_var($decodedValue[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
- throw new \UnexpectedValueException($this->l->t('The given IP range is not valid for IPv4'), 3);
- }
- if ($decodedValue[1] > 32 || $decodedValue[1] <= 0) {
- throw new \UnexpectedValueException($this->l->t('The given IP range is not valid for IPv4'), 4);
- }
- } else {
- if (!filter_var($decodedValue[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
- throw new \UnexpectedValueException($this->l->t('The given IP range is not valid for IPv6'), 3);
- }
- if ($decodedValue[1] > 128 || $decodedValue[1] <= 0) {
- throw new \UnexpectedValueException($this->l->t('The given IP range is not valid for IPv6'), 4);
- }
- }
- }
-
- protected function matchIPv4($ip, $rangeIp, $bits) {
- $rangeDecimal = ip2long($rangeIp);
- $ipDecimal = ip2long($ip);
- $mask = -1 << (32 - $bits);
- return ($ipDecimal & $mask) === ($rangeDecimal & $mask);
- }
-
- protected function matchIPv6($ip, $rangeIp, $bits) {
- $ipNet = inet_pton($ip);
- $binaryIp = $this->ipv6ToBits($ipNet);
- $ipNetBits = substr($binaryIp, 0, $bits);
- $rangeNet = inet_pton($rangeIp);
- $binaryRange = $this->ipv6ToBits($rangeNet);
- $rangeNetBits = substr($binaryRange, 0, $bits);
- return $ipNetBits === $rangeNetBits;
- }
-
- protected function ipv6ToBits($packedIp) {
- $unpackedIp = unpack('A16', $packedIp);
- $unpackedIp = str_split($unpackedIp[1]);
- $binaryIp = '';
- foreach ($unpackedIp as $char) {
- $binaryIp .= str_pad(decbin(ord($char)), 8, '0', STR_PAD_LEFT);
- }
- return str_pad($binaryIp, 128, '0', STR_PAD_RIGHT);
- }
-
- public function supportedEntities(): array {
- return [];
- }
-
- public function isAvailableForScope(int $scope): bool {
- return true;
- }
- }
|