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