X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FWebRequest.php;h=a5a550bfc229dc68d95f64394de5c73b744b7ec3;hb=4331d13d8701cbd0217ecbe593df100b81693ef2;hp=f4b029461bb5ace158b2cf041c0fe16e6ab89995;hpb=bc14eb8045cb02dead76a1c4203c45ab6e31cb36;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/WebRequest.php b/includes/WebRequest.php index f4b029461b..a5a550bfc2 100644 --- a/includes/WebRequest.php +++ b/includes/WebRequest.php @@ -1,7 +1,6 @@ @@ -22,6 +21,15 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # http://www.gnu.org/copyleft/gpl.html + +/** + * Some entry points may use this file without first enabling the + * autoloader. + */ +if ( !function_exists( '__autoload' ) ) { + require_once( dirname(__FILE__) . '/normal/UtfNormal.php' ); +} + /** * The WebRequest class encapsulates getting at data passed in the * URL or via a POSTed form, handling remove of "magic quotes" slashes, @@ -32,18 +40,100 @@ * you want to pass arbitrary data to some function in place of the web * input. * - * @package MediaWiki */ class WebRequest { - function WebRequest() { + function __construct() { $this->checkMagicQuotes(); + } + + /** + * Check for title, action, and/or variant data in the URL + * and interpolate it into the GET variables. + * This should only be run after $wgContLang is available, + * as we may need the list of language variants to determine + * available variant URLs. + */ + function interpolateTitle() { global $wgUsePathInfo; - if( isset( $_SERVER['PATH_INFO'] ) && ($_SERVER['PATH_INFO'] != '') && $wgUsePathInfo ) { - # Stuff it! - $_GET['title'] = $_REQUEST['title'] = - substr( $_SERVER['PATH_INFO'], 1 ); + if ( $wgUsePathInfo ) { + // PATH_INFO is mangled due to http://bugs.php.net/bug.php?id=31892 + // And also by Apache 2.x, double slashes are converted to single slashes. + // So we will use REQUEST_URI if possible. + $matches = array(); + if ( !empty( $_SERVER['REQUEST_URI'] ) ) { + // Slurp out the path portion to examine... + $url = $_SERVER['REQUEST_URI']; + if ( !preg_match( '!^https?://!', $url ) ) { + $url = 'http://unused' . $url; + } + $a = parse_url( $url ); + if( $a ) { + $path = $a['path']; + + global $wgArticlePath; + $matches = $this->extractTitle( $path, $wgArticlePath ); + + global $wgActionPaths; + if( !$matches && $wgActionPaths) { + $matches = $this->extractTitle( $path, $wgActionPaths, 'action' ); + } + + global $wgVariantArticlePath, $wgContLang; + if( !$matches && $wgVariantArticlePath ) { + $variantPaths = array(); + foreach( $wgContLang->getVariants() as $variant ) { + $variantPaths[$variant] = + str_replace( '$2', $variant, $wgVariantArticlePath ); + } + $matches = $this->extractTitle( $path, $variantPaths, 'variant' ); + } + } + } elseif ( isset( $_SERVER['ORIG_PATH_INFO'] ) && $_SERVER['ORIG_PATH_INFO'] != '' ) { + // Mangled PATH_INFO + // http://bugs.php.net/bug.php?id=31892 + // Also reported when ini_get('cgi.fix_pathinfo')==false + $matches['title'] = substr( $_SERVER['ORIG_PATH_INFO'], 1 ); + + } elseif ( isset( $_SERVER['PATH_INFO'] ) && ($_SERVER['PATH_INFO'] != '') ) { + // Regular old PATH_INFO yay + $matches['title'] = substr( $_SERVER['PATH_INFO'], 1 ); + } + foreach( $matches as $key => $val) { + $_GET[$key] = $_REQUEST[$key] = $val; + } + } + } + + /** + * Internal URL rewriting function; tries to extract page title and, + * optionally, one other fixed parameter value from a URL path. + * + * @param string $path the URL path given from the client + * @param array $bases one or more URLs, optionally with $1 at the end + * @param string $key if provided, the matching key in $bases will be + * passed on as the value of this URL parameter + * @return array of URL variables to interpolate; empty if no match + */ + private function extractTitle( $path, $bases, $key=false ) { + foreach( (array)$bases as $keyValue => $base ) { + // Find the part after $wgArticlePath + $base = str_replace( '$1', '', $base ); + $baseLen = strlen( $base ); + if( substr( $path, 0, $baseLen ) == $base ) { + $raw = substr( $path, $baseLen ); + if( $raw !== '' ) { + $matches = array( 'title' => rawurldecode( $raw ) ); + if( $key ) { + $matches[$key] = $keyValue; + } + return $matches; + } + } } + return array(); } + + private $_response; /** * Recursively strips slashes from the given array; @@ -117,7 +207,6 @@ class WebRequest { $data = $wgContLang->checkTitleEncoding( $data ); } } - require_once( 'normal/UtfNormal.php' ); $data = $this->normalizeUnicode( $data ); return $data; } else { @@ -127,7 +216,9 @@ class WebRequest { /** * Fetch a scalar from the input or return $default if it's not set. - * Returns a string. Arrays are discarded. + * Returns a string. Arrays are discarded. Useful for + * non-freeform text inputs (e.g. predefined internal text keys + * selected by a drop-down menu). For freeform input, see getText(). * * @param string $name * @param string $default optional default (or NULL) @@ -237,7 +328,9 @@ class WebRequest { * Fetch a text string from the given array or return $default if it's not * set. \r is stripped from the text, and with some language modules there * is an input transliteration applied. This should generally be used for - * form