MediaWiki  REL1_24
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     private $config;
00054 
00058     function __construct( Config $config ) {
00059         wfProfileIn( __METHOD__ );
00060 
00061         $this->config = $config;
00062 
00063         $this->mode = "";
00064 
00065         if ( !empty( $_GET["rs"] ) ) {
00066             $this->mode = "get";
00067         }
00068 
00069         if ( !empty( $_POST["rs"] ) ) {
00070             $this->mode = "post";
00071         }
00072 
00073         switch ( $this->mode ) {
00074             case 'get':
00075                 $this->func_name = isset( $_GET["rs"] ) ? $_GET["rs"] : '';
00076                 if ( !empty( $_GET["rsargs"] ) ) {
00077                     $this->args = $_GET["rsargs"];
00078                 } else {
00079                     $this->args = array();
00080                 }
00081                 break;
00082             case 'post':
00083                 $this->func_name = isset( $_POST["rs"] ) ? $_POST["rs"] : '';
00084                 if ( !empty( $_POST["rsargs"] ) ) {
00085                     $this->args = $_POST["rsargs"];
00086                 } else {
00087                     $this->args = array();
00088                 }
00089                 break;
00090             default:
00091                 wfProfileOut( __METHOD__ );
00092                 return;
00093                 # Or we could throw an exception:
00094                 # throw new MWException( __METHOD__ . ' called without any data (mode empty).' );
00095         }
00096 
00097         wfProfileOut( __METHOD__ );
00098     }
00099 
00108     function performAction( User $user ) {
00109         if ( empty( $this->mode ) ) {
00110             return;
00111         }
00112 
00113         wfProfileIn( __METHOD__ );
00114 
00115         if ( !in_array( $this->func_name, $this->config->get( 'AjaxExportList' ) ) ) {
00116             wfDebug( __METHOD__ . ' Bad Request for unknown function ' . $this->func_name . "\n" );
00117 
00118             wfHttpError(
00119                 400,
00120                 'Bad Request',
00121                 "unknown function " . $this->func_name
00122             );
00123         } elseif ( !User::isEveryoneAllowed( 'read' ) && !$user->isAllowed( 'read' ) ) {
00124             wfHttpError(
00125                 403,
00126                 'Forbidden',
00127                 'You are not allowed to view pages.' );
00128         } else {
00129             wfDebug( __METHOD__ . ' dispatching ' . $this->func_name . "\n" );
00130 
00131             try {
00132                 $result = call_user_func_array( $this->func_name, $this->args );
00133 
00134                 if ( $result === false || $result === null ) {
00135                     wfDebug( __METHOD__ . ' ERROR while dispatching '
00136                             . $this->func_name . "(" . var_export( $this->args, true ) . "): "
00137                             . "no data returned\n" );
00138 
00139                     wfHttpError( 500, 'Internal Error',
00140                         "{$this->func_name} returned no data" );
00141                 } else {
00142                     if ( is_string( $result ) ) {
00143                         $result = new AjaxResponse( $result );
00144                     }
00145 
00146                     $result->sendHeaders();
00147                     $result->printText();
00148 
00149                     wfDebug( __METHOD__ . ' dispatch complete for ' . $this->func_name . "\n" );
00150                 }
00151             } catch ( Exception $e ) {
00152                 wfDebug( __METHOD__ . ' ERROR while dispatching '
00153                         . $this->func_name . "(" . var_export( $this->args, true ) . "): "
00154                         . get_class( $e ) . ": " . $e->getMessage() . "\n" );
00155 
00156                 if ( !headers_sent() ) {
00157                     wfHttpError( 500, 'Internal Error',
00158                         $e->getMessage() );
00159                 } else {
00160                     print $e->getMessage();
00161                 }
00162             }
00163         }
00164 
00165         wfProfileOut( __METHOD__ );
00166     }
00167 }