Project

General

Profile

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

    
3

    
4
class sajax {
5

    
6
	var $sajax_debug_mode = 0;
7
	var $sajax_export_list = array();
8
	var $sajax_request_type = "GET";
9
	var $sajax_remote_uri = "";
10
	var $sajax_js_has_been_shown = 0;
11

    
12

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

    
24

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

    
35

    
36
	/**
37
	 *
38
	 *
39
	 */
40
	function sajax_get_my_uri()
41
	{
42
		global $REQUEST_URI;
43

    
44
		return $REQUEST_URI;
45
	}
46

    
47

    
48

    
49
	function sajax_handle_client_request() {
50

    
51
		$mode = "";
52

    
53
		if (! empty($_GET["rs"])) 
54
			$mode = "get";
55

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

    
59
		if (empty($mode)) 
60
			return;
61

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

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

    
93

    
94
	function sajax_get_common_js() {
95

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

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

    
184

    
185
	function sajax_show_common_js()
186
	{
187
		echo $this->sajax_get_common_js();
188
	}
189

    
190

    
191
	function sajax_esc($val)
192
	{
193
		return str_replace('"', '\\\\"', $val);
194
	}
195

    
196

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

    
220

    
221
	function sajax_show_one_stub($func_name) {
222
		echo sajax_get_one_stub($func_name);
223
	}
224

    
225

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

    
237

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

    
251

    
252
	function sajax_show_javascript()
253
	{
254
		echo $this->sajax_get_javascript();
255
	}
256

    
257

    
258
} // End sajax class
259

    
260

    
261
?>
(3-3/3)