Fix Bug #29231: PhpHttpRequest doesn't support HTTPS
[lhc/web/wiklou.git] / api.php
1 <?php
2
3 /**
4 * API for MediaWiki 1.8+
5 *
6 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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
26 /**
27 * This file is the entry point for all API queries. It begins by checking
28 * whether the API is enabled on this wiki; if not, it informs the user that
29 * s/he should set $wgEnableAPI to true and exits. Otherwise, it constructs
30 * a new ApiMain using the parameter passed to it as an argument in the URL
31 * ('?action=') and with write-enabled set to the value of $wgEnableWriteAPI
32 * as specified in LocalSettings.php. It then invokes "execute()" on the
33 * ApiMain object instance, which produces output in the format sepecified
34 * in the URL.
35 */
36
37 // So extensions (and other code) can check whether they're running in API mode
38 define( 'MW_API', true );
39
40 // We want a plain message on catastrophic errors that machines can identify
41 function wfDie( $msg = '' ) {
42 header( $_SERVER['SERVER_PROTOCOL'] . ' 500 MediaWiki configuration Error', true, 500 );
43 echo $msg;
44 die( 1 );
45 }
46
47 // Die on unsupported PHP versions
48 if( !function_exists( 'version_compare' ) || version_compare( phpversion(), '5.2.3' ) < 0 ){
49 $version = htmlspecialchars( $wgVersion );
50 wfDie( "MediaWiki $version requires at least PHP version 5.2.3." );
51 }
52
53 // Initialise common code.
54 if ( isset( $_SERVER['MW_COMPILED'] ) ) {
55 require ( 'phase3/includes/WebStart.php' );
56 } else {
57 require ( dirname( __FILE__ ) . '/includes/WebStart.php' );
58 }
59
60 wfProfileIn( 'api.php' );
61 $starttime = microtime( true );
62
63 // URL safety checks
64 //
65 // See RawPage.php for details; summary is that MSIE can override the
66 // Content-Type if it sees a recognized extension on the URL, such as
67 // might be appended via PATH_INFO after 'api.php'.
68 //
69 // Some data formats can end up containing unfiltered user-provided data
70 // which will end up triggering HTML detection and execution, hence
71 // XSS injection and all that entails.
72 //
73 if ( $wgRequest->isPathInfoBad() ) {
74 wfHttpError( 403, 'Forbidden',
75 'Invalid file extension found in PATH_INFO or QUERY_STRING.' );
76 return;
77 }
78
79 // Verify that the API has not been disabled
80 if ( !$wgEnableAPI ) {
81 wfDie( 'MediaWiki API is not enabled for this site. Add the following line to your LocalSettings.php'
82 . '<pre><b>$wgEnableAPI=true;</b></pre>'
83 );
84 }
85
86 // Selectively allow cross-site AJAX
87
88 /*
89 * Helper function to convert wildcard string into a regex
90 * '*' => '.*?'
91 * '?' => '.'
92 * @ return string
93 */
94 function convertWildcard( $search ) {
95 $search = preg_quote( $search, '/' );
96 $search = str_replace(
97 array( '\*', '\?' ),
98 array( '.*?', '.' ),
99 $search
100 );
101 return "/$search/";
102 }
103
104 if ( $wgCrossSiteAJAXdomains && isset( $_SERVER['HTTP_ORIGIN'] ) ) {
105 $exceptions = array_map( 'convertWildcard', $wgCrossSiteAJAXdomainExceptions );
106 $regexes = array_map( 'convertWildcard', $wgCrossSiteAJAXdomains );
107 foreach ( $regexes as $regex ) {
108 if ( preg_match( $regex, $_SERVER['HTTP_ORIGIN'] ) ) {
109 foreach ( $exceptions as $exc ) { // Check against exceptions
110 if ( preg_match( $exc, $_SERVER['HTTP_ORIGIN'] ) ) {
111 break 2;
112 }
113 }
114 header( "Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}" );
115 header( 'Access-Control-Allow-Credentials: true' );
116 break;
117 }
118 }
119 }
120
121 // Set a dummy $wgTitle, because $wgTitle == null breaks various things
122 // In a perfect world this wouldn't be necessary
123 $wgTitle = Title::makeTitle( NS_MAIN, 'API' );
124
125 /* Construct an ApiMain with the arguments passed via the URL. What we get back
126 * is some form of an ApiMain, possibly even one that produces an error message,
127 * but we don't care here, as that is handled by the ctor.
128 */
129 $processor = new ApiMain( $wgRequest, $wgEnableWriteAPI );
130
131 // Process data & print results
132 $processor->execute();
133
134 // Execute any deferred updates
135 wfDoUpdates();
136
137 // Log what the user did, for book-keeping purposes.
138 $endtime = microtime( true );
139 wfProfileOut( 'api.php' );
140 wfLogProfilingData();
141
142 // Log the request
143 if ( $wgAPIRequestLog ) {
144 $items = array(
145 wfTimestamp( TS_MW ),
146 $endtime - $starttime,
147 wfGetIP(),
148 $_SERVER['HTTP_USER_AGENT']
149 );
150 $items[] = $wgRequest->wasPosted() ? 'POST' : 'GET';
151 $module = $processor->getModule();
152 if ( $module->mustBePosted() ) {
153 $items[] = "action=" . $wgRequest->getVal( 'action' );
154 } else {
155 $items[] = wfArrayToCGI( $wgRequest->getValues() );
156 }
157 wfErrorLog( implode( ',', $items ) . "\n", $wgAPIRequestLog );
158 wfDebug( "Logged API request to $wgAPIRequestLog\n" );
159 }
160
161 // Shut down the database. foo()->bar() syntax is not supported in PHP4: we won't ever actually
162 // get here to worry about whether this should be = or =&, but the file has to parse properly.
163 $lb = wfGetLBFactory();
164 $lb->shutdown();
165