Highway to PSR2
[openemr.git] / portal / patient / fwk / libs / verysimple / RSS / Writer.php
blobe1de3f1eee157fb78632cc2543069fe1419cc79e
1 <?php
2 /** @package verysimple::RSS */
3 require_once("verysimple/XML/XMLUtil.php");
5 /**
6 * A API for writing RSS feeds
8 * @package verysimple::RSS
9 * @link http://www.110mb.com/forum/how-to-make-a-rss-feed-with-php-t11030.0.html
10 * @version 1.1
11 * @example require_once('verysimple/RSS/Writer.php');
12 * $rss_writer = new RSS_Writer('Channel','www.dev.nul','Feed Title');
13 * $rss_writer->setLanguage('us-en');
14 * $rss_writer->addCategory("Category Name");
15 * $rss_writer->addItem("Title","http://...",'Category Name','Author',date(DATE_RSS,strtotime('2008-01-04: 01:01:01')));
16 * $rss_writer->writeOut();
18 class RSS_Writer
20 protected $channel;
21 private $title;
22 private $link;
23 private $description;
24 private $rss_dom;
25 private $rss_simplexml;
26 protected $item_list;
27 protected $item_counter;
29 /**
30 * Instantiate an RSS_Writer
32 * @param string $channel_title
33 * @param string $channel_link
34 * @param string $channel_description
35 * @param string $generator
36 * [optional]
37 * @param string $docs
38 * [optional]
40 public function __construct($channel_title, $channel_link, $channel_description, $generator = "RSS_Writer", $docs = "http://cyber.law.harvard.edu/rss/rss.html")
42 $this->item_list = array ();
43 $this->item_counter = 0;
44 $this->rss_dom = new DOMDocument('1.0', 'UTF-8');
45 $rss_node = $this->elementCreate($this->rss_dom, 'rss', '', array (
46 'version' => '2.0'
47 ));
48 $this->channel = $this->elementCreate($rss_node, 'channel');
49 $this->title = $this->elementCreate($this->channel, 'title', $channel_title);
50 $this->link = $this->elementCreate($this->channel, 'link', $channel_link);
51 $this->description = $this->elementCreate($this->channel, 'description', $channel_description);
52 $this->elementCreate($this->channel, 'lastBuildDate', date(DATE_RSS), false, false);
53 $this->elementCreate($this->channel, 'generator', $generator, false, false);
54 $this->elementCreate($this->channel, 'docs', $docs, false, false);
55 $this->rss_simplexml = simplexml_import_dom($this->rss_dom);
57 private function elementCreate($parent_node, $node_name, $content = '', $attributes = false, $return = true)
59 $content = XMLUtil::Escape($content);
61 $element = $this->rss_dom->createElement($node_name, $content);
62 if ($attributes) {
63 foreach ($attributes as $key => $value) {
64 $element->setAttribute($key, $value);
68 $parent_node->appendChild($element);
69 if ($return) {
70 return $element;
71 } else {
72 return false;
75 protected function elementSet($parent_node, $xpath, $node_name, $value, $attributes = false)
77 $result = $this->rss_simplexml->xpath($xpath . $node_name);
78 if ($result) {
79 $result [0] = $value;
81 if ($attributes) {
82 foreach ($attributes as $key => $value) {
83 $result [0] [$key] = $value;
86 } else {
87 $this->elementCreate($parent_node, $node_name, $value, $attributes, false);
90 public function setLanguage($language = 'en-us')
92 $this->elementSet($this->channel, '/channel/', 'language', $language);
94 public function addCategory($category)
96 $this->elementCreate($this->channel, 'category', $category);
98 public function addImage($url, $width = 88, $height = 31, $description = 'channel image')
100 $image = $this->elementCreate($this->channel, 'image');
101 $this->elementCreate($image, 'url', $url);
103 if ($width > 144) {
104 $width = 144;
107 if ($height > 400) {
108 $height = 400;
111 $this->elementCreate($image, 'width', $width);
112 $this->elementCreate($image, 'height', $height);
114 $this->elementCreate($image, 'title', $this->title->nodeValue);
115 $this->elementCreate($image, 'link', $this->link->nodeValue);
117 $this->elementCreate($image, 'description', $description);
119 public function addItem($title, $link, $description, $author, $date, $source = '', $guid = '')
121 $this->item_counter ++;
122 if (! $guid) {
123 $guid = 'item' . $this->item_counter;
126 $item_element = $this->elementCreate($this->channel, 'item', '', false, true);
127 $this->elementCreate($item_element, 'title', $title);
128 $this->elementCreate($item_element, 'link', $link);
129 $this->elementCreate($item_element, 'description', $description);
130 $this->elementCreate($item_element, 'author', $author);
131 $this->elementCreate($item_element, 'guid', $guid);
132 $this->elementCreate($item_element, 'pubDate', $date);
133 if ($source) {
134 $this->elementCreate($item_element, 'source', $source);
137 $this->item_list [$this->item_counter] = $item_element;
139 public function addEnclosure($url, $type)
141 $item_element = $this->item_list [$this->item_counter];
142 $curl = curl_init($url);
143 if (! $curl || ! $item_element) {
144 return false;
145 } else {
146 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
147 curl_setopt($curl, CURLOPT_HEADER, 1);
148 curl_setopt($curl, CURLOPT_NOBODY, 1);
149 curl_exec($curl);
150 $size = curl_getinfo($curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
151 curl_close($curl);
154 $this->elementCreate($item_element, 'enclosure', '', array (
155 'url' => $url,
156 'length' => $size,
157 'type' => $type
158 ), false);
162 * returns RSS xml
164 * @return string RSS
167 public function returnXML()
169 return $this->rss_dom->saveXML();
173 * Writes out the XML header and content to the browser
175 public function writeOut()
177 header("Content-Type: text/xml");
178 echo $this->returnXML();