summaryrefslogtreecommitdiff
path: root/includes/AjaxDispatcher.php
blob: 618c2736c8f14aaf167635e0df8ab34f2de819c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php

if( !defined( 'MEDIAWIKI' ) )
        die( 1 );

if ( ! $wgUseAjax ) {
	die( 1 );
}

require_once( 'AjaxFunctions.php' );

class AjaxDispatcher {
	var $mode;
	var $func_name;
	var $args;

	function AjaxDispatcher() {
		wfProfileIn( 'AjaxDispatcher::AjaxDispatcher' );

		$this->mode = "";

		if (! empty($_GET["rs"])) {
			$this->mode = "get";
		}

		if (!empty($_POST["rs"])) {
			$this->mode = "post";
		}

		if ($this->mode == "get") {
			$this->func_name = $_GET["rs"];
			if (! empty($_GET["rsargs"])) {
				$this->args = $_GET["rsargs"];
			} else {
				$this->args = array();
			}
		} else {
			$this->func_name = $_POST["rs"];
			if (! empty($_POST["rsargs"])) {
				$this->args = $_POST["rsargs"];
			} else {
				$this->args = array();
			}
		}
		wfProfileOut( 'AjaxDispatcher::AjaxDispatcher' );
	}

	function performAction() {
		global $wgAjaxExportList, $wgOut;
		
		if ( empty( $this->mode ) ) {
			return;
		}
		wfProfileIn( 'AjaxDispatcher::performAction' );

		if (! in_array( $this->func_name, $wgAjaxExportList ) ) {
			header( 'Status: 400 Bad Request', true, 400 );
			echo "unknown function {$this->func_name}";
		} else {
			try {
				$result = call_user_func_array($this->func_name, $this->args);
				
				if ( $result === false || $result === NULL ) {
					header( 'Status: 500 Internal Error', true, 500 );
					echo "{$this->func_name} returned no data";
				}
				else {
					if ( is_string( $result ) ) {
						$result= new AjaxResponse( $result );
					}
					
					$result->sendHeaders();
					$result->printText();
				}
				
			} catch (Exception $e) {
				if (!headers_sent()) {
					header( 'Status: 500 Internal Error', true, 500 );
					print $e->getMessage();
				} else {
					print $e->getMessage();
				}
			}
		}
		
		wfProfileOut( 'AjaxDispatcher::performAction' );
		$wgOut = null;
	}
}

?>