/joomla/libraries/joomla/feed/factory.php

https://gitlab.com/ricardosanchez/prueba · PHP · 142 lines · 66 code · 14 blank · 62 comment · 11 complexity · 8bf70da3bec2388ff3c7e23d05bf46ab MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Feed
  5. *
  6. * @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * Feed factory class.
  12. *
  13. * @since 12.3
  14. */
  15. class JFeedFactory
  16. {
  17. /**
  18. * @var array The list of registered parser classes for feeds.
  19. * @since 12.3
  20. */
  21. protected $parsers = array('rss' => 'JFeedParserRss', 'feed' => 'JFeedParserAtom');
  22. /**
  23. * Method to load a URI into the feed reader for parsing.
  24. *
  25. * @param string $uri The URI of the feed to load. Idn uris must be passed already converted to punycode.
  26. *
  27. * @return JFeedReader
  28. *
  29. * @since 12.3
  30. * @throws InvalidArgumentException
  31. * @throws RuntimeException
  32. */
  33. public function getFeed($uri)
  34. {
  35. // Create the XMLReader object.
  36. $reader = new XMLReader;
  37. // Open the URI within the stream reader.
  38. if (!@$reader->open($uri, null, LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_NOWARNING))
  39. {
  40. // If allow_url_fopen is enabled
  41. if (ini_get('allow_url_fopen'))
  42. {
  43. // This is an error
  44. throw new RuntimeException('Unable to open the feed.');
  45. }
  46. else
  47. {
  48. // Retry with JHttpFactory that allow using CURL and Sockets as alternative method when available
  49. $connector = JHttpFactory::getHttp();
  50. $feed = $connector->get($uri);
  51. // Set the value to the XMLReader parser
  52. if (!$reader->xml($feed->body, null, LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_NOWARNING))
  53. {
  54. throw new RuntimeException('Unable to parse the feed.');
  55. }
  56. }
  57. }
  58. try
  59. {
  60. // Skip ahead to the root node.
  61. while ($reader->read())
  62. {
  63. if ($reader->nodeType == XMLReader::ELEMENT)
  64. {
  65. break;
  66. }
  67. }
  68. }
  69. catch (Exception $e)
  70. {
  71. throw new RuntimeException('Error reading feed.');
  72. }
  73. // Setup the appopriate feed parser for the feed.
  74. $parser = $this->_fetchFeedParser($reader->name, $reader);
  75. return $parser->parse();
  76. }
  77. /**
  78. * Method to register a JFeedParser class for a given root tag name.
  79. *
  80. * @param string $tagName The root tag name for which to register the parser class.
  81. * @param string $className The JFeedParser class name to register for a root tag name.
  82. * @param boolean $overwrite True to overwrite the parser class if one is already registered.
  83. *
  84. * @return JFeedFactory
  85. *
  86. * @since 12.3
  87. * @throws InvalidArgumentException
  88. */
  89. public function registerParser($tagName, $className, $overwrite = false)
  90. {
  91. // Verify that the class exists.
  92. if (!class_exists($className))
  93. {
  94. throw new InvalidArgumentException('The feed parser class ' . $className . ' does not exist.');
  95. }
  96. // Validate that the tag name is valid.
  97. if (!preg_match('/\A(?!XML)[a-z][\w0-9-]*/i', $tagName))
  98. {
  99. throw new InvalidArgumentException('The tag name ' . $tagName . ' is not valid.');
  100. }
  101. // Register the given parser class for the tag name if nothing registered or the overwrite flag set.
  102. if (empty($this->parsers[$tagName]) || (bool) $overwrite)
  103. {
  104. $this->parsers[(string) $tagName] = (string) $className;
  105. }
  106. return $this;
  107. }
  108. /**
  109. * Method to return a new JFeedParser object based on the registered parsers and a given type.
  110. *
  111. * @param string $type The name of parser to return.
  112. * @param XMLReader $reader The XMLReader instance for the feed.
  113. *
  114. * @return JFeedParser
  115. *
  116. * @since 12.3
  117. * @throws LogicException
  118. */
  119. private function _fetchFeedParser($type, XMLReader $reader)
  120. {
  121. // Look for a registered parser for the feed type.
  122. if (empty($this->parsers[$type]))
  123. {
  124. throw new LogicException('No registered feed parser for type ' . $type . '.');
  125. }
  126. return new $this->parsers[$type]($reader);
  127. }
  128. }