]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/AjaxDispatcher.php
MediaWiki 1.17.4
[autoinstallsdev/mediawiki.git] / includes / AjaxDispatcher.php
1 <?php
2 /**
3  * @defgroup Ajax Ajax
4  *
5  * @file
6  * @ingroup Ajax
7  * Handle ajax requests and send them to the proper handler.
8  */
9
10 if ( !( defined( 'MEDIAWIKI' ) && $wgUseAjax ) ) {
11         die( 1 );
12 }
13
14 require_once( 'AjaxFunctions.php' );
15
16 /**
17  * Object-Oriented Ajax functions.
18  * @ingroup Ajax
19  */
20 class AjaxDispatcher {
21         /** The way the request was made, either a 'get' or a 'post' */
22         private $mode;
23
24         /** Name of the requested handler */
25         private $func_name;
26
27         /** Arguments passed */
28         private $args;
29
30         /** Load up our object with user supplied data */
31         function __construct() {
32                 wfProfileIn( __METHOD__ );
33
34                 $this->mode = "";
35
36                 if ( ! empty( $_GET["rs"] ) ) {
37                         $this->mode = "get";
38                 }
39
40                 if ( !empty( $_POST["rs"] ) ) {
41                         $this->mode = "post";
42                 }
43
44                 switch( $this->mode ) {
45                         case 'get':
46                                 $this->func_name = isset( $_GET["rs"] ) ? $_GET["rs"] : '';
47                                 if ( ! empty( $_GET["rsargs"] ) ) {
48                                         $this->args = $_GET["rsargs"];
49                                 } else {
50                                         $this->args = array();
51                                 }
52                                 break;
53                         case 'post':
54                                 $this->func_name = isset( $_POST["rs"] ) ? $_POST["rs"] : '';
55                                 if ( ! empty( $_POST["rsargs"] ) ) {
56                                         $this->args = $_POST["rsargs"];
57                                 } else {
58                                         $this->args = array();
59                                 }
60                                 break;
61                         default:
62                                 wfProfileOut( __METHOD__ );
63                                 return;
64                                 # Or we could throw an exception:
65                                 # throw new MWException( __METHOD__ . ' called without any data (mode empty).' );
66                 }
67
68                 wfProfileOut( __METHOD__ );
69         }
70
71         /** Pass the request to our internal function.
72          * BEWARE! Data are passed as they have been supplied by the user,
73          * they should be carefully handled in the function processing the
74          * request.
75          */
76         function performAction() {
77                 global $wgAjaxExportList, $wgOut, $wgUser;
78
79                 if ( empty( $this->mode ) ) {
80                         return;
81                 }
82
83                 wfProfileIn( __METHOD__ );
84
85                 if ( ! in_array( $this->func_name, $wgAjaxExportList ) ) {
86                         wfDebug( __METHOD__ . ' Bad Request for unknown function ' . $this->func_name . "\n" );
87
88                         wfHttpError(
89                                 400,
90                                 'Bad Request',
91                                 "unknown function " . (string) $this->func_name
92                         );
93                 } elseif ( !in_array( 'read', User::getGroupPermissions( array( '*' ) ), true ) 
94                         && !$wgUser->isAllowed( 'read' ) )
95                 {
96                         wfHttpError(
97                                 403,
98                                 'Forbidden',
99                                 'You must log in to view pages.' );
100                 } else {
101                         wfDebug( __METHOD__ . ' dispatching ' . $this->func_name . "\n" );
102
103                         if ( strpos( $this->func_name, '::' ) !== false ) {
104                                 $func = explode( '::', $this->func_name, 2 );
105                         } else {
106                                 $func = $this->func_name;
107                         }
108
109                         try {
110                                 $result = call_user_func_array( $func, $this->args );
111
112                                 if ( $result === false || $result === null ) {
113                                         wfDebug( __METHOD__ . ' ERROR while dispatching '
114                                                         . $this->func_name . "(" . var_export( $this->args, true ) . "): "
115                                                         . "no data returned\n" );
116
117                                         wfHttpError( 500, 'Internal Error',
118                                                 "{$this->func_name} returned no data" );
119                                 } else {
120                                         if ( is_string( $result ) ) {
121                                                 $result = new AjaxResponse( $result );
122                                         }
123
124                                         $result->sendHeaders();
125                                         $result->printText();
126
127                                         wfDebug( __METHOD__ . ' dispatch complete for ' . $this->func_name . "\n" );
128                                 }
129                         } catch ( Exception $e ) {
130                                 wfDebug( __METHOD__ . ' ERROR while dispatching '
131                                                 . $this->func_name . "(" . var_export( $this->args, true ) . "): "
132                                                 . get_class( $e ) . ": " . $e->getMessage() . "\n" );
133
134                                 if ( !headers_sent() ) {
135                                         wfHttpError( 500, 'Internal Error',
136                                                 $e->getMessage() );
137                                 } else {
138                                         print $e->getMessage();
139                                 }
140                         }
141                 }
142
143                 $wgOut = null;
144                 wfProfileOut( __METHOD__ );
145         }
146 }