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