WebRequest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. <?php
  2. /**
  3. * Deal with importing all those nasssty globals and things
  4. */
  5. # Copyright (C) 2003 Brion Vibber <brion@pobox.com>
  6. # http://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. * Some entry points may use this file without first enabling the
  24. * autoloader.
  25. */
  26. if ( !function_exists( '__autoload' ) ) {
  27. require_once( dirname(__FILE__) . '/normal/UtfNormal.php' );
  28. }
  29. /**
  30. * The WebRequest class encapsulates getting at data passed in the
  31. * URL or via a POSTed form, handling remove of "magic quotes" slashes,
  32. * stripping illegal input characters and normalizing Unicode sequences.
  33. *
  34. * Usually this is used via a global singleton, $wgRequest. You should
  35. * not create a second WebRequest object; make a FauxRequest object if
  36. * you want to pass arbitrary data to some function in place of the web
  37. * input.
  38. *
  39. * @ingroup HTTP
  40. */
  41. class WebRequest {
  42. var $data = array();
  43. var $headers;
  44. private $_response;
  45. function __construct() {
  46. /// @fixme This preemptive de-quoting can interfere with other web libraries
  47. /// and increases our memory footprint. It would be cleaner to do on
  48. /// demand; but currently we have no wrapper for $_SERVER etc.
  49. $this->checkMagicQuotes();
  50. // POST overrides GET data
  51. // We don't use $_REQUEST here to avoid interference from cookies...
  52. $this->data = $_POST + $_GET;
  53. }
  54. /**
  55. * Check for title, action, and/or variant data in the URL
  56. * and interpolate it into the GET variables.
  57. * This should only be run after $wgContLang is available,
  58. * as we may need the list of language variants to determine
  59. * available variant URLs.
  60. */
  61. function interpolateTitle() {
  62. global $wgUsePathInfo;
  63. if ( $wgUsePathInfo ) {
  64. // PATH_INFO is mangled due to http://bugs.php.net/bug.php?id=31892
  65. // And also by Apache 2.x, double slashes are converted to single slashes.
  66. // So we will use REQUEST_URI if possible.
  67. $matches = array();
  68. if ( !empty( $_SERVER['REQUEST_URI'] ) ) {
  69. // Slurp out the path portion to examine...
  70. $url = $_SERVER['REQUEST_URI'];
  71. if ( !preg_match( '!^https?://!', $url ) ) {
  72. $url = 'http://unused' . $url;
  73. }
  74. $a = parse_url( $url );
  75. if( $a ) {
  76. $path = isset( $a['path'] ) ? $a['path'] : '';
  77. global $wgScript;
  78. if( $path == $wgScript ) {
  79. // Script inside a rewrite path?
  80. // Abort to keep from breaking...
  81. return;
  82. }
  83. // Raw PATH_INFO style
  84. $matches = $this->extractTitle( $path, "$wgScript/$1" );
  85. global $wgArticlePath;
  86. if( !$matches && $wgArticlePath ) {
  87. $matches = $this->extractTitle( $path, $wgArticlePath );
  88. }
  89. global $wgActionPaths;
  90. if( !$matches && $wgActionPaths ) {
  91. $matches = $this->extractTitle( $path, $wgActionPaths, 'action' );
  92. }
  93. global $wgVariantArticlePath, $wgContLang;
  94. if( !$matches && $wgVariantArticlePath ) {
  95. $variantPaths = array();
  96. foreach( $wgContLang->getVariants() as $variant ) {
  97. $variantPaths[$variant] =
  98. str_replace( '$2', $variant, $wgVariantArticlePath );
  99. }
  100. $matches = $this->extractTitle( $path, $variantPaths, 'variant' );
  101. }
  102. }
  103. } elseif ( isset( $_SERVER['ORIG_PATH_INFO'] ) && $_SERVER['ORIG_PATH_INFO'] != '' ) {
  104. // Mangled PATH_INFO
  105. // http://bugs.php.net/bug.php?id=31892
  106. // Also reported when ini_get('cgi.fix_pathinfo')==false
  107. $matches['title'] = substr( $_SERVER['ORIG_PATH_INFO'], 1 );
  108. } elseif ( isset( $_SERVER['PATH_INFO'] ) && ($_SERVER['PATH_INFO'] != '') ) {
  109. // Regular old PATH_INFO yay
  110. $matches['title'] = substr( $_SERVER['PATH_INFO'], 1 );
  111. }
  112. foreach( $matches as $key => $val) {
  113. $this->data[$key] = $_GET[$key] = $_REQUEST[$key] = $val;
  114. }
  115. }
  116. }
  117. /**
  118. * Internal URL rewriting function; tries to extract page title and,
  119. * optionally, one other fixed parameter value from a URL path.
  120. *
  121. * @param $path string: the URL path given from the client
  122. * @param $bases array: one or more URLs, optionally with $1 at the end
  123. * @param $key string: if provided, the matching key in $bases will be
  124. * passed on as the value of this URL parameter
  125. * @return array of URL variables to interpolate; empty if no match
  126. */
  127. private function extractTitle( $path, $bases, $key=false ) {
  128. foreach( (array)$bases as $keyValue => $base ) {
  129. // Find the part after $wgArticlePath
  130. $base = str_replace( '$1', '', $base );
  131. $baseLen = strlen( $base );
  132. if( substr( $path, 0, $baseLen ) == $base ) {
  133. $raw = substr( $path, $baseLen );
  134. if( $raw !== '' ) {
  135. $matches = array( 'title' => rawurldecode( $raw ) );
  136. if( $key ) {
  137. $matches[$key] = $keyValue;
  138. }
  139. return $matches;
  140. }
  141. }
  142. }
  143. return array();
  144. }
  145. /**
  146. * Recursively strips slashes from the given array;
  147. * used for undoing the evil that is magic_quotes_gpc.
  148. * @param $arr array: will be modified
  149. * @return array the original array
  150. * @private
  151. */
  152. function &fix_magic_quotes( &$arr ) {
  153. foreach( $arr as $key => $val ) {
  154. if( is_array( $val ) ) {
  155. $this->fix_magic_quotes( $arr[$key] );
  156. } else {
  157. $arr[$key] = stripslashes( $val );
  158. }
  159. }
  160. return $arr;
  161. }
  162. /**
  163. * If magic_quotes_gpc option is on, run the global arrays
  164. * through fix_magic_quotes to strip out the stupid slashes.
  165. * WARNING: This should only be done once! Running a second
  166. * time could damage the values.
  167. * @private
  168. */
  169. function checkMagicQuotes() {
  170. if ( function_exists( 'get_magic_quotes_gpc' ) && get_magic_quotes_gpc() ) {
  171. $this->fix_magic_quotes( $_COOKIE );
  172. $this->fix_magic_quotes( $_ENV );
  173. $this->fix_magic_quotes( $_GET );
  174. $this->fix_magic_quotes( $_POST );
  175. $this->fix_magic_quotes( $_REQUEST );
  176. $this->fix_magic_quotes( $_SERVER );
  177. }
  178. }
  179. /**
  180. * Recursively normalizes UTF-8 strings in the given array.
  181. * @param $data string or array
  182. * @return cleaned-up version of the given
  183. * @private
  184. */
  185. function normalizeUnicode( $data ) {
  186. if( is_array( $data ) ) {
  187. foreach( $data as $key => $val ) {
  188. $data[$key] = $this->normalizeUnicode( $val );
  189. }
  190. } else {
  191. $data = UtfNormal::cleanUp( $data );
  192. }
  193. return $data;
  194. }
  195. /**
  196. * Fetch a value from the given array or return $default if it's not set.
  197. *
  198. * @param $arr array
  199. * @param $name string
  200. * @param $default mixed
  201. * @return mixed
  202. * @private
  203. */
  204. function getGPCVal( $arr, $name, $default ) {
  205. if( isset( $arr[$name] ) ) {
  206. global $wgContLang;
  207. $data = $arr[$name];
  208. if( isset( $_GET[$name] ) && !is_array( $data ) ) {
  209. # Check for alternate/legacy character encoding.
  210. if( isset( $wgContLang ) ) {
  211. $data = $wgContLang->checkTitleEncoding( $data );
  212. }
  213. }
  214. $data = $this->normalizeUnicode( $data );
  215. return $data;
  216. } else {
  217. taint( $default );
  218. return $default;
  219. }
  220. }
  221. /**
  222. * Fetch a scalar from the input or return $default if it's not set.
  223. * Returns a string. Arrays are discarded. Useful for
  224. * non-freeform text inputs (e.g. predefined internal text keys
  225. * selected by a drop-down menu). For freeform input, see getText().
  226. *
  227. * @param $name string
  228. * @param $default string: optional default (or NULL)
  229. * @return string
  230. */
  231. function getVal( $name, $default = NULL ) {
  232. $val = $this->getGPCVal( $this->data, $name, $default );
  233. if( is_array( $val ) ) {
  234. $val = $default;
  235. }
  236. if( is_null( $val ) ) {
  237. return $val;
  238. } else {
  239. return (string)$val;
  240. }
  241. }
  242. /**
  243. * Set an aribtrary value into our get/post data.
  244. * @param $key string Key name to use
  245. * @param $value mixed Value to set
  246. * @return mixed old value if one was present, null otherwise
  247. */
  248. function setVal( $key, $value ) {
  249. $ret = isset( $this->data[$key] ) ? $this->data[$key] : null;
  250. $this->data[$key] = $value;
  251. return $ret;
  252. }
  253. /**
  254. * Fetch an array from the input or return $default if it's not set.
  255. * If source was scalar, will return an array with a single element.
  256. * If no source and no default, returns NULL.
  257. *
  258. * @param $name string
  259. * @param $default array: optional default (or NULL)
  260. * @return array
  261. */
  262. function getArray( $name, $default = NULL ) {
  263. $val = $this->getGPCVal( $this->data, $name, $default );
  264. if( is_null( $val ) ) {
  265. return null;
  266. } else {
  267. return (array)$val;
  268. }
  269. }
  270. /**
  271. * Fetch an array of integers, or return $default if it's not set.
  272. * If source was scalar, will return an array with a single element.
  273. * If no source and no default, returns NULL.
  274. * If an array is returned, contents are guaranteed to be integers.
  275. *
  276. * @param $name string
  277. * @param $default array: option default (or NULL)
  278. * @return array of ints
  279. */
  280. function getIntArray( $name, $default = NULL ) {
  281. $val = $this->getArray( $name, $default );
  282. if( is_array( $val ) ) {
  283. $val = array_map( 'intval', $val );
  284. }
  285. return $val;
  286. }
  287. /**
  288. * Fetch an integer value from the input or return $default if not set.
  289. * Guaranteed to return an integer; non-numeric input will typically
  290. * return 0.
  291. * @param $name string
  292. * @param $default int
  293. * @return int
  294. */
  295. function getInt( $name, $default = 0 ) {
  296. return intval( $this->getVal( $name, $default ) );
  297. }
  298. /**
  299. * Fetch an integer value from the input or return null if empty.
  300. * Guaranteed to return an integer or null; non-numeric input will
  301. * typically return null.
  302. * @param $name string
  303. * @return int
  304. */
  305. function getIntOrNull( $name ) {
  306. $val = $this->getVal( $name );
  307. return is_numeric( $val )
  308. ? intval( $val )
  309. : null;
  310. }
  311. /**
  312. * Fetch a boolean value from the input or return $default if not set.
  313. * Guaranteed to return true or false, with normal PHP semantics for
  314. * boolean interpretation of strings.
  315. * @param $name string
  316. * @param $default bool
  317. * @return bool
  318. */
  319. function getBool( $name, $default = false ) {
  320. return $this->getVal( $name, $default ) ? true : false;
  321. }
  322. /**
  323. * Return true if the named value is set in the input, whatever that
  324. * value is (even "0"). Return false if the named value is not set.
  325. * Example use is checking for the presence of check boxes in forms.
  326. * @param $name string
  327. * @return bool
  328. */
  329. function getCheck( $name ) {
  330. # Checkboxes and buttons are only present when clicked
  331. # Presence connotes truth, abscense false
  332. $val = $this->getVal( $name, NULL );
  333. return isset( $val );
  334. }
  335. /**
  336. * Fetch a text string from the given array or return $default if it's not
  337. * set. \r is stripped from the text, and with some language modules there
  338. * is an input transliteration applied. This should generally be used for
  339. * form <textarea> and <input> fields. Used for user-supplied freeform text
  340. * input (for which input transformations may be required - e.g. Esperanto
  341. * x-coding).
  342. *
  343. * @param $name string
  344. * @param $default string: optional
  345. * @return string
  346. */
  347. function getText( $name, $default = '' ) {
  348. global $wgContLang;
  349. $val = $this->getVal( $name, $default );
  350. return str_replace( "\r\n", "\n",
  351. $wgContLang->recodeInput( $val ) );
  352. }
  353. /**
  354. * Extracts the given named values into an array.
  355. * If no arguments are given, returns all input values.
  356. * No transformation is performed on the values.
  357. */
  358. function getValues() {
  359. $names = func_get_args();
  360. if ( count( $names ) == 0 ) {
  361. $names = array_keys( $this->data );
  362. }
  363. $retVal = array();
  364. foreach ( $names as $name ) {
  365. $value = $this->getVal( $name );
  366. if ( !is_null( $value ) ) {
  367. $retVal[$name] = $value;
  368. }
  369. }
  370. return $retVal;
  371. }
  372. /**
  373. * Returns true if the present request was reached by a POST operation,
  374. * false otherwise (GET, HEAD, or command-line).
  375. *
  376. * Note that values retrieved by the object may come from the
  377. * GET URL etc even on a POST request.
  378. *
  379. * @return bool
  380. */
  381. function wasPosted() {
  382. return $_SERVER['REQUEST_METHOD'] == 'POST';
  383. }
  384. /**
  385. * Returns true if there is a session cookie set.
  386. * This does not necessarily mean that the user is logged in!
  387. *
  388. * If you want to check for an open session, use session_id()
  389. * instead; that will also tell you if the session was opened
  390. * during the current request (in which case the cookie will
  391. * be sent back to the client at the end of the script run).
  392. *
  393. * @return bool
  394. */
  395. function checkSessionCookie() {
  396. return isset( $_COOKIE[session_name()] );
  397. }
  398. /**
  399. * Return the path portion of the request URI.
  400. * @return string
  401. */
  402. function getRequestURL() {
  403. if( isset( $_SERVER['REQUEST_URI'] ) ) {
  404. $base = $_SERVER['REQUEST_URI'];
  405. } elseif( isset( $_SERVER['SCRIPT_NAME'] ) ) {
  406. // Probably IIS; doesn't set REQUEST_URI
  407. $base = $_SERVER['SCRIPT_NAME'];
  408. if( isset( $_SERVER['QUERY_STRING'] ) && $_SERVER['QUERY_STRING'] != '' ) {
  409. $base .= '?' . $_SERVER['QUERY_STRING'];
  410. }
  411. } else {
  412. // This shouldn't happen!
  413. throw new MWException( "Web server doesn't provide either " .
  414. "REQUEST_URI or SCRIPT_NAME. Report details of your " .
  415. "web server configuration to http://bugzilla.wikimedia.org/" );
  416. }
  417. // User-agents should not send a fragment with the URI, but
  418. // if they do, and the web server passes it on to us, we
  419. // need to strip it or we get false-positive redirect loops
  420. // or weird output URLs
  421. $hash = strpos( $base, '#' );
  422. if( $hash !== false ) {
  423. $base = substr( $base, 0, $hash );
  424. }
  425. if( $base{0} == '/' ) {
  426. return $base;
  427. } else {
  428. // We may get paths with a host prepended; strip it.
  429. return preg_replace( '!^[^:]+://[^/]+/!', '/', $base );
  430. }
  431. }
  432. /**
  433. * Return the request URI with the canonical service and hostname.
  434. * @return string
  435. */
  436. function getFullRequestURL() {
  437. global $wgServer;
  438. return $wgServer . $this->getRequestURL();
  439. }
  440. /**
  441. * Take an arbitrary query and rewrite the present URL to include it
  442. * @param $query String: query string fragment; do not include initial '?'
  443. * @return string
  444. */
  445. function appendQuery( $query ) {
  446. global $wgTitle;
  447. $basequery = '';
  448. foreach( $_GET as $var => $val ) {
  449. if ( $var == 'title' )
  450. continue;
  451. if ( is_array( $val ) )
  452. /* This will happen given a request like
  453. * http://en.wikipedia.org/w/index.php?title[]=Special:Userlogin&returnto[]=Main_Page
  454. */
  455. continue;
  456. $basequery .= '&' . urlencode( $var ) . '=' . urlencode( $val );
  457. }
  458. $basequery .= '&' . $query;
  459. # Trim the extra &
  460. $basequery = substr( $basequery, 1 );
  461. return $wgTitle->getLocalURL( $basequery );
  462. }
  463. /**
  464. * HTML-safe version of appendQuery().
  465. * @param $query String: query string fragment; do not include initial '?'
  466. * @return string
  467. */
  468. function escapeAppendQuery( $query ) {
  469. return htmlspecialchars( $this->appendQuery( $query ) );
  470. }
  471. function appendQueryValue( $key, $value, $onlyquery = false ) {
  472. return $this->appendQueryArray( array( $key => $value ), $onlyquery );
  473. }
  474. /**
  475. * Appends or replaces value of query variables.
  476. * @param $array Array of values to replace/add to query
  477. * @param $onlyquery Bool: whether to only return the query string and not
  478. * the complete URL
  479. * @return string
  480. */
  481. function appendQueryArray( $array, $onlyquery = false ) {
  482. global $wgTitle;
  483. $newquery = $_GET;
  484. unset( $newquery['title'] );
  485. $newquery = array_merge( $newquery, $array );
  486. $query = wfArrayToCGI( $newquery );
  487. return $onlyquery ? $query : $wgTitle->getLocalURL( $query );
  488. }
  489. /**
  490. * Check for limit and offset parameters on the input, and return sensible
  491. * defaults if not given. The limit must be positive and is capped at 5000.
  492. * Offset must be positive but is not capped.
  493. *
  494. * @param $deflimit Integer: limit to use if no input and the user hasn't set the option.
  495. * @param $optionname String: to specify an option other than rclimit to pull from.
  496. * @return array first element is limit, second is offset
  497. */
  498. function getLimitOffset( $deflimit = 50, $optionname = 'rclimit' ) {
  499. global $wgUser;
  500. $limit = $this->getInt( 'limit', 0 );
  501. if( $limit < 0 ) $limit = 0;
  502. if( ( $limit == 0 ) && ( $optionname != '' ) ) {
  503. $limit = (int)$wgUser->getOption( $optionname );
  504. }
  505. if( $limit <= 0 ) $limit = $deflimit;
  506. if( $limit > 5000 ) $limit = 5000; # We have *some* limits...
  507. $offset = $this->getInt( 'offset', 0 );
  508. if( $offset < 0 ) $offset = 0;
  509. return array( $limit, $offset );
  510. }
  511. /**
  512. * Return the path to the temporary file where PHP has stored the upload.
  513. * @param $key String:
  514. * @return string or NULL if no such file.
  515. */
  516. function getFileTempname( $key ) {
  517. if( !isset( $_FILES[$key] ) ) {
  518. return NULL;
  519. }
  520. return $_FILES[$key]['tmp_name'];
  521. }
  522. /**
  523. * Return the size of the upload, or 0.
  524. * @param $key String:
  525. * @return integer
  526. */
  527. function getFileSize( $key ) {
  528. if( !isset( $_FILES[$key] ) ) {
  529. return 0;
  530. }
  531. return $_FILES[$key]['size'];
  532. }
  533. /**
  534. * Return the upload error or 0
  535. * @param $key String:
  536. * @return integer
  537. */
  538. function getUploadError( $key ) {
  539. if( !isset( $_FILES[$key] ) || !isset( $_FILES[$key]['error'] ) ) {
  540. return 0/*UPLOAD_ERR_OK*/;
  541. }
  542. return $_FILES[$key]['error'];
  543. }
  544. /**
  545. * Return the original filename of the uploaded file, as reported by
  546. * the submitting user agent. HTML-style character entities are
  547. * interpreted and normalized to Unicode normalization form C, in part
  548. * to deal with weird input from Safari with non-ASCII filenames.
  549. *
  550. * Other than this the name is not verified for being a safe filename.
  551. *
  552. * @param $key String:
  553. * @return string or NULL if no such file.
  554. */
  555. function getFileName( $key ) {
  556. if( !isset( $_FILES[$key] ) ) {
  557. return NULL;
  558. }
  559. $name = $_FILES[$key]['name'];
  560. # Safari sends filenames in HTML-encoded Unicode form D...
  561. # Horrid and evil! Let's try to make some kind of sense of it.
  562. $name = Sanitizer::decodeCharReferences( $name );
  563. $name = UtfNormal::cleanUp( $name );
  564. wfDebug( "WebRequest::getFileName() '" . $_FILES[$key]['name'] . "' normalized to '$name'\n" );
  565. return $name;
  566. }
  567. /**
  568. * Return a handle to WebResponse style object, for setting cookies,
  569. * headers and other stuff, for Request being worked on.
  570. */
  571. function response() {
  572. /* Lazy initialization of response object for this request */
  573. if (!is_object($this->_response)) {
  574. $this->_response = new WebResponse;
  575. }
  576. return $this->_response;
  577. }
  578. /**
  579. * Get a request header, or false if it isn't set
  580. * @param $name String: case-insensitive header name
  581. */
  582. function getHeader( $name ) {
  583. $name = strtoupper( $name );
  584. if ( function_exists( 'apache_request_headers' ) ) {
  585. if ( !isset( $this->headers ) ) {
  586. $this->headers = array();
  587. foreach ( apache_request_headers() as $tempName => $tempValue ) {
  588. $this->headers[ strtoupper( $tempName ) ] = $tempValue;
  589. }
  590. }
  591. if ( isset( $this->headers[$name] ) ) {
  592. return $this->headers[$name];
  593. } else {
  594. return false;
  595. }
  596. } else {
  597. $name = 'HTTP_' . str_replace( '-', '_', $name );
  598. if ( isset( $_SERVER[$name] ) ) {
  599. return $_SERVER[$name];
  600. } else {
  601. return false;
  602. }
  603. }
  604. }
  605. /*
  606. * Get data from $_SESSION
  607. */
  608. function getSessionData( $key ) {
  609. if( !isset( $_SESSION[$key] ) )
  610. return null;
  611. return $_SESSION[$key];
  612. }
  613. function setSessionData( $key, $data ) {
  614. $_SESSION[$key] = $data;
  615. }
  616. }
  617. /**
  618. * WebRequest clone which takes values from a provided array.
  619. *
  620. * @ingroup HTTP
  621. */
  622. class FauxRequest extends WebRequest {
  623. var $wasPosted = false;
  624. /**
  625. * @param $data Array of *non*-urlencoded key => value pairs, the
  626. * fake GET/POST values
  627. * @param $wasPosted Bool: whether to treat the data as POST
  628. */
  629. function FauxRequest( $data, $wasPosted = false, $session = null ) {
  630. if( is_array( $data ) ) {
  631. $this->data = $data;
  632. } else {
  633. throw new MWException( "FauxRequest() got bogus data" );
  634. }
  635. $this->wasPosted = $wasPosted;
  636. $this->headers = array();
  637. $this->session = $session ? $session : array();
  638. }
  639. function notImplemented( $method ) {
  640. throw new MWException( "{$method}() not implemented" );
  641. }
  642. function getText( $name, $default = '' ) {
  643. # Override; don't recode since we're using internal data
  644. return (string)$this->getVal( $name, $default );
  645. }
  646. function getValues() {
  647. return $this->data;
  648. }
  649. function wasPosted() {
  650. return $this->wasPosted;
  651. }
  652. function checkSessionCookie() {
  653. return false;
  654. }
  655. function getRequestURL() {
  656. $this->notImplemented( __METHOD__ );
  657. }
  658. function appendQuery( $query ) {
  659. $this->notImplemented( __METHOD__ );
  660. }
  661. function getHeader( $name ) {
  662. return isset( $this->headers[$name] ) ? $this->headers[$name] : false;
  663. }
  664. function getSessionData( $key ) {
  665. if( !isset( $this->session[$key] ) )
  666. return null;
  667. return $this->session[$key];
  668. }
  669. function setSessionData( $key, $data ) {
  670. $this->notImplemented( __METHOD__ );
  671. }
  672. }