// which will end up triggering HTML detection and execution, hence
// XSS injection and all that entails.
//
-// Ensure that all access is through the canonical entry point...
-//
-if( isset( $_SERVER['SCRIPT_NAME'] ) ) {
- $url = $_SERVER['SCRIPT_NAME'];
-} else {
- $url = $_SERVER['URL'];
-}
-if( strcmp( "$wgScriptPath/api$wgScriptExtension", $url ) ) {
+if( $wgRequest->isPathInfoBad() ) {
wfHttpError( 403, 'Forbidden',
- 'API must be accessed through the primary script entry point.' );
+ 'Invalid file extension found in PATH_INFO. ' .
+ 'The API must be accessed through the primary script entry point.' );
return;
}
die(1);
}
+// Selectively allow cross-site AJAX
+if ( $wgCrossSiteAJAXdomains && isset($_SERVER['HTTP_ORIGIN']) ) {
+ if ( $wgCrossSiteAJAXdomains == '*' ) {
+ header( "Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}" );
+ header( 'Access-Control-Allow-Credentials: true' );
+ } elseif ( $wgCrossSiteAJAXdomainsRegex ) {
+ foreach ( $wgCrossSiteAJAXdomains as $regex ) {
+ if ( preg_match( $regex, $_SERVER['HTTP_ORIGIN'] ) ) {
+ header( "Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}" );
+ header( 'Access-Control-Allow-Credentials: true' );
+ break;
+ }
+ }
+ } elseif ( in_array( $_SERVER['HTTP_ORIGIN'], $wgCrossSiteAJAXdomains ) ) {
+ header( "Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}" );
+ header( 'Access-Control-Allow-Credentials: true' );
+ }
+}
+
+// So extensions can check whether they're running in API mode
+define('MW_API', true);
+
+// Set a dummy $wgTitle, because $wgTitle == null breaks various things
+// In a perfect world this wouldn't be necessary
+$wgTitle = Title::newFromText('API');
+
/* Construct an ApiMain with the arguments passed via the URL. What we get back
* is some form of an ApiMain, possibly even one that produces an error message,
* but we don't care here, as that is handled by the ctor.