PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/media/install_4c8fc5f7070f7/Sajax.php

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