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