Abstract.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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_Feed
  17. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Feed
  23. */
  24. require_once 'Zend/Feed.php';
  25. /**
  26. * @see Zend_Feed_Element
  27. */
  28. require_once 'Zend/Feed/Element.php';
  29. /** @see Zend_Xml_Security */
  30. require_once 'Zend/Xml/Security.php';
  31. /**
  32. * Zend_Feed_Entry_Abstract represents a single entry in an Atom or RSS
  33. * feed.
  34. *
  35. * @category Zend
  36. * @package Zend_Feed
  37. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. */
  40. abstract class Zend_Feed_Entry_Abstract extends Zend_Feed_Element
  41. {
  42. /**
  43. * Root XML element for entries. Subclasses must define this to a
  44. * non-null value.
  45. *
  46. * @var string
  47. */
  48. protected $_rootElement;
  49. /**
  50. * Root namespace for entries. Subclasses may define this to a
  51. * non-null value.
  52. *
  53. * @var string
  54. */
  55. protected $_rootNamespace = null;
  56. /**
  57. * Zend_Feed_Entry_Abstract constructor
  58. *
  59. * The Zend_Feed_Entry_Abstract constructor takes the URI of the feed the entry
  60. * is part of, and optionally an XML construct (usually a
  61. * SimpleXMLElement, but it can be an XML string or a DOMNode as
  62. * well) that contains the contents of the entry.
  63. *
  64. * @param string $uri
  65. * @param SimpleXMLElement|DOMNode|string $element
  66. * @return void
  67. * @throws Zend_Feed_Exception
  68. */
  69. public function __construct($uri = null, $element = null)
  70. {
  71. if (!($element instanceof DOMElement)) {
  72. if ($element) {
  73. // Load the feed as an XML DOMDocument object
  74. @ini_set('track_errors', 1);
  75. $doc = new DOMDocument();
  76. $doc = @Zend_Xml_Security::scan($element, $doc);
  77. @ini_restore('track_errors');
  78. if (!$doc) {
  79. // prevent the class to generate an undefined variable notice (ZF-2590)
  80. if (!isset($php_errormsg)) {
  81. if (function_exists('xdebug_is_enabled')) {
  82. $php_errormsg = '(error message not available, when XDebug is running)';
  83. } else {
  84. $php_errormsg = '(error message not available)';
  85. }
  86. }
  87. /**
  88. * @see Zend_Feed_Exception
  89. */
  90. require_once 'Zend/Feed/Exception.php';
  91. throw new Zend_Feed_Exception("DOMDocument cannot parse XML: $php_errormsg");
  92. }
  93. $element = $doc->getElementsByTagName($this->_rootElement)->item(0);
  94. if (!$element) {
  95. /**
  96. * @see Zend_Feed_Exception
  97. */
  98. require_once 'Zend/Feed/Exception.php';
  99. throw new Zend_Feed_Exception('No root <' . $this->_rootElement . '> element found, cannot parse feed.');
  100. }
  101. } else {
  102. $doc = new DOMDocument('1.0', 'utf-8');
  103. if ($this->_rootNamespace !== null) {
  104. $element = $doc->createElementNS(Zend_Feed::lookupNamespace($this->_rootNamespace), $this->_rootElement);
  105. } else {
  106. $element = $doc->createElement($this->_rootElement);
  107. }
  108. }
  109. }
  110. parent::__construct($element);
  111. }
  112. }