<? Php
Class rss74 {
// RSS feed title:
Var $ title = "Untitled ";
// RSS description:
Var $ desc = "";
// RSS base url
//-> Example: http://www.jonasjohn.de/
Var $ base_url = "";
// XSL file for the resulting RSS feed:
Var $ pai_file = 'RSS. xsl ';
// RSS 2.0 Specification (URL ):
Var $ doc_url = 'http: // blogs.law.harvard.edu/tech/rss ';
// Copyright text:
//-> Example: Copyright 2006, Jonas John
Var $ copyright = '';
// RSS language setting:
// Example: en-us, de-de, fr-fr
Var $ language = 'en-us ';
// Managing editor and webmaster:
// (Shocould contain a E-Mail adress)
Var $ managing_editor = '';
Var $ webmaster = '';
// Feedburner URL
// Example: http://feeds.feedburner.com/codedump-rss
// (If a fb url is set, all requests need t the Feedburner and Google
// Requests will be redricted to the feedburner URL)
Var $ feedburner_url = '';
// RSS generator:
Var $ generator = 'rss74/v0.3 ';
// Limit RSS entries:
// (Example: 20, 30, 40, 50, etc .)
Var $ limit_entries = 20;
// RSS items:
Var $ items = array ();
// Constructor:
Function rss74 (){
}
Function add_entry ($ entry ){
// Create date key:
$ Date = isset ($ entry ['Date'])? $ Entry ['Date']: time ();
// Add unique string:
$ Date. = '_'. md5 ($ entry ['title']);
$ This-> items [$ date] = $ entry;
}
Function _ get_val (& $ array, $ key ){
Return isset ($ array [$ key])? $ Array [$ key]: '';
}
Function _ exists_val (& $ array, $ key ){
Return isset ($ array [$ key]);
}
Function get_rss_headers ($ rss_webpath ){
Return '<link rel = "alternate" type = "application/rss + xml" title = "RSS" href = "'. $ rss_webpath. '"/> ';
}
Function print_rss (){
Global $ _ SERVER;
Krsort ($ this-> items );
$ This-> items = array_slice ($ this-> items, 0, $ this-> limit_entries );
$ First_item = array_keys ($ this-> items );
$ First_item = $ first_item [0];
$ Last_change = $ this-> items [$ first_item] ['Date'];
Header ('last-Modified: '. gmdate ('d, d m y h: I: S', $ last_change). 'Gmt ');
Header ('content-Type: text/xml; charset = utf-8 ');
If (! Empty ($ this-> feedburner_url )){
If (! Preg_match ("/feedburner/I", $ _ SERVER ['http _ USER_AGENT ']) and! Preg_match ("/google/I", $ _ SERVER ['http _ USER_AGENT ']) {
Header ('http/1.1 301 Moved Permanently ');
Header ('Location: '. $ this-> feedburner_url );
Print '<a href = "'. $ this-> feedburner_url. '"> Redirecting... </a> ';
Return true;
}
}
Print '<? Xml version = "1.0" encoding = "UTF-8"?> '."";
If ($ this-> pai_file! = '')
Print '<? Xml-stylesheet href = "rss. xsl" type = "text/xsl" media = "screen"?> '."";
Print '<! DOCTYPE rss [<! ENTITY % HTMLlat1 PUBLIC "-// W3C // ENTITIES Latin 1 for XHTML //" http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent ">]> '."";
Print "<! -- Hello! This web page is a RSS file that is meant to be read by a RSS reader application. Look at http://en.wikipedia.org/wiki/RSS to learn more about RSS. --> ";
Print '<rss version = "2.0" xmlns: xhtml = "http://www.w3.org/1999/xhtml"> '."";
Print '<channel> '."";
Print "". '<title> '.html entities ($ this-> title).' </title> '."";
If (! Empty ($ this-> desc ))
Print "". '<description> '.html entities ($ this-> desc).' </description> '."";
If (! Empty ($ this-> base_url ))
Print "". '<link> '.html entities ($ this-> base_url).' </link> '."";
Print "". '<lastBuildDate>'. date ('R', time (). '</lastBuildDate> '."";
Print "". '<pubDate>'. date ('R', time (). '</pubDate> '."";
If (! Empty ($ this-> generator ))
Print "". '<generator>'. $ this-> generator. '</generator> '."";
If (! Empty ($ this-> copyright ))
Print "". '<copyright>'. $ this-> copyright. '</copyright> '."";
If (! Empty ($ this-> doc_url ))
Print "". '<docs>'. $ this-> doc_url. '</docs> '."";
If (! Empty ($ this-> language ))
Print "<language>". $ this-> language. "</language> ";
If (! Empty ($ this-> managing_editor ))
Print "<managingEditor>". $ this-> managing_editor. "</managingEditor> ";
If (! Empty ($ this-> webmaster ))
Print "<webMaster>". $ this-> webmaster. "</webMaster> ";
While (list ($ num, $ item) = each ($ this-> items )){
Print "<item> ";
Print "<title>". htmlentities ($ this-> _ get_val ($ item, 'title'). "</title> ";
If ($ this-> _ exists_val ($ item, 'URL ')){
Print "<link>". $ this-> _ get_val ($ item, 'URL'). "</link> ";
Print "<guid isPermaLink =" true ">". $ this-> _ get_val ($ item, 'URL'). "</guid> ";
}
If ($ this-> _ exists_val ($ item, 'desc '))
Print "<description> <! [CDATA [". $ this-> _ get_val ($ item, 'desc')."]> </description> ";
If ($ this-> _ exists_val ($ item, 'date ')){
Print "<pubDate>". date ('R', intval ($ this-> _ get_val ($ item, 'date'). "</pubDate> ";
}
$ Cats = array ();
While (list ($ cn, $ citem) = each ($ cats )){
Print "<category> $ citem </category> ";
}
Print "</item> ";
}
Print "</channel> ";
Print "</rss> ";
Return true;
}
}
Instance
Include ('Inc. Rss74.php ');
// RSS items list:
$ Example_list = array ();
/*
** Create some test entries:
*/
$ M = rand (8, 30 );
For ($ x = 0; $ x <$ m; $ x ++ ){
// Create a random UNIX-Timestamp:
$ Date = rand (1166000000,116 6400000 );
$ Example_list [] = array (
'Title' => 'example RSS message # '. $ x,
'URL' => 'http: // www.jonasjohn.de/shanghai'. $ x,
'Desc' => 'This is a example message from RSS74! ',
'Date' => $ date
);
}
// Create new RSS object:
$ Rss = new rss74 ();
/*
** Set RSS informations:
*/
// RSS title:
$ Rss-> title = 'RSS example 2 ';
// RSS description:
$ Rss-> desc = 'This feed shows some random entries .';
// Base URL of your homepage:
$ Rss-> base_url = 'http: // www.example.org /';
// Limit entry count to 20
$ Rss-> limit_entries = 20;
// RSS Editor
$ Rss-> managing_editor = 'yourname <yourname@example.org> ';
// Webmaster name
$ Rss-> webmaster = 'yourname <yourname@example.org> ';
// Language:
$ Rss-> language = 'en-us ';
// Copyright message:
$ Rss-> copyright = 'copyright 2006, yourname ';
// Set Feedburner adress:
// $ Rss-> feedburner_url = 'http: // feeds.feedburner.com/codedump-rss ';
// (Empty) = No redirection
// Set "pai_file" to empty to disable the XSL file:
$ Rss-> pai_file = '';
// Add entries to the RSS object:
While (list ($ date, $ entry) = each ($ example_list )){
$ Rss-> add_entry (array (
'Title' => $ entry ['title'],
'URL' => $ entry ['URL'],
'Desc' => $ entry ['desc'],
'Date' => $ entry ['Date']
));
}
// Let rss74 do the rest:
$ Rss-> print_rss ();
?>