Simplify $assoc check
[mediawiki.git] / includes / AjaxDispatcher.php
blob5bc9f06742739c3d26fcf30179e4a8f86b50e6e7
1 <?php
2 /**
3 * @defgroup Ajax Ajax
5 * @file
6 * @ingroup Ajax
7 * Handle ajax requests and send them to the proper handler.
8 */
10 /**
11 * Object-Oriented Ajax functions.
12 * @ingroup Ajax
14 class AjaxDispatcher {
15 /** The way the request was made, either a 'get' or a 'post' */
16 private $mode;
18 /** Name of the requested handler */
19 private $func_name;
21 /** Arguments passed */
22 private $args;
24 /** Load up our object with user supplied data */
25 function __construct() {
26 wfProfileIn( __METHOD__ );
28 $this->mode = "";
30 if ( ! empty( $_GET["rs"] ) ) {
31 $this->mode = "get";
34 if ( !empty( $_POST["rs"] ) ) {
35 $this->mode = "post";
38 switch( $this->mode ) {
39 case 'get':
40 $this->func_name = isset( $_GET["rs"] ) ? $_GET["rs"] : '';
41 if ( ! empty( $_GET["rsargs"] ) ) {
42 $this->args = $_GET["rsargs"];
43 } else {
44 $this->args = array();
46 break;
47 case 'post':
48 $this->func_name = isset( $_POST["rs"] ) ? $_POST["rs"] : '';
49 if ( ! empty( $_POST["rsargs"] ) ) {
50 $this->args = $_POST["rsargs"];
51 } else {
52 $this->args = array();
54 break;
55 default:
56 wfProfileOut( __METHOD__ );
57 return;
58 # Or we could throw an exception:
59 # throw new MWException( __METHOD__ . ' called without any data (mode empty).' );
62 wfProfileOut( __METHOD__ );
65 /** Pass the request to our internal function.
66 * BEWARE! Data are passed as they have been supplied by the user,
67 * they should be carefully handled in the function processing the
68 * request.
70 function performAction() {
71 global $wgAjaxExportList, $wgOut, $wgUser;
73 if ( empty( $this->mode ) ) {
74 return;
77 wfProfileIn( __METHOD__ );
79 if ( ! in_array( $this->func_name, $wgAjaxExportList ) ) {
80 wfDebug( __METHOD__ . ' Bad Request for unknown function ' . $this->func_name . "\n" );
82 wfHttpError(
83 400,
84 'Bad Request',
85 "unknown function " . (string) $this->func_name
87 } elseif ( !in_array( 'read', User::getGroupPermissions( array( '*' ) ), true )
88 && !$wgUser->isAllowed( 'read' ) )
90 wfHttpError(
91 403,
92 'Forbidden',
93 'You must log in to view pages.' );
94 } else {
95 wfDebug( __METHOD__ . ' dispatching ' . $this->func_name . "\n" );
97 if ( strpos( $this->func_name, '::' ) !== false ) {
98 $func = explode( '::', $this->func_name, 2 );
99 } else {
100 $func = $this->func_name;
103 try {
104 $result = call_user_func_array( $func, $this->args );
106 if ( $result === false || $result === null ) {
107 wfDebug( __METHOD__ . ' ERROR while dispatching '
108 . $this->func_name . "(" . var_export( $this->args, true ) . "): "
109 . "no data returned\n" );
111 wfHttpError( 500, 'Internal Error',
112 "{$this->func_name} returned no data" );
113 } else {
114 if ( is_string( $result ) ) {
115 $result = new AjaxResponse( $result );
118 $result->sendHeaders();
119 $result->printText();
121 wfDebug( __METHOD__ . ' dispatch complete for ' . $this->func_name . "\n" );
123 } catch ( Exception $e ) {
124 wfDebug( __METHOD__ . ' ERROR while dispatching '
125 . $this->func_name . "(" . var_export( $this->args, true ) . "): "
126 . get_class( $e ) . ": " . $e->getMessage() . "\n" );
128 if ( !headers_sent() ) {
129 wfHttpError( 500, 'Internal Error',
130 $e->getMessage() );
131 } else {
132 print $e->getMessage();
137 $wgOut = null;
138 wfProfileOut( __METHOD__ );