function rss_create($items, $channel_title, $channel_description, $dest_file = false) { if (is_array($items) && count($items) > 0) { $rss_top = '<?xml version="1.0" encoding="iso-8859-1"?> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" > <channel rdf:about="http://' . PEAR_CHANNELNAME . '"> <link>http://' . PEAR_CHANNELNAME . '/</link> <dc:creator>' . PEAR_WEBMASTER_EMAIL . '</dc:creator> <dc:publisher>' . PEAR_WEBMASTER_EMAIL . '</dc:publisher> <dc:language>en-us</dc:language>'; $items_xml = "<items>\n<rdf:Seq>"; $item_entries = ''; foreach ($items as $item) { if (is_int($item['releasedate'])) { $date = date("Y-m-d\\TH:i:s-05:00", $item['releasedate']); } else { $date = date("Y-m-d\\TH:i:s-05:00", strtotime($item['releasedate'])); } /* allows to override the default link */ if (!isset($item['link'])) { $url = 'http://' . PEAR_CHANNELNAME . '/package/' . $item['name'] . '/download/' . $item['version'] . '/'; } else { $url = $item['link']; } if (!empty($item['version'])) { $title = $item['name'] . ' ' . $item['version']; } else { $title = $item['name']; } $items_xml .= '<rdf:li rdf:resource="' . $url . '"/>' . "\n"; $item_entries .= "<item rdf:about=" . '"' . $url . '"' . ">\n <title>{$title}</title>\n <link>{$url}</link>\n <content:encoded>" . htmlspecialchars(nl2br($item['releasenotes'])) . "\n </content:encoded>\n <dc:date>{$date}</dc:date>\n</item>\n"; $item_entries .= ""; } $items_xml .= "</rdf:Seq>\n</items>\n"; $rss_feed = $rss_top . $items_xml . "\n<title>{$channel_title}</title>\n<description>{$channel_description}</description>\n</channel>\n\n{$item_entries}\n</rdf:RDF>"; if ($dest_file && (!file_exists($dest_file) || filemtime($dest_file) < time() - $timeout)) { $stream = fopen($url, 'r'); $tmpf = tempnam('/tmp', 'RSS'); // Note the direct write from the stream here file_put_contents($tmpf, $stream); fclose($stream); rename($tmpf, $dest_file); } header("Content-Type: text/xml; charset=iso-8859-1"); echo $rss_feed; } else { rss_bailout(); } }
function rss_create($items, $channel_title, $channel_description, $dest_file = false) { if (is_array($items) && count($items) > 0) { $rss_top = <<<EOT <?xml version="1.0" encoding="utf-8"?> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" xmlns:dc="http://purl.org/dc/elements/1.1/"> <channel rdf:about="http://pecl.php.net/"> <link>http://pecl.php.net/</link> <dc:creator>php-webmaster@lists.php.net</dc:creator> <dc:publisher>php-webmaster@lists.php.net</dc:publisher> <dc:language>en-us</dc:language> EOT; $items_xml = "<items>\n<rdf:Seq>"; $item_entries = ''; foreach ($items as $item) { $date = date("Y-m-d\\TH:i:s-05:00", strtotime($item['releasedate'])); /* allows to override the default link */ if (!isset($item['link'])) { $url = 'http://' . PEAR_CHANNELNAME . '/get/' . $item['name'] . '/' . $item['version']; } else { $url = $item['link']; } if (!empty($item['version'])) { $title = $item['name'] . ' ' . $item['version']; } else { $title = $item['name']; } //$node = $this->newItem($title, $url, $item['releasenotes'], $date); $items_xml .= '<rdf:li rdf:resource="' . $url . '"/>' . "\n"; $item_entries .= "<item rdf:about=" . '"' . $url . '"' . ">\n<title>{$title}</title>\n <link>{$url}</link>\n <description>" . htmlspecialchars($item['releasenotes']) . "\n</description>\n<dc:date>{$date}</dc:date>\n</item>"; $item_entries .= ""; } $items_xml .= "</rdf:Seq>\n</items>\n"; $rss_feed = $rss_top . $items_xml . "\n<title>{$channel_title}</title>\n<description>{$channel_description}</description>\n</channel>\n{$item_entries}\n</rdf:RDF>"; /* lock free write, thx rasmus for the tip */ if ($dest_file && (!file_exists($dest_file) || filemtime($dest_file) < time() - $timeout)) { $stream = fopen($url, 'r'); $tmpf = tempnam('/tmp', 'YWS'); // Note the direct write from the stream here file_put_contents($tmpf, $stream); fclose($stream); rename($tmpf, $dest_file); } header("Content-Type: text/xml; charset=utf-8"); echo $rss_feed; } else { rss_bailout(); } }