PageRenderTime 2ms CodeModel.GetById 36ms app.highlight 3ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/mp3act_11/includes/Sajax.php

https://github.com/gburca/Grammafone
PHP | 308 lines | 232 code | 47 blank | 29 comment | 56 complexity | 82ca26cb9ffc918202416132e4b073ed MD5 | raw file
  1<?php	
  2if (!isset($SAJAX_INCLUDED)) {
  3
  4	/*  
  5	 * GLOBALS AND DEFAULTS
  6	 *
  7	 */ 
  8	$sajax_debug_mode = 0;
  9	$sajax_export_list = array();
 10	$sajax_request_type = "GET";
 11	$sajax_remote_uri = "";
 12	
 13	/*
 14	 * CODE
 15	 *
 16	 */ 
 17	 
 18	//
 19	// Initialize the Sajax library.
 20	//
 21	function sajax_init() {
 22	}
 23	
 24	//
 25	// Helper function to return the script's own URI. 
 26	// 
 27	function sajax_get_my_uri() {
 28		global $REQUEST_URI;
 29		
 30		return $REQUEST_URI;
 31	}
 32	$sajax_remote_uri = sajax_get_my_uri();
 33	
 34	//
 35	// Helper function to return an eval()-usable representation
 36	// of an object in JavaScript.
 37	// 
 38	function sajax_get_js_repr($value) {
 39		$type = gettype($value);
 40		
 41		if ($type == "boolean" ||
 42			$type == "integer") {
 43			return "parseInt($value)";
 44		} 
 45		elseif ($type == "double") {
 46			return "parseFloat($value)";
 47		} 
 48		elseif ($type == "array" || $type == "object" ) {
 49			//
 50			// XXX Arrays with non-numeric indices are not
 51			// permitted according to ECMAScript, yet everyone
 52			// uses them.. We'll use an object.
 53			// 
 54			$s = "{ ";
 55			if ($type == "object") {
 56				$value = get_object_vars($value);
 57			} 
 58			foreach ($value as $k=>$v) {
 59				$esc_key = sajax_esc($k);
 60				if (is_numeric($k)) 
 61					$s .= "$k: " . sajax_get_js_repr($v) . ", ";
 62				else
 63					$s .= "\"$esc_key\": " . sajax_get_js_repr($v) . ", ";
 64			}
 65			return substr($s, 0, -2) . " }";
 66		} 
 67		else {
 68			$esc_val = sajax_esc($value);
 69			$s = "\"$esc_val\"";
 70			return $s;
 71		}
 72	}
 73
 74	function sajax_handle_client_request() {
 75		global $sajax_export_list;
 76		
 77		$mode = "";
 78		
 79		if (! empty($_GET["rs"])) 
 80			$mode = "get";
 81		
 82		if (!empty($_POST["rs"]))
 83			$mode = "post";
 84			
 85		if (empty($mode)) 
 86			return;
 87
 88		$target = "";
 89		
 90		if ($mode == "get") {
 91			// Bust cache in the head
 92			header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");    // Date in the past
 93			header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
 94			// always modified
 95			header ("Cache-Control: no-cache, must-revalidate");  // HTTP/1.1
 96			header ("Pragma: no-cache");                          // HTTP/1.0
 97			$func_name = $_GET["rs"];
 98			if (! empty($_GET["rsargs"])) 
 99				$args = $_GET["rsargs"];
100			else
101				$args = array();
102		}
103		else {
104			$func_name = $_POST["rs"];
105			if (! empty($_POST["rsargs"])) 
106				$args = $_POST["rsargs"];
107			else
108				$args = array();
109		}
110		
111		if (! in_array($func_name, $sajax_export_list))
112			echo "-:$func_name not callable";
113		else {
114			echo "+:";
115			$result = call_user_func_array($func_name, $args);
116			echo "var res = " . sajax_get_js_repr($result) . "; res;";
117		}
118		exit;
119	}
120	
121	function sajax_get_common_js() {
122		global $sajax_debug_mode;
123		global $sajax_request_type;
124		global $sajax_remote_uri;
125		
126		$t = strtoupper($sajax_request_type);
127		if ($t != "" && $t != "GET" && $t != "POST") 
128			return "// Invalid type: $t.. \n\n";
129		
130		ob_start();
131		?>
132		
133		// remote scripting library
134		// (c) copyright 2005 modernmethod, inc
135		var sajax_debug_mode = <?php echo $sajax_debug_mode ? "true" : "false"; ?>;
136		var sajax_request_type = "<?php echo $t; ?>";
137		var sajax_target_id = "";
138		
139		function sajax_debug(text) {
140			if (sajax_debug_mode)
141				alert("RSD: " + text)
142		}
143 		function sajax_init_object() {
144 			sajax_debug("sajax_init_object() called..")
145 			
146 			var A;
147			try {
148				A=new ActiveXObject("Msxml2.XMLHTTP");
149			} catch (e) {
150				try {
151					A=new ActiveXObject("Microsoft.XMLHTTP");
152				} catch (oc) {
153					A=null;
154				}
155			}
156			if(!A && typeof XMLHttpRequest != "undefined")
157				A = new XMLHttpRequest();
158			if (!A)
159				sajax_debug("Could not create connection object.");
160			return A;
161		}
162		function sajax_do_call(func_name, args) {
163			var i, x, n;
164			var uri;
165			var post_data;
166			var target_id;
167			
168			sajax_debug("in sajax_do_call().." + sajax_request_type + "/" + sajax_target_id);
169			target_id = sajax_target_id;
170			if (sajax_request_type == "") 
171				sajax_request_type = "GET";
172			
173			uri = "<?php echo $sajax_remote_uri; ?>";
174			if (sajax_request_type == "GET") {
175			
176				if (uri.indexOf("?") == -1) 
177					uri += "?rs=" + escape(func_name);
178				else
179					uri += "&rs=" + escape(func_name);
180				uri += "&rst=" + escape(sajax_target_id);
181				uri += "&rsrnd=" + new Date().getTime();
182				
183				for (i = 0; i < args.length-1; i++) 
184					uri += "&rsargs[]=" + escape(args[i]);
185
186				post_data = null;
187			} 
188			else if (sajax_request_type == "POST") {
189				post_data = "rs=" + escape(func_name);
190				post_data += "&rst=" + escape(sajax_target_id);
191				post_data += "&rsrnd=" + new Date().getTime();
192				
193				for (i = 0; i < args.length-1; i++) 
194					post_data = post_data + "&rsargs[]=" + escape(args[i]);
195			}
196			else {
197				alert("Illegal request type: " + sajax_request_type);
198			}
199			
200			x = sajax_init_object();
201			x.open(sajax_request_type, uri, true);
202			
203			if (sajax_request_type == "POST") {
204				x.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
205				x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
206			}
207			
208			x.onreadystatechange = function() {
209				if (x.readyState != 4) 
210					return;
211
212				sajax_debug("received " + x.responseText);
213				
214				var status;
215				var data;
216				status = x.responseText.charAt(0);
217				data = x.responseText.substring(2);
218				if (status == "-") 
219					alert("Error: " + data);
220				else {
221					if (target_id != "") 
222						document.getElementById(target_id).innerHTML = eval(data);
223					else
224						args[args.length-1](eval(data));
225				}
226			}
227			sajax_debug(func_name + " uri = " + uri + "/post = " + post_data);
228			x.send(post_data);
229			sajax_debug(func_name + " waiting..");
230			delete x;
231			return true;
232		}
233		
234		<?php
235		$html = ob_get_contents();
236		ob_end_clean();
237		return $html;
238	}
239	
240	function sajax_show_common_js() {
241		echo sajax_get_common_js();
242	}
243	
244	// javascript escape a value
245	function sajax_esc($val)
246	{
247		$val = str_replace("\\", "\\\\", $val);
248		$val = str_replace("\r", "\\r", $val);
249		$val = str_replace("\n", "\\n", $val);
250		return str_replace('"', '\\"', $val);
251	}
252
253	function sajax_get_one_stub($func_name) {
254		ob_start();	
255		?>
256		
257		// wrapper for <?php echo $func_name; ?>
258		
259		function x_<?php echo $func_name; ?>() {
260			sajax_do_call("<?php echo $func_name; ?>",
261				x_<?php echo $func_name; ?>.arguments);
262		}
263		
264		<?php
265		$html = ob_get_contents();
266		ob_end_clean();
267		return $html;
268	}
269	
270	function sajax_show_one_stub($func_name) {
271		echo sajax_get_one_stub($func_name);
272	}
273	
274	function sajax_export() {
275		global $sajax_export_list;
276		
277		$n = func_num_args();
278		for ($i = 0; $i < $n; $i++) {
279			$sajax_export_list[] = func_get_arg($i);
280		}
281	}
282	
283	$sajax_js_has_been_shown = 0;
284	function sajax_get_javascript()
285	{
286		global $sajax_js_has_been_shown;
287		global $sajax_export_list;
288		
289		$html = "";
290		if (! $sajax_js_has_been_shown) {
291			$html .= sajax_get_common_js();
292			$sajax_js_has_been_shown = 1;
293		}
294		foreach ($sajax_export_list as $func) {
295			$html .= sajax_get_one_stub($func);
296		}
297		return $html;
298	}
299	
300	function sajax_show_javascript()
301	{
302		echo sajax_get_javascript();
303	}
304
305	
306	$SAJAX_INCLUDED = 1;
307}
308?>