RequestRemoteAddress.php 4.4 KB

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