remove old readme
[atutor.git] / include / classes / feedcreator.class.php
1 <?php\r
2 /***************************************************************************\r
3 \r
4 FeedCreator class v1.7.2\r
5 originally (c) Kai Blankenhorn\r
6 www.bitfolge.de\r
7 kaib@bitfolge.de\r
8 v1.3 work by Scott Reynen (scott@randomchaos.com) and Kai Blankenhorn\r
9 v1.5 OPML support by Dirk Clemens\r
10 \r
11 This library is free software; you can redistribute it and/or\r
12 modify it under the terms of the GNU Lesser General Public\r
13 License as published by the Free Software Foundation; either\r
14 version 2.1 of the License, or (at your option) any later version.\r
15 \r
16 This library is distributed in the hope that it will be useful,\r
17 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
19 Lesser General Public License for more details.\r
20 \r
21 You should have received a copy of the GNU Lesser General Public\r
22 License along with this library; if not, write to the Free Software\r
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
24 \r
25 ****************************************************************************\r
26 \r
27 \r
28 Changelog:\r
29 \r
30 v1.7.2  10-11-04\r
31         license changed to LGPL\r
32 \r
33 v1.7.1\r
34         fixed a syntax bug\r
35         fixed left over debug code\r
36 \r
37 v1.7    07-18-04\r
38         added HTML and JavaScript feeds (configurable via CSS) (thanks to Pascal Van Hecke)\r
39         added HTML descriptions for all feed formats (thanks to Pascal Van Hecke)\r
40         added a switch to select an external stylesheet (thanks to Pascal Van Hecke)\r
41         changed default content-type to application/xml\r
42         added character encoding setting\r
43         fixed numerous smaller bugs (thanks to Sören Fuhrmann of golem.de)\r
44         improved changing ATOM versions handling (thanks to August Trometer)\r
45         improved the UniversalFeedCreator's useCached method (thanks to Sören Fuhrmann of golem.de)\r
46         added charset output in HTTP headers (thanks to Sören Fuhrmann of golem.de)\r
47         added Slashdot namespace to RSS 1.0 (thanks to Sören Fuhrmann of golem.de)\r
48 \r
49 v1.6    05-10-04\r
50         added stylesheet to RSS 1.0 feeds\r
51         fixed generator comment (thanks Kevin L. Papendick and Tanguy Pruvot)\r
52         fixed RFC822 date bug (thanks Tanguy Pruvot)\r
53         added TimeZone customization for RFC8601 (thanks Tanguy Pruvot)\r
54         fixed Content-type could be empty (thanks Tanguy Pruvot)\r
55         fixed author/creator in RSS1.0 (thanks Tanguy Pruvot)\r
56 \r
57 v1.6 beta       02-28-04\r
58         added Atom 0.3 support (not all features, though)\r
59         improved OPML 1.0 support (hopefully - added more elements)\r
60         added support for arbitrary additional elements (use with caution)\r
61         code beautification :-)\r
62         considered beta due to some internal changes\r
63 \r
64 v1.5.1  01-27-04\r
65         fixed some RSS 1.0 glitches (thanks to Stéphane Vanpoperynghe)\r
66         fixed some inconsistencies between documentation and code (thanks to Timothy Martin)\r
67 \r
68 v1.5    01-06-04\r
69         added support for OPML 1.0\r
70         added more documentation\r
71 \r
72 v1.4    11-11-03\r
73         optional feed saving and caching\r
74         improved documentation\r
75         minor improvements\r
76 \r
77 v1.3    10-02-03\r
78         renamed to FeedCreator, as it not only creates RSS anymore\r
79         added support for mbox\r
80         tentative support for echo/necho/atom/pie/???\r
81         \r
82 v1.2    07-20-03\r
83         intelligent auto-truncating of RSS 0.91 attributes\r
84         don't create some attributes when they're not set\r
85         documentation improved\r
86         fixed a real and a possible bug with date conversions\r
87         code cleanup\r
88 \r
89 v1.1    06-29-03\r
90         added images to feeds\r
91         now includes most RSS 0.91 attributes\r
92         added RSS 2.0 feeds\r
93 \r
94 v1.0    06-24-03\r
95         initial release\r
96 \r
97 \r
98 \r
99 ***************************************************************************/\r
100 \r
101 /*** GENERAL USAGE *********************************************************\r
102 \r
103 include("feedcreator.class.php"); \r
104 \r
105 $rss = new UniversalFeedCreator(); \r
106 $rss->useCached(); // use cached version if age<1 hour\r
107 $rss->title = "PHP news"; \r
108 $rss->description = "daily news from the PHP scripting world"; \r
109 \r
110 //optional\r
111 $rss->descriptionTruncSize = 500;\r
112 $rss->descriptionHtmlSyndicated = true;\r
113 \r
114 $rss->link = "http://www.dailyphp.net/news"; \r
115 $rss->syndicationURL = "http://www.dailyphp.net/".$_SERVER["PHP_SELF"]; \r
116 \r
117 $image = new FeedImage(); \r
118 $image->title = "dailyphp.net logo"; \r
119 $image->url = "http://www.dailyphp.net/images/logo.gif"; \r
120 $image->link = "http://www.dailyphp.net"; \r
121 $image->description = "Feed provided by dailyphp.net. Click to visit."; \r
122 \r
123 //optional\r
124 $image->descriptionTruncSize = 500;\r
125 $image->descriptionHtmlSyndicated = true;\r
126 \r
127 $rss->image = $image; \r
128 \r
129 // get your news items from somewhere, e.g. your database: \r
130 mysql_select_db($dbHost, $dbUser, $dbPass); \r
131 $res = mysql_query("SELECT * FROM news ORDER BY newsdate DESC"); \r
132 while ($data = mysql_fetch_object($res)) { \r
133     $item = new FeedItem(); \r
134     $item->title = $data->title; \r
135     $item->link = $data->url; \r
136     $item->description = $data->short; \r
137     \r
138     //optional\r
139     item->descriptionTruncSize = 500;\r
140     item->descriptionHtmlSyndicated = true;\r
141 \r
142     $item->date = $data->newsdate; \r
143     $item->source = "http://www.dailyphp.net"; \r
144     $item->author = "John Doe"; \r
145      \r
146     $rss->addItem($item); \r
147\r
148 \r
149 // valid format strings are: RSS0.91, RSS1.0, RSS2.0, PIE0.1 (deprecated),\r
150 // MBOX, OPML, ATOM, ATOM0.3, HTML, JS\r
151 echo $rss->saveFeed("RSS1.0", "news/feed.xml");\r
152 \r
153 \r
154 ***************************************************************************\r
155 *          A little setup                                                 *\r
156 **************************************************************************/\r
157 \r
158 // your local timezone, set to "" to disable or for GMT\r
159 define("TIME_ZONE","");\r
160 \r
161 \r
162 /**\r
163  * Version string.\r
164  **/\r
165 define("FEEDCREATOR_VERSION", "FeedCreator 1.7.2");\r
166 \r
167 //Greg Gay\r
168 //Added ATutor charset encoding, see line 502\r
169 //define("MY_ENCODING" , $myLang->getCharacterSet());\r
170 \r
171 /**\r
172  * A FeedItem is a part of a FeedCreator feed.\r
173  *\r
174  * @author Kai Blankenhorn <kaib@bitfolge.de>\r
175  * @since 1.3\r
176  */\r
177 class FeedItem extends HtmlDescribable {\r
178         /**\r
179          * Mandatory attributes of an item.\r
180          */\r
181         var $title, $description, $link;\r
182         \r
183         /**\r
184          * Optional attributes of an item.\r
185          */\r
186         var $author, $authorEmail, $image, $category, $comments, $guid, $source, $creator;\r
187         \r
188         /**\r
189          * Publishing date of an item. May be in one of the following formats:\r
190          *\r
191          *      RFC 822:\r
192          *      "Mon, 20 Jan 03 18:05:41 +0400"\r
193          *      "20 Jan 03 18:05:41 +0000"\r
194          *\r
195          *      ISO 8601:\r
196          *      "2003-01-20T18:05:41+04:00"\r
197          *\r
198          *      Unix:\r
199          *      1043082341\r
200          */\r
201         var $date;\r
202         \r
203         /**\r
204          * Any additional elements to include as an assiciated array. All $key => $value pairs\r
205          * will be included unencoded in the feed item in the form\r
206          *     <$key>$value</$key>\r
207          * Again: No encoding will be used! This means you can invalidate or enhance the feed\r
208          * if $value contains markup. This may be abused to embed tags not implemented by\r
209          * the FeedCreator class used.\r
210          */\r
211         var $additionalElements = Array();\r
212 \r
213         // on hold\r
214         // var $source;\r
215 }\r
216 \r
217 \r
218 \r
219 /**\r
220  * An FeedImage may be added to a FeedCreator feed.\r
221  * @author Kai Blankenhorn <kaib@bitfolge.de>\r
222  * @since 1.3\r
223  */\r
224 class FeedImage extends HtmlDescribable {\r
225         /**\r
226          * Mandatory attributes of an image.\r
227          */\r
228         var $title, $url, $link;\r
229         \r
230         /**\r
231          * Optional attributes of an image.\r
232          */\r
233         var $width, $height, $description;\r
234 }\r
235 \r
236 \r
237 \r
238 /**\r
239  * An HtmlDescribable is an item within a feed that can have a description that may\r
240  * include HTML markup.\r
241  */\r
242 class HtmlDescribable {\r
243         /**\r
244          * Indicates whether the description field should be rendered in HTML.\r
245          */\r
246         var $descriptionHtmlSyndicated;\r
247         \r
248         /**\r
249          * Indicates whether and to how many characters a description should be truncated.\r
250          */\r
251         var $descriptionTruncSize;\r
252         \r
253         /**\r
254          * Returns a formatted description field, depending on descriptionHtmlSyndicated and\r
255          * $descriptionTruncSize properties\r
256          * @return    string    the formatted description  \r
257          */\r
258         function getDescription() {\r
259                 $descriptionField = new FeedHtmlField($this->description);\r
260                 $descriptionField->syndicateHtml = $this->descriptionHtmlSyndicated;\r
261                 $descriptionField->truncSize = $this->descriptionTruncSize;\r
262                 return $descriptionField->output();\r
263         }\r
264 \r
265 }\r
266 \r
267 \r
268 /**\r
269  * An FeedHtmlField describes and generates\r
270  * a feed, item or image html field (probably a description). Output is \r
271  * generated based on $truncSize, $syndicateHtml properties.\r
272  * @author Pascal Van Hecke <feedcreator.class.php@vanhecke.info>\r
273  * @version 1.6\r
274  */\r
275 class FeedHtmlField {\r
276         /**\r
277          * Mandatory attributes of a FeedHtmlField.\r
278          */\r
279         var $rawFieldContent;\r
280         \r
281         /**\r
282          * Optional attributes of a FeedHtmlField.\r
283          * \r
284          */\r
285         var $truncSize, $syndicateHtml;\r
286         \r
287         /**\r
288          * Creates a new instance of FeedHtmlField.\r
289          * @param  $string: if given, sets the rawFieldContent property\r
290          */\r
291         function FeedHtmlField($parFieldContent) {\r
292                 if ($parFieldContent) {\r
293                         $this->rawFieldContent = $parFieldContent;\r
294                 }\r
295         }\r
296                 \r
297                 \r
298         /**\r
299          * Creates the right output, depending on $truncSize, $syndicateHtml properties.\r
300          * @return string    the formatted field\r
301          */\r
302         function output() {\r
303                 // when field available and syndicated in html we assume \r
304                 // - valid html in $rawFieldContent and we enclose in CDATA tags\r
305                 // - no truncation (truncating risks producing invalid html)\r
306                 if (!$this->rawFieldContent) {\r
307                         $result = "";\r
308                 }       elseif ($this->syndicateHtml) {\r
309                         $result = "<![CDATA[".$this->rawFieldContent."]]>";\r
310                 } else {\r
311                         if ($this->truncSize and is_int($this->truncSize)) {\r
312                                 $result = FeedCreator::iTrunc(htmlspecialchars($this->rawFieldContent),$this->truncSize);\r
313                         } else {\r
314                                 $result = htmlspecialchars($this->rawFieldContent);\r
315                         }\r
316                 }\r
317                 return $result;\r
318         }\r
319 \r
320 }\r
321 \r
322 \r
323 \r
324 /**\r
325  * UniversalFeedCreator lets you choose during runtime which\r
326  * format to build.\r
327  * For general usage of a feed class, see the FeedCreator class\r
328  * below or the example above.\r
329  *\r
330  * @since 1.3\r
331  * @author Kai Blankenhorn <kaib@bitfolge.de>\r
332  */\r
333 class UniversalFeedCreator extends FeedCreator {\r
334         var $_feed;\r
335         \r
336         function _setFormat($format) {\r
337                 switch (strtoupper($format)) {\r
338                         \r
339                         case "2.0":\r
340                                 // fall through\r
341                         case "RSS2.0":\r
342                                 $this->_feed = new RSSCreator20();\r
343                                 break;\r
344                         \r
345                         case "1.0":\r
346                                 // fall through\r
347                         case "RSS1.0":\r
348                                 $this->_feed = new RSSCreator10();\r
349                                 break;\r
350                         \r
351                         case "0.91":\r
352                                 // fall through\r
353                         case "RSS0.91":\r
354                                 $this->_feed = new RSSCreator091();\r
355                                 break;\r
356                         \r
357                         case "PIE0.1":\r
358                                 $this->_feed = new PIECreator01();\r
359                                 break;\r
360                         \r
361                         case "MBOX":\r
362                                 $this->_feed = new MBOXCreator();\r
363                                 break;\r
364                         \r
365                         case "OPML":\r
366                                 $this->_feed = new OPMLCreator();\r
367                                 break;\r
368                                 \r
369                         case "ATOM":\r
370                                 // fall through: always the latest ATOM version\r
371                                 \r
372                         case "ATOM0.3":\r
373                                 $this->_feed = new AtomCreator03();\r
374                                 break;\r
375                                 \r
376                         case "HTML":\r
377                                 $this->_feed = new HTMLCreator();\r
378                                 break;\r
379                         \r
380                         case "JS":\r
381                                 // fall through\r
382                         case "JAVASCRIPT":\r
383                                 $this->_feed = new JSCreator();\r
384                                 break;\r
385                         \r
386                         default:\r
387                                 $this->_feed = new RSSCreator091();\r
388                                 break;\r
389                 }\r
390         \r
391                 $vars = get_object_vars($this);\r
392                 foreach ($vars as $key => $value) {\r
393                         // prevent overwriting of properties "contentType", "encoding"; do not copy "_feed" itself\r
394                         if (!in_array($key, array("_feed", "contentType", "encoding"))) {\r
395                                 $this->_feed->{$key} = $this->{$key};\r
396                         }\r
397                 }\r
398         }\r
399         \r
400         /**\r
401          * Creates a syndication feed based on the items previously added.\r
402          *\r
403          * @see        FeedCreator::addItem()\r
404          * @param    string    format    format the feed should comply to. Valid values are:\r
405          *                      "PIE0.1", "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM0.3", "HTML", "JS"\r
406          * @return    string    the contents of the feed.\r
407          */\r
408         function createFeed($format = "RSS0.91") {\r
409                 $this->_setFormat($format);\r
410                 return $this->_feed->createFeed();\r
411         }\r
412         \r
413         \r
414         \r
415         /**\r
416          * Saves this feed as a file on the local disk. After the file is saved, an HTTP redirect\r
417          * header may be sent to redirect the use to the newly created file.\r
418          * @since 1.4\r
419          * \r
420          * @param       string  format  format the feed should comply to. Valid values are:\r
421          *                      "PIE0.1" (deprecated), "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM", "ATOM0.3", "HTML", "JS"\r
422          * @param       string  filename        optional        the filename where a recent version of the feed is saved. If not specified, the filename is $_SERVER["PHP_SELF"] with the extension changed to .xml (see _generateFilename()).\r
423          * @param       boolean displayContents optional        send the content of the file or not. If true, the file will be sent in the body of the response.\r
424          */\r
425         function saveFeed($format="RSS0.91", $filename="", $displayContents=true) {\r
426                 $this->_setFormat($format);\r
427                 $this->_feed->saveFeed($filename, $displayContents);\r
428         }\r
429 \r
430 \r
431    /**\r
432     * Turns on caching and checks if there is a recent version of this feed in the cache.\r
433     * If there is, an HTTP redirect header is sent.\r
434     * To effectively use caching, you should create the FeedCreator object and call this method\r
435     * before anything else, especially before you do the time consuming task to build the feed\r
436     * (web fetching, for example).\r
437     *\r
438     * @param   string   format   format the feed should comply to. Valid values are:\r
439     *       "PIE0.1" (deprecated), "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM0.3".\r
440     * @param filename   string   optional the filename where a recent version of the feed is saved. If not specified, the filename is $_SERVER["PHP_SELF"] with the extension changed to .xml (see _generateFilename()).\r
441     * @param timeout int      optional the timeout in seconds before a cached version is refreshed (defaults to 3600 = 1 hour)\r
442     */\r
443    function useCached($format="RSS0.91", $filename="", $timeout=3600) {\r
444       $this->_setFormat($format);\r
445       $this->_feed->useCached($filename, $timeout);\r
446    }\r
447 \r
448 }\r
449 \r
450 \r
451 /**\r
452  * FeedCreator is the abstract base implementation for concrete\r
453  * implementations that implement a specific format of syndication.\r
454  *\r
455  * @abstract\r
456  * @author Kai Blankenhorn <kaib@bitfolge.de>\r
457  * @since 1.4\r
458  */\r
459 class FeedCreator extends HtmlDescribable {\r
460 \r
461         /**\r
462          * Mandatory attributes of a feed.\r
463          */\r
464         var $title, $description, $link;\r
465         \r
466         //var $my_charset;\r
467         /**\r
468          * Optional attributes of a feed.\r
469          */\r
470         var $syndicationURL, $image, $language, $copyright, $pubDate, $lastBuildDate, $editor, $editorEmail, $webmaster, $category, $docs, $ttl, $rating, $skipHours, $skipDays;\r
471 \r
472         /**\r
473         * The url of the external xsl stylesheet used to format the naked rss feed.\r
474         * Ignored in the output when empty.\r
475         */\r
476         var $xslStyleSheet = "";\r
477         \r
478         \r
479         /**\r
480          * @access private\r
481          */\r
482         var $items = Array();\r
483         \r
484         \r
485         /**\r
486          * This feed's MIME content type.\r
487          * @since 1.4\r
488          * @access private\r
489          */\r
490         var $contentType = "application/xml";\r
491         \r
492         \r
493         /**\r
494          * This feed's character encoding.\r
495          * @since 1.6.1\r
496          **/\r
497          //Modified to accommodate ATutor charsets\r
498          //Greg Gay\r
499 \r
500         //var $encoding = MY_ENCODING;\r
501         var $encoding =  "utf-8";\r
502         //var $encoding =  "ISO-8859-1";\r
503         \r
504         \r
505         /**\r
506          * Any additional elements to include as an assiciated array. All $key => $value pairs\r
507          * will be included unencoded in the feed in the form\r
508          *     <$key>$value</$key>\r
509          * Again: No encoding will be used! This means you can invalidate or enhance the feed\r
510          * if $value contains markup. This may be abused to embed tags not implemented by\r
511          * the FeedCreator class used.\r
512          */\r
513         var $additionalElements = Array();\r
514    \r
515     \r
516         /**\r
517          * Adds an FeedItem to the feed.\r
518          *\r
519          * @param object FeedItem $item The FeedItem to add to the feed.\r
520          * @access public\r
521          */\r
522         function addItem($item) {\r
523                 $this->items[] = $item;\r
524         }\r
525         \r
526         \r
527         /**\r
528          * Truncates a string to a certain length at the most sensible point.\r
529          * First, if there's a '.' character near the end of the string, the string is truncated after this character.\r
530          * If there is no '.', the string is truncated after the last ' ' character.\r
531          * If the string is truncated, " ..." is appended.\r
532          * If the string is already shorter than $length, it is returned unchanged.\r
533          * \r
534          * @static\r
535          * @param string    string A string to be truncated.\r
536          * @param int        length the maximum length the string should be truncated to\r
537          * @return string    the truncated string\r
538          */\r
539         function iTrunc($string, $length) {\r
540                 if (strlen($string)<=$length) {\r
541                         return $string;\r
542                 }\r
543                 \r
544                 $pos = strrpos($string,".");\r
545                 if ($pos>=$length-4) {\r
546                         $string = substr($string,0,$length-4);\r
547                         $pos = strrpos($string,".");\r
548                 }\r
549                 if ($pos>=$length*0.4) {\r
550                         return substr($string,0,$pos+1)." ...";\r
551                 }\r
552                 \r
553                 $pos = strrpos($string," ");\r
554                 if ($pos>=$length-4) {\r
555                         $string = substr($string,0,$length-4);\r
556                         $pos = strrpos($string," ");\r
557                 }\r
558                 if ($pos>=$length*0.4) {\r
559                         return substr($string,0,$pos)." ...";\r
560                 }\r
561                 \r
562                 return substr($string,0,$length-4)." ...";\r
563                         \r
564         }\r
565         \r
566         \r
567         /**\r
568          * Creates a comment indicating the generator of this feed.\r
569          * The format of this comment seems to be recognized by\r
570          * Syndic8.com.\r
571          */\r
572         function _createGeneratorComment() {\r
573                 return "<!-- generator=\"".FEEDCREATOR_VERSION."\" -->\n";\r
574         }\r
575         \r
576         \r
577         /**\r
578          * Creates a string containing all additional elements specified in\r
579          * $additionalElements.\r
580          * @param       elements        array   an associative array containing key => value pairs\r
581          * @param indentString  string  a string that will be inserted before every generated line\r
582          * @return    string    the XML tags corresponding to $additionalElements\r
583          */\r
584         function _createAdditionalElements($elements, $indentString="") {\r
585                 $ae = "";\r
586                 if (is_array($elements)) {\r
587                         foreach($elements AS $key => $value) {\r
588                                 $ae.= $indentString."<$key>$value</$key>\n";\r
589                         }\r
590                 }\r
591                 return $ae;\r
592         }\r
593         \r
594         function _createStylesheetReferences() {\r
595                 $xml = "";\r
596                 if ($this->cssStyleSheet) $xml .= "<?xml-stylesheet href=\"".$this->cssStyleSheet."\" type=\"text/css\"?>\n";\r
597                 if ($this->xslStyleSheet) $xml .= "<?xml-stylesheet href=\"".$this->xslStyleSheet."\" type=\"text/xsl\"?>\n";\r
598                 return $xml;\r
599         }\r
600         \r
601         \r
602         /**\r
603          * Builds the feed's text.\r
604          * @abstract\r
605          * @return    string    the feed's complete text \r
606          */\r
607         function createFeed() {\r
608         }\r
609         \r
610         /**\r
611          * Generate a filename for the feed cache file. The result will be $_SERVER["PHP_SELF"] with the extension changed to .xml.\r
612          * For example:\r
613          * \r
614          * echo $_SERVER["PHP_SELF"]."\n";\r
615          * echo FeedCreator::_generateFilename();\r
616          * \r
617          * would produce:\r
618          * \r
619          * /rss/latestnews.php\r
620          * latestnews.xml\r
621          *\r
622          * @return string the feed cache filename\r
623          * @since 1.4\r
624          * @access private\r
625          */\r
626         function _generateFilename() {\r
627                 $fileInfo = pathinfo($_SERVER["PHP_SELF"]);\r
628                 return substr($fileInfo["basename"],0,-(strlen($fileInfo["extension"])+1)).".xml";\r
629         }\r
630         \r
631         \r
632         /**\r
633          * @since 1.4\r
634          * @access private\r
635          */\r
636         function _redirect($filename) {\r
637                 // attention, heavily-commented-out-area\r
638                 \r
639                 // maybe use this in addition to file time checking\r
640                 //Header("Expires: ".date("r",time()+$this->_timeout));\r
641                 \r
642                 /* no caching at all, doesn't seem to work as good:\r
643                 Header("Cache-Control: no-cache");\r
644                 Header("Pragma: no-cache");\r
645                 */\r
646                 \r
647                 // HTTP redirect, some feed readers' simple HTTP implementations don't follow it\r
648                 //Header("Location: ".$filename);\r
649 \r
650                 Header("Content-Type: ".$this->contentType."; charset=".$this->encoding."; filename=".basename($filename));\r
651                 Header("Content-Disposition: inline; filename=".basename($filename));\r
652                 readfile($filename, "r");\r
653                 die();\r
654         }\r
655     \r
656         /**\r
657          * Turns on caching and checks if there is a recent version of this feed in the cache.\r
658          * If there is, an HTTP redirect header is sent.\r
659          * To effectively use caching, you should create the FeedCreator object and call this method\r
660          * before anything else, especially before you do the time consuming task to build the feed\r
661          * (web fetching, for example).\r
662          * @since 1.4\r
663          * @param filename      string  optional        the filename where a recent version of the feed is saved. If not specified, the filename is $_SERVER["PHP_SELF"] with the extension changed to .xml (see _generateFilename()).\r
664          * @param timeout       int             optional        the timeout in seconds before a cached version is refreshed (defaults to 3600 = 1 hour)\r
665          */\r
666         function useCached($filename="", $timeout=3600) {\r
667                 $this->_timeout = $timeout;\r
668                 if ($filename=="") {\r
669                         $filename = $this->_generateFilename();\r
670                 }\r
671                 if (file_exists($filename) AND (time()-filemtime($filename) < $timeout)) {\r
672                         $this->_redirect($filename);\r
673                 }\r
674         }\r
675         \r
676         \r
677         /**\r
678          * Saves this feed as a file on the local disk. After the file is saved, a redirect\r
679          * header may be sent to redirect the user to the newly created file.\r
680          * @since 1.4\r
681          * \r
682          * @param filename      string  optional        the filename where a recent version of the feed is saved. If not specified, the filename is $_SERVER["PHP_SELF"] with the extension changed to .xml (see _generateFilename()).\r
683          * @param redirect      boolean optional        send an HTTP redirect header or not. If true, the user will be automatically redirected to the created file.\r
684          */\r
685         function saveFeed($filename="", $displayContents=true) {\r
686                 if ($filename=="") {\r
687                         $filename = $this->_generateFilename();\r
688                 }\r
689                 $feedFile = fopen($filename, "w+");\r
690                 if ($feedFile) {\r
691                         fputs($feedFile,$this->createFeed());\r
692                         fclose($feedFile);\r
693                         if ($displayContents) {\r
694                                 $this->_redirect($filename);\r
695                         }\r
696                 } else {\r
697                         echo "<br /><b>Error creating feed file, please check write permissions.</b><br />";\r
698                 }\r
699         }\r
700         \r
701 }\r
702 \r
703 \r
704 /**\r
705  * FeedDate is an internal class that stores a date for a feed or feed item.\r
706  * Usually, you won't need to use this.\r
707  */\r
708 class FeedDate {\r
709         var $unix;\r
710         \r
711         /**\r
712          * Creates a new instance of FeedDate representing a given date.\r
713          * Accepts RFC 822, ISO 8601 date formats as well as unix time stamps.\r
714          * @param mixed $dateString optional the date this FeedDate will represent. If not specified, the current date and time is used.\r
715          */\r
716         function FeedDate($dateString="") {\r
717                 if ($dateString=="") $dateString = date("r");\r
718                 \r
719                 if (is_integer($dateString)) {\r
720                         $this->unix = $dateString;\r
721                         return;\r
722                 }\r
723                 if (preg_match("~(?:(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun),\\s+)?(\\d{1,2})\\s+([a-zA-Z]{3})\\s+(\\d{4})\\s+(\\d{2}):(\\d{2}):(\\d{2})\\s+(.*)~",$dateString,$matches)) {\r
724                         $months = Array("Jan"=>1,"Feb"=>2,"Mar"=>3,"Apr"=>4,"May"=>5,"Jun"=>6,"Jul"=>7,"Aug"=>8,"Sep"=>9,"Oct"=>10,"Nov"=>11,"Dec"=>12);\r
725                         $this->unix = mktime($matches[4],$matches[5],$matches[6],$months[$matches[2]],$matches[1],$matches[3]);\r
726                         if (substr($matches[7],0,1)=='+' OR substr($matches[7],0,1)=='-') {\r
727                                 $tzOffset = (substr($matches[7],0,3) * 60 + substr($matches[7],-2)) * 60;\r
728                         } else {\r
729                                 if (strlen($matches[7])==1) {\r
730                                         $oneHour = 3600;\r
731                                         $ord = ord($matches[7]);\r
732                                         if ($ord < ord("M")) {\r
733                                                 $tzOffset = (ord("A") - $ord - 1) * $oneHour;\r
734                                         } elseif ($ord >= ord("M") AND $matches[7]!="Z") {\r
735                                                 $tzOffset = ($ord - ord("M")) * $oneHour;\r
736                                         } elseif ($matches[7]=="Z") {\r
737                                                 $tzOffset = 0;\r
738                                         }\r
739                                 }\r
740                                 switch ($matches[7]) {\r
741                                         case "UT":\r
742                                         case "GMT":     $tzOffset = 0;\r
743                                 }\r
744                         }\r
745                         $this->unix += $tzOffset;\r
746                         return;\r
747                 }\r
748                 if (preg_match("~(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2})(.*)~",$dateString,$matches)) {\r
749                         $this->unix = mktime($matches[4],$matches[5],$matches[6],$matches[2],$matches[3],$matches[1]);\r
750                         if (substr($matches[7],0,1)=='+' OR substr($matches[7],0,1)=='-') {\r
751                                 $tzOffset = (substr($matches[7],0,3) * 60 + substr($matches[7],-2)) * 60;\r
752                         } else {\r
753                                 if ($matches[7]=="Z") {\r
754                                         $tzOffset = 0;\r
755                                 }\r
756                         }\r
757                         $this->unix += $tzOffset;\r
758                         return;\r
759                 }\r
760                 $this->unix = 0;\r
761         }\r
762 \r
763         /**\r
764          * Gets the date stored in this FeedDate as an RFC 822 date.\r
765          *\r
766          * @return a date in RFC 822 format\r
767          */\r
768         function rfc822() {\r
769                 //return gmdate("r",$this->unix);\r
770                 $date = gmdate("D, d M Y H:i:s", $this->unix);\r
771                 if (TIME_ZONE!="") $date .= " ".str_replace(":","",TIME_ZONE);\r
772                 return $date;\r
773         }\r
774         \r
775         /**\r
776          * Gets the date stored in this FeedDate as an ISO 8601 date.\r
777          *\r
778          * @return a date in ISO 8601 format\r
779          */\r
780         function iso8601() {\r
781                 $date = gmdate("Y-m-d\TH:i:sO",$this->unix);\r
782                 $date = substr($date,0,22) . ':' . substr($date,-2);\r
783                 if (TIME_ZONE!="") $date = str_replace("+00:00",TIME_ZONE,$date);\r
784                 return $date;\r
785         }\r
786         \r
787         /**\r
788          * Gets the date stored in this FeedDate as unix time stamp.\r
789          *\r
790          * @return a date as a unix time stamp\r
791          */\r
792         function unix() {\r
793                 return $this->unix;\r
794         }\r
795 }\r
796 \r
797 \r
798 /**\r
799  * RSSCreator10 is a FeedCreator that implements RDF Site Summary (RSS) 1.0.\r
800  *\r
801  * @see http://www.purl.org/rss/1.0/\r
802  * @since 1.3\r
803  * @author Kai Blankenhorn <kaib@bitfolge.de>\r
804  */\r
805 class RSSCreator10 extends FeedCreator {\r
806 \r
807         /**\r
808          * Builds the RSS feed's text. The feed will be compliant to RDF Site Summary (RSS) 1.0.\r
809          * The feed will contain all items previously added in the same order.\r
810          * @return    string    the feed's complete text \r
811          */\r
812         function createFeed() {     \r
813                 $feed = "<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n";\r
814                 $feed.= $this->_createGeneratorComment();\r
815                 if ($this->cssStyleSheet=="") {\r
816                         $cssStyleSheet = "http://www.w3.org/2000/08/w3c-synd/style.css";\r
817                 }\r
818                 $feed.= $this->_createStylesheetReferences();\r
819                 $feed.= "<rdf:RDF\n";\r
820                 $feed.= "    xmlns=\"http://purl.org/rss/1.0/\"\n";\r
821                 $feed.= "    xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n"; \r
822                 $feed.= "    xmlns:slash=\"http://purl.org/rss/1.0/modules/slash/\"\n";\r
823                 $feed.= "    xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n";\r
824                 $feed.= "    <channel rdf:about=\"".$this->syndicationURL."\">\n";\r
825                 $feed.= "        <title>".htmlspecialchars($this->title)."</title>\n";\r
826                 $feed.= "        <description>".htmlspecialchars($this->description)."</description>\n";\r
827                 $feed.= "        <link>".$this->link."</link>\n";\r
828                 if ($this->image!=null) {\r
829                         $feed.= "        <image rdf:resource=\"".$this->image->url."\" />\n";\r
830                 }\r
831                 $now = new FeedDate();\r
832                 $feed.= "       <dc:date>".htmlspecialchars($now->iso8601())."</dc:date>\n";\r
833                 $feed.= "        <items>\n";\r
834                 $feed.= "            <rdf:Seq>\n";\r
835                 for ($i=0;$i<count($this->items);$i++) {\r
836                         $feed.= "                <rdf:li rdf:resource=\"".htmlspecialchars($this->items[$i]->link)."\"/>\n";\r
837                 }\r
838                 $feed.= "            </rdf:Seq>\n";\r
839                 $feed.= "        </items>\n";\r
840                 $feed.= "    </channel>\n";\r
841                 if ($this->image!=null) {\r
842                         $feed.= "    <image rdf:about=\"".$this->image->url."\">\n";\r
843                         $feed.= "        <title>".$this->image->title."</title>\n";\r
844                         $feed.= "        <link>".$this->image->link."</link>\n";\r
845                         $feed.= "        <url>".$this->image->url."</url>\n";\r
846                         $feed.= "    </image>\n";\r
847                 }\r
848                 $feed.= $this->_createAdditionalElements($this->additionalElements, "    ");\r
849                 \r
850                 for ($i=0;$i<count($this->items);$i++) {\r
851                         $feed.= "    <item rdf:about=\"".htmlspecialchars($this->items[$i]->link)."\">\n";\r
852                         //$feed.= "        <dc:type>Posting</dc:type>\n";\r
853                         $feed.= "        <dc:format>text/html</dc:format>\n";\r
854                         if ($this->items[$i]->date!=null) {\r
855                                 $itemDate = new FeedDate($this->items[$i]->date);\r
856                                 $feed.= "        <dc:date>".htmlspecialchars($itemDate->iso8601())."</dc:date>\n";\r
857                         }\r
858                         if ($this->items[$i]->source!="") {\r
859                                 $feed.= "        <dc:source>".htmlspecialchars($this->items[$i]->source)."</dc:source>\n";\r
860                         }\r
861                         if ($this->items[$i]->author!="") {\r
862                                 $feed.= "        <dc:creator>".htmlspecialchars($this->items[$i]->author)."</dc:creator>\n";\r
863                         }\r
864                         $feed.= "        <title>".htmlspecialchars(strip_tags(strtr($this->items[$i]->title,"\n\r","  ")))."</title>\n";\r
865                         $feed.= "        <link>".htmlspecialchars($this->items[$i]->link)."</link>\n";\r
866                         $feed.= "        <description>".htmlspecialchars($this->items[$i]->description)."</description>\n";\r
867                         $feed.= $this->_createAdditionalElements($this->items[$i]->additionalElements, "        ");\r
868                         $feed.= "    </item>\n";\r
869                 }\r
870                 $feed.= "</rdf:RDF>\n";\r
871                 return $feed;\r
872         }\r
873 }\r
874 \r
875 \r
876 \r
877 /**\r
878  * RSSCreator091 is a FeedCreator that implements RSS 0.91 Spec, revision 3.\r
879  *\r
880  * @see http://my.netscape.com/publish/formats/rss-spec-0.91.html\r
881  * @since 1.3\r
882  * @author Kai Blankenhorn <kaib@bitfolge.de>\r
883  */\r
884 class RSSCreator091 extends FeedCreator {\r
885 \r
886         /**\r
887          * Stores this RSS feed's version number.\r
888          * @access private\r
889          */\r
890         var $RSSVersion;\r
891 \r
892         function RSSCreator091() {\r
893                 $this->_setRSSVersion("0.91");\r
894                 $this->contentType = "application/rss+xml";\r
895         }\r
896         \r
897         /**\r
898          * Sets this RSS feed's version number.\r
899          * @access private\r
900          */\r
901         function _setRSSVersion($version) {\r
902                 $this->RSSVersion = $version;\r
903         }\r
904 \r
905         /**\r
906          * Builds the RSS feed's text. The feed will be compliant to RDF Site Summary (RSS) 1.0.\r
907          * The feed will contain all items previously added in the same order.\r
908          * @return    string    the feed's complete text \r
909          */\r
910         function createFeed() {\r
911                 $feed = "<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n";\r
912                 $feed.= $this->_createGeneratorComment();\r
913                 $feed.= $this->_createStylesheetReferences();\r
914                 $feed.= "<rss version=\"".$this->RSSVersion."\">\n"; \r
915                 $feed.= "    <channel>\n";\r
916                 $feed.= "        <title>".FeedCreator::iTrunc(htmlspecialchars($this->title),100)."</title>\n";\r
917                 $this->descriptionTruncSize = 500;\r
918                 $feed.= "        <description>".$this->getDescription()."</description>\n";\r
919                 $feed.= "        <link>".$this->link."</link>\n";\r
920                 $now = new FeedDate();\r
921                 $feed.= "        <lastBuildDate>".htmlspecialchars($now->rfc822())."</lastBuildDate>\n";\r
922                 $feed.= "        <generator>".FEEDCREATOR_VERSION."</generator>\n";\r
923 \r
924                 if ($this->image!=null) {\r
925                         $feed.= "        <image>\n";\r
926                         $feed.= "            <url>".$this->image->url."</url>\n"; \r
927                         $feed.= "            <title>".FeedCreator::iTrunc(htmlspecialchars($this->image->title),100)."</title>\n"; \r
928                         $feed.= "            <link>".$this->image->link."</link>\n";\r
929                         if ($this->image->width!="") {\r
930                                 $feed.= "            <width>".$this->image->width."</width>\n";\r
931                         }\r
932                         if ($this->image->height!="") {\r
933                                 $feed.= "            <height>".$this->image->height."</height>\n";\r
934                         }\r
935                         if ($this->image->description!="") {\r
936                                 $feed.= "            <description>".$this->image->getDescription()."</description>\n";\r
937                         }\r
938                         $feed.= "        </image>\n";\r
939                 }\r
940                 if ($this->language!="") {\r
941                         $feed.= "        <language>".$this->language."</language>\n";\r
942                 }\r
943                 if ($this->copyright!="") {\r
944                         $feed.= "        <copyright>".FeedCreator::iTrunc(htmlspecialchars($this->copyright),100)."</copyright>\n";\r
945                 }\r
946                 if ($this->editor!="") {\r
947                         $feed.= "        <managingEditor>".FeedCreator::iTrunc(htmlspecialchars($this->editor),100)."</managingEditor>\n";\r
948                 }\r
949                 if ($this->webmaster!="") {\r
950                         $feed.= "        <webMaster>".FeedCreator::iTrunc(htmlspecialchars($this->webmaster),100)."</webMaster>\n";\r
951                 }\r
952                 if ($this->pubDate!="") {\r
953                         $pubDate = new FeedDate($this->pubDate);\r
954                         $feed.= "        <pubDate>".htmlspecialchars($pubDate->rfc822())."</pubDate>\n";\r
955                 }\r
956                 if ($this->category!="") {\r
957                         $feed.= "        <category>".htmlspecialchars($this->category)."</category>\n";\r
958                 }\r
959                 if ($this->docs!="") {\r
960                         $feed.= "        <docs>".FeedCreator::iTrunc(htmlspecialchars($this->docs),500)."</docs>\n";\r
961                 }\r
962                 if ($this->ttl!="") {\r
963                         $feed.= "        <ttl>".htmlspecialchars($this->ttl)."</ttl>\n";\r
964                 }\r
965                 if ($this->rating!="") {\r
966                         $feed.= "        <rating>".FeedCreator::iTrunc(htmlspecialchars($this->rating),500)."</rating>\n";\r
967                 }\r
968                 if ($this->skipHours!="") {\r
969                         $feed.= "        <skipHours>".htmlspecialchars($this->skipHours)."</skipHours>\n";\r
970                 }\r
971                 if ($this->skipDays!="") {\r
972                         $feed.= "        <skipDays>".htmlspecialchars($this->skipDays)."</skipDays>\n";\r
973                 }\r
974                 $feed.= $this->_createAdditionalElements($this->additionalElements, "    ");\r
975 \r
976                 for ($i=0;$i<count($this->items);$i++) {\r
977                         $feed.= "        <item>\n";\r
978                         $feed.= "            <title>".FeedCreator::iTrunc(htmlspecialchars(strip_tags($this->items[$i]->title)),100)."</title>\n";\r
979                         $feed.= "            <link>".htmlspecialchars($this->items[$i]->link)."</link>\n";\r
980                         $feed.= "            <description>".$this->items[$i]->getDescription()."</description>\n";\r
981                         \r
982                         if ($this->items[$i]->author!="") {\r
983                                 $feed.= "            <author>".htmlspecialchars($this->items[$i]->author)."</author>\n";\r
984                         }\r
985                         /*\r
986                         // on hold\r
987                         if ($this->items[$i]->source!="") {\r
988                                         $feed.= "            <source>".htmlspecialchars($this->items[$i]->source)."</source>\n";\r
989                         }\r
990                         */\r
991                         if ($this->items[$i]->category!="") {\r
992                                 $feed.= "            <category>".htmlspecialchars($this->items[$i]->category)."</category>\n";\r
993                         }\r
994                         if ($this->items[$i]->comments!="") {\r
995                                 $feed.= "            <comments>".htmlspecialchars($this->items[$i]->comments)."</comments>\n";\r
996                         }\r
997                         if ($this->items[$i]->date!="") {\r
998                         $itemDate = new FeedDate($this->items[$i]->date);\r
999                                 $feed.= "            <pubDate>".htmlspecialchars($itemDate->rfc822())."</pubDate>\n";\r
1000                         }\r
1001                         if ($this->items[$i]->guid!="") {\r
1002                                 $feed.= "            <guid>".htmlspecialchars($this->items[$i]->guid)."</guid>\n";\r
1003                         }\r
1004                         $feed.= $this->_createAdditionalElements($this->items[$i]->additionalElements, "        ");\r
1005                         $feed.= "        </item>\n";\r
1006                 }\r
1007                 $feed.= "    </channel>\n";\r
1008                 $feed.= "</rss>\n";\r
1009                 return $feed;\r
1010         }\r
1011 }\r
1012 \r
1013 \r
1014 \r
1015 /**\r
1016  * RSSCreator20 is a FeedCreator that implements RDF Site Summary (RSS) 2.0.\r
1017  *\r
1018  * @see http://backend.userland.com/rss\r
1019  * @since 1.3\r
1020  * @author Kai Blankenhorn <kaib@bitfolge.de>\r
1021  */\r
1022 class RSSCreator20 extends RSSCreator091 {\r
1023 \r
1024     function RSSCreator20() {\r
1025         parent::_setRSSVersion("2.0");\r
1026     }\r
1027     \r
1028 }\r
1029 \r
1030 \r
1031 /**\r
1032  * PIECreator01 is a FeedCreator that implements the emerging PIE specification,\r
1033  * as in http://intertwingly.net/wiki/pie/Syntax.\r
1034  *\r
1035  * @deprecated\r
1036  * @since 1.3\r
1037  * @author Scott Reynen <scott@randomchaos.com> and Kai Blankenhorn <kaib@bitfolge.de>\r
1038  */\r
1039 class PIECreator01 extends FeedCreator {\r
1040         \r
1041         function PIECreator01() {\r
1042                 $this->encoding = "utf-8";\r
1043         }\r
1044     \r
1045         function createFeed() {\r
1046                 $feed = "<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n";\r
1047                 $feed.= $this->_createStylesheetReferences();\r
1048                 $feed.= "<feed version=\"0.1\" xmlns=\"http://example.com/newformat#\">\n"; \r
1049                 $feed.= "    <title>".FeedCreator::iTrunc(htmlspecialchars($this->title),100)."</title>\n";\r
1050                 $this->truncSize = 500;\r
1051                 $feed.= "    <subtitle>".$this->getDescription()."</subtitle>\n";\r
1052                 $feed.= "    <link>".$this->link."</link>\n";\r
1053                 for ($i=0;$i<count($this->items);$i++) {\r
1054                         $feed.= "    <entry>\n";\r
1055                         $feed.= "        <title>".FeedCreator::iTrunc(htmlspecialchars(strip_tags($this->items[$i]->title)),100)."</title>\n";\r
1056                         $feed.= "        <link>".htmlspecialchars($this->items[$i]->link)."</link>\n";\r
1057                         $itemDate = new FeedDate($this->items[$i]->date);\r
1058                         $feed.= "        <created>".htmlspecialchars($itemDate->iso8601())."</created>\n";\r
1059                         $feed.= "        <issued>".htmlspecialchars($itemDate->iso8601())."</issued>\n";\r
1060                         $feed.= "        <modified>".htmlspecialchars($itemDate->iso8601())."</modified>\n";\r
1061                         $feed.= "        <id>".htmlspecialchars($this->items[$i]->guid)."</id>\n";\r
1062                         if ($this->items[$i]->author!="") {\r
1063                                 $feed.= "        <author>\n";\r
1064                                 $feed.= "            <name>".htmlspecialchars($this->items[$i]->author)."</name>\n";\r
1065                                 if ($this->items[$i]->authorEmail!="") {\r
1066                                         $feed.= "            <email>".$this->items[$i]->authorEmail."</email>\n";\r
1067                                 }\r
1068                                 $feed.="        </author>\n";\r
1069                         }\r
1070                         $feed.= "        <content type=\"text/html\" xml:lang=\"en-us\">\n";\r
1071                         $feed.= "            <div xmlns=\"http://www.w3.org/1999/xhtml\">".$this->items[$i]->getDescription()."</div>\n";\r
1072                         $feed.= "        </content>\n";\r
1073                         $feed.= "    </entry>\n";\r
1074                 }\r
1075                 $feed.= "</feed>\n";\r
1076                 return $feed;\r
1077         }\r
1078 }\r
1079 \r
1080 \r
1081 /**\r
1082  * AtomCreator03 is a FeedCreator that implements the atom specification,\r
1083  * as in http://www.intertwingly.net/wiki/pie/FrontPage.\r
1084  * Please note that just by using AtomCreator03 you won't automatically\r
1085  * produce valid atom files. For example, you have to specify either an editor\r
1086  * for the feed or an author for every single feed item.\r
1087  *\r
1088  * Some elements have not been implemented yet. These are (incomplete list):\r
1089  * author URL, item author's email and URL, item contents, alternate links, \r
1090  * other link content types than text/html. Some of them may be created with\r
1091  * AtomCreator03::additionalElements.\r
1092  *\r
1093  * @see FeedCreator#additionalElements\r
1094  * @since 1.6\r
1095  * @author Kai Blankenhorn <kaib@bitfolge.de>, Scott Reynen <scott@randomchaos.com>\r
1096  */\r
1097 class AtomCreator03 extends FeedCreator {\r
1098 \r
1099         function AtomCreator03() {\r
1100                 $this->contentType = "application/atom+xml";\r
1101                 $this->encoding = "utf-8";\r
1102         }\r
1103         \r
1104         function createFeed() {\r
1105                 $feed = "<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n";\r
1106                 $feed.= $this->_createGeneratorComment();\r
1107                 $feed.= $this->_createStylesheetReferences();\r
1108                 $feed.= "<feed version=\"0.3\" xmlns=\"http://purl.org/atom/ns#\"";\r
1109                 if ($this->language!="") {\r
1110                         $feed.= " xml:lang=\"".$this->language."\"";\r
1111                 }\r
1112                 $feed.= ">\n"; \r
1113                 $feed.= "    <title>".htmlspecialchars($this->title)."</title>\n";\r
1114                 $feed.= "    <tagline>".htmlspecialchars($this->description)."</tagline>\n";\r
1115                 $feed.= "    <link rel=\"alternate\" type=\"text/html\" href=\"".htmlspecialchars($this->link)."\"/>\n";\r
1116                 $feed.= "    <id>".htmlspecialchars($this->link)."</id>\n";\r
1117                 $now = new FeedDate();\r
1118                 $feed.= "    <modified>".htmlspecialchars($now->iso8601())."</modified>\n";\r
1119                 if ($this->editor!="") {\r
1120                         $feed.= "    <author>\n";\r
1121                         $feed.= "        <name>".$this->editor."</name>\n";\r
1122                         if ($this->editorEmail!="") {\r
1123                                 $feed.= "        <email>".$this->editorEmail."</email>\n";\r
1124                         }\r
1125                         $feed.= "    </author>\n";\r
1126                 }\r
1127                 $feed.= "    <generator>".FEEDCREATOR_VERSION."</generator>\n";\r
1128                 $feed.= $this->_createAdditionalElements($this->additionalElements, "    ");\r
1129                 for ($i=0;$i<count($this->items);$i++) {\r
1130                         $feed.= "    <entry>\n";\r
1131                         $feed.= "        <title>".htmlspecialchars(strip_tags($this->items[$i]->title))."</title>\n";\r
1132                         $feed.= "        <link rel=\"alternate\" type=\"text/html\" href=\"".htmlspecialchars($this->items[$i]->link)."\"/>\n";\r
1133                         if ($this->items[$i]->date=="") {\r
1134                                 $this->items[$i]->date = time();\r
1135                         }\r
1136                         $itemDate = new FeedDate($this->items[$i]->date);\r
1137                         $feed.= "        <created>".htmlspecialchars($itemDate->iso8601())."</created>\n";\r
1138                         $feed.= "        <issued>".htmlspecialchars($itemDate->iso8601())."</issued>\n";\r
1139                         $feed.= "        <modified>".htmlspecialchars($itemDate->iso8601())."</modified>\n";\r
1140                         $feed.= "        <id>".htmlspecialchars($this->items[$i]->link)."</id>\n";\r
1141                         $feed.= $this->_createAdditionalElements($this->items[$i]->additionalElements, "        ");\r
1142                         if ($this->items[$i]->author!="") {\r
1143                                 $feed.= "        <author>\n";\r
1144                                 $feed.= "            <name>".htmlspecialchars($this->items[$i]->author)."</name>\n";\r
1145                                 $feed.= "        </author>\n";\r
1146                         }\r
1147                         if ($this->items[$i]->description!="") {\r
1148                                 $feed.= "        <summary>".htmlspecialchars($this->items[$i]->description)."</summary>\n";\r
1149                         }\r
1150                         $feed.= "    </entry>\n";\r
1151                 }\r
1152                 $feed.= "</feed>\n";\r
1153                 return $feed;\r
1154         }\r
1155 }\r
1156 \r
1157 \r
1158 /**\r
1159  * MBOXCreator is a FeedCreator that implements the mbox format\r
1160  * as described in http://www.qmail.org/man/man5/mbox.html\r
1161  *\r
1162  * @since 1.3\r
1163  * @author Kai Blankenhorn <kaib@bitfolge.de>\r
1164  */\r
1165 class MBOXCreator extends FeedCreator {\r
1166 \r
1167         function MBOXCreator() {\r
1168                 $this->contentType = "text/plain";\r
1169                 $this->encoding = "ISO-8859-15";\r
1170         }\r
1171     \r
1172         function qp_enc($input = "", $line_max = 76) { \r
1173                 $hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'); \r
1174                 $lines = preg_split("/(?:\r\n|\r|\n)/", $input); \r
1175                 $eol = "\r\n"; \r
1176                 $escape = "="; \r
1177                 $output = ""; \r
1178                 while( list(, $line) = each($lines) ) { \r
1179                         //$line = rtrim($line); // remove trailing white space -> no =20\r\n necessary \r
1180                         $linlen = strlen($line); \r
1181                         $newline = ""; \r
1182                         for($i = 0; $i < $linlen; $i++) { \r
1183                                 $c = substr($line, $i, 1); \r
1184                                 $dec = ord($c); \r
1185                                 if ( ($dec == 32) && ($i == ($linlen - 1)) ) { // convert space at eol only \r
1186                                         $c = "=20"; \r
1187                                 } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { // always encode "\t", which is *not* required \r
1188                                         $h2 = floor($dec/16); $h1 = floor($dec%16); \r
1189                                         $c = $escape.$hex["$h2"].$hex["$h1"]; \r
1190                                 } \r
1191                                 if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted \r
1192                                         $output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay \r
1193                                         $newline = ""; \r
1194                                 } \r
1195                                 $newline .= $c; \r
1196                         } // end of for \r
1197                         $output .= $newline.$eol; \r
1198                 } \r
1199                 return trim($output); \r
1200         }\r
1201         \r
1202 \r
1203         /**\r
1204          * Builds the MBOX contents.\r
1205          * @return    string    the feed's complete text \r
1206          */\r
1207         function createFeed() {\r
1208                 for ($i=0;$i<count($this->items);$i++) {\r
1209                         if ($this->items[$i]->author!="") {\r
1210                                 $from = $this->items[$i]->author;\r
1211                         } else {\r
1212                                 $from = $this->title;\r
1213                         }\r
1214                         $itemDate = new FeedDate($this->items[$i]->date);\r
1215                         $feed.= "From ".strtr(MBOXCreator::qp_enc($from)," ","_")." ".date("D M d H:i:s Y",$itemDate->unix())."\n";\r
1216                         $feed.= "Content-Type: text/plain;\n";\r
1217                         $feed.= "       charset=\"".$this->encoding."\"\n";\r
1218                         $feed.= "Content-Transfer-Encoding: quoted-printable\n";\r
1219                         $feed.= "Content-Type: text/plain\n";\r
1220                         $feed.= "From: \"".MBOXCreator::qp_enc($from)."\"\n";\r
1221                         $feed.= "Date: ".$itemDate->rfc822()."\n";\r
1222                         $feed.= "Subject: ".MBOXCreator::qp_enc(FeedCreator::iTrunc($this->items[$i]->title,100))."\n";\r
1223                         $feed.= "\n";\r
1224                         $body = chunk_split(MBOXCreator::qp_enc($this->items[$i]->description));\r
1225                         $feed.= preg_replace("~\nFrom ([^\n]*)(\n?)~","\n>From $1$2\n",$body);\r
1226                         $feed.= "\n";\r
1227                         $feed.= "\n";\r
1228                 }\r
1229                 return $feed;\r
1230         }\r
1231         \r
1232         /**\r
1233          * Generate a filename for the feed cache file. Overridden from FeedCreator to prevent XML data types.\r
1234          * @return string the feed cache filename\r
1235          * @since 1.4\r
1236          * @access private\r
1237          */\r
1238         function _generateFilename() {\r
1239                 $fileInfo = pathinfo($_SERVER["PHP_SELF"]);\r
1240                 return substr($fileInfo["basename"],0,-(strlen($fileInfo["extension"])+1)).".mbox";\r
1241         }\r
1242 }\r
1243 \r
1244 \r
1245 /**\r
1246  * OPMLCreator is a FeedCreator that implements OPML 1.0.\r
1247  * \r
1248  * @see http://opml.scripting.com/spec\r
1249  * @author Dirk Clemens, Kai Blankenhorn\r
1250  * @since 1.5\r
1251  */\r
1252 class OPMLCreator extends FeedCreator {\r
1253 \r
1254         function OPMLCreator() {\r
1255                 $this->encoding = "utf-8";\r
1256         }\r
1257     \r
1258         function createFeed() {     \r
1259                 $feed = "<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n";\r
1260                 $feed.= $this->_createGeneratorComment();\r
1261                 $feed.= $this->_createStylesheetReferences();\r
1262                 $feed.= "<opml xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n";\r
1263                 $feed.= "    <head>\n";\r
1264                 $feed.= "        <title>".htmlspecialchars($this->title)."</title>\n";\r
1265                 if ($this->pubDate!="") {\r
1266                         $date = new FeedDate($this->pubDate);\r
1267                         $feed.= "         <dateCreated>".$date->rfc822()."</dateCreated>\n";\r
1268                 }\r
1269                 if ($this->lastBuildDate!="") {\r
1270                         $date = new FeedDate($this->lastBuildDate);\r
1271                         $feed.= "         <dateModified>".$date->rfc822()."</dateModified>\n";\r
1272                 }\r
1273                 if ($this->editor!="") {\r
1274                         $feed.= "         <ownerName>".$this->editor."</ownerName>\n";\r
1275                 }\r
1276                 if ($this->editorEmail!="") {\r
1277                         $feed.= "         <ownerEmail>".$this->editorEmail."</ownerEmail>\n";\r
1278                 }\r
1279                 $feed.= "    </head>\n";\r
1280                 $feed.= "    <body>\n";\r
1281                 for ($i=0;$i<count($this->items);$i++) {\r
1282                         $feed.= "    <outline type=\"rss\" ";\r
1283                         $title = htmlspecialchars(strip_tags(strtr($this->items[$i]->title,"\n\r","  ")));\r
1284                         $feed.= " title=\"".$title."\"";\r
1285                         $feed.= " text=\"".$title."\"";\r
1286                         //$feed.= " description=\"".htmlspecialchars($this->items[$i]->description)."\"";\r
1287                         $feed.= " url=\"".htmlspecialchars($this->items[$i]->link)."\"";\r
1288                         $feed.= "/>\n";\r
1289                 }\r
1290                 $feed.= "    </body>\n";\r
1291                 $feed.= "</opml>\n";\r
1292                 return $feed;\r
1293         }\r
1294 }\r
1295 \r
1296 \r
1297 \r
1298 /**\r
1299  * HTMLCreator is a FeedCreator that writes an HTML feed file to a specific \r
1300  * location, overriding the createFeed method of the parent FeedCreator.\r
1301  * The HTML produced can be included over http by scripting languages, or serve\r
1302  * as the source for an IFrame.\r
1303  * All output by this class is embedded in <div></div> tags to enable formatting\r
1304  * using CSS. \r
1305  *\r
1306  * @author Pascal Van Hecke\r
1307  * @since 1.7\r
1308  */\r
1309 class HTMLCreator extends FeedCreator {\r
1310 \r
1311         var $contentType = "text/html";\r
1312         \r
1313         /**\r
1314          * Contains HTML to be output at the start of the feed's html representation.\r
1315          */\r
1316         var $header;\r
1317         \r
1318         /**\r
1319          * Contains HTML to be output at the end of the feed's html representation.\r
1320          */\r
1321         var $footer ;\r
1322         \r
1323         /**\r
1324          * Contains HTML to be output between entries. A separator is only used in \r
1325          * case of multiple entries.\r
1326          */\r
1327         var $separator;\r
1328         \r
1329         /**\r
1330          * Used to prefix the stylenames to make sure they are unique \r
1331          * and do not clash with stylenames on the users' page.\r
1332          */\r
1333         var $stylePrefix;\r
1334         \r
1335         /**\r
1336          * Determines whether the links open in a new window or not.\r
1337          */\r
1338         var $openInNewWindow = true;\r
1339         \r
1340         var $imageAlign ="right";\r
1341         \r
1342         /**\r
1343          * In case of very simple output you may want to get rid of the style tags,\r
1344          * hence this variable.  There's no equivalent on item level, but of course you can \r
1345          * add strings to it while iterating over the items ($this->stylelessOutput .= ...)\r
1346          * and when it is non-empty, ONLY the styleless output is printed, the rest is ignored\r
1347          * in the function createFeed().\r
1348          */\r
1349         var $stylelessOutput ="";\r
1350 \r
1351         /**\r
1352          * Writes the HTML.\r
1353          * @return    string    the scripts's complete text \r
1354          */\r
1355         function createFeed() {\r
1356                 // if there is styleless output, use the content of this variable and ignore the rest\r
1357                 if ($this->stylelessOutput!="") {\r
1358                         return $this->stylelessOutput;\r
1359                 }\r
1360                 \r
1361                 //if no stylePrefix is set, generate it yourself depending on the script name\r
1362                 if ($this->stylePrefix=="") {\r
1363                         $this->stylePrefix = str_replace(".", "_", $this->_generateFilename())."_";\r
1364                 }\r
1365 \r
1366                 //set an openInNewWindow_token_to be inserted or not\r
1367                 if ($this->openInNewWindow) {\r
1368                         $targetInsert = " target='_blank'";\r
1369                 }\r
1370                 \r
1371                 // use this array to put the lines in and implode later with "document.write" javascript\r
1372                 $feedArray = array();\r
1373                 if ($this->image!=null) {\r
1374                         $imageStr = "<a href='".$this->image->link."'".$targetInsert.">".\r
1375                                                         "<img src='".$this->image->url."' border='0' alt='".\r
1376                                                         FeedCreator::iTrunc(htmlspecialchars($this->image->title),100).\r
1377                                                         "' align='".$this->imageAlign."' ";\r
1378                         if ($this->image->width) {\r
1379                                 $imageStr .=" width='".$this->image->width. "' ";\r
1380                         }\r
1381                         if ($this->image->height) {\r
1382                                 $imageStr .=" height='".$this->image->height."' ";\r
1383                         }\r
1384                         $imageStr .="/></a>";\r
1385                         $feedArray[] = $imageStr;\r
1386                 }\r
1387                 \r
1388                 if ($this->title) {\r
1389                         $feedArray[] = "<div class='".$this->stylePrefix."title'><a href='".$this->link."' ".$targetInsert." class='".$this->stylePrefix."title'>".\r
1390                                 FeedCreator::iTrunc(htmlspecialchars($this->title),100)."</a></div>";\r
1391                 }\r
1392                 if ($this->getDescription()) {\r
1393                         $feedArray[] = "<div class='".$this->stylePrefix."description'>".\r
1394                                 str_replace("]]>", "", str_replace("<![CDATA[", "", $this->getDescription())).\r
1395                                 "</div>";\r
1396                 }\r
1397                 \r
1398                 if ($this->header) {\r
1399                         $feedArray[] = "<div class='".$this->stylePrefix."header'>".$this->header."</div>";\r
1400                 }\r
1401                 \r
1402                 for ($i=0;$i<count($this->items);$i++) {\r
1403                         if ($this->separator and $i > 0) {\r
1404                                 $feedArray[] = "<div class='".$this->stylePrefix."separator'>".$this->separator."</div>";\r
1405                         }\r
1406                         \r
1407                         if ($this->items[$i]->title) {\r
1408                                 if ($this->items[$i]->link) {\r
1409                                         $feedArray[] = \r
1410                                                 "<div class='".$this->stylePrefix."item_title'><a href='".$this->items[$i]->link."' class='".$this->stylePrefix.\r
1411                                                 "item_title'".$targetInsert.">".FeedCreator::iTrunc(htmlspecialchars(strip_tags($this->items[$i]->title)),100).\r
1412                                                 "</a></div>";\r
1413                                 } else {\r
1414                                         $feedArray[] = \r
1415                                                 "<div class='".$this->stylePrefix."item_title'>".\r
1416                                                 FeedCreator::iTrunc(htmlspecialchars(strip_tags($this->items[$i]->title)),100).\r
1417                                                 "</div>";\r
1418                                 }\r
1419                         }\r
1420                         if ($this->items[$i]->getDescription()) {\r
1421                                 $feedArray[] = \r
1422                                 "<div class='".$this->stylePrefix."item_description'>".\r
1423                                         str_replace("]]>", "", str_replace("<![CDATA[", "", $this->items[$i]->getDescription())).\r
1424                                         "</div>";\r
1425                         }\r
1426                 }\r
1427                 if ($this->footer) {\r
1428                         $feedArray[] = "<div class='".$this->stylePrefix."footer'>".$this->footer."</div>";\r
1429                 }\r
1430                 \r
1431                 $feed= "".join($feedArray, "\r\n");\r
1432                 return $feed;\r
1433         }\r
1434     \r
1435         /**\r
1436          * Overrrides parent to produce .html extensions\r
1437          *\r
1438          * @return string the feed cache filename\r
1439          * @since 1.4\r
1440          * @access private\r
1441          */\r
1442         function _generateFilename() {\r
1443                 $fileInfo = pathinfo($_SERVER["PHP_SELF"]);\r
1444                 return substr($fileInfo["basename"],0,-(strlen($fileInfo["extension"])+1)).".html";\r
1445         }\r
1446 }       \r
1447 \r
1448 \r
1449 /**\r
1450  * JSCreator is a class that writes a js file to a specific \r
1451  * location, overriding the createFeed method of the parent HTMLCreator.\r
1452  *\r
1453  * @author Pascal Van Hecke\r
1454  */\r
1455 class JSCreator extends HTMLCreator {\r
1456         var $contentType = "text/javascript";\r
1457         \r
1458         /**\r
1459          * writes the javascript\r
1460          * @return    string    the scripts's complete text \r
1461          */\r
1462         function createFeed() \r
1463         {\r
1464                 $feed = parent::createFeed();\r
1465                 $feedArray = explode("\n",$feed);\r
1466                 \r
1467                 $jsFeed = "";\r
1468                 foreach ($feedArray as $value) {\r
1469                         $jsFeed .= "document.write('".trim(addslashes($value))."');\n";\r
1470                 }\r
1471                 return $jsFeed;\r
1472         }\r
1473     \r
1474         /**\r
1475          * Overrrides parent to produce .js extensions\r
1476          *\r
1477          * @return string the feed cache filename\r
1478          * @since 1.4\r
1479          * @access private\r
1480          */\r
1481         function _generateFilename() {\r
1482                 $fileInfo = pathinfo($_SERVER["PHP_SELF"]);\r
1483                 return substr($fileInfo["basename"],0,-(strlen($fileInfo["extension"])+1)).".js";\r
1484         }\r
1485         \r
1486 }       \r
1487 \r
1488 \r
1489 \r
1490 /*** TEST SCRIPT *********************************************************\r
1491 \r
1492 //include("feedcreator.class.php"); \r
1493 \r
1494 $rss = new UniversalFeedCreator(); \r
1495 $rss->useCached(); \r
1496 $rss->title = "PHP news"; \r
1497 $rss->description = "daily news from the PHP scripting world"; \r
1498 \r
1499 //optional\r
1500 //$rss->descriptionTruncSize = 500;\r
1501 //$rss->descriptionHtmlSyndicated = true;\r
1502 //$rss->xslStyleSheet = "http://feedster.com/rss20.xsl";\r
1503 \r
1504 $rss->link = "http://www.dailyphp.net/news"; \r
1505 $rss->feedURL = "http://www.dailyphp.net/".$PHP_SELF; \r
1506 \r
1507 $image = new FeedImage(); \r
1508 $image->title = "dailyphp.net logo"; \r
1509 $image->url = "http://www.dailyphp.net/images/logo.gif"; \r
1510 $image->link = "http://www.dailyphp.net"; \r
1511 $image->description = "Feed provided by dailyphp.net. Click to visit."; \r
1512 \r
1513 //optional\r
1514 $image->descriptionTruncSize = 500;\r
1515 $image->descriptionHtmlSyndicated = true;\r
1516 \r
1517 $rss->image = $image; \r
1518 \r
1519 // get your news items from somewhere, e.g. your database: \r
1520 //mysql_select_db($dbHost, $dbUser, $dbPass); \r
1521 //$res = mysql_query("SELECT * FROM news ORDER BY newsdate DESC"); \r
1522 //while ($data = mysql_fetch_object($res)) { \r
1523         $item = new FeedItem(); \r
1524         $item->title = "This is an the test title of an item"; \r
1525         $item->link = "http://localhost/item/"; \r
1526         $item->description = "<b>description in </b><br/>HTML"; \r
1527         \r
1528         //optional\r
1529         //item->descriptionTruncSize = 500;\r
1530         $item->descriptionHtmlSyndicated = true;\r
1531         \r
1532         $item->date = time(); \r
1533         $item->source = "http://www.dailyphp.net"; \r
1534         $item->author = "John Doe"; \r
1535          \r
1536         $rss->addItem($item); \r
1537 //} \r
1538 \r
1539 // valid format strings are: RSS0.91, RSS1.0, RSS2.0, PIE0.1, MBOX, OPML, ATOM0.3, HTML, JS\r
1540 echo $rss->saveFeed("RSS0.91", "feed.xml"); \r
1541 \r
1542 \r
1543 \r
1544 ***************************************************************************/\r
1545 ?>