Xml.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <?php
  2. /**
  3. * XML handling for Cake.
  4. *
  5. * The methods in these classes enable the datasources that use XML to work.
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @package Cake.Utility
  17. * @since CakePHP v .0.10.3.1400
  18. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. App::uses('HttpSocket', 'Network/Http');
  21. /**
  22. * XML handling for CakePHP.
  23. *
  24. * The methods in these classes enable the datasources that use XML to work.
  25. *
  26. * @package Cake.Utility
  27. */
  28. class Xml {
  29. /**
  30. * Initialize SimpleXMLElement or DOMDocument from a given XML string, file path, URL or array.
  31. *
  32. * ### Usage:
  33. *
  34. * Building XML from a string:
  35. *
  36. * `$xml = Xml::build('<example>text</example>');`
  37. *
  38. * Building XML from string (output DOMDocument):
  39. *
  40. * `$xml = Xml::build('<example>text</example>', array('return' => 'domdocument'));`
  41. *
  42. * Building XML from a file path:
  43. *
  44. * `$xml = Xml::build('/path/to/an/xml/file.xml');`
  45. *
  46. * Building from a remote URL:
  47. *
  48. * `$xml = Xml::build('http://example.com/example.xml');`
  49. *
  50. * Building from an array:
  51. *
  52. * {{{
  53. * $value = array(
  54. * 'tags' => array(
  55. * 'tag' => array(
  56. * array(
  57. * 'id' => '1',
  58. * 'name' => 'defect'
  59. * ),
  60. * array(
  61. * 'id' => '2',
  62. * 'name' => 'enhancement'
  63. * )
  64. * )
  65. * )
  66. * );
  67. * $xml = Xml::build($value);
  68. * }}}
  69. *
  70. * When building XML from an array ensure that there is only one top level element.
  71. *
  72. * ### Options
  73. *
  74. * - `return` Can be 'simplexml' to return object of SimpleXMLElement or 'domdocument' to return DOMDocument.
  75. * - `loadEntities` Defaults to false. Set to true to enable loading of `<!ENTITY` definitions. This
  76. * is disabled by default for security reasons.
  77. * - If using array as input, you can pass `options` from Xml::fromArray.
  78. *
  79. * @param string|array $input XML string, a path to a file, a URL or an array
  80. * @param array $options The options to use
  81. * @return SimpleXMLElement|DOMDocument SimpleXMLElement or DOMDocument
  82. * @throws XmlException
  83. */
  84. public static function build($input, $options = array()) {
  85. if (!is_array($options)) {
  86. $options = array('return' => (string)$options);
  87. }
  88. $defaults = array(
  89. 'return' => 'simplexml',
  90. 'loadEntities' => false,
  91. );
  92. $options += $defaults;
  93. if (is_array($input) || is_object($input)) {
  94. return self::fromArray((array)$input, $options);
  95. } elseif (strpos($input, '<') !== false) {
  96. return self::_loadXml($input, $options);
  97. } elseif (file_exists($input)) {
  98. return self::_loadXml(file_get_contents($input), $options);
  99. } elseif (strpos($input, 'http://') === 0 || strpos($input, 'https://') === 0) {
  100. try {
  101. $socket = new HttpSocket(array('request' => array('redirect' => 10)));
  102. $response = $socket->get($input);
  103. if (!$response->isOk()) {
  104. throw new XmlException(__d('cake_dev', 'XML cannot be read.'));
  105. }
  106. return self::_loadXml($response->body, $options);
  107. } catch (SocketException $e) {
  108. throw new XmlException(__d('cake_dev', 'XML cannot be read.'));
  109. }
  110. } elseif (!is_string($input)) {
  111. throw new XmlException(__d('cake_dev', 'Invalid input.'));
  112. }
  113. throw new XmlException(__d('cake_dev', 'XML cannot be read.'));
  114. }
  115. /**
  116. * Parse the input data and create either a SimpleXmlElement object or a DOMDocument.
  117. *
  118. * @param string $input The input to load.
  119. * @param array $options The options to use. See Xml::build()
  120. * @return SimpleXmlElement|DOMDocument
  121. * @throws XmlException
  122. */
  123. protected static function _loadXml($input, $options) {
  124. $hasDisable = function_exists('libxml_disable_entity_loader');
  125. $internalErrors = libxml_use_internal_errors(true);
  126. if ($hasDisable && !$options['loadEntities']) {
  127. libxml_disable_entity_loader(true);
  128. }
  129. try {
  130. if ($options['return'] === 'simplexml' || $options['return'] === 'simplexmlelement') {
  131. $xml = new SimpleXMLElement($input, LIBXML_NOCDATA);
  132. } else {
  133. $xml = new DOMDocument();
  134. $xml->loadXML($input);
  135. }
  136. } catch (Exception $e) {
  137. $xml = null;
  138. }
  139. if ($hasDisable && !$options['loadEntities']) {
  140. libxml_disable_entity_loader(false);
  141. }
  142. libxml_use_internal_errors($internalErrors);
  143. if ($xml === null) {
  144. throw new XmlException(__d('cake_dev', 'Xml cannot be read.'));
  145. }
  146. return $xml;
  147. }
  148. /**
  149. * Transform an array into a SimpleXMLElement
  150. *
  151. * ### Options
  152. *
  153. * - `format` If create childs ('tags') or attributes ('attribute').
  154. * - `pretty` Returns formatted Xml when set to `true`. Defaults to `false`
  155. * - `version` Version of XML document. Default is 1.0.
  156. * - `encoding` Encoding of XML document. If null remove from XML header. Default is the some of application.
  157. * - `return` If return object of SimpleXMLElement ('simplexml') or DOMDocument ('domdocument'). Default is SimpleXMLElement.
  158. *
  159. * Using the following data:
  160. *
  161. * {{{
  162. * $value = array(
  163. * 'root' => array(
  164. * 'tag' => array(
  165. * 'id' => 1,
  166. * 'value' => 'defect',
  167. * '@' => 'description'
  168. * )
  169. * )
  170. * );
  171. * }}}
  172. *
  173. * Calling `Xml::fromArray($value, 'tags');` Will generate:
  174. *
  175. * `<root><tag><id>1</id><value>defect</value>description</tag></root>`
  176. *
  177. * And calling `Xml::fromArray($value, 'attribute');` Will generate:
  178. *
  179. * `<root><tag id="1" value="defect">description</tag></root>`
  180. *
  181. * @param array $input Array with data
  182. * @param array $options The options to use
  183. * @return SimpleXMLElement|DOMDocument SimpleXMLElement or DOMDocument
  184. * @throws XmlException
  185. */
  186. public static function fromArray($input, $options = array()) {
  187. if (!is_array($input) || count($input) !== 1) {
  188. throw new XmlException(__d('cake_dev', 'Invalid input.'));
  189. }
  190. $key = key($input);
  191. if (is_int($key)) {
  192. throw new XmlException(__d('cake_dev', 'The key of input must be alphanumeric'));
  193. }
  194. if (!is_array($options)) {
  195. $options = array('format' => (string)$options);
  196. }
  197. $defaults = array(
  198. 'format' => 'tags',
  199. 'version' => '1.0',
  200. 'encoding' => Configure::read('App.encoding'),
  201. 'return' => 'simplexml',
  202. 'pretty' => false
  203. );
  204. $options += $defaults;
  205. $dom = new DOMDocument($options['version'], $options['encoding']);
  206. if ($options['pretty']) {
  207. $dom->formatOutput = true;
  208. }
  209. self::_fromArray($dom, $dom, $input, $options['format']);
  210. $options['return'] = strtolower($options['return']);
  211. if ($options['return'] === 'simplexml' || $options['return'] === 'simplexmlelement') {
  212. return new SimpleXMLElement($dom->saveXML());
  213. }
  214. return $dom;
  215. }
  216. /**
  217. * Recursive method to create childs from array
  218. *
  219. * @param DOMDocument $dom Handler to DOMDocument
  220. * @param DOMElement $node Handler to DOMElement (child)
  221. * @param array &$data Array of data to append to the $node.
  222. * @param string $format Either 'attribute' or 'tags'. This determines where nested keys go.
  223. * @return void
  224. * @throws XmlException
  225. */
  226. protected static function _fromArray($dom, $node, &$data, $format) {
  227. if (empty($data) || !is_array($data)) {
  228. return;
  229. }
  230. foreach ($data as $key => $value) {
  231. if (is_string($key)) {
  232. if (!is_array($value)) {
  233. if (is_bool($value)) {
  234. $value = (int)$value;
  235. } elseif ($value === null) {
  236. $value = '';
  237. }
  238. $isNamespace = strpos($key, 'xmlns:');
  239. if ($isNamespace !== false) {
  240. $node->setAttributeNS('http://www.w3.org/2000/xmlns/', $key, $value);
  241. continue;
  242. }
  243. if ($key[0] !== '@' && $format === 'tags') {
  244. $child = null;
  245. if (!is_numeric($value)) {
  246. // Escape special characters
  247. // http://www.w3.org/TR/REC-xml/#syntax
  248. // https://bugs.php.net/bug.php?id=36795
  249. $child = $dom->createElement($key, '');
  250. $child->appendChild(new DOMText($value));
  251. } else {
  252. $child = $dom->createElement($key, $value);
  253. }
  254. $node->appendChild($child);
  255. } else {
  256. if ($key[0] === '@') {
  257. $key = substr($key, 1);
  258. }
  259. $attribute = $dom->createAttribute($key);
  260. $attribute->appendChild($dom->createTextNode($value));
  261. $node->appendChild($attribute);
  262. }
  263. } else {
  264. if ($key[0] === '@') {
  265. throw new XmlException(__d('cake_dev', 'Invalid array'));
  266. }
  267. if (is_numeric(implode('', array_keys($value)))) { // List
  268. foreach ($value as $item) {
  269. $itemData = compact('dom', 'node', 'key', 'format');
  270. $itemData['value'] = $item;
  271. self::_createChild($itemData);
  272. }
  273. } else { // Struct
  274. self::_createChild(compact('dom', 'node', 'key', 'value', 'format'));
  275. }
  276. }
  277. } else {
  278. throw new XmlException(__d('cake_dev', 'Invalid array'));
  279. }
  280. }
  281. }
  282. /**
  283. * Helper to _fromArray(). It will create childs of arrays
  284. *
  285. * @param array $data Array with informations to create childs
  286. * @return void
  287. */
  288. protected static function _createChild($data) {
  289. extract($data);
  290. $childNS = $childValue = null;
  291. if (is_array($value)) {
  292. if (isset($value['@'])) {
  293. $childValue = (string)$value['@'];
  294. unset($value['@']);
  295. }
  296. if (isset($value['xmlns:'])) {
  297. $childNS = $value['xmlns:'];
  298. unset($value['xmlns:']);
  299. }
  300. } elseif (!empty($value) || $value === 0) {
  301. $childValue = (string)$value;
  302. }
  303. $child = $dom->createElement($key);
  304. if ($childValue !== null) {
  305. $child->appendChild($dom->createTextNode($childValue));
  306. }
  307. if ($childNS) {
  308. $child->setAttribute('xmlns', $childNS);
  309. }
  310. self::_fromArray($dom, $child, $value, $format);
  311. $node->appendChild($child);
  312. }
  313. /**
  314. * Returns this XML structure as a array.
  315. *
  316. * @param SimpleXMLElement|DOMDocument|DOMNode $obj SimpleXMLElement, DOMDocument or DOMNode instance
  317. * @return array Array representation of the XML structure.
  318. * @throws XmlException
  319. */
  320. public static function toArray($obj) {
  321. if ($obj instanceof DOMNode) {
  322. $obj = simplexml_import_dom($obj);
  323. }
  324. if (!($obj instanceof SimpleXMLElement)) {
  325. throw new XmlException(__d('cake_dev', 'The input is not instance of SimpleXMLElement, DOMDocument or DOMNode.'));
  326. }
  327. $result = array();
  328. $namespaces = array_merge(array('' => ''), $obj->getNamespaces(true));
  329. self::_toArray($obj, $result, '', array_keys($namespaces));
  330. return $result;
  331. }
  332. /**
  333. * Recursive method to toArray
  334. *
  335. * @param SimpleXMLElement $xml SimpleXMLElement object
  336. * @param array &$parentData Parent array with data
  337. * @param string $ns Namespace of current child
  338. * @param array $namespaces List of namespaces in XML
  339. * @return void
  340. */
  341. protected static function _toArray($xml, &$parentData, $ns, $namespaces) {
  342. $data = array();
  343. foreach ($namespaces as $namespace) {
  344. foreach ($xml->attributes($namespace, true) as $key => $value) {
  345. if (!empty($namespace)) {
  346. $key = $namespace . ':' . $key;
  347. }
  348. $data['@' . $key] = (string)$value;
  349. }
  350. foreach ($xml->children($namespace, true) as $child) {
  351. self::_toArray($child, $data, $namespace, $namespaces);
  352. }
  353. }
  354. $asString = trim((string)$xml);
  355. if (empty($data)) {
  356. $data = $asString;
  357. } elseif (strlen($asString) > 0) {
  358. $data['@'] = $asString;
  359. }
  360. if (!empty($ns)) {
  361. $ns .= ':';
  362. }
  363. $name = $ns . $xml->getName();
  364. if (isset($parentData[$name])) {
  365. if (!is_array($parentData[$name]) || !isset($parentData[$name][0])) {
  366. $parentData[$name] = array($parentData[$name]);
  367. }
  368. $parentData[$name][] = $data;
  369. } else {
  370. $parentData[$name] = $data;
  371. }
  372. }
  373. }