]> git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/3rdparty/libraries/feedwriter/FeedWriter.php
d708e99b0ec36e12dce2dcc6309410f3f8e598dc
[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($withHeaders = true)
91 {
92 if ($withHeaders) {
93 if ($this->version == RSS2) {
94 header('Content-type: text/xml; charset=UTF-8');
95 // this line prevents Chrome 20 from prompting download
96 // used by Google: https://news.google.com/news/feeds?ned=us&topic=b&output=rss
97 header('X-content-type-options: nosniff');
98 } elseif ($this->version == JSON) {
99 header('Content-type: application/json; charset=UTF-8');
100 } elseif ($this->version == JSONP) {
101 header('Content-type: application/javascript; charset=UTF-8');
102 }
103 }
104
105 if ($this->version == JSON || $this->version == JSONP) {
106 $this->json = new stdClass();
107 }
108
109
110 $this->printHead();
111 $this->printChannels();
112 $this->printItems();
113 $this->printTale();
114 if ($this->version == JSON || $this->version == JSONP) {
115 echo json_encode($this->json);
116 }
117 }
118
119 /**
120 * Create a new FeedItem.
121 *
122 * @access public
123 * @return object instance of FeedItem class
124 */
125 public function createNewItem()
126 {
127 $Item = new FeedItem($this->version);
128 return $Item;
129 }
130
131 /**
132 * Add a FeedItem to the main class
133 *
134 * @access public
135 * @param object instance of FeedItem class
136 * @return void
137 */
138 public function addItem($feedItem)
139 {
140 $this->items[] = $feedItem;
141 }
142
143 // Wrapper functions -------------------------------------------------------------------
144
145 /**
146 * Set the 'title' channel element
147 *
148 * @access public
149 * @param srting value of 'title' channel tag
150 * @return void
151 */
152 public function setTitle($title)
153 {
154 $this->setChannelElement('title', $title);
155 }
156
157 /**
158 * Add a hub to the channel element
159 *
160 * @access public
161 * @param string URL
162 * @return void
163 */
164 public function addHub($hub)
165 {
166 $this->hubs[] = $hub;
167 }
168
169 /**
170 * Set XSL URL
171 *
172 * @access public
173 * @param string URL
174 * @return void
175 */
176 public function setXsl($xsl)
177 {
178 $this->xsl = $xsl;
179 }
180
181 /**
182 * Set self URL
183 *
184 * @access public
185 * @param string URL
186 * @return void
187 */
188 public function setSelf($self)
189 {
190 $this->self = $self;
191 }
192
193 /**
194 * Set the 'description' channel element
195 *
196 * @access public
197 * @param srting value of 'description' channel tag
198 * @return void
199 */
200 public function setDescription($description)
201 {
202 $this->setChannelElement('description', $description);
203 }
204
205 /**
206 * Set the 'link' channel element
207 *
208 * @access public
209 * @param srting value of 'link' channel tag
210 * @return void
211 */
212 public function setLink($link)
213 {
214 $this->setChannelElement('link', $link);
215 }
216
217 /**
218 * Set the 'image' channel element
219 *
220 * @access public
221 * @param srting title of image
222 * @param srting link url of the imahe
223 * @param srting path url of the image
224 * @return void
225 */
226 public function setImage($title, $link, $url)
227 {
228 $this->setChannelElement('image', array('title'=>$title, 'link'=>$link, 'url'=>$url));
229 }
230
231 // End # public functions ----------------------------------------------
232
233 // Start # private functions ----------------------------------------------
234
235 /**
236 * Prints the xml and rss namespace
237 *
238 * @access private
239 * @return void
240 */
241 private function printHead()
242 {
243 if ($this->version == RSS2)
244 {
245 $out = '<?xml version="1.0" encoding="utf-8"?>'."\n";
246 if ($this->xsl) $out .= '<?xml-stylesheet type="text/xsl" href="'.htmlspecialchars($this->xsl).'"?>' . PHP_EOL;
247 $out .= '<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/">' . PHP_EOL;
248 echo $out;
249 }
250 elseif ($this->version == JSON || $this->version == JSONP)
251 {
252 $this->json->rss = array('@attributes' => array('version' => '2.0'));
253 }
254 }
255
256 /**
257 * Closes the open tags at the end of file
258 *
259 * @access private
260 * @return void
261 */
262 private function printTale()
263 {
264 if ($this->version == RSS2)
265 {
266 echo '</channel>',PHP_EOL,'</rss>';
267 }
268 // do nothing for JSON
269 }
270
271 /**
272 * Creates a single node as xml format
273 *
274 * @access private
275 * @param string name of the tag
276 * @param mixed tag value as string or array of nested tags in 'tagName' => 'tagValue' format
277 * @param array Attributes(if any) in 'attrName' => 'attrValue' format
278 * @return string formatted xml tag
279 */
280 private function makeNode($tagName, $tagContent, $attributes = null)
281 {
282 if ($this->version == RSS2)
283 {
284 $nodeText = '';
285 $attrText = '';
286 if (is_array($attributes))
287 {
288 foreach ($attributes as $key => $value)
289 {
290 $attrText .= " $key=\"$value\" ";
291 }
292 }
293 $nodeText .= "<{$tagName}{$attrText}>";
294 if (is_array($tagContent))
295 {
296 foreach ($tagContent as $key => $value)
297 {
298 $nodeText .= $this->makeNode($key, $value);
299 }
300 }
301 else
302 {
303 //$nodeText .= (in_array($tagName, $this->CDATAEncoding))? $tagContent : htmlentities($tagContent);
304 $nodeText .= htmlspecialchars($tagContent);
305 }
306 //$nodeText .= (in_array($tagName, $this->CDATAEncoding))? "]]></$tagName>" : "</$tagName>";
307 $nodeText .= "</$tagName>";
308 return $nodeText . PHP_EOL;
309 }
310 elseif ($this->version == JSON || $this->version == JSONP)
311 {
312 $tagName = (string)$tagName;
313 $tagName = strtr($tagName, ':', '_');
314 $node = null;
315 if (!$tagContent && is_array($attributes) && count($attributes))
316 {
317 $node = array('@attributes' => $this->json_keys($attributes));
318 } else {
319 if (is_array($tagContent)) {
320 $node = $this->json_keys($tagContent);
321 } else {
322 $node = $tagContent;
323 }
324 }
325 return $node;
326 }
327 return ''; // should not get here
328 }
329
330 private function json_keys(array $array) {
331 $new = array();
332 foreach ($array as $key => $val) {
333 if (is_string($key)) $key = strtr($key, ':', '_');
334 if (is_array($val)) {
335 $new[$key] = $this->json_keys($val);
336 } else {
337 $new[$key] = $val;
338 }
339 }
340 return $new;
341 }
342
343 /**
344 * @desc Print channels
345 * @access private
346 * @return void
347 */
348 private function printChannels()
349 {
350 //Start channel tag
351 if ($this->version == RSS2) {
352 echo '<channel>' . PHP_EOL;
353 // add hubs
354 foreach ($this->hubs as $hub) {
355 //echo $this->makeNode('link', '', array('rel'=>'hub', 'href'=>$hub, 'xmlns'=>'http://www.w3.org/2005/Atom'));
356 echo '<link rel="hub" href="'.htmlspecialchars($hub).'" xmlns="http://www.w3.org/2005/Atom" />' . PHP_EOL;
357 }
358 // add self
359 if (isset($this->self)) {
360 //echo $this->makeNode('link', '', array('rel'=>'self', 'href'=>$this->self, 'xmlns'=>'http://www.w3.org/2005/Atom'));
361 echo '<link rel="self" href="'.htmlspecialchars($this->self).'" xmlns="http://www.w3.org/2005/Atom" />' . PHP_EOL;
362 }
363 //Print Items of channel
364 foreach ($this->channels as $key => $value)
365 {
366 echo $this->makeNode($key, $value);
367 }
368 } elseif ($this->version == JSON || $this->version == JSONP) {
369 $this->json->rss['channel'] = (object)$this->json_keys($this->channels);
370 }
371 }
372
373 /**
374 * Prints formatted feed items
375 *
376 * @access private
377 * @return void
378 */
379 private function printItems()
380 {
381 foreach ($this->items as $item) {
382 $itemElements = $item->getElements();
383
384 echo $this->startItem();
385
386 if ($this->version == JSON || $this->version == JSONP) {
387 $json_item = array();
388 }
389
390 foreach ($itemElements as $thisElement) {
391 foreach ($thisElement as $instance) {
392 if ($this->version == RSS2) {
393 echo $this->makeNode($instance['name'], $instance['content'], $instance['attributes']);
394 } elseif ($this->version == JSON || $this->version == JSONP) {
395 $_json_node = $this->makeNode($instance['name'], $instance['content'], $instance['attributes']);
396 if (count($thisElement) > 1) {
397 $json_item[strtr($instance['name'], ':', '_')][] = $_json_node;
398 } else {
399 $json_item[strtr($instance['name'], ':', '_')] = $_json_node;
400 }
401 }
402 }
403 }
404 echo $this->endItem();
405 if ($this->version == JSON || $this->version == JSONP) {
406 if (count($this->items) > 1) {
407 $this->json->rss['channel']->item[] = $json_item;
408 } else {
409 $this->json->rss['channel']->item = $json_item;
410 }
411 }
412 }
413 }
414
415 /**
416 * Make the starting tag of channels
417 *
418 * @access private
419 * @return void
420 */
421 private function startItem()
422 {
423 if ($this->version == RSS2)
424 {
425 echo '<item>' . PHP_EOL;
426 }
427 // nothing for JSON
428 }
429
430 /**
431 * Closes feed item tag
432 *
433 * @access private
434 * @return void
435 */
436 private function endItem()
437 {
438 if ($this->version == RSS2)
439 {
440 echo '</item>' . PHP_EOL;
441 }
442 // nothing for JSON
443 }
444
445 // End # private functions ----------------------------------------------
446 }