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