[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Feed/Parser/ -> RssParser.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2012 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Feed\Parser;
  11  
  12  use Joomla\CMS\Feed\Feed;
  13  use Joomla\CMS\Feed\FeedEntry;
  14  use Joomla\CMS\Feed\FeedLink;
  15  use Joomla\CMS\Feed\FeedParser;
  16  use Joomla\CMS\Feed\FeedPerson;
  17  
  18  // phpcs:disable PSR1.Files.SideEffects
  19  \defined('JPATH_PLATFORM') or die;
  20  // phpcs:enable PSR1.Files.SideEffects
  21  
  22  /**
  23   * RSS Feed Parser class.
  24   *
  25   * @link   http://cyber.law.harvard.edu/rss/rss.html
  26   * @since  3.1.4
  27   */
  28  class RssParser extends FeedParser
  29  {
  30      /**
  31       * @var    string  The feed element name for the entry elements.
  32       * @since  3.1.4
  33       */
  34      protected $entryElementName = 'item';
  35  
  36      /**
  37       * @var    string  The feed format version.
  38       * @since  3.1.4
  39       */
  40      protected $version;
  41  
  42      /**
  43       * Method to handle the `<category>` element for the feed.
  44       *
  45       * @param   Feed               $feed  The Feed object being built from the parsed feed.
  46       * @param   \SimpleXMLElement  $el    The current XML element object to handle.
  47       *
  48       * @return  void
  49       *
  50       * @since   3.1.4
  51       */
  52      protected function handleCategory(Feed $feed, \SimpleXMLElement $el)
  53      {
  54          // Get the data from the element.
  55          $domain    = (string) $el['domain'];
  56          $category  = $this->inputFilter->clean((string) $el, 'html');
  57  
  58          $feed->addCategory($category, $domain);
  59      }
  60  
  61      /**
  62       * Method to handle the `<cloud>` element for the feed.
  63       *
  64       * @param   Feed               $feed  The Feed object being built from the parsed feed.
  65       * @param   \SimpleXMLElement  $el    The current XML element object to handle.
  66       *
  67       * @return  void
  68       *
  69       * @since   3.1.4
  70       */
  71      protected function handleCloud(Feed $feed, \SimpleXMLElement $el)
  72      {
  73          $cloud = new \stdClass();
  74          $cloud->domain            = (string) $el['domain'];
  75          $cloud->port              = (string) $el['port'];
  76          $cloud->path              = (string) $el['path'];
  77          $cloud->protocol          = (string) $el['protocol'];
  78          $cloud->registerProcedure = (string) $el['registerProcedure'];
  79  
  80          $feed->cloud = $cloud;
  81      }
  82  
  83      /**
  84       * Method to handle the `<copyright>` element for the feed.
  85       *
  86       * @param   Feed               $feed  The Feed object being built from the parsed feed.
  87       * @param   \SimpleXMLElement  $el    The current XML element object to handle.
  88       *
  89       * @return  void
  90       *
  91       * @since   3.1.4
  92       */
  93      protected function handleCopyright(Feed $feed, \SimpleXMLElement $el)
  94      {
  95          $feed->copyright = $this->inputFilter->clean((string) $el, 'html');
  96      }
  97  
  98      /**
  99       * Method to handle the `<description>` element for the feed.
 100       *
 101       * @param   Feed               $feed  The Feed object being built from the parsed feed.
 102       * @param   \SimpleXMLElement  $el    The current XML element object to handle.
 103       *
 104       * @return  void
 105       *
 106       * @since   3.1.4
 107       */
 108      protected function handleDescription(Feed $feed, \SimpleXMLElement $el)
 109      {
 110          $feed->description = $this->inputFilter->clean((string) $el, 'html');
 111      }
 112  
 113      /**
 114       * Method to handle the `<generator>` element for the feed.
 115       *
 116       * @param   Feed               $feed  The Feed object being built from the parsed feed.
 117       * @param   \SimpleXMLElement  $el    The current XML element object to handle.
 118       *
 119       * @return  void
 120       *
 121       * @since   3.1.4
 122       */
 123      protected function handleGenerator(Feed $feed, \SimpleXMLElement $el)
 124      {
 125          $feed->generator = $this->inputFilter->clean((string) $el, 'html');
 126      }
 127  
 128      /**
 129       * Method to handle the `<image>` element for the feed.
 130       *
 131       * @param   Feed               $feed  The Feed object being built from the parsed feed.
 132       * @param   \SimpleXMLElement  $el    The current XML element object to handle.
 133       *
 134       * @return  void
 135       *
 136       * @since   3.1.4
 137       */
 138      protected function handleImage(Feed $feed, \SimpleXMLElement $el)
 139      {
 140          // Create a feed link object for the image.
 141          $image = new FeedLink(
 142              (string) $el->url,
 143              null,
 144              'logo',
 145              null,
 146              $this->inputFilter->clean((string) $el->title, 'html')
 147          );
 148  
 149          // Populate extra fields if they exist.
 150          $image->link         = (string) filter_var($el->link, FILTER_VALIDATE_URL);
 151          $image->description  = $this->inputFilter->clean((string) $el->description, 'html');
 152          $image->height       = (string) $el->height;
 153          $image->width        = (string) $el->width;
 154  
 155          $feed->image = $image;
 156      }
 157  
 158      /**
 159       * Method to handle the `<language>` element for the feed.
 160       *
 161       * @param   Feed               $feed  The Feed object being built from the parsed feed.
 162       * @param   \SimpleXMLElement  $el    The current XML element object to handle.
 163       *
 164       * @return  void
 165       *
 166       * @since   3.1.4
 167       */
 168      protected function handleLanguage(Feed $feed, \SimpleXMLElement $el)
 169      {
 170          $feed->language = $this->inputFilter->clean((string) $el, 'html');
 171      }
 172  
 173      /**
 174       * Method to handle the `<lastBuildDate>` element for the feed.
 175       *
 176       * @param   Feed               $feed  The Feed object being built from the parsed feed.
 177       * @param   \SimpleXMLElement  $el    The current XML element object to handle.
 178       *
 179       * @return  void
 180       *
 181       * @since   3.1.4
 182       */
 183      protected function handleLastBuildDate(Feed $feed, \SimpleXMLElement $el)
 184      {
 185          $feed->updatedDate = $this->inputFilter->clean((string) $el, 'html');
 186      }
 187  
 188      /**
 189       * Method to handle the `<link>` element for the feed.
 190       *
 191       * @param   Feed               $feed  The Feed object being built from the parsed feed.
 192       * @param   \SimpleXMLElement  $el    The current XML element object to handle.
 193       *
 194       * @return  void
 195       *
 196       * @since   3.1.4
 197       */
 198      protected function handleLink(Feed $feed, \SimpleXMLElement $el)
 199      {
 200          $link = new FeedLink();
 201          $link->uri = (string) $el['href'];
 202          $feed->link = $link;
 203      }
 204  
 205      /**
 206       * Method to handle the `<managingEditor>` element for the feed.
 207       *
 208       * @param   Feed               $feed  The Feed object being built from the parsed feed.
 209       * @param   \SimpleXMLElement  $el    The current XML element object to handle.
 210       *
 211       * @return  void
 212       *
 213       * @since   3.1.4
 214       */
 215      protected function handleManagingEditor(Feed $feed, \SimpleXMLElement $el)
 216      {
 217          $feed->author = $this->processPerson((string) $el);
 218      }
 219  
 220      /**
 221       * Method to handle the `<skipDays>` element for the feed.
 222       *
 223       * @param   Feed               $feed  The Feed object being built from the parsed feed.
 224       * @param   \SimpleXMLElement  $el    The current XML element object to handle.
 225       *
 226       * @return  void
 227       *
 228       * @since   3.1.4
 229       */
 230      protected function handleSkipDays(Feed $feed, \SimpleXMLElement $el)
 231      {
 232          // Initialise the array.
 233          $days = array();
 234  
 235          // Add all of the day values from the feed to the array.
 236          foreach ($el->day as $day) {
 237              $days[] = (string) $day;
 238          }
 239  
 240          $feed->skipDays = $days;
 241      }
 242  
 243      /**
 244       * Method to handle the `<skipHours>` element for the feed.
 245       *
 246       * @param   Feed               $feed  The Feed object being built from the parsed feed.
 247       * @param   \SimpleXMLElement  $el    The current XML element object to handle.
 248       *
 249       * @return  void
 250       *
 251       * @since   3.1.4
 252       */
 253      protected function handleSkipHours(Feed $feed, \SimpleXMLElement $el)
 254      {
 255          // Initialise the array.
 256          $hours = array();
 257  
 258          // Add all of the day values from the feed to the array.
 259          foreach ($el->hour as $hour) {
 260              $hours[] = (int) $hour;
 261          }
 262  
 263          $feed->skipHours = $hours;
 264      }
 265  
 266      /**
 267       * Method to handle the `<pubDate>` element for the feed.
 268       *
 269       * @param   Feed               $feed  The Feed object being built from the parsed feed.
 270       * @param   \SimpleXMLElement  $el    The current XML element object to handle.
 271       *
 272       * @return  void
 273       *
 274       * @since   3.1.4
 275       */
 276      protected function handlePubDate(Feed $feed, \SimpleXMLElement $el)
 277      {
 278          $feed->publishedDate = $this->inputFilter->clean((string) $el, 'html');
 279      }
 280  
 281      /**
 282       * Method to handle the `<title>` element for the feed.
 283       *
 284       * @param   Feed               $feed  The Feed object being built from the parsed feed.
 285       * @param   \SimpleXMLElement  $el    The current XML element object to handle.
 286       *
 287       * @return  void
 288       *
 289       * @since   3.1.4
 290       */
 291      protected function handleTitle(Feed $feed, \SimpleXMLElement $el)
 292      {
 293          $feed->title = $this->inputFilter->clean((string) $el, 'html');
 294      }
 295  
 296      /**
 297       * Method to handle the `<ttl>` element for the feed.
 298       *
 299       * @param   Feed               $feed  The Feed object being built from the parsed feed.
 300       * @param   \SimpleXMLElement  $el    The current XML element object to handle.
 301       *
 302       * @return  void
 303       *
 304       * @since   3.1.4
 305       */
 306      protected function handleTtl(Feed $feed, \SimpleXMLElement $el)
 307      {
 308          $feed->ttl = (int) $this->inputFilter->clean((string) $el, 'int');
 309      }
 310  
 311      /**
 312       * Method to handle the `<webmaster>` element for the feed.
 313       *
 314       * @param   Feed               $feed  The Feed object being built from the parsed feed.
 315       * @param   \SimpleXMLElement  $el    The current XML element object to handle.
 316       *
 317       * @return  void
 318       *
 319       * @since   3.1.4
 320       */
 321      protected function handleWebmaster(Feed $feed, \SimpleXMLElement $el)
 322      {
 323          // Get the tag contents and split it over the first space.
 324          $tmp = (string) $el;
 325          $tmp = explode(' ', $tmp, 2);
 326  
 327          // This is really cheap parsing.  Probably need to create a method to do this more robustly.
 328          $name = null;
 329  
 330          if (isset($tmp[1])) {
 331              $name = trim(
 332                  $this->inputFilter->clean($tmp[1], 'html'),
 333                  ' ()'
 334              );
 335          }
 336  
 337          $email = trim(
 338              filter_var((string) $tmp[0], FILTER_VALIDATE_EMAIL)
 339          );
 340  
 341          $feed->addContributor($name, $email, null, 'webmaster');
 342      }
 343  
 344      /**
 345       * Method to initialise the feed for parsing.  Here we detect the version and advance the stream
 346       * reader so that it is ready to parse feed elements.
 347       *
 348       * @return  void
 349       *
 350       * @since   3.1.4
 351       */
 352      protected function initialise()
 353      {
 354          // We are on the first XML Element after the xml doc type declaration
 355          $this->version = $this->stream->getAttribute('version');
 356  
 357          // We want to move forward to the first element after the <channel> element.
 358          $this->moveToNextElement('channel');
 359          $this->moveToNextElement();
 360      }
 361  
 362      /**
 363       * Method to handle a `<item>` element for the feed.
 364       *
 365       * @param   FeedEntry          $entry  The FeedEntry object being built from the parsed feed entry.
 366       * @param   \SimpleXMLElement  $el     The current XML element object to handle.
 367       *
 368       * @return  void
 369       *
 370       * @since   3.1.4
 371       */
 372      protected function processFeedEntry(FeedEntry $entry, \SimpleXMLElement $el)
 373      {
 374          $entry->uri           = (string) filter_var($el->link, FILTER_VALIDATE_URL);
 375          $entry->title         = $this->inputFilter->clean((string) $el->title, 'html');
 376          $entry->publishedDate = $this->inputFilter->clean((string) $el->pubDate, 'html');
 377          $entry->updatedDate   = $this->inputFilter->clean((string) $el->pubDate, 'html');
 378          $entry->content       = $this->inputFilter->clean((string) $el->description, 'html');
 379          $entry->guid          = $this->inputFilter->clean((string) $el->guid, 'html');
 380          $entry->isPermaLink   = $entry->guid !== '' && (string) $el->guid['isPermaLink'] !== 'false';
 381          $entry->comments      = $this->inputFilter->clean((string) $el->comments, 'html');
 382  
 383          // Add the feed entry author if available.
 384          $author = $this->inputFilter->clean((string) $el->author, 'html');
 385  
 386          if (!empty($author)) {
 387              $entry->author = $this->processPerson($author);
 388          }
 389  
 390          // Add any categories to the entry.
 391          foreach ($el->category as $category) {
 392              $entry->addCategory((string) $category, (string) $category['domain']);
 393          }
 394  
 395          // Add any enclosures to the entry.
 396          foreach ($el->enclosure as $enclosure) {
 397              $link = new FeedLink(
 398                  (string) $enclosure['url'],
 399                  null,
 400                  (string) $enclosure['type'],
 401                  null,
 402                  null,
 403                  (int) $enclosure['length']
 404              );
 405  
 406              $entry->addLink($link);
 407          }
 408      }
 409  
 410      /**
 411       * Method to parse a string with person data and return a FeedPerson object.
 412       *
 413       * @param   string  $data  The string to parse for a person.
 414       *
 415       * @return  FeedPerson
 416       *
 417       * @since   3.1.4
 418       */
 419      protected function processPerson($data)
 420      {
 421          // Create a new person object.
 422          $person = new FeedPerson();
 423  
 424          // This is really cheap parsing, but so far good enough. :)
 425          $data = explode(' ', $data, 2);
 426  
 427          if (isset($data[1])) {
 428              $person->name = trim(
 429                  $this->inputFilter->clean($data[1], 'html'),
 430                  ' ()'
 431              );
 432          }
 433  
 434          // Set the email for the person.
 435          $person->email = trim(
 436              filter_var((string) $data[0], FILTER_VALIDATE_EMAIL)
 437          );
 438  
 439          return $person;
 440      }
 441  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer