<?php
$myts =& MyTextSanitizer::getInstance();

$uid=0;
$uname="";
if(!empty($_GET['uid']) and $_GET['uid']>0){
	$uid=intval($_GET['uid']);
	$query = "SELECT uname FROM ".$xoopsDB->prefix("users")." 
		 WHERE uid='".$uid."'";
	$result = $xoopsDB->query($query);
	if ( $line = $xoopsDB->fetchArray($result)){
		//$uname=mb_convert_encoding($line['uname'], "UTF-8", "EUC-JP");
		$uname=mb_convert_encoding($line['uname']. constant('_MD_DIARY_PERSON'), "UTF-8");
	}else{
		$uid=0;
	}
}

	if(!empty($_GET['cid']) and $_GET['cid']>0){
		$cid=intval($_GET['cid']);
		$whr_cid = " AND d.cid=".$cid;
	} else {
		$cid=0;
		$whr_cid = "";
	}

//if(empty($uname)){exit();}

mkrdf_php4($mydirname, $uid, $uname, $cid, $whr_cid);

function mkrdf_php4($mydirname, $uid, $uname, $cid=0, $whr_cid="")
{
	global $myts, $xoopsDB, $xoopsConfig, $xoopsModule;

	header("Content-type: text/xml; charset=UTF-8");

echo '<?xml version="1.0" encoding="UTF-8"?>'."\n";
echo '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:cc="http://web.resource.org/cc/" xmlns="http://purl.org/rss/1.0/">'."\n";

	echo '  <channel rdf:about="'.XOOPS_URL.'">'."\n";
	if(empty($uname)){
		echo '    <title>'.mb_convert_encoding($xoopsModule->name(), "UTF-8"). ' - '. mb_convert_encoding($xoopsConfig['sitename'], "UTF-8").'</title>'."\n";
	}else{
		echo '    <title>'.$uname. ' - '.mb_convert_encoding($xoopsConfig['sitename'], "UTF-8").'</title>'."\n";
	}
	if(empty($uname)){
		echo '    <link>'.XOOPS_URL.'/modules/$mydirname/index.php?page=diarylist</link>'."\n";
	}else{
		echo '    <link>'.XOOPS_URL."/modules/$mydirname/index.php?req_uid=".$uid.'</link>'."\n";
	}
	echo '    <description/>'."\n";
	echo '    <dc:language>ja</dc:language>'."\n";
	echo '    <dc:creator/>'."\n";

	// get timezone offset
        $tzd  = date('O', time());
        $tzd  = substr( chunk_split( $tzd, 3, ':' ), 0, 6 );

	// dc:date
	$query = "SELECT d.create_time, d.uid FROM ".$xoopsDB->prefix($mydirname."_diary"). " d LEFT JOIN "
			.$xoopsDB->prefix($mydirname.'_config'). " c ON d.uid=c.uid LEFT JOIN "
			.$xoopsDB->prefix($mydirname.'_category')." cat ON (d.cid=cat.cid AND d.uid=cat.uid) 
			WHERE (d.openarea=0 OR d.openarea IS NULL) AND (c.openarea=0 OR c.openarea IS NULL) AND (cat.openarea=0 OR cat.openarea IS NULL) ".$whr_cid;
	if($uid>0){
		$query .= " AND uid='".$uid."' ";
	}
	$query .= " ORDER BY create_time DESC LIMIT 0,1";
	$result = $xoopsDB->query($query);
	if ( $line = $xoopsDB->fetchArray($result)){
		$entry_uid=intval($line['uid']);
		$ctime=split("[-: ]",$line['create_time']);
		$temp_time = mktime($ctime[3],$ctime[4],$ctime[5],$ctime[1],$ctime[2],$ctime[0]);

		//$tmp=gmdate("Y-m-d\TH:i:s+09:00", mktime($ctime[3],$ctime[4],$ctime[5],$ctime[1],$ctime[2],$ctime[0]));
		//$tmp=MyformatTimestamp(mktime($ctime[3],$ctime[4],$ctime[5],$ctime[1],$ctime[2],$ctime[0]), "Y-m-d\TH:i:s+09:00");
		$tmp=MyformatTimestamp($temp_time, "Y-m-d\TH:i:s".$tzd);
	}else{
		//$tmp=gmdate("Y-m-d")."T".gmdate("H:i:s")."+09:00";
		//$tmp=gmdate("Y-m-d")."T".gmdate("H:i:s").$tzd;
		$tmp=date("Y-m-d")."T".date("H:i:s").$tzd;
	}
	echo '    <dc:date>'.$tmp.'</dc:date>'."\n";
	
	if($uid>0){
		$query = "SELECT blogtype FROM ".$xoopsDB->prefix($mydirname."_config")." WHERE uid='".$uid."'";
		$result = $xoopsDB->query($query);
		$line = mysql_fetch_array($result, MYSQL_ASSOC);
	}

	// diary in this site -------------------------------------
    if(empty($uid) or empty($line['blogtype'])){
	
	
	// items
	$query = "SELECT * FROM ".$xoopsDB->prefix($mydirname."_diary")." d LEFT JOIN "
			.$xoopsDB->prefix($mydirname.'_config'). " c ON d.uid=c.uid LEFT JOIN "
			.$xoopsDB->prefix($mydirname.'_category')." cat ON (d.cid=cat.cid AND d.uid=cat.uid) 
			WHERE (d.openarea=0 OR d.openarea IS NULL) AND (c.openarea=0 OR c.openarea IS NULL) AND (cat.openarea=0 OR cat.openarea IS NULL) ".$whr_cid;

	if($uid>0){
		$query .= " AND d.uid='".$uid."' ";
	}
	$query .= " ORDER BY create_time DESC LIMIT 0,30";
	
	// items
	echo '    <items>'."\n";
	echo '      <rdf:Seq>'."\n";
	$result = $xoopsDB->query($query);
	while ( $line = $xoopsDB->fetchArray($result)){
		echo '<rdf:li rdf:resource="'.XOOPS_URL."/modules/$mydirname/detail.php?bid=".$line['bid'].'"/>'."\n";
	}
	echo '      </rdf:Seq>'."\n";
	echo '    </items>'."\n";
	echo '  </channel>'."\n";
	
	// item
	$result = $xoopsDB->query($query);
	while ( $line = $xoopsDB->fetchArray($result)){
		echo '  <item rdf:about="'.XOOPS_URL."/modules/".$mydirname."/detail.php?bid=".$line['bid'].'">'."\n";
		echo '    <title>'.htmlSpecialChars($myts->makeTboxData4Show(mb_convert_encoding($line['title'], "UTF-8"))).'</title>'."\n";
		echo '    <link>'.XOOPS_URL."/modules/".$mydirname."/detail.php?bid=".$line['bid'].'</link>'."\n";
		echo '    <description>'.strip_tags($myts->previewTarea(mb_convert_encoding($line['diary'], "UTF-8"))).'</description>'."\n";
		echo '    <dc:subject></dc:subject>'."\n";
		echo '    <dc:creator/>'."\n";

		//date
		$lastmodify=split("[-: ]",$line['create_time']);
//		$tmp=$lastmodify[0]."-".$lastmodify[1]."-".$lastmodify[2]."T".$lastmodify[3].":".$lastmodify[4].":".$lastmodify[5]."+09:00";
//		$tmp=MyformatTimestamp(mktime($lastmodify[3],$lastmodify[4],$lastmodify[5],$lastmodify[1],$lastmodify[2],$lastmodify[0]), "Y-m-d\TH:i:s+09:00");
		$tmp=MyformatTimestamp(mktime($lastmodify[3],$lastmodify[4],$lastmodify[5],$lastmodify[1],$lastmodify[2],$lastmodify[0]), "Y-m-d\TH:i:s".$tzd);
		echo '    <dc:date>'.$tmp.'</dc:date>'."\n";
		echo '  </item>'."\n";
	}
	
    }
	echo '</rdf:RDF>'."\n";
	// -------------------------------------

}


function MyformatTimestamp($time, $format, $timeoffset=""){
    global $xoopsConfig, $xoopsUser;
    $usertimestamp = xoops_getUserTimestamp($time, $timeoffset);
	return date($format, $usertimestamp);
}


?>
