00001 <?php
00002 if (!isset($SAJAX_INCLUDED)) {
00003
00004
00005
00006
00007
00008 $GLOBALS['sajax_version'] = '0.12';
00009 $GLOBALS['sajax_debug_mode'] = 0;
00010 $GLOBALS['sajax_export_list'] = array();
00011 $GLOBALS['sajax_request_type'] = 'GET';
00012 $GLOBALS['sajax_remote_uri'] = '';
00013 $GLOBALS['sajax_failure_redirect'] = '';
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 function sajax_init() {
00024 }
00025
00026
00027
00028
00029 function sajax_get_my_uri() {
00030 return $_SERVER["REQUEST_URI"];
00031 }
00032 $sajax_remote_uri = sajax_get_my_uri();
00033
00034
00035
00036
00037
00038 function sajax_get_js_repr($value) {
00039 $type = gettype($value);
00040
00041 if ($type == "boolean") {
00042 return ($value) ? "Boolean(true)" : "Boolean(false)";
00043 }
00044 elseif ($type == "integer") {
00045 return "parseInt($value)";
00046 }
00047 elseif ($type == "double") {
00048 return "parseFloat($value)";
00049 }
00050 elseif ($type == "array" || $type == "object" ) {
00051
00052
00053
00054
00055
00056 $s = "{ ";
00057 if ($type == "object") {
00058 $value = get_object_vars($value);
00059 }
00060 foreach ($value as $k=>$v) {
00061 $esc_key = sajax_esc($k);
00062 if (is_numeric($k))
00063 $s .= "$k: " . sajax_get_js_repr($v) . ", ";
00064 else
00065 $s .= "\"$esc_key\": " . sajax_get_js_repr($v) . ", ";
00066 }
00067 if (count($value))
00068 $s = substr($s, 0, -2);
00069 return $s . " }";
00070 }
00071 else {
00072 $esc_val = sajax_esc($value);
00073 $s = "'$esc_val'";
00074 return $s;
00075 }
00076 }
00077
00078 function sajax_handle_client_request() {
00079 global $sajax_export_list;
00080
00081 $mode = "";
00082
00083 if (! empty($_GET["rs"]))
00084 $mode = "get";
00085
00086 if (!empty($_POST["rs"]))
00087 $mode = "post";
00088
00089 if (empty($mode))
00090 return;
00091
00092 $target = "";
00093
00094 if ($mode == "get") {
00095
00096 header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
00097 header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
00098
00099 header ("Cache-Control: no-cache, must-revalidate");
00100 header ("Pragma: no-cache");
00101 $func_name = $_GET["rs"];
00102 if (! empty($_GET["rsargs"]))
00103 $args = $_GET["rsargs"];
00104 else
00105 $args = array();
00106 }
00107 else {
00108 $func_name = $_POST["rs"];
00109 if (! empty($_POST["rsargs"]))
00110 $args = $_POST["rsargs"];
00111 else
00112 $args = array();
00113 }
00114
00115 if (! in_array($func_name, $sajax_export_list))
00116 echo "-:$func_name not callable";
00117 else {
00118 echo "+:";
00119 $result = call_user_func_array($func_name, $args);
00120 echo "var res = " . trim(sajax_get_js_repr($result)) . "; res;";
00121 }
00122 exit;
00123 }
00124
00125 function sajax_get_common_js() {
00126 global $sajax_debug_mode;
00127 global $sajax_request_type;
00128 global $sajax_remote_uri;
00129 global $sajax_failure_redirect;
00130
00131 $t = strtoupper($sajax_request_type);
00132 if ($t != "" && $t != "GET" && $t != "POST")
00133 return "// Invalid type: $t.. \n\n";
00134
00135 ob_start();
00136 ?>
00137
00138
00139
00140 var sajax_debug_mode = <?php echo $sajax_debug_mode ? "true" : "false"; ?>;
00141 var sajax_request_type = "<?php echo $t; ?>";
00142 var sajax_target_id = "";
00143 var sajax_failure_redirect = "<?php echo $sajax_failure_redirect; ?>";
00144
00145 function sajax_debug(text) {
00146 if (sajax_debug_mode)
00147 alert(text);
00148 }
00149
00150 function sajax_init_object() {
00151 sajax_debug("sajax_init_object() called..")
00152
00153 var A;
00154
00155 var msxmlhttp = new Array(
00156 'Msxml2.XMLHTTP.5.0',
00157 'Msxml2.XMLHTTP.4.0',
00158 'Msxml2.XMLHTTP.3.0',
00159 'Msxml2.XMLHTTP',
00160 'Microsoft.XMLHTTP');
00161 for (var i = 0; i < msxmlhttp.length; i++) {
00162 try {
00163 A = new ActiveXObject(msxmlhttp[i]);
00164 } catch (e) {
00165 A = null;
00166 }
00167 }
00168
00169 if(!A && typeof XMLHttpRequest != "undefined")
00170 A = new XMLHttpRequest();
00171 if (!A)
00172 sajax_debug("Could not create connection object.");
00173 return A;
00174 }
00175
00176 var sajax_requests = new Array();
00177
00178 function sajax_cancel() {
00179 for (var i = 0; i < sajax_requests.length; i++)
00180 sajax_requests[i].abort();
00181 }
00182
00183 function sajax_do_call(func_name, args) {
00184 var i, x, n;
00185 var uri;
00186 var post_data;
00187 var target_id;
00188
00189 sajax_debug("in sajax_do_call().." + sajax_request_type + "/" + sajax_target_id);
00190 target_id = sajax_target_id;
00191 if (typeof(sajax_request_type) == "undefined" || sajax_request_type == "")
00192 sajax_request_type = "GET";
00193
00194 uri = "<?php echo $sajax_remote_uri; ?>";
00195 if (sajax_request_type == "GET") {
00196
00197 if (uri.indexOf("?") == -1)
00198 uri += "?rs=" + escape(func_name);
00199 else
00200 uri += "&rs=" + escape(func_name);
00201 uri += "&rst=" + escape(sajax_target_id);
00202 uri += "&rsrnd=" + new Date().getTime();
00203
00204 for (i = 0; i < args.length-1; i++)
00205 uri += "&rsargs[]=" + escape(args[i]);
00206
00207 post_data = null;
00208 }
00209 else if (sajax_request_type == "POST") {
00210 post_data = "rs=" + escape(func_name);
00211 post_data += "&rst=" + escape(sajax_target_id);
00212 post_data += "&rsrnd=" + new Date().getTime();
00213
00214 for (i = 0; i < args.length-1; i++)
00215 post_data = post_data + "&rsargs[]=" + escape(args[i]);
00216 }
00217 else {
00218 alert("Illegal request type: " + sajax_request_type);
00219 }
00220
00221 x = sajax_init_object();
00222 if (x == null) {
00223 if (sajax_failure_redirect != "") {
00224 location.href = sajax_failure_redirect;
00225 return false;
00226 } else {
00227 sajax_debug("NULL sajax object for user agent:\n" + navigator.userAgent);
00228 return false;
00229 }
00230 } else {
00231 x.open(sajax_request_type, uri, true);
00232
00233
00234 sajax_requests[sajax_requests.length] = x;
00235
00236 if (sajax_request_type == "POST") {
00237 x.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
00238 x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
00239 }
00240
00241 x.onreadystatechange = function() {
00242 if (x.readyState != 4)
00243 return;
00244
00245 sajax_debug("received " + x.responseText);
00246
00247 var status;
00248 var data;
00249 var txt = x.responseText.replace(/^\s*|\s*$/g,"");
00250 status = txt.charAt(0);
00251 data = txt.substring(2);
00252
00253 if (status == "") {
00254
00255 } else if (status == "-")
00256 alert("Error: " + data);
00257 else {
00258 if (target_id != "")
00259 document.getElementById(target_id).innerHTML = eval(data);
00260 else {
00261 try {
00262 var callback;
00263 var extra_data = false;
00264 if (typeof args[args.length-1] == "object") {
00265 callback = args[args.length-1].callback;
00266 extra_data = args[args.length-1].extra_data;
00267 } else {
00268 callback = args[args.length-1];
00269 }
00270 callback(eval(data), extra_data);
00271 } catch (e) {
00272 sajax_debug("Caught error " + e + ": Could not eval " + data );
00273 }
00274 }
00275 }
00276 }
00277 }
00278
00279 sajax_debug(func_name + " uri = " + uri + "/post = " + post_data);
00280 x.send(post_data);
00281 sajax_debug(func_name + " waiting..");
00282 delete x;
00283 return true;
00284 }
00285
00286 <?php
00287 $html = ob_get_contents();
00288 ob_end_clean();
00289 return $html;
00290 }
00291
00292 function sajax_show_common_js() {
00293 echo sajax_get_common_js();
00294 }
00295
00296
00297 function sajax_esc($val)
00298 {
00299 $val = str_replace("\\", "\\\\", $val);
00300 $val = str_replace("\r", "\\r", $val);
00301 $val = str_replace("\n", "\\n", $val);
00302 $val = str_replace("'", "\\'", $val);
00303 return str_replace('"', '\\"', $val);
00304 }
00305
00306 function sajax_get_one_stub($func_name) {
00307 ob_start();
00308 ?>
00309
00310
00311
00312 function x_<?php echo $func_name; ?>() {
00313 sajax_do_call("<?php echo $func_name; ?>",
00314 x_<?php echo $func_name; ?>.arguments);
00315 }
00316
00317 <?php
00318 $html = ob_get_contents();
00319 ob_end_clean();
00320 return $html;
00321 }
00322
00323 function sajax_show_one_stub($func_name) {
00324 echo sajax_get_one_stub($func_name);
00325 }
00326
00327 function sajax_export() {
00328 global $sajax_export_list;
00329
00330 $n = func_num_args();
00331 for ($i = 0; $i < $n; $i++) {
00332 $sajax_export_list[] = func_get_arg($i);
00333 }
00334 }
00335
00336 $sajax_js_has_been_shown = 0;
00337 function sajax_get_javascript()
00338 {
00339 global $sajax_js_has_been_shown;
00340 global $sajax_export_list;
00341
00342 $html = "";
00343 if (! $sajax_js_has_been_shown) {
00344 $html .= sajax_get_common_js();
00345 $sajax_js_has_been_shown = 1;
00346 }
00347 foreach ($sajax_export_list as $func) {
00348 $html .= sajax_get_one_stub($func);
00349 }
00350 return $html;
00351 }
00352
00353 function sajax_show_javascript()
00354 {
00355 echo sajax_get_javascript();
00356 }
00357
00358
00359 $SAJAX_INCLUDED = 1;
00360 }
00361