RequestRemoteAddress.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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\Files\Storage\IStorage;
  23. use OCP\IL10N;
  24. use OCP\IRequest;
  25. use OCP\WorkflowEngine\ICheck;
  26. class RequestRemoteAddress implements ICheck {
  27. /** @var IL10N */
  28. protected $l;
  29. /** @var IRequest */
  30. protected $request;
  31. /**
  32. * @param IL10N $l
  33. * @param IRequest $request
  34. */
  35. public function __construct(IL10N $l, IRequest $request) {
  36. $this->l = $l;
  37. $this->request = $request;
  38. }
  39. /**
  40. * @param IStorage $storage
  41. * @param string $path
  42. * @param bool $isDir
  43. */
  44. public function setFileInfo(IStorage $storage, $path, $isDir = false) {
  45. // A different path doesn't change time, so nothing to do here.
  46. }
  47. /**
  48. * @param string $operator
  49. * @param string $value
  50. * @return bool
  51. */
  52. public function executeCheck($operator, $value) {
  53. $actualValue = $this->request->getRemoteAddress();
  54. $decodedValue = explode('/', $value);
  55. if ($operator === 'matchesIPv4') {
  56. return $this->matchIPv4($actualValue, $decodedValue[0], $decodedValue[1]);
  57. } else if ($operator === '!matchesIPv4') {
  58. return !$this->matchIPv4($actualValue, $decodedValue[0], $decodedValue[1]);
  59. } else if ($operator === 'matchesIPv6') {
  60. return $this->matchIPv6($actualValue, $decodedValue[0], $decodedValue[1]);
  61. } else {
  62. return !$this->matchIPv6($actualValue, $decodedValue[0], $decodedValue[1]);
  63. }
  64. }
  65. /**
  66. * @param string $operator
  67. * @param string $value
  68. * @throws \UnexpectedValueException
  69. */
  70. public function validateCheck($operator, $value) {
  71. if (!in_array($operator, ['matchesIPv4', '!matchesIPv4', 'matchesIPv6', '!matchesIPv6'])) {
  72. throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1);
  73. }
  74. $decodedValue = explode('/', $value);
  75. if (count($decodedValue) !== 2) {
  76. throw new \UnexpectedValueException($this->l->t('The given IP range is invalid'), 2);
  77. }
  78. if (in_array($operator, ['matchesIPv4', '!matchesIPv4'])) {
  79. if (!filter_var($decodedValue[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
  80. throw new \UnexpectedValueException($this->l->t('The given IP range is not valid for IPv4'), 3);
  81. }
  82. if ($decodedValue[1] > 32 || $decodedValue[1] <= 0) {
  83. throw new \UnexpectedValueException($this->l->t('The given IP range is not valid for IPv4'), 4);
  84. }
  85. } else {
  86. if (!filter_var($decodedValue[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
  87. throw new \UnexpectedValueException($this->l->t('The given IP range is not valid for IPv6'), 3);
  88. }
  89. if ($decodedValue[1] > 128 || $decodedValue[1] <= 0) {
  90. throw new \UnexpectedValueException($this->l->t('The given IP range is not valid for IPv6'), 4);
  91. }
  92. }
  93. }
  94. /**
  95. * Based on http://stackoverflow.com/a/594134
  96. * @param string $ip
  97. * @param string $rangeIp
  98. * @param int $bits
  99. * @return bool
  100. */
  101. protected function matchIPv4($ip, $rangeIp, $bits) {
  102. $rangeDecimal = ip2long($rangeIp);
  103. $ipDecimal = ip2long($ip);
  104. $mask = -1 << (32 - $bits);
  105. return ($ipDecimal & $mask) === ($rangeDecimal & $mask);
  106. }
  107. /**
  108. * Based on http://stackoverflow.com/a/7951507
  109. * @param string $ip
  110. * @param string $rangeIp
  111. * @param int $bits
  112. * @return bool
  113. */
  114. protected function matchIPv6($ip, $rangeIp, $bits) {
  115. $ipNet = inet_pton($ip);
  116. $binaryIp = $this->ipv6ToBits($ipNet);
  117. $ipNetBits = substr($binaryIp, 0, $bits);
  118. $rangeNet = inet_pton($rangeIp);
  119. $binaryRange = $this->ipv6ToBits($rangeNet);
  120. $rangeNetBits = substr($binaryRange, 0, $bits);
  121. return $ipNetBits === $rangeNetBits;
  122. }
  123. /**
  124. * Based on http://stackoverflow.com/a/7951507
  125. * @param string $packedIp
  126. * @return string
  127. */
  128. protected function ipv6ToBits($packedIp) {
  129. $unpackedIp = unpack('A16', $packedIp);
  130. $unpackedIp = str_split($unpackedIp[1]);
  131. $binaryIp = '';
  132. foreach ($unpackedIp as $char) {
  133. $binaryIp .= str_pad(decbin(ord($char)), 8, '0', STR_PAD_LEFT);
  134. }
  135. return str_pad($binaryIp, 128, '0', STR_PAD_RIGHT);
  136. }
  137. }