* Fixed E_NOTICE..
[mediawiki.git] / includes / AjaxDispatcher.php
blob7b85ed205409ef03f68b29203814798a0b10d3ca
1 <?php
2 /**
3 * Handle ajax requests and send them to the proper handler.
4 */
6 if( !(defined( 'MEDIAWIKI' ) && $wgUseAjax ) ) {
7 die( 1 );
10 require_once( 'AjaxFunctions.php' );
12 /**
13 * Object-Oriented Ajax functions.
14 * @addtogroup Ajax
16 class AjaxDispatcher {
17 /** The way the request was made, either a 'get' or a 'post' */
18 private $mode;
20 /** Name of the requested handler */
21 private $func_name;
23 /** Arguments passed */
24 private $args;
26 /** Load up our object with user supplied data */
27 function __construct() {
28 wfProfileIn( __METHOD__ );
30 $this->mode = "";
32 if (! empty($_GET["rs"])) {
33 $this->mode = "get";
36 if (!empty($_POST["rs"])) {
37 $this->mode = "post";
40 switch( $this->mode ) {
42 case 'get':
43 $this->func_name = isset( $_GET["rs"] ) ? $_GET["rs"] : '';
44 if (! empty($_GET["rsargs"])) {
45 $this->args = $_GET["rsargs"];
46 } else {
47 $this->args = array();
49 break;
51 case 'post':
52 $this->func_name = isset( $_POST["rs"] ) ? $_POST["rs"] : '';
53 if (! empty($_POST["rsargs"])) {
54 $this->args = $_POST["rsargs"];
55 } else {
56 $this->args = array();
58 break;
60 default:
61 return;
62 # Or we could throw an exception:
63 #throw new MWException( __METHOD__ . ' called without any data (mode empty).' );
67 wfProfileOut( __METHOD__ );
70 /** Pass the request to our internal function.
71 * BEWARE! Data are passed as they have been supplied by the user,
72 * they should be carefully handled in the function processing the
73 * request.
75 function performAction() {
76 global $wgAjaxExportList, $wgOut;
78 if ( empty( $this->mode ) ) {
79 return;
81 wfProfileIn( __METHOD__ );
83 if (! in_array( $this->func_name, $wgAjaxExportList ) ) {
84 wfHttpError( 400, 'Bad Request',
85 "unknown function " . (string) $this->func_name );
86 } else {
87 if ( strpos( $this->func_name, '::' ) !== false ) {
88 $func = explode( '::', $this->func_name, 2 );
89 } else {
90 $func = $this->func_name;
92 try {
93 $result = call_user_func_array($func, $this->args);
95 if ( $result === false || $result === NULL ) {
96 wfHttpError( 500, 'Internal Error',
97 "{$this->func_name} returned no data" );
99 else {
100 if ( is_string( $result ) ) {
101 $result= new AjaxResponse( $result );
104 $result->sendHeaders();
105 $result->printText();
108 } catch (Exception $e) {
109 if (!headers_sent()) {
110 wfHttpError( 500, 'Internal Error',
111 $e->getMessage() );
112 } else {
113 print $e->getMessage();
118 wfProfileOut( __METHOD__ );
119 $wgOut = null;