* Genarate RSS2 or JSON (original: RSS 1.0, RSS2.0 and ATOM Feed)\r
*\r
* Modified for FiveFilters.org's Full-Text RSS project\r
- * to allow for inclusion of hubs, JSON output. \r
+ * to allow for inclusion of hubs, JSON output.\r
* Stripped RSS1 and ATOM support.\r
- * \r
+ *\r
* @package UnivarselFeedWriter\r
* @author Anis uddin Ahmad <anisniit@gmail.com>\r
* @link http://www.ajaxray.com/projects/rss\r
private $CDATAEncoding = array(); // The tag names which have to encoded as CDATA\r
private $xsl = null; // stylesheet to render RSS (used by Chrome)\r
private $json = null; // JSON object\r
- \r
- private $version = null; \r
- \r
+\r
+ private $version = null;\r
+\r
/**\r
* Constructor\r
- * \r
- * @param constant the version constant (RSS2 or JSON). \r
- */ \r
+ *\r
+ * @param constant the version constant (RSS2 or JSON).\r
+ */\r
function __construct($version = RSS2)\r
- { \r
+ {\r
$this->version = $version;\r
- \r
+\r
// Setting default value for assential channel elements\r
$this->channels['title'] = $version . ' Feed';\r
$this->channels['link'] = 'http://www.ajaxray.com/blog';\r
- \r
+\r
//Tag names to encode in CDATA\r
$this->CDATAEncoding = array('description', 'content:encoded', 'content', 'subtitle', 'summary');\r
}\r
- \r
+\r
public function setFormat($format) {\r
$this->version = $format;\r
}\r
\r
// Start # public functions ---------------------------------------------\r
- \r
+\r
/**\r
* Set a channel element\r
* @access public\r
{\r
$this->channels[$elementName] = $content ;\r
}\r
- \r
+\r
/**\r
- * Set multiple channel elements from an array. Array elements \r
+ * Set multiple channel elements from an array. Array elements\r
* should be 'channelName' => 'channelContent' format.\r
- * \r
+ *\r
* @access public\r
* @param array array of channels\r
* @return void\r
public function setChannelElementsFromArray($elementArray)\r
{\r
if(! is_array($elementArray)) return;\r
- foreach ($elementArray as $elementName => $content) \r
+ foreach ($elementArray as $elementName => $content)\r
{\r
$this->setChannelElement($elementName, $content);\r
}\r
}\r
- \r
+\r
/**\r
* Genarate the actual RSS/JSON file\r
- * \r
+ *\r
* @access public\r
* @return void\r
- */ \r
+ */\r
public function genarateFeed()\r
{\r
if ($this->version == RSS2) {\r
- header('Content-type: text/xml; charset=UTF-8');\r
+// header('Content-type: text/xml; charset=UTF-8');\r
// this line prevents Chrome 20 from prompting download\r
// used by Google: https://news.google.com/news/feeds?ned=us&topic=b&output=rss\r
- header('X-content-type-options: nosniff');\r
+// header('X-content-type-options: nosniff');\r
} elseif ($this->version == JSON) {\r
- header('Content-type: application/json; charset=UTF-8');\r
+// header('Content-type: application/json; charset=UTF-8');\r
$this->json = new stdClass();\r
} elseif ($this->version == JSONP) {\r
- header('Content-type: application/javascript; charset=UTF-8');\r
+// header('Content-type: application/javascript; charset=UTF-8');\r
$this->json = new stdClass();\r
}\r
$this->printHead();\r
echo json_encode($this->json);\r
}\r
}\r
- \r
+\r
/**\r
* Create a new FeedItem.\r
- * \r
+ *\r
* @access public\r
* @return object instance of FeedItem class\r
*/\r
$Item = new FeedItem($this->version);\r
return $Item;\r
}\r
- \r
+\r
/**\r
* Add a FeedItem to the main class\r
- * \r
+ *\r
* @access public\r
* @param object instance of FeedItem class\r
* @return void\r
*/\r
public function addItem($feedItem)\r
{\r
- $this->items[] = $feedItem; \r
+ $this->items[] = $feedItem;\r
}\r
- \r
+\r
// Wrapper functions -------------------------------------------------------------------\r
- \r
+\r
/**\r
* Set the 'title' channel element\r
- * \r
+ *\r
* @access public\r
* @param srting value of 'title' channel tag\r
* @return void\r
{\r
$this->setChannelElement('title', $title);\r
}\r
- \r
+\r
/**\r
* Add a hub to the channel element\r
- * \r
+ *\r
* @access public\r
* @param string URL\r
* @return void\r
*/\r
public function addHub($hub)\r
{\r
- $this->hubs[] = $hub; \r
+ $this->hubs[] = $hub;\r
}\r
- \r
+\r
/**\r
* Set XSL URL\r
- * \r
+ *\r
* @access public\r
* @param string URL\r
* @return void\r
*/\r
public function setXsl($xsl)\r
{\r
- $this->xsl = $xsl; \r
- } \r
- \r
+ $this->xsl = $xsl;\r
+ }\r
+\r
/**\r
* Set self URL\r
- * \r
+ *\r
* @access public\r
* @param string URL\r
* @return void\r
*/\r
public function setSelf($self)\r
{\r
- $this->self = $self; \r
- } \r
- \r
+ $this->self = $self;\r
+ }\r
+\r
/**\r
* Set the 'description' channel element\r
- * \r
+ *\r
* @access public\r
* @param srting value of 'description' channel tag\r
* @return void\r
*/\r
public function setDescription($desciption)\r
{\r
- $tag = ($this->version == ATOM)? 'subtitle' : 'description'; \r
+ $tag = ($this->version == ATOM)? 'subtitle' : 'description';\r
$this->setChannelElement($tag, $desciption);\r
}\r
- \r
+\r
/**\r
* Set the 'link' channel element\r
- * \r
+ *\r
* @access public\r
* @param srting value of 'link' channel tag\r
* @return void\r
{\r
$this->setChannelElement('link', $link);\r
}\r
- \r
+\r
/**\r
* Set the 'image' channel element\r
- * \r
+ *\r
* @access public\r
* @param srting title of image\r
* @param srting link url of the imahe\r
{\r
$this->setChannelElement('image', array('title'=>$title, 'link'=>$link, 'url'=>$url));\r
}\r
- \r
+\r
// End # public functions ----------------------------------------------\r
- \r
+\r
// Start # private functions ----------------------------------------------\r
- \r
+\r
/**\r
* Prints the xml and rss namespace\r
- * \r
+ *\r
* @access private\r
* @return void\r
*/\r
$this->json->rss = array('@attributes' => array('version' => '2.0'));\r
}\r
}\r
- \r
+\r
/**\r
* Closes the open tags at the end of file\r
- * \r
+ *\r
* @access private\r
* @return void\r
*/\r
{\r
if ($this->version == RSS2)\r
{\r
- echo '</channel>',PHP_EOL,'</rss>'; \r
- } \r
+ echo '</channel>',PHP_EOL,'</rss>';\r
+ }\r
// do nothing for JSON\r
}\r
\r
/**\r
* Creates a single node as xml format\r
- * \r
+ *\r
* @access private\r
* @param string name of the tag\r
* @param mixed tag value as string or array of nested tags in 'tagName' => 'tagValue' format\r
* @return string formatted xml tag\r
*/\r
private function makeNode($tagName, $tagContent, $attributes = null)\r
- { \r
+ {\r
if ($this->version == RSS2)\r
{\r
$nodeText = '';\r
$attrText = '';\r
if (is_array($attributes))\r
{\r
- foreach ($attributes as $key => $value) \r
+ foreach ($attributes as $key => $value)\r
{\r
$attrText .= " $key=\"$value\" ";\r
}\r
}\r
$nodeText .= "<{$tagName}{$attrText}>";\r
if (is_array($tagContent))\r
- { \r
- foreach ($tagContent as $key => $value) \r
+ {\r
+ foreach ($tagContent as $key => $value)\r
{\r
$nodeText .= $this->makeNode($key, $value);\r
}\r
{\r
//$nodeText .= (in_array($tagName, $this->CDATAEncoding))? $tagContent : htmlentities($tagContent);\r
$nodeText .= htmlspecialchars($tagContent);\r
- } \r
+ }\r
//$nodeText .= (in_array($tagName, $this->CDATAEncoding))? "]]></$tagName>" : "</$tagName>";\r
$nodeText .= "</$tagName>";\r
return $nodeText . PHP_EOL;\r
}\r
return ''; // should not get here\r
}\r
- \r
+\r
private function json_keys(array $array) {\r
$new = array();\r
foreach ($array as $key => $val) {\r
}\r
return $new;\r
}\r
- \r
+\r
/**\r
* @desc Print channels\r
* @access private\r
{\r
//Start channel tag\r
if ($this->version == RSS2) {\r
- echo '<channel>' . PHP_EOL; \r
+ echo '<channel>' . PHP_EOL;\r
// add hubs\r
foreach ($this->hubs as $hub) {\r
//echo $this->makeNode('link', '', array('rel'=>'hub', 'href'=>$hub, 'xmlns'=>'http://www.w3.org/2005/Atom'));\r
echo '<link rel="self" href="'.htmlspecialchars($this->self).'" xmlns="http://www.w3.org/2005/Atom" />' . PHP_EOL;\r
}\r
//Print Items of channel\r
- foreach ($this->channels as $key => $value) \r
+ foreach ($this->channels as $key => $value)\r
{\r
echo $this->makeNode($key, $value);\r
}\r
$this->json->rss['channel'] = (object)$this->json_keys($this->channels);\r
}\r
}\r
- \r
+\r
/**\r
* Prints formatted feed items\r
- * \r
+ *\r
* @access private\r
* @return void\r
*/\r
private function printItems()\r
- { \r
+ {\r
foreach ($this->items as $item) {\r
$itemElements = $item->getElements();\r
- \r
+\r
echo $this->startItem();\r
- \r
+\r
if ($this->version == JSON || $this->version == JSONP) {\r
$json_item = array();\r
}\r
- \r
+\r
foreach ($itemElements as $thisElement) {\r
- foreach ($thisElement as $instance) { \r
+ foreach ($thisElement as $instance) {\r
if ($this->version == RSS2) {\r
echo $this->makeNode($instance['name'], $instance['content'], $instance['attributes']);\r
} elseif ($this->version == JSON || $this->version == JSONP) {\r
}\r
}\r
}\r
- \r
+\r
/**\r
* Make the starting tag of channels\r
- * \r
+ *\r
* @access private\r
* @return void\r
*/\r
{\r
if ($this->version == RSS2)\r
{\r
- echo '<item>' . PHP_EOL; \r
- } \r
+ echo '<item>' . PHP_EOL;\r
+ }\r
// nothing for JSON\r
}\r
- \r
+\r
/**\r
* Closes feed item tag\r
- * \r
+ *\r
* @access private\r
* @return void\r
*/\r
{\r
if ($this->version == RSS2)\r
{\r
- echo '</item>' . PHP_EOL; \r
- } \r
+ echo '</item>' . PHP_EOL;\r
+ }\r
// nothing for JSON\r
}\r
- \r
+\r
// End # private functions ----------------------------------------------\r
}
\ No newline at end of file