PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/Sajax.php

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