Merge "Allow to send the memory usage with UDP profiler."
[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 * Load up our object with user supplied data
52 */
53 function __construct() {
54 wfProfileIn( __METHOD__ );
55
56 $this->mode = "";
57
58 if ( !empty( $_GET["rs"] ) ) {
59 $this->mode = "get";
60 }
61
62 if ( !empty( $_POST["rs"] ) ) {
63 $this->mode = "post";
64 }
65
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();
73 }
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();
81 }
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).' );
88 }
89
90 wfProfileOut( __METHOD__ );
91 }
92
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.
98 */
99 function performAction() {
100 global $wgAjaxExportList, $wgUser;
101
102 if ( empty( $this->mode ) ) {
103 return;
104 }
105
106 wfProfileIn( __METHOD__ );
107
108 if ( !in_array( $this->func_name, $wgAjaxExportList ) ) {
109 wfDebug( __METHOD__ . ' Bad Request for unknown function ' . $this->func_name . "\n" );
110
111 wfHttpError(
112 400,
113 'Bad Request',
114 "unknown function " . $this->func_name
115 );
116 } elseif ( !User::isEveryoneAllowed( 'read' ) && !$wgUser->isAllowed( 'read' ) ) {
117 wfHttpError(
118 403,
119 'Forbidden',
120 'You are not allowed to view pages.' );
121 } else {
122 wfDebug( __METHOD__ . ' dispatching ' . $this->func_name . "\n" );
123
124 try {
125 $result = call_user_func_array( $this->func_name, $this->args );
126
127 if ( $result === false || $result === null ) {
128 wfDebug( __METHOD__ . ' ERROR while dispatching '
129 . $this->func_name . "(" . var_export( $this->args, true ) . "): "
130 . "no data returned\n" );
131
132 wfHttpError( 500, 'Internal Error',
133 "{$this->func_name} returned no data" );
134 } else {
135 if ( is_string( $result ) ) {
136 $result = new AjaxResponse( $result );
137 }
138
139 $result->sendHeaders();
140 $result->printText();
141
142 wfDebug( __METHOD__ . ' dispatch complete for ' . $this->func_name . "\n" );
143 }
144 } catch ( Exception $e ) {
145 wfDebug( __METHOD__ . ' ERROR while dispatching '
146 . $this->func_name . "(" . var_export( $this->args, true ) . "): "
147 . get_class( $e ) . ": " . $e->getMessage() . "\n" );
148
149 if ( !headers_sent() ) {
150 wfHttpError( 500, 'Internal Error',
151 $e->getMessage() );
152 } else {
153 print $e->getMessage();
154 }
155 }
156 }
157
158 wfProfileOut( __METHOD__ );
159 }
160 }