#!/usr/bin/perl

#-初期設定------------------------------------------------------------------------------#

require './initial.cgi';
require './jcode.pl';

#-書き込み開始--------------------------------------------------------------------------#

#フォーム処理
if ($ENV{'REQUEST_METHOD'} eq "POST") {
	read(STDIN, $query_string, $ENV{'CONTENT_LENGTH'});
} else {
	$query_string = $ENV{'QUERY_STRING'};
}
@a = split(/&/, $query_string);
foreach $a (@a) {
	($keyneme, $value) = split(/=/, $a);
	$value =~ tr/+/ /;
	$value =~ s/%([0-9a-fA-F][0-9a-fA-F])/pack("C", hex($1))/eg;
	$value =~ s/,/\0/g;
	$value =~ s/&/&amp;/g;
	$value =~ s/</&lt;/g;
	$value =~ s/>/&gt;/g;
	&jcode'convert(*value,'sjis');
	$FORM{$keyneme} = $value;
}
#変数割り当て'
$mode = $FORM{'mode'};
$msgnum = $FORM{'msgnum'};
$target = $FORM{'target'};
$nunmove = $FORM{'nunmove'};
$jumpid = $FORM{'jumpid'};

#---------------------------------------------------------------------------------------#
#表示設定
#---------------------------------------------------------------------------------------#
#ヘッダー処理
print "Content-type: text/html; charset=Shift_JIS\n\n";
&mainbew;
sub mainbew{
	local @texts = &lockread;
	my $i;
	#ページ処理
	use POSIX;
	$page_total = ceil( @texts / $pmax ); #ページ数
	$page_start = ( $page - 1 ) * $pmax;  #表示を開始する履歴番号
	$page_end = $page_start + $pmax - 1; #表示を終了する履歴番号
	if( $page_end > @texts) { $page_end = @texts; }
	#表示数制御
	if($jumpid){
		for ($i = 0; $i < @texts; $i++) {
			($frontid) = split(/,/, $texts[$i]);
			if( $jumpid == $frontid ){$tagline = $i; last; }
		}
		$nowpage = ceil( ($tagline+1) / $pmax);
		$msgnum = $pmax*($nowpage-1);
		$oldpage = $pmax+$msgnum;
		$newpage = $msgnum;
	} elsif( $mode eq 'move' ){
		$oldpage = $pmax+$msgnum;
		$newpage = $msgnum-$pmax;
		$nowpage = $oldpage/$pmax;
	} else{
		$msgnum=0;
		$nowpage = $msgnum +1 ;
		$oldpage = $pmax;
	}
	#表示用html読み込み
	open(IN,$mainskin);
	$file =join('',<IN>);
	close(IN);
	&option($file);
	#スキンファイル切り分け
	($top_html,$main_html,$bottom_html) = split(/<!--maindata-->/, $file);
	print $top_html;
	$star_i = $msgnum;
	if ($star_i < 0) {$star_i = 0;}
	$end_i = $msgnum + $pmax;
	if ($end_i > @texts) {$end_i = @texts;}
	for ($i = $star_i; $i < $end_i; $i++) {
		&cut_log($texts[$i]);
		#スキンファイルへの埋め込み
		$mainpage = &print_main($main_html);
		print "<a name=\"$putid\"></a>";
		print $mainpage;
	}
	print $bottom_html;
	exit;
}
#エラー表示#----------------------------------------------------------------------------#
sub error{
	print "<html><head>\n";
	print "<meta http-equiv=\"content-type\" content=\"text/html;charset=Shift_JIS\">\n";
	print "<title>エラー</title>\n";
	print "</head>\n";
	print "<body bgcolor=\"#f5f5f5\">\n";
	print "<div align=\"center\">\n";
	print $_[0];
	print "</div></body>\n";
	exit;
}

sub headline{
	#表示用html読み込み
	open(IN,$headskin);
	$file =join('',<IN>);
	close(IN);
	($top_html,$main_html,$bottom_html) = split(/<!--maindata-->/, $file);
	print $top_html;
	local @texts = &lockread;
	if( $headmax > @texts ){ $headmax = @texts; }
	for ( $i=0; $i<$headmax; $i++) {
		&cut_log($texts[$i]);
		$putfilename = '';
		#if( lengs($putmessage) > $head_mesmax ){ $putmessage = substr($putmessage, 0, $head_mesmax ); }
		my $mainpage = &print_main($main_html);
		print $mainpage;
	}
	print $bottom_html;
	exit;
}

#------------------------------------------------------------------------------
#ファイルロックの判別とファイルオープン
sub lockread {
	if (!open(IN,"+<$logfile")) { &error("ログファイルが開けません。"); }
	if( $filelock eq 'on'){
		eval { flock(IN, 2); };
		if ($@) { &error("ファイルロックを使用出来ない環境です。"); }
		if ($@) { warn "WARNING: flock isn't supported.\n"; }
	}
	my @texts =<IN>;
	close(IN);
	return @texts;
}

