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

/administrator/components/com_joomlapack/temp/installation/includes/sajax.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 419 lines | 323 code | 49 blank | 47 comment | 78 complexity | 97de721a82df276da0eea914747f955b MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @package JoomlaPackInstaller
  4. * @copyright Copyright (C) 2008 JoomlaPack Developers. All rights reserved.
  5. * @version 2.0
  6. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
  7. *
  8. * Simple AJAX library [ this library is called (S)AJAX and is copyrighted by ModernMethod ]
  9. *
  10. * JoomlaPack is free software. This version may have been modified pursuant
  11. * to the GNU General Public License, and as distributed it includes or
  12. * is derivative of works licensed under the GNU General Public License or
  13. * other free or open source software licenses.
  14. */
  15. /** ensure this file is being included by a parent file */
  16. defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
  17. if (!isset($SAJAX_INCLUDED)) {
  18. /*
  19. * GLOBALS AND DEFAULTS
  20. *
  21. */
  22. $GLOBALS['sajax_version'] = '0.12';
  23. $GLOBALS['sajax_debug_mode'] = 0;
  24. $GLOBALS['sajax_export_list'] = array();
  25. $GLOBALS['sajax_request_type'] = 'POST';
  26. $GLOBALS['sajax_remote_uri'] = '';
  27. $GLOBALS['sajax_failure_redirect'] = '';
  28. /*
  29. * CODE
  30. *
  31. */
  32. //
  33. // Initialize the Sajax library.
  34. //
  35. function sajax_init() {
  36. }
  37. // Since str_split used in sajax_get_my_uri is only available on PHP 5, we have
  38. // to provide an alternative for those using PHP 4.x
  39. if(!function_exists('str_split')){
  40. function str_split($string,$split_length=1){
  41. $count = strlen($string);
  42. if($split_length < 1){
  43. return false;
  44. } elseif($split_length > $count){
  45. return array($string);
  46. } else {
  47. $num = (int)ceil($count/$split_length);
  48. $ret = array();
  49. for($i=0;$i<$num;$i++){
  50. $ret[] = substr($string,$i*$split_length,$split_length);
  51. }
  52. return $ret;
  53. }
  54. }
  55. }
  56. //
  57. // Helper function to return the script's own URI.
  58. //
  59. function sajax_get_my_uri() {
  60. $myURI = $_SERVER["REQUEST_URI"];
  61. $upto = strrpos($myURI, "/");
  62. if (is_numeric($upto)) {
  63. $myArray = str_split($myURI, $upto);
  64. $myURI = $myArray[0];
  65. $myURI .= "/";
  66. }
  67. $myURI .= "index.php?task=ajax";
  68. return $myURI;
  69. //return $_SERVER["REQUEST_URI"];
  70. }
  71. $sajax_remote_uri = sajax_get_my_uri();
  72. //
  73. // Helper function to return an eval()-usable representation
  74. // of an object in JavaScript.
  75. //
  76. function sajax_get_js_repr($value) {
  77. $type = gettype($value);
  78. if ($type == "boolean") {
  79. return ($value) ? "Boolean(true)" : "Boolean(false)";
  80. }
  81. elseif ($type == "integer") {
  82. return "parseInt($value)";
  83. }
  84. elseif ($type == "double") {
  85. return "parseFloat($value)";
  86. }
  87. elseif ($type == "array" || $type == "object" ) {
  88. //
  89. // XXX Arrays with non-numeric indices are not
  90. // permitted according to ECMAScript, yet everyone
  91. // uses them.. We'll use an object.
  92. //
  93. $s = "{ ";
  94. if ($type == "object") {
  95. $value = get_object_vars($value);
  96. }
  97. foreach ($value as $k=>$v) {
  98. $esc_key = sajax_esc($k);
  99. if (is_numeric($k))
  100. $s .= "$k: " . sajax_get_js_repr($v) . ", ";
  101. else
  102. $s .= "\"$esc_key\": " . sajax_get_js_repr($v) . ", ";
  103. }
  104. if (count($value))
  105. $s = substr($s, 0, -2);
  106. return $s . " }";
  107. }
  108. else {
  109. $esc_val = sajax_esc($value);
  110. $s = "'$esc_val'";
  111. return $s;
  112. }
  113. }
  114. function sajax_handle_client_request() {
  115. global $sajax_export_list;
  116. $mode = "";
  117. if (! empty($_GET["rs"]))
  118. $mode = "get";
  119. if (!empty($_POST["rs"]))
  120. $mode = "post";
  121. if (empty($mode))
  122. return;
  123. $target = "";
  124. ob_clean();
  125. if ($mode == "get") {
  126. // Bust cache in the head
  127. header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
  128. header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  129. // always modified
  130. header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  131. header ("Pragma: no-cache"); // HTTP/1.0
  132. ob_flush();
  133. $func_name = $_GET["rs"];
  134. if (! empty($_GET["rsargs"]))
  135. $args = $_GET["rsargs"];
  136. else
  137. $args = array();
  138. }
  139. else {
  140. $func_name = $_POST["rs"];
  141. if (! empty($_POST["rsargs"]))
  142. $args = $_POST["rsargs"];
  143. else
  144. $args = array();
  145. }
  146. if (! in_array($func_name, $sajax_export_list))
  147. echo "-:$func_name not callable";
  148. else {
  149. echo "+:";
  150. ob_flush();
  151. $result = call_user_func_array($func_name, $args);
  152. ob_clean();
  153. echo "var res = " . trim(sajax_get_js_repr($result)) . "; res;";
  154. ob_flush();
  155. flush();
  156. }
  157. exit;
  158. }
  159. function sajax_get_common_js() {
  160. global $sajax_debug_mode;
  161. global $sajax_request_type;
  162. global $sajax_remote_uri;
  163. global $sajax_failure_redirect;
  164. $t = strtoupper($sajax_request_type);
  165. if ($t != "" && $t != "GET" && $t != "POST")
  166. return "// Invalid type: $t.. \n\n";
  167. ob_start();
  168. ?>
  169. // remote scripting library
  170. // (c) copyright 2005 modernmethod, inc
  171. var sajax_debug_mode = <?php echo $sajax_debug_mode ? "true" : "false"; ?>;
  172. var sajax_request_type = "<?php echo $t; ?>";
  173. var sajax_target_id = "";
  174. var sajax_failure_redirect = "<?php echo $sajax_failure_redirect; ?>";
  175. var sajax_failed_eval = "";
  176. var sajax_fail_handle = "";
  177. function sajax_debug(text) {
  178. if (sajax_debug_mode)
  179. alert(text);
  180. }
  181. function sajax_init_object() {
  182. sajax_debug("sajax_init_object() called..")
  183. var A;
  184. var msxmlhttp = new Array(
  185. 'Msxml2.XMLHTTP.5.0',
  186. 'Msxml2.XMLHTTP.4.0',
  187. 'Msxml2.XMLHTTP.3.0',
  188. 'Msxml2.XMLHTTP',
  189. 'Microsoft.XMLHTTP');
  190. for (var i = 0; i < msxmlhttp.length; i++) {
  191. try {
  192. A = new ActiveXObject(msxmlhttp[i]);
  193. } catch (e) {
  194. A = null;
  195. }
  196. }
  197. if(!A && typeof XMLHttpRequest != "undefined")
  198. A = new XMLHttpRequest();
  199. if (!A)
  200. sajax_debug("Could not create connection object.");
  201. return A;
  202. }
  203. var sajax_requests = new Array();
  204. function sajax_cancel() {
  205. for (var i = 0; i < sajax_requests.length; i++)
  206. sajax_requests[i].abort();
  207. }
  208. function sajax_do_call(func_name, args) {
  209. var i, x, n;
  210. var uri;
  211. var post_data;
  212. var target_id;
  213. sajax_debug("in sajax_do_call().." + sajax_request_type + "/" + sajax_target_id);
  214. target_id = sajax_target_id;
  215. if (typeof(sajax_request_type) == "undefined" || sajax_request_type == "")
  216. sajax_request_type = "GET";
  217. uri = "<?php echo sajax_get_my_uri(); ?>";
  218. if (sajax_request_type == "GET") {
  219. if (uri.indexOf("?") == -1)
  220. uri += "?rs=" + escape(func_name);
  221. else
  222. uri += "&rs=" + escape(func_name);
  223. uri += "&rst=" + escape(sajax_target_id);
  224. uri += "&rsrnd=" + new Date().getTime();
  225. for (i = 0; i < args.length-1; i++)
  226. uri += "&rsargs[]=" + escape(args[i]);
  227. post_data = null;
  228. }
  229. else if (sajax_request_type == "POST") {
  230. post_data = "rs=" + escape(func_name);
  231. post_data += "&rst=" + escape(sajax_target_id);
  232. post_data += "&rsrnd=" + new Date().getTime();
  233. for (i = 0; i < args.length-1; i++)
  234. post_data = post_data + "&rsargs[]=" + escape(args[i]);
  235. }
  236. else {
  237. alert("Illegal request type: " + sajax_request_type);
  238. }
  239. x = sajax_init_object();
  240. if (x == null) {
  241. if (sajax_failure_redirect != "") {
  242. location.href = sajax_failure_redirect;
  243. return false;
  244. } else {
  245. sajax_debug("NULL sajax object for user agent:\n" + navigator.userAgent);
  246. return false;
  247. }
  248. } else {
  249. x.open(sajax_request_type, uri, true);
  250. // window.open(uri);
  251. sajax_requests[sajax_requests.length] = x;
  252. if (sajax_request_type == "POST") {
  253. x.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
  254. x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  255. }
  256. x.onreadystatechange = function() {
  257. if (x.readyState != 4)
  258. return;
  259. sajax_debug("received " + x.responseText);
  260. var status;
  261. var data;
  262. var txt = x.responseText.replace(/^\s*|\s*$/g,"");
  263. status = txt.charAt(0);
  264. data = txt.substring(2);
  265. if (status == "") {
  266. // let's just assume this is a pre-response bailout and let it slide for now
  267. } else if (status == "-")
  268. alert("Error: " + data);
  269. else {
  270. if (target_id != "")
  271. document.getElementById(target_id).innerHTML = eval(data);
  272. else {
  273. try {
  274. var callback;
  275. var extra_data = false;
  276. if (typeof args[args.length-1] == "object") {
  277. callback = args[args.length-1].callback;
  278. extra_data = args[args.length-1].extra_data;
  279. } else {
  280. callback = args[args.length-1];
  281. }
  282. callback(eval(data), extra_data);
  283. } catch (e) {
  284. alert("Caught AJAX error " + e + ": Could not eval " + data );
  285. sajax_failed_eval = data;
  286. sajax_fail_handle(data);
  287. }
  288. }
  289. }
  290. }
  291. }
  292. sajax_debug(func_name + " uri = " + uri + "/post = " + post_data);
  293. x.send(post_data);
  294. sajax_debug(func_name + " waiting..");
  295. delete x;
  296. return true;
  297. }
  298. <?php
  299. $html = ob_get_contents();
  300. ob_end_clean();
  301. return $html;
  302. }
  303. function sajax_show_common_js() {
  304. echo sajax_get_common_js();
  305. }
  306. // javascript escape a value
  307. function sajax_esc($val)
  308. {
  309. $val = str_replace("\\", "\\\\", $val);
  310. $val = str_replace("\r", "\\r", $val);
  311. $val = str_replace("\n", "\\n", $val);
  312. $val = str_replace("'", "\\'", $val);
  313. return str_replace('"', '\\"', $val);
  314. }
  315. function sajax_get_one_stub($func_name) {
  316. ob_start();
  317. ?>
  318. // wrapper for <?php echo $func_name; ?>
  319. function x_<?php echo $func_name; ?>() {
  320. sajax_do_call("<?php echo $func_name; ?>",
  321. x_<?php echo $func_name; ?>.arguments);
  322. }
  323. <?php
  324. $html = ob_get_contents();
  325. ob_end_clean();
  326. return $html;
  327. }
  328. function sajax_show_one_stub($func_name) {
  329. echo sajax_get_one_stub($func_name);
  330. }
  331. function sajax_export() {
  332. global $sajax_export_list;
  333. $n = func_num_args();
  334. for ($i = 0; $i < $n; $i++) {
  335. $sajax_export_list[] = func_get_arg($i);
  336. }
  337. }
  338. $sajax_js_has_been_shown = 0;
  339. function sajax_get_javascript()
  340. {
  341. global $sajax_js_has_been_shown;
  342. global $sajax_export_list;
  343. $html = "";
  344. if (! $sajax_js_has_been_shown) {
  345. $html .= sajax_get_common_js();
  346. $sajax_js_has_been_shown = 1;
  347. }
  348. foreach ($sajax_export_list as $func) {
  349. $html .= sajax_get_one_stub($func);
  350. }
  351. return $html;
  352. }
  353. function sajax_show_javascript()
  354. {
  355. echo sajax_get_javascript();
  356. }
  357. $SAJAX_INCLUDED = 1;
  358. }
  359. ?>