[Php-it] mime in upload

Emiliano Gabrielli (aka AlberT) AlberT at superalbert.it
Tue Jan 2 11:48:59 CET 2007


On Tuesday 02 January 2007 11:46, Giovanni Battista Lenoci wrote:
> secondo voi sbaglio?

no :-)

-- 
<?php echo '     Emiliano Gabrielli (aka AlberT)     ',"\n",
'              socio fondatore del GrUSP             ',"\n",
' AlberT_at_SuperAlberT_it   -   www.SuperAlberT.it  ',"\n",
'  IRC:    #php,#AES azzurra.com ',"\n",'ICQ: 158591185'; ?>
-------------- parte successiva --------------
<?php

class Template
{
	/**
	 * The charset of the internal
	 * generated xml-tree
	 */
	const ROOT_CHARSET = 'utf-8';

	/**
	 * The version of the internal
	 * generated xml-tree
	 */
	const ROOT_VERSION = '1.0';

	const RESOURCE_FILE 	= 0;
	const RESOURCE_DOCUMENT = 1;
	const RESOURCE_STRING 	= 2;

	/**
	 * The <i>XSLTProcessor</i>.
	 *
	 * @access protected
	 * @var XSLTProcessor $_xslt
	 */
	protected $_xslt;

	/**
	 * Where assigned XSLT parameters are kept.
	 *
	 * @access protected
	 * @var array $_xsltParams
	 */
	protected $_xsltParams = array();

	/**
	 * Where assigned XSLT values are kept.
	 *
	 * @access protected
	 * @var array $_xsltValues
	 */
	protected $_xsltValues = array();

	/**
	 * Used to save the xml-tree while processing
	 *
	 * @access protected
	 * @var DOMDocument $_xmlTree
	 */
	protected $_xmlTree;


	protected $_extXML = array();

	/**
	 * Constructs a new <i>View_XSLTemplate</i> object and sets
	 * the internal properties with application settings.
	 *
	 * @access public
	 */
	public function __construct()
	{
		$this->_xslt = new XSLTProcessor();

		$this->_xmlTree =	new DOMDocument(
								self::ROOT_VERSION,
								self::ROOT_CHARSET
							);
	}

	/**
	 * Assigns values to the $this->_xsltValues array.
	 *
	 * @access public
	 * @param string $name The xml node name
	 * @param mixed $value The value to assign
	 */
	public function assign($name, $value)
	{
		$this->_xsltValues[$name] = $value;
	}

	public function __set($name, $value)
	{
		$this->_xsltValues[$name] = $value;
	}

	/**
	 * Assigns values to xslt template parameters.
	 *
	 * @access public
	 * @param string $param The xslt template parameter name
	 * @param mixed $value The value to assign
	 */
	public function setParameter($param, $value)
	{
		$this->_xsltParams[$param] = $value;
	}

	public function setXML($content, $resource)
	{
		$xml = new DOMDocument();

		switch ($resource) {
			case self::RESOURCE_STRING:
				$xml->loadXML($content);
				break;
			case self::RESOURCE_FILE:
				$xml = simplexml_load_file($content);
				break;
			case sefl::RESOURCE_DOCUMENT:
				$xml = $content;
				break;
		}

		$this->_extXML[] = $xml;
	}

	/**
	 * Executes and returns the xslt template results.
	 *
	 * @access public
	 * @param string $template File name of the xslt template
	 * @return string
	 */
	public function render($template = null)
	{
		if (count($this->_extXML)) {
			foreach ($this->_extXML as $extXML) {
				$domnode = dom_import_simplexml($extXML);
				$domnode = $this->_xmlTree->importNode($domnode, true);
				$this->_xmlTree->appendChild($domnode);
			}
		}

		foreach ($this->_xsltValues as $name => $value) {
			$this->_appendElements($name, $value, $this->_xmlTree);
		}

		foreach ($this->_xsltParams as $name => $value) {
			$this->_xslt->setParameter('', $name, $value);
		}

		if (is_null($template)) {
			$this->_xmlTree->formatOutput = true;
			return $this->_xmlTree->saveXML();
		}

		$xsl = new DOMDocument();
		$xsl->load($template);

		$this->_xslt->importStyleSheet($xsl);

		return $this->_xslt->transformToXML($this->_xmlTree);
	}

	/**
	 * Executes and displays the xslt template results.
	 *
	 * @access public
	 * @param string $template File name of the xslt template
	 * @param string $xml The XML document
	 */
	public function display($template, $xml = null)
	{
		echo $this->render($template, $xml);
	}

	public function escape($string)
	{
		return htmlspecialchars($string, ENT_QUOTES, self::ROOT_CHARSET);
	}

	private function _appendElements($name, $value, &$element)
	{
		if (is_array($value)) {
			if ($this->_onlyNumericKeys($value)) {
				foreach ($value as $temp_val) {
					$this->_appendElements($name, $temp_val, $element);
				}
			} else {
				if (is_numeric($name)) {
					$name = 'element_' . $name;
				}

				$temp_element = $this->_xmlTree->createElement($name);

				foreach ($value as $temp_name => $temp_val) {
					$this->_appendElements($temp_name, $temp_val, $temp_element);
				}

				$element->appendChild($temp_element);
			}
		} else {
			if (is_bool($value)) {
				$value = (int) $value;
			}

			$child =
				$this->_xmlTree->createElement($name,
				                               $this->escape((string) $value));

			$element->appendChild($child);
		}
	}

	private function _onlyNumericKeys($array)
	{
		$keys = array_keys($array);

		foreach ($keys as $key) {
			if (!is_numeric($key)) {
				return false;
			}
		}

		return true;
	}
}

class Template_Exception extends Exception {}



More information about the Php-it mailing list