1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231:
<?php
$plugin_description = gettext("Merges several RSS feeds into one.");
$plugin_author = "Malte Müller (acrylian)";
$plugin_disable = (class_exists('SimpleXMLElement')) ? false : gettext('PHP <em>SimpleXML</em> is required.');
$option_interface = 'MergedRSSOptions';
$plugin_category = gettext('Feed');
if (isset($_GET['mergedrss'])) {
$feeds = getOption('mergedrss_feeds');
$feeds = explode(';', $feeds);
if (count($feeds) < 0) {
exitZP();
}
header("Content-type: text/xml");
$RSS_date = date("r", mktime(10, 0, 0, 9, 8, 2010));
if (isset($_GET['lang'])) {
$locale = sanitize($_GET['lang']);
} else {
$locale = getOption('locale');
}
$gallery = new Gallery();
$MergedRSS = new MergedRSS($feeds, getBare(get_language_string($gallery->getTitle(), $locale)), FULLWEBPATH, getBare(get_language_string($gallery->getDesc(), $locale)), $RSS_date);
$mergedrss_feeditems = getOption('mergedrss_items');
if (empty($mergedrss_feeditems)) {
$mergedrss_feeditems = 10;
}
$MergedRSS->export(false, true, $mergedrss_feeditems);
exitZP();
}
class MergedRSSOptions {
function __construct() {
setOptionDefault('mergedrss_items', 10);
}
function getOptionsSupported() {
return array(
gettext('RSS feeds to merge') => array('key' => 'mergedrss_feeds', 'type' => OPTION_TYPE_TEXTAREA,
'order' => 11,
'multilingual' => false,
'desc' => gettext('Enter the full urls of the feeds to merge separated by semicolons (e.g. "http://www.domain1.com/rss; http://www.domain2.com/rss")')),
gettext('Feed items:') => array('key' => 'mergedrss_items', 'type' => OPTION_TYPE_TEXTBOX,
'order' => 2,
'desc' => gettext("The number of new entries you want to appear in your site’s RSS feed")),
);
}
function handleOption($option, $currentValue) {
}
}
class MergedRSS {
private $myFeeds = null;
private $myTitle = null;
private $myLink = null;
private $myDescription = null;
private $myPubDate = null;
private $myCacheTime = null;
public function __construct($feeds, $channel_title = null, $channel_link = null, $channel_description = null, $channel_pubdate = null, $cache_time_in_seconds = 86400) {
$this->myTitle = $channel_title;
$this->myLink = $channel_link;
$this->myDescription = $channel_description;
$this->myPubDate = $channel_pubdate;
$this->myCacheTime = $cache_time_in_seconds;
$this->myFeeds = array();
if (!is_array($feeds)) {
$feeds = array($feeds);
}
foreach ($feeds as $feed) {
$this->myFeeds[] = trim($feed);
}
}
public function export($return_as_string = true, $output = false, $limit = null) {
$mergedrss_cache_file = SERVERPATH . '/' . STATIC_CACHE_FOLDER . '/rss/mergedrss.xml';
$use_mergedrss_cache = file_exists($mergedrss_cache_file) && time() - filemtime($mergedrss_cache_file) < $this->myCacheTime;
if ($use_mergedrss_cache) {
$xml = file_get_contents($mergedrss_cache_file);
} else {
$items = array();
foreach ($this->myFeeds as $RSS_url) {
$cache_file = SERVERPATH . '/' . STATIC_CACHE_FOLDER . '/rss/' . self::create_RSS_key($RSS_url) . '.xml';
if (strstr($RSS_url, FULLWEBPATH)) {
$use_cache = false;
} else {
$use_cache = file_exists($cache_file) && time() - filemtime($cache_file) < $this->myCacheTime;
}
if ($use_cache) {
$sxe = self::fetch_from_cache($cache_file);
$results = $sxe->channel->item;
} else {
$sxe = self::fetch_from_url($RSS_url);
$results = $sxe->channel->item;
if ($use_cache) {
if (isset($results)) {
$sxe->asXML($cache_file);
} else {
if (file_exists($cache_file)) {
$sxe = self::fetch_from_cache($cache_file);
$results = $sxe->channel->item;
}
}
}
}
if (isset($results)) {
foreach ($results as $item) {
$items[] = $item;
}
}
}
}
if (!$use_mergedrss_cache) {
$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$xml .= "<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\n";
$xml .= "<channel>\n";
if (isset($this->myTitle)) {
$xml .= "\t<title>" . html_encode($this->myTitle) . "</title>\n";
}
$xml .= "\t<atom:link href=\"" . PROTOCOL . ':/' . WEBPATH . '/index.php?mergedrss' . "\" rel=\"self\" type=\"application/rss+xml\" />\n";
if (isset($this->myLink)) {
$xml .= "\t<link>" . $this->myLink . "</link>\n";
}
if (isset($this->myDescription)) {
$xml .= "\t<description>" . html_encode($this->myDescription) . "</description>\n";
}
if (isset($this->myPubDate)) {
$xml .= "\t<pubDate>" . $this->myPubDate . "</pubDate>\n";
}
if (sizeof($items) > 0) {
usort($items, array($this, "self::compare_items"));
if (isset($limit)) {
array_splice($items, intval($limit));
}
for ($i = 0; $i < sizeof($items); $i++) {
$xml .= $items[$i]->asXML() . "\n";
}
}
$xml .= "</channel>\n</rss>";
$rssobj = new SimpleXMLElement($xml);
$rssobj->asXML($mergedrss_cache_file);
}
if ($output) {
header('Content-Type: application/xml');
echo $xml;
}
if ($return_as_string) {
return $xml;
}
}
private static function compare_items($a, $b) {
return strtotime($b->pubDate) - strtotime($a->pubDate);
}
private static function fetch_from_cache($cache_file) {
if (file_exists($cache_file)) {
return simplexml_load_file($cache_file);
}
return null;
}
private static function fetch_from_url($url) {
$sxe = new SimpleXMLElement($url, null, true);
return $sxe;
}
private static function create_RSS_key($url) {
return preg_replace('/[^a-zA-Z0-9\.]/', '_', $url) . 'cache';
}
}
?>