00001 <?php
00010 if( !(defined( 'MEDIAWIKI' ) && $wgUseAjax ) ) {
00011 die( 1 );
00012 }
00013
00014 require_once( 'AjaxFunctions.php' );
00015
00020 class AjaxDispatcher {
00022 private $mode;
00023
00025 private $func_name;
00026
00028 private $args;
00029
00031 function __construct() {
00032 wfProfileIn( __METHOD__ );
00033
00034 $this->mode = "";
00035
00036 if (! empty($_GET["rs"])) {
00037 $this->mode = "get";
00038 }
00039
00040 if (!empty($_POST["rs"])) {
00041 $this->mode = "post";
00042 }
00043
00044 switch( $this->mode ) {
00045
00046 case 'get':
00047 $this->func_name = isset( $_GET["rs"] ) ? $_GET["rs"] : '';
00048 if (! empty($_GET["rsargs"])) {
00049 $this->args = $_GET["rsargs"];
00050 } else {
00051 $this->args = array();
00052 }
00053 break;
00054
00055 case 'post':
00056 $this->func_name = isset( $_POST["rs"] ) ? $_POST["rs"] : '';
00057 if (! empty($_POST["rsargs"])) {
00058 $this->args = $_POST["rsargs"];
00059 } else {
00060 $this->args = array();
00061 }
00062 break;
00063
00064 default:
00065 wfProfileOut( __METHOD__ );
00066 return;
00067 # Or we could throw an exception:
00068 #throw new MWException( __METHOD__ . ' called without any data (mode empty).' );
00069
00070 }
00071
00072 wfProfileOut( __METHOD__ );
00073 }
00074
00080 function performAction() {
00081 global $wgAjaxExportList, $wgOut;
00082
00083 if ( empty( $this->mode ) ) {
00084 return;
00085 }
00086 wfProfileIn( __METHOD__ );
00087
00088 if (! in_array( $this->func_name, $wgAjaxExportList ) ) {
00089 wfDebug( __METHOD__ . ' Bad Request for unknown function ' . $this->func_name . "\n" );
00090
00091 wfHttpError( 400, 'Bad Request',
00092 "unknown function " . (string) $this->func_name );
00093 } else {
00094 wfDebug( __METHOD__ . ' dispatching ' . $this->func_name . "\n" );
00095
00096 if ( strpos( $this->func_name, '::' ) !== false ) {
00097 $func = explode( '::', $this->func_name, 2 );
00098 } else {
00099 $func = $this->func_name;
00100 }
00101 try {
00102 $result = call_user_func_array($func, $this->args);
00103
00104 if ( $result === false || $result === NULL ) {
00105 wfDebug( __METHOD__ . ' ERROR while dispatching '
00106 . $this->func_name . "(" . var_export( $this->args, true ) . "): "
00107 . "no data returned\n" );
00108
00109 wfHttpError( 500, 'Internal Error',
00110 "{$this->func_name} returned no data" );
00111 }
00112 else {
00113 if ( is_string( $result ) ) {
00114 $result= new AjaxResponse( $result );
00115 }
00116
00117 $result->sendHeaders();
00118 $result->printText();
00119
00120 wfDebug( __METHOD__ . ' dispatch complete for ' . $this->func_name . "\n" );
00121 }
00122
00123 } catch (Exception $e) {
00124 wfDebug( __METHOD__ . ' ERROR while dispatching '
00125 . $this->func_name . "(" . var_export( $this->args, true ) . "): "
00126 . get_class($e) . ": " . $e->getMessage() . "\n" );
00127
00128 if (!headers_sent()) {
00129 wfHttpError( 500, 'Internal Error',
00130 $e->getMessage() );
00131 } else {
00132 print $e->getMessage();
00133 }
00134 }
00135 }
00136
00137 wfProfileOut( __METHOD__ );
00138 $wgOut = null;
00139 }
00140 }