<?php

include_once dirname( dirname(__FILE__) ).'/class/category.class.php';
include_once dirname( dirname(__FILE__) ).'/class/d3diaryConf.class.php';

$category =& Category::getInstance();
$d3dConf =& D3diaryConf::getInstance($mydirname);
$myts =& $d3dConf->myts;

$uid=0;
$uname="";
if(!empty($_GET['uid']) and $_GET['uid']>0){
	$uid=intval($_GET['uid']);
	$query = "SELECT uname, name FROM ".$xoopsDB->prefix("users")." 
		 WHERE uid='".$uid."'";
	$result = $xoopsDB->query($query);
	if ( $line = $xoopsDB->fetchArray($result)){
		if( $d3dConf->mod_config['use_name'] == 1 && !empty($line['name'])) {
			$uname=mb_convert_encoding($line['name']. constant('_MD_DIARY_PERSON'), "UTF-8");
		} else {
			$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']);
		$category->uid=$uid;
		$category->cid=$cid;
		$category->getchildren($mydirname);
		if($category->children){
			$whr_cid =" AND d.cid IN (".implode(",",$category->children).") ";
		} else {
			$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, $d3dConf;

	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 );

	// for neglect future entry
	$now = date( "Y-m-d H:i:s" );
	$whr_ctime = " AND create_time<'".$now."'";
	
	// 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 OR cat.uid=0)) 
			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.$whr_ctime;
	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 d.uid AS uid, d.bid AS bid, d.title AS title, d.diary AS diary,
			 d.create_time AS create_time, d.dohtml as dohtml 
			 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 OR cat.uid=0)) 
			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.$whr_ctime;

	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(mb_convert_encoding($line['title'], "UTF-8")).'</title>'."\n";
		echo '    <link>'.XOOPS_URL."/modules/".$mydirname."/detail.php?bid=".$line['bid'].'</link>'."\n";
		echo '    <dc:subject>'.htmlSpecialChars(mb_convert_encoding($line['title'], "UTF-8")).'</dc:subject>'."\n";
		echo '    <dc:creator/>'."\n";
		echo '    <description>'.htmlSpecialChars(mb_convert_encoding($d3dConf->func->substrTarea($line['diary'], $line['dohtml'], 300, true, "UTF-8"), "UTF-8", "auto")).'</description>'."\n";

		//date
		$lastmodify=split("[-: ]",$line['create_time']);
		$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);
}


?>
