Localisation updates from http://translatewiki.net.
[mediawiki.git] / includes / AjaxDispatcher.php
blobe22fe20af95b38b8cf9c1ee6294fe6f6ebb50bc6
1 <?php
2 /**
3 * Handle ajax requests and send them to the proper handler.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Ajax
24 /**
25 * @defgroup Ajax Ajax
28 /**
29 * Object-Oriented Ajax functions.
30 * @ingroup Ajax
32 class AjaxDispatcher {
33 /**
34 * The way the request was made, either a 'get' or a 'post'
35 * @var string $mode
37 private $mode;
39 /**
40 * Name of the requested handler
41 * @var string $func_name
43 private $func_name;
45 /** Arguments passed
46 * @var array $args
48 private $args;
50 /**
51 * Load up our object with user supplied data
53 function __construct() {
54 wfProfileIn( __METHOD__ );
56 $this->mode = "";
58 if ( ! empty( $_GET["rs"] ) ) {
59 $this->mode = "get";
62 if ( !empty( $_POST["rs"] ) ) {
63 $this->mode = "post";
66 switch ( $this->mode ) {
67 case 'get':
68 $this->func_name = isset( $_GET["rs"] ) ? $_GET["rs"] : '';
69 if ( ! empty( $_GET["rsargs"] ) ) {
70 $this->args = $_GET["rsargs"];
71 } else {
72 $this->args = array();
74 break;
75 case 'post':
76 $this->func_name = isset( $_POST["rs"] ) ? $_POST["rs"] : '';
77 if ( ! empty( $_POST["rsargs"] ) ) {
78 $this->args = $_POST["rsargs"];
79 } else {
80 $this->args = array();
82 break;
83 default:
84 wfProfileOut( __METHOD__ );
85 return;
86 # Or we could throw an exception:
87 # throw new MWException( __METHOD__ . ' called without any data (mode empty).' );
90 wfProfileOut( __METHOD__ );
93 /**
94 * Pass the request to our internal function.
95 * BEWARE! Data are passed as they have been supplied by the user,
96 * they should be carefully handled in the function processing the
97 * request.
99 function performAction() {
100 global $wgAjaxExportList, $wgUser;
102 if ( empty( $this->mode ) ) {
103 return;
106 wfProfileIn( __METHOD__ );
108 if ( ! in_array( $this->func_name, $wgAjaxExportList ) ) {
109 wfDebug( __METHOD__ . ' Bad Request for unknown function ' . $this->func_name . "\n" );
111 wfHttpError(
112 400,
113 'Bad Request',
114 "unknown function " . (string) $this->func_name
116 } elseif ( !in_array( 'read', User::getGroupPermissions( array( '*' ) ), true )
117 && !$wgUser->isAllowed( 'read' ) )
119 wfHttpError(
120 403,
121 'Forbidden',
122 'You must log in to view pages.' );
123 } else {
124 wfDebug( __METHOD__ . ' dispatching ' . $this->func_name . "\n" );
126 try {
127 $result = call_user_func_array( $this->func_name, $this->args );
129 if ( $result === false || $result === null ) {
130 wfDebug( __METHOD__ . ' ERROR while dispatching '
131 . $this->func_name . "(" . var_export( $this->args, true ) . "): "
132 . "no data returned\n" );
134 wfHttpError( 500, 'Internal Error',
135 "{$this->func_name} returned no data" );
136 } else {
137 if ( is_string( $result ) ) {
138 $result = new AjaxResponse( $result );
141 $result->sendHeaders();
142 $result->printText();
144 wfDebug( __METHOD__ . ' dispatch complete for ' . $this->func_name . "\n" );
146 } catch ( Exception $e ) {
147 wfDebug( __METHOD__ . ' ERROR while dispatching '
148 . $this->func_name . "(" . var_export( $this->args, true ) . "): "
149 . get_class( $e ) . ": " . $e->getMessage() . "\n" );
151 if ( !headers_sent() ) {
152 wfHttpError( 500, 'Internal Error',
153 $e->getMessage() );
154 } else {
155 print $e->getMessage();
160 wfProfileOut( __METHOD__ );