Drupal investigation

XmlEncoder.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Serializer\Encoder;
  11. use Symfony\Component\Serializer\Exception\UnexpectedValueException;
  12. /**
  13. * Encodes XML data.
  14. *
  15. * @author Jordi Boggiano <j.boggiano@seld.be>
  16. * @author John Wards <jwards@whiteoctober.co.uk>
  17. * @author Fabian Vogler <fabian@equivalence.ch>
  18. * @author Kévin Dunglas <dunglas@gmail.com>
  19. */
  20. class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, DecoderInterface, NormalizationAwareInterface
  21. {
  22. /**
  23. * @var \DOMDocument
  24. */
  25. private $dom;
  26. private $format;
  27. private $context;
  28. private $rootNodeName = 'response';
  29. /**
  30. * Construct new XmlEncoder and allow to change the root node element name.
  31. *
  32. * @param string $rootNodeName
  33. */
  34. public function __construct($rootNodeName = 'response')
  35. {
  36. $this->rootNodeName = $rootNodeName;
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function encode($data, $format, array $context = array())
  42. {
  43. if ($data instanceof \DOMDocument) {
  44. return $data->saveXML();
  45. }
  46. $xmlRootNodeName = $this->resolveXmlRootName($context);
  47. $this->dom = $this->createDomDocument($context);
  48. $this->format = $format;
  49. $this->context = $context;
  50. if (null !== $data && !is_scalar($data)) {
  51. $root = $this->dom->createElement($xmlRootNodeName);
  52. $this->dom->appendChild($root);
  53. $this->buildXml($root, $data, $xmlRootNodeName);
  54. } else {
  55. $this->appendNode($this->dom, $data, $xmlRootNodeName);
  56. }
  57. return $this->dom->saveXML();
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function decode($data, $format, array $context = array())
  63. {
  64. if ('' === trim($data)) {
  65. throw new UnexpectedValueException('Invalid XML data, it can not be empty.');
  66. }
  67. $internalErrors = libxml_use_internal_errors(true);
  68. $disableEntities = libxml_disable_entity_loader(true);
  69. libxml_clear_errors();
  70. $dom = new \DOMDocument();
  71. $dom->loadXML($data, LIBXML_NONET | LIBXML_NOBLANKS);
  72. libxml_use_internal_errors($internalErrors);
  73. libxml_disable_entity_loader($disableEntities);
  74. if ($error = libxml_get_last_error()) {
  75. libxml_clear_errors();
  76. throw new UnexpectedValueException($error->message);
  77. }
  78. foreach ($dom->childNodes as $child) {
  79. if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
  80. throw new UnexpectedValueException('Document types are not allowed.');
  81. }
  82. }
  83. $rootNode = $dom->firstChild;
  84. // todo: throw an exception if the root node name is not correctly configured (bc)
  85. if ($rootNode->hasChildNodes()) {
  86. $xpath = new \DOMXPath($dom);
  87. $data = array();
  88. foreach ($xpath->query('namespace::*', $dom->documentElement) as $nsNode) {
  89. $data['@'.$nsNode->nodeName] = $nsNode->nodeValue;
  90. }
  91. unset($data['@xmlns:xml']);
  92. if (empty($data)) {
  93. return $this->parseXml($rootNode);
  94. }
  95. return array_merge($data, (array) $this->parseXml($rootNode));
  96. }
  97. if (!$rootNode->hasAttributes()) {
  98. return $rootNode->nodeValue;
  99. }
  100. $data = array();
  101. foreach ($rootNode->attributes as $attrKey => $attr) {
  102. $data['@'.$attrKey] = $attr->nodeValue;
  103. }
  104. $data['#'] = $rootNode->nodeValue;
  105. return $data;
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function supportsEncoding($format)
  111. {
  112. return 'xml' === $format;
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function supportsDecoding($format)
  118. {
  119. return 'xml' === $format;
  120. }
  121. /**
  122. * Sets the root node name.
  123. *
  124. * @param string $name root node name
  125. */
  126. public function setRootNodeName($name)
  127. {
  128. $this->rootNodeName = $name;
  129. }
  130. /**
  131. * Returns the root node name.
  132. *
  133. * @return string
  134. */
  135. public function getRootNodeName()
  136. {
  137. return $this->rootNodeName;
  138. }
  139. /**
  140. * @param \DOMNode $node
  141. * @param string $val
  142. *
  143. * @return bool
  144. */
  145. final protected function appendXMLString(\DOMNode $node, $val)
  146. {
  147. if (strlen($val) > 0) {
  148. $frag = $this->dom->createDocumentFragment();
  149. $frag->appendXML($val);
  150. $node->appendChild($frag);
  151. return true;
  152. }
  153. return false;
  154. }
  155. /**
  156. * @param \DOMNode $node
  157. * @param string $val
  158. *
  159. * @return bool
  160. */
  161. final protected function appendText(\DOMNode $node, $val)
  162. {
  163. $nodeText = $this->dom->createTextNode($val);
  164. $node->appendChild($nodeText);
  165. return true;
  166. }
  167. /**
  168. * @param \DOMNode $node
  169. * @param string $val
  170. *
  171. * @return bool
  172. */
  173. final protected function appendCData(\DOMNode $node, $val)
  174. {
  175. $nodeText = $this->dom->createCDATASection($val);
  176. $node->appendChild($nodeText);
  177. return true;
  178. }
  179. /**
  180. * @param \DOMNode $node
  181. * @param \DOMDocumentFragment $fragment
  182. *
  183. * @return bool
  184. */
  185. final protected function appendDocumentFragment(\DOMNode $node, $fragment)
  186. {
  187. if ($fragment instanceof \DOMDocumentFragment) {
  188. $node->appendChild($fragment);
  189. return true;
  190. }
  191. return false;
  192. }
  193. /**
  194. * Checks the name is a valid xml element name.
  195. *
  196. * @param string $name
  197. *
  198. * @return bool
  199. */
  200. final protected function isElementNameValid($name)
  201. {
  202. return $name &&
  203. false === strpos($name, ' ') &&
  204. preg_match('#^[\pL_][\pL0-9._:-]*$#ui', $name);
  205. }
  206. /**
  207. * Parse the input DOMNode into an array or a string.
  208. *
  209. * @param \DOMNode $node xml to parse
  210. *
  211. * @return array|string
  212. */
  213. private function parseXml(\DOMNode $node)
  214. {
  215. $data = $this->parseXmlAttributes($node);
  216. $value = $this->parseXmlValue($node);
  217. if (!count($data)) {
  218. return $value;
  219. }
  220. if (!is_array($value)) {
  221. $data['#'] = $value;
  222. return $data;
  223. }
  224. if (1 === count($value) && key($value)) {
  225. $data[key($value)] = current($value);
  226. return $data;
  227. }
  228. foreach ($value as $key => $val) {
  229. $data[$key] = $val;
  230. }
  231. return $data;
  232. }
  233. /**
  234. * Parse the input DOMNode attributes into an array.
  235. *
  236. * @param \DOMNode $node xml to parse
  237. *
  238. * @return array
  239. */
  240. private function parseXmlAttributes(\DOMNode $node)
  241. {
  242. if (!$node->hasAttributes()) {
  243. return array();
  244. }
  245. $data = array();
  246. foreach ($node->attributes as $attr) {
  247. if (ctype_digit($attr->nodeValue)) {
  248. $data['@'.$attr->nodeName] = (int) $attr->nodeValue;
  249. } else {
  250. $data['@'.$attr->nodeName] = $attr->nodeValue;
  251. }
  252. }
  253. return $data;
  254. }
  255. /**
  256. * Parse the input DOMNode value (content and children) into an array or a string.
  257. *
  258. * @param \DOMNode $node xml to parse
  259. *
  260. * @return array|string
  261. */
  262. private function parseXmlValue(\DOMNode $node)
  263. {
  264. if (!$node->hasChildNodes()) {
  265. return $node->nodeValue;
  266. }
  267. if (1 === $node->childNodes->length && in_array($node->firstChild->nodeType, array(XML_TEXT_NODE, XML_CDATA_SECTION_NODE))) {
  268. return $node->firstChild->nodeValue;
  269. }
  270. $value = array();
  271. foreach ($node->childNodes as $subnode) {
  272. $val = $this->parseXml($subnode);
  273. if ('item' === $subnode->nodeName && isset($val['@key'])) {
  274. if (isset($val['#'])) {
  275. $value[$val['@key']] = $val['#'];
  276. } else {
  277. $value[$val['@key']] = $val;
  278. }
  279. } else {
  280. $value[$subnode->nodeName][] = $val;
  281. }
  282. }
  283. foreach ($value as $key => $val) {
  284. if (is_array($val) && 1 === count($val)) {
  285. $value[$key] = current($val);
  286. }
  287. }
  288. return $value;
  289. }
  290. /**
  291. * Parse the data and convert it to DOMElements.
  292. *
  293. * @param \DOMNode $parentNode
  294. * @param array|object $data
  295. * @param string|null $xmlRootNodeName
  296. *
  297. * @return bool
  298. *
  299. * @throws UnexpectedValueException
  300. */
  301. private function buildXml(\DOMNode $parentNode, $data, $xmlRootNodeName = null)
  302. {
  303. $append = true;
  304. if (is_array($data) || ($data instanceof \Traversable && !$this->serializer->supportsNormalization($data, $this->format))) {
  305. foreach ($data as $key => $data) {
  306. //Ah this is the magic @ attribute types.
  307. if (0 === strpos($key, '@') && $this->isElementNameValid($attributeName = substr($key, 1))) {
  308. if (!is_scalar($data)) {
  309. $data = $this->serializer->normalize($data, $this->format, $this->context);
  310. }
  311. $parentNode->setAttribute($attributeName, $data);
  312. } elseif ($key === '#') {
  313. $append = $this->selectNodeType($parentNode, $data);
  314. } elseif (is_array($data) && false === is_numeric($key)) {
  315. // Is this array fully numeric keys?
  316. if (ctype_digit(implode('', array_keys($data)))) {
  317. /*
  318. * Create nodes to append to $parentNode based on the $key of this array
  319. * Produces <xml><item>0</item><item>1</item></xml>
  320. * From array("item" => array(0,1));.
  321. */
  322. foreach ($data as $subData) {
  323. $append = $this->appendNode($parentNode, $subData, $key);
  324. }
  325. } else {
  326. $append = $this->appendNode($parentNode, $data, $key);
  327. }
  328. } elseif (is_numeric($key) || !$this->isElementNameValid($key)) {
  329. $append = $this->appendNode($parentNode, $data, 'item', $key);
  330. } else {
  331. $append = $this->appendNode($parentNode, $data, $key);
  332. }
  333. }
  334. return $append;
  335. }
  336. if (is_object($data)) {
  337. $data = $this->serializer->normalize($data, $this->format, $this->context);
  338. if (null !== $data && !is_scalar($data)) {
  339. return $this->buildXml($parentNode, $data, $xmlRootNodeName);
  340. }
  341. // top level data object was normalized into a scalar
  342. if (!$parentNode->parentNode->parentNode) {
  343. $root = $parentNode->parentNode;
  344. $root->removeChild($parentNode);
  345. return $this->appendNode($root, $data, $xmlRootNodeName);
  346. }
  347. return $this->appendNode($parentNode, $data, 'data');
  348. }
  349. throw new UnexpectedValueException(sprintf('An unexpected value could not be serialized: %s', var_export($data, true)));
  350. }
  351. /**
  352. * Selects the type of node to create and appends it to the parent.
  353. *
  354. * @param \DOMNode $parentNode
  355. * @param array|object $data
  356. * @param string $nodeName
  357. * @param string $key
  358. *
  359. * @return bool
  360. */
  361. private function appendNode(\DOMNode $parentNode, $data, $nodeName, $key = null)
  362. {
  363. $node = $this->dom->createElement($nodeName);
  364. if (null !== $key) {
  365. $node->setAttribute('key', $key);
  366. }
  367. $appendNode = $this->selectNodeType($node, $data);
  368. // we may have decided not to append this node, either in error or if its $nodeName is not valid
  369. if ($appendNode) {
  370. $parentNode->appendChild($node);
  371. }
  372. return $appendNode;
  373. }
  374. /**
  375. * Checks if a value contains any characters which would require CDATA wrapping.
  376. *
  377. * @param string $val
  378. *
  379. * @return bool
  380. */
  381. private function needsCdataWrapping($val)
  382. {
  383. return preg_match('/[<>&]/', $val);
  384. }
  385. /**
  386. * Tests the value being passed and decide what sort of element to create.
  387. *
  388. * @param \DOMNode $node
  389. * @param mixed $val
  390. *
  391. * @return bool
  392. *
  393. * @throws UnexpectedValueException
  394. */
  395. private function selectNodeType(\DOMNode $node, $val)
  396. {
  397. if (is_array($val)) {
  398. return $this->buildXml($node, $val);
  399. } elseif ($val instanceof \SimpleXMLElement) {
  400. $child = $this->dom->importNode(dom_import_simplexml($val), true);
  401. $node->appendChild($child);
  402. } elseif ($val instanceof \Traversable) {
  403. $this->buildXml($node, $val);
  404. } elseif (is_object($val)) {
  405. return $this->selectNodeType($node, $this->serializer->normalize($val, $this->format, $this->context));
  406. } elseif (is_numeric($val)) {
  407. return $this->appendText($node, (string) $val);
  408. } elseif (is_string($val) && $this->needsCdataWrapping($val)) {
  409. return $this->appendCData($node, $val);
  410. } elseif (is_string($val)) {
  411. return $this->appendText($node, $val);
  412. } elseif (is_bool($val)) {
  413. return $this->appendText($node, (int) $val);
  414. } elseif ($val instanceof \DOMNode) {
  415. $child = $this->dom->importNode($val, true);
  416. $node->appendChild($child);
  417. }
  418. return true;
  419. }
  420. /**
  421. * Get real XML root node name, taking serializer options into account.
  422. *
  423. * @param array $context
  424. *
  425. * @return string
  426. */
  427. private function resolveXmlRootName(array $context = array())
  428. {
  429. return isset($context['xml_root_node_name'])
  430. ? $context['xml_root_node_name']
  431. : $this->rootNodeName;
  432. }
  433. /**
  434. * Create a DOM document, taking serializer options into account.
  435. *
  436. * @param array $context options that the encoder has access to
  437. *
  438. * @return \DOMDocument
  439. */
  440. private function createDomDocument(array $context)
  441. {
  442. $document = new \DOMDocument();
  443. // Set an attribute on the DOM document specifying, as part of the XML declaration,
  444. $xmlOptions = array(
  445. // nicely formats output with indentation and extra space
  446. 'xml_format_output' => 'formatOutput',
  447. // the version number of the document
  448. 'xml_version' => 'xmlVersion',
  449. // the encoding of the document
  450. 'xml_encoding' => 'encoding',
  451. // whether the document is standalone
  452. 'xml_standalone' => 'xmlStandalone',
  453. );
  454. foreach ($xmlOptions as $xmlOption => $documentProperty) {
  455. if (isset($context[$xmlOption])) {
  456. $document->$documentProperty = $context[$xmlOption];
  457. }
  458. }
  459. return $document;
  460. }
  461. }