Merge "Revert "Remove useless $out parameter from SkinTemplate::prepareQuickTemplate()""
[lhc/web/wiklou.git] / includes / AjaxDispatcher.php
1 <?php
2 /**
3 * Handle ajax requests and send them to the proper handler.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Ajax
22 */
23
24 /**
25 * @defgroup Ajax Ajax
26 */
27
28 /**
29 * Object-Oriented Ajax functions.
30 * @ingroup Ajax
31 */
32 class AjaxDispatcher {
33 /**
34 * The way the request was made, either a 'get' or a 'post'
35 * @var string $mode
36 */
37 private $mode;
38
39 /**
40 * Name of the requested handler
41 * @var string $func_name
42 */
43 private $func_name;
44
45 /** Arguments passed
46 * @var array $args
47 */
48 private $args;
49
50 /**
51 * @var Config
52 */
53 private $config;
54
55 /**
56 * Load up our object with user supplied data
57 */
58 function __construct( Config $config ) {
59 wfProfileIn( __METHOD__ );
60
61 $this->config = $config;
62
63 $this->mode = "";
64
65 if ( !empty( $_GET["rs"] ) ) {
66 $this->mode = "get";
67 }
68
69 if ( !empty( $_POST["rs"] ) ) {
70 $this->mode = "post";
71 }
72
73 switch ( $this->mode ) {
74 case 'get':
75 $this->func_name = isset( $_GET["rs"] ) ? $_GET["rs"] : '';
76 if ( !empty( $_GET["rsargs"] ) ) {
77 $this->args = $_GET["rsargs"];
78 } else {
79 $this->args = array();
80 }
81 break;
82 case 'post':
83 $this->func_name = isset( $_POST["rs"] ) ? $_POST["rs"] : '';
84 if ( !empty( $_POST["rsargs"] ) ) {
85 $this->args = $_POST["rsargs"];
86 } else {
87 $this->args = array();
88 }
89 break;
90 default:
91 wfProfileOut( __METHOD__ );
92 return;
93 # Or we could throw an exception:
94 # throw new MWException( __METHOD__ . ' called without any data (mode empty).' );
95 }
96
97 wfProfileOut( __METHOD__ );
98 }
99
100 /**
101 * Pass the request to our internal function.
102 * BEWARE! Data are passed as they have been supplied by the user,
103 * they should be carefully handled in the function processing the
104 * request.
105 *
106 * @param User $user
107 */
108 function performAction( User $user ) {
109 if ( empty( $this->mode ) ) {
110 return;
111 }
112
113 wfProfileIn( __METHOD__ );
114
115 if ( !in_array( $this->func_name, $this->config->get( 'AjaxExportList' ) ) ) {
116 wfDebug( __METHOD__ . ' Bad Request for unknown function ' . $this->func_name . "\n" );
117
118 wfHttpError(
119 400,
120 'Bad Request',
121 "unknown function " . $this->func_name
122 );
123 } elseif ( !User::isEveryoneAllowed( 'read' ) && !$user->isAllowed( 'read' ) ) {
124 wfHttpError(
125 403,
126 'Forbidden',
127 'You are not allowed to view pages.' );
128 } else {
129 wfDebug( __METHOD__ . ' dispatching ' . $this->func_name . "\n" );
130
131 try {
132 $result = call_user_func_array( $this->func_name, $this->args );
133
134 if ( $result === false || $result === null ) {
135 wfDebug( __METHOD__ . ' ERROR while dispatching '
136 . $this->func_name . "(" . var_export( $this->args, true ) . "): "
137 . "no data returned\n" );
138
139 wfHttpError( 500, 'Internal Error',
140 "{$this->func_name} returned no data" );
141 } else {
142 if ( is_string( $result ) ) {
143 $result = new AjaxResponse( $result );
144 }
145
146 $result->sendHeaders();
147 $result->printText();
148
149 wfDebug( __METHOD__ . ' dispatch complete for ' . $this->func_name . "\n" );
150 }
151 } catch ( Exception $e ) {
152 wfDebug( __METHOD__ . ' ERROR while dispatching '
153 . $this->func_name . "(" . var_export( $this->args, true ) . "): "
154 . get_class( $e ) . ": " . $e->getMessage() . "\n" );
155
156 if ( !headers_sent() ) {
157 wfHttpError( 500, 'Internal Error',
158 $e->getMessage() );
159 } else {
160 print $e->getMessage();
161 }
162 }
163 }
164
165 wfProfileOut( __METHOD__ );
166 }
167 }