WebRequest.php 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  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\MediaWikiServices;
  26. use MediaWiki\Session\Session;
  27. use MediaWiki\Session\SessionId;
  28. use MediaWiki\Session\SessionManager;
  29. /**
  30. * The WebRequest class encapsulates getting at data passed in the
  31. * URL or via a POSTed form stripping illegal input characters and
  32. * normalizing Unicode sequences.
  33. *
  34. * @ingroup HTTP
  35. */
  36. class WebRequest {
  37. protected $data, $headers = [];
  38. /**
  39. * Flag to make WebRequest::getHeader return an array of values.
  40. * @since 1.26
  41. */
  42. const GETHEADER_LIST = 1;
  43. /**
  44. * The unique request ID.
  45. * @var string
  46. */
  47. private static $reqId;
  48. /**
  49. * Lazy-init response object
  50. * @var WebResponse
  51. */
  52. private $response;
  53. /**
  54. * Cached client IP address
  55. * @var string
  56. */
  57. private $ip;
  58. /**
  59. * The timestamp of the start of the request, with microsecond precision.
  60. * @var float
  61. */
  62. protected $requestTime;
  63. /**
  64. * Cached URL protocol
  65. * @var string
  66. */
  67. protected $protocol;
  68. /**
  69. * @var SessionId|null Session ID to use for this
  70. * request. We can't save the session directly due to reference cycles not
  71. * working too well (slow GC in Zend and never collected in HHVM).
  72. */
  73. protected $sessionId = null;
  74. /** @var bool Whether this HTTP request is "safe" (even if it is an HTTP post) */
  75. protected $markedAsSafe = false;
  76. /**
  77. * @codeCoverageIgnore
  78. */
  79. public function __construct() {
  80. $this->requestTime = $_SERVER['REQUEST_TIME_FLOAT'];
  81. // POST overrides GET data
  82. // We don't use $_REQUEST here to avoid interference from cookies...
  83. $this->data = $_POST + $_GET;
  84. }
  85. /**
  86. * Extract relevant query arguments from the http request uri's path
  87. * to be merged with the normal php provided query arguments.
  88. * Tries to use the REQUEST_URI data if available and parses it
  89. * according to the wiki's configuration looking for any known pattern.
  90. *
  91. * If the REQUEST_URI is not provided we'll fall back on the PATH_INFO
  92. * provided by the server if any and use that to set a 'title' parameter.
  93. *
  94. * @param string $want If this is not 'all', then the function
  95. * will return an empty array if it determines that the URL is
  96. * inside a rewrite path.
  97. *
  98. * @return array Any query arguments found in path matches.
  99. */
  100. public static function getPathInfo( $want = 'all' ) {
  101. global $wgUsePathInfo;
  102. // PATH_INFO is mangled due to https://bugs.php.net/bug.php?id=31892
  103. // And also by Apache 2.x, double slashes are converted to single slashes.
  104. // So we will use REQUEST_URI if possible.
  105. $matches = [];
  106. if ( !empty( $_SERVER['REQUEST_URI'] ) ) {
  107. // Slurp out the path portion to examine...
  108. $url = $_SERVER['REQUEST_URI'];
  109. if ( !preg_match( '!^https?://!', $url ) ) {
  110. $url = 'http://unused' . $url;
  111. }
  112. Wikimedia\suppressWarnings();
  113. $a = parse_url( $url );
  114. Wikimedia\restoreWarnings();
  115. if ( $a ) {
  116. $path = isset( $a['path'] ) ? $a['path'] : '';
  117. global $wgScript;
  118. if ( $path == $wgScript && $want !== 'all' ) {
  119. // Script inside a rewrite path?
  120. // Abort to keep from breaking...
  121. return $matches;
  122. }
  123. $router = new PathRouter;
  124. // Raw PATH_INFO style
  125. $router->add( "$wgScript/$1" );
  126. if ( isset( $_SERVER['SCRIPT_NAME'] )
  127. && preg_match( '/\.php/', $_SERVER['SCRIPT_NAME'] )
  128. ) {
  129. # Check for SCRIPT_NAME, we handle index.php explicitly
  130. # But we do have some other .php files such as img_auth.php
  131. # Don't let root article paths clober the parsing for them
  132. $router->add( $_SERVER['SCRIPT_NAME'] . "/$1" );
  133. }
  134. global $wgArticlePath;
  135. if ( $wgArticlePath ) {
  136. $router->add( $wgArticlePath );
  137. }
  138. global $wgActionPaths;
  139. if ( $wgActionPaths ) {
  140. $router->add( $wgActionPaths, [ 'action' => '$key' ] );
  141. }
  142. global $wgVariantArticlePath, $wgContLang;
  143. if ( $wgVariantArticlePath ) {
  144. $router->add( $wgVariantArticlePath,
  145. [ 'variant' => '$2' ],
  146. [ '$2' => $wgContLang->getVariants() ]
  147. );
  148. }
  149. Hooks::run( 'WebRequestPathInfoRouter', [ $router ] );
  150. $matches = $router->parse( $path );
  151. }
  152. } elseif ( $wgUsePathInfo ) {
  153. if ( isset( $_SERVER['ORIG_PATH_INFO'] ) && $_SERVER['ORIG_PATH_INFO'] != '' ) {
  154. // Mangled PATH_INFO
  155. // https://bugs.php.net/bug.php?id=31892
  156. // Also reported when ini_get('cgi.fix_pathinfo')==false
  157. $matches['title'] = substr( $_SERVER['ORIG_PATH_INFO'], 1 );
  158. } elseif ( isset( $_SERVER['PATH_INFO'] ) && $_SERVER['PATH_INFO'] != '' ) {
  159. // Regular old PATH_INFO yay
  160. $matches['title'] = substr( $_SERVER['PATH_INFO'], 1 );
  161. }
  162. }
  163. return $matches;
  164. }
  165. /**
  166. * Work out an appropriate URL prefix containing scheme and host, based on
  167. * information detected from $_SERVER
  168. *
  169. * @return string
  170. */
  171. public static function detectServer() {
  172. global $wgAssumeProxiesUseDefaultProtocolPorts;
  173. $proto = self::detectProtocol();
  174. $stdPort = $proto === 'https' ? 443 : 80;
  175. $varNames = [ 'HTTP_HOST', 'SERVER_NAME', 'HOSTNAME', 'SERVER_ADDR' ];
  176. $host = 'localhost';
  177. $port = $stdPort;
  178. foreach ( $varNames as $varName ) {
  179. if ( !isset( $_SERVER[$varName] ) ) {
  180. continue;
  181. }
  182. $parts = IP::splitHostAndPort( $_SERVER[$varName] );
  183. if ( !$parts ) {
  184. // Invalid, do not use
  185. continue;
  186. }
  187. $host = $parts[0];
  188. if ( $wgAssumeProxiesUseDefaultProtocolPorts && isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) ) {
  189. // T72021: Assume that upstream proxy is running on the default
  190. // port based on the protocol. We have no reliable way to determine
  191. // the actual port in use upstream.
  192. $port = $stdPort;
  193. } elseif ( $parts[1] === false ) {
  194. if ( isset( $_SERVER['SERVER_PORT'] ) ) {
  195. $port = $_SERVER['SERVER_PORT'];
  196. } // else leave it as $stdPort
  197. } else {
  198. $port = $parts[1];
  199. }
  200. break;
  201. }
  202. return $proto . '://' . IP::combineHostAndPort( $host, $port, $stdPort );
  203. }
  204. /**
  205. * Detect the protocol from $_SERVER.
  206. * This is for use prior to Setup.php, when no WebRequest object is available.
  207. * At other times, use the non-static function getProtocol().
  208. *
  209. * @return string
  210. */
  211. public static function detectProtocol() {
  212. if ( ( !empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] !== 'off' ) ||
  213. ( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) &&
  214. $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' ) ) {
  215. return 'https';
  216. } else {
  217. return 'http';
  218. }
  219. }
  220. /**
  221. * Get the number of seconds to have elapsed since request start,
  222. * in fractional seconds, with microsecond resolution.
  223. *
  224. * @return float
  225. * @since 1.25
  226. */
  227. public function getElapsedTime() {
  228. return microtime( true ) - $this->requestTime;
  229. }
  230. /**
  231. * Get the unique request ID.
  232. * This is either the value of the UNIQUE_ID envvar (if present) or a
  233. * randomly-generated 24-character string.
  234. *
  235. * @return string
  236. * @since 1.27
  237. */
  238. public static function getRequestId() {
  239. // This method is called from various error handlers and should be kept simple.
  240. if ( !self::$reqId ) {
  241. self::$reqId = isset( $_SERVER['UNIQUE_ID'] )
  242. ? $_SERVER['UNIQUE_ID'] : wfRandomString( 24 );
  243. }
  244. return self::$reqId;
  245. }
  246. /**
  247. * Override the unique request ID. This is for sub-requests, such as jobs,
  248. * that wish to use the same id but are not part of the same execution context.
  249. *
  250. * @param string $id
  251. * @since 1.27
  252. */
  253. public static function overrideRequestId( $id ) {
  254. self::$reqId = $id;
  255. }
  256. /**
  257. * Get the current URL protocol (http or https)
  258. * @return string
  259. */
  260. public function getProtocol() {
  261. if ( $this->protocol === null ) {
  262. $this->protocol = self::detectProtocol();
  263. }
  264. return $this->protocol;
  265. }
  266. /**
  267. * Check for title, action, and/or variant data in the URL
  268. * and interpolate it into the GET variables.
  269. * This should only be run after $wgContLang is available,
  270. * as we may need the list of language variants to determine
  271. * available variant URLs.
  272. */
  273. public function interpolateTitle() {
  274. // T18019: title interpolation on API queries is useless and sometimes harmful
  275. if ( defined( 'MW_API' ) ) {
  276. return;
  277. }
  278. $matches = self::getPathInfo( 'title' );
  279. foreach ( $matches as $key => $val ) {
  280. $this->data[$key] = $_GET[$key] = $_REQUEST[$key] = $val;
  281. }
  282. }
  283. /**
  284. * URL rewriting function; tries to extract page title and,
  285. * optionally, one other fixed parameter value from a URL path.
  286. *
  287. * @param string $path The URL path given from the client
  288. * @param array $bases One or more URLs, optionally with $1 at the end
  289. * @param string|bool $key If provided, the matching key in $bases will be
  290. * passed on as the value of this URL parameter
  291. * @return array Array of URL variables to interpolate; empty if no match
  292. */
  293. static function extractTitle( $path, $bases, $key = false ) {
  294. foreach ( (array)$bases as $keyValue => $base ) {
  295. // Find the part after $wgArticlePath
  296. $base = str_replace( '$1', '', $base );
  297. $baseLen = strlen( $base );
  298. if ( substr( $path, 0, $baseLen ) == $base ) {
  299. $raw = substr( $path, $baseLen );
  300. if ( $raw !== '' ) {
  301. $matches = [ 'title' => rawurldecode( $raw ) ];
  302. if ( $key ) {
  303. $matches[$key] = $keyValue;
  304. }
  305. return $matches;
  306. }
  307. }
  308. }
  309. return [];
  310. }
  311. /**
  312. * Recursively normalizes UTF-8 strings in the given array.
  313. *
  314. * @param string|array $data
  315. * @return array|string Cleaned-up version of the given
  316. * @private
  317. */
  318. public function normalizeUnicode( $data ) {
  319. if ( is_array( $data ) ) {
  320. foreach ( $data as $key => $val ) {
  321. $data[$key] = $this->normalizeUnicode( $val );
  322. }
  323. } else {
  324. global $wgContLang;
  325. $data = isset( $wgContLang ) ?
  326. $wgContLang->normalize( $data ) :
  327. UtfNormal\Validator::cleanUp( $data );
  328. }
  329. return $data;
  330. }
  331. /**
  332. * Fetch a value from the given array or return $default if it's not set.
  333. *
  334. * @param array $arr
  335. * @param string $name
  336. * @param mixed $default
  337. * @return mixed
  338. */
  339. private function getGPCVal( $arr, $name, $default ) {
  340. # PHP is so nice to not touch input data, except sometimes:
  341. # https://secure.php.net/variables.external#language.variables.external.dot-in-names
  342. # Work around PHP *feature* to avoid *bugs* elsewhere.
  343. $name = strtr( $name, '.', '_' );
  344. if ( isset( $arr[$name] ) ) {
  345. global $wgContLang;
  346. $data = $arr[$name];
  347. if ( isset( $_GET[$name] ) && !is_array( $data ) ) {
  348. # Check for alternate/legacy character encoding.
  349. if ( isset( $wgContLang ) ) {
  350. $data = $wgContLang->checkTitleEncoding( $data );
  351. }
  352. }
  353. $data = $this->normalizeUnicode( $data );
  354. return $data;
  355. } else {
  356. return $default;
  357. }
  358. }
  359. /**
  360. * Fetch a scalar from the input without normalization, or return $default
  361. * if it's not set.
  362. *
  363. * Unlike self::getVal(), this does not perform any normalization on the
  364. * input value.
  365. *
  366. * @since 1.28
  367. * @param string $name
  368. * @param string|null $default
  369. * @return string|null
  370. */
  371. public function getRawVal( $name, $default = null ) {
  372. $name = strtr( $name, '.', '_' ); // See comment in self::getGPCVal()
  373. if ( isset( $this->data[$name] ) && !is_array( $this->data[$name] ) ) {
  374. $val = $this->data[$name];
  375. } else {
  376. $val = $default;
  377. }
  378. if ( is_null( $val ) ) {
  379. return $val;
  380. } else {
  381. return (string)$val;
  382. }
  383. }
  384. /**
  385. * Fetch a scalar from the input or return $default if it's not set.
  386. * Returns a string. Arrays are discarded. Useful for
  387. * non-freeform text inputs (e.g. predefined internal text keys
  388. * selected by a drop-down menu). For freeform input, see getText().
  389. *
  390. * @param string $name
  391. * @param string|null $default Optional default (or null)
  392. * @return string|null
  393. */
  394. public function getVal( $name, $default = null ) {
  395. $val = $this->getGPCVal( $this->data, $name, $default );
  396. if ( is_array( $val ) ) {
  397. $val = $default;
  398. }
  399. if ( is_null( $val ) ) {
  400. return $val;
  401. } else {
  402. return (string)$val;
  403. }
  404. }
  405. /**
  406. * Set an arbitrary value into our get/post data.
  407. *
  408. * @param string $key Key name to use
  409. * @param mixed $value Value to set
  410. * @return mixed Old value if one was present, null otherwise
  411. */
  412. public function setVal( $key, $value ) {
  413. $ret = isset( $this->data[$key] ) ? $this->data[$key] : null;
  414. $this->data[$key] = $value;
  415. return $ret;
  416. }
  417. /**
  418. * Unset an arbitrary value from our get/post data.
  419. *
  420. * @param string $key Key name to use
  421. * @return mixed Old value if one was present, null otherwise
  422. */
  423. public function unsetVal( $key ) {
  424. if ( !isset( $this->data[$key] ) ) {
  425. $ret = null;
  426. } else {
  427. $ret = $this->data[$key];
  428. unset( $this->data[$key] );
  429. }
  430. return $ret;
  431. }
  432. /**
  433. * Fetch an array from the input or return $default if it's not set.
  434. * If source was scalar, will return an array with a single element.
  435. * If no source and no default, returns null.
  436. *
  437. * @param string $name
  438. * @param array $default Optional default (or null)
  439. * @return array|null
  440. */
  441. public function getArray( $name, $default = null ) {
  442. $val = $this->getGPCVal( $this->data, $name, $default );
  443. if ( is_null( $val ) ) {
  444. return null;
  445. } else {
  446. return (array)$val;
  447. }
  448. }
  449. /**
  450. * Fetch an array of integers, or return $default if it's not set.
  451. * If source was scalar, will return an array with a single element.
  452. * If no source and no default, returns null.
  453. * If an array is returned, contents are guaranteed to be integers.
  454. *
  455. * @param string $name
  456. * @param array $default Option default (or null)
  457. * @return array Array of ints
  458. */
  459. public function getIntArray( $name, $default = null ) {
  460. $val = $this->getArray( $name, $default );
  461. if ( is_array( $val ) ) {
  462. $val = array_map( 'intval', $val );
  463. }
  464. return $val;
  465. }
  466. /**
  467. * Fetch an integer value from the input or return $default if not set.
  468. * Guaranteed to return an integer; non-numeric input will typically
  469. * return 0.
  470. *
  471. * @param string $name
  472. * @param int $default
  473. * @return int
  474. */
  475. public function getInt( $name, $default = 0 ) {
  476. return intval( $this->getRawVal( $name, $default ) );
  477. }
  478. /**
  479. * Fetch an integer value from the input or return null if empty.
  480. * Guaranteed to return an integer or null; non-numeric input will
  481. * typically return null.
  482. *
  483. * @param string $name
  484. * @return int|null
  485. */
  486. public function getIntOrNull( $name ) {
  487. $val = $this->getRawVal( $name );
  488. return is_numeric( $val )
  489. ? intval( $val )
  490. : null;
  491. }
  492. /**
  493. * Fetch a floating point value from the input or return $default if not set.
  494. * Guaranteed to return a float; non-numeric input will typically
  495. * return 0.
  496. *
  497. * @since 1.23
  498. * @param string $name
  499. * @param float $default
  500. * @return float
  501. */
  502. public function getFloat( $name, $default = 0.0 ) {
  503. return floatval( $this->getRawVal( $name, $default ) );
  504. }
  505. /**
  506. * Fetch a boolean value from the input or return $default if not set.
  507. * Guaranteed to return true or false, with normal PHP semantics for
  508. * boolean interpretation of strings.
  509. *
  510. * @param string $name
  511. * @param bool $default
  512. * @return bool
  513. */
  514. public function getBool( $name, $default = false ) {
  515. return (bool)$this->getRawVal( $name, $default );
  516. }
  517. /**
  518. * Fetch a boolean value from the input or return $default if not set.
  519. * Unlike getBool, the string "false" will result in boolean false, which is
  520. * useful when interpreting information sent from JavaScript.
  521. *
  522. * @param string $name
  523. * @param bool $default
  524. * @return bool
  525. */
  526. public function getFuzzyBool( $name, $default = false ) {
  527. return $this->getBool( $name, $default )
  528. && strcasecmp( $this->getRawVal( $name ), 'false' ) !== 0;
  529. }
  530. /**
  531. * Return true if the named value is set in the input, whatever that
  532. * value is (even "0"). Return false if the named value is not set.
  533. * Example use is checking for the presence of check boxes in forms.
  534. *
  535. * @param string $name
  536. * @return bool
  537. */
  538. public function getCheck( $name ) {
  539. # Checkboxes and buttons are only present when clicked
  540. # Presence connotes truth, absence false
  541. return $this->getRawVal( $name, null ) !== null;
  542. }
  543. /**
  544. * Fetch a text string from the given array or return $default if it's not
  545. * set. Carriage returns are stripped from the text. This should generally
  546. * be used for form "<textarea>" and "<input>" fields, and for
  547. * user-supplied freeform text input.
  548. *
  549. * @param string $name
  550. * @param string $default Optional
  551. * @return string
  552. */
  553. public function getText( $name, $default = '' ) {
  554. $val = $this->getVal( $name, $default );
  555. return str_replace( "\r\n", "\n", $val );
  556. }
  557. /**
  558. * Extracts the given named values into an array.
  559. * If no arguments are given, returns all input values.
  560. * No transformation is performed on the values.
  561. *
  562. * @return array
  563. */
  564. public function getValues() {
  565. $names = func_get_args();
  566. if ( count( $names ) == 0 ) {
  567. $names = array_keys( $this->data );
  568. }
  569. $retVal = [];
  570. foreach ( $names as $name ) {
  571. $value = $this->getGPCVal( $this->data, $name, null );
  572. if ( !is_null( $value ) ) {
  573. $retVal[$name] = $value;
  574. }
  575. }
  576. return $retVal;
  577. }
  578. /**
  579. * Returns the names of all input values excluding those in $exclude.
  580. *
  581. * @param array $exclude
  582. * @return array
  583. */
  584. public function getValueNames( $exclude = [] ) {
  585. return array_diff( array_keys( $this->getValues() ), $exclude );
  586. }
  587. /**
  588. * Get the values passed in the query string.
  589. * No transformation is performed on the values.
  590. *
  591. * @codeCoverageIgnore
  592. * @return array
  593. */
  594. public function getQueryValues() {
  595. return $_GET;
  596. }
  597. /**
  598. * Return the contents of the Query with no decoding. Use when you need to
  599. * know exactly what was sent, e.g. for an OAuth signature over the elements.
  600. *
  601. * @codeCoverageIgnore
  602. * @return string
  603. */
  604. public function getRawQueryString() {
  605. return $_SERVER['QUERY_STRING'];
  606. }
  607. /**
  608. * Return the contents of the POST with no decoding. Use when you need to
  609. * know exactly what was sent, e.g. for an OAuth signature over the elements.
  610. *
  611. * @return string
  612. */
  613. public function getRawPostString() {
  614. if ( !$this->wasPosted() ) {
  615. return '';
  616. }
  617. return $this->getRawInput();
  618. }
  619. /**
  620. * Return the raw request body, with no processing. Cached since some methods
  621. * disallow reading the stream more than once. As stated in the php docs, this
  622. * does not work with enctype="multipart/form-data".
  623. *
  624. * @return string
  625. */
  626. public function getRawInput() {
  627. static $input = null;
  628. if ( $input === null ) {
  629. $input = file_get_contents( 'php://input' );
  630. }
  631. return $input;
  632. }
  633. /**
  634. * Get the HTTP method used for this request.
  635. *
  636. * @return string
  637. */
  638. public function getMethod() {
  639. return isset( $_SERVER['REQUEST_METHOD'] ) ? $_SERVER['REQUEST_METHOD'] : 'GET';
  640. }
  641. /**
  642. * Returns true if the present request was reached by a POST operation,
  643. * false otherwise (GET, HEAD, or command-line).
  644. *
  645. * Note that values retrieved by the object may come from the
  646. * GET URL etc even on a POST request.
  647. *
  648. * @return bool
  649. */
  650. public function wasPosted() {
  651. return $this->getMethod() == 'POST';
  652. }
  653. /**
  654. * Return the session for this request
  655. *
  656. * This might unpersist an existing session if it was invalid.
  657. *
  658. * @since 1.27
  659. * @note For performance, keep the session locally if you will be making
  660. * much use of it instead of calling this method repeatedly.
  661. * @return Session
  662. */
  663. public function getSession() {
  664. if ( $this->sessionId !== null ) {
  665. $session = SessionManager::singleton()->getSessionById( (string)$this->sessionId, true, $this );
  666. if ( $session ) {
  667. return $session;
  668. }
  669. }
  670. $session = SessionManager::singleton()->getSessionForRequest( $this );
  671. $this->sessionId = $session->getSessionId();
  672. return $session;
  673. }
  674. /**
  675. * Set the session for this request
  676. * @since 1.27
  677. * @private For use by MediaWiki\Session classes only
  678. * @param SessionId $sessionId
  679. */
  680. public function setSessionId( SessionId $sessionId ) {
  681. $this->sessionId = $sessionId;
  682. }
  683. /**
  684. * Get the session id for this request, if any
  685. * @since 1.27
  686. * @private For use by MediaWiki\Session classes only
  687. * @return SessionId|null
  688. */
  689. public function getSessionId() {
  690. return $this->sessionId;
  691. }
  692. /**
  693. * Get a cookie from the $_COOKIE jar
  694. *
  695. * @param string $key The name of the cookie
  696. * @param string $prefix A prefix to use for the cookie name, if not $wgCookiePrefix
  697. * @param mixed $default What to return if the value isn't found
  698. * @return mixed Cookie value or $default if the cookie not set
  699. */
  700. public function getCookie( $key, $prefix = null, $default = null ) {
  701. if ( $prefix === null ) {
  702. global $wgCookiePrefix;
  703. $prefix = $wgCookiePrefix;
  704. }
  705. return $this->getGPCVal( $_COOKIE, $prefix . $key, $default );
  706. }
  707. /**
  708. * Return the path and query string portion of the main request URI.
  709. * This will be suitable for use as a relative link in HTML output.
  710. *
  711. * @throws MWException
  712. * @return string
  713. */
  714. public static function getGlobalRequestURL() {
  715. // This method is called on fatal errors; it should not depend on anything complex.
  716. if ( isset( $_SERVER['REQUEST_URI'] ) && strlen( $_SERVER['REQUEST_URI'] ) ) {
  717. $base = $_SERVER['REQUEST_URI'];
  718. } elseif ( isset( $_SERVER['HTTP_X_ORIGINAL_URL'] )
  719. && strlen( $_SERVER['HTTP_X_ORIGINAL_URL'] )
  720. ) {
  721. // Probably IIS; doesn't set REQUEST_URI
  722. $base = $_SERVER['HTTP_X_ORIGINAL_URL'];
  723. } elseif ( isset( $_SERVER['SCRIPT_NAME'] ) ) {
  724. $base = $_SERVER['SCRIPT_NAME'];
  725. if ( isset( $_SERVER['QUERY_STRING'] ) && $_SERVER['QUERY_STRING'] != '' ) {
  726. $base .= '?' . $_SERVER['QUERY_STRING'];
  727. }
  728. } else {
  729. // This shouldn't happen!
  730. throw new MWException( "Web server doesn't provide either " .
  731. "REQUEST_URI, HTTP_X_ORIGINAL_URL or SCRIPT_NAME. Report details " .
  732. "of your web server configuration to https://phabricator.wikimedia.org/" );
  733. }
  734. // User-agents should not send a fragment with the URI, but
  735. // if they do, and the web server passes it on to us, we
  736. // need to strip it or we get false-positive redirect loops
  737. // or weird output URLs
  738. $hash = strpos( $base, '#' );
  739. if ( $hash !== false ) {
  740. $base = substr( $base, 0, $hash );
  741. }
  742. if ( $base[0] == '/' ) {
  743. // More than one slash will look like it is protocol relative
  744. return preg_replace( '!^/+!', '/', $base );
  745. } else {
  746. // We may get paths with a host prepended; strip it.
  747. return preg_replace( '!^[^:]+://[^/]+/+!', '/', $base );
  748. }
  749. }
  750. /**
  751. * Return the path and query string portion of the request URI.
  752. * This will be suitable for use as a relative link in HTML output.
  753. *
  754. * @throws MWException
  755. * @return string
  756. */
  757. public function getRequestURL() {
  758. return self::getGlobalRequestURL();
  759. }
  760. /**
  761. * Return the request URI with the canonical service and hostname, path,
  762. * and query string. This will be suitable for use as an absolute link
  763. * in HTML or other output.
  764. *
  765. * If $wgServer is protocol-relative, this will return a fully
  766. * qualified URL with the protocol that was used for this request.
  767. *
  768. * @return string
  769. */
  770. public function getFullRequestURL() {
  771. return wfExpandUrl( $this->getRequestURL(), PROTO_CURRENT );
  772. }
  773. /**
  774. * @param string $key
  775. * @param string $value
  776. * @return string
  777. */
  778. public function appendQueryValue( $key, $value ) {
  779. return $this->appendQueryArray( [ $key => $value ] );
  780. }
  781. /**
  782. * Appends or replaces value of query variables.
  783. *
  784. * @param array $array Array of values to replace/add to query
  785. * @return string
  786. */
  787. public function appendQueryArray( $array ) {
  788. $newquery = $this->getQueryValues();
  789. unset( $newquery['title'] );
  790. $newquery = array_merge( $newquery, $array );
  791. return wfArrayToCgi( $newquery );
  792. }
  793. /**
  794. * Check for limit and offset parameters on the input, and return sensible
  795. * defaults if not given. The limit must be positive and is capped at 5000.
  796. * Offset must be positive but is not capped.
  797. *
  798. * @param int $deflimit Limit to use if no input and the user hasn't set the option.
  799. * @param string $optionname To specify an option other than rclimit to pull from.
  800. * @return int[] First element is limit, second is offset
  801. */
  802. public function getLimitOffset( $deflimit = 50, $optionname = 'rclimit' ) {
  803. global $wgUser;
  804. $limit = $this->getInt( 'limit', 0 );
  805. if ( $limit < 0 ) {
  806. $limit = 0;
  807. }
  808. if ( ( $limit == 0 ) && ( $optionname != '' ) ) {
  809. $limit = $wgUser->getIntOption( $optionname );
  810. }
  811. if ( $limit <= 0 ) {
  812. $limit = $deflimit;
  813. }
  814. if ( $limit > 5000 ) {
  815. $limit = 5000; # We have *some* limits...
  816. }
  817. $offset = $this->getInt( 'offset', 0 );
  818. if ( $offset < 0 ) {
  819. $offset = 0;
  820. }
  821. return [ $limit, $offset ];
  822. }
  823. /**
  824. * Return the path to the temporary file where PHP has stored the upload.
  825. *
  826. * @param string $key
  827. * @return string|null String or null if no such file.
  828. */
  829. public function getFileTempname( $key ) {
  830. $file = new WebRequestUpload( $this, $key );
  831. return $file->getTempName();
  832. }
  833. /**
  834. * Return the upload error or 0
  835. *
  836. * @param string $key
  837. * @return int
  838. */
  839. public function getUploadError( $key ) {
  840. $file = new WebRequestUpload( $this, $key );
  841. return $file->getError();
  842. }
  843. /**
  844. * Return the original filename of the uploaded file, as reported by
  845. * the submitting user agent. HTML-style character entities are
  846. * interpreted and normalized to Unicode normalization form C, in part
  847. * to deal with weird input from Safari with non-ASCII filenames.
  848. *
  849. * Other than this the name is not verified for being a safe filename.
  850. *
  851. * @param string $key
  852. * @return string|null String or null if no such file.
  853. */
  854. public function getFileName( $key ) {
  855. $file = new WebRequestUpload( $this, $key );
  856. return $file->getName();
  857. }
  858. /**
  859. * Return a WebRequestUpload object corresponding to the key
  860. *
  861. * @param string $key
  862. * @return WebRequestUpload
  863. */
  864. public function getUpload( $key ) {
  865. return new WebRequestUpload( $this, $key );
  866. }
  867. /**
  868. * Return a handle to WebResponse style object, for setting cookies,
  869. * headers and other stuff, for Request being worked on.
  870. *
  871. * @return WebResponse
  872. */
  873. public function response() {
  874. /* Lazy initialization of response object for this request */
  875. if ( !is_object( $this->response ) ) {
  876. $class = ( $this instanceof FauxRequest ) ? FauxResponse::class : WebResponse::class;
  877. $this->response = new $class();
  878. }
  879. return $this->response;
  880. }
  881. /**
  882. * Initialise the header list
  883. */
  884. protected function initHeaders() {
  885. if ( count( $this->headers ) ) {
  886. return;
  887. }
  888. $apacheHeaders = function_exists( 'apache_request_headers' ) ? apache_request_headers() : false;
  889. if ( $apacheHeaders ) {
  890. foreach ( $apacheHeaders as $tempName => $tempValue ) {
  891. $this->headers[strtoupper( $tempName )] = $tempValue;
  892. }
  893. } else {
  894. foreach ( $_SERVER as $name => $value ) {
  895. if ( substr( $name, 0, 5 ) === 'HTTP_' ) {
  896. $name = str_replace( '_', '-', substr( $name, 5 ) );
  897. $this->headers[$name] = $value;
  898. } elseif ( $name === 'CONTENT_LENGTH' ) {
  899. $this->headers['CONTENT-LENGTH'] = $value;
  900. }
  901. }
  902. }
  903. }
  904. /**
  905. * Get an array containing all request headers
  906. *
  907. * @return array Mapping header name to its value
  908. */
  909. public function getAllHeaders() {
  910. $this->initHeaders();
  911. return $this->headers;
  912. }
  913. /**
  914. * Get a request header, or false if it isn't set.
  915. *
  916. * @param string $name Case-insensitive header name
  917. * @param int $flags Bitwise combination of:
  918. * WebRequest::GETHEADER_LIST Treat the header as a comma-separated list
  919. * of values, as described in RFC 2616 § 4.2.
  920. * (since 1.26).
  921. * @return string|array|bool False if header is unset; otherwise the
  922. * header value(s) as either a string (the default) or an array, if
  923. * WebRequest::GETHEADER_LIST flag was set.
  924. */
  925. public function getHeader( $name, $flags = 0 ) {
  926. $this->initHeaders();
  927. $name = strtoupper( $name );
  928. if ( !isset( $this->headers[$name] ) ) {
  929. return false;
  930. }
  931. $value = $this->headers[$name];
  932. if ( $flags & self::GETHEADER_LIST ) {
  933. $value = array_map( 'trim', explode( ',', $value ) );
  934. }
  935. return $value;
  936. }
  937. /**
  938. * Get data from the session
  939. *
  940. * @note Prefer $this->getSession() instead if making multiple calls.
  941. * @param string $key Name of key in the session
  942. * @return mixed
  943. */
  944. public function getSessionData( $key ) {
  945. return $this->getSession()->get( $key );
  946. }
  947. /**
  948. * Set session data
  949. *
  950. * @note Prefer $this->getSession() instead if making multiple calls.
  951. * @param string $key Name of key in the session
  952. * @param mixed $data
  953. */
  954. public function setSessionData( $key, $data ) {
  955. $this->getSession()->set( $key, $data );
  956. }
  957. /**
  958. * Check if Internet Explorer will detect an incorrect cache extension in
  959. * PATH_INFO or QUERY_STRING. If the request can't be allowed, show an error
  960. * message or redirect to a safer URL. Returns true if the URL is OK, and
  961. * false if an error message has been shown and the request should be aborted.
  962. *
  963. * @param array $extWhitelist
  964. * @throws HttpError
  965. * @return bool
  966. */
  967. public function checkUrlExtension( $extWhitelist = [] ) {
  968. $extWhitelist[] = 'php';
  969. if ( IEUrlExtension::areServerVarsBad( $_SERVER, $extWhitelist ) ) {
  970. if ( !$this->wasPosted() ) {
  971. $newUrl = IEUrlExtension::fixUrlForIE6(
  972. $this->getFullRequestURL(), $extWhitelist );
  973. if ( $newUrl !== false ) {
  974. $this->doSecurityRedirect( $newUrl );
  975. return false;
  976. }
  977. }
  978. throw new HttpError( 403,
  979. 'Invalid file extension found in the path info or query string.' );
  980. }
  981. return true;
  982. }
  983. /**
  984. * Attempt to redirect to a URL with a QUERY_STRING that's not dangerous in
  985. * IE 6. Returns true if it was successful, false otherwise.
  986. *
  987. * @param string $url
  988. * @return bool
  989. */
  990. protected function doSecurityRedirect( $url ) {
  991. header( 'Location: ' . $url );
  992. header( 'Content-Type: text/html' );
  993. $encUrl = htmlspecialchars( $url );
  994. echo <<<HTML
  995. <!DOCTYPE html>
  996. <html>
  997. <head>
  998. <title>Security redirect</title>
  999. </head>
  1000. <body>
  1001. <h1>Security redirect</h1>
  1002. <p>
  1003. We can't serve non-HTML content from the URL you have requested, because
  1004. Internet Explorer would interpret it as an incorrect and potentially dangerous
  1005. content type.</p>
  1006. <p>Instead, please use <a href="$encUrl">this URL</a>, which is the same as the
  1007. URL you have requested, except that "&amp;*" is appended. This prevents Internet
  1008. Explorer from seeing a bogus file extension.
  1009. </p>
  1010. </body>
  1011. </html>
  1012. HTML;
  1013. echo "\n";
  1014. return true;
  1015. }
  1016. /**
  1017. * Parse the Accept-Language header sent by the client into an array
  1018. *
  1019. * @return array Array( languageCode => q-value ) sorted by q-value in
  1020. * descending order then appearing time in the header in ascending order.
  1021. * May contain the "language" '*', which applies to languages other than those explicitly listed.
  1022. * This is aligned with rfc2616 section 14.4
  1023. * Preference for earlier languages appears in rfc3282 as an extension to HTTP/1.1.
  1024. */
  1025. public function getAcceptLang() {
  1026. // Modified version of code found at
  1027. // http://www.thefutureoftheweb.com/blog/use-accept-language-header
  1028. $acceptLang = $this->getHeader( 'Accept-Language' );
  1029. if ( !$acceptLang ) {
  1030. return [];
  1031. }
  1032. // Return the language codes in lower case
  1033. $acceptLang = strtolower( $acceptLang );
  1034. // Break up string into pieces (languages and q factors)
  1035. $lang_parse = null;
  1036. preg_match_all(
  1037. '/([a-z]{1,8}(-[a-z]{1,8})*|\*)\s*(;\s*q\s*=\s*(1(\.0{0,3})?|0(\.[0-9]{0,3})?)?)?/',
  1038. $acceptLang,
  1039. $lang_parse
  1040. );
  1041. if ( !count( $lang_parse[1] ) ) {
  1042. return [];
  1043. }
  1044. $langcodes = $lang_parse[1];
  1045. $qvalues = $lang_parse[4];
  1046. $indices = range( 0, count( $lang_parse[1] ) - 1 );
  1047. // Set default q factor to 1
  1048. foreach ( $indices as $index ) {
  1049. if ( $qvalues[$index] === '' ) {
  1050. $qvalues[$index] = 1;
  1051. } elseif ( $qvalues[$index] == 0 ) {
  1052. unset( $langcodes[$index], $qvalues[$index], $indices[$index] );
  1053. }
  1054. }
  1055. // Sort list. First by $qvalues, then by order. Reorder $langcodes the same way
  1056. array_multisort( $qvalues, SORT_DESC, SORT_NUMERIC, $indices, $langcodes );
  1057. // Create a list like "en" => 0.8
  1058. $langs = array_combine( $langcodes, $qvalues );
  1059. return $langs;
  1060. }
  1061. /**
  1062. * Fetch the raw IP from the request
  1063. *
  1064. * @since 1.19
  1065. *
  1066. * @throws MWException
  1067. * @return string
  1068. */
  1069. protected function getRawIP() {
  1070. if ( !isset( $_SERVER['REMOTE_ADDR'] ) ) {
  1071. return null;
  1072. }
  1073. if ( is_array( $_SERVER['REMOTE_ADDR'] ) || strpos( $_SERVER['REMOTE_ADDR'], ',' ) !== false ) {
  1074. throw new MWException( __METHOD__
  1075. . " : Could not determine the remote IP address due to multiple values." );
  1076. } else {
  1077. $ipchain = $_SERVER['REMOTE_ADDR'];
  1078. }
  1079. return IP::canonicalize( $ipchain );
  1080. }
  1081. /**
  1082. * Work out the IP address based on various globals
  1083. * For trusted proxies, use the XFF client IP (first of the chain)
  1084. *
  1085. * @since 1.19
  1086. *
  1087. * @throws MWException
  1088. * @return string
  1089. */
  1090. public function getIP() {
  1091. global $wgUsePrivateIPs;
  1092. # Return cached result
  1093. if ( $this->ip !== null ) {
  1094. return $this->ip;
  1095. }
  1096. # collect the originating ips
  1097. $ip = $this->getRawIP();
  1098. if ( !$ip ) {
  1099. throw new MWException( 'Unable to determine IP.' );
  1100. }
  1101. # Append XFF
  1102. $forwardedFor = $this->getHeader( 'X-Forwarded-For' );
  1103. if ( $forwardedFor !== false ) {
  1104. $proxyLookup = MediaWikiServices::getInstance()->getProxyLookup();
  1105. $isConfigured = $proxyLookup->isConfiguredProxy( $ip );
  1106. $ipchain = array_map( 'trim', explode( ',', $forwardedFor ) );
  1107. $ipchain = array_reverse( $ipchain );
  1108. array_unshift( $ipchain, $ip );
  1109. # Step through XFF list and find the last address in the list which is a
  1110. # trusted server. Set $ip to the IP address given by that trusted server,
  1111. # unless the address is not sensible (e.g. private). However, prefer private
  1112. # IP addresses over proxy servers controlled by this site (more sensible).
  1113. # Note that some XFF values might be "unknown" with Squid/Varnish.
  1114. foreach ( $ipchain as $i => $curIP ) {
  1115. $curIP = IP::sanitizeIP( IP::canonicalize( $curIP ) );
  1116. if ( !$curIP || !isset( $ipchain[$i + 1] ) || $ipchain[$i + 1] === 'unknown'
  1117. || !$proxyLookup->isTrustedProxy( $curIP )
  1118. ) {
  1119. break; // IP is not valid/trusted or does not point to anything
  1120. }
  1121. if (
  1122. IP::isPublic( $ipchain[$i + 1] ) ||
  1123. $wgUsePrivateIPs ||
  1124. $proxyLookup->isConfiguredProxy( $curIP ) // T50919; treat IP as sane
  1125. ) {
  1126. // Follow the next IP according to the proxy
  1127. $nextIP = IP::canonicalize( $ipchain[$i + 1] );
  1128. if ( !$nextIP && $isConfigured ) {
  1129. // We have not yet made it past CDN/proxy servers of this site,
  1130. // so either they are misconfigured or there is some IP spoofing.
  1131. throw new MWException( "Invalid IP given in XFF '$forwardedFor'." );
  1132. }
  1133. $ip = $nextIP;
  1134. // keep traversing the chain
  1135. continue;
  1136. }
  1137. break;
  1138. }
  1139. }
  1140. # Allow extensions to improve our guess
  1141. Hooks::run( 'GetIP', [ &$ip ] );
  1142. if ( !$ip ) {
  1143. throw new MWException( "Unable to determine IP." );
  1144. }
  1145. wfDebug( "IP: $ip\n" );
  1146. $this->ip = $ip;
  1147. return $ip;
  1148. }
  1149. /**
  1150. * @param string $ip
  1151. * @return void
  1152. * @since 1.21
  1153. */
  1154. public function setIP( $ip ) {
  1155. $this->ip = $ip;
  1156. }
  1157. /**
  1158. * Check if this request uses a "safe" HTTP method
  1159. *
  1160. * Safe methods are verbs (e.g. GET/HEAD/OPTIONS) used for obtaining content. Such requests
  1161. * are not expected to mutate content, especially in ways attributable to the client. Verbs
  1162. * like POST and PUT are typical of non-safe requests which often change content.
  1163. *
  1164. * @return bool
  1165. * @see https://tools.ietf.org/html/rfc7231#section-4.2.1
  1166. * @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
  1167. * @since 1.28
  1168. */
  1169. public function hasSafeMethod() {
  1170. if ( !isset( $_SERVER['REQUEST_METHOD'] ) ) {
  1171. return false; // CLI mode
  1172. }
  1173. return in_array( $_SERVER['REQUEST_METHOD'], [ 'GET', 'HEAD', 'OPTIONS', 'TRACE' ] );
  1174. }
  1175. /**
  1176. * Whether this request should be identified as being "safe"
  1177. *
  1178. * This means that the client is not requesting any state changes and that database writes
  1179. * are not inherently required. Ideally, no visible updates would happen at all. If they
  1180. * must, then they should not be publically attributed to the end user.
  1181. *
  1182. * In more detail:
  1183. * - Cache populations and refreshes MAY occur.
  1184. * - Private user session updates and private server logging MAY occur.
  1185. * - Updates to private viewing activity data MAY occur via DeferredUpdates.
  1186. * - Other updates SHOULD NOT occur (e.g. modifying content assets).
  1187. *
  1188. * @return bool
  1189. * @see https://tools.ietf.org/html/rfc7231#section-4.2.1
  1190. * @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
  1191. * @since 1.28
  1192. */
  1193. public function isSafeRequest() {
  1194. if ( $this->markedAsSafe && $this->wasPosted() ) {
  1195. return true; // marked as a "safe" POST
  1196. }
  1197. return $this->hasSafeMethod();
  1198. }
  1199. /**
  1200. * Mark this request as identified as being nullipotent even if it is a POST request
  1201. *
  1202. * POST requests are often used due to the need for a client payload, even if the request
  1203. * is otherwise equivalent to a "safe method" request.
  1204. *
  1205. * @see https://tools.ietf.org/html/rfc7231#section-4.2.1
  1206. * @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
  1207. * @since 1.28
  1208. */
  1209. public function markAsSafeRequest() {
  1210. $this->markedAsSafe = true;
  1211. }
  1212. }