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