#!/usr/bin/perl

##=======================================================================##
## AD-EDIT TEMPLATE-EDIT v4.0.1
## (c)2008-2011 NorenzPRO Ltd. TakafumiArai 2011/11/22
##=======================================================================##
use CGI::Carp qw(fatalsToBrowser);

##=======================================================================##
## 設定項目
##=======================================================================##

##★ライブラリ
use Encode;
require '../admin/aelib.cgi';

##★表示CGIスクリプトの名前
$targindex = 'index.cgi';

##★データファイルのパス
$datafile = './data.dat';

##=====================================================================================##
##メインルーチン
##=====================================================================================##

##システム設定ファイル読込
$servconf = '../admin/conf.dat';
if (-e $servconf) {
	open(SVCFGRD,"$servconf");
	@SVCONF = <SVCFGRD>;
	close(SVCFGRD);
	##文字コード判定→UTF8変換
	my $contCC=&checkCharCode($servconf);
	if ($contCC eq 'shiftjis') {
		foreach (@SVCONF) { Encode::from_to($_, 'shiftjis', 'utf8'); }
	}
	$Domain =$SVCONF[3];
	$Domain =~ s/\r//g; $Domain =~ s/\n//g;
	$RootPath =$SVCONF[10];
	$RootPath =~ s/\r//g; $RootPath =~ s/\n//g;
	$pcoutCode =$SVCONF[22];
	$pcoutCode =~ s/\r//g; $pcoutCode =~ s/\n//g;
}
if ($pcoutCode eq '') { $pcoutCode='sjis'; }

##設定ファイル読込
$conffile='./conf.dat';
open(CONFRD,"$conffile");
@CONF = <CONFRD>;
close(CONFRD);
##文字コード判定→UTF8変換
my $contCC=&checkCharCode($conffile);
if ($contCC eq 'shiftjis') {
	foreach (@CONF) { Encode::from_to($_, 'shiftjis', 'utf8'); }
}
$PageTitle =$CONF[3];
$PageTitle =~ s/\r//g;$PageTitle =~ s/\n//g;
$FileTypeLine =$CONF[13];
$FileTypeLine =~ s/\r//g;$FileTypeLine =~ s/\n//g;
@FileType=split(/:/,$FileTypeLine);
$DirName =$CONF[14];
$DirName =~ s/\r//g;$DirName =~ s/\n//g;
$hdPageTitle =$CONF[30];
$hdPageTitle =~ s/\r//g;$hdPageTitle =~ s/\n//g;
if ($hdPageTitle eq '') { $hdPageTitle=$PageTitle; }

##URLの取得
if ($RootPath ne '') {
	$SiteURL='http://'.$Domain.'/'.$RootPath.'/';
	$TargURL='http://'.$Domain.'/'.$RootPath.'/'.$DirName.'/'.$targindex;
	$ImageBase='http://'.$Domain.'/'.$RootPath.'/'.$DirName.'/file/';
} else {
	$SiteURL='http://'.$Domain.'/';
	$TargURL='http://'.$Domain.'/'.$DirName.'/'.$targindex;
	$ImageBase='http://'.$Domain.'/'.$DirName.'/file/';
}

##日付・時刻情報の取得
$ENV{'TZ'} = "JST-9"; 
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year += 1900;
$mon++;
$xmon=sprintf("%02d",$mon); ;
$xmday=sprintf("%02d",$mday); ;
$xhour=sprintf("%02d",$hour); ;
$xmin=sprintf("%02d",$min); ;
$DATE = $year.'-'.$xmon.'-'.$xmday.'T'.$xhour.':'.$xmin.':00+09:00';
$DATE3 = $year.$xmon.$xmday.$xhour.$xmin.'';

##==============メインアクション==============

##RDF出力
print "Content-type: text/html\n\n";

$rdfBody = <<"RDFOUT";
<?xml version="1.0" encoding="utf-8"?>

<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:content="http://purl.org/rss/1.0/modules/content/"
  xmlns:cc="http://web.resource.org/cc/"
  xmlns="http://purl.org/rss/1.0/">

<channel rdf:about="$SiteURL">
<title>$hdPageTitle</title>
<link>$SiteURL</link>
<description></description>
<dc:language>ja-JP</dc:language>
<dc:creator></dc:creator>
<dc:date>$DATE</dc:date>
<admin:generatorAgent rdf:resource="http://adedit.norenz.net/" />


<items>
<rdf:Seq><rdf:li rdf:resource="$TargURL" />
</rdf:Seq>
</items>

</channel>

RDFOUT
print $rdfBody;

&list;

$rdfBody = <<"RDFOUT";
</rdf:RDF>
RDFOUT
print $rdfBody;

exit;

