MediaWiki  REL1_22
AjaxDispatcher.php
Go to the documentation of this file.
00001 <?php
00032 class AjaxDispatcher {
00037     private $mode;
00038 
00043     private $func_name;
00044 
00048     private $args;
00049 
00053     function __construct() {
00054         wfProfileIn( __METHOD__ );
00055 
00056         $this->mode = "";
00057 
00058         if ( ! empty( $_GET["rs"] ) ) {
00059             $this->mode = "get";
00060         }
00061 
00062         if ( !empty( $_POST["rs"] ) ) {
00063             $this->mode = "post";
00064         }
00065 
00066         switch ( $this->mode ) {
00067             case 'get':
00068                 $this->func_name = isset( $_GET["rs"] ) ? $_GET["rs"] : '';
00069                 if ( ! empty( $_GET["rsargs"] ) ) {
00070                     $this->args = $_GET["rsargs"];
00071                 } else {
00072                     $this->args = array();
00073                 }
00074                 break;
00075             case 'post':
00076                 $this->func_name = isset( $_POST["rs"] ) ? $_POST["rs"] : '';
00077                 if ( ! empty( $_POST["rsargs"] ) ) {
00078                     $this->args = $_POST["rsargs"];
00079                 } else {
00080                     $this->args = array();
00081                 }
00082                 break;
00083             default:
00084                 wfProfileOut( __METHOD__ );
00085                 return;
00086                 # Or we could throw an exception:
00087                 # throw new MWException( __METHOD__ . ' called without any data (mode empty).' );
00088         }
00089 
00090         wfProfileOut( __METHOD__ );
00091     }
00092 
00099     function performAction() {
00100         global $wgAjaxExportList, $wgUser;
00101 
00102         if ( empty( $this->mode ) ) {
00103             return;
00104         }
00105 
00106         wfProfileIn( __METHOD__ );
00107 
00108         if ( ! in_array( $this->func_name, $wgAjaxExportList ) ) {
00109             wfDebug( __METHOD__ . ' Bad Request for unknown function ' . $this->func_name . "\n" );
00110 
00111             wfHttpError(
00112                 400,
00113                 'Bad Request',
00114                 "unknown function " . $this->func_name
00115             );
00116         } elseif ( !User::isEveryoneAllowed( 'read' ) && !$wgUser->isAllowed( 'read' ) ) {
00117             wfHttpError(
00118                 403,
00119                 'Forbidden',
00120                 'You are not allowed to view pages.' );
00121         } else {
00122             wfDebug( __METHOD__ . ' dispatching ' . $this->func_name . "\n" );
00123 
00124             try {
00125                 $result = call_user_func_array( $this->func_name, $this->args );
00126 
00127                 if ( $result === false || $result === null ) {
00128                     wfDebug( __METHOD__ . ' ERROR while dispatching '
00129                             . $this->func_name . "(" . var_export( $this->args, true ) . "): "
00130                             . "no data returned\n" );
00131 
00132                     wfHttpError( 500, 'Internal Error',
00133                         "{$this->func_name} returned no data" );
00134                 } else {
00135                     if ( is_string( $result ) ) {
00136                         $result = new AjaxResponse( $result );
00137                     }
00138 
00139                     $result->sendHeaders();
00140                     $result->printText();
00141 
00142                     wfDebug( __METHOD__ . ' dispatch complete for ' . $this->func_name . "\n" );
00143                 }
00144             } catch ( Exception $e ) {
00145                 wfDebug( __METHOD__ . ' ERROR while dispatching '
00146                         . $this->func_name . "(" . var_export( $this->args, true ) . "): "
00147                         . get_class( $e ) . ": " . $e->getMessage() . "\n" );
00148 
00149                 if ( !headers_sent() ) {
00150                     wfHttpError( 500, 'Internal Error',
00151                         $e->getMessage() );
00152                 } else {
00153                     print $e->getMessage();
00154                 }
00155             }
00156         }
00157 
00158         wfProfileOut( __METHOD__ );
00159     }
00160 }