#!/usr/bin/perl
use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use CGI::Cookie;
use HTML::Template;
#######################################################################
# ライブラリ
#######################################################################
#######################################################################
# 環境設定
#######################################################################
{ package cfg; do './mng/admin_cnf.pl'; }
my $tempfile = './news_disp%d.html';
my $datafile = './mng/news_dat%d.pl';
#######################################################################
# データ&変数宣言
#######################################################################
my $q;
my %para;
my %cookies;
my $news_no;
my $type;
my $index;
my $bnkey;
my $tpl;
my @data;
my @wdays = ('日', '月', '火', '水', '木', '金', '土');
my $utf8c = "(?:[\x00-\x7f]|[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3})";
#######################################################################
# 関数宣言
#######################################################################
sub ReadData();
#######################################################################
# メイン
#######################################################################
#======================================================================
# パラメータ受け取り
#======================================================================
$q = new CGI;
foreach ( $q->param() ) {
$para{$_} = $q->param($_);
$para{$_} =~ s/^\s+//;
$para{$_} =~ s/\s+$//;
}
$news_no = exists($para{'news_no'}) ? int($para{'news_no'}) : 0;
$type = $cfg::news_config[$news_no]{'type'};
$tempfile = sprintf($tempfile, $news_no);
$datafile = sprintf($datafile, $news_no);
#======================================================================
# select版:クッキー読み込み
#======================================================================
if ( $type == 2 ) {
$bnkey = sprintf('bn%d', $news_no);
%cookies = fetch CGI::Cookie;
$para{'news_no'} = $cookies{$bnkey}->value if ( exists($cookies{$bnkey}) );
}
#======================================================================
# データ読み込み
#======================================================================
ReadData();
#======================================================================
# お知らせIDを元にデータ特定
#======================================================================
$index = 0;
if ( exists($para{'id'}) ) {
for ( my $i=0; $i<@data; $i++ ) {
my ( $id ) = split(/<>/, $data[$i]);
if ( $id eq $para{'id'} ) {
$index = $i;
last;
}
}
}
#======================================================================
# 表示
#======================================================================
print "Content-type: text/html; charset=utf-8\n\n";
$tpl = HTML::Template->new(filename => $tempfile);
if ( @data ) {
my ( $id, $subject, $message ) = split(/<>/, $data[$index]);
$subject =~ s/&/&/ig;
$message =~ s/&/&/ig;
$message =~ s/
/
/ig;
$id =~ /^(\d{10})_/;
my ( $sec, $min, $hour, $mday, $mon, $year, $wday ) = localtime($1);
my $date = sprintf('%04d/%02d/%02d(%s)', $year+1900, $mon+1, $mday, $wdays[$wday]);
my $new = qq| NEW| if ( time - $1 < 60*60*24*$cfg::news_config[$news_no]{'new_days'} );
$tpl->param(subj => $subject);
$tpl->param(date => "$date$new");
$tpl->param(mesg => $message);
} else {
$tpl->param(title => $cfg::news_config[$news_no]{'title'}) if ( $type == 2 );
}
print $tpl->output();
exit(0);
#######################################################################
# サブルーチン
#######################################################################
#======================================================================
# データ読み込み
#======================================================================
sub ReadData() {
@data = ();
if ( open(DAT, "<$datafile") ) {
# @data = ;
while ( ) {
# WAVE DASH(U+301C, E3809C)、WAVY DASH(U+3030,E380B0) ⇒ FULLWIDTH TILDE(U+FF5E, EFBD9E)
$_ =~ s/\G($utf8c*?)(\xE3\x80\x9C|\xE3\x80\xB0)/$1\xEF\xBD\x9E/g;
push(@data, $_);
}
close(DAT);
}
}