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