Location: PHPKode > projects > Aukyla Platform > aukyla/base/Output.php
<?php
/*
     Output.php, output buffer with support for applying server-side XSLT
                 transformations.
     Copyright (C) 2004 Arend van Beelen, Auton Rijnsburg

     This program is free software; you can redistribute it and/or modify it
     under the terms of the GNU General Public License as published by the Free
     Software Foundation; either version 2 of the License, or (at your option)
     any later version.

     This program 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 General Public License for
     more details.

     You should have received a copy of the GNU General Public License along
     with this program; if not, write to the Free Software Foundation, Inc.,
     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

     For any questions, comments or whatever, you may mail me at: hide@address.com
*/

require_once('Config.php');

/**
 * @brief Provides output buffering and XSLT transformations.
 *
 * The output class provides central output handling for all Widget classes.
 * All output generated by the widgets is stored in a buffer which is
 * automatically converted through an XSLT stylesheet to generate the final
 * content which is then send to the client. The actual stylesheet conversion
 * can also be done by the browser to unload the server.
 *
 * The XML doctype declaration is also initialized by this class, so you should
 * not write these to the buffer yourself.
 */
class Output
{
	/**
	 * Constructor.
	 *
	 * You should not have to instantiate this class yourself, a global instance
	 * is created for you automatically.
	 */
	public function __construct()
	{
		$this->buffer = '';
		$this->transformation = true;
		$this->xmlVersion = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
		$this->method = Config::globals('output', 'XHTML');
		$this->serverSideXSLT = Config::globals('serverSideXSLT', 'true');
	}

	/**
	 * Destructor.
	 *
	 * Does the final XSLT transformation and flushes the output buffer to the
	 * client when the script terminates.
	 */
	public function __destruct()
	{
		if($this->transformation == false ||
		   $this->buffer == '')
		{
			return;
		}

		if($this->serverSideXSLT == 'true')
		{
			$inputDoc = new DOMDocument();
			$inputDoc->loadXML($this->xmlVersion.
			                   $this->buffer);

			$xsltDoc = new DOMDocument();
			if($xsltDoc->load(AUKYLA_DIR."/htdocs/resources/base/Output/{$this->method}.xsl") == false)
			{
				die('Could not open Output XSLT transformation!');
			}

			$proc = new XSLTProcessor();
			$proc->importStyleSheet($xsltDoc);

			$doctype = @file_get_contents(AUKYLA_DIR."/htdocs/resources/base/Output/{$this->method}.doctype");
			if($doctype !== false)
			{
				echo $doctype;
			}
			echo $proc->transformToXML($inputDoc);
		}
		else
		{
			echo $this->xmlVersion.
			     "<?xml-stylesheet type=\"text/xsl\" href=\"resources/base/Output/{$this->method}.xsl\"?>\n".
			     $this->buffer;
		}
	}

	/**
	 * Disable output transformation. This will discard all data which is being
	 * written to the output buffer. This can be useful if you access a script
	 * through an XML HTTP Request, for example.
	 *
	 * @note Once disabled, output cannot be re-enabled.
	 */
	public static function disableTransformation()
	{
		global $Output;

		$Output->transformation = false;
	}

	/**
	 * Sets the output method to use. This determines which XSLT transformation is
	 * performed before the output is send to the client.
	 *
	 * @warning You should set the preferred output method as soon as you can.
	 *          Some code may behave differently depending on the selected output
	 *          method, therefore changing the method later on could give
	 *          unexpected results.
	 *
	 * @param outputMethod The new output method to use.
	 *
	 * @sa method()
	 */
	public static function setMethod($outputMethod)
	{
		global $Output;

		$Output->method = $outputMethod;
	}

	/**
	 * Returns the output method which is being used.
	 *
	 * @return The currently selected output method.
	 *
	 * @sa setMethod()
	 */
	public static function method()
	{
		global $Output;

		return $Output->method;
	}

	/**
	 * @deprecated Use setMethod() instead.
	 */
	public static function setOutputMethod($outputMethod)
	{
		self::setMethod($outputMethod);
	}

	/**
	 * @deprecated Use method() instead.
	 */
	public static function outputMethod()
	{
		return self::method();
	}

	/**
	 * Writes a string to the output buffer.
	 *
	 * @param string The string to write to the output buffer.
	 */
	public static function write($string)
	{
		global $Output;

		$Output->buffer .= $string;
	}

	private $buffer;
	private $doctype;
	private $method;
	private $serverSideXSLT;
	private $transformation;
}

// create one global instance of the class
global $Output;
$Output = new Output();

?>
Return current item: Aukyla Platform