summaryrefslogtreecommitdiff
path: root/includes/AjaxDispatcher.php
blob: ca129029194eeb93df90b5869bb4e150e5453559 (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
92
93
94
95
96
<?php

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

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

require_once( 'AjaxFunctions.php' );

/**
 * Object-Oriented Ajax functions.
 * @addtogroup Ajax
 */
class AjaxDispatcher {
	var $mode;
	var $func_name;
	var $args;

	function __construct() {
		wfProfileIn( __METHOD__ );

		$this->mode = "";

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

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

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

	function performAction() {
		global $wgAjaxExportList, $wgOut;

		if ( empty( $this->mode ) ) {
			return;
		}
		wfProfileIn( __METHOD__ );

		if (! in_array( $this->func_name, $wgAjaxExportList ) ) {
			wfHttpError( 400, 'Bad Request',
				"unknown function " . (string) $this->func_name );
		} else {
			try {
				$result = call_user_func_array($this->func_name, $this->args);

				if ( $result === false || $result === NULL ) {
					wfHttpError( 500, 'Internal Error',
						"{$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()) {
					wfHttpError( 500, 'Internal Error',
						$e->getMessage() );
				} else {
					print $e->getMessage();
				}
			}
		}

		wfProfileOut( __METHOD__ );
		$wgOut = null;
	}
}

?>