Source of Sajax_json.php
<?php
if (!isset($SAJAX_INCLUDED)) {
$sajax_debug_mode = 0;
$sajax_export_list = array();
function sajax_init() {
}
function sajax_handle_client_request() {
global $sajax_export_list;
if (empty($_GET["rs"]))
return;
// Bust cache in the head
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
// always modified
header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header ("Pragma: no-cache"); // HTTP/1.0
$func_name = $_GET["rs"];
if (! in_array($func_name, $sajax_export_list))
echo "-:$func_name not callable";
else {
echo "+:";
if (empty($_GET["rsargs"]))
$result = call_user_func($_GET["rs"]);
else
$result = call_user_func_array($_GET["rs"], $_GET["rsargs"]);
echo $result;
}
exit;
}
function setObject($obj,$val) {
$response = '"' . $obj . '": ';
if ( is_array($val)) {
$response .= "{";
$firstItem = true;
foreach($val as $key => $value) {
if ( !$firstItem ) {
$response .= ',';
} else {
$firstItem = false;
}
$response .= setObject($key,$value);
}
$response .= "}";
} else {
if ( is_string($val)) {
$response .= '"' . addslashes($val) . '"';
} else {
$response .= $val;
}
}
return $response;
}
function sajax_serialize() {
// data pairs should be "variable name","variable value","variable name","variable value",...
$response = "";
$numArgs = func_num_args();
if ( $numArgs < 2 ) {
return $response;
}
$response = '{"numArgs": ' . $numArgs;
$argList = func_get_args();
for( $i=0; $i < $numArgs; $i+=2 ) {
$response .= ',';
$response .= setObject($argList[$i],$argList[$i+1]);
}
$response .= "}";
return $response;
}
function sajax_get_common_js() {
global $sajax_debug_mode;
global $REQUEST_URI;
$uri = (strpos($REQUEST_URI,"?") === false)?"?rs=":"&rs=";
/*
if ( strpos($uri,"?") === false )
$uri .= "?rs=";
else
$uri .= "&rs=";
*/
ob_start();
?>
// remote scripting library
// (c) copyright 2005 modernmethod, inc
var sajax_debug_mode = <?php echo $sajax_debug_mode ? "true" : "false"; ?>;
function sajax_debug(text) {
if (sajax_debug_mode)
alert("RSD: " + text)
}
function sajax_init_object() {
sajax_debug("sajax_init_object() called..")
if (window.sajax_objects) {
if (window.sajax_objects.length > 0) {
return window.sajax_objects.pop();
}
}
var A;
try {
A=new ActiveXObject("Msxml2.XMLHTTP.6.0");
} catch(e6) {
try {
A=new ActiveXObject("Msxml2.XMLHTTP.3.0");
} catch(e3) {
try {
A=new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
A=null;
try {
A=new ActiveXObject("Microsoft.XMLHTTP");
} catch (oc) {
A=null;
}
}
}
}
if(!A && typeof XMLHttpRequest != "undefined")
A = new XMLHttpRequest();
if (!A)
sajax_debug("Could not create connection object.");
return A;
}
function sajax_do_call(func_name, args) {
var i, x, n;
var uri;
uri = "<?= $REQUEST_URI ?>";
if ( uri.indexOf("?") == -1 ) {
uri = uri + "?rs=" + escape(func_name);
} else {
uri = uri + "&rs=" + escape(func_name);
}
var url = uri;
// var url = "<?= substr($_SERVER["PHP_SELF"],strrpos($_SERVER["PHP_SELF"],"/")+1) ?>?rs=" + func_name;
for (i = 0; i < args.length-1; i++)
url = url + "&rsargs[]=" + escape(args[i]);
url = url + "&rsrnd=" + new Date().getTime();
x = sajax_init_object();
if ( x == null ) {
return false;
}
// x.open("GET", url, true);
x.open("POST", url, true);
// Request a header Added 2005-03-15
x.setRequestHeader("Method", "POST HTTP/1.1");
x.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
x.onreadystatechange = function() {
if (x.readyState == 1 /* loading */ ) {
if (typeof(sajax_loading)=="function") {
sajax_loading();
}
} else if (x.readyState == 2 /* loaded */ ) {
if (typeof(sajax_loaded)=="function") {
sajax_loaded();
}
}
if (x.readyState != 4)
return;
sajax_debug("received " + x.responseText);
var status;
var data;
status = x.responseText.charAt(0);
data = x.responseText.substring(2);
if (status == "-") {
alert("Error: " + data);
return false;
} else if ( args[args.length-1].charAt) /* (0) == '#') */{
var theTarget = args[args.length-1].substring(1);
var theItem = document.getElementById(theTarget);
// alert(''+theItem.getAttribute("value")+'');
if ( theItem.getAttribute("value") == null) {
// alert("innerHTML!");
theItem.innerHTML = data;
} else {
// alert("Value!");
theItem.value = data;
}
} else {
args[args.length-1](data);
}
if (!window.sajax_objects) {
window.sajax_objects = new Array();
}
window.sajax_objects.push(x);
}
x.send(null);
sajax_debug(func_name + " url = " + url);
sajax_debug(func_name + " waiting..");
delete x;
return true;
}
function isObject(a) {
return (a && typeof a == 'object');
}
function isArray(a) {
return (isObject(a) && a.constructor == Array);
}
function explode(needle,haystack) {
nArray = new Object();
if (( needle == null ) ||
( haystack == null )) {
return null;
}
numItems = 1;
while( haystack.indexOf(needle) > -1 ) {
nArray[numItems] = haystack.substring(0,haystack.indexOf(needle));
haystack = haystack.substring((haystack.indexOf(needle))+1);
numItems++;
}
nArray[numItems] = haystack;
return nArray;
}
function parseGET(result) {
FORM_DATA = new Object();
keypairs = new Object();
numKP = 1;
separator = ',';
while( result.indexOf('&') > -1 ) {
keypairs[numKP] = result.substring(0,result.indexOf('&'));
result = result.substring((result.indexOf('&'))+1);
numKP++;
}
keypairs[numKP] = result;
for ( i in keypairs ) {
keyName = keypairs[i].substring(0,keypairs[i].indexOf('='));
keyValue = keypairs[i].substring((keypairs[i].indexOf('='))+1);
while( keyValue.indexOf('+')>-1) {
keyValue = keyValue.substring(0,keyValue.indexOf('+'))+' '+keyValue.substring(keyValue.indexOf('+') + 1);
}
keyValue = unescape(keyValue);
if ( FORM_DATA[keyName]) {
if ( isArray(FORM_DATA[keyName])) {
FORM_DATA[keyName][FORM_DATA[keyName].length] = keyValue;
} else {
tempVal = FORM_DATA[keyName];
FORM_DATA[keyName] = new Array();
FORM_DATA[keyName][0] = tempVal;
FORM_DATA[keyName][1] = keyValue;
}
// FORM_DATA[keyName] = FORM_DATA[keyName] + separator + keyValue;
} else {
FORM_DATA[keyName] = keyValue;
}
}
return FORM_DATA;
}
<?php
$html = ob_get_contents();
ob_end_clean();
return $html;
}
function sajax_show_common_js() {
echo sajax_get_common_js();
}
// javascript escape a value
function sajax_esc($val)
{
return str_replace('"', '\\\\"', $val);
}
function sajax_get_one_stub($func_name) {
global $REQUEST_URI;
$uri = $REQUEST_URI;
if (strpos($uri,"?") === false)
$uri .= "?rs=".urlencode($func_name);
else
$uri .= "&rs=".urlencode($func_name);
ob_start();
?>
// wrapper for <?php echo $func_name; ?>
function x_<?php echo $func_name; ?>() {
// count args; build URL
sajax_do_call("<?php echo $func_name; ?>",
<? /* x_<?php echo $func_name; ?>. */ ?>arguments);
}
<?php
$html = ob_get_contents();
ob_end_clean();
return $html;
}
function sajax_show_one_stub($func_name) {
echo sajax_get_one_stub($func_name);
}
function sajax_export() {
global $sajax_export_list;
$n = func_num_args();
for ($i = 0; $i < $n; $i++) {
$sajax_export_list[] = func_get_arg($i);
}
}
$sajax_js_has_been_shown = 0;
function sajax_get_javascript()
{
global $sajax_js_has_been_shown;
global $sajax_export_list;
$html = "";
if (! $sajax_js_has_been_shown) {
$html .= sajax_get_common_js();
$sajax_js_has_been_shown = 1;
}
foreach ($sajax_export_list as $func) {
$html .= sajax_get_one_stub($func);
}
return $html;
}
function sajax_show_javascript()
{
echo sajax_get_javascript();
}
$SAJAX_INCLUDED = 1;
}
?>