More RTL fixes and also another fix to r52944 which broke the inclusion of common...
[mediawiki.git] / includes / AjaxDispatcher.php
blobc489cf1cef4777e59014f717f41578e7378f0a07
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 if( !(defined( 'MEDIAWIKI' ) && $wgUseAjax ) ) {
11 die( 1 );
14 require_once( 'AjaxFunctions.php' );
16 /**
17 * Object-Oriented Ajax functions.
18 * @ingroup Ajax
20 class AjaxDispatcher {
21 /** The way the request was made, either a 'get' or a 'post' */
22 private $mode;
24 /** Name of the requested handler */
25 private $func_name;
27 /** Arguments passed */
28 private $args;
30 /** Load up our object with user supplied data */
31 function __construct() {
32 wfProfileIn( __METHOD__ );
34 $this->mode = "";
36 if (! empty($_GET["rs"])) {
37 $this->mode = "get";
40 if (!empty($_POST["rs"])) {
41 $this->mode = "post";
44 switch( $this->mode ) {
46 case 'get':
47 $this->func_name = isset( $_GET["rs"] ) ? $_GET["rs"] : '';
48 if (! empty($_GET["rsargs"])) {
49 $this->args = $_GET["rsargs"];
50 } else {
51 $this->args = array();
53 break;
55 case 'post':
56 $this->func_name = isset( $_POST["rs"] ) ? $_POST["rs"] : '';
57 if (! empty($_POST["rsargs"])) {
58 $this->args = $_POST["rsargs"];
59 } else {
60 $this->args = array();
62 break;
64 default:
65 wfProfileOut( __METHOD__ );
66 return;
67 # Or we could throw an exception:
68 #throw new MWException( __METHOD__ . ' called without any data (mode empty).' );
72 wfProfileOut( __METHOD__ );
75 /** Pass the request to our internal function.
76 * BEWARE! Data are passed as they have been supplied by the user,
77 * they should be carefully handled in the function processing the
78 * request.
80 function performAction() {
81 global $wgAjaxExportList, $wgOut;
83 if ( empty( $this->mode ) ) {
84 return;
86 wfProfileIn( __METHOD__ );
88 if (! in_array( $this->func_name, $wgAjaxExportList ) ) {
89 wfDebug( __METHOD__ . ' Bad Request for unknown function ' . $this->func_name . "\n" );
91 wfHttpError( 400, 'Bad Request',
92 "unknown function " . (string) $this->func_name );
93 } else {
94 wfDebug( __METHOD__ . ' dispatching ' . $this->func_name . "\n" );
96 if ( strpos( $this->func_name, '::' ) !== false ) {
97 $func = explode( '::', $this->func_name, 2 );
98 } else {
99 $func = $this->func_name;
101 try {
102 $result = call_user_func_array($func, $this->args);
104 if ( $result === false || $result === NULL ) {
105 wfDebug( __METHOD__ . ' ERROR while dispatching '
106 . $this->func_name . "(" . var_export( $this->args, true ) . "): "
107 . "no data returned\n" );
109 wfHttpError( 500, 'Internal Error',
110 "{$this->func_name} returned no data" );
112 else {
113 if ( is_string( $result ) ) {
114 $result= new AjaxResponse( $result );
117 $result->sendHeaders();
118 $result->printText();
120 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 wfProfileOut( __METHOD__ );
138 $wgOut = null;