##=====================================================================================##
##メインルーチンここまで
##=====================================================================================##
sub list {

&data_read;
@TRUE = grep(/code=/,@DATA);
##公開指定のみ@DATAへ
$i=0;
foreach(@TRUE) {
	$FILE{'vSt'}='200001010000';$FILE{'vFn'}='299912310000';
	@termLine=split(/\t/,$TRUE[$i]);
	foreach (@termLine) {
		($key, $value) = split(/=/,$_);
		$FILE{$key} = $value;
	}
	if (($DATE3 > $FILE{'vSt'})&&($DATE3 <= $FILE{'vFn'})) { push(@TERM,$TRUE[$i]); }
	$i++;
}
@PRE = grep(/\tshow=on\t/,@TERM);
@ItemList = grep(!/\ttitle=\t/,@PRE);

$itemLot=@ItemList-1;
$i=0;
foreach (0.. $itemLot) {
	$f1_code=''; $f0_imgtag='';
	@LINE = split(/\t/,$ItemList[$i]);
	foreach (@LINE) {
		($key, $value) = split(/=/,$_);
		$value =~ s/</\&lt\;/g;
		$value =~ s/>/\&gt\;/g;
		$value =~ s/\|eq\|/=/g;
		$value =~ s/\|br\|//g;
		$value =~ s/\"/\&quot\;/g;
		$value =~ s/\'/\&apos\;/g;
		$value =~ s/\|apos\|/\&apos\;/g;
		$value =~ s/\|quot\|/\&quot\;/g;
		$value =~ s/\&nbsp\;/ /g;
		$value =~ s/\&/\&amp;/g;
		$value =~ s/\&amp\;gt\;/\&gt\;/g;
		$value =~ s/\&amp\;lt\;/\&lt\;/g;
		$value =~ s/\&amp\;quot\;/\&quot\;/g;
		$value =~ s/\&amp\;apos\;/\&apos\;/g;
		$FILE{$key} = $value;
	}
	$f1_code = $FILE{'code'};
	$f1_show = $FILE{'show'};
	$f1_tshow = $FILE{'tshow'};
	$f1_align = $FILE{'align'};
	$f1_date = $FILE{'date'};
	$f1_date =~ s/20\d\d\///g;
	$f1_title = $FILE{'message1'};
	$f1_sub = $FILE{'sub'};
	$loti=1;
	$f1_message='';
	for (0..39) {
		$f1_message = $f1_message.$FILE{'message'.$loti}.'&lt;br /&gt;';
		if ($FILE{'message'.$loti} eq '') { last; }
		$loti++;
	}
	$regDATE = $FILE{'regDATE'};
	@itemDate=split(/\//,$regDATE);
	$viewDATE = $itemDate[0].'-'.$itemDate[1].'-'.$itemDate[2].'T'.$itemDate[3].':'.$itemDate[4].':00+09:00';
	
	##★添付ファイル1があれば表示
	$FtLot=@FileType-1;$j=0;
	for (0..$FtLot) {
		$f0_filepath = './file/'.$f1_code.'0.'.$FileType[$j];
		$h0_filepath = $ImageBase.$f1_code.'0.'.$FileType[$j];
		if ($FileType[$j] eq 'jpg') { $FtypeName='[JPG画像]'; }
		elsif ($FileType[$j] eq 'gif') { $FtypeName='[GIF画像]'; }
		elsif ($FileType[$j] eq 'png') { $FtypeName='[PNG画像]'; }
		elsif ($FileType[$j] eq 'txt') { $FtypeName='[テキスト書類]'; }
		elsif ($FileType[$j] eq 'doc') { $FtypeName='[WORD書類]'; }
		elsif ($FileType[$j] eq 'docx') { $FtypeName='[WORD2007書類]'; }
		elsif ($FileType[$j] eq 'xls') { $FtypeName='[EXCEL書類]'; }
		elsif ($FileType[$j] eq 'xlsx') { $FtypeName='[EXCEL2007書類]'; }
		elsif ($FileType[$j] eq 'ppt') { $FtypeName='[PPT書類]'; }
		elsif ($FileType[$j] eq 'pptx') { $FtypeName='[PPT2007書類]'; }
		elsif ($FileType[$j] eq 'jtd') { $FtypeName='[一太郎書類]'; }
		elsif ($FileType[$j] eq 'pdf') { $FtypeName='[PDF書類]'; }
		elsif ($FileType[$j] eq 'mp3') { $FtypeName='[MP3音声]'; }
		elsif ($FileType[$j] eq 'wma') { $FtypeName='[WMA音声]'; }
		elsif ($FileType[$j] eq 'mmf') { $FtypeName='[MMF音声]'; }
		elsif ($FileType[$j] eq 'mld') { $FtypeName='[MLD音声]'; }
		elsif ($FileType[$j] eq 'wmv') { $FtypeName='[WMV動画]'; }
		elsif ($FileType[$j] eq 'mp4') { $FtypeName='[MPEG4動画]'; }
		elsif ($FileType[$j] eq 'ogg') { $FtypeName='[OGG動画]'; }
		elsif ($FileType[$j] eq '3gp') { $FtypeName='[QuickTime動画]'; }
		elsif ($FileType[$j] eq '3g2') { $FtypeName='[QuickTime動画]'; }
		elsif ($FileType[$j] eq 'flv') { $FtypeName='[FLV動画]'; }
		elsif ($FileType[$j] eq 'zip') { $FtypeName='[ZIPファイル]'; }
		elsif ($FileType[$j] eq 'lzh') { $FtypeName='[LZHファイル]'; }
		if (-e $f0_filepath) {
			$f0_imgtag ='&lt;a href=&quot;'.$h0_filepath.'&quot;&gt;'.$FtypeName.'&lt;/a&gt;';
			last;
		} else { $f0_imgtag=''; }
		$j++;
	}
	##★添付ファイル2があれば表示
	$FtLot=@FileType-1;$j=0;
	for (0..$FtLot) {
		$f1_filepath = './file/'.$f1_code.'1.'.$FileType[$j];
		$h1_filepath = $ImageBase.$f1_code.'1.'.$FileType[$j];
		if ($FileType[$j] eq 'jpg') { $FtypeName='[JPG画像]'; }
		elsif ($FileType[$j] eq 'gif') { $FtypeName='[GIF画像]'; }
		elsif ($FileType[$j] eq 'png') { $FtypeName='[PNG画像]'; }
		elsif ($FileType[$j] eq 'txt') { $FtypeName='[テキスト書類]'; }
		elsif ($FileType[$j] eq 'doc') { $FtypeName='[WORD書類]'; }
		elsif ($FileType[$j] eq 'docx') { $FtypeName='[WORD2007書類]'; }
		elsif ($FileType[$j] eq 'xls') { $FtypeName='[EXCEL書類]'; }
		elsif ($FileType[$j] eq 'xlsx') { $FtypeName='[EXCEL2007書類]'; }
		elsif ($FileType[$j] eq 'ppt') { $FtypeName='[PPT書類]'; }
		elsif ($FileType[$j] eq 'pptx') { $FtypeName='[PPT2007書類]'; }
		elsif ($FileType[$j] eq 'jtd') { $FtypeName='[一太郎書類]'; }
		elsif ($FileType[$j] eq 'pdf') { $FtypeName='[PDF書類]'; }
		elsif ($FileType[$j] eq 'mp3') { $FtypeName='[MP3音声]'; }
		elsif ($FileType[$j] eq 'wma') { $FtypeName='[WMA音声]'; }
		elsif ($FileType[$j] eq 'mmf') { $FtypeName='[MMF音声]'; }
		elsif ($FileType[$j] eq 'mld') { $FtypeName='[MLD音声]'; }
		elsif ($FileType[$j] eq 'wmv') { $FtypeName='[WMV動画]'; }
		elsif ($FileType[$j] eq 'mp4') { $FtypeName='[MPEG4動画]'; }
		elsif ($FileType[$j] eq 'ogg') { $FtypeName='[OGG動画]'; }
		elsif ($FileType[$j] eq '3gp') { $FtypeName='[QuickTime動画]'; }
		elsif ($FileType[$j] eq '3g2') { $FtypeName='[QuickTime動画]'; }
		elsif ($FileType[$j] eq 'flv') { $FtypeName='[FLV動画]'; }
		elsif ($FileType[$j] eq 'zip') { $FtypeName='[ZIPファイル]'; }
		elsif ($FileType[$j] eq 'lzh') { $FtypeName='[LZHファイル]'; }
		if (-e $f1_filepath) {
			$f1_imgtag ='&lt;a href=&quot;'.$h1_filepath.'&quot;&gt;'.$FtypeName.'&lt;/a&gt;';
			last;
		} else { $f1_imgtag=''; }
		$j++;
	}

	##★画面リストの変更はここを訂正
	$rdfBody = <<"RDFOUT";
<item rdf:about="$TargURL?pg=$f1_code">
<title>$f1_title</title>
<link>$TargURL?pg=$f1_code</link>
<description>$f1_title</description>
<content:encoded>$f1_message $f0_imgtag $f1_imgtag </content:encoded>
<dc:creator></dc:creator>
<dc:date>$DATE</dc:date>
</item>

RDFOUT
	print $rdfBody;
	$i++;
}


}
##=======================================================================================
sub data_read {
	if (open(DBREAD,"$datafile")) {
		@DATA = <DBREAD>;
		close(DBREAD);
		##文字コード判定→UTF8変換
		my $contCC=&checkCharCode($datafile);
		if ($contCC eq 'shiftjis') {
			foreach (@DATA) { Encode::from_to($_, 'shiftjis', 'utf8'); }
		}
	} else { &error('DBファイルの読込に失敗しました。'); }
}
##=======================================================================================
sub error {

$error = $_[0];

##★エラーメッセージの変更はここを訂正
$errHTML = <<"ERRORMESS";
<h4 align="center"><span style="color:#ff0000">ERROR</span></h4>
<div align="center">&nbsp;<br />$error</div>
ERRORMESS

if ($pcoutCode eq 'sjis') { Encode::from_to($errHTML, 'utf8', 'shiftjis'); }
print $errHTML;
exit;

}
##=======================================================================================
