Project

General

Profile

Download (5.22 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2

    
3
if(Connection_Aborted()) {
4
	exit;
5
}
6

    
7
class sajax {
8

    
9
	var $sajax_debug_mode = 0;
10
	var $sajax_export_list = array();
11
	var $sajax_request_type = "GET";
12
	var $sajax_remote_uri = "";
13
	var $sajax_js_has_been_shown = 0;
14

    
15

    
16
	/**
17
	 * PHP 5 Constructor
18
	 *
19
	 * @param string $remoteURI
20
	 */
21
	function __construct($remoteURI = NULL)
22
	{
23
		if (!isset($remoteURL))
24
			$this->sajax_remote_uri = $this->sajax_get_my_uri();
25
	}
26

    
27

    
28
	/**
29
	 * PHP 4 Constructor
30
	 *
31
	 * @param string $remoteURI
32
	 */
33
	function sajax($remoteURI = NULL)
34
	{
35
		$this->__construct($$remoteURI);
36
	}
37

    
38

    
39
	/**
40
	 *
41
	 *
42
	 */
43
	function sajax_get_my_uri()
44
	{
45
		global $REQUEST_URI;
46

    
47
		return $REQUEST_URI;
48
	}
49

    
50

    
51

    
52
	function sajax_handle_client_request() {
53

    
54
		$mode = "";
55

    
56
		if (! empty($_GET["rs"])) 
57
			$mode = "get";
58

    
59
		if (!empty($_POST["rs"]))
60
			$mode = "post";
61

    
62
		if (empty($mode)) 
63
			return;
64

    
65
		if ($mode == "get") {
66
			// Bust cache in the head
67
			header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");    // Date in the past
68
			header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
69
			// always modified
70
			header ("Cache-Control: no-cache, must-revalidate");  // HTTP/1.1
71
			header ("Pragma: no-cache");                          // HTTP/1.0
72
			$func_name = $_GET["rs"];
73
			if (! empty($_GET["rsargs"])) 
74
				$args = $_GET["rsargs"];
75
			else
76
				$args = array();
77
		}
78
		else {
79
			$func_name = $_POST["rs"];
80
			if (! empty($_POST["rsargs"])) 
81
				$args = $_POST["rsargs"];
82
			else
83
				$args = array();
84
		}
85

    
86
		if (! in_array($func_name, $this->sajax_export_list))
87
			echo "-:$func_name not callable";
88
		else {
89
			echo "+:";
90
			$result = call_user_func_array($func_name, $args);
91
			echo $result;
92
		}
93
		exit;
94
	} // End sajax_handle_client_request() function ...
95

    
96

    
97
	function sajax_get_common_js() {
98

    
99
		$t = strtoupper($this->sajax_request_type);
100
		if ($t != "GET" && $t != "POST") 
101
			return "// Invalid type: $t.. \n\n";
102

    
103
		ob_start();
104
		?>
105
		<!--
106
		// remote scripting library
107
		// (c) copyright 2005 modernmethod, inc
108
		var sajax_debug_mode = <?php echo $this->sajax_debug_mode ? "true" : "false"; ?>;
109
		var sajax_request_type = "<?php echo $t; ?>";
110
		
111
		function sajax_debug(text) {
112
			if (sajax_debug_mode)
113
				alert("RSD: " + text)
114
		}
115
 		function sajax_init_object() {
116
 			sajax_debug("sajax_init_object() called..")
117
 			
118
 			var A;
119
			try {
120
				A=new ActiveXObject("Msxml2.XMLHTTP");
121
			} catch (e) {
122
				try {
123
					A=new ActiveXObject("Microsoft.XMLHTTP");
124
				} catch (oc) {
125
					A=null;
126
				}
127
			}
128
			if(!A && typeof XMLHttpRequest != "undefined")
129
				A = new XMLHttpRequest();
130
			if (!A)
131
				sajax_debug("Could not create connection object.");
132
			return A;
133
		}
134
		function sajax_do_call(func_name, args) {
135
			var i, x, n;
136
			var uri;
137
			var post_data;
138
			
139
			uri = "<?php echo $this->sajax_remote_uri; ?>";
140
			if (sajax_request_type == "GET") {
141
				if (uri.indexOf("?") == -1) 
142
					uri = uri + "?rs=" + escape(func_name);
143
				else
144
					uri = uri + "&rs=" + escape(func_name);
145
				for (i = 0; i < args.length-1; i++) 
146
					uri = uri + "&rsargs[]=" + escape(args[i]);
147
				uri = uri + "&rsrnd=" + new Date().getTime();
148
				post_data = null;
149
			} else {
150
				post_data = "rs=" + escape(func_name);
151
				for (i = 0; i < args.length-1; i++) 
152
					post_data = post_data + "&rsargs[]=" + escape(args[i]);
153
			}
154
			
155
			x = sajax_init_object();
156
			x.open(sajax_request_type, uri, true);
157
			if (sajax_request_type == "POST") {
158
				x.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
159
				x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
160
			}
161
			x.onreadystatechange = function() {
162
				if (x.readyState != 4) 
163
					return;
164
				sajax_debug("received " + x.responseText);
165
				
166
				var status;
167
				var data;
168
				status = x.responseText.charAt(0);
169
				data = x.responseText.substring(2);
170
				if (status == "-") 
171
					alert("Error: " + data);
172
				else  
173
					args[args.length-1](data);
174
			}
175
			x.send(post_data);
176
			sajax_debug(func_name + " uri = " + uri + "/post = " + post_data);
177
			sajax_debug(func_name + " waiting..");
178
			delete x;
179
		}
180
		//-->
181
		<?php
182
		$html = ob_get_contents();
183
		ob_end_clean();
184
		return $html;
185
	}
186

    
187

    
188
	function sajax_show_common_js()
189
	{
190
		echo $this->sajax_get_common_js();
191
	}
192

    
193

    
194
	function sajax_esc($val)
195
	{
196
		return str_replace('"', '\\\\"', $val);
197
	}
198

    
199

    
200
	/**
201
	 * sajax_get_one_stub
202
	 *
203
	 * @param string $func_name
204
	 */
205
	function sajax_get_one_stub($func_name)
206
	{
207
		ob_start();	
208
		?>
209
		
210
		// wrapper for <?php echo $func_name; ?>
211
		
212
		function x_<?php echo $func_name; ?>() {
213
			sajax_do_call("<?php echo $func_name; ?>",
214
				x_<?php echo $func_name; ?>.arguments);
215
		}
216
		
217
		<?php
218
		$html = ob_get_contents();
219
		ob_end_clean();
220
		return $html;
221
	}
222

    
223

    
224
	function sajax_show_one_stub($func_name) {
225
		echo sajax_get_one_stub($func_name);
226
	}
227

    
228

    
229
	/**
230
	 * export user functions to sajax ...
231
	 *
232
	 */
233
	function sajax_export() {
234
		$n = func_num_args();
235
		for ($i = 0; $i < $n; $i++) {
236
			$this->sajax_export_list[] = func_get_arg($i);
237
		}
238
	}
239

    
240

    
241
	function sajax_get_javascript()
242
	{
243
		$html = "";
244
		if (! $this->sajax_js_has_been_shown) {
245
			$html .= $this->sajax_get_common_js();
246
			$this->sajax_js_has_been_shown = 1;
247
		}
248
		foreach ($this->sajax_export_list as $func) {
249
			$html .= $this->sajax_get_one_stub($func);
250
		}
251
		return $html;
252
	}
253

    
254

    
255
	function sajax_show_javascript()
256
	{
257
		echo $this->sajax_get_javascript();
258
	}
259

    
260

    
261
} // End sajax class
262

    
263

    
264
?>
(3-3/3)