1<?php
2/**
3 * modified XML Element
4 *
5 * PHP version 5
6 *
7 * @category  PHP
8 * @package   PSI_XML
9 * @author    Michael Cramer <BigMichi1@users.sourceforge.net>
10 * @copyright 2009 phpSysInfo
11 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
12 * @version   SVN: $Id: class.SimpleXMLExtended.inc.php 610 2012-07-11 19:12:12Z namiltd $
13 * @link      http://phpsysinfo.sourceforge.net
14 */
15 /**
16 * class extends the SimpleXML element for including some special functions, like encoding stuff and cdata support
17 *
18 * @category  PHP
19 * @package   PSI_XML
20 * @author    Michael Cramer <BigMichi1@users.sourceforge.net>
21 * @copyright 2009 phpSysInfo
22 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
23 * @version   Release: 3.0
24 * @link      http://phpsysinfo.sourceforge.net
25 */
26class SimpleXMLExtended
27{
28    /**
29     * store the encoding that is used for conversation to utf8
30     *
31     * @var String base encoding
32     */
33    private $_encoding = null;
34
35    /**
36     * SimpleXMLElement to which every call is delegated
37     *
38     * @var SimpleXMLElement delegated SimpleXMLElement
39     */
40    private $_SimpleXmlElement = null;
41
42    /**
43     * _CP437toUTF8Table for code page conversion for CP437
44     *
45     * @var array _CP437toUTF8Table array
46     */
47    private static $_CP437toUTF8Table = array(
48        "\xC3\x87","\xC3\xBC","\xC3\xA9","\xC3\xA2",
49        "\xC3\xA4","\xC3\xA0","\xC3\xA5","\xC3\xA7",
50        "\xC3\xAA","\xC3\xAB","\xC3\xA8","\xC3\xAF",
51        "\xC3\xAE","\xC3\xAC","\xC3\x84","\xC3\x85",
52        "\xC3\x89","\xC3\xA6","\xC3\x86","\xC3\xB4",
53        "\xC3\xB6","\xC3\xB2","\xC3\xBB","\xC3\xB9",
54        "\xC3\xBF","\xC3\x96","\xC3\x9C","\xC3\xA2",
55        "\xC2\xA3","\xC3\xA5","\xE2\x82\xA7","\xC6\x92",
56        "\xC3\xA1","\xC3\xAD","\xC3\xB3","\xC3\xBA",
57        "\xC3\xB1","\xC3\x91","\xC2\xAA","\xC2\xBA",
58        "\xC2\xBF","\xE2\x8C\x90","\xC2\xAC","\xC2\xBD",
59        "\xC2\xBC","\xC2\xA1","\xC2\xAB","\xC2\xBB",
60        "\xE2\x96\x91","\xE2\x96\x92","\xE2\x96\x93","\xE2\x94\x82",
61        "\xE2\x94\xA4","\xE2\x95\xA1","\xE2\x95\xA2","\xE2\x95\x96",
62        "\xE2\x95\x95","\xE2\x95\xA3","\xE2\x95\x91","\xE2\x95\x97",
63        "\xE2\x95\x9D","\xE2\x95\x9C","\xE2\x95\x9B","\xE2\x94\x90",
64        "\xE2\x94\x94","\xE2\x94\xB4","\xE2\x94\xAC","\xE2\x94\x9C",
65        "\xE2\x94\x80","\xE2\x94\xBC","\xE2\x95\x9E","\xE2\x95\x9F",
66        "\xE2\x95\x9A","\xE2\x95\x94","\xE2\x95\xA9","\xE2\x95\xA6",
67        "\xE2\x95\xA0","\xE2\x95\x90","\xE2\x95\xAC","\xE2\x95\xA7",
68        "\xE2\x95\xA8","\xE2\x95\xA4","\xE2\x95\xA5","\xE2\x95\x99",
69        "\xE2\x95\x98","\xE2\x95\x92","\xE2\x95\x93","\xE2\x95\xAB",
70        "\xE2\x95\xAA","\xE2\x94\x98","\xE2\x94\x8C","\xE2\x96\x88",
71        "\xE2\x96\x84","\xE2\x96\x8C","\xE2\x96\x90","\xE2\x96\x80",
72        "\xCE\xB1","\xC3\x9F","\xCE\x93","\xCF\x80",
73        "\xCE\xA3","\xCF\x83","\xC2\xB5","\xCF\x84",
74        "\xCE\xA6","\xCE\x98","\xCE\xA9","\xCE\xB4",
75        "\xE2\x88\x9E","\xCF\x86","\xCE\xB5","\xE2\x88\xA9",
76        "\xE2\x89\xA1","\xC2\xB1","\xE2\x89\xA5","\xE2\x89\xA4",
77        "\xE2\x8C\xA0","\xE2\x8C\xA1","\xC3\xB7","\xE2\x89\x88",
78        "\xC2\xB0","\xE2\x88\x99","\xC2\xB7","\xE2\x88\x9A",
79        "\xE2\x81\xBF","\xC2\xB2","\xE2\x96\xA0","\xC2\xA0");
80
81    /**
82     * create a new extended SimpleXMLElement and set encoding if specified
83     *
84     * @param SimpleXMLElement $xml      base xml element
85     * @param String           $encoding base encoding that should be used for conversation to utf8
86     *
87     * @return void
88     */
89    public function __construct($xml, $encoding = null)
90    {
91        if ($encoding != null) {
92            $this->_encoding = $encoding;
93        }
94        $this->_SimpleXmlElement = $xml;
95    }
96
97    /**
98     * insert a child element with or without a value, also doing conversation of name and if value is set to utf8
99     *
100     * @param String $name  name of the child element
101     * @param String $value a value that should be insert to the child
102     *
103     * @return SimpleXMLExtended extended child SimpleXMLElement
104     */
105    public function addChild($name, $value = null)
106    {
107        $nameUtf8 = $this->_toUTF8($name);
108        if ($value == null) {
109            return new SimpleXMLExtended($this->_SimpleXmlElement->addChild($nameUtf8), $this->_encoding);
110        } else {
111            $valueUtf8 = htmlspecialchars($this->_toUTF8($value), ENT_COMPAT, "UTF-8");
112
113            return new SimpleXMLExtended($this->_SimpleXmlElement->addChild($nameUtf8, $valueUtf8), $this->_encoding);
114        }
115    }
116
117    /**
118     * insert a child with cdata section
119     *
120     * @param String $name  name of the child element
121     * @param String $cdata data for CDATA section
122     *
123     * @return SimpleXMLExtended extended child SimpleXMLElement
124     */
125    public function addCData($name, $cdata)
126    {
127        $nameUtf8 = $this->_toUTF8($name);
128        $node = $this->_SimpleXmlElement->addChild($nameUtf8);
129        $domnode = dom_import_simplexml($node);
130        $no = $domnode->ownerDocument;
131        $domnode->appendChild($no->createCDATASection($cdata));
132
133        return new SimpleXMLExtended($node, $this->_encoding);
134    }
135
136    /**
137     * add a attribute to a child and convert name and value to utf8
138     *
139     * @param String $name  name of the attribute
140     * @param String $value value of the attribute
141     *
142     * @return Void
143     */
144    public function addAttribute($name, $value)
145    {
146        $nameUtf8 = $this->_toUTF8($name);
147        $valueUtf8 = htmlspecialchars($this->_toUTF8($value), ENT_COMPAT, "UTF-8");
148        if (($valueUtf8 === "") && (version_compare("5.2.2", PHP_VERSION, ">"))) {
149            $this->_SimpleXmlElement->addAttribute($nameUtf8, "\0"); // Fixing bug #41175 (addAttribute() fails to add an attribute with an empty value)
150        } else {
151            $this->_SimpleXmlElement->addAttribute($nameUtf8, $valueUtf8);
152        }
153    }
154
155    /**
156     * append a xml-tree to another xml-tree
157     *
158     * @param SimpleXMLElement $new_child child that should be appended
159     *
160     * @return Void
161     */
162    public function combinexml(SimpleXMLElement $new_child)
163    {
164        $node1 = dom_import_simplexml($this->_SimpleXmlElement);
165        $dom_sxe = dom_import_simplexml($new_child);
166        $node2 = $node1->ownerDocument->importNode($dom_sxe, true);
167        $node1->appendChild($node2);
168    }
169
170    /**
171     * convert a string into an UTF-8 string
172     *
173     * @param String $str string to convert
174     *
175     * @return String UTF-8 string
176     */
177    private function _toUTF8($str)
178    {
179        $str = trim(preg_replace('/[\x00-\x09\x0b-\x1F]/', ' ', $str)); //remove nonprintable characters
180        if ($this->_encoding != null) {
181            if (strcasecmp($this->_encoding, "UTF-8") == 0) {
182                return $str;
183            } elseif (strcasecmp($this->_encoding, "CP437") == 0) {
184                $strr = "";
185                if (($strl = strlen($str)) > 0) for ($i = 0; $i < $strl; $i++) {
186                    $strc = substr($str, $i, 1);
187                    if ($strc < 128) $strr.=$strc;
188                        else $strr.=self::$_CP437toUTF8Table[$strc-128];
189                }
190
191                 return $strr;
192            } else {
193                if (preg_match("/^windows-\d+ \((.+)\)$/", $this->_encoding, $buf)) {
194                    $encoding = $buf[1];
195                } else {
196                    $encoding = $this->_encoding;
197                }
198                $enclist = mb_list_encodings();
199                if (in_array($encoding, $enclist)) {
200                    return mb_convert_encoding($str, 'UTF-8', $encoding);
201                } elseif (function_exists("iconv")) {
202                    if (($iconvout=iconv($encoding, 'UTF-8', $str))!==false) {
203                        return $iconvout;
204                    } else {
205                        return mb_convert_encoding($str, 'UTF-8');
206                    }
207                } elseif (function_exists("libiconv") && (($iconvout=libiconv($encoding, 'UTF-8', $str))!==false)) {
208                    return $iconvout;
209                } else {
210                    return mb_convert_encoding($str, 'UTF-8');
211                }
212           }
213        } else {
214            return mb_convert_encoding($str, 'UTF-8');
215        }
216    }
217
218    /**
219     * Returns the SimpleXmlElement
220     *
221     * @return SimpleXmlElement entire xml as SimpleXmlElement
222     */
223    public function getSimpleXmlElement()
224    {
225        return $this->_SimpleXmlElement;
226    }
227}
228