Added missing GPLv2 headers in some places.
[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 /** The way the request was made, either a 'get' or a 'post' */
34 private $mode;
35
36 /** Name of the requested handler */
37 private $func_name;
38
39 /** Arguments passed */
40 private $args;
41
42 /** Load up our object with user supplied data */
43 function __construct() {
44 wfProfileIn( __METHOD__ );
45
46 $this->mode = "";
47
48 if ( ! empty( $_GET["rs"] ) ) {
49 $this->mode = "get";
50 }
51
52 if ( !empty( $_POST["rs"] ) ) {
53 $this->mode = "post";
54 }
55
56 switch( $this->mode ) {
57 case 'get':
58 $this->func_name = isset( $_GET["rs"] ) ? $_GET["rs"] : '';
59 if ( ! empty( $_GET["rsargs"] ) ) {
60 $this->args = $_GET["rsargs"];
61 } else {
62 $this->args = array();
63 }
64 break;
65 case 'post':
66 $this->func_name = isset( $_POST["rs"] ) ? $_POST["rs"] : '';
67 if ( ! empty( $_POST["rsargs"] ) ) {
68 $this->args = $_POST["rsargs"];
69 } else {
70 $this->args = array();
71 }
72 break;
73 default:
74 wfProfileOut( __METHOD__ );
75 return;
76 # Or we could throw an exception:
77 # throw new MWException( __METHOD__ . ' called without any data (mode empty).' );
78 }
79
80 wfProfileOut( __METHOD__ );
81 }
82
83 /** Pass the request to our internal function.
84 * BEWARE! Data are passed as they have been supplied by the user,
85 * they should be carefully handled in the function processing the
86 * request.
87 */
88 function performAction() {
89 global $wgAjaxExportList, $wgOut, $wgUser;
90
91 if ( empty( $this->mode ) ) {
92 return;
93 }
94
95 wfProfileIn( __METHOD__ );
96
97 if ( ! in_array( $this->func_name, $wgAjaxExportList ) ) {
98 wfDebug( __METHOD__ . ' Bad Request for unknown function ' . $this->func_name . "\n" );
99
100 wfHttpError(
101 400,
102 'Bad Request',
103 "unknown function " . (string) $this->func_name
104 );
105 } elseif ( !in_array( 'read', User::getGroupPermissions( array( '*' ) ), true )
106 && !$wgUser->isAllowed( 'read' ) )
107 {
108 wfHttpError(
109 403,
110 'Forbidden',
111 'You must log in to view pages.' );
112 } else {
113 wfDebug( __METHOD__ . ' dispatching ' . $this->func_name . "\n" );
114
115 if ( strpos( $this->func_name, '::' ) !== false ) {
116 $func = explode( '::', $this->func_name, 2 );
117 } else {
118 $func = $this->func_name;
119 }
120
121 try {
122 $result = call_user_func_array( $func, $this->args );
123
124 if ( $result === false || $result === null ) {
125 wfDebug( __METHOD__ . ' ERROR while dispatching '
126 . $this->func_name . "(" . var_export( $this->args, true ) . "): "
127 . "no data returned\n" );
128
129 wfHttpError( 500, 'Internal Error',
130 "{$this->func_name} returned no data" );
131 } else {
132 if ( is_string( $result ) ) {
133 $result = new AjaxResponse( $result );
134 }
135
136 $result->sendHeaders();
137 $result->printText();
138
139 wfDebug( __METHOD__ . ' dispatch complete for ' . $this->func_name . "\n" );
140 }
141 } catch ( Exception $e ) {
142 wfDebug( __METHOD__ . ' ERROR while dispatching '
143 . $this->func_name . "(" . var_export( $this->args, true ) . "): "
144 . get_class( $e ) . ": " . $e->getMessage() . "\n" );
145
146 if ( !headers_sent() ) {
147 wfHttpError( 500, 'Internal Error',
148 $e->getMessage() );
149 } else {
150 print $e->getMessage();
151 }
152 }
153 }
154
155 $wgOut = null;
156 wfProfileOut( __METHOD__ );
157 }
158 }