PageRenderTime 27ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

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