Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like WebRequest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WebRequest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class WebRequest { |
||
| 39 | protected $data, $headers = []; |
||
|
|
|||
| 40 | |||
| 41 | /** |
||
| 42 | * Flag to make WebRequest::getHeader return an array of values. |
||
| 43 | * @since 1.26 |
||
| 44 | */ |
||
| 45 | const GETHEADER_LIST = 1; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The unique request ID. |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | private static $reqId; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Lazy-init response object |
||
| 55 | * @var WebResponse |
||
| 56 | */ |
||
| 57 | private $response; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Cached client IP address |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | private $ip; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * The timestamp of the start of the request, with microsecond precision. |
||
| 67 | * @var float |
||
| 68 | */ |
||
| 69 | protected $requestTime; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Cached URL protocol |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | protected $protocol; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var SessionId|null Session ID to use for this |
||
| 79 | * request. We can't save the session directly due to reference cycles not |
||
| 80 | * working too well (slow GC in Zend and never collected in HHVM). |
||
| 81 | */ |
||
| 82 | protected $sessionId = null; |
||
| 83 | |||
| 84 | /** @var bool Whether this HTTP request is "safe" (even if it is an HTTP post) */ |
||
| 85 | protected $markedAsSafe = false; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @codeCoverageIgnore |
||
| 89 | */ |
||
| 90 | public function __construct() { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Extract relevant query arguments from the http request uri's path |
||
| 101 | * to be merged with the normal php provided query arguments. |
||
| 102 | * Tries to use the REQUEST_URI data if available and parses it |
||
| 103 | * according to the wiki's configuration looking for any known pattern. |
||
| 104 | * |
||
| 105 | * If the REQUEST_URI is not provided we'll fall back on the PATH_INFO |
||
| 106 | * provided by the server if any and use that to set a 'title' parameter. |
||
| 107 | * |
||
| 108 | * @param string $want If this is not 'all', then the function |
||
| 109 | * will return an empty array if it determines that the URL is |
||
| 110 | * inside a rewrite path. |
||
| 111 | * |
||
| 112 | * @return array Any query arguments found in path matches. |
||
| 113 | */ |
||
| 114 | public static function getPathInfo( $want = 'all' ) { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Work out an appropriate URL prefix containing scheme and host, based on |
||
| 193 | * information detected from $_SERVER |
||
| 194 | * |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | public static function detectServer() { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Detect the protocol from $_SERVER. |
||
| 238 | * This is for use prior to Setup.php, when no WebRequest object is available. |
||
| 239 | * At other times, use the non-static function getProtocol(). |
||
| 240 | * |
||
| 241 | * @return array |
||
| 242 | */ |
||
| 243 | public static function detectProtocol() { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Get the number of seconds to have elapsed since request start, |
||
| 255 | * in fractional seconds, with microsecond resolution. |
||
| 256 | * |
||
| 257 | * @return float |
||
| 258 | * @since 1.25 |
||
| 259 | */ |
||
| 260 | public function getElapsedTime() { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Get the unique request ID. |
||
| 266 | * This is either the value of the UNIQUE_ID envvar (if present) or a |
||
| 267 | * randomly-generated 24-character string. |
||
| 268 | * |
||
| 269 | * @return string |
||
| 270 | * @since 1.27 |
||
| 271 | */ |
||
| 272 | public static function getRequestId() { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Override the unique request ID. This is for sub-requests, such as jobs, |
||
| 283 | * that wish to use the same id but are not part of the same execution context. |
||
| 284 | * |
||
| 285 | * @param string $id |
||
| 286 | * @since 1.27 |
||
| 287 | */ |
||
| 288 | public static function overrideRequestId( $id ) { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Get the current URL protocol (http or https) |
||
| 294 | * @return string |
||
| 295 | */ |
||
| 296 | public function getProtocol() { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Check for title, action, and/or variant data in the URL |
||
| 305 | * and interpolate it into the GET variables. |
||
| 306 | * This should only be run after $wgContLang is available, |
||
| 307 | * as we may need the list of language variants to determine |
||
| 308 | * available variant URLs. |
||
| 309 | */ |
||
| 310 | public function interpolateTitle() { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * URL rewriting function; tries to extract page title and, |
||
| 324 | * optionally, one other fixed parameter value from a URL path. |
||
| 325 | * |
||
| 326 | * @param string $path The URL path given from the client |
||
| 327 | * @param array $bases One or more URLs, optionally with $1 at the end |
||
| 328 | * @param string|bool $key If provided, the matching key in $bases will be |
||
| 329 | * passed on as the value of this URL parameter |
||
| 330 | * @return array Array of URL variables to interpolate; empty if no match |
||
| 331 | */ |
||
| 332 | static function extractTitle( $path, $bases, $key = false ) { |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Recursively normalizes UTF-8 strings in the given array. |
||
| 353 | * |
||
| 354 | * @param string|array $data |
||
| 355 | * @return array|string Cleaned-up version of the given |
||
| 356 | * @private |
||
| 357 | */ |
||
| 358 | public function normalizeUnicode( $data ) { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Fetch a value from the given array or return $default if it's not set. |
||
| 374 | * |
||
| 375 | * @param array $arr |
||
| 376 | * @param string $name |
||
| 377 | * @param mixed $default |
||
| 378 | * @return mixed |
||
| 379 | */ |
||
| 380 | private function getGPCVal( $arr, $name, $default ) { |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Fetch a scalar from the input without normalization, or return $default |
||
| 403 | * if it's not set. |
||
| 404 | * |
||
| 405 | * Unlike self::getVal(), this does not perform any normalization on the |
||
| 406 | * input value. |
||
| 407 | * |
||
| 408 | * @since 1.28 |
||
| 409 | * @param string $name |
||
| 410 | * @param string|null $default Optional default |
||
| 411 | * @return string |
||
| 412 | */ |
||
| 413 | public function getRawVal( $name, $default = null ) { |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Fetch a scalar from the input or return $default if it's not set. |
||
| 429 | * Returns a string. Arrays are discarded. Useful for |
||
| 430 | * non-freeform text inputs (e.g. predefined internal text keys |
||
| 431 | * selected by a drop-down menu). For freeform input, see getText(). |
||
| 432 | * |
||
| 433 | * @param string $name |
||
| 434 | * @param string $default Optional default (or null) |
||
| 435 | * @return string |
||
| 436 | */ |
||
| 437 | public function getVal( $name, $default = null ) { |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Set an arbitrary value into our get/post data. |
||
| 451 | * |
||
| 452 | * @param string $key Key name to use |
||
| 453 | * @param mixed $value Value to set |
||
| 454 | * @return mixed Old value if one was present, null otherwise |
||
| 455 | */ |
||
| 456 | public function setVal( $key, $value ) { |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Unset an arbitrary value from our get/post data. |
||
| 464 | * |
||
| 465 | * @param string $key Key name to use |
||
| 466 | * @return mixed Old value if one was present, null otherwise |
||
| 467 | */ |
||
| 468 | public function unsetVal( $key ) { |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Fetch an array from the input or return $default if it's not set. |
||
| 480 | * If source was scalar, will return an array with a single element. |
||
| 481 | * If no source and no default, returns null. |
||
| 482 | * |
||
| 483 | * @param string $name |
||
| 484 | * @param array $default Optional default (or null) |
||
| 485 | * @return array |
||
| 486 | */ |
||
| 487 | public function getArray( $name, $default = null ) { |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Fetch an array of integers, or return $default if it's not set. |
||
| 498 | * If source was scalar, will return an array with a single element. |
||
| 499 | * If no source and no default, returns null. |
||
| 500 | * If an array is returned, contents are guaranteed to be integers. |
||
| 501 | * |
||
| 502 | * @param string $name |
||
| 503 | * @param array $default Option default (or null) |
||
| 504 | * @return array Array of ints |
||
| 505 | */ |
||
| 506 | public function getIntArray( $name, $default = null ) { |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Fetch an integer value from the input or return $default if not set. |
||
| 516 | * Guaranteed to return an integer; non-numeric input will typically |
||
| 517 | * return 0. |
||
| 518 | * |
||
| 519 | * @param string $name |
||
| 520 | * @param int $default |
||
| 521 | * @return int |
||
| 522 | */ |
||
| 523 | public function getInt( $name, $default = 0 ) { |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Fetch an integer value from the input or return null if empty. |
||
| 529 | * Guaranteed to return an integer or null; non-numeric input will |
||
| 530 | * typically return null. |
||
| 531 | * |
||
| 532 | * @param string $name |
||
| 533 | * @return int|null |
||
| 534 | */ |
||
| 535 | public function getIntOrNull( $name ) { |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Fetch a floating point value from the input or return $default if not set. |
||
| 544 | * Guaranteed to return a float; non-numeric input will typically |
||
| 545 | * return 0. |
||
| 546 | * |
||
| 547 | * @since 1.23 |
||
| 548 | * @param string $name |
||
| 549 | * @param float $default |
||
| 550 | * @return float |
||
| 551 | */ |
||
| 552 | public function getFloat( $name, $default = 0.0 ) { |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Fetch a boolean value from the input or return $default if not set. |
||
| 558 | * Guaranteed to return true or false, with normal PHP semantics for |
||
| 559 | * boolean interpretation of strings. |
||
| 560 | * |
||
| 561 | * @param string $name |
||
| 562 | * @param bool $default |
||
| 563 | * @return bool |
||
| 564 | */ |
||
| 565 | public function getBool( $name, $default = false ) { |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Fetch a boolean value from the input or return $default if not set. |
||
| 571 | * Unlike getBool, the string "false" will result in boolean false, which is |
||
| 572 | * useful when interpreting information sent from JavaScript. |
||
| 573 | * |
||
| 574 | * @param string $name |
||
| 575 | * @param bool $default |
||
| 576 | * @return bool |
||
| 577 | */ |
||
| 578 | public function getFuzzyBool( $name, $default = false ) { |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Return true if the named value is set in the input, whatever that |
||
| 585 | * value is (even "0"). Return false if the named value is not set. |
||
| 586 | * Example use is checking for the presence of check boxes in forms. |
||
| 587 | * |
||
| 588 | * @param string $name |
||
| 589 | * @return bool |
||
| 590 | */ |
||
| 591 | public function getCheck( $name ) { |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Fetch a text string from the given array or return $default if it's not |
||
| 599 | * set. Carriage returns are stripped from the text. This should generally |
||
| 600 | * be used for form "<textarea>" and "<input>" fields, and for |
||
| 601 | * user-supplied freeform text input. |
||
| 602 | * |
||
| 603 | * @param string $name |
||
| 604 | * @param string $default Optional |
||
| 605 | * @return string |
||
| 606 | */ |
||
| 607 | public function getText( $name, $default = '' ) { |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Extracts the given named values into an array. |
||
| 614 | * If no arguments are given, returns all input values. |
||
| 615 | * No transformation is performed on the values. |
||
| 616 | * |
||
| 617 | * @return array |
||
| 618 | */ |
||
| 619 | public function getValues() { |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Returns the names of all input values excluding those in $exclude. |
||
| 637 | * |
||
| 638 | * @param array $exclude |
||
| 639 | * @return array |
||
| 640 | */ |
||
| 641 | public function getValueNames( $exclude = [] ) { |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Get the values passed in the query string. |
||
| 647 | * No transformation is performed on the values. |
||
| 648 | * |
||
| 649 | * @codeCoverageIgnore |
||
| 650 | * @return array |
||
| 651 | */ |
||
| 652 | public function getQueryValues() { |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Return the contents of the Query with no decoding. Use when you need to |
||
| 658 | * know exactly what was sent, e.g. for an OAuth signature over the elements. |
||
| 659 | * |
||
| 660 | * @codeCoverageIgnore |
||
| 661 | * @return string |
||
| 662 | */ |
||
| 663 | public function getRawQueryString() { |
||
| 666 | |||
| 667 | /** |
||
| 668 | * Return the contents of the POST with no decoding. Use when you need to |
||
| 669 | * know exactly what was sent, e.g. for an OAuth signature over the elements. |
||
| 670 | * |
||
| 671 | * @return string |
||
| 672 | */ |
||
| 673 | public function getRawPostString() { |
||
| 679 | |||
| 680 | /** |
||
| 681 | * Return the raw request body, with no processing. Cached since some methods |
||
| 682 | * disallow reading the stream more than once. As stated in the php docs, this |
||
| 683 | * does not work with enctype="multipart/form-data". |
||
| 684 | * |
||
| 685 | * @return string |
||
| 686 | */ |
||
| 687 | public function getRawInput() { |
||
| 694 | |||
| 695 | /** |
||
| 696 | * Get the HTTP method used for this request. |
||
| 697 | * |
||
| 698 | * @return string |
||
| 699 | */ |
||
| 700 | public function getMethod() { |
||
| 703 | |||
| 704 | /** |
||
| 705 | * Returns true if the present request was reached by a POST operation, |
||
| 706 | * false otherwise (GET, HEAD, or command-line). |
||
| 707 | * |
||
| 708 | * Note that values retrieved by the object may come from the |
||
| 709 | * GET URL etc even on a POST request. |
||
| 710 | * |
||
| 711 | * @return bool |
||
| 712 | */ |
||
| 713 | public function wasPosted() { |
||
| 716 | |||
| 717 | /** |
||
| 718 | * Return the session for this request |
||
| 719 | * |
||
| 720 | * This might unpersist an existing session if it was invalid. |
||
| 721 | * |
||
| 722 | * @since 1.27 |
||
| 723 | * @note For performance, keep the session locally if you will be making |
||
| 724 | * much use of it instead of calling this method repeatedly. |
||
| 725 | * @return Session |
||
| 726 | */ |
||
| 727 | public function getSession() { |
||
| 739 | |||
| 740 | /** |
||
| 741 | * Set the session for this request |
||
| 742 | * @since 1.27 |
||
| 743 | * @private For use by MediaWiki\Session classes only |
||
| 744 | * @param SessionId $sessionId |
||
| 745 | */ |
||
| 746 | public function setSessionId( SessionId $sessionId ) { |
||
| 749 | |||
| 750 | /** |
||
| 751 | * Get the session id for this request, if any |
||
| 752 | * @since 1.27 |
||
| 753 | * @private For use by MediaWiki\Session classes only |
||
| 754 | * @return SessionId|null |
||
| 755 | */ |
||
| 756 | public function getSessionId() { |
||
| 759 | |||
| 760 | /** |
||
| 761 | * Returns true if the request has a persistent session. |
||
| 762 | * This does not necessarily mean that the user is logged in! |
||
| 763 | * |
||
| 764 | * @deprecated since 1.27, use |
||
| 765 | * \MediaWiki\Session\SessionManager::singleton()->getPersistedSessionId() |
||
| 766 | * instead. |
||
| 767 | * @return bool |
||
| 768 | */ |
||
| 769 | public function checkSessionCookie() { |
||
| 775 | |||
| 776 | /** |
||
| 777 | * Get a cookie from the $_COOKIE jar |
||
| 778 | * |
||
| 779 | * @param string $key The name of the cookie |
||
| 780 | * @param string $prefix A prefix to use for the cookie name, if not $wgCookiePrefix |
||
| 781 | * @param mixed $default What to return if the value isn't found |
||
| 782 | * @return mixed Cookie value or $default if the cookie not set |
||
| 783 | */ |
||
| 784 | public function getCookie( $key, $prefix = null, $default = null ) { |
||
| 791 | |||
| 792 | /** |
||
| 793 | * Return the path and query string portion of the main request URI. |
||
| 794 | * This will be suitable for use as a relative link in HTML output. |
||
| 795 | * |
||
| 796 | * @throws MWException |
||
| 797 | * @return string |
||
| 798 | */ |
||
| 799 | public static function getGlobalRequestURL() { |
||
| 835 | |||
| 836 | /** |
||
| 837 | * Return the path and query string portion of the request URI. |
||
| 838 | * This will be suitable for use as a relative link in HTML output. |
||
| 839 | * |
||
| 840 | * @throws MWException |
||
| 841 | * @return string |
||
| 842 | */ |
||
| 843 | public function getRequestURL() { |
||
| 846 | |||
| 847 | /** |
||
| 848 | * Return the request URI with the canonical service and hostname, path, |
||
| 849 | * and query string. This will be suitable for use as an absolute link |
||
| 850 | * in HTML or other output. |
||
| 851 | * |
||
| 852 | * If $wgServer is protocol-relative, this will return a fully |
||
| 853 | * qualified URL with the protocol that was used for this request. |
||
| 854 | * |
||
| 855 | * @return string |
||
| 856 | */ |
||
| 857 | public function getFullRequestURL() { |
||
| 860 | |||
| 861 | /** |
||
| 862 | * @param string $key |
||
| 863 | * @param string $value |
||
| 864 | * @return string |
||
| 865 | */ |
||
| 866 | public function appendQueryValue( $key, $value ) { |
||
| 869 | |||
| 870 | /** |
||
| 871 | * Appends or replaces value of query variables. |
||
| 872 | * |
||
| 873 | * @param array $array Array of values to replace/add to query |
||
| 874 | * @return string |
||
| 875 | */ |
||
| 876 | public function appendQueryArray( $array ) { |
||
| 883 | |||
| 884 | /** |
||
| 885 | * Check for limit and offset parameters on the input, and return sensible |
||
| 886 | * defaults if not given. The limit must be positive and is capped at 5000. |
||
| 887 | * Offset must be positive but is not capped. |
||
| 888 | * |
||
| 889 | * @param int $deflimit Limit to use if no input and the user hasn't set the option. |
||
| 890 | * @param string $optionname To specify an option other than rclimit to pull from. |
||
| 891 | * @return int[] First element is limit, second is offset |
||
| 892 | */ |
||
| 893 | public function getLimitOffset( $deflimit = 50, $optionname = 'rclimit' ) { |
||
| 917 | |||
| 918 | /** |
||
| 919 | * Return the path to the temporary file where PHP has stored the upload. |
||
| 920 | * |
||
| 921 | * @param string $key |
||
| 922 | * @return string|null String or null if no such file. |
||
| 923 | */ |
||
| 924 | public function getFileTempname( $key ) { |
||
| 928 | |||
| 929 | /** |
||
| 930 | * Return the upload error or 0 |
||
| 931 | * |
||
| 932 | * @param string $key |
||
| 933 | * @return int |
||
| 934 | */ |
||
| 935 | public function getUploadError( $key ) { |
||
| 939 | |||
| 940 | /** |
||
| 941 | * Return the original filename of the uploaded file, as reported by |
||
| 942 | * the submitting user agent. HTML-style character entities are |
||
| 943 | * interpreted and normalized to Unicode normalization form C, in part |
||
| 944 | * to deal with weird input from Safari with non-ASCII filenames. |
||
| 945 | * |
||
| 946 | * Other than this the name is not verified for being a safe filename. |
||
| 947 | * |
||
| 948 | * @param string $key |
||
| 949 | * @return string|null String or null if no such file. |
||
| 950 | */ |
||
| 951 | public function getFileName( $key ) { |
||
| 955 | |||
| 956 | /** |
||
| 957 | * Return a WebRequestUpload object corresponding to the key |
||
| 958 | * |
||
| 959 | * @param string $key |
||
| 960 | * @return WebRequestUpload |
||
| 961 | */ |
||
| 962 | public function getUpload( $key ) { |
||
| 965 | |||
| 966 | /** |
||
| 967 | * Return a handle to WebResponse style object, for setting cookies, |
||
| 968 | * headers and other stuff, for Request being worked on. |
||
| 969 | * |
||
| 970 | * @return WebResponse |
||
| 971 | */ |
||
| 972 | public function response() { |
||
| 980 | |||
| 981 | /** |
||
| 982 | * Initialise the header list |
||
| 983 | */ |
||
| 984 | protected function initHeaders() { |
||
| 1005 | |||
| 1006 | /** |
||
| 1007 | * Get an array containing all request headers |
||
| 1008 | * |
||
| 1009 | * @return array Mapping header name to its value |
||
| 1010 | */ |
||
| 1011 | public function getAllHeaders() { |
||
| 1015 | |||
| 1016 | /** |
||
| 1017 | * Get a request header, or false if it isn't set. |
||
| 1018 | * |
||
| 1019 | * @param string $name Case-insensitive header name |
||
| 1020 | * @param int $flags Bitwise combination of: |
||
| 1021 | * WebRequest::GETHEADER_LIST Treat the header as a comma-separated list |
||
| 1022 | * of values, as described in RFC 2616 § 4.2. |
||
| 1023 | * (since 1.26). |
||
| 1024 | * @return string|array|bool False if header is unset; otherwise the |
||
| 1025 | * header value(s) as either a string (the default) or an array, if |
||
| 1026 | * WebRequest::GETHEADER_LIST flag was set. |
||
| 1027 | */ |
||
| 1028 | public function getHeader( $name, $flags = 0 ) { |
||
| 1040 | |||
| 1041 | /** |
||
| 1042 | * Get data from the session |
||
| 1043 | * |
||
| 1044 | * @note Prefer $this->getSession() instead if making multiple calls. |
||
| 1045 | * @param string $key Name of key in the session |
||
| 1046 | * @return mixed |
||
| 1047 | */ |
||
| 1048 | public function getSessionData( $key ) { |
||
| 1051 | |||
| 1052 | /** |
||
| 1053 | * Set session data |
||
| 1054 | * |
||
| 1055 | * @note Prefer $this->getSession() instead if making multiple calls. |
||
| 1056 | * @param string $key Name of key in the session |
||
| 1057 | * @param mixed $data |
||
| 1058 | */ |
||
| 1059 | public function setSessionData( $key, $data ) { |
||
| 1062 | |||
| 1063 | /** |
||
| 1064 | * Check if Internet Explorer will detect an incorrect cache extension in |
||
| 1065 | * PATH_INFO or QUERY_STRING. If the request can't be allowed, show an error |
||
| 1066 | * message or redirect to a safer URL. Returns true if the URL is OK, and |
||
| 1067 | * false if an error message has been shown and the request should be aborted. |
||
| 1068 | * |
||
| 1069 | * @param array $extWhitelist |
||
| 1070 | * @throws HttpError |
||
| 1071 | * @return bool |
||
| 1072 | */ |
||
| 1073 | public function checkUrlExtension( $extWhitelist = [] ) { |
||
| 1089 | |||
| 1090 | /** |
||
| 1091 | * Attempt to redirect to a URL with a QUERY_STRING that's not dangerous in |
||
| 1092 | * IE 6. Returns true if it was successful, false otherwise. |
||
| 1093 | * |
||
| 1094 | * @param string $url |
||
| 1095 | * @return bool |
||
| 1096 | */ |
||
| 1097 | protected function doSecurityRedirect( $url ) { |
||
| 1122 | |||
| 1123 | /** |
||
| 1124 | * Parse the Accept-Language header sent by the client into an array |
||
| 1125 | * |
||
| 1126 | * @return array Array( languageCode => q-value ) sorted by q-value in |
||
| 1127 | * descending order then appearing time in the header in ascending order. |
||
| 1128 | * May contain the "language" '*', which applies to languages other than those explicitly listed. |
||
| 1129 | * This is aligned with rfc2616 section 14.4 |
||
| 1130 | * Preference for earlier languages appears in rfc3282 as an extension to HTTP/1.1. |
||
| 1131 | */ |
||
| 1132 | public function getAcceptLang() { |
||
| 1176 | |||
| 1177 | /** |
||
| 1178 | * Fetch the raw IP from the request |
||
| 1179 | * |
||
| 1180 | * @since 1.19 |
||
| 1181 | * |
||
| 1182 | * @throws MWException |
||
| 1183 | * @return string |
||
| 1184 | */ |
||
| 1185 | protected function getRawIP() { |
||
| 1199 | |||
| 1200 | /** |
||
| 1201 | * Work out the IP address based on various globals |
||
| 1202 | * For trusted proxies, use the XFF client IP (first of the chain) |
||
| 1203 | * |
||
| 1204 | * @since 1.19 |
||
| 1205 | * |
||
| 1206 | * @throws MWException |
||
| 1207 | * @return string |
||
| 1208 | */ |
||
| 1209 | public function getIP() { |
||
| 1275 | |||
| 1276 | /** |
||
| 1277 | * @param string $ip |
||
| 1278 | * @return void |
||
| 1279 | * @since 1.21 |
||
| 1280 | */ |
||
| 1281 | public function setIP( $ip ) { |
||
| 1284 | |||
| 1285 | /** |
||
| 1286 | * Check if this request uses a "safe" HTTP method |
||
| 1287 | * |
||
| 1288 | * Safe methods are verbs (e.g. GET/HEAD/OPTIONS) used for obtaining content. Such requests |
||
| 1289 | * are not expected to mutate content, especially in ways attributable to the client. Verbs |
||
| 1290 | * like POST and PUT are typical of non-safe requests which often change content. |
||
| 1291 | * |
||
| 1292 | * @return bool |
||
| 1293 | * @see https://tools.ietf.org/html/rfc7231#section-4.2.1 |
||
| 1294 | * @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html |
||
| 1295 | * @since 1.28 |
||
| 1296 | */ |
||
| 1297 | public function hasSafeMethod() { |
||
| 1304 | |||
| 1305 | /** |
||
| 1306 | * Whether this request should be identified as being "safe" |
||
| 1307 | * |
||
| 1308 | * This means that the client is not requesting any state changes and that database writes |
||
| 1309 | * are not inherently required. Ideally, no visible updates would happen at all. If they |
||
| 1310 | * must, then they should not be publically attributed to the end user. |
||
| 1311 | * |
||
| 1312 | * In more detail: |
||
| 1313 | * - Cache populations and refreshes MAY occur. |
||
| 1314 | * - Private user session updates and private server logging MAY occur. |
||
| 1315 | * - Updates to private viewing activity data MAY occur via DeferredUpdates. |
||
| 1316 | * - Other updates SHOULD NOT occur (e.g. modifying content assets). |
||
| 1317 | * |
||
| 1318 | * @return bool |
||
| 1319 | * @see https://tools.ietf.org/html/rfc7231#section-4.2.1 |
||
| 1320 | * @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html |
||
| 1321 | * @since 1.28 |
||
| 1322 | */ |
||
| 1323 | public function isSafeRequest() { |
||
| 1330 | |||
| 1331 | /** |
||
| 1332 | * Mark this request as identified as being nullipotent even if it is a POST request |
||
| 1333 | * |
||
| 1334 | * POST requests are often used due to the need for a client payload, even if the request |
||
| 1335 | * is otherwise equivalent to a "safe method" request. |
||
| 1336 | * |
||
| 1337 | * @see https://tools.ietf.org/html/rfc7231#section-4.2.1 |
||
| 1338 | * @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html |
||
| 1339 | * @since 1.28 |
||
| 1340 | */ |
||
| 1341 | public function markAsSafeRequest() { |
||
| 1344 | } |
||
| 1345 |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.