PHP Universal Feed Generator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

856 lines
22 KiB

  1. <?php
  2. namespace FeedWriter\View;
  3. use \DateTime;
  4. use \FeedWriter\Item;
  5. /*
  6. * Copyright (C) 2008 Anis uddin Ahmad <anisniit@gmail.com>
  7. * Copyright (C) 2010-2013 Michael Bemmerl <mail@mx-server.de>
  8. *
  9. * This file is part of the "Universal Feed Writer" project.
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. // RSS 0.90 Officially obsoleted by 1.0
  25. // RSS 0.91, 0.92, 0.93 and 0.94 Officially obsoleted by 2.0
  26. // So, define constants for RSS 1.0, RSS 2.0 and ATOM
  27. /**
  28. * Universal Feed Writer class
  29. *
  30. * Generate RSS 1.0, RSS2.0 and ATOM Feeds
  31. *
  32. * @package UniversalFeedWriter
  33. * @author Anis uddin Ahmad <anisniit@gmail.com>
  34. * @link http://www.ajaxray.com/projects/rss
  35. */
  36. abstract class SyndicationBase
  37. {
  38. const RSS1 = 'RSS 1.0';
  39. const RSS2 = 'RSS 2.0';
  40. const ATOM = 'ATOM';
  41. /**
  42. * Collection of all channel elements
  43. */
  44. private $channels = array();
  45. /**
  46. * Collection of items as object of \FeedWriter\Item class.
  47. */
  48. private $items = array();
  49. /**
  50. * Store some other version wise data
  51. */
  52. private $data = array();
  53. /**
  54. * The tag names which have to encoded as CDATA
  55. */
  56. private $CDATAEncoding = array();
  57. /**
  58. * Collection of XML namespaces
  59. */
  60. private $namespaces = array();
  61. /**
  62. * Contains the format of this feed.
  63. */
  64. private $version = null;
  65. /**
  66. * Constructor
  67. *
  68. * If no version is given, a feed in RSS 2.0 format will be generated.
  69. *
  70. * @param constant the version constant (RSS1/RSS2/ATOM).
  71. */
  72. protected function __construct($version = self::RSS2)
  73. {
  74. $this->version = $version;
  75. // Setting default value for essential channel elements
  76. $this->channels['title'] = $version . ' Feed';
  77. $this->channels['link'] = 'http://www.ajaxray.com/blog';
  78. // Add some default XML namespaces
  79. $this->namespaces['content'] = 'http://purl.org/rss/1.0/modules/content/';
  80. $this->namespaces['wfw'] = 'http://wellformedweb.org/CommentAPI/';
  81. $this->namespaces['atom'] = 'http://www.w3.org/2005/Atom';
  82. $this->namespaces['rdf'] = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
  83. $this->namespaces['rss1'] = 'http://purl.org/rss/1.0/';
  84. $this->namespaces['dc'] = 'http://purl.org/dc/elements/1.1/';
  85. $this->namespaces['sy'] = 'http://purl.org/rss/1.0/modules/syndication/';
  86. //Tag names to encode in CDATA
  87. $this->CDATAEncoding = array('description', 'content:encoded', 'summary');
  88. }
  89. // Start # public functions ---------------------------------------------
  90. /**
  91. * Adds a channel element indicating the program used to generate the feed.
  92. *
  93. * @return void
  94. */
  95. public function addGenerator()
  96. {
  97. if ($this->version == static::ATOM)
  98. $this->setChannelElement('atom:generator', 'FeedWriter', array('uri' => 'https://github.com/mibe/FeedWriter'));
  99. else if ($this->version == static::RSS2)
  100. $this->setChannelElement('generator', 'FeedWriter');
  101. else
  102. throw new Exception('The generator element is not supported in RSS1 feeds.');
  103. return $this;
  104. }
  105. /**
  106. * Add a XML namespace to the internal list of namespaces. After that,
  107. * custom channel elements can be used properly to generate a valid feed.
  108. *
  109. * @access public
  110. * @param string namespace prefix
  111. * @param string namespace name (URI)
  112. * @return void
  113. * @link http://www.w3.org/TR/REC-xml-names/
  114. */
  115. public function addNamespace($prefix, $uri)
  116. {
  117. $this->namespaces[$prefix] = $uri;
  118. return $this;
  119. }
  120. /**
  121. * Add a channel element to the feed.
  122. *
  123. * @access public
  124. * @param string name of the channel tag
  125. * @param string content of the channel tag
  126. * @param bool TRUE if this element can appear multiple times
  127. * @return void
  128. */
  129. public function setChannelElement($elementName, $content, $multiple = false)
  130. {
  131. if ($multiple === TRUE)
  132. $this->channels[$elementName][] = $content;
  133. else
  134. $this->channels[$elementName] = $content;
  135. return $this;
  136. }
  137. /**
  138. * Set multiple channel elements from an array. Array elements
  139. * should be 'channelName' => 'channelContent' format.
  140. *
  141. * @access public
  142. * @param array array of channels
  143. * @return void
  144. */
  145. public function setChannelElementsFromArray($elementArray)
  146. {
  147. if (!is_array($elementArray))
  148. return;
  149. foreach ($elementArray as $elementName => $content)
  150. {
  151. $this->setChannelElement($elementName, $content);
  152. }
  153. return $this;
  154. }
  155. /**
  156. * Get the appropriate MIME type string for the current feed.
  157. *
  158. * @access public
  159. * @return string
  160. */
  161. public function getMIMEType()
  162. {
  163. switch($this->version)
  164. {
  165. case static::RSS2 : $mimeType = "application/rss+xml";
  166. break;
  167. case static::RSS1 : $mimeType = "application/rdf+xml";
  168. break;
  169. case static::ATOM : $mimeType = "application/atom+xml";
  170. break;
  171. default : $mimeType = "text/xml";
  172. }
  173. return $mimeType;
  174. }
  175. /**
  176. * Print the actual RSS/ATOM file
  177. *
  178. * Sets a Content-Type header and echoes the contents of the feed.
  179. * Should only be used in situations where direct output is desired;
  180. * if you need to pass a string around, use generateFeed() instead.
  181. *
  182. * @access public
  183. * @param bool FALSE if the specific feed media type should be sent.
  184. * @return void
  185. */
  186. public function printFeed($useGenericContentType = false)
  187. {
  188. $contentType = "text/xml";
  189. if (!$useGenericContentType)
  190. {
  191. $contentType = $this->getMIMEType();
  192. }
  193. header("Content-Type: " . $contentType);
  194. echo $this->generateFeed();
  195. }
  196. /**
  197. * Generate the feed.
  198. *
  199. * @access public
  200. * @return string
  201. */
  202. public function generateFeed()
  203. {
  204. return $this->makeHeader()
  205. . $this->makeChannels()
  206. . $this->makeItems()
  207. . $this->makeFooter();
  208. }
  209. /**
  210. * Create a new Item.
  211. *
  212. * @access public
  213. * @return object instance of Item class
  214. */
  215. public function createNewItem()
  216. {
  217. $Item = new Item($this->version);
  218. return $Item;
  219. }
  220. /**
  221. * Add a FeedItem to the main class
  222. *
  223. * @access public
  224. * @param Item instance of Item class
  225. * @return void
  226. */
  227. public function addItem(Item $feedItem)
  228. {
  229. if ($feedItem->getVersion() != $this->version)
  230. throw new Exception('Feed type mismatch: This instance can handle ' . $this->version . ' feeds only, but item with type ' . $feedItem->getVersion() . ' given.');
  231. $this->items[] = $feedItem;
  232. return $this;
  233. }
  234. // Wrapper functions -------------------------------------------------------------------
  235. /**
  236. * Set the 'title' channel element
  237. *
  238. * @access public
  239. * @param string value of 'title' channel tag
  240. * @return void
  241. */
  242. public function setTitle($title)
  243. {
  244. return $this->setChannelElement('title', $title);
  245. }
  246. /**
  247. * Set the date when the ATOM feed was lastly updated.
  248. *
  249. * This adds the 'updated' element to the feed. The value of the date parameter
  250. * can be either an instance of the DateTime class, an integer containing a UNIX
  251. * timestamp or a string which is parseable by PHP's 'strtotime' function.
  252. *
  253. * Not supported in RSS1 feeds.
  254. *
  255. * @access public
  256. * @param DateTime|int|string Date which should be used.
  257. * @return void
  258. */
  259. public function setDate($date)
  260. {
  261. if ($this->version == static::RSS1)
  262. throw new Exception('The publication date is not supported in RSS1 feeds.');
  263. // The feeds have different date formats.
  264. $format = $this->version == static::ATOM ? \DATE_ATOM : \DATE_RSS;
  265. if ($date instanceof DateTime)
  266. $date = $date->format($format);
  267. else if(is_numeric($date) && $date >= 0)
  268. $date = date($format, $date);
  269. else if (is_string($date))
  270. $date = date($format, strtotime($date));
  271. else
  272. throw new Exception('The given date was not an instance of DateTime, a UNIX timestamp or a date string.');
  273. if ($this->version == static::ATOM)
  274. $this->setChannelElement('updated', $date);
  275. else
  276. $this->setChannelElement('lastBuildDate', $date);
  277. return $this;
  278. }
  279. /**
  280. * Set the 'description' channel element
  281. *
  282. * @access public
  283. * @param string value of 'description' channel tag
  284. * @return void
  285. */
  286. public function setDescription($description)
  287. {
  288. if ($this->version != static::ATOM)
  289. $this->setChannelElement('description', $description);
  290. return $this;
  291. }
  292. /**
  293. * Set the 'link' channel element
  294. *
  295. * @access public
  296. * @param string value of 'link' channel tag
  297. * @return void
  298. */
  299. public function setLink($link)
  300. {
  301. return $this->setChannelElement('link', $link);
  302. }
  303. /**
  304. * Set custom 'link' channel elements.
  305. *
  306. * In ATOM feeds, only one link with alternate relation and the same combination of
  307. * type and hreflang values.
  308. *
  309. * @access public
  310. * @param string URI of this link
  311. * @param string relation type of the resource
  312. * @param string media type of the target resource
  313. * @param string language of the resource
  314. * @param string human-readable information about the resource
  315. * @param int length of the resource in bytes
  316. * @link https://www.iana.org/assignments/link-relations/link-relations.xml
  317. * @link https://tools.ietf.org/html/rfc4287#section-4.1.1
  318. */
  319. public function setAtomLink($href, $rel = null, $type = null, $hreflang = null, $title = null, $length = null)
  320. {
  321. $data = array('href' => $href);
  322. if ($rel != null)
  323. {
  324. if (!is_string($rel) || empty($rel))
  325. throw new Exception('rel parameter must be a string and a valid relation identifier.');
  326. $data['rel'] = $rel;
  327. }
  328. if ($type != null)
  329. {
  330. // Regex used from RFC 4287, page 41
  331. if (!is_string($type) || preg_match('/.+\/.+/', $type) != 1)
  332. throw new Exception('type parameter must be a string and a MIME type.');
  333. $data['type'] = $type;
  334. }
  335. if ($hreflang != null)
  336. {
  337. // Regex used from RFC 4287, page 41
  338. if (!is_string($hreflang) || preg_match('/[A-Za-z]{1,8}(-[A-Za-z0-9]{1,8})*/', $hreflang) != 1)
  339. throw new Exception('hreflang parameter must be a string and a valid language code.');
  340. $data['hreflang'] = $hreflang;
  341. }
  342. if ($title != null)
  343. {
  344. if (!is_string($title) || empty($title))
  345. throw new Exception('title parameter must be a string and not empty.');
  346. $data['title'] = $title;
  347. }
  348. if ($length != null)
  349. {
  350. if (!is_int($length) || $length < 0)
  351. throw new Exception('length parameter must be a positive integer.');
  352. $data['length'] = (string)$length;
  353. }
  354. // ATOM spec. has some restrictions on atom:link usage
  355. // See RFC 4287, page 12 (4.1.1)
  356. if ($this->version == static::ATOM)
  357. {
  358. foreach($this->channels as $key => $value)
  359. {
  360. if ($key != 'atom:link')
  361. continue;
  362. // $value is an array , so check every element
  363. foreach($value as $linkItem)
  364. {
  365. // Only one link with relation alternate and same hreflang & type is allowed.
  366. if (@$linkItem['rel'] == 'alternate' && @$linkItem['hreflang'] == $hreflang && @$linkItem['type'] == $type)
  367. throw new Exception('The feed must not contain more than one link element with a relation of "alternate"'
  368. . ' that has the same combination of type and hreflang attribute values.');
  369. }
  370. }
  371. }
  372. return $this->setChannelElement('atom:link', $data, true);
  373. }
  374. /**
  375. * Set an 'atom:link' channel element with relation=self attribute.
  376. * Needs the full URL to this feed.
  377. *
  378. * @link http://www.rssboard.org/rss-profile#namespace-elements-atom-link
  379. * @access public
  380. * @param string URL to this feed
  381. * @return void
  382. */
  383. public function setSelfLink($url)
  384. {
  385. return $this->setAtomLink($url, 'self', $this->getMIMEType());
  386. }
  387. /**
  388. * Set the 'image' channel element
  389. *
  390. * @access public
  391. * @param string title of image
  392. * @param string link url of the image
  393. * @param string path url of the image
  394. * @return void
  395. */
  396. public function setImage($title, $link, $url)
  397. {
  398. return $this->setChannelElement('image', array('title'=>$title, 'link'=>$link, 'url'=>$url));
  399. }
  400. /**
  401. * Set the 'about' channel element. Only for RSS 1.0
  402. *
  403. * @access public
  404. * @param string value of 'about' channel tag
  405. * @return void
  406. */
  407. public function setChannelAbout($url)
  408. {
  409. $this->data['ChannelAbout'] = $url;
  410. return $this;
  411. }
  412. /**
  413. * Generate an UUID.
  414. *
  415. * The UUID is based on an MD5 hash. If no key is given, a unique ID as the input
  416. * for the MD5 hash is generated.
  417. *
  418. * @author Anis uddin Ahmad <admin@ajaxray.com>
  419. * @param string optional key on which the UUID is generated
  420. * @param string an optional prefix
  421. * @return string the formated UUID
  422. */
  423. public static function uuid($key = null, $prefix = '')
  424. {
  425. $key = ($key == null) ? uniqid(rand()) : $key;
  426. $chars = md5($key);
  427. $uuid = substr($chars,0,8) . '-';
  428. $uuid .= substr($chars,8,4) . '-';
  429. $uuid .= substr($chars,12,4) . '-';
  430. $uuid .= substr($chars,16,4) . '-';
  431. $uuid .= substr($chars,20,12);
  432. return $prefix . $uuid;
  433. }
  434. // End # public functions ----------------------------------------------
  435. // Start # private functions ----------------------------------------------
  436. /**
  437. * Returns all used XML namespace prefixes in this instance.
  438. * This includes all channel elements and feed items.
  439. * Unfortunately some namespace prefixes are not included,
  440. * because they are hardcoded, e.g. rdf.
  441. *
  442. * @access private
  443. * @return array Array with namespace prefix as value.
  444. */
  445. private function getNamespacePrefixes()
  446. {
  447. $prefixes = array();
  448. // Get all tag names from channel elements...
  449. $tags = array_keys($this->channels);
  450. // ... and now all names from feed items
  451. foreach ($this->items as $item)
  452. $tags = array_merge($tags, array_keys($item->getElements()));
  453. // Look for prefixes in those tag names
  454. foreach($tags as $tag)
  455. {
  456. $elements = explode(':', $tag);
  457. if (count($elements) != 2)
  458. continue;
  459. $prefixes[] = $elements[0];
  460. }
  461. return array_unique($prefixes);
  462. }
  463. /**
  464. * Returns the XML header and root element, depending on the feed type.
  465. *
  466. * @access private
  467. * @return void
  468. */
  469. private function makeHeader()
  470. {
  471. $out = '<?xml version="1.0" encoding="utf-8"?>' . PHP_EOL;
  472. $prefixes = $this->getNamespacePrefixes();
  473. $attributes = array();
  474. $tagName = '';
  475. $defaultNamespace = '';
  476. if($this->version == static::RSS2)
  477. {
  478. $tagName = 'rss';
  479. $attributes['version'] = '2.0';
  480. }
  481. elseif($this->version == static::RSS1)
  482. {
  483. $tagName = 'rdf:RDF';
  484. $prefixes[] = 'rdf';
  485. $defaultNamespace = $this->namespaces['rss1'];
  486. }
  487. else if($this->version == static::ATOM)
  488. {
  489. $tagName = 'feed';
  490. $defaultNamespace = $this->namespaces['atom'];
  491. // Ugly hack to remove the 'atom' value from the prefixes array.
  492. $prefixes = array_flip($prefixes);
  493. unset($prefixes['atom']);
  494. $prefixes = array_flip($prefixes);
  495. }
  496. // Iterate through every namespace prefix and add it to the element attributes.
  497. foreach($prefixes as $prefix)
  498. {
  499. if (!isset($this->namespaces[$prefix]))
  500. throw new Exception('Unknown XML namespace prefix: \'' . $prefix . '\'. Use the addNamespace method to add support for this prefix.');
  501. else
  502. $attributes['xmlns:' . $prefix] = $this->namespaces[$prefix];
  503. }
  504. // Include default namepsace, if required
  505. if (!empty($defaultNamespace))
  506. $attributes['xmlns'] = $defaultNamespace;
  507. $out .= $this->makeNode($tagName, '', $attributes, true);
  508. return $out;
  509. }
  510. /**
  511. * Closes the open tags at the end of file
  512. *
  513. * @access private
  514. * @return void
  515. */
  516. private function makeFooter()
  517. {
  518. if($this->version == static::RSS2)
  519. {
  520. return '</channel>' . PHP_EOL . '</rss>';
  521. }
  522. elseif($this->version == static::RSS1)
  523. {
  524. return '</rdf:RDF>';
  525. }
  526. else if($this->version == static::ATOM)
  527. {
  528. return '</feed>';
  529. }
  530. }
  531. /**
  532. * Creates a single node in XML format
  533. *
  534. * @access private
  535. * @param string name of the tag
  536. * @param mixed tag value as string or array of nested tags in 'tagName' => 'tagValue' format
  537. * @param array Attributes (if any) in 'attrName' => 'attrValue' format
  538. * @param string True if the end tag should be omitted. Defaults to false.
  539. * @return string formatted xml tag
  540. */
  541. private function makeNode($tagName, $tagContent, $attributes = null, $omitEndTag = false)
  542. {
  543. $nodeText = '';
  544. $attrText = '';
  545. if(is_array($attributes) && count($attributes) > 0)
  546. {
  547. foreach ($attributes as $key => $value)
  548. {
  549. $value = htmlspecialchars($value);
  550. $attrText .= " $key=\"$value\"";
  551. }
  552. }
  553. if(is_array($tagContent) && $this->version == static::RSS1)
  554. {
  555. $attrText = ' rdf:parseType="Resource"';
  556. }
  557. $attrText .= (in_array($tagName, $this->CDATAEncoding) && $this->version == static::ATOM) ? ' type="html"' : '';
  558. $nodeText .= "<{$tagName}{$attrText}>";
  559. $nodeText .= (in_array($tagName, $this->CDATAEncoding)) ? '<![CDATA[' : '';
  560. if(is_array($tagContent))
  561. {
  562. foreach ($tagContent as $key => $value)
  563. {
  564. $nodeText .= $this->makeNode($key, $value);
  565. }
  566. }
  567. else
  568. {
  569. $nodeText .= (in_array($tagName, $this->CDATAEncoding)) ? $this->sanitizeCDATA($tagContent) : htmlspecialchars($tagContent);
  570. }
  571. $nodeText .= (in_array($tagName, $this->CDATAEncoding)) ? ']]>' : '';
  572. if (!$omitEndTag)
  573. $nodeText .= "</$tagName>";
  574. $nodeText .= PHP_EOL;
  575. return $nodeText;
  576. }
  577. /**
  578. * Make the channels.
  579. *
  580. * @access private
  581. * @return void
  582. */
  583. private function makeChannels()
  584. {
  585. $out = '';
  586. //Start channel tag
  587. switch ($this->version)
  588. {
  589. case static::RSS2:
  590. $out .= '<channel>' . PHP_EOL;
  591. break;
  592. case static::RSS1:
  593. $out .= (isset($this->data['ChannelAbout']))? "<channel rdf:about=\"{$this->data['ChannelAbout']}\">" : "<channel rdf:about=\"{$this->channels['link']}\">";
  594. break;
  595. }
  596. //Print Items of channel
  597. foreach ($this->channels as $key => $value)
  598. {
  599. // ATOM feed needs some special handling
  600. if ($this->version == static::ATOM)
  601. {
  602. // Strip all ATOM namespace prefixes from tags. Not needed here, because the ATOM namespace name is
  603. // used as default namespace.
  604. if (strncmp($key, 'atom', 4) == 0)
  605. $key = substr($key, 5);
  606. if ($key == 'link')
  607. {
  608. if (is_array($value))
  609. {
  610. // $value is an array containing multiple atom:link element attributes
  611. foreach($value as $attributes)
  612. {
  613. // $attributes contains actually the node attributes, not the value.
  614. $out .= $this->makeNode($key, '', $attributes);
  615. }
  616. }
  617. else
  618. {
  619. // ATOM prints link element as href attribute
  620. $out .= $this->makeNode($key, '', array('href' => $value));
  621. //Add the id for ATOM
  622. $out .= $this->makeNode('id', static::uuid($value, 'urn:uuid:'));
  623. }
  624. }
  625. else
  626. $out .= $this->makeNode($key, $value);
  627. }
  628. else
  629. {
  630. if ($key == 'atom:link')
  631. {
  632. // $value is an array containing multiple atom:link element attributes
  633. foreach($value as $attributes)
  634. {
  635. // $attributes contains actually the node attributes, not the value.
  636. $out .= $this->makeNode($key, '', $attributes);
  637. }
  638. }
  639. else
  640. {
  641. $out .= $this->makeNode($key, $value);
  642. }
  643. }
  644. }
  645. //RSS 1.0 have special tag <rdf:Seq> with channel
  646. if($this->version == static::RSS1)
  647. {
  648. $out .= "<items>" . PHP_EOL . "<rdf:Seq>" . PHP_EOL;
  649. foreach ($this->items as $item)
  650. {
  651. $thisItems = $item->getElements();
  652. $out .= "<rdf:li resource=\"{$thisItems['link']['content']}\"/>" . PHP_EOL;
  653. }
  654. $out .= "</rdf:Seq>" . PHP_EOL . "</items>" . PHP_EOL . "</channel>" . PHP_EOL;
  655. }
  656. return $out;
  657. }
  658. /**
  659. * Prints formatted feed items
  660. *
  661. * @access private
  662. * @return void
  663. */
  664. private function makeItems()
  665. {
  666. $out = '';
  667. foreach ($this->items as $item)
  668. {
  669. $thisItems = $item->getElements();
  670. // the argument is printed as rdf:about attribute of item in rss 1.0
  671. $out .= $this->startItem($thisItems['link']['content']);
  672. foreach ($thisItems as $feedItem)
  673. {
  674. $name = $feedItem['name'];
  675. // Strip all ATOM namespace prefixes from tags when feed is an ATOM feed.
  676. // Not needed here, because the ATOM namespace name is used as default namespace.
  677. if ($this->version == static::ATOM && strncmp($name, 'atom', 4) == 0)
  678. $name = substr($name, 5);
  679. $out .= $this->makeNode($name, $feedItem['content'], $feedItem['attributes']);
  680. }
  681. $out .= $this->endItem();
  682. }
  683. return $out;
  684. }
  685. /**
  686. * Make the starting tag of channels
  687. *
  688. * @access private
  689. * @param string The vale of about tag which is used for RSS 1.0 only.
  690. * @return void
  691. */
  692. private function startItem($about = false)
  693. {
  694. $out = '';
  695. if($this->version == static::RSS2)
  696. {
  697. $out .= '<item>' . PHP_EOL;
  698. }
  699. else if($this->version == static::RSS1)
  700. {
  701. if($about)
  702. {
  703. $out .= "<item rdf:about=\"$about\">" . PHP_EOL;
  704. }
  705. else
  706. {
  707. throw new Exception("link element is not set - It's required for RSS 1.0 to be used as the about attribute of the item tag.");
  708. }
  709. }
  710. else if($this->version == static::ATOM)
  711. {
  712. $out .= "<entry>" . PHP_EOL;
  713. }
  714. return $out;
  715. }
  716. /**
  717. * Closes feed item tag
  718. *
  719. * @access private
  720. * @return void
  721. */
  722. private function endItem()
  723. {
  724. if($this->version == static::RSS2 || $this->version == static::RSS1)
  725. {
  726. return '</item>' . PHP_EOL;
  727. }
  728. else if($this->version == static::ATOM)
  729. {
  730. return '</entry>' . PHP_EOL;
  731. }
  732. }
  733. /**
  734. * Sanitizes data which will be later on returned as CDATA in the feed.
  735. *
  736. * A "]]>" respectively "<![CDATA" in the data would break the CDATA in the
  737. * XML, so the brackets are converted to a HTML entity.
  738. *
  739. * @access private
  740. * @param string Data to be sanitized
  741. * @return string Sanitized data
  742. */
  743. private function sanitizeCDATA($text)
  744. {
  745. $text = str_replace("]]>", "]]&gt;", $text);
  746. $text = str_replace("<![CDATA[", "&lt;![CDATA[", $text);
  747. return $text;
  748. }
  749. // End # private functions ----------------------------------------------
  750. } // end of class Feed