File indexing completed on 2024-05-26 06:03:11

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Log
0017  * @subpackage Formatter
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /** Zend_Log_Formatter_Abstract */
0024 // require_once 'Zend/Log/Formatter/Abstract.php';
0025 
0026 /**
0027  * @category   Zend
0028  * @package    Zend_Log
0029  * @subpackage Formatter
0030  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0031  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0032  * @version    $Id$
0033  */
0034 class Zend_Log_Formatter_Xml extends Zend_Log_Formatter_Abstract
0035 {
0036     /**
0037      * @var string Name of root element
0038      */
0039     protected $_rootElement;
0040 
0041     /**
0042      * @var array Relates XML elements to log data field keys.
0043      */
0044     protected $_elementMap;
0045 
0046     /**
0047      * @var string Encoding to use in XML
0048      */
0049     protected $_encoding;
0050 
0051     /**
0052      * Class constructor
0053      * (the default encoding is UTF-8)
0054      *
0055      * @param array|Zend_Config $options
0056      * @return void
0057      */
0058     public function __construct($options = array())
0059     {
0060         if ($options instanceof Zend_Config) {
0061             $options = $options->toArray();
0062         } elseif (!is_array($options)) {
0063             $args = func_get_args();
0064 
0065             $options = array(
0066               'rootElement' => array_shift($args)
0067             );
0068 
0069             if (count($args)) {
0070                 $options['elementMap'] = array_shift($args);
0071             }
0072 
0073             if (count($args)) {
0074                 $options['encoding'] = array_shift($args);
0075             }
0076         }
0077 
0078         if (!array_key_exists('rootElement', $options)) {
0079             $options['rootElement'] = 'logEntry';
0080         }
0081 
0082         if (!array_key_exists('encoding', $options)) {
0083             $options['encoding'] = 'UTF-8';
0084         }
0085 
0086         $this->_rootElement = $options['rootElement'];
0087         $this->setEncoding($options['encoding']);
0088 
0089         if (array_key_exists('elementMap', $options)) {
0090             $this->_elementMap  = $options['elementMap'];
0091         }
0092     }
0093 
0094     /**
0095    * Factory for Zend_Log_Formatter_Xml classe
0096    *
0097    * @param array|Zend_Config $options
0098    * @return Zend_Log_Formatter_Xml
0099      */
0100     public static function factory($options)
0101     {
0102         return new self($options);
0103     }
0104 
0105     /**
0106      * Get encoding
0107      *
0108      * @return string
0109      */
0110     public function getEncoding()
0111     {
0112         return $this->_encoding;
0113     }
0114 
0115     /**
0116      * Set encoding
0117      *
0118      * @param  string $value
0119      * @return Zend_Log_Formatter_Xml
0120      */
0121     public function setEncoding($value)
0122     {
0123         $this->_encoding = (string) $value;
0124         return $this;
0125     }
0126 
0127     /**
0128      * Formats data into a single line to be written by the writer.
0129      *
0130      * @param  array    $event    event data
0131      * @return string             formatted line to write to the log
0132      */
0133     public function format($event)
0134     {
0135         if ($this->_elementMap === null) {
0136             $dataToInsert = $event;
0137         } else {
0138             $dataToInsert = array();
0139             foreach ($this->_elementMap as $elementName => $fieldKey) {
0140                 $dataToInsert[$elementName] = $event[$fieldKey];
0141             }
0142         }
0143 
0144         $enc = $this->getEncoding();
0145         $dom = new DOMDocument('1.0', $enc);
0146         $elt = $dom->appendChild(new DOMElement($this->_rootElement));
0147 
0148         foreach ($dataToInsert as $key => $value) {
0149             if (empty($value) 
0150                 || is_scalar($value) 
0151                 || (is_object($value) && method_exists($value,'__toString'))
0152             ) {
0153                 if($key == "message") {
0154                     $value = htmlspecialchars($value, ENT_COMPAT, $enc);
0155                 }
0156                 $elt->appendChild(new DOMElement($key, (string)$value));
0157             }
0158         }
0159 
0160         $xml = $dom->saveXML();
0161         $xml = preg_replace('/<\?xml version="1.0"( encoding="[^\"]*")?\?>\n/u', '', $xml);
0162 
0163         return $xml . PHP_EOL;
0164     }
0165 }