Remove ?>'s from files. They're pointless, and just asking for people to mess with...
[lhc/web/wiklou.git] / includes / AjaxDispatcher.php
1 <?php
2 /**
3 * Handle ajax requests and send them to the proper handler.
4 */
5
6 if( !(defined( 'MEDIAWIKI' ) && $wgUseAjax ) ) {
7 die( 1 );
8 }
9
10 require_once( 'AjaxFunctions.php' );
11
12 /**
13 * Object-Oriented Ajax functions.
14 * @addtogroup Ajax
15 */
16 class AjaxDispatcher {
17 /** The way the request was made, either a 'get' or a 'post' */
18 private $mode;
19
20 /** Name of the requested handler */
21 private $func_name;
22
23 /** Arguments passed */
24 private $args;
25
26 /** Load up our object with user supplied data */
27 function __construct() {
28 wfProfileIn( __METHOD__ );
29
30 $this->mode = "";
31
32 if (! empty($_GET["rs"])) {
33 $this->mode = "get";
34 }
35
36 if (!empty($_POST["rs"])) {
37 $this->mode = "post";
38 }
39
40 switch( $this->mode ) {
41
42 case 'get':
43 $this->func_name = isset( $_GET["rs"] ) ? $_GET["rs"] : '';
44 if (! empty($_GET["rsargs"])) {
45 $this->args = $_GET["rsargs"];
46 } else {
47 $this->args = array();
48 }
49 break;
50
51 case 'post':
52 $this->func_name = isset( $_POST["rs"] ) ? $_POST["rs"] : '';
53 if (! empty($_POST["rsargs"])) {
54 $this->args = $_POST["rsargs"];
55 } else {
56 $this->args = array();
57 }
58 break;
59
60 default:
61 return;
62 # Or we could throw an exception:
63 #throw new MWException( __METHOD__ . ' called without any data (mode empty).' );
64
65 }
66
67 wfProfileOut( __METHOD__ );
68 }
69
70 /** Pass the request to our internal function.
71 * BEWARE! Data are passed as they have been supplied by the user,
72 * they should be carefully handled in the function processing the
73 * request.
74 */
75 function performAction() {
76 global $wgAjaxExportList, $wgOut;
77
78 if ( empty( $this->mode ) ) {
79 return;
80 }
81 wfProfileIn( __METHOD__ );
82
83 if (! in_array( $this->func_name, $wgAjaxExportList ) ) {
84 wfHttpError( 400, 'Bad Request',
85 "unknown function " . (string) $this->func_name );
86 } else {
87 try {
88 $result = call_user_func_array($this->func_name, $this->args);
89
90 if ( $result === false || $result === NULL ) {
91 wfHttpError( 500, 'Internal Error',
92 "{$this->func_name} returned no data" );
93 }
94 else {
95 if ( is_string( $result ) ) {
96 $result= new AjaxResponse( $result );
97 }
98
99 $result->sendHeaders();
100 $result->printText();
101 }
102
103 } catch (Exception $e) {
104 if (!headers_sent()) {
105 wfHttpError( 500, 'Internal Error',
106 $e->getMessage() );
107 } else {
108 print $e->getMessage();
109 }
110 }
111 }
112
113 wfProfileOut( __METHOD__ );
114 $wgOut = null;
115 }
116 }
117
118