#------------------------------------------------------------------------------
#親記事ログ切り分けのサブルーチン
sub cut_log{
	my @cut_logs = split(/<&>/, $_[0]);
	($putid,$putname,$puttitle,$putmessage,$putfilename,$puttimepre) = split(/,/, $cut_logs[0]);
	$putname =~ s/\0/,/g;
	$puttitle =~ s/\0/,/g;
	$putmessage =~ s/\0/,/g;
	#時間表示処理
	$puttimepre =~ s/\n//;
	($putyear,$putmonth,$putday,$putweek,$puthour,$putmin) = split(/<>/, $puttimepre);
	$putyear -= 2000;
	if( $putyear<10 ){ $putyear = '0'.$putyear; }
	#URLの自動リンク
	if($autlink eq 'on'){
		if($putmessage !~ /(<a)\s*(.*>.*<\/a>)/gi){
			$putmessage =~ s/(s?https?:\/\/[-_.!~*'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)/<a href="$1" target="_blank">$1<\/a>/ig;
		}
	}
}

#------------------------------------------------------------------------------
#オプション埋め込みのサブルーチン
sub option{
	$_[0] =~ s/<!--nowpage-->/$nowpage/gsi;
	$_[0] =~ s/<!--totalpage-->/$page_total/gsi;
	$_[0] =~ s/<!--logleng-->/@texts/gsi;
	$_[0] =~ s/(<a href="top")/<a href=\"$cgipas\"/gsi;
	if( $nowpage == 1 ){
		$_[0] =~ s/(<a href="newlog">)(.*?)(<\/a>)/$2/gsi;
	} else{
		$_[0] =~ s/(<a href="newlog">)(.*?)(<\/a>)/<a href=\"$cgipas?mode=move&msgnum=$newpage\"><img src="images\/bacbtn2.gif" width="67" height="11" border="0"><\/a>/gsi;
	}
	if($oldpage>($page_total-1)*$pmax){
		$_[0] =~ s/(<a href="oldlog">)(.*?)(<\/a>)/$2/gsi;
	} else{
		$_[0] =~ s/(<a href="oldlog">)(.*?)(<\/a>)/<a href=\"$cgipas?mode=move&msgnum=$oldpage\"><img src="images\/nextbtn.gif" width="67" height="11" border="0"><\/a>/gsi;
	}
	my ($pagelink,$i);
	for ($i = 0; $i < $page_total; $i++) {
		my $page_s = $i+1;
		my $target_s = $i*$pmax;
		if( $nowpage == $page_s ){ $pagelink .= "$page_s</a> \n"; }
		else{ $pagelink .= "<a href=\"$cgipas?mode=move&msgnum=$target_s\" style=\"color:#000000;text-decoration:none\">$page_s</a> \n"; }
	}
	$_[0] =~ s/<!--pagelink-->/$pagelink/gsi;
}

#------------------------------------------------------------------------------
#メイン記事表示のサブルーチン
sub print_main{
	my $i;
	$mainpage = $_[0];
	my $bewupfile = '';
	if( $putfilename ){
		$bewupfile = "<a href=\"$updir\/$putfilename\" target=\"_blank\">[アップロードファイル]</a>";
	}
	$mainpage =~ s/<!--upfile-->/$bewupfile/;
	$mainpage =~ s/<!--name-->/$putname/i;
	$mainpage =~ s/<!--title-->/$puttitle/i;
	$mainpage =~ s/<!--message-->/$putmessage/i;
	$mainpage =~ s/<!--year-->/$putyear/i;
	$mainpage =~ s/<!--month-->/$putmonth/i;
	$mainpage =~ s/<!--day-->/$putday/i;
	if( $week_bew == 2 ) {
		$putweek = ( "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" )[$putweek];
	} else{	$putweek = ( "日", "月", "火", "水", "木", "金", "土" )[$putweek]; }
	$mainpage =~ s/<!--week-->/$putweek/i;
	$mainpage =~ s/<!--hour-->/$puthour/i;
	$mainpage =~ s/<!--min-->/$putmin/i;
	my $lastday = &get_writetime;
	my $newmark = '';
	if( $putyear.$putmonth.$putday.$puthour.$putmin > $lastday ){ $newmark = '<img src="images/new.gif" width="35" height="21">'; }
	$mainpage =~ s/<!--new-->/$newmark/i;
	return $mainpage;
}
#時間取得
sub get_writetime{
	my($month,$millyear,$week);
	my($time) = time-86400*7;
	my( $sec, $min, $hour, $day, $mon, $year, $wday, $s_time ) = localtime( $time );
	$month = $mon+1;
	$millyear = $year+1900;
	if($hour<10){$hour="0".$hour;}
	if($min<10){$min="0".$min;}
	if($sec<10){$sec="0".$sec;}
	return $millyear.$month.$day.$hour.$min;
}

#End of Script
