]> git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/3rdparty/libraries/feedwriter/FeedWriter.php
Merge pull request #609 from wallabag/fix-desciption-typo
[github/wallabag/wallabag.git] / inc / 3rdparty / libraries / feedwriter / FeedWriter.php
1 <?php
2 define('RSS2', 1, true);
3 define('JSON', 2, true);
4 define('JSONP', 3, true);
5
6 /**
7 * Univarsel Feed Writer class
8 *
9 * Genarate RSS2 or JSON (original: RSS 1.0, RSS2.0 and ATOM Feed)
10 *
11 * Modified for FiveFilters.org's Full-Text RSS project
12 * to allow for inclusion of hubs, JSON output.
13 * Stripped RSS1 and ATOM support.
14 *
15 * @package UnivarselFeedWriter
16 * @author Anis uddin Ahmad <anisniit@gmail.com>
17 * @link http://www.ajaxray.com/projects/rss
18 */
19 class FeedWriter
20 {
21 private $self = null; // self URL - http://feed2.w3.org/docs/warning/MissingAtomSelfLink.html
22 private $hubs = array(); // PubSubHubbub hubs
23 private $channels = array(); // Collection of channel elements
24 private $items = array(); // Collection of items as object of FeedItem class.
25 private $data = array(); // Store some other version wise data
26 private $CDATAEncoding = array(); // The tag names which have to encoded as CDATA
27 private $xsl = null; // stylesheet to render RSS (used by Chrome)
28 private $json = null; // JSON object
29
30 private $version = null;
31
32 /**
33 * Constructor
34 *
35 * @param constant the version constant (RSS2 or JSON).
36 */
37 function __construct($version = RSS2)
38 {
39 $this->version = $version;
40
41 // Setting default value for assential channel elements
42 $this->channels['title'] = $version . ' Feed';
43 $this->channels['link'] = 'http://www.ajaxray.com/blog';
44
45 //Tag names to encode in CDATA
46 $this->CDATAEncoding = array('description', 'content:encoded', 'content', 'subtitle', 'summary');
47 }
48
49 public function setFormat($format) {
50 $this->version = $format;
51 }
52
53 // Start # public functions ---------------------------------------------
54
55 /**
56 * Set a channel element
57 * @access public
58 * @param srting name of the channel tag
59 * @param string content of the channel tag
60 * @return void
61 */
62 public function setChannelElement($elementName, $content)
63 {
64 $this->channels[$elementName] = $content ;
65 }
66
67 /**
68 * Set multiple channel elements from an array. Array elements
69 * should be 'channelName' => 'channelContent' format.
70 *
71 * @access public
72 * @param array array of channels
73 * @return void
74 */
75 public function setChannelElementsFromArray($elementArray)
76 {
77 if(! is_array($elementArray)) return;
78 foreach ($elementArray as $elementName => $content)
79 {
80 $this->setChannelElement($elementName, $content);
81 }
82 }
83
84 /**
85 * Genarate the actual RSS/JSON file
86 *
87 * @access public
88 * @return void
89 */
90 public function genarateFeed()
91 {
92 if ($this->version == RSS2) {
93 // header('Content-type: text/xml; charset=UTF-8');
94 // this line prevents Chrome 20 from prompting download
95 // used by Google: https://news.google.com/news/feeds?ned=us&topic=b&output=rss
96 // header('X-content-type-options: nosniff');
97 } elseif ($this->version == JSON) {
98 // header('Content-type: application/json; charset=UTF-8');
99 $this->json = new stdClass();
100 } elseif ($this->version == JSONP) {
101 // header('Content-type: application/javascript; charset=UTF-8');
102 $this->json = new stdClass();
103 }
104 $this->printHead();
105 $this->printChannels();
106 $this->printItems();
107 $this->printTale();
108 if ($this->version == JSON || $this->version == JSONP) {
109 echo json_encode($this->json);
110 }
111 }
112
113 /**
114 * Create a new FeedItem.
115 *
116 * @access public
117 * @return object instance of FeedItem class
118 */
119 public function createNewItem()
120 {
121 $Item = new FeedItem($this->version);
122 return $Item;
123 }
124
125 /**
126 * Add a FeedItem to the main class
127 *
128 * @access public
129 * @param object instance of FeedItem class
130 * @return void
131 */
132 public function addItem($feedItem)
133 {
134 $this->items[] = $feedItem;
135 }
136
137 // Wrapper functions -------------------------------------------------------------------
138
139 /**
140 * Set the 'title' channel element
141 *
142 * @access public
143 * @param srting value of 'title' channel tag
144 * @return void
145 */
146 public function setTitle($title)
147 {
148 $this->setChannelElement('title', $title);
149 }
150
151 /**
152 * Add a hub to the channel element
153 *
154 * @access public
155 * @param string URL
156 * @return void
157 */
158 public function addHub($hub)
159 {
160 $this->hubs[] = $hub;
161 }
162
163 /**
164 * Set XSL URL
165 *
166 * @access public
167 * @param string URL
168 * @return void
169 */
170 public function setXsl($xsl)
171 {
172 $this->xsl = $xsl;
173 }
174
175 /**
176 * Set self URL
177 *
178 * @access public
179 * @param string URL
180 * @return void
181 */
182 public function setSelf($self)
183 {
184 $this->self = $self;
185 }
186
187 /**
188 * Set the 'description' channel element
189 *
190 * @access public
191 * @param srting value of 'description' channel tag
192 * @return void
193 */
194 public function setDescription($description)
195 {
196 $this->setChannelElement('description', $description);
197 }
198
199 /**
200 * Set the 'link' channel element
201 *
202 * @access public
203 * @param srting value of 'link' channel tag
204 * @return void
205 */
206 public function setLink($link)
207 {
208 $this->setChannelElement('link', $link);
209 }
210
211 /**
212 * Set the 'image' channel element
213 *
214 * @access public
215 * @param srting title of image
216 * @param srting link url of the imahe
217 * @param srting path url of the image
218 * @return void
219 */
220 public function setImage($title, $link, $url)
221 {
222 $this->setChannelElement('image', array('title'=>$title, 'link'=>$link, 'url'=>$url));
223 }
224
225 // End # public functions ----------------------------------------------
226
227 // Start # private functions ----------------------------------------------
228
229 /**
230 * Prints the xml and rss namespace
231 *
232 * @access private
233 * @return void
234 */
235 private function printHead()
236 {
237 if ($this->version == RSS2)
238 {
239 $out = '<?xml version="1.0" encoding="utf-8"?>'."\n";
240 if ($this->xsl) $out .= '<?xml-stylesheet type="text/xsl" href="'.htmlspecialchars($this->xsl).'"?>' . PHP_EOL;
241 $out .= '<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/">' . PHP_EOL;
242 echo $out;
243 }
244 elseif ($this->version == JSON || $this->version == JSONP)
245 {
246 $this->json->rss = array('@attributes' => array('version' => '2.0'));
247 }
248 }
249
250 /**
251 * Closes the open tags at the end of file
252 *
253 * @access private
254 * @return void
255 */
256 private function printTale()
257 {
258 if ($this->version == RSS2)
259 {
260 echo '</channel>',PHP_EOL,'</rss>';
261 }
262 // do nothing for JSON
263 }
264
265 /**
266 * Creates a single node as xml format
267 *
268 * @access private
269 * @param string name of the tag
270 * @param mixed tag value as string or array of nested tags in 'tagName' => 'tagValue' format
271 * @param array Attributes(if any) in 'attrName' => 'attrValue' format
272 * @return string formatted xml tag
273 */
274 private function makeNode($tagName, $tagContent, $attributes = null)
275 {
276 if ($this->version == RSS2)
277 {
278 $nodeText = '';
279 $attrText = '';
280 if (is_array($attributes))
281 {
282 foreach ($attributes as $key => $value)
283 {
284 $attrText .= " $key=\"$value\" ";
285 }
286 }
287 $nodeText .= "<{$tagName}{$attrText}>";
288 if (is_array($tagContent))
289 {
290 foreach ($tagContent as $key => $value)
291 {
292 $nodeText .= $this->makeNode($key, $value);
293 }
294 }
295 else
296 {
297 //$nodeText .= (in_array($tagName, $this->CDATAEncoding))? $tagContent : htmlentities($tagContent);
298 $nodeText .= htmlspecialchars($tagContent);
299 }
300 //$nodeText .= (in_array($tagName, $this->CDATAEncoding))? "]]></$tagName>" : "</$tagName>";
301 $nodeText .= "</$tagName>";
302 return $nodeText . PHP_EOL;
303 }
304 elseif ($this->version == JSON || $this->version == JSONP)
305 {
306 $tagName = (string)$tagName;
307 $tagName = strtr($tagName, ':', '_');
308 $node = null;
309 if (!$tagContent && is_array($attributes) && count($attributes))
310 {
311 $node = array('@attributes' => $this->json_keys($attributes));
312 } else {
313 if (is_array($tagContent)) {
314 $node = $this->json_keys($tagContent);
315 } else {
316 $node = $tagContent;
317 }
318 }
319 return $node;
320 }
321 return ''; // should not get here
322 }
323
324 private function json_keys(array $array) {
325 $new = array();
326 foreach ($array as $key => $val) {
327 if (is_string($key)) $key = strtr($key, ':', '_');
328 if (is_array($val)) {
329 $new[$key] = $this->json_keys($val);
330 } else {
331 $new[$key] = $val;
332 }
333 }
334 return $new;
335 }
336
337 /**
338 * @desc Print channels
339 * @access private
340 * @return void
341 */
342 private function printChannels()
343 {
344 //Start channel tag
345 if ($this->version == RSS2) {
346 echo '<channel>' . PHP_EOL;
347 // add hubs
348 foreach ($this->hubs as $hub) {
349 //echo $this->makeNode('link', '', array('rel'=>'hub', 'href'=>$hub, 'xmlns'=>'http://www.w3.org/2005/Atom'));
350 echo '<link rel="hub" href="'.htmlspecialchars($hub).'" xmlns="http://www.w3.org/2005/Atom" />' . PHP_EOL;
351 }
352 // add self
353 if (isset($this->self)) {
354 //echo $this->makeNode('link', '', array('rel'=>'self', 'href'=>$this->self, 'xmlns'=>'http://www.w3.org/2005/Atom'));
355 echo '<link rel="self" href="'.htmlspecialchars($this->self).'" xmlns="http://www.w3.org/2005/Atom" />' . PHP_EOL;
356 }
357 //Print Items of channel
358 foreach ($this->channels as $key => $value)
359 {
360 echo $this->makeNode($key, $value);
361 }
362 } elseif ($this->version == JSON || $this->version == JSONP) {
363 $this->json->rss['channel'] = (object)$this->json_keys($this->channels);
364 }
365 }
366
367 /**
368 * Prints formatted feed items
369 *
370 * @access private
371 * @return void
372 */
373 private function printItems()
374 {
375 foreach ($this->items as $item) {
376 $itemElements = $item->getElements();
377
378 echo $this->startItem();
379
380 if ($this->version == JSON || $this->version == JSONP) {
381 $json_item = array();
382 }
383
384 foreach ($itemElements as $thisElement) {
385 foreach ($thisElement as $instance) {
386 if ($this->version == RSS2) {
387 echo $this->makeNode($instance['name'], $instance['content'], $instance['attributes']);
388 } elseif ($this->version == JSON || $this->version == JSONP) {
389 $_json_node = $this->makeNode($instance['name'], $instance['content'], $instance['attributes']);
390 if (count($thisElement) > 1) {
391 $json_item[strtr($instance['name'], ':', '_')][] = $_json_node;
392 } else {
393 $json_item[strtr($instance['name'], ':', '_')] = $_json_node;
394 }
395 }
396 }
397 }
398 echo $this->endItem();
399 if ($this->version == JSON || $this->version == JSONP) {
400 if (count($this->items) > 1) {
401 $this->json->rss['channel']->item[] = $json_item;
402 } else {
403 $this->json->rss['channel']->item = $json_item;
404 }
405 }
406 }
407 }
408
409 /**
410 * Make the starting tag of channels
411 *
412 * @access private
413 * @return void
414 */
415 private function startItem()
416 {
417 if ($this->version == RSS2)
418 {
419 echo '<item>' . PHP_EOL;
420 }
421 // nothing for JSON
422 }
423
424 /**
425 * Closes feed item tag
426 *
427 * @access private
428 * @return void
429 */
430 private function endItem()
431 {
432 if ($this->version == RSS2)
433 {
434 echo '</item>' . PHP_EOL;
435 }
436 // nothing for JSON
437 }
438
439 // End # private functions ----------------------------------------------
440 }