MediaWiki  REL1_19
AjaxDispatcher.php
Go to the documentation of this file.
00001 <?php
00014 class AjaxDispatcher {
00016         private $mode;
00017 
00019         private $func_name;
00020 
00022         private $args;
00023 
00025         function __construct() {
00026                 wfProfileIn( __METHOD__ );
00027 
00028                 $this->mode = "";
00029 
00030                 if ( ! empty( $_GET["rs"] ) ) {
00031                         $this->mode = "get";
00032                 }
00033 
00034                 if ( !empty( $_POST["rs"] ) ) {
00035                         $this->mode = "post";
00036                 }
00037 
00038                 switch( $this->mode ) {
00039                         case 'get':
00040                                 $this->func_name = isset( $_GET["rs"] ) ? $_GET["rs"] : '';
00041                                 if ( ! empty( $_GET["rsargs"] ) ) {
00042                                         $this->args = $_GET["rsargs"];
00043                                 } else {
00044                                         $this->args = array();
00045                                 }
00046                                 break;
00047                         case 'post':
00048                                 $this->func_name = isset( $_POST["rs"] ) ? $_POST["rs"] : '';
00049                                 if ( ! empty( $_POST["rsargs"] ) ) {
00050                                         $this->args = $_POST["rsargs"];
00051                                 } else {
00052                                         $this->args = array();
00053                                 }
00054                                 break;
00055                         default:
00056                                 wfProfileOut( __METHOD__ );
00057                                 return;
00058                                 # Or we could throw an exception:
00059                                 # throw new MWException( __METHOD__ . ' called without any data (mode empty).' );
00060                 }
00061 
00062                 wfProfileOut( __METHOD__ );
00063         }
00064 
00070         function performAction() {
00071                 global $wgAjaxExportList, $wgOut, $wgUser;
00072 
00073                 if ( empty( $this->mode ) ) {
00074                         return;
00075                 }
00076 
00077                 wfProfileIn( __METHOD__ );
00078 
00079                 if ( ! in_array( $this->func_name, $wgAjaxExportList ) ) {
00080                         wfDebug( __METHOD__ . ' Bad Request for unknown function ' . $this->func_name . "\n" );
00081 
00082                         wfHttpError(
00083                                 400,
00084                                 'Bad Request',
00085                                 "unknown function " . (string) $this->func_name
00086                         );
00087                 } elseif ( !in_array( 'read', User::getGroupPermissions( array( '*' ) ), true ) 
00088                         && !$wgUser->isAllowed( 'read' ) )
00089                 {
00090                         wfHttpError(
00091                                 403,
00092                                 'Forbidden',
00093                                 'You must log in to view pages.' );
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 
00103                         try {
00104                                 $result = call_user_func_array( $func, $this->args );
00105 
00106                                 if ( $result === false || $result === null ) {
00107                                         wfDebug( __METHOD__ . ' ERROR while dispatching '
00108                                                         . $this->func_name . "(" . var_export( $this->args, true ) . "): "
00109                                                         . "no data returned\n" );
00110 
00111                                         wfHttpError( 500, 'Internal Error',
00112                                                 "{$this->func_name} returned no data" );
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                         } 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                 $wgOut = null;
00138                 wfProfileOut( __METHOD__ );
00139         }
00140 }