Web日記
#! /usr/local/bin/perl # 設定 $FILE = './dat/diary.dat'; # 日記保存ファイル $TITLE = 'Web日記'; # 日記のタイトル $CALENDAR = 5; # カレンダー表示月数 $MAX = $CALENDAR * 31; # 日記保存件数 $TITLEVIEW = 50; # 新着日記タイトルの表示件数 $DIARYVIEW = 5; # トップページの日記表示件数 $TITLEMAX = 30; # 題名文字数制限(全角) $DIARYMAX = 5000; # 日記文字数制限(全角) $PASSWORD = '1234'; # 書き込み・編集・削除用パスワード loadForm(); # フォームデータ取り込み loadData(); # データの読み込み・削除・書き込み print qq(Content-type: text/html; charset=Shift_JIS\n\n); print qq(<html>\n); print qq(<head>\n); print qq(<title>$TITLE</title>\n); print qq(</head>\n); print qq(<body link="#000000" vlink="#000000" style="margin:0;padding:0">\n); print qq(<table align="center" cellspacing="0" style="margin:0;width:100%">\n); print qq(<tr>\n); print qq(<td colspan="2">\n); printCalendar(); # カレンダー表示 print qq(</td>\n); print qq(</tr>\n); print qq(<tr>\n); print qq(<td valign="top" style="padding:5px;font-size:12px;width:20%">\n); printTitle(); # 新着日記タイトル一覧表示 print qq(</td>\n); print qq(<td valign="top" style="padding:5px;font-size:13px">\n); printDiary(); # 日記表示 print qq(</td>\n); print qq(</tr>\n); print qq(</table>\n); print qq(</body>\n); print qq(</html>\n); exit; #================================================ 日記の表示 sub printDiary { my $i; if($FORM{'date'}) { my ($year, $mon, $date, $title, $diary); for($i = 0 ; $i < @DATA ; ++$i) { ($year, $mon, $date, $title, $diary) = split(/\t/, $DATA[$i]); if($FORM{'year'} == $year and $FORM{'mon'} == $mon and $FORM{'date'} == $date) { last; } } if($FORM{'mode'} ne "input") { printf qq(<h1 style="padding:5px;background:#fc6;font-size:13px">%d月%d日 ), $FORM{'mon'} + 1, $FORM{'date'}; if($i < @DATA) { print qq($title</h1>\n); print qq($diary<br>\n); } else { print qq(日記は書かれていません。</h1>\n); } print qq(<form action="$ENV{'SCRIPT_NAME'}" method="POST" style="text-align:right">\n); print qq(<input type="hidden" name="mode" value="input">\n); print qq(<input type="hidden" name="year" value="$FORM{'year'}">\n); print qq(<input type="hidden" name="mon" value="$FORM{'mon'}">\n); print qq(<input type="hidden" name="date" value="$FORM{'date'}">\n); print qq(パスワード:); print qq(<input type="password" name="pass" size="5" value="$FORM{'pass'}"><br>\n); print qq(<input type="submit" value="書込/編集/削除"><br>\n); print qq(</form>\n); } else { if($FORM{'pass'} eq $PASSWORD) { if($i >= @DATA) { $title = ""; $diary = ""; } else { $diary =~ s/<br>/\x0D\x0A/g; } print qq(<form action="$ENV{'SCRIPT_NAME'}" method="POST">\n); print qq(■■■■■【 書込/編集 】■■■■■<br>\n); print qq(題名:<br>\n); print qq(<input type="text" name="title" size="80" maxlength="$TITLEMAX" value="$title"><br>\n); print qq(日記: <small>※$DIARYMAX文字以内</small><br>\n); print qq(<textarea name="diary" style="width:95%;height:300px">$diary</textarea><br>\n); print qq(<input type="submit" value=" 日記書込 ">\n); print qq(<input type="hidden" name="year" value="$FORM{'year'}">\n); print qq(<input type="hidden" name="mon" value="$FORM{'mon'}">\n); print qq(<input type="hidden" name="date" value="$FORM{'date'}">\n); print qq(<input type="hidden" name="pass" value="$FORM{'pass'}">\n); print qq(<input type="hidden" name="mode" value="write">\n); print qq(</form>\n); print qq(<br>\n); if($i < @DATA) { print qq(<form action="$ENV{'SCRIPT_NAME'}" method="POST">\n); print qq(■■■■■【 削 除 】■■■■■<br>\n); print qq(<input type="hidden" name="year" value="$FORM{'year'}">\n); print qq(<input type="hidden" name="mon" value="$FORM{'mon'}">\n); print qq(<input type="hidden" name="date" value="$FORM{'date'}">\n); print qq(<input type="hidden" name="pass" value="$FORM{'pass'}">\n); print qq(<input type="hidden" name="mode" value="delete">\n); print qq(<input type="submit" value=" 日記削除 ">\n); print qq(</form>\n); } } else { print qq(パスワードが違います!!<br>\n); } } } else { print qq(<h1 style="padding:5px;text-align:center;font-size:14px">$TITLE</h1>\n); if($DIARYVIEW > @DATA) { $DIARYVIEW = @DATA; } for($i = 0 ; $i < $DIARYVIEW ; ++$i) { my ($year, $mon, $date, $title, $diary) = split(/\t/, $DATA[$i]); printf qq(<h2 style="padding:5px;background:#fc6;font-size:14px">%d月%d日 ), $mon + 1, $date; print qq($title</h2>\n); print qq($diary<br>\n); print qq(<br><br>\n); } my ($sec, $min, $hour, $date, $mon, $year, $wday) = localtime time; print qq(<form action="$ENV{'SCRIPT_NAME'}" method="POST" style="text-align:right">\n); print qq(<input type="hidden" name="mode" value="input">\n); print qq(<input type="hidden" name="year" value="$year">\n); print qq(<input type="hidden" name="mon" value="$mon">\n); print qq(<input type="hidden" name="date" value="$date">\n); print qq(パスワード:); print qq(<input type="password" name="pass" size="5"><br>\n); print qq(<input type="submit" value="今日の日記を書く">\n); print qq(</form>\n); } } #================================================ 新着日記タイトル一覧表示 sub printTitle { my $i; print qq(<a href="$ENV{'SCRIPT_NAME'}" style="display:block;text-align:center">$TITLE</a>\n); print qq(<hr color="#cccccc">\n); if($TITLEVIEW > @DATA) { $TITLEVIEW = @DATA; } for($i = 0 ; $i < $TITLEVIEW ; ++$i) { my ($year, $mon, $date, $title, $diary) = split(/\t/, $DATA[$i]); printf qq(■%d/$date-), $mon + 1; print qq(<a href="$ENV{'SCRIPT_NAME'}?year=$year&mon=$mon&date=$date">$title</a><br>\n), } print qq(<hr color="#cccccc">\n); } #================================================ カレンダー表示 sub printCalendar { my $i; my ($sec, $min, $hour, $date, $mon, $year, $wday) = localtime time; my $oneday = 60 * 60 * 24; my $starttime = time - $oneday * ((31 * ($CALENDAR - 1)) + $date); for(; $starttime <= time ; $starttime += $oneday) { my ($sec, $min, $hour, $date, $mon, $year, $wday) = localtime $starttime; if($date == 1) { last; } } print qq(<table style="width:100%;font-size:12px;border-bottom:1px solid #f93">\n); for(; $starttime <= time ;) { my ($sec, $min, $hour, $date, $mon, $year, $wday) = localtime $starttime; if($mon % 2 == 0) { print qq(<tr style="background:#eee">\n); } else { print qq(<tr>\n); } printf qq(<th style="text-align:right">%d月</th>\n), $mon + 1; for($i = 1 ; $i <= 31 ; $i++) { my ($sec, $min, $hour, $date, $mon, $year, $wday) = localtime $starttime; if($i == $date) { if($starttime <= time) { if($FORM{'year'} == $year and $FORM{'mon'} == $mon and $FORM{'date'} == $date) { print qq(<th style="width:3%;background:#fc9">); } else { print qq(<th style="width:3%">); } print qq(<a href="$ENV{'SCRIPT_NAME'}?year=$year&mon=$mon&date=$date">$date</a>); print qq(</th>\n); } else { print qq(<th style="width:3%;color:#999">$date</th>\n); } $starttime += $oneday; } else { print qq(<th> </th>\n); } } print qq(</tr>\n); } print qq(</table>\n); } #================================================ データ読み込み sub loadData { open(FILE, "<$FILE"); # データ読み込み eval{ flock(FILE, 1) }; @DATA = <FILE>; close FILE; my $i; if($FORM{'mode'} and $FORM{'pass'} eq $PASSWORD) { if($FORM{'mode'} eq 'write') { # データ追加 for($i = 0 ; $i < @DATA ; ++$i) { my ($year, $mon, $date, $title, $diary) = split(/\t/, $DATA[$i]); if($FORM{'year'} == $year and $FORM{'mon'} == $mon and $FORM{'date'} == $date) { splice @DATA, $i, 1; last; } } $FORM{'mon'} = sprintf("%02d", $FORM{'mon'}); $FORM{'date'} = sprintf("%02d", $FORM{'date'}); if(length($FORM{'title'}) > $TITLEMAX * 2) { $FORM{'title'} = substr($FORM{'title'}, 0, $TITLEMAX * 2) . " "; } if(length($FORM{'diary'}) > $DIARYMAX * 2) { $FORM{'diary'} = substr($FORM{'diary'}, 0, $DIARYMAX * 2) . " "; } unshift @DATA, "$FORM{'year'}\t$FORM{'mon'}\t$FORM{'date'}\t$FORM{'title'}\t$FORM{'diary'}\t\n"; @DATA = sort { $b cmp $a } @DATA; while(@DATA > $MAX) { pop @DATA; } } elsif($FORM{'mode'} eq 'delete') { # データ削除 for($i = 0 ; $i < @DATA ; ++$i) { my ($year, $mon, $date, $title, $diary) = split(/\t/, $DATA[$i]); if($FORM{'year'} == $year and $FORM{'mon'} == $mon and $FORM{'date'} == $date) { splice @DATA, $i, 1; last; } } } open(FILE, ">$FILE"); # データ書き込み eval{ flock(FILE, 2) }; print FILE @DATA; close FILE; } } #================================================ フォームデータ取り込み sub loadForm { my ($query, $pair); if($ENV{'REQUEST_METHOD'} eq 'POST') { read(STDIN, $query, $ENV{'CONTENT_LENGTH'}); } else { $query = $ENV{'QUERY_STRING'}; } foreach $pair (split(/&/, $query)) { my ($key, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr(hex($1))/eg; $value =~ s/&/&/g; $value =~ s/</</g; $value =~ s/>/>/g; $value =~ s/\x0D\x0A/<br>/g; $value =~ tr/\t/ /; $FORM{$key} = $value; } }
〔 実行する 〕