charset = is_object($GLOBALS['egw']->translation) ? $GLOBALS['egw']->translation->charset() : 'UTF-8';
//error_log("xajaxResponse: charset=$this->charset");
$this->xml = "charset\"?>";
$this->xml .= "";
}
// addAssign() adds an assign command message to your xml response
// $sTarget is a string containing the id of an HTML element
// $sAttribute is the part of the element you wish to modify ("innerHTML", "value", etc.)
// $sData is the data you want to set the attribute to
// usage: $objResponse->addAssign("contentDiv","innerHTML","Some Text");
function addAssign($sTarget,$sAttribute,$sData)
{
$this->xml .= "";
$this->xml .= "$sTarget";
$this->xml .= "";
$this->xml .= "";
}
// addAppend() adds an append command message to your xml response
// $sTarget is a string containing the id of an HTML element
// $sAttribute is the part of the element you wish to modify ("innerHTML", "value", etc.)
// $sData is the data you want to append to the end of the attribute
// usage: $objResponse->addAppend("contentDiv","innerHTML","Some Text");
function addAppend($sTarget,$sAttribute,$sData)
{
$this->xml .= "";
$this->xml .= "$sTarget";
$this->xml .= "";
$this->xml .= "";
}
// addPrepend() adds an prepend command message to your xml response
// $sTarget is a string containing the id of an HTML element
// $sAttribute is the part of the element you wish to modify ("innerHTML", "value", etc.)
// $sData is the data you want to prepend to the beginning of the attribute
// usage: $objResponse->addPrepend("contentDiv","innerHTML","Some Text");
function addPrepend($sTarget,$sAttribute,$sData)
{
$this->xml .= "";
$this->xml .= "$sTarget";
$this->xml .= "";
$this->xml .= "";
}
// addReplace() adds an replace command message to your xml response
// $sTarget is a string containing the id of an HTML element
// $sAttribute is the part of the element you wish to modify ("innerHTML", "value", etc.)
// $sSearch is a string to search for
// $sData is a string to replace the search string when found in the attribute
// usage: $objResponse->addReplace("contentDiv","innerHTML","text","text");
function addReplace($sTarget,$sAttribute,$sSearch,$sData)
{
$this->xml .= "";
$this->xml .= "$sTarget";
$this->xml .= "";
$this->xml .= "";
$this->xml .= "";
}
// addClear() adds an clear command message to your xml response
// $sTarget is a string containing the id of an HTML element
// $sAttribute is the part of the element you wish to clear ("innerHTML", "value", etc.)
// usage: $objResponse->addClear("contentDiv","innerHTML");
function addClear($sTarget,$sAttribute)
{
$this->xml .= "";
$this->xml .= "$sTarget";
$this->xml .= "";
}
// addAlert() adds an alert command message to your xml response
// $sMsg is a text to be displayed in the alert box
// usage: $objResponse->addAlert("This is some text");
function addAlert($sMsg)
{
$this->xml .= "";
}
// addScript() adds a jscript command message to your xml response
// $sJS is a string containing javascript code to be executed
// usage: $objResponse->addAlert("var x = prompt('get some text');");
function addScript($sJS)
{
$this->xml .= "";
}
// addRemove() adds a Remove Element command message to your xml response
// $sTarget is a string containing the id of an HTML element to be removed
// from your page
// usage: $objResponse->addRemove("Div2");
function addRemove($sTarget)
{
$this->xml .= "";
$this->xml .= "$sTarget";
$this->xml .= "";
}
function addCreate($sParent, $sTag, $sId, $sType="")
{
$this->xml .= "";
$this->xml .= "$sParent";
$this->xml .= "";
if ($sType != "")
$this->xml .= "";
$this->xml .= "";
}
// getXML() returns the xml to be returned from your function to the xajax
// processor on your page
// usage: $objResponse->getXML();
function getXML()
{
if (strstr($this->xml,"") == false)
$this->xml .= "";
return $this->xml;
}
}// end class xajaxResponse
// Communication Method Defines
if (!defined ('GET'))
{
define ('GET', 0);
}
if (!defined ('POST'))
{
define ('POST', 1);
}
// the xajax class generates the xajax javascript for your page including the
// javascript wrappers for the PHP functions that you want to call from your page.
// It also handles processing and executing the command messages in the xml responses
// sent back to your page from your PHP functions.
class xajax
{
var $aFunctions; // Array of PHP functions that will be callable through javascript wrappers
var $aFunctionRequestTypes; // Array of RequestTypes to be used with each function (key=function name)
var $sRequestURI; // The URI for making requests to the xajax object
var $bDebug; // Show debug messages true/false
var $sWrapperPrefix; // The prefix to prepend to the javascript wraper function name
var $bStatusMessages; // Show debug messages true/false
var $aObjArray; // Array for parsing complex objects
var $iPos; // Position in $aObjArray
// Contructor
// $sRequestURI - defaults to the current page
// $bDebug Mode - defaults to false
// $sWrapperPrefix - defaults to "xajax_";
// usage: $xajax = new xajax();
function xajax($sRequestURI="",$sWrapperPrefix="xajax_",$bDebug=false)
{
$this->aFunctions = array();
$this->sRequestURI = $sRequestURI;
if ($this->sRequestURI == "")
$this->sRequestURI = $this->detectURI();
$this->sWrapperPrefix = $sWrapperPrefix;
$this->bDebug = $bDebug;
}
// detectURL() returns the current URL based upon the SERVER vars
// used internally
function detectURI()
{
$aUri = array();
if (!empty($_SERVER['REQUEST_URI']))
{
$aUri = parse_url($_SERVER['REQUEST_URI']);
}
if (empty($aUri['scheme']))
{
if (!empty($_SERVER['HTTP_SCHEME']))
{
$aUri['scheme'] = $_SERVER['HTTP_SCHEME'];
}
else
{
$aUri['scheme'] = (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) != 'off') ? 'https' : 'http';
}
if (!empty($_SERVER['HTTP_HOST']))
{
if (strpos($_SERVER['HTTP_HOST'], ':') > 0)
{
list($aUri['host'], $aUri['port']) = explode(':', $_SERVER['HTTP_HOST']);
}
else
{
$aUri['host'] = $_SERVER['HTTP_HOST'];
}
}
else if (!empty($_SERVER['SERVER_NAME']))
{
$aUri['host'] = $_SERVER['SERVER_NAME'];
}
else
{
print "xajax Error: xajax failed to automatically identify your Request URI.";
print "Please set the Request URI explicitly when you instantiate the xajax object.";
exit();
}
if (empty($aUri['port']) && !empty($_SERVER['SERVER_PORT']))
{
$aUri['port'] = $_SERVER['SERVER_PORT'];
}
if (empty($aUri['path']))
{
if (!empty($_SERVER['PATH_INFO']))
{
$path = parse_url($_SERVER['PATH_INFO']);
}
else
{
$path = parse_url($_SERVER['PHP_SELF']);
}
$aUri['path'] = $path['path'];
unset($path);
}
}
$sUri = $aUri['scheme'] . '://';
if (!empty($aUri['user']))
{
$sUri .= $aUri['user'];
if (!empty($aUri['pass']))
{
$sUri .= ':' . $aUri['pass'];
}
$sUri .= '@';
}
$sUri .= $aUri['host'];
if (!empty($aUri['port']) && (($aUri['scheme'] == 'http' && $aUri['port'] != 80) || ($aUri['scheme'] == 'https' && $aUri['port'] != 443)))
{
$sUri .= ':'.$aUri['port'];
}
// And finally path, without script name
$sUri .= substr($aUri['path'], 0, strrpos($aUri['path'], '/') + 1);
unset($aUri);
return $sUri.basename($_SERVER['SCRIPT_NAME']);
}
// setRequestURI() sets the URI to which requests will be made
// usage: $xajax->setRequestURI("http://xajax.sourceforge.net");
function setRequestURI($sRequestURI)
{
$this->sRequestURI = $sRequestURI;
}
// debugOn() enables debug messages for xajax
// usage: $xajax->debugOn();
function debugOn()
{
$this->bDebug = true;
}
// debugOff() disables debug messages for xajax
// usage: $xajax->debugOff();
function debugOff()
{
$this->bDebug = false;
}
// statusMessagesOn() enables messages in the statusbar for xajax
// usage: $xajax->statusMessagesOn();
function statusMessagesOn()
{
$this->bStatusMessages = true;
}
// statusMessagesOff() disables messages in the statusbar for xajax
// usage: $xajax->statusMessagesOff();
function statusMessagesOff()
{
$this->bStatusMessages = false;
}
// setWrapperPrefix() sets the prefix that will be appended to the javascript
// wraper functions.
function setWrapperPrefix($sPrefix)
{
$this->sWrapperPrefix = $sPrefix;
}
//Dpericated. Use registerFunction();
function addFunction($sFunction,$sRequestType=POST)
{
trigger_error("xajax: the addFunction() method has been renamed registerFunction().
Please use ->registerFunction('$sFunction'".($sRequestType==GET?",GET":"")."); instead.",E_USER_WARNING);
$this->registerFunction($sFunction,$sRequestType);
}
// registerFunction() registers a PHP function to be callable through xajax
// $sFunction is a string containing the function name
// $sRequestType is the RequestType (GET/POST) that should be used
// for this function. Defaults to POST.
// usage: $xajax->registerFunction("myfunction",POST);
function registerFunction($sFunction,$sRequestType=POST)
{
$this->aFunctions[] = $sFunction;
$this->aFunctionRequestTypes[$sFunction] = $sRequestType;
}
// generates the javascript wrapper for the specified PHP function
// used internally
function wrap($sFunction,$sRequestType=POST)
{
$js = "function ".$this->sWrapperPrefix."$sFunction(){xajax.call(\"$sFunction\", arguments, ".$sRequestType.");}\n";
return $js;
}
// processRequests() is the main communications engine of xajax
// The engine handles all incoming xajax requests, calls the apporiate PHP functions
// and passes the xml responses back to the javascript response handler
// if your RequestURI is the same as your web page then this function should
// be called before any headers or html has been sent.
// usage: $xajax->processRequests()
function processRequests()
{
if (!empty($_GET['xajaxjs']))
{
header("Content-type: text/javascript");
print $this->generateJavascript();
exit();
return;
}
$requestMode = -1;
$sFunctionName = "";
$aArgs = array();
$sResponse = "";
if (!empty($_GET["xajax"]))
$requestMode = GET;
if (!empty($_POST["xajax"]))
$requestMode = POST;
if ($requestMode == -1)
return;
if ($requestMode == POST)
{
$sFunctionName = $_POST["xajax"];
if (!empty($_POST["xajaxargs"]))
$aArgs = $_POST["xajaxargs"];
}
else
{
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");
header("Content-type: text/xml");
$sFunctionName = $_GET["xajax"];
if (!empty($_GET["xajaxargs"]))
$aArgs = $_GET["xajaxargs"];
}
if (!in_array($sFunctionName, $this->aFunctions))
{
$objResponse = new xajaxResponse();
$objResponse->addAlert("Unknown Function $sFunctionName.");
$sResponse = $objResponse->getXML();
}
else if ($this->aFunctionRequestTypes[$sFunctionName] != $requestMode)
{
$objResponse = new xajaxResponse();
$objResponse->addAlert("Incorrect Request Type.");
$sResponse = $objResponse->getXML();
}
else
{
for ($i = 0; $i < sizeof($aArgs); $i++)
{
if (stristr($aArgs[$i],"") != false)
{
$aArgs[$i] = $this->xmlToArray("xjxobj",$aArgs[$i]);
}
else if (stristr($aArgs[$i],"") != false)
{
$aArgs[$i] = $this->xmlToArray("xjxquery",$aArgs[$i]);
}
}
$sResponse = call_user_func_array($sFunctionName, $aArgs);
}
header("Content-type: text/xml; charset=$this->charset");
print $sResponse;
exit();
}
// xmlToArray() takes a string containing xajax xjxobj xml or xjxquery xml
// and builds an array representation of it to pass as an argument to
// the php function being called. Returns an array.
// used internally
function xmlToArray($rootTag, $sXml)
{
$aArray = array();
$sXml = str_replace("<$rootTag>","<$rootTag>|~|",$sXml);
$sXml = str_replace("$rootTag>","$rootTag>|~|",$sXml);
$sXml = str_replace("","|~|",$sXml);
$sXml = str_replace("","|~|",$sXml);
$sXml = str_replace("","|~|",$sXml);
$sXml = str_replace("","|~||~|",$sXml);
$sXml = str_replace("","|~|",$sXml);
$sXml = str_replace("","|~||~|",$sXml);
$sXml = str_replace("","|~|",$sXml);
$sXml = str_replace("
","|~|
|~|",$sXml);
$this->aObjArray = explode("|~|",$sXml);
$this->iPos = 0;
$aArray = $this->parseObjXml($rootTag);
return $aArray;
}
// parseObjXml() is a recursive function that generates an array from the
// contents of $this->aObjArray. Returns an array.
// used internally
function parseObjXml($rootTag)
{
$aArray = array();
if ($rootTag == "xjxobj")
{
while(!stristr($this->aObjArray[$this->iPos],""))
{
$this->iPos++;
if(stristr($this->aObjArray[$this->iPos],""))
{
$key = "";
$value = null;
$this->iPos++;
while(!stristr($this->aObjArray[$this->iPos],""))
{
if(stristr($this->aObjArray[$this->iPos],""))
{
$this->iPos++;
while(!stristr($this->aObjArray[$this->iPos],""))
{
$key .= $this->aObjArray[$this->iPos];
$this->iPos++;
}
}
if(stristr($this->aObjArray[$this->iPos],""))
{
$this->iPos++;
while(!stristr($this->aObjArray[$this->iPos],""))
{
if(stristr($this->aObjArray[$this->iPos],""))
{
$value = $this->parseObjXml("xjxobj");
$this->iPos++;
}
else
{
$value .= $this->aObjArray[$this->iPos];
}
$this->iPos++;
}
}
$this->iPos++;
}
$aArray[$key]=$value;
}
}
}
if ($rootTag == "xjxquery")
{
$sQuery = "";
$this->iPos++;
while(!stristr($this->aObjArray[$this->iPos],""))
{
if (stristr($this->aObjArray[$this->iPos],"") || stristr($this->aObjArray[$this->iPos],"
"))
{
$this->iPos++;
continue;
}
$sQuery .= $this->aObjArray[$this->iPos];
$this->iPos++;
}
parse_str($sQuery, $aArray);
}
return $aArray;
}
// Depricated. Use printJavascript();
function javascript($sJsURI="")
{
trigger_error("xajax: the javascript() method has been renamed printJavascript().
Please use ->printJavascript(".($sJsURI==""?"":"'$sJsURI'")."); instead.",E_USER_WARNING);
$this->printJavascript($sJsURI);
}
// printJavascript() prints the xajax javascript code into your page
// it should only be called between the tags
// usage:
//
// ...
// printJavascript();
function printJavascript($sJsURI="")
{
print $this->getJavascript($sJsURI);
}
// getJavascript() returns the xajax javascript code that should be added to
// your page between the tags
// usage:
//
// ...
// getJavascript();
function getJavascript($sJsURI="")
{
if ($sJsURI == "")
$sJsURI = $this->sRequestURI;
$separator=strpos($sJsURI,'?')==false?'?':'&';
$html = "\n";
$html .= "\t\n";
return $html;
}
// compressJavascript() compresses the javascript code for more efficient delivery
// used internally
// $sJS is a string containing the javascript code to compress
function compressJavascript($sJS)
{
//remove windows cariage returns
$sJS = str_replace("\r","",$sJS);
//array to store replaced literal strings
$literal_strings = array();
//explode the string into lines
$lines = explode("\n",$sJS);
//loop through all the lines, building a new string at the same time as removing literal strings
$clean = "";
$inComment = false;
$literal = "";
$inQuote = false;
$escaped = false;
$quoteChar = "";
for($i=0;$ibDebug){ $js .= "var xajaxDebug=".($this->bDebug?"true":"false").";\n"; }
ob_start();
?>
function Xajax()
{
bDebug){ ?>this.DebugMessage = function(text){if (xajaxDebug) alert("Xajax Debug:\n " + text)}
this.workId = 'xajaxWork'+ new Date().getTime();
this.depth = 0;
//Get the XMLHttpRequest Object
this.getRequestObject = function()
{
bDebug){ ?>this.DebugMessage("Initializing Request Object..");
var req;
try
{
req=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
req=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e2)
{
req=null;
}
}
if(!req && typeof XMLHttpRequest != "undefined")
req = new XMLHttpRequest();
bDebug){ ?>if (!req) this.DebugMessage("Request Object Instantiation failed.");
return req;
}
// xajax.$() is shorthand for document.getElementById()
this.$ = function(sId)
{
return document.getElementById(sId);
}
// xajax.getFormValues() builds a query string XML message from the elements of a form object
this.getFormValues = function(frm)
{
var objForm;
if (typeof(frm) == "string")
objForm = this.$(frm);
else
objForm = frm;
var sXml = "";
if (objForm && objForm.tagName == 'FORM')
{
var formElements = objForm.elements;
for( var i=0; i < formElements.length; i++)
{
if ((formElements[i].type == 'radio' || formElements[i].type == 'checkbox') && formElements[i].checked == false)
continue;
var name = formElements[i].name;
if (name)
{
if (sXml != '')
sXml += '&';
sXml += name+"="+encodeURIComponent(formElements[i].value);
}
}
}
sXml +="
";
return sXml;
}
// Generates an XML message that xajax can understand from a javascript object
this.objectToXML = function(obj)
{
var sXml = "";
for (i in obj)
{
try
{
if (i == 'constructor')
continue;
if (obj[i] && typeof(obj[i]) == 'function')
continue;
var key = i;
var value = obj[i];
if (value && typeof(value)=="object" &&
(value.constructor == Array
) && this.depth <= 50)
{
this.depth++;
value = this.objectToXML(value);
this.depth--;
}
sXml += ""+key+""+value+"";
}
catch(e)
{
bDebug){ ?>this.DebugMessage(e);
}
}
sXml += "";
return sXml;
}
// Sends a XMLHttpRequest to call the specified PHP function on the server
this.call = function(sFunction, aArgs, sRequestType)
{
var i,r,postData;
if (document.body)
document.body.style.cursor = 'wait';
bStatusMessages == true){?>window.status = 'Sending Request...';
bDebug){ ?>this.DebugMessage("Starting xajax...");
var xajaxRequestType = sRequestType;
var uri = xajaxRequestUri;
var value;
switch(xajaxRequestType)
{
case :{
var uriGet = uri.indexOf("?")==-1?"?xajax="+encodeURIComponent(sFunction):"&xajax="+encodeURIComponent(sFunction);
for (i = 0; i:{
postData = "xajax="+encodeURIComponent(sFunction);
postData += "&xajaxr="+new Date().getTime();
for (i = 0; i ?"GET":"POST", uri, true);
if (xajaxRequestType == )
{
try
{
r.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
r.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
}
catch(e)
{
alert("Your browser does not appear to support asynchronous requests using POST.");
return false;
}
}
r.onreadystatechange = function()
{
if (r.readyState != 4)
return;
if (r.status==200)
{
bDebug){ ?>xajax.DebugMessage("Received:\n" + r.responseText);
var data = r.responseXML;
if (data)
xajax.processResponse(data);
}
}
bDebug){ ?>this.DebugMessage("Calling "+sFunction +" uri="+uri+" (post:"+ postData +")");
r.send(postData);
bStatusMessages == true){?>window.status = 'Waiting for data...';
bDebug){ ?>this.DebugMessage(sFunction + " waiting..");
delete r;
return true;
}
// Tests if the new Data is the same as the extant data
this.willChange = function(element, attribute, newData)
{
var oldData;
if (attribute == "innerHTML")
{
tmpXajax = this.$(this.workId);
if (tmpXajax == null)
{
tmpXajax = document.createElement("div");
tmpXajax.setAttribute('id',this.workId);
tmpXajax.style.display = "none";
tmpXajax.style.visibility = "hidden";
document.body.appendChild(tmpXajax);
}
tmpXajax.innerHTML = newData;
newData = tmpXajax.innerHTML;
}
eval("oldData=document.getElementById('"+element+"')."+attribute);
if (newData != oldData)
return true;
return false;
}
//Process XML xajaxResponses returned from the request
this.processResponse = function(xml)
{
bStatusMessages == true){?> window.status = 'Recieving data...';
var tmpXajax = null;
xml = xml.documentElement;
for (i=0; i -1)
{
x = v.indexOf(search)+search.length+1;
v2 += v.substr(0,x).replace(search,data);
v = v.substr(x,v.length-x);
}
if (this.willChange(element,attribute,v2))
eval('document.getElementById("'+element+'").'+attribute+'=v2;');
}
if (action=="clear")
eval("document.getElementById('"+element+"')."+attribute+"='';");
if (action=="remove")
{
objElement = this.$(element);
if (objElement.parentNode && objElement.parentNode.removeChild)
{
objElement.parentNode.removeChild(objElement);
}
}
if (action=="create")
{
var objParent = this.$(element);
objElement = document.createElement(attribute);
objElement.setAttribute('id',data);
if (type && type != '')
objElement.setAttribute('type',type);
objParent.appendChild(objElement);
if (objParent.tagName == "FORM")
{
}
}
}
}
document.body.style.cursor = 'default';
bStatusMessages == true){?> window.status = 'Done';
}
}
var xajax = new Xajax();
aFunctions as $sFunction)
$js .= $this->wrap($sFunction,$this->aFunctionRequestTypes[$sFunction]);
if ($this->bDebug == false)
$js = $this->compressJavascript($js);
print $js;
}
}// end class xajax
?>