PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/mp3act_xspf-naz-20070516/includes/Sajax.php

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