2
0

PhpSerialize.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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$
  21. */
  22. /** @see Zend_Serializer_Adapter_AdapterAbstract */
  23. require_once 'Zend/Serializer/Adapter/AdapterAbstract.php';
  24. /**
  25. * @category Zend
  26. * @package Zend_Serializer
  27. * @subpackage Adapter
  28. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Serializer_Adapter_PhpSerialize extends Zend_Serializer_Adapter_AdapterAbstract
  32. {
  33. /**
  34. * @var null|string Serialized boolean false value
  35. */
  36. private static $_serializedFalse = null;
  37. /**
  38. * Constructor
  39. *
  40. * @param array|Zend_Config $opts
  41. * @return void
  42. */
  43. public function __construct($opts = array())
  44. {
  45. parent::__construct($opts);
  46. if (self::$_serializedFalse === null) {
  47. self::$_serializedFalse = serialize(false);
  48. }
  49. }
  50. /**
  51. * Serialize using serialize()
  52. *
  53. * @param mixed $value
  54. * @param array $opts
  55. * @return string
  56. * @throws Zend_Serializer_Exception On serialize error
  57. */
  58. public function serialize($value, array $opts = array())
  59. {
  60. $ret = serialize($value);
  61. if ($ret === false) {
  62. $lastErr = error_get_last();
  63. require_once 'Zend/Serializer/Exception.php';
  64. throw new Zend_Serializer_Exception($lastErr['message']);
  65. }
  66. return $ret;
  67. }
  68. /**
  69. * Unserialize
  70. *
  71. * @todo Allow integration with unserialize_callback_func
  72. * @param string $serialized
  73. * @param array $opts
  74. * @return mixed
  75. * @throws Zend_Serializer_Exception on unserialize error
  76. */
  77. public function unserialize($serialized, array $opts = array())
  78. {
  79. // TODO: @see php.ini directive "unserialize_callback_func"
  80. $ret = @unserialize($serialized);
  81. if ($ret === false && $serialized !== self::$_serializedFalse) {
  82. $lastErr = error_get_last();
  83. require_once 'Zend/Serializer/Exception.php';
  84. throw new Zend_Serializer_Exception($lastErr['message']);
  85. }
  86. return $ret;
  87. }
  88. }