Add Html::input() convenience function
[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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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
42 // URL safety checks
43 //
44 // See RawPage.php for details; summary is that MSIE can override the
45 // Content-Type if it sees a recognized extension on the URL, such as
46 // might be appended via PATH_INFO after 'api.php'.
47 //
48 // Some data formats can end up containing unfiltered user-provided data
49 // which will end up triggering HTML detection and execution, hence
50 // XSS injection and all that entails.
51 //
52 // Ensure that all access is through the canonical entry point...
53 //
54 if( isset( $_SERVER['SCRIPT_NAME'] ) ) {
55 $url = $_SERVER['SCRIPT_NAME'];
56 } else {
57 $url = $_SERVER['URL'];
58 }
59 if( strcmp( "$wgScriptPath/api$wgScriptExtension", $url ) ) {
60 wfHttpError( 403, 'Forbidden',
61 'API must be accessed through the primary script entry point.' );
62 return;
63 }
64
65 // Verify that the API has not been disabled
66 if (!$wgEnableAPI) {
67 echo 'MediaWiki API is not enabled for this site. Add the following line to your LocalSettings.php';
68 echo '<pre><b>$wgEnableAPI=true;</b></pre>';
69 die(1);
70 }
71
72 // Selectively allow cross-site AJAX
73 if ( $wgCrossSiteAJAXdomains && isset($_SERVER['HTTP_ORIGIN']) ) {
74 if ( $wgCrossSiteAJAXdomains == '*' ) {
75 header( "Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}" );
76 header( 'Access-Control-Allow-Credentials: true' );
77 } elseif ( $wgCrossSiteAJAXdomainsRegex ) {
78 foreach ( $wgCrossSiteAJAXdomains as $regex ) {
79 if ( preg_match( $regex, $_SERVER['HTTP_ORIGIN'] ) ) {
80 header( "Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}" );
81 header( 'Access-Control-Allow-Credentials: true' );
82 break;
83 }
84 }
85 } elseif ( in_array( $_SERVER['HTTP_ORIGIN'], $wgCrossSiteAJAXdomains ) ) {
86 header( "Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}" );
87 header( 'Access-Control-Allow-Credentials: true' );
88 }
89 }
90
91 // So extensions can check whether they're running in API mode
92 define('MW_API', true);
93
94 // Set a dummy $wgTitle, because $wgTitle == null breaks various things
95 // In a perfect world this wouldn't be necessary
96 $wgTitle = Title::newFromText('API');
97
98 /* Construct an ApiMain with the arguments passed via the URL. What we get back
99 * is some form of an ApiMain, possibly even one that produces an error message,
100 * but we don't care here, as that is handled by the ctor.
101 */
102 $processor = new ApiMain($wgRequest, $wgEnableWriteAPI);
103
104 // Process data & print results
105 $processor->execute();
106
107 // Execute any deferred updates
108 wfDoUpdates();
109
110 // Log what the user did, for book-keeping purposes.
111 wfProfileOut('api.php');
112 wfLogProfilingData();
113
114 // Shut down the database
115 wfGetLBFactory()->shutdown();
116