#!/usr/local/bin/perl
###################################################
#webmemo.cgi WEBメモプログラム
# 2004.01.22 MARCO
# edit 2004.01.27 MARCO
#
# パーミッション
# webmemo.cgi(755)
# $datafile(666)
###################################################
require 'jcode.pl';
############ 設定項目 ##############
#データファイル
$defaultfile='webmemo.txt';
#ログファイル(つけた名前+連番+'.log')
$logfile='weblog';
#バックグランドの画像ファイル
$bg_gif='onpu.gif';
#ページのタイトル
$title='WebMemo';
#バックグランドカラー
$bg_color='#FFFFFF';
#ホームページアドレス
$hp_address='../../../index.html';
#ホームページアイコン
$hp_icon='top.gif';
#テキストの文字色
$textcolor='#003366';
$linkcolor='#006633';
#テーブルの背景色
$tblcolor1='#ffffcc';
$tblcolor2='#ccffff';
$tblcolor3='#eeeeee';
#自動ログ実行間隔
$autolog=50;
#1ページに表示する件数
$pagevew=10;
#クッキー名
$CookieName='webmemo';
#管理者パスワード
$master_key='password';
######################################
#日付と時間を取得
($sec,$min,$hour,$dd,$mm,$yy,$wd)=localtime();
#日付時刻のフォーマットを整える
$yy+=1900;
$mm++;
$mm=sprintf("%02d",$mm);
$dd=sprintf("%02d",$dd);
$hour=sprintf("%02d",$hour);
$min=sprintf("%02d",$min);
$udate="$yy/$mm/$dd $hour:$min";
#環境変数からクッキーの情報を取得
$cookies=$ENV{'HTTP_COOKIE'};
@pairs=split(/;/,$cookies);
#この掲示板のクッキーだけを抜き出す
foreach $pair (@pairs){
($name,$value)=split(/=/,$pair);
$name=~ s/ //g;
$DUMMY{$name}=$value;
}
#さらに、各パーツに分解する
@pairs=split(/,/,$DUMMY{$CookieName});
foreach $pair (@pairs) {
($name,$value)=split(/\!/,$pair);
$COOKIE{$name}=$value;
}
#フォームからのデータを取得
use CGI;
$form=CGI->new();
$sts=$form->param("status");
$ittl=$form->param("title");
$iurl=$form->param("url");
$ikey=$form->param("key");
$icom=$form->param("comment");
$idno=$form->param("delno");
$ipln=$form->param("pline");
$datafile=$form->param("filename");
if($datafile eq ''){
$datafile=$defaultfile;
}
#コメントの文字変換
$icom=~ tr/+/ /;
$icom=~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
$icom=~ s/</</g;
$icom=~ s/>/>/g;
$icom=~ s/\n//g;
$icom=~ s/\,/,/g;
&jcode'convert(*icom,'sjis');
open(DAT,"$datafile");
@DATA=<DAT>;
close(DAT);
opendir(DIR,'.');
@files=readdir(DIR);
closedir(DIR);
$fcnt=0;
foreach $jfile (@files){
$ext=substr($jfile,rindex($jfile,"."));
if($ext eq '.log'){
$fcnt++;
}
}
$fcnt++;
if($sts eq 'ins' && $iurl ne '' && $ikey eq
$master_key){
#タイトルがない場合はURLをセット
if($ittl eq ''){
$ittl=$iurl;
}
#連番を取得
$newno=0;
foreach(@DATA){
($no,$ttl,$url,$com,$date)=split(/\t/,$_);
if($no>$newno){$newno=$no;}
}
$newno++;
unshift(@DATA,"$newno\t$ittl\t$iurl\t$icom\t$udate\n");
$count=@DATA;
if($count>$autolog){
pop(@DATA);
}
#自動ログ出力
if($newno%$autolog==0){
$fcnt=sprintf("%05d",$fcnt);
$newlogfile="$logfile$fcnt\.log";
open(LOG,">$newlogfile");
print LOG @DATA;
close(LOG);
}
open(DAT,">$datafile");
print DAT @DATA;
close(DAT);
set_cookie();
$COOKIE{'key'}=$ikey;
}
if($sts eq 'del' && $ikey eq $master_key){
$i=0; $match=0;
foreach(@DATA){
($no,$ttl,$url,$com,$date)=split(/\t/,$_);
if($idno==$no){
splice(@DATA,$i,1);
$match=1;
last;
}
$i++;
}
if ($match) {
open(DAT,">$datafile");
print DAT @DATA;
close(DAT);
}
}
print "Content-type: text/html\n\n";
print "<html><head>";
print "<meta http-equiv=\"Content-Type\"
content=\"text/html;charset=x-sjis\">\n";
print "<STYLE TYPE=\"text/css\">\n";
print "<!--\n";
print "a:link { text-decoration:none; color:$linkcolor; }\n";
print "a:visited { text-decoration:none; color:$linkcolor; }\n";
print "a:active { text-decoration:none; color:$linkcolor; }\n";
print "a:hover { text-decoration:underline; color:$linkcolor; }\n";
print "-->\n";
print "</STYLE>\n";
print "<title>$title</title></head>\n";
print "<body bgcolor=$bg_color text=$textcolor link=$linkcolor
alink=$linkcolor vlink=$linkcolor background=$bg_gif>\n";
print "<center><font
size=5><b>$title</b></font></center><br>\n";
print "<table border=0 width=100%><tr><td>\n";
if($hp_icon ne ''){print "<a href=$hp_address><img src=$hp_icon
border=0></a>\n";}
else{print "<a href=$hp_address>HomePage</a>\n";}
print "</td><td align=right>";
if($fcnt>1){
print "<form action=webmemo.cgi method=POST>\n";
print "ログ<select name=filename>\n";
print "<option value=$defaultfile>現在のファイル<br>\n";
foreach $jfile (@files){
$ext=substr($jfile,rindex($jfile,"."));
if($ext eq '.log'){
print "<option value=$jfile>$jfile<br>\n";
}
}
print "</select><input type=submit value=開く></form>\n";
}
print "</td></tr></table>";
print "<form action=webmemo.cgi method=POST>\n";
print "<input type=hidden name=status value=del>\n";
print "<table border=0 width=100% cellspacing=0 cellpadding=0>\n";
$lcnt=0;
$wymd='';
if($ipln eq ''){$pline=0;}else{$pline=$ipln;}
$end_data=@DATA-1;
$page_end=$pline+($pagevew-1);
if($page_end>=$end_data){
$page_end=$end_data;
}
foreach(($pline .. $page_end)){
($no,$ttl,$url,$com,$date)=split(/\t/,$DATA[$_]);
$com=~ s/\r/<br>/g;
chomp($date);
$ymd=substr($date,0,10);
$hms=substr($date,11,5);
if($ymd ne $wymd){
print "<tr><td colspan=3 bgcolor=$tblcolor3><font
size=4> <b>$ymd</b></font></td></tr>\n";
$wymd=$ymd;
}
if($lcnt%2==0){$tbc=$tblcolor1;}
else{$tbc=$tblcolor2;}
print "<tr bgcolor=$tbc><td colspan=2><input type=radio
name=delno value=$no>\n";
print "■ <a
href=$url><b>$ttl</b></a></td>\n";
print "<td width=10%><font size=1>- $hms
-</font></td></tr>\n";
print "<tr bgcolor=$tbc><td
width=10%></td><td><font
size=2>$com</font></td><td></td></tr>\n";
$lcnt++;
}
if($datafile eq $defaultfile && @DATA>0){
print "<tr><td colspan=3 bgcolor=$tblcolor3 align=right><font
size=2>削除キー</font>";
print "<input type=password size=12 name=key
value=$COOKIE{'key'}>\n";
print "</font><input type=submit
value=削除></td></tr>\n";
}
print "</form></table>\n";
$next_line=$page_end + 1;
if($page_end!=$end_data){
print "<form method=POST action=webmemo.cgi>\n";
print "<input type=hidden name=pline value=$next_line>\n";
print "<input type=submit value=次のページ>\n";
print "</form>\n";
}
#投稿フォーム
if($datafile eq $defaultfile){
print "<form action=webmemo.cgi method=POST>\n";
print "<input type=hidden name=status value=ins>\n";
print "<table border=0>\n";
print "<tr><td><font
size=2>タイトル</font></td>";
print "<td><input type=text size=80
name=title></td></tr>\n";
print "<tr><td><font
size=2>URL</font></td>";
print "<td><input type=text size=80
name=url></td></tr>\n";
print "<tr><td><font size=2>メモ</font></td>";
print "<td><textarea name=comment rows=4
cols=60></textarea></td></tr>\n";
print "<tr><td><font size=2>キー</font></td>";
print "<td><input type=password size=12 name=key
value=$COOKIE{'key'}>\n";
print "</font><input type=submit
value=送信></td></tr></table>\n";
print "</form>\n";
}
print "</body></html>\n";
exit;
sub set_cookie{
$ENV{'TZ'} = "GMT";
local(@date)=localtime(time+10*86400);
$date[5]+=1900;
$date[3]=sprintf("%02d",$date[3]);
$date[2]=sprintf("%02d",$date[2]);
$date[1]=sprintf("%02d",$date[1]);
$date[0]=sprintf("%02d",$date[0]);
$wday=('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday')
[$date[6]];
$month=('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')
[$date[4]];
$date_gmt="$wday, $date[3]\-$month\-$date[5] $date[2]:$date[1]:$date[0]
GMT";
$cook="key\!$ikey";
print "Set-Cookie: $CookieName=$cook; expires=$date_gmt\n";
}