FauxRequest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 getValues() {
  86. return $this->data;
  87. }
  88. /**
  89. * @return array
  90. */
  91. public function getQueryValues() {
  92. if ( $this->wasPosted ) {
  93. return [];
  94. } else {
  95. return $this->data;
  96. }
  97. }
  98. public function getMethod() {
  99. return $this->wasPosted ? 'POST' : 'GET';
  100. }
  101. /**
  102. * @return bool
  103. */
  104. public function wasPosted() {
  105. return $this->wasPosted;
  106. }
  107. public function getCookie( $key, $prefix = null, $default = null ) {
  108. if ( $prefix === null ) {
  109. global $wgCookiePrefix;
  110. $prefix = $wgCookiePrefix;
  111. }
  112. $name = $prefix . $key;
  113. return $this->cookies[$name] ?? $default;
  114. }
  115. /**
  116. * @since 1.26
  117. * @param string $key Unprefixed name of the cookie to set
  118. * @param string|null $value Value of the cookie to set
  119. * @param string|null $prefix Cookie prefix. Defaults to $wgCookiePrefix
  120. */
  121. public function setCookie( $key, $value, $prefix = null ) {
  122. $this->setCookies( [ $key => $value ], $prefix );
  123. }
  124. /**
  125. * @since 1.26
  126. * @param array $cookies
  127. * @param string|null $prefix Cookie prefix. Defaults to $wgCookiePrefix
  128. */
  129. public function setCookies( $cookies, $prefix = null ) {
  130. if ( $prefix === null ) {
  131. global $wgCookiePrefix;
  132. $prefix = $wgCookiePrefix;
  133. }
  134. foreach ( $cookies as $key => $value ) {
  135. $name = $prefix . $key;
  136. $this->cookies[$name] = $value;
  137. }
  138. }
  139. /**
  140. * @since 1.25
  141. * @param string $url
  142. */
  143. public function setRequestURL( $url ) {
  144. $this->requestUrl = $url;
  145. }
  146. /**
  147. * @since 1.25 MWException( "getRequestURL not implemented" )
  148. * no longer thrown.
  149. * @return string
  150. */
  151. public function getRequestURL() {
  152. if ( $this->requestUrl === null ) {
  153. throw new MWException( 'Request URL not set' );
  154. }
  155. return $this->requestUrl;
  156. }
  157. public function getProtocol() {
  158. return $this->protocol;
  159. }
  160. /**
  161. * @param string $name
  162. * @param string $val
  163. */
  164. public function setHeader( $name, $val ) {
  165. $this->setHeaders( [ $name => $val ] );
  166. }
  167. /**
  168. * @since 1.26
  169. * @param array $headers
  170. */
  171. public function setHeaders( $headers ) {
  172. foreach ( $headers as $name => $val ) {
  173. $name = strtoupper( $name );
  174. $this->headers[$name] = $val;
  175. }
  176. }
  177. /**
  178. * @return array|null
  179. */
  180. public function getSessionArray() {
  181. if ( $this->sessionId !== null ) {
  182. return iterator_to_array( $this->getSession() );
  183. }
  184. return null;
  185. }
  186. /**
  187. * FauxRequests shouldn't depend on raw request data (but that could be implemented here)
  188. * @return string
  189. */
  190. public function getRawQueryString() {
  191. return '';
  192. }
  193. /**
  194. * FauxRequests shouldn't depend on raw request data (but that could be implemented here)
  195. * @return string
  196. */
  197. public function getRawPostString() {
  198. return '';
  199. }
  200. /**
  201. * FauxRequests shouldn't depend on raw request data (but that could be implemented here)
  202. * @return string
  203. */
  204. public function getRawInput() {
  205. return '';
  206. }
  207. /**
  208. * @codeCoverageIgnore
  209. * @param array $extWhitelist
  210. * @return bool
  211. */
  212. public function checkUrlExtension( $extWhitelist = [] ) {
  213. return true;
  214. }
  215. /**
  216. * @codeCoverageIgnore
  217. * @return string
  218. */
  219. protected function getRawIP() {
  220. return '127.0.0.1';
  221. }
  222. }