[ Index ] |
PHP Cross Reference of MyBB |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * MyBB 1.6 4 * Copyright 2010 MyBB Group, All Rights Reserved 5 * 6 * Website: http://mybb.com 7 * License: http://mybb.com/about/license 8 * 9 * $Id$ 10 */ 11 12 class FeedGenerator 13 { 14 /** 15 * The type of feed to generate. 16 * 17 * @var string 18 */ 19 public $feed_format = 'rss2.0'; 20 21 /** 22 * The XML to output. 23 * 24 * @var string 25 */ 26 public $xml = ""; 27 28 /** 29 * Array of all of the items 30 * 31 * @var array 32 */ 33 public $items = array(); 34 35 /** 36 * Array of the channel information. 37 * 38 * @var array 39 */ 40 public $channel = array(); 41 42 /** 43 * Set the type of feed to be used. 44 * 45 * @param string The feed type. 46 */ 47 function set_feed_format($feed_format) 48 { 49 if($feed_format == 'atom1.0') 50 { 51 $this->feed_format = 'atom1.0'; 52 } 53 else 54 { 55 $this->feed_format = 'rss2.0'; 56 } 57 } 58 59 /** 60 * Sets the channel information for the RSS feed. 61 * 62 * @param array The channel information 63 */ 64 function set_channel($channel) 65 { 66 $this->channel = $channel; 67 } 68 69 /** 70 * Adds an item to the RSS feed. 71 * 72 * @param array The item. 73 */ 74 function add_item($item) 75 { 76 $this->items[] = $item; 77 } 78 79 80 /** 81 * Generate and echo XML for the feed. 82 * 83 */ 84 function generate_feed() 85 { 86 global $lang; 87 88 // First, add the feed metadata. 89 switch($this->feed_format) 90 { 91 // Ouput an Atom 1.0 formatted feed. 92 case "atom1.0": 93 $this->channel['date'] = gmdate("Y-m-d\TH:i:s\Z", $this->channel['date']); 94 $this->xml .= "<?xml version=\"1.0\" encoding=\"{$lang->settings['charset']}\"?>\n"; 95 $this->xml .= "<feed xmlns=\"http://www.w3.org/2005/Atom\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"; 96 $this->xml .= "\t<title type=\"html\"><![CDATA[".$this->sanitize_content($this->channel['title'])."]]></title>\n"; 97 $this->xml .= "\t<subtitle type=\"html\"><![CDATA[".$this->sanitize_content($this->channel['description'])."]]></subtitle>\n"; 98 $this->xml .= "\t<link rel=\"self\" href=\"{$this->channel['link']}syndication.php\"/>\n"; 99 $this->xml .= "\t<id>{$this->channel['link']}</id>\n"; 100 $this->xml .= "\t<link rel=\"alternate\" type=\"text/html\" href=\"{$this->channel['link']}\"/>\n"; 101 $this->xml .= "\t<updated>{$this->channel['date']}</updated>\n"; 102 $this->xml .= "\t<generator uri=\"http://mybb.com\">MyBB</generator>\n"; 103 break; 104 // The default is the RSS 2.0 format. 105 default: 106 $this->channel['date'] = gmdate("D, d M Y H:i:s O", $this->channel['date']); 107 $this->xml .= "<?xml version=\"1.0\" encoding=\"{$lang->settings['charset']}\"?>\n"; 108 $this->xml .= "<rss version=\"2.0\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"; 109 $this->xml .= "\t<channel>\n"; 110 $this->xml .= "\t\t<title><![CDATA[".$this->sanitize_content($this->channel['title'])."]]></title>\n"; 111 $this->xml .= "\t\t<link>".$this->channel['link']."</link>\n"; 112 $this->xml .= "\t\t<description><![CDATA[".$this->sanitize_content($this->channel['description'])."]]></description>\n"; 113 $this->xml .= "\t\t<pubDate>".$this->channel['date']."</pubDate>\n"; 114 $this->xml .= "\t\t<generator>MyBB</generator>\n"; 115 } 116 117 // Now loop through all of the items and add them to the feed XML. 118 foreach($this->items as $item) 119 { 120 if(!$item['date']) 121 { 122 $item['date'] = TIME_NOW; 123 } 124 switch($this->feed_format) 125 { 126 // Output Atom 1.0 format feed. 127 case "atom1.0": 128 $item['date'] = date("Y-m-d\TH:i:s\Z", $item['date']); 129 $this->xml .= "\t<entry xmlns=\"http://www.w3.org/2005/Atom\">\n"; 130 if(!empty($item['author'])) 131 { 132 $this->xml .= "\t\t<author>\n"; 133 $this->xml .= "\t\t\t<name type=\"html\" xml:space=\"preserve\"><![CDATA[".$this->sanitize_content($item['author'])."]]></name>\n"; 134 $this->xml .= "\t\t</author>\n"; 135 } 136 $this->xml .= "\t\t<published>{$item['date']}</published>\n"; 137 if(empty($item['updated'])) 138 { 139 $item['updated'] = $item['date']; 140 } 141 else 142 { 143 $item['updated'] = date("Y-m-d\TH:i:s\Z", $item['updated']); 144 } 145 $this->xml .= "\t\t<updated>{$item['updated']}</updated>\n"; 146 $this->xml .= "\t\t<link rel=\"alternate\" type=\"text/html\" href=\"{$item['link']}\" />\n"; 147 $this->xml .= "\t\t<id>{$item['link']}</id>\n"; 148 $this->xml .= "\t\t<title type=\"html\" xml:space=\"preserve\"><![CDATA[".$this->sanitize_content($item['title'])."]]></title>\n"; 149 $this->xml .= "\t\t<content type=\"html\" xml:space=\"preserve\" xml:base=\"{$item['link']}\"><![CDATA[".$this->sanitize_content($item['description'])."]]></content>\n"; 150 $this->xml .= "\t\t<draft xmlns=\"http://purl.org/atom-blog/ns#\">false</draft>\n"; 151 $this->xml .= "\t</entry>\n"; 152 break; 153 154 // The default is the RSS 2.0 format. 155 default: 156 $item['date'] = date("D, d M Y H:i:s O", $item['date']); 157 $this->xml .= "\t\t<item>\n"; 158 $this->xml .= "\t\t\t<title><![CDATA[".$this->sanitize_content($item['title'])."]]></title>\n"; 159 $this->xml .= "\t\t\t<link>".$item['link']."</link>\n"; 160 $this->xml .= "\t\t\t<pubDate>".$item['date']."</pubDate>\n"; 161 if($item['author']) 162 { 163 $this->xml .= "\t\t\t<dc:creator><![CDATA[".$this->sanitize_content($item['author'])."]]></dc:creator>\n"; 164 } 165 $this->xml .= "\t\t\t<guid isPermaLink=\"false\">".$item['link']."</guid>\n"; 166 $this->xml .= "\t\t\t<description><![CDATA[".$item['description']."]]></description>\n"; 167 $this->xml .= "\t\t\t<content:encoded><![CDATA[".$item['description']."]]></content:encoded>\n"; 168 $this->xml .= "\t\t</item>\n"; 169 break; 170 } 171 } 172 173 // Now, neatly end the feed XML. 174 switch($this->feed_format) 175 { 176 case "atom1.0": 177 $this->xml .= "</feed>"; 178 break; 179 default: 180 $this->xml .= "\t</channel>\n"; 181 $this->xml .= "</rss>"; 182 } 183 } 184 185 /** 186 * Sanitize content suitable for RSS feeds. 187 * 188 * @param string The string we wish to sanitize. 189 * @return string The cleaned string. 190 */ 191 function sanitize_content($content) 192 { 193 $content = preg_replace("#&[^\s]([^\#])(?![a-z1-4]{1,10});#i", "&$1", $content); 194 $content = str_replace("]]>", "]]]]><![CDATA[>", $content); 195 196 return $content; 197 } 198 199 /** 200 * Output the feed XML. 201 */ 202 function output_feed() 203 { 204 global $lang; 205 // Send an appropriate header to the browser. 206 switch($this->feed_format) 207 { 208 case "atom1.0": 209 header("Content-Type: application/atom+xml; charset=\"{$lang->settings['charset']}\""); 210 break; 211 default: 212 header("Content-Type: text/xml; charset=\"{$lang->settings['charset']}\""); 213 } 214 215 // Output the feed XML. If the feed hasn't been generated, do so. 216 if($this->xml) 217 { 218 echo $this->xml; 219 } 220 else 221 { 222 $this->generate_feed(); 223 echo $this->xml; 224 } 225 } 226 } 227 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Tue Oct 8 19:19:50 2013 | Cross-referenced by PHPXref 0.7.1 |