123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587 |
- <?php
- class xajaxResponse
- {
-
-
- var $xml;
-
- var $sEncoding;
-
- var $bOutputEntities;
-
-
- function xajaxResponse($sEncoding=XAJAX_DEFAULT_CHAR_ENCODING, $bOutputEntities=false)
- {
- $this->setCharEncoding($sEncoding);
- $this->bOutputEntities = $bOutputEntities;
- }
-
- function setCharEncoding($sEncoding)
- {
- $this->sEncoding = $sEncoding;
- }
-
- function outputEntitiesOn()
- {
- $this->bOutputEntities = true;
- }
-
- function outputEntitiesOff()
- {
- $this->bOutputEntities = false;
- }
-
- function addConfirmCommands($iCmdNumber, $sMessage)
- {
- $this->xml .= $this->_cmdXML(array("n"=>"cc","t"=>$iCmdNumber),$sMessage);
- }
-
- function addAssign($sTarget,$sAttribute,$sData)
- {
- $this->xml .= $this->_cmdXML(array("n"=>"as","t"=>$sTarget,"p"=>$sAttribute),$sData);
- }
-
- function addAppend($sTarget,$sAttribute,$sData)
- {
- $this->xml .= $this->_cmdXML(array("n"=>"ap","t"=>$sTarget,"p"=>$sAttribute),$sData);
- }
-
- function addPrepend($sTarget,$sAttribute,$sData)
- {
- $this->xml .= $this->_cmdXML(array("n"=>"pp","t"=>$sTarget,"p"=>$sAttribute),$sData);
- }
-
- function addReplace($sTarget,$sAttribute,$sSearch,$sData)
- {
- $sDta = "<s><![CDATA[$sSearch]]></s><r><![CDATA[$sData]]></r>";
- $this->xml .= $this->_cmdXML(array("n"=>"rp","t"=>$sTarget,"p"=>$sAttribute),$sDta);
- }
-
- function addClear($sTarget,$sAttribute)
- {
- $this->addAssign($sTarget,$sAttribute,'');
- }
-
- function addAlert($sMsg)
- {
- $this->xml .= $this->_cmdXML(array("n"=>"al"),$sMsg);
- }
-
- function addRedirect($sURL)
- {
-
-
-
- $queryStart = strpos($sURL, '?', strrpos($sURL, '/'));
- if ($queryStart !== FALSE)
- {
- $queryStart++;
- $queryEnd = strpos($sURL, '#', $queryStart);
- if ($queryEnd === FALSE)
- $queryEnd = strlen($sURL);
- $queryPart = substr($sURL, $queryStart, $queryEnd-$queryStart);
- $queryParts = array();
- parse_str($queryPart, $queryParts);
- $newQueryPart = "";
- foreach($queryParts as $key => $value)
- {
- $newQueryPart .= rawurlencode($key).'='.rawurlencode($value).ini_get('arg_separator.output');
- }
- $sURL = str_replace($queryPart, $newQueryPart, $sURL);
- }
- $this->addScript('window.location = "'.$sURL.'";');
- }
-
- function addScript($sJS)
- {
- $this->xml .= $this->_cmdXML(array("n"=>"js"),$sJS);
- }
-
- function addScriptCall() {
- $arguments = func_get_args();
- $sFunc = array_shift($arguments);
- $sData = $this->_buildObjXml($arguments);
- $this->xml .= $this->_cmdXML(array("n"=>"jc","t"=>$sFunc),$sData);
- }
-
- function addRemove($sTarget)
- {
- $this->xml .= $this->_cmdXML(array("n"=>"rm","t"=>$sTarget),'');
- }
-
- function addCreate($sParent, $sTag, $sId, $sType="")
- {
- if ($sType)
- {
- trigger_error("The \$sType parameter of addCreate has been deprecated. Use the addCreateInput() method instead.", E_USER_WARNING);
- return;
- }
- $this->xml .= $this->_cmdXML(array("n"=>"ce","t"=>$sParent,"p"=>$sId),$sTag);
- }
-
- function addInsert($sBefore, $sTag, $sId)
- {
- $this->xml .= $this->_cmdXML(array("n"=>"ie","t"=>$sBefore,"p"=>$sId),$sTag);
- }
-
- function addInsertAfter($sAfter, $sTag, $sId)
- {
- $this->xml .= $this->_cmdXML(array("n"=>"ia","t"=>$sAfter,"p"=>$sId),$sTag);
- }
-
- function addCreateInput($sParent, $sType, $sName, $sId)
- {
- $this->xml .= $this->_cmdXML(array("n"=>"ci","t"=>$sParent,"p"=>$sId,"c"=>$sType),$sName);
- }
-
- function addInsertInput($sBefore, $sType, $sName, $sId)
- {
- $this->xml .= $this->_cmdXML(array("n"=>"ii","t"=>$sBefore,"p"=>$sId,"c"=>$sType),$sName);
- }
-
- function addInsertInputAfter($sAfter, $sType, $sName, $sId)
- {
- $this->xml .= $this->_cmdXML(array("n"=>"iia","t"=>$sAfter,"p"=>$sId,"c"=>$sType),$sName);
- }
-
- function addEvent($sTarget,$sEvent,$sScript)
- {
- $this->xml .= $this->_cmdXML(array("n"=>"ev","t"=>$sTarget,"p"=>$sEvent),$sScript);
- }
-
- function addHandler($sTarget,$sEvent,$sHandler)
- {
- $this->xml .= $this->_cmdXML(array("n"=>"ah","t"=>$sTarget,"p"=>$sEvent),$sHandler);
- }
-
- function addRemoveHandler($sTarget,$sEvent,$sHandler)
- {
- $this->xml .= $this->_cmdXML(array("n"=>"rh","t"=>$sTarget,"p"=>$sEvent),$sHandler);
- }
-
- function addIncludeScript($sFileName)
- {
- $this->xml .= $this->_cmdXML(array("n"=>"in"),$sFileName);
- }
-
- function getXML()
- {
- $sXML = "<?xml version=\"1.0\"";
- if ($this->sEncoding && strlen(trim($this->sEncoding)) > 0)
- $sXML .= " encoding=\"".$this->sEncoding."\"";
- $sXML .= " ?"."><xjx>" . $this->xml . "</xjx>";
- return $sXML;
- }
-
- function loadXML($mXML)
- {
- if (is_a($mXML, "xajaxResponse")) {
- $mXML = $mXML->getXML();
- }
- $sNewXML = "";
- $iStartPos = strpos($mXML, "<xjx>") + 5;
- $sNewXML = substr($mXML, $iStartPos);
- $iEndPos = strpos($sNewXML, "</xjx>");
- $sNewXML = substr($sNewXML, 0, $iEndPos);
- $this->xml .= $sNewXML;
- }
-
- function _cmdXML($aAttributes, $sData)
- {
- if ($this->bOutputEntities) {
-
- if (function_exists('api_convert_encoding')) {
- $sData = call_user_func_array('api_convert_encoding', array(&$sData, 'HTML-ENTITIES', $this->sEncoding));
- }
-
- elseif (function_exists('mb_convert_encoding')) {
-
- $sData = call_user_func_array('mb_convert_encoding', array(&$sData, 'HTML-ENTITIES', $this->sEncoding));
- }
- else {
- trigger_error("The xajax XML response output could not be converted to HTML entities because the mb_convert_encoding function is not available", E_USER_NOTICE);
- }
- }
- $xml = "<cmd";
- foreach($aAttributes as $sAttribute => $sValue)
- $xml .= " $sAttribute=\"$sValue\"";
- if ($sData !== null && !stristr($sData,'<![CDATA['))
- $xml .= "><![CDATA[$sData]]></cmd>";
- else if ($sData !== null)
- $xml .= ">$sData</cmd>";
- else
- $xml .= "></cmd>";
- return $xml;
- }
-
- function _buildObjXml($var) {
- if (gettype($var) == "object") $var = get_object_vars($var);
- if (!is_array($var)) {
- return "<![CDATA[$var]]>";
- }
- else {
- $data = "<xjxobj>";
- foreach ($var as $key => $value) {
- $data .= "<e>";
- $data .= "<k>" . htmlspecialchars($key) . "</k>";
- $data .= "<v>" . $this->_buildObjXml($value) . "</v>";
- $data .= "</e>";
- }
- $data .= "</xjxobj>";
- return $data;
- }
- }
- }
- ?>
|