feed_format = 'json'; } elseif($feed_format == 'atom1.0') { $this->feed_format = 'atom1.0'; } else { $this->feed_format = 'rss2.0'; } } /** * Sets the channel information for the RSS feed. * * @param array $channel The channel information */ function set_channel($channel) { $this->channel = $channel; } /** * Adds an item to the RSS feed. * * @param array $item The item. */ function add_item($item) { $this->items[] = $item; } /** * Generate the feed. * */ function generate_feed() { global $lang; // First, add the feed metadata. switch($this->feed_format) { // Ouput JSON formatted feed. case "json": $this->feed .= "{\n\t\"version\": ".json_encode('https://jsonfeed.org/version/1').",\n"; $this->feed .= "\t\"title\": \"".$this->channel['title']."\",\n"; $this->feed .= "\t\"home_page_url\": ".json_encode($this->channel['link']).",\n"; $this->feed .= "\t\"feed_url\": ".json_encode($this->channel['link']."syndication.php").",\n"; $this->feed .= "\t\"description\": ".json_encode($this->channel['description']).",\n"; $this->feed .= "\t\"items\": [\n"; $serial = 0; break; // Ouput Atom 1.0 formatted feed. case "atom1.0": $this->channel['date'] = gmdate("Y-m-d\TH:i:s\Z", $this->channel['date']); $this->feed .= "settings['charset']}\"?>\n"; $this->feed .= "\n"; $this->feed .= "\t<![CDATA[".$this->sanitize_content($this->channel['title'])."]]>\n"; $this->feed .= "\tsanitize_content($this->channel['description'])."]]>\n"; $this->feed .= "\tchannel['link']}syndication.php\"/>\n"; $this->feed .= "\t{$this->channel['link']}\n"; $this->feed .= "\tchannel['link']}\"/>\n"; $this->feed .= "\t{$this->channel['date']}\n"; $this->feed .= "\tMyBB\n"; break; // The default is the RSS 2.0 format. default: $this->channel['date'] = gmdate("D, d M Y H:i:s O", $this->channel['date']); $this->feed .= "settings['charset']}\"?>\n"; $this->feed .= "\n"; $this->feed .= "\t\n"; $this->feed .= "\t\t<![CDATA[".$this->sanitize_content($this->channel['title'])."]]>\n"; $this->feed .= "\t\t".$this->channel['link']."\n"; $this->feed .= "\t\tsanitize_content($this->channel['description'])."]]>\n"; $this->feed .= "\t\t".$this->channel['date']."\n"; $this->feed .= "\t\tMyBB\n"; } // Now loop through all of the items and add them to the feed. foreach($this->items as $item) { if(!$item['date']) { $item['date'] = TIME_NOW; } switch($this->feed_format) { // Output JSON formatted feed. case "json": ++$serial; $end = $serial < count($this->items) ? "," : ""; $item_id = explode('tid=', $item['link']); if(empty($item['updated'])) { $item['updated'] = $item['date']; } $this->feed .= "\t\t{\n"; $this->feed .= "\t\t\t\"id\": \"".end($item_id)."\",\n"; $this->feed .= "\t\t\t\"url\": ".json_encode($item['link']).",\n"; $this->feed .= "\t\t\t\"title\": ".json_encode($item['title']).",\n"; if(!empty($item['author'])) { $this->feed .= "\t\t\t\"author\": {\n\t\t\t\t\"name\": ".json_encode($item['author']['name']).",\n"; $this->feed .= "\t\t\t\t\"url\": ".json_encode($this->channel['link']."member.php?action=profile&uid=".$item['author']['uid'])."\n"; $this->feed .= "\t\t\t},\n"; } $this->feed .= "\t\t\t\"content_html\": ".json_encode($item['description']).",\n"; $this->feed .= "\t\t\t\"date_published\": \"".date('c', $item['date'])."\",\n"; $this->feed .= "\t\t\t\"date_modified \": \"".date('c', $item['updated'])."\"\n"; $this->feed .= "\t\t}".$end."\n"; break; // Output Atom 1.0 formatted feed. case "atom1.0": $item['date'] = date("Y-m-d\TH:i:s\Z", $item['date']); $this->feed .= "\t\n"; if(!empty($item['author'])) { $author = "channel['link']."member.php?action=profile&uid=".$item['author']['uid']."\">".$item['author']['name'].""; $this->feed .= "\t\t\n"; $this->feed .= "\t\t\tsanitize_content($author)."]]>\n"; $this->feed .= "\t\t\n"; } $this->feed .= "\t\t{$item['date']}\n"; if(empty($item['updated'])) { $item['updated'] = $item['date']; } else { $item['updated'] = date("Y-m-d\TH:i:s\Z", $item['updated']); } $this->feed .= "\t\t{$item['updated']}\n"; $this->feed .= "\t\t\n"; $this->feed .= "\t\t{$item['link']}\n"; $this->feed .= "\t\t<![CDATA[".$this->sanitize_content($item['title'])."]]>\n"; $this->feed .= "\t\tsanitize_content($item['description'])."]]>\n"; $this->feed .= "\t\tfalse\n"; $this->feed .= "\t\n"; break; // The default is the RSS 2.0 format. default: $item['date'] = date("D, d M Y H:i:s O", $item['date']); $this->feed .= "\t\t\n"; $this->feed .= "\t\t\t<![CDATA[".$this->sanitize_content($item['title'])."]]>\n"; $this->feed .= "\t\t\t".$item['link']."\n"; $this->feed .= "\t\t\t".$item['date']."\n"; if(!empty($item['author'])) { $author = "channel['link']."member.php?action=profile&uid=".$item['author']['uid']."\">".$item['author']['name'].""; $this->feed .= "\t\t\tsanitize_content($author)."]]>\n"; } $this->feed .= "\t\t\t".$item['link']."\n"; $this->feed .= "\t\t\t\n"; $this->feed .= "\t\t\t\n"; $this->feed .= "\t\t\n"; break; } } // Now, neatly end the feed. switch($this->feed_format) { case "json": $this->feed .= "\t]\n}"; break; case "atom1.0": $this->feed .= ""; break; default: $this->feed .= "\t\n"; $this->feed .= ""; } } /** * Sanitize content suitable for RSS feeds. * * @param string $string The string we wish to sanitize. * @return string The cleaned string. */ function sanitize_content($content) { $content = preg_replace("#&[^\s]([^\#])(?![a-z1-4]{1,10});#i", "&$1", $content); $content = str_replace("]]>", "]]]]>", $content); return $content; } /** * Output the feed. */ function output_feed() { global $lang; // Send an appropriate header to the browser. switch($this->feed_format) { case "json": header("Content-Type: application/json; charset=\"{$lang->settings['charset']}\""); break; case "atom1.0": header("Content-Type: application/atom+xml; charset=\"{$lang->settings['charset']}\""); break; default: header("Content-Type: text/xml; charset=\"{$lang->settings['charset']}\""); } // If the feed hasn't been generated, do so. if($this->feed) { echo $this->feed; } else { $this->generate_feed(); echo $this->feed; } } }