<?php

/*
	[SupeSite/X-Space] (C)2001-2006 Comsenz Inc.
	RSS功\能

	$RCSfile: rss.php,v $
	$Revision: 1.27 $
	$Date: 2006/09/28 09:44:55 $
*/

if(!defined('IN_SUPESITE')) {
	exit('Access Denied');
}

include_once(S_ROOT.'./include/common.inc.php');

$itemsarr = $wherearr = array();
$rssarr = array(
	'charset' => $_SCONFIG['charset'],
	'title' => $_SCONFIG['sitename'],
	'link' => S_URL,
	'description' => $_SCONFIG['sitename'],
	'copyright' => 'Copyright(C) '.$_SCONFIG['sitename'],
	'generator' => 'SupeSite/X-Space',
	'lastBuildDate' => sgmdate($_SGLOBAL['timestamp']),
	'items' => array()
);

if(!empty($_SGET['uid'])) {
	$_SGET['uid'] = intval($_SGET['uid']);
}
if(empty($_SGET['uid'])) {
	$rssarr['title'] = 'rss_url_error';
	$rssarr['link'] = S_URL;
	showrss($rssarr);
	exit;
}
$wherearr[] = 'uid=\''.$_SGET['uid'].'\'';

$type = 'blog';
$typearr = array('news', 'blog', 'image', 'goods', 'file', 'link');
if(!empty($_SGET['type']) && in_array ($_SGET['type'], $typearr)) {
	$type = $_SGET['type'];
}
$wherearr[] = 'type=\''.$type.'\'';

$space = getuserspace($_SGET['uid']);
$rssarr['title'] = $space['spacename'];
$rssarr['link'] = geturl('uid/'.$_SGET['uid'].'/action/space');
$rssarr['description'] = $space['announcement'];
$rssarr['copyright'] = 'Copyright(C) '.$space['spacename'];

$query = $_SGLOBAL['db']->query('SELECT * FROM '.tname('rss').' WHERE '.implode(' AND ',$wherearr));
if(empty($items) || $items['updatetime'] < $_SGLOBAL['timestamp']) {
	$itemsarr = updaterss($_SGET['uid'], $type);
} else {
	$itemsarr = unserialize($items['data']);
}
if(!empty($itemsarr)) {
	foreach($itemsarr as $key => $value) {
		$rssarr['items'][] = array(
			'title' => $value['subject'],
			'link' => geturl('uid/'.$_SGET['uid'].'/action/viewspace/itemid/'.$value['itemid']),
			'description' => $value['message'],
			'category' => $value['type'],
			'author' => $value['username'],
			'pubDate' => sgmdate($value['dateline'])
		);	
	}
}

showrss($rssarr);

function showrss($rssarr) {
	header("Content-type: application/xml");
	echo '<?xml version="1.0" encoding="'.$rssarr['charset'].'"?>
			<?xml-stylesheet type="text/css" href="'.S_URL.'/css/rss.css"?>
			<rss version="2.0">
			  <channel>
			    <title>'.$rssarr['title'].'</title>
			    <link>'.$rssarr['link'].'</link>
			    <description>'.$rssarr['description'].'</description>
			    <copyright>'.$rssarr['copyright'].'</copyright>
			    <generator>'.$rssarr['generator'].'</generator>
			    <lastBuildDate>'.$rssarr['lastBuildDate'].'</lastBuildDate>';
			    if(!empty($rssarr['items'])) {
				    foreach($rssarr['items'] as $key => $value) {
				    	echo '<item>
								<title>'.$value['title'].'</title>
								<link>'.$value['link'].'</link>
								<description><![CDATA['.$value['description'].']]></description>
								<category>'.$value['category'].'</category>
								<author>'.$value['author'].'</author>
								<pubDate>'.$value['pubDate'].'</pubDate>
							</item>
							';
				    }
			    }
	echo '
		</channel>
	</rss>';
}

function updaterss($uid, $type) {
	global $_SGLOBAL, $_SGET, $_SCONFIG;

	$rssarr = array();
	$othermsgarr = $attacharr = array();
	if(empty($_SCONFIG['rssnum'])) $_SCONFIG['rssnum'] = 10;
	$sql = 'SELECT i.subject, i.itemid, i.type, i.username, i.dateline, i.hash, b.* FROM '.tname('spaceitems').' i LEFT JOIN '.tname(gettypetablename($type)).' b ON b.itemid=i.itemid WHERE i.uid=\''.$uid.'\' AND i.type=\''.$type.'\''.' AND i.folder=\'1\' ORDER BY i.dateline DESC LIMIT 0,'.$_SCONFIG['rssnum'];
	$query = $_SGLOBAL['db']->query($sql);
	while($items = $_SGLOBAL['db']->fetch_array($query)) {
		$items['message'] = cutstr($items['message'], 255, 1);

		if($type == 'goods') {
			$othermsgarr[] = '商品名稱:'.$items['subject'];
			$othermsgarr[] = '新舊程度:'.($items['quality'] == 'new'?'全新':'二手');
			$othermsgarr[] = '商品價格:'.$items['price'].'元';
			$othermsgarr[] = '運   費:'.($items['chargemode'] == 'buy'?'買家承擔運費,'.'平郵:'.$items['chargemail'].'元,'.'快遞'.$items['chargeexpress'].'元,EMS:'.$items['chargeems'].'元':'賣家承擔運費');
			$othermsgarr[] = '所 在 地:'.$items['province'].'  '.$items['city'];
		} elseif($type == 'link') {
			$othermsgarr[] = '標題: '.'<a href="'.$items['url'].'" target="_blank">'.$items['subject'].'</a>';
			$othermsgarr[] = 'URL: <a href="'.$items['url'].'" target="_blank">'.$items['url'].'</a>';
		}

		if(!empty($othermsgarr)) $items['message'] = implode('<br>', $othermsgarr).'<br>'.$items['message'];
		if(!empty($items['hash'])) $attacharr[] = $items['hash'];
		$rssarr[$items['itemid']] = $items;
	}
	if(!empty($rssarr)) {

		if(!empty($attacharr)) {
			$query = $_SGLOBAL['db']->query('SELECT a.* FROM '.tname('attachments').' a  WHERE a.hash IN (\''.implode('\',\'',$attacharr).'\')');
			while($attach = $_SGLOBAL['db']->fetch_array($query)) {
				$attachstr = '';
				if($attach['type'] == 'file') {
					$attach['isimage'] = 0;
				}
				if($attach['isimage']) {
					$attachstr = 'Image: <br><img src="'.A_URL.'/'.$attach['thumbpath'].'" border="0" /><br>'.$attach['subject'].'<br>';
				} else {
					$attachstr = 'Attachment: <a href="'.S_URL.'/batch.download.php?aid='.$attach['aid'].'" target="_blank">'.$attach['filename'].'</a><br>';
				}
				$rssarr[$attach['itemid']]['message'] = $attachstr.'<br>'.$rssarr[$attach['itemid']]['message'];
			}
		}
		
		if($_SCONFIG['rssupdatetime'] < 300) $_SCONFIG['rssupdatetime'] = 300;
		$insertsqlarr = array(
			'uid' => $uid,
			'type' => $type,
			'data' => addslashes(serialize($rssarr)),
			'updatetime' => $_SGLOBAL['timestamp'] + $_SCONFIG['rssupdatetime']
		);
		replacetable('rss', $insertsqlarr);
	}
	return $rssarr;
}

?>
