PageRenderTime 39ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Zend/Serializer/Adapter/Wddx.php

http://grupal.googlecode.com/
PHP | 130 lines | 61 code | 11 blank | 58 comment | 8 complexity | 85bb7c45a5b1f15c1f9843f737d40589 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Serializer
  17. * @subpackage Adapter
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Wddx.php 25031 2012-08-17 19:42:00Z matthew $
  21. */
  22. /** @see Zend_Serializer_Adapter_AdapterAbstract */
  23. require_once 'Zend/Serializer/Adapter/AdapterAbstract.php';
  24. /**
  25. * @link http://www.infoloom.com/gcaconfs/WEB/chicago98/simeonov.HTM
  26. * @link http://en.wikipedia.org/wiki/WDDX
  27. * @category Zend
  28. * @package Zend_Serializer
  29. * @subpackage Adapter
  30. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Serializer_Adapter_Wddx extends Zend_Serializer_Adapter_AdapterAbstract
  34. {
  35. /**
  36. * @var array Default options
  37. */
  38. protected $_options = array(
  39. 'comment' => null,
  40. );
  41. /**
  42. * Constructor
  43. *
  44. * @param array $opts
  45. * @return void
  46. * @throws Zend_Serializer_Exception if wddx extension not found
  47. */
  48. public function __construct($opts = array())
  49. {
  50. if (!extension_loaded('wddx')) {
  51. require_once 'Zend/Serializer/Exception.php';
  52. throw new Zend_Serializer_Exception('PHP extension "wddx" is required for this adapter');
  53. }
  54. parent::__construct($opts);
  55. }
  56. /**
  57. * Serialize PHP to WDDX
  58. *
  59. * @param mixed $value
  60. * @param array $opts
  61. * @return string
  62. * @throws Zend_Serializer_Exception on wddx error
  63. */
  64. public function serialize($value, array $opts = array())
  65. {
  66. $opts = $opts + $this->_options;
  67. if (isset($opts['comment']) && $opts['comment']) {
  68. $wddx = wddx_serialize_value($value, (string)$opts['comment']);
  69. } else {
  70. $wddx = wddx_serialize_value($value);
  71. }
  72. if ($wddx === false) {
  73. $lastErr = error_get_last();
  74. require_once 'Zend/Serializer/Exception.php';
  75. throw new Zend_Serializer_Exception($lastErr['message']);
  76. }
  77. return $wddx;
  78. }
  79. /**
  80. * Unserialize from WDDX to PHP
  81. *
  82. * @param string $wddx
  83. * @param array $opts
  84. * @return mixed
  85. * @throws Zend_Serializer_Exception on wddx error
  86. */
  87. public function unserialize($wddx, array $opts = array())
  88. {
  89. $ret = wddx_deserialize($wddx);
  90. if ($ret === null) {
  91. // check if the returned NULL is valid
  92. // or based on an invalid wddx string
  93. try {
  94. $oldLibxmlDisableEntityLoader = libxml_disable_entity_loader(true);
  95. $dom = new DOMDocument;
  96. $dom->loadXML($wddx);
  97. foreach ($dom->childNodes as $child) {
  98. if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
  99. require_once 'Zend/Serializer/Exception.php';
  100. throw new Zend_Serializer_Exception(
  101. 'Invalid XML: Detected use of illegal DOCTYPE'
  102. );
  103. }
  104. }
  105. $simpleXml = simplexml_import_dom($dom);
  106. libxml_disable_entity_loader($oldLibxmlDisableEntityLoader);
  107. if (isset($simpleXml->data[0]->null[0])) {
  108. return null; // valid null
  109. }
  110. $errMsg = 'Can\'t unserialize wddx string';
  111. } catch (Exception $e) {
  112. $errMsg = $e->getMessage();
  113. }
  114. require_once 'Zend/Serializer/Exception.php';
  115. throw new Zend_Serializer_Exception($errMsg);
  116. }
  117. return $ret;
  118. }
  119. }