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