FauxRequest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. /**
  3. * Deal with importing all those nasty globals and things
  4. *
  5. * Copyright © 2003 Brion Vibber <brion@pobox.com>
  6. * https://www.mediawiki.org/
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. * http://www.gnu.org/copyleft/gpl.html
  22. *
  23. * @file
  24. */
  25. use MediaWiki\Session\SessionManager;
  26. /**
  27. * WebRequest clone which takes values from a provided array.
  28. *
  29. * @ingroup HTTP
  30. */
  31. class FauxRequest extends WebRequest {
  32. private $wasPosted = false;
  33. private $requestUrl;
  34. protected $cookies = [];
  35. /**
  36. * @param array $data Array of *non*-urlencoded key => value pairs, the
  37. * fake GET/POST values
  38. * @param bool $wasPosted Whether to treat the data as POST
  39. * @param MediaWiki\Session\Session|array|null $session Session, session
  40. * data array, or null
  41. * @param string $protocol 'http' or 'https'
  42. * @throws MWException
  43. */
  44. public function __construct( $data = [], $wasPosted = false,
  45. $session = null, $protocol = 'http'
  46. ) {
  47. $this->requestTime = microtime( true );
  48. if ( is_array( $data ) ) {
  49. $this->data = $data;
  50. } else {
  51. throw new MWException( "FauxRequest() got bogus data" );
  52. }
  53. $this->wasPosted = $wasPosted;
  54. if ( $session instanceof MediaWiki\Session\Session ) {
  55. $this->sessionId = $session->getSessionId();
  56. } elseif ( is_array( $session ) ) {
  57. $mwsession = SessionManager::singleton()->getEmptySession( $this );
  58. $this->sessionId = $mwsession->getSessionId();
  59. foreach ( $session as $key => $value ) {
  60. $mwsession->set( $key, $value );
  61. }
  62. } elseif ( $session !== null ) {
  63. throw new MWException( "FauxRequest() got bogus session" );
  64. }
  65. $this->protocol = $protocol;
  66. }
  67. /**
  68. * Initialise the header list
  69. */
  70. protected function initHeaders() {
  71. // Nothing to init
  72. }
  73. /**
  74. * @param string $name
  75. * @param string $default
  76. * @return string
  77. */
  78. public function getText( $name, $default = '' ) {
  79. # Override; don't recode since we're using internal data
  80. return (string)$this->getVal( $name, $default );
  81. }
  82. /**
  83. * @return array
  84. */
  85. public function getQueryValues() {
  86. if ( $this->wasPosted ) {
  87. return [];
  88. } else {
  89. return $this->data;
  90. }
  91. }
  92. public function getMethod() {
  93. return $this->wasPosted ? 'POST' : 'GET';
  94. }
  95. /**
  96. * @return bool
  97. */
  98. public function wasPosted() {
  99. return $this->wasPosted;
  100. }
  101. public function getCookie( $key, $prefix = null, $default = null ) {
  102. if ( $prefix === null ) {
  103. global $wgCookiePrefix;
  104. $prefix = $wgCookiePrefix;
  105. }
  106. $name = $prefix . $key;
  107. return $this->cookies[$name] ?? $default;
  108. }
  109. /**
  110. * @since 1.26
  111. * @param string $key Unprefixed name of the cookie to set
  112. * @param string|null $value Value of the cookie to set
  113. * @param string|null $prefix Cookie prefix. Defaults to $wgCookiePrefix
  114. */
  115. public function setCookie( $key, $value, $prefix = null ) {
  116. $this->setCookies( [ $key => $value ], $prefix );
  117. }
  118. /**
  119. * @since 1.26
  120. * @param array $cookies
  121. * @param string|null $prefix Cookie prefix. Defaults to $wgCookiePrefix
  122. */
  123. public function setCookies( $cookies, $prefix = null ) {
  124. if ( $prefix === null ) {
  125. global $wgCookiePrefix;
  126. $prefix = $wgCookiePrefix;
  127. }
  128. foreach ( $cookies as $key => $value ) {
  129. $name = $prefix . $key;
  130. $this->cookies[$name] = $value;
  131. }
  132. }
  133. /**
  134. * @since 1.25
  135. * @param string $url
  136. */
  137. public function setRequestURL( $url ) {
  138. $this->requestUrl = $url;
  139. }
  140. /**
  141. * @since 1.25 MWException( "getRequestURL not implemented" )
  142. * no longer thrown.
  143. * @return string
  144. */
  145. public function getRequestURL() {
  146. if ( $this->requestUrl === null ) {
  147. throw new MWException( 'Request URL not set' );
  148. }
  149. return $this->requestUrl;
  150. }
  151. public function getProtocol() {
  152. return $this->protocol;
  153. }
  154. /**
  155. * @param string $name
  156. * @param string $val
  157. */
  158. public function setHeader( $name, $val ) {
  159. $this->setHeaders( [ $name => $val ] );
  160. }
  161. /**
  162. * @since 1.26
  163. * @param array $headers
  164. */
  165. public function setHeaders( $headers ) {
  166. foreach ( $headers as $name => $val ) {
  167. $name = strtoupper( $name );
  168. $this->headers[$name] = $val;
  169. }
  170. }
  171. /**
  172. * @return array|null
  173. */
  174. public function getSessionArray() {
  175. if ( $this->sessionId !== null ) {
  176. return iterator_to_array( $this->getSession() );
  177. }
  178. return null;
  179. }
  180. /**
  181. * FauxRequests shouldn't depend on raw request data (but that could be implemented here)
  182. * @return string
  183. */
  184. public function getRawQueryString() {
  185. return '';
  186. }
  187. /**
  188. * FauxRequests shouldn't depend on raw request data (but that could be implemented here)
  189. * @return string
  190. */
  191. public function getRawPostString() {
  192. return '';
  193. }
  194. /**
  195. * FauxRequests shouldn't depend on raw request data (but that could be implemented here)
  196. * @return string
  197. */
  198. public function getRawInput() {
  199. return '';
  200. }
  201. /**
  202. * @codeCoverageIgnore
  203. * @param array $extWhitelist
  204. * @return bool
  205. */
  206. public function checkUrlExtension( $extWhitelist = [] ) {
  207. return true;
  208. }
  209. /**
  210. * @codeCoverageIgnore
  211. * @return string
  212. */
  213. protected function getRawIP() {
  214. return '127.0.0.1';
  215. }
  216. }