RequestRemoteAddress.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  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. namespace OCA\WorkflowEngine\Check;
  22. use OCP\IL10N;
  23. use OCP\IRequest;
  24. use OCP\WorkflowEngine\ICheck;
  25. class RequestRemoteAddress implements ICheck {
  26. /** @var IL10N */
  27. protected $l;
  28. /** @var IRequest */
  29. protected $request;
  30. /**
  31. * @param IL10N $l
  32. * @param IRequest $request
  33. */
  34. public function __construct(IL10N $l, IRequest $request) {
  35. $this->l = $l;
  36. $this->request = $request;
  37. }
  38. /**
  39. * @param string $operator
  40. * @param string $value
  41. * @return bool
  42. */
  43. public function executeCheck($operator, $value) {
  44. $actualValue = $this->request->getRemoteAddress();
  45. $decodedValue = explode('/', $value);
  46. if ($operator === 'matchesIPv4') {
  47. return $this->matchIPv4($actualValue, $decodedValue[0], $decodedValue[1]);
  48. } elseif ($operator === '!matchesIPv4') {
  49. return !$this->matchIPv4($actualValue, $decodedValue[0], $decodedValue[1]);
  50. } elseif ($operator === 'matchesIPv6') {
  51. return $this->matchIPv6($actualValue, $decodedValue[0], $decodedValue[1]);
  52. } else {
  53. return !$this->matchIPv6($actualValue, $decodedValue[0], $decodedValue[1]);
  54. }
  55. }
  56. /**
  57. * @param string $operator
  58. * @param string $value
  59. * @throws \UnexpectedValueException
  60. */
  61. public function validateCheck($operator, $value) {
  62. if (!in_array($operator, ['matchesIPv4', '!matchesIPv4', 'matchesIPv6', '!matchesIPv6'])) {
  63. throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1);
  64. }
  65. $decodedValue = explode('/', $value);
  66. if (count($decodedValue) !== 2) {
  67. throw new \UnexpectedValueException($this->l->t('The given IP range is invalid'), 2);
  68. }
  69. if (in_array($operator, ['matchesIPv4', '!matchesIPv4'])) {
  70. if (!filter_var($decodedValue[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
  71. throw new \UnexpectedValueException($this->l->t('The given IP range is not valid for IPv4'), 3);
  72. }
  73. if ($decodedValue[1] > 32 || $decodedValue[1] <= 0) {
  74. throw new \UnexpectedValueException($this->l->t('The given IP range is not valid for IPv4'), 4);
  75. }
  76. } else {
  77. if (!filter_var($decodedValue[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
  78. throw new \UnexpectedValueException($this->l->t('The given IP range is not valid for IPv6'), 3);
  79. }
  80. if ($decodedValue[1] > 128 || $decodedValue[1] <= 0) {
  81. throw new \UnexpectedValueException($this->l->t('The given IP range is not valid for IPv6'), 4);
  82. }
  83. }
  84. }
  85. /**
  86. * Based on https://stackoverflow.com/a/594134
  87. * @param string $ip
  88. * @param string $rangeIp
  89. * @param int $bits
  90. * @return bool
  91. */
  92. protected function matchIPv4($ip, $rangeIp, $bits) {
  93. $rangeDecimal = ip2long($rangeIp);
  94. $ipDecimal = ip2long($ip);
  95. $mask = -1 << (32 - $bits);
  96. return ($ipDecimal & $mask) === ($rangeDecimal & $mask);
  97. }
  98. /**
  99. * Based on https://stackoverflow.com/a/7951507
  100. * @param string $ip
  101. * @param string $rangeIp
  102. * @param int $bits
  103. * @return bool
  104. */
  105. protected function matchIPv6($ip, $rangeIp, $bits) {
  106. $ipNet = inet_pton($ip);
  107. $binaryIp = $this->ipv6ToBits($ipNet);
  108. $ipNetBits = substr($binaryIp, 0, $bits);
  109. $rangeNet = inet_pton($rangeIp);
  110. $binaryRange = $this->ipv6ToBits($rangeNet);
  111. $rangeNetBits = substr($binaryRange, 0, $bits);
  112. return $ipNetBits === $rangeNetBits;
  113. }
  114. /**
  115. * Based on https://stackoverflow.com/a/7951507
  116. * @param string $packedIp
  117. * @return string
  118. */
  119. protected function ipv6ToBits($packedIp) {
  120. $unpackedIp = unpack('A16', $packedIp);
  121. $unpackedIp = str_split($unpackedIp[1]);
  122. $binaryIp = '';
  123. foreach ($unpackedIp as $char) {
  124. $binaryIp .= str_pad(decbin(ord($char)), 8, '0', STR_PAD_LEFT);
  125. }
  126. return str_pad($binaryIp, 128, '0', STR_PAD_RIGHT);
  127. }
  128. /**
  129. * returns a list of Entities the checker supports. The values must match
  130. * the class name of the entity.
  131. *
  132. * An empty result means the check is universally available.
  133. *
  134. * @since 18.0.0
  135. */
  136. public function supportedEntities(): array {
  137. return [];
  138. }
  139. /**
  140. * returns whether the operation can be used in the requested scope.
  141. *
  142. * Scope IDs are defined as constants in OCP\WorkflowEngine\IManager. At
  143. * time of writing these are SCOPE_ADMIN and SCOPE_USER.
  144. *
  145. * For possibly unknown future scopes the recommended behaviour is: if
  146. * user scope is permitted, the default behaviour should return `true`,
  147. * otherwise `false`.
  148. *
  149. * @since 18.0.0
  150. */
  151. public function isAvailableForScope(int $scope): bool {
  152. return true;
  153. }
  154. }