Location: PHPKode > projects > OpenNitro > trunk/Nitro/XML/XMLResponse.inc.php
<?php
//
// +---------------------------------------------------------------------------+
// | Nitro :: XML :: XMLResponse                                               |
// +---------------------------------------------------------------------------+
// | Copyright (c) 2007 June Systems BV                                        |
// +---------------------------------------------------------------------------+
// | This library is free software; you can redistribute it and/or modify it   |
// | under the terms of the GNU Lesser General Public License as published by  |
// | the Free Software Foundation; either version 2.1 of the License, or (at   |
// | your option) any later version.                                           |
// |                                                                           |
// | This library is distributed in the hope that it will be useful, but       |
// | WITHOUT ANY WARRANTY; without even the implied warranty of                |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser   |
// | General Public License for more details.                                  |
// |                                                                           |
// | You should have received a copy of the GNU Lesser General Public License  |
// | along with this library; if not, write to the Free Software Foundation,   |
// | Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA            |
// +---------------------------------------------------------------------------+
// | Authors: Siggi Oskarsson <hide@address.com>                          |
// |          Jesper Avot <hide@address.com>                        |
// +---------------------------------------------------------------------------+
//
// $Id: XMLResponse.inc.php 246 2008-09-17 11:42:15Z oli $
//
// Nitro's XML response class
//

/**
 * This file contains the XML response class
 *
 * @author			Siggi Oskarsson
 * @author      Jesper Avot
 * @copyright		June Systems BV, 2007
 * @version			$Revision: $
 * @package			Nitro
 * @subpackage	XML
 */


/**
 * This class converts an array to an XML file.
 *
 * @package			Nitro
 * @subpackage	XML
 * @staticvar		string	$Error	Possible error generated by the last called function.
 */
class XMLResponse {
	var $DataDivs = array();
	var $DataUpdates = array();
	var $FormUpdates = array();
	var $Standalone = 'yes';
	var $Encoding = 'ISO-8859-1';
	var $ShowHostname = TRUE;

	function XMLResponse($DataDivs = array())
	{
		if ($DataDivs) $this->DataDivs = $DataDivs;
	}

	function addDataDiv($DivID, $HTML, $Overwrite = FALSE)
	{
		if (strlen($DivID)) {
			if ($Overwrite) {
				$this->DataDivs[$DivID] = $HTML;
			} else {
				// Always add HTML to end of DIV, automatically created if not exists
				$this->DataDivs[$DivID].= $HTML;
			}
		}
	}

	function addDataUpdate($ID, $Value)
	{
		if (strlen($ID)) {
			$this->DataUpdates[$ID] = $Value;
		}
	}
	
	/**
	 * Add form update to the XML response
	 *
	 * @param	$ID	Id of form element to update
	 * @param	$Values	Form values to update in element ( Array($key => Array('Selected' => <boolean>, 'Value' => <string>)) )
	 * @param	$WidgetType	Type of widget to update ( e.g. SELECT or TEXT )
	 */
	function addFormUpdate($ID, $Values, $WidgetType)
	{
		if (strlen($ID)) {
			$this->FormUpdates[$ID] = array(
				'WidgetType' 	=> $WidgetType,
				'Values' 			=> $Values
			);
		}
	}

	function setEncoding($Encoding)
	{
		$this->Encoding = $Encoding;
	}

	function setStandalone($Standalone)
	{
		if ($Standalone) {
			$this->Standalone = 'yes';
		} else {
			$this->Standalone = 'no';
		}
	}

	function getXML()
	{
		$XML = '<?xml version="1.0" standalone="'.$this->Standalone.'" ?>'."\n";
		/*  encoding="'.$this->Encoding.'" */
		$XML.= "<Responses>\n";
		
		foreach($this->FormUpdates AS $id => $update) {
			$XML.= "<Response Type=\"FormValue\" TargetID=\"".$id."\" WidgetType=\"".$update['WidgetType']."\">\n";
			
			foreach($update['Values'] AS $key => $valueArr) {
				$XML.= "<Value Key=\"".$key."\" Selected=\"".($valueArr['Selected'] === TRUE ? "true" : "false")."\"";
				
				if (isset($valueArr['Value']) && strlen($valueArr['Value'])) {
					$XML.= " Contents=\"".$valueArr['Value']."\" />\n";
				} else {
					$XML.= " />\n";
				}
			}
			
			$XML.= "</Response>\n";
		}

		$DataTypes = array('DataDiv', 'DataUpdate');
		foreach ($DataTypes as $dataType) {
			eval('$dataArray = $this->'.$dataType.'s;');
			if (is_array($dataArray) && count($dataArray)) {
				foreach($dataArray AS $divID => $HTML) {
					$jScript = '';
					$jScriptFunctions = '';
					if (strlen($HTML) && (is_string($HTML) || is_numeric($HTML))) {
						$matches = array();
						preg_match_all("/<SCRIPT[^>]*>(.*)<\/SCRIPT>/sUi", $HTML, $matches); // match all SCRIPTs
						$HTML = preg_replace("/<SCRIPT[^>]*>.*<\/SCRIPT>/sUi", '', $HTML); // remove all SCRIPTs from HTML
						//$HTML = preg_replace('/[\t\n\r]+/', '', $HTML); // remove useless white spaces
						//Removing white-spaces causes problems in generated HTML code if you use only enters and tabs (see Daphne_IQ_Listing.tpl)
						if (is_array($matches[1]) && count($matches[1])) {
							foreach ($matches[1] AS $jScriptCode) {
								$jScriptCode = preg_replace('"\/\/[^\n]*\n"', '', $jScriptCode); // remove comments
								$jScriptCode = preg_replace('/[\t\n\r]+/', '', $jScriptCode); // remove useless white spaces
								$jScriptCode = preg_replace('/ +/', ' ', $jScriptCode);
								if (chop($jScriptCode)) { // not empty
									if (0) {//substr($jScriptCode, -1, 1) === '}') { // werkt nog niet goed!
										$jScriptFunctions.= $jScriptCode."\n";
									} else {
										$jScript.= $jScriptCode.(substr($jScriptCode, -1, 1) === ';' || substr($jScriptCode, -1, 1) === '}' ? '' : ';')."\n";
									}
								}
							}
						}
					} else {
						$HTML = '';
					}
					
					$XML.= "<Response Type=\"".$dataType."\" TargetID=\"$divID\" Script=\"".rawurlencode($jScript)."\" ScriptFunctions=\"".rawurlencode($jScriptFunctions)."\" Contents=\"".rawurlencode(str_replace("\0", "", $HTML))."\" />\n";
				}
			}
		}
		
		if ($this->ShowHostname) {
			$XML.= "<Hostname Value=\"".(strlen(array_key_exists('HOSTNAME', $_SERVER) && $_SERVER['HOSTNAME']) ? $_SERVER['HOSTNAME'] : 'localhost')."\" />\n";
		}
		
		$XML.= "</Responses>\n";
		return $XML;
	}
	
	function writeXML()
	{
		// Empty outputbuffer and add to $ob variable
		$ob = ob_get_contents();
		while (@ob_end_clean());
		
		//write changed session to disk
		//SaveSessionToDisk();

		if (!headers_sent()) {
			header("Content-Type: text/xml");
			header('Cache-Control: no-cache');
			header("Pragma: no-cache");
			header("Expires: -1");
		}
		
		echo $this->getXML();
		
		if (strlen($ob)) {
			echo '<!-- '.$ob.' -->';
		}
		
		exit;
	}
}
?>
Return current item: OpenNitro