* Non-working API to facilitate dev collaboration. Do not enable this yet in localset...
[lhc/web/wiklou.git] / includes / AjaxDispatcher.php
1 <?php
2
3 if( !defined( 'MEDIAWIKI' ) )
4 die( 1 );
5
6 if ( ! $wgUseAjax ) {
7 die( 1 );
8 }
9
10 require_once( 'AjaxFunctions.php' );
11 require_once( 'AjaxResponse.php' );
12
13 class AjaxDispatcher {
14 var $mode;
15 var $func_name;
16 var $args;
17
18 function AjaxDispatcher() {
19 wfProfileIn( 'AjaxDispatcher::AjaxDispatcher' );
20
21 $this->mode = "";
22
23 if (! empty($_GET["rs"])) {
24 $this->mode = "get";
25 }
26
27 if (!empty($_POST["rs"])) {
28 $this->mode = "post";
29 }
30
31 if ($this->mode == "get") {
32 $this->func_name = $_GET["rs"];
33 if (! empty($_GET["rsargs"])) {
34 $this->args = $_GET["rsargs"];
35 } else {
36 $this->args = array();
37 }
38 } else {
39 $this->func_name = $_POST["rs"];
40 if (! empty($_POST["rsargs"])) {
41 $this->args = $_POST["rsargs"];
42 } else {
43 $this->args = array();
44 }
45 }
46 wfProfileOut( 'AjaxDispatcher::AjaxDispatcher' );
47 }
48
49 function performAction() {
50 global $wgAjaxExportList, $wgOut;
51
52 if ( empty( $this->mode ) ) {
53 return;
54 }
55 wfProfileIn( 'AjaxDispatcher::performAction' );
56
57 if (! in_array( $this->func_name, $wgAjaxExportList ) ) {
58 header( 'Status: 400 Bad Request', true, 400 );
59 echo "unknown function {$this->func_name}";
60 } else {
61 try {
62 $result = call_user_func_array($this->func_name, $this->args);
63
64 if ( $result === false || $result === NULL ) {
65 header( 'Status: 500 Internal Error', true, 500 );
66 echo "{$this->func_name} returned no data";
67 }
68 else {
69 if ( is_string( $result ) ) {
70 $result= new AjaxResponse( $result );
71 }
72
73 $result->sendHeaders();
74 $result->printText();
75 }
76
77 } catch (Exception $e) {
78 if (!headers_sent()) {
79 header( 'Status: 500 Internal Error', true, 500 );
80 print $e->getMessage();
81 } else {
82 print $e->getMessage();
83 }
84 }
85 }
86
87 wfProfileOut( 'AjaxDispatcher::performAction' );
88 $wgOut = null;
89 }
90 }
91
92 ?>