#!/usr/bin/perl
#↑１行目にサーバへ設置されているPerlのパスを設定してください。
#不明な場合にはサーバ管理者へ問い合わせてください。
#（このスクリプトはPerl5で書かれています。）

##
## このファイルについて
##    「管理ページ実行ファイル」
##


#---------------------------------------------------------------------------#

#初期設定ファイルini.cgiを要求
require( "./ini.cgi" );

my %in = ();
&parse_params( \%in );

#処理開始
&start();


## 処理開始
## @return void
sub start {
	
	if( exists( $in{'mode'} ) && ($in{'mode'} eq 'info') ) {
		require( $LIB_DIR."appinfo.pl" );
		&appinfo::display();
	}
	
	#ロック
	&lock();
	
	#記事ファイルを開く
	if( !open( RECS, "<".$DATA_DIR."recs.cgi" ) ) {
		&error( '記事ファイルアクセスエラー。' );
	}
	
	#全行を読む
	my @rows = <RECS>;
	
	#ファイルを閉じる
	close( RECS );
	
	#逆順
	if( $REVERSE_ORDER ) { @rows = reverse @rows; }
	
	#ロック解除
	&unlock();
	
	
	#HTTPレスポンスヘッダとHTMLヘッダ
	&print_http_header();
	&print_header();
	
	#レコードを書き出す
	my $tg_win = "";
	if( $AUTO_LINK_TG ne "" ) { $tg_win = " target=\"$AUTO_LINK_TG\""; }
	
	for( my $i = 0; $i < @rows; $i++ ) {
		&parse_rec( $rows[$i] );
		
		#改行
		if( $rec{'auto_ret'} ) {
			$rec{'content'} =~ s/\t/<br>/g;
		}
		
		#HTMLタグ
		if( $rec{'tag'} ) {
			$rec{'content'} =~ s/&gt;/>/g;
			$rec{'content'} =~ s/&lt;/</g;
			$rec{'content'} =~ s/&quot;/\"/g;
			$rec{'content'} =~ s/&\#39;/\'/g;
		}
		
		#オートリンク
		if( $AUTO_LINK ) {
			$rec{'content'} =~ s/([^=^\"]|^)(http|https|ftp)(\:\/\/[\w\.\~\-\/\?\&\+\=\:\@\%\;\#\%]+)/$1<a href=\"$2$3\"$tg_win>$2$3<\/a>/g;
		}
		
		#表示レイアウト
		if( $LAYOUT == 0 ) {
			print << "END_OF_DOC";
 <tr>
    <td width="20" class="headline" align="center">■</td>
    <td width="80" class="date" align="left">$rec{'date'}</td>
    <td width="340" class="content" align="left">$rec{'content'}</td>
  </tr>
$REC_BOUNDARY
END_OF_DOC
		} else {
			print << "END_OF_DOC";
<font color="#F8C7BC">■</font><font class="headline">$rec{'headline'}</font><br><br>
<font class="content">$rec{'content'}</font>
<p></p>
<div align="right"><font class="date">$rec{'date'}</font></div>
$REC_BOUNDARY
END_OF_DOC
		}
	}
	
	#HTMLフッタ
	&print_footer();
	
	exit(0);
}



#---------------------------------------------------------------------------#


## この実行ファイルのHTTPレスポンスヘッダを出力
## @return void
sub print_http_header {
	#キャッシュ制御
	print "Pragma: no-cache\n";
	print "Cache-Control: no-cache\n";
	print "Expires: -1\n";
	#ロボット用
	print "Robots: index,follow\n";
	#コンテンツ形
	print "Content-Type: text/html;charset=Shift_JIS\n";
	#コネクション
	print "Proxy-Connection: close\n";
	print "Connection: close\n\n";
	return;
}


## HTMLヘッダを出力
## @return void
sub print_header {
	my $style = $LIB_DOCS_ADDR.'style_index.css';
	print << "END_OF_DOC";
<html lang="ja-JP">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
	<meta http-equiv="Content-Style-Type" content="text/css">
	<link rel="stylesheet" href="$style" type="text/css">
	<title>$TITLE</title>
</head>
<body>
<table width="440" border="0" cellspacing="0" cellpadding="0">
END_OF_DOC
	return;
}

## HTMLフッタを出力
## @return void
sub print_footer {
	print "</table>\n";
	print "</body>\n";
	print "</html>\n";
	return;
}




0;
