Stop profiling before ending the function
[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 wfProfileOut( __METHOD__ );
62 return;
63 # Or we could throw an exception:
64 #throw new MWException( __METHOD__ . ' called without any data (mode empty).' );
65
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;
78
79 if ( empty( $this->mode ) ) {
80 return;
81 }
82 wfProfileIn( __METHOD__ );
83
84 if (! in_array( $this->func_name, $wgAjaxExportList ) ) {
85 wfHttpError( 400, 'Bad Request',
86 "unknown function " . (string) $this->func_name );
87 } else {
88 if ( strpos( $this->func_name, '::' ) !== false ) {
89 $func = explode( '::', $this->func_name, 2 );
90 } else {
91 $func = $this->func_name;
92 }
93 try {
94 $result = call_user_func_array($func, $this->args);
95
96 if ( $result === false || $result === NULL ) {
97 wfHttpError( 500, 'Internal Error',
98 "{$this->func_name} returned no data" );
99 }
100 else {
101 if ( is_string( $result ) ) {
102 $result= new AjaxResponse( $result );
103 }
104
105 $result->sendHeaders();
106 $result->printText();
107 }
108
109 } catch (Exception $e) {
110 if (!headers_sent()) {
111 wfHttpError( 500, 'Internal Error',
112 $e->getMessage() );
113 } else {
114 print $e->getMessage();
115 }
116 }
117 }
118
119 wfProfileOut( __METHOD__ );
120 $wgOut = null;
121 }
122 }