09-10-2007, 06:24 AM
|
|
|
|
X-Cart team
|
|
Join Date: Apr 2004
Location: Ulyanovsk, Russia
Posts: 125
|
|
|
Re: Syndicate X-Cart news as RSS 2.0 Channel
New version of the file works with x-cart 4.0.x and x-cart 4.1.x:
PHP Code:
<?php
/*
* news_feed.php.
* 2007-09-10. Zhuravlev Alexander (zaa@qualiteam.biz).
* License: MIT (http://www.opensource.org/licenses/mit-license.php)
*
* Syndicate X-Cart news as RSS 2.0 Channel
* Script supports HTTP confitional GET
* http://fishbowl.pastiche.org/2002/10/21/http_conditional_get_for_rss_hackers
*
* Note: News should be added at News Management page of x-cart
* administraton area.
*
*/
@include_once "./top.inc.php";
if (!defined('DIR_CUSTOMER')) die("ERROR: Can not initiate application! Please check configuration.");
define('XCART_SESSION_START', 1); # We do not need any sessions
if (@file_exists($xcart_dir."/init.php")) {
require $xcart_dir."/init.php";
} else {
require $xcart_dir."/config.php";
}
# *Required* RSS channel properties. [[[
$rss_channel = array();
# The name of the channel. It's how people refer to your service.
$rss_channel['title'] = $config['Company']['company_name'] . " News Headlines";
# The URL to the HTML website corresponding to the channel.
$rss_channel['link'] = $http_location;
# Phrase or sentence describing the channel
$rss_channel['description'] = "The latest news from " . $config['Company']['company_name'];
# The language the channel is written in.
# http://www.w3.org/TR/REC-html40/struct/dirlang.html#langcodes
$rss_channel['language'] = "en";
# By default we assume that text is provided in UTF-8 enconding.
# You may change this according to your needs.
$rss_channel['encoding'] = 'UTF-8';
# ]]]
define('RSS20_DATE_FORMAT', "D, d M Y H:i:s O");
# Fetch messages info
if ($active_modules['News_Management']) {
$messages_info = func_query_first("SELECT COUNT(*) as msg_count, MAX(send_date) AS last_send_time FROM $sql_tbl[newslists] AS nl, $sql_tbl[newsletter] AS n WHERE nl.listid = n.listid AND nl.avail='Y' AND nl.show_as_news='Y' ANd n.show_as_news = 'Y' ORDER BY n.send_date DESC");
}
if (!empty($messages_info) && $messages_info['last_send_time']) {
$last_mod_time = date(RSS20_DATE_FORMAT, $messages_info['last_send_time']);
$news_page_url = $http_location.DIR_CUSTOMER."/news.php";
$messages = func_query("SELECT n.newsid, n.subject, n.body, n.send_date FROM $sql_tbl[newslists] AS nl, $sql_tbl[newsletter] AS n WHERE nl.listid = n.listid AND nl.avail='Y' AND nl.show_as_news='Y' and n.show_as_news = 'Y' ORDER BY n.send_date DESC");
$items_code = "";
foreach ($messages as $m) {
$pubDate = date(RSS20_DATE_FORMAT, $m['send_date']);
$m['title'] = htmlspecialchars($m['subject']);
$items_code .= <<<EX
<item>
<title>{$m['subject']}</title>
<description><![CDATA[{$m['body']}]]></description>
<link>$news_page_url</link>
<guid isPermaLink="false">{$m['newsid']}@$news_page_url</guid>
<pubDate>$pubDate</pubDate>
</item>
EX;
}
} else {
$last_mod_time = date(RSS20_DATE_FORMAT, time());
}
# Channel header
$rss_channel['title'] = htmlspecialchars($rss_channel['title']);
$rss_channel['description'] = htmlspecialchars($rss_channel['description']);
$channel = <<<EX
<?xml version="1.0" encoding="{$rss_channel['encoding']}"?>
<rss version="2.0">
<channel>
<title>{$rss_channel['title']}</title>
<link>{$rss_channel['link']}</link>
<description>{$rss_channel['description']}</description>
<language>{$rss_channel['language']}</language>
<generator>X-Cart</generator>
<pubDate>$last_mod_time</pubDate>
<lastBuildDate>$last_mod_time</lastBuildDate>
EX;
if (!empty($items_code)) {
$channel .= $items_code;
unset($items_code);
}
$channel .= <<<EX
</channel>
</rss>
EX;
$channel_etag = '"'.md5($channel).'"';
# Conditional GET logic [[[
# Check if the channel contents should be passed to client.
# We analyze HTTP response headers: If-Modified-Since, If-None-Match
$IMS_FLAG = 0;
if (!empty($HTTP_SERVER_VARS['HTTP_IF_MODIFIED_SINCE'])) {
if (strtotime($HTTP_SERVER_VARS['HTTP_IF_MODIFIED_SINCE']) == strtotime($last_mod_time)) {
$IMS_FLAG = 1;
}
}
$INM_FLAG = 0;
if (!empty($HTTP_SERVER_VARS['HTTP_IF_NONE_MATCH'])) {
if ($HTTP_SERVER_VARS['HTTP_IF_NONE_MATCH'] == $channel_etag) {
$INM_FLAG = 1;
}
}
$pass_channel_to_client = !($IMS_FLAG || $INM_FLAG);
# ]]]
if (!$pass_channel_to_client) {
header("HTTP/1.0 304 Not Modified");
header('ETag: ' . $channel_etag);
} else {
# Output headers [[[
header("Content-Type: application/xml; charset=".$rss_channel['encoding']);
header("Content-length: " . strlen($channel));
header("Last-Modified: " . $last_mod_time);
header('ETag: ' . $channel_etag);
# ]]]
echo $channel;
}
# vim600: set et sw=4 sts=4 ts=4 fdm=marker fmr=[[[,]]]:
?>
|
|