MWRestrictions.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * A class to check request restrictions expressed as a JSON object
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. */
  20. /**
  21. * A class to check request restrictions expressed as a JSON object
  22. */
  23. class MWRestrictions {
  24. private $ipAddresses = [ '0.0.0.0/0', '::/0' ];
  25. /**
  26. * @param array|null $restrictions
  27. * @throws InvalidArgumentException
  28. */
  29. protected function __construct( array $restrictions = null ) {
  30. if ( $restrictions !== null ) {
  31. $this->loadFromArray( $restrictions );
  32. }
  33. }
  34. /**
  35. * @return MWRestrictions
  36. */
  37. public static function newDefault() {
  38. return new self();
  39. }
  40. /**
  41. * @param array $restrictions
  42. * @return MWRestrictions
  43. * @throws InvalidArgumentException
  44. */
  45. public static function newFromArray( array $restrictions ) {
  46. return new self( $restrictions );
  47. }
  48. /**
  49. * @param string $json JSON representation of the restrictions
  50. * @return MWRestrictions
  51. * @throws InvalidArgumentException
  52. */
  53. public static function newFromJson( $json ) {
  54. $restrictions = FormatJson::decode( $json, true );
  55. if ( !is_array( $restrictions ) ) {
  56. throw new InvalidArgumentException( 'Invalid restrictions JSON' );
  57. }
  58. return new self( $restrictions );
  59. }
  60. private function loadFromArray( array $restrictions ) {
  61. static $validKeys = [ 'IPAddresses' ];
  62. static $neededKeys = [ 'IPAddresses' ];
  63. $keys = array_keys( $restrictions );
  64. $invalidKeys = array_diff( $keys, $validKeys );
  65. if ( $invalidKeys ) {
  66. throw new InvalidArgumentException(
  67. 'Array contains invalid keys: ' . implode( ', ', $invalidKeys )
  68. );
  69. }
  70. $missingKeys = array_diff( $neededKeys, $keys );
  71. if ( $missingKeys ) {
  72. throw new InvalidArgumentException(
  73. 'Array is missing required keys: ' . implode( ', ', $missingKeys )
  74. );
  75. }
  76. if ( !is_array( $restrictions['IPAddresses'] ) ) {
  77. throw new InvalidArgumentException( 'IPAddresses is not an array' );
  78. }
  79. foreach ( $restrictions['IPAddresses'] as $ip ) {
  80. if ( !\IP::isIPAddress( $ip ) ) {
  81. throw new InvalidArgumentException( "Invalid IP address: $ip" );
  82. }
  83. }
  84. $this->ipAddresses = $restrictions['IPAddresses'];
  85. }
  86. /**
  87. * Return the restrictions as an array
  88. * @return array
  89. */
  90. public function toArray() {
  91. return [
  92. 'IPAddresses' => $this->ipAddresses,
  93. ];
  94. }
  95. /**
  96. * Return the restrictions as a JSON string
  97. * @param bool|string $pretty Pretty-print the JSON output, see FormatJson::encode
  98. * @return string
  99. */
  100. public function toJson( $pretty = false ) {
  101. return FormatJson::encode( $this->toArray(), $pretty, FormatJson::ALL_OK );
  102. }
  103. public function __toString() {
  104. return $this->toJson();
  105. }
  106. /**
  107. * Test against the passed WebRequest
  108. * @param WebRequest $request
  109. * @return Status
  110. */
  111. public function check( WebRequest $request ) {
  112. $ok = [
  113. 'ip' => $this->checkIP( $request->getIP() ),
  114. ];
  115. $status = Status::newGood();
  116. $status->setResult( $ok === array_filter( $ok ), $ok );
  117. return $status;
  118. }
  119. /**
  120. * Test an IP address
  121. * @param string $ip
  122. * @return bool
  123. */
  124. public function checkIP( $ip ) {
  125. foreach ( $this->ipAddresses as $range ) {
  126. if ( \IP::isInRange( $ip, $range ) ) {
  127. return true;
  128. }
  129. }
  130. return false;
  131. }
  132. }