MediaWiki  REL1_20
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 " . (string) $this->func_name
00115                         );
00116                 } elseif ( !in_array( 'read', User::getGroupPermissions( array( '*' ) ), true )
00117                         && !$wgUser->isAllowed( 'read' ) )
00118                 {
00119                         wfHttpError(
00120                                 403,
00121                                 'Forbidden',
00122                                 'You must log in to view pages.' );
00123                 } else {
00124                         wfDebug( __METHOD__ . ' dispatching ' . $this->func_name . "\n" );
00125 
00126                         try {
00127                                 $result = call_user_func_array( $this->func_name, $this->args );
00128 
00129                                 if ( $result === false || $result === null ) {
00130                                         wfDebug( __METHOD__ . ' ERROR while dispatching '
00131                                                         . $this->func_name . "(" . var_export( $this->args, true ) . "): "
00132                                                         . "no data returned\n" );
00133 
00134                                         wfHttpError( 500, 'Internal Error',
00135                                                 "{$this->func_name} returned no data" );
00136                                 } else {
00137                                         if ( is_string( $result ) ) {
00138                                                 $result = new AjaxResponse( $result );
00139                                         }
00140 
00141                                         $result->sendHeaders();
00142                                         $result->printText();
00143 
00144                                         wfDebug( __METHOD__ . ' dispatch complete for ' . $this->func_name . "\n" );
00145                                 }
00146                         } catch ( Exception $e ) {
00147                                 wfDebug( __METHOD__ . ' ERROR while dispatching '
00148                                                 . $this->func_name . "(" . var_export( $this->args, true ) . "): "
00149                                                 . get_class( $e ) . ": " . $e->getMessage() . "\n" );
00150 
00151                                 if ( !headers_sent() ) {
00152                                         wfHttpError( 500, 'Internal Error',
00153                                                 $e->getMessage() );
00154                                 } else {
00155                                         print $e->getMessage();
00156                                 }
00157                         }
00158                 }
00159 
00160                 wfProfileOut( __METHOD__ );
00161         }
00162 }