Added SQLite version of the patch since the other one has syntax errors on SQLite
[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 // Initialise common code
38 require ( dirname( __FILE__ ) . '/includes/WebStart.php' );
39
40 wfProfileIn( 'api.php' );
41 $starttime = microtime( true );
42
43 // URL safety checks
44 //
45 // See RawPage.php for details; summary is that MSIE can override the
46 // Content-Type if it sees a recognized extension on the URL, such as
47 // might be appended via PATH_INFO after 'api.php'.
48 //
49 // Some data formats can end up containing unfiltered user-provided data
50 // which will end up triggering HTML detection and execution, hence
51 // XSS injection and all that entails.
52 //
53 if ( $wgRequest->isPathInfoBad() ) {
54 wfHttpError( 403, 'Forbidden',
55 'Invalid file extension found in PATH_INFO. ' .
56 'The API must be accessed through the primary script entry point.' );
57 return;
58 }
59
60 // Verify that the API has not been disabled
61 if ( !$wgEnableAPI ) {
62 echo 'MediaWiki API is not enabled for this site. Add the following line to your LocalSettings.php';
63 echo '<pre><b>$wgEnableAPI=true;</b></pre>';
64 die( 1 );
65 }
66
67 // Selectively allow cross-site AJAX
68
69 /*
70 * Helper function to convert wildcard string into a regex
71 * '*' => '.*?'
72 * '?' => '.'
73 * @ return string
74 */
75 function convertWildcard( $search ) {
76 $search = preg_quote( $search, '/' );
77 $search = str_replace(
78 array( '\*', '\?' ),
79 array( '.*?', '.' ),
80 $search
81 );
82 return "/$search/";
83 }
84
85 if ( $wgCrossSiteAJAXdomains && isset( $_SERVER['HTTP_ORIGIN'] ) ) {
86 $exceptions = array_map( 'convertWildcard', $wgCrossSiteAJAXdomainExceptions );
87 $regexes = array_map( 'convertWildcard', $wgCrossSiteAJAXdomains );
88 foreach ( $regexes as $regex ) {
89 if ( preg_match( $regex, $_SERVER['HTTP_ORIGIN'] ) ) {
90 foreach ( $exceptions as $exc ) { // Check against exceptions
91 if ( preg_match( $exc, $_SERVER['HTTP_ORIGIN'] ) ) {
92 break 2;
93 }
94 }
95 header( "Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}" );
96 header( 'Access-Control-Allow-Credentials: true' );
97 break;
98 }
99 }
100 }
101
102 // So extensions can check whether they're running in API mode
103 define( 'MW_API', true );
104
105 // Set a dummy $wgTitle, because $wgTitle == null breaks various things
106 // In a perfect world this wouldn't be necessary
107 $wgTitle = Title::makeTitle( NS_MAIN, 'API' );
108
109 /* Construct an ApiMain with the arguments passed via the URL. What we get back
110 * is some form of an ApiMain, possibly even one that produces an error message,
111 * but we don't care here, as that is handled by the ctor.
112 */
113 $processor = new ApiMain( $wgRequest, $wgEnableWriteAPI );
114
115 // Process data & print results
116 $processor->execute();
117
118 // Execute any deferred updates
119 wfDoUpdates();
120
121 // Log what the user did, for book-keeping purposes.
122 $endtime = microtime( true );
123 wfProfileOut( 'api.php' );
124 wfLogProfilingData();
125
126 // Log the request
127 if ( $wgAPIRequestLog ) {
128 $items = array(
129 wfTimestamp( TS_MW ),
130 $endtime - $starttime,
131 wfGetIP(),
132 $_SERVER['HTTP_USER_AGENT']
133 );
134 $items[] = $wgRequest->wasPosted() ? 'POST' : 'GET';
135 if ( $processor->getModule()->mustBePosted() ) {
136 $items[] = "action=" . $wgRequest->getVal( 'action' );
137 } else {
138 $items[] = wfArrayToCGI( $wgRequest->getValues() );
139 }
140 wfErrorLog( implode( ',', $items ) . "\n", $wgAPIRequestLog );
141 wfDebug( "Logged API request to $wgAPIRequestLog\n" );
142 }
143
144 // Shut down the database
145 wfGetLBFactory()->shutdown();
146