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
00087 wfProfileIn( __METHOD__ );
00088
00089 if ( ! in_array( $this->func_name, $wgAjaxExportList ) ) {
00090 wfDebug( __METHOD__ . ' Bad Request for unknown function ' . $this->func_name . "\n" );
00091
00092 wfHttpError( 400, 'Bad Request',
00093 "unknown function " . (string) $this->func_name );
00094 } else {
00095 wfDebug( __METHOD__ . ' dispatching ' . $this->func_name . "\n" );
00096
00097 if ( strpos( $this->func_name, '::' ) !== false ) {
00098 $func = explode( '::', $this->func_name, 2 );
00099 } else {
00100 $func = $this->func_name;
00101 }
00102 try {
00103 $result = call_user_func_array( $func, $this->args );
00104
00105 if ( $result === false || $result === null ) {
00106 wfDebug( __METHOD__ . ' ERROR while dispatching '
00107 . $this->func_name . "(" . var_export( $this->args, true ) . "): "
00108 . "no data returned\n" );
00109
00110 wfHttpError( 500, 'Internal Error',
00111 "{$this->func_name} returned no data" );
00112 }
00113 else {
00114 if ( is_string( $result ) ) {
00115 $result = new AjaxResponse( $result );
00116 }
00117
00118 $result->sendHeaders();
00119 $result->printText();
00120
00121 wfDebug( __METHOD__ . ' dispatch complete for ' . $this->func_name . "\n" );
00122 }
00123
00124 } catch ( Exception $e ) {
00125 wfDebug( __METHOD__ . ' ERROR while dispatching '
00126 . $this->func_name . "(" . var_export( $this->args, true ) . "): "
00127 . get_class( $e ) . ": " . $e->getMessage() . "\n" );
00128
00129 if ( !headers_sent() ) {
00130 wfHttpError( 500, 'Internal Error',
00131 $e->getMessage() );
00132 } else {
00133 print $e->getMessage();
00134 }
00135 }
00136 }
00137
00138 wfProfileOut( __METHOD__ );
00139 $wgOut = null;
00140 }
00141 }