123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734 |
- <?php
- if ( !function_exists( '__autoload' ) ) {
- require_once( dirname(__FILE__) . '/normal/UtfNormal.php' );
- }
- class WebRequest {
- var $data = array();
- var $headers;
- private $_response;
- function __construct() {
-
-
-
- $this->checkMagicQuotes();
-
-
- $this->data = $_POST + $_GET;
- }
-
- function interpolateTitle() {
- global $wgUsePathInfo;
- if ( $wgUsePathInfo ) {
-
-
-
- $matches = array();
- if ( !empty( $_SERVER['REQUEST_URI'] ) ) {
-
- $url = $_SERVER['REQUEST_URI'];
- if ( !preg_match( '!^https?://!', $url ) ) {
- $url = 'http://unused' . $url;
- }
- $a = parse_url( $url );
- if( $a ) {
- $path = isset( $a['path'] ) ? $a['path'] : '';
- global $wgScript;
- if( $path == $wgScript ) {
-
-
- return;
- }
-
- $matches = $this->extractTitle( $path, "$wgScript/$1" );
- global $wgArticlePath;
- if( !$matches && $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'] != '' ) {
-
-
-
- $matches['title'] = substr( $_SERVER['ORIG_PATH_INFO'], 1 );
- } elseif ( isset( $_SERVER['PATH_INFO'] ) && ($_SERVER['PATH_INFO'] != '') ) {
-
- $matches['title'] = substr( $_SERVER['PATH_INFO'], 1 );
- }
- foreach( $matches as $key => $val) {
- $this->data[$key] = $_GET[$key] = $_REQUEST[$key] = $val;
- }
- }
- }
-
- private function extractTitle( $path, $bases, $key=false ) {
- foreach( (array)$bases as $keyValue => $base ) {
-
- $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();
- }
-
- function &fix_magic_quotes( &$arr ) {
- foreach( $arr as $key => $val ) {
- if( is_array( $val ) ) {
- $this->fix_magic_quotes( $arr[$key] );
- } else {
- $arr[$key] = stripslashes( $val );
- }
- }
- return $arr;
- }
-
- function checkMagicQuotes() {
- if ( function_exists( 'get_magic_quotes_gpc' ) && get_magic_quotes_gpc() ) {
- $this->fix_magic_quotes( $_COOKIE );
- $this->fix_magic_quotes( $_ENV );
- $this->fix_magic_quotes( $_GET );
- $this->fix_magic_quotes( $_POST );
- $this->fix_magic_quotes( $_REQUEST );
- $this->fix_magic_quotes( $_SERVER );
- }
- }
-
- function normalizeUnicode( $data ) {
- if( is_array( $data ) ) {
- foreach( $data as $key => $val ) {
- $data[$key] = $this->normalizeUnicode( $val );
- }
- } else {
- $data = UtfNormal::cleanUp( $data );
- }
- return $data;
- }
-
- function getGPCVal( $arr, $name, $default ) {
- if( isset( $arr[$name] ) ) {
- global $wgContLang;
- $data = $arr[$name];
- if( isset( $_GET[$name] ) && !is_array( $data ) ) {
-
- if( isset( $wgContLang ) ) {
- $data = $wgContLang->checkTitleEncoding( $data );
- }
- }
- $data = $this->normalizeUnicode( $data );
- return $data;
- } else {
- taint( $default );
- return $default;
- }
- }
-
- function getVal( $name, $default = NULL ) {
- $val = $this->getGPCVal( $this->data, $name, $default );
- if( is_array( $val ) ) {
- $val = $default;
- }
- if( is_null( $val ) ) {
- return $val;
- } else {
- return (string)$val;
- }
- }
-
-
- function setVal( $key, $value ) {
- $ret = isset( $this->data[$key] ) ? $this->data[$key] : null;
- $this->data[$key] = $value;
- return $ret;
- }
-
- function getArray( $name, $default = NULL ) {
- $val = $this->getGPCVal( $this->data, $name, $default );
- if( is_null( $val ) ) {
- return null;
- } else {
- return (array)$val;
- }
- }
-
- function getIntArray( $name, $default = NULL ) {
- $val = $this->getArray( $name, $default );
- if( is_array( $val ) ) {
- $val = array_map( 'intval', $val );
- }
- return $val;
- }
-
- function getInt( $name, $default = 0 ) {
- return intval( $this->getVal( $name, $default ) );
- }
-
- function getIntOrNull( $name ) {
- $val = $this->getVal( $name );
- return is_numeric( $val )
- ? intval( $val )
- : null;
- }
-
- function getBool( $name, $default = false ) {
- return $this->getVal( $name, $default ) ? true : false;
- }
-
- function getCheck( $name ) {
-
-
- $val = $this->getVal( $name, NULL );
- return isset( $val );
- }
-
- function getText( $name, $default = '' ) {
- global $wgContLang;
- $val = $this->getVal( $name, $default );
- return str_replace( "\r\n", "\n",
- $wgContLang->recodeInput( $val ) );
- }
-
- function getValues() {
- $names = func_get_args();
- if ( count( $names ) == 0 ) {
- $names = array_keys( $this->data );
- }
- $retVal = array();
- foreach ( $names as $name ) {
- $value = $this->getVal( $name );
- if ( !is_null( $value ) ) {
- $retVal[$name] = $value;
- }
- }
- return $retVal;
- }
-
- function wasPosted() {
- return $_SERVER['REQUEST_METHOD'] == 'POST';
- }
-
- function checkSessionCookie() {
- return isset( $_COOKIE[session_name()] );
- }
-
- function getRequestURL() {
- if( isset( $_SERVER['REQUEST_URI'] ) ) {
- $base = $_SERVER['REQUEST_URI'];
- } elseif( isset( $_SERVER['SCRIPT_NAME'] ) ) {
-
- $base = $_SERVER['SCRIPT_NAME'];
- if( isset( $_SERVER['QUERY_STRING'] ) && $_SERVER['QUERY_STRING'] != '' ) {
- $base .= '?' . $_SERVER['QUERY_STRING'];
- }
- } else {
-
- throw new MWException( "Web server doesn't provide either " .
- "REQUEST_URI or SCRIPT_NAME. Report details of your " .
- "web server configuration to http://bugzilla.wikimedia.org/" );
- }
-
-
-
-
- $hash = strpos( $base, '#' );
- if( $hash !== false ) {
- $base = substr( $base, 0, $hash );
- }
- if( $base{0} == '/' ) {
- return $base;
- } else {
-
- return preg_replace( '!^[^:]+://[^/]+/!', '/', $base );
- }
- }
-
- function getFullRequestURL() {
- global $wgServer;
- return $wgServer . $this->getRequestURL();
- }
-
- function appendQuery( $query ) {
- global $wgTitle;
- $basequery = '';
- foreach( $_GET as $var => $val ) {
- if ( $var == 'title' )
- continue;
- if ( is_array( $val ) )
-
- continue;
- $basequery .= '&' . urlencode( $var ) . '=' . urlencode( $val );
- }
- $basequery .= '&' . $query;
-
- $basequery = substr( $basequery, 1 );
- return $wgTitle->getLocalURL( $basequery );
- }
-
- function escapeAppendQuery( $query ) {
- return htmlspecialchars( $this->appendQuery( $query ) );
- }
- function appendQueryValue( $key, $value, $onlyquery = false ) {
- return $this->appendQueryArray( array( $key => $value ), $onlyquery );
- }
-
- function appendQueryArray( $array, $onlyquery = false ) {
- global $wgTitle;
- $newquery = $_GET;
- unset( $newquery['title'] );
- $newquery = array_merge( $newquery, $array );
- $query = wfArrayToCGI( $newquery );
- return $onlyquery ? $query : $wgTitle->getLocalURL( $query );
- }
-
- function getLimitOffset( $deflimit = 50, $optionname = 'rclimit' ) {
- global $wgUser;
- $limit = $this->getInt( 'limit', 0 );
- if( $limit < 0 ) $limit = 0;
- if( ( $limit == 0 ) && ( $optionname != '' ) ) {
- $limit = (int)$wgUser->getOption( $optionname );
- }
- if( $limit <= 0 ) $limit = $deflimit;
- if( $limit > 5000 ) $limit = 5000;
- $offset = $this->getInt( 'offset', 0 );
- if( $offset < 0 ) $offset = 0;
- return array( $limit, $offset );
- }
-
- function getFileTempname( $key ) {
- if( !isset( $_FILES[$key] ) ) {
- return NULL;
- }
- return $_FILES[$key]['tmp_name'];
- }
-
- function getFileSize( $key ) {
- if( !isset( $_FILES[$key] ) ) {
- return 0;
- }
- return $_FILES[$key]['size'];
- }
-
- function getUploadError( $key ) {
- if( !isset( $_FILES[$key] ) || !isset( $_FILES[$key]['error'] ) ) {
- return 0;
- }
- return $_FILES[$key]['error'];
- }
-
- function getFileName( $key ) {
- if( !isset( $_FILES[$key] ) ) {
- return NULL;
- }
- $name = $_FILES[$key]['name'];
-
-
- $name = Sanitizer::decodeCharReferences( $name );
- $name = UtfNormal::cleanUp( $name );
- wfDebug( "WebRequest::getFileName() '" . $_FILES[$key]['name'] . "' normalized to '$name'\n" );
- return $name;
- }
-
- function response() {
-
- if (!is_object($this->_response)) {
- $this->_response = new WebResponse;
- }
- return $this->_response;
- }
-
- function getHeader( $name ) {
- $name = strtoupper( $name );
- if ( function_exists( 'apache_request_headers' ) ) {
- if ( !isset( $this->headers ) ) {
- $this->headers = array();
- foreach ( apache_request_headers() as $tempName => $tempValue ) {
- $this->headers[ strtoupper( $tempName ) ] = $tempValue;
- }
- }
- if ( isset( $this->headers[$name] ) ) {
- return $this->headers[$name];
- } else {
- return false;
- }
- } else {
- $name = 'HTTP_' . str_replace( '-', '_', $name );
- if ( isset( $_SERVER[$name] ) ) {
- return $_SERVER[$name];
- } else {
- return false;
- }
- }
- }
-
-
- function getSessionData( $key ) {
- if( !isset( $_SESSION[$key] ) )
- return null;
- return $_SESSION[$key];
- }
- function setSessionData( $key, $data ) {
- $_SESSION[$key] = $data;
- }
- }
- class FauxRequest extends WebRequest {
- var $wasPosted = false;
-
- function FauxRequest( $data, $wasPosted = false, $session = null ) {
- if( is_array( $data ) ) {
- $this->data = $data;
- } else {
- throw new MWException( "FauxRequest() got bogus data" );
- }
- $this->wasPosted = $wasPosted;
- $this->headers = array();
- $this->session = $session ? $session : array();
- }
-
- function notImplemented( $method ) {
- throw new MWException( "{$method}() not implemented" );
- }
- function getText( $name, $default = '' ) {
-
- return (string)$this->getVal( $name, $default );
- }
- function getValues() {
- return $this->data;
- }
- function wasPosted() {
- return $this->wasPosted;
- }
- function checkSessionCookie() {
- return false;
- }
- function getRequestURL() {
- $this->notImplemented( __METHOD__ );
- }
- function appendQuery( $query ) {
- $this->notImplemented( __METHOD__ );
- }
- function getHeader( $name ) {
- return isset( $this->headers[$name] ) ? $this->headers[$name] : false;
- }
- function getSessionData( $key ) {
- if( !isset( $this->session[$key] ) )
- return null;
- return $this->session[$key];
- }
- function setSessionData( $key, $data ) {
- $this->notImplemented( __METHOD__ );
- }
- }
|