#!/usr/bin/perl
#       ↑あなたが加入しているプロバイダの｢perl｣言語が使用できる


#         パスを指定します。一般的に｢#!/usr/local/bin/perl｣で大丈夫


#==============================================================================


#       BBS01 Version 01.01-98.3


#==============================================================================


#日本語コード変換モジュール


require 'jcode.pl';


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


#bbs01を設置するＵＲＬを設定


#コメント送信後、リロードしますので必ず指定


$reload = 'bbs01.pl';


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


#あなたのホームページのアドレス


$homepage = 'http://www.nagomi.com';


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


#メッセージを格納するデータベースファイル


$datafile = 'bbs01.txt';


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


#バックグランドの画像ファイル


$bg_gif = '';


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


#区切り線　画像を使用しない場合は''にする


$line_gif1 = '';
$line_gif2 = '';


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


#管理者削除モードのパスワード


$password = 'nagomi0701_1';


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


#掲示板の名前


$title = 'なごみ道場';


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


#バックグランドカラー


$bg_color = '#ffffff';


#テキストの文字色


$text_color = '#000000';


#リンク文字色


$link_color = '#0000ff';


#Ｖリンク文字色


$vlink_color = '#ff0088';


#Ａリンク文字色


$alink_color = '#ffdddd';


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


#コメント最大記憶数（極端に多くするとパフォーマンスが低下します）


$max = 100;


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


#1ページに表示する件数


$pagevew = 10;


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


#クッキーを格納する名前を設定する


$CookieName = 'bbs01';


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


#<body>タグは自動で付けられるのでそれ以降のヘッダー部


$title_html = <<"TITLE_HTML_END";


<!--　ここにページの先頭に表示するHTMLを自由に記入　-->


  <div align=center>


　<H1>\な\ご\み\道\場</H1>


  </div>


    <BR clear="all">


  <div align="center">  日々の生活を豊かにし未来志向のライフスタイルを実現するために・・
  </div>

<!--　ここまでページの先頭に表示するタイトルを自由に記入　　-->
TITLE_HTML_END
#==============================================================================
#     初期設定が必要なのはここまでです。
#==============================================================================
#日付と時刻を取得して、すべて２桁に統一する
$ENV{'TZ'} = "JST-9"; 
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
  $year = sprintf("%02d",$year);
  $month = sprintf("%02d",$mon + 1);
  $mday = sprintf("%02d",$mday);
  $hour = sprintf("%02d",$hour);
  $min = sprintf("%02d",$min);
  $sec = sprintf("%02d",$sec);
  #日付時刻のフォーマットを整えます
  $year += 1900;
  $youbi = ('日','月','火','水','木','金','土') [$wday];
  $date_now = "$year年$month月$mday日($youbi) $hour時$min分";

&cookie_read;
$agent = $ENV{'HTTP_USER_AGENT'};
$textwidth = 80;
if ($agent =~ /MSIE\ 3/i) { $textwidth = $textwidth * 1.5; }if ($ENV{'REQUEST_METHOD'} eq "POST") {  read(STDIN, $formdata, $ENV{'CONTENT_LENGTH'});
} else { $formdata = $ENV{'QUERY_STRING'}; }
@pairs = split(/&/,$formdata);
foreach $pair (@pairs) {
  ($name, $value) = split(/=/, $pair);
  $value =~ tr/+/ /;
  $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  $value =~ s/</&lt;/g;
  $value =~ s/>/&gt;/g;
  $value =~ s/\n//g;
  $value =~ s/\,/，/g;
  &jcode'convert(*value,'sjis');
  $FORM{$name} = $value;
}

if (!open(NOTE,"$datafile")) { &error(bad_file); }
@DATA = <NOTE>;
close(NOTE);

#押されたボタンや、呼び出しに対しての分岐
if ($FORM{'password'} eq $password) { &deletemode; }
elsif ($FORM{'action'} eq 'comment') { &com_html; }
elsif ($FORM{'action'} eq 'delete') { &delete; }
elsif ($FORM{'action'} eq 'regist') { &regist; }
elsif ($FORM{'action'} eq 'new') { &new_html; }
else { &html; }
#==============================================================================
#ヘッダー部分
#==============================================================================


sub html_head {
  print "Content-type: text/html\n\n";


  print "<html>\n";


  print "<head>\n";


  print "<title>" . $title . "</title>\n";


  print "<meta http-equiv=Content-Type content=text/html; charset=x-sjis>\n";


  print "</head>\n";


  print "<body bgcolor=$bg_color text=$text_color link=$link_color vlink=$vlink_color alink=$alink_color background=$bg_gif>\n";
  print "$title_html\n";
}
#==============================================================================
#フッター部分
#==============================================================================
sub html_foot {
  print "<BR><P>\n";
  print "<center>\n";
  print "<a href=$homepage>ＨＯＭＥ</a>\n";
  print "</center>\n";
  print "<BR><P>\n";
}
#==============================================================================
#新規投稿フォーム
#==============================================================================
sub comment_form {
  print "<div align=center><center>\n";
  print "<form name=inputform action=bbs01.pl method=POST>\n";
    print "<input type=hidden name=action value=regist>\n";
    print "<input type=hidden name=res value=$FORM{'res'}>\n";
    print "<table border=0>\n";
      print "<tr>";
      print "<td align=right>お名前:</td><td><input type=text size=35 name=name value=$COOKIE{'name'}></td></tr>\n";

print "<tr><td align=right>E-mail:</td><td><input type=text size=35 name=email value=$COOKIE{'email'}></td></tr>\n";
      print "<tr><td align=right>HomePage:</td><td><input type=text size=60 name=HP value=$COOKIE{'HP'}></td></tr>\n";
      if ($new eq 1) {
        print "<tr><td align=right>題名:</td><td><input type=text size=60 name=subject></td></tr>\n";
        print "<input type=hidden name=new value=1>";
      } else {
        print "<input type=hidden name=new value=0>";
      }
    print "</table>\n";
    print "<textarea name=comment rows=4 cols=$textwidth></textarea><br>";
    print "<input type=submit value=書き込み>\n";
  print "</form>\n";
  print "</center></div>\n";
}
#==============================================================================
sub button_form {
  print "<div align=center>\n";
  print "<form action=bbs01.pl method=POST>\n";
  print "<input type=button value=新規投稿 onclick=\'location.href=\"$reload?action=new\"\'>\n";
  print "</form>\n";
  print "</div>\n";
}
#==============================================================================


sub html {
  foreach $line (@DATA) {
    ($date,$code,$re,$name,$email,$HP,$subject,$comment) = split(/\,/,$line);
    if ($re < 1) { push(@MATCH,$line); }
  }
  $match = @MATCH;
  &html_head;
  &button_form;
  &listvew;
  print "<form method=POST action=bbs01.pl>\n";
    print "<input type=hidden name=action value=delmode>\n";
    print "<input type=password size=20 name=password>\n";
    print " <input type=submit value=管理者専用>\n";
  print "</form>\n";
  &html_foot;
  print "

<BR clear=all>\n";


  print "</body></html>

\n";  exit;


}


#==============================================================================


#コメント表示
#==============================================================================


sub com_html {


  $new='0';


  foreach $line (@DATA) {


    ($date,$code,$re,$name,$email,$HP,$subject,$comment) = split(/\,/,$line);


    if ($code == $FORM{'res'}) { push(@MATCH,$line); last; }


  }


  &html_head;


  &comment_form;


  print "<div align=center>\n";


  &listvew;


  print "</div>\n";


  &html_foot;


  print "</body></html>\n";


  exit;


}


#==============================================================================
sub new_html {
  $new='1';
  &html_head;
  print "<div align=center>\n";
  &comment_form;
  print "</div>\n";
  &html_foot;
  print "</body></html>\n";
  exit;
}
#==============================================================================
sub sea_html {
  foreach $line (@DATA) {
    ($date,$code,$re,$name,$email,$HP,$subject,$comment) = split(/\,/,$line);
    if ($re < 1 && index($line,$FORM{'key'}) >= 0) { push(@MATCH,$line); }


  }


  &html_head;


  print "<div align=center>\n";


  &listvew;


  print "</div>\n";


  &html_foot;


  print "</body></html>\n";


  exit;


}


#==============================================================================


sub cookie_regist {


  $ENV{'TZ'} = "GMT"; 


  ($c_sec,$c_min,$c_hour,$c_mday,$c_mon,$c_year,$c_wday,$c_yday,$c_isdst) = localtime(time + 30 * 86400);


  $c_year = sprintf("%02d",$c_year);


  $c_sec  = sprintf("%02d",$c_sec);


  $c_min  = sprintf("%02d",$c_min);


  $c_hour = sprintf("%02d",$c_hour);


  $c_mday = sprintf("%02d",$c_mday);


  $youbi = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday') [$c_wday];


  $month = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec') [$c_mon];


  $date_gmt = "$youbi, $c_mday\-$month\-$c_year $c_hour:$c_min:$c_sec GMT";


  $cook = "name\!$FORM{'name'}\,email\!$FORM{'email'}\,HP\!$FORM{'HP'}";


  print "Set-Cookie: $CookieName=$cook; expires=$date_gmt\n";


}


#==============================================================================


sub cookie_read {


  $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;


  }


}


#==============================================================================


#表示


#==============================================================================


sub listvew {


  if ($FORM{'pline'} eq '') { $pline = 0; } else { $pline = $FORM{'pline'}; }


  $end_data = @MATCH - 1;


  $page_end = $pline + ($pagevew - 1);


  if ($page_end >= $end_data) { $page_end = $end_data; }


  if ($line_gif1 eq '') { print "<hr width=80%>"; }


  else { print "<img src=$line_gif2>"; }


  foreach ($pline .. $page_end) {


    ($date,$code,$res,$name,$email,$HP,$subject,$comment) = split(/\,/,$MATCH[$_]);
    $comment =~ s/([^=^\"]|^)((http|ftp|mailto):[!#-9A-~]+)/$1<a href=$2 target=_top>ＨＯＭＥ<\/a>/g;
    $comment =~ s/\r/<br>/g;
    print "<form method=POST action=bbs01.pl>\n";
      print "<input type=hidden name=action value=comment>\n";
      print "<input type=hidden name=res value=$code>\n";
      print "<input type=hidden name=mailto value=$email>\n";
      print "<table border=0 cellspacing=2 width=600>\n";
      print "<tr><td>\n";
      print "<p><font size=4 color=#ff0088><b>$subject</b><BR>\n";
      #メールアドレスがあればリンクを設定
      if ($email ne '') { print "<B><a href=mailto:" . $email . ">" . $name . "</a></B>\n"; }


      else { print "<B>" . $name . "</B>\n"; }


      print "</font> さん\n";


      #URLがあればリンクを設定


      if ($HP ne '') { print " <a href=$HP target=_blank>HomePage</a>"; }


      print "<font size=-1> $date</font>\n";


      if ($FORM{'action'} ne 'comment') { print " <input type=submit value=追加>"; }


      print "</p>\n";


      print "<blockquote>\n";


      print "<p>$comment</p>\n";


      $i = 1;


      foreach $line (@DATA) {


        ($da,$co,$re,$na,$em,$H,$su,$com) = split(/\,/,$line);


        if ($code eq $re) {


          $com =~ s/\r/<br>/g;


          if ($i == 1) {


            $ulflag = 1;


            if ($line_gif1 eq '') { print "<hr width=80%>"; }


            else { print "<img src=$line_gif1>"; }


            print "<ul>\n";


          }


          $com =~ s/([^=^\"]|^)((http|ftp|mailto):[!#-9A-~]+)/$1<a href=$2 target=_top>ここを押して<\/a>/g;


          print "<li>\n";


          #メールアドレスがあればリンクを設定


          if ($em ne '') { print "<b><a href=mailto:" . $em . ">" . $na . "</a></b>\n"; }


          else { print "<b>" . $na . "</b>\n"; }


          print "</font> さん\n";


          #URLがあればリンクを設定


          if ($H ne '') { print " <a href=$H>HomePage</a>\n"; }


          print "<font size=2> 投稿日：$da</font><br>\n";


          print "$com</li><br>\　\n";


          $i++;


        }


      }


      if ($ulflag == 1) { print "</ul>\n"; }


    print "</blockquote>\n";


    print "</td></tr></table></form>\n";


    if ($line_gif1 eq '') { print "<hr width=80%>"; }


    else { print "<img src=$line_gif2><br>"; }


  }


  $next_line = $page_end + 1;


  if ($page_end ne $end_data) {


    print "<form method=POST action=bbs01.pl>\n";


      print "<input type=hidden name=action value=$FORM{'action'}>\n";


      print "<input type=hidden name=key value=$FORM{'key'}>\n";


      print "<input type=hidden name=pline value=" . $next_line . ">\n";




      print "<input type=submit value=次のページ>\n";


    print "</form>\n";


    print "<hr width=80%>\n";


  }


}


#==============================================================================


sub regist {


  &cookie_regist;


  if ($FORM{'name'} eq '') { &error(bad_name); }


  if ($FORM{'new'} eq '1') {


    if ($FORM{'subject'} eq '') { &error(bad_subject); }


  }


  if ($FORM{'comment'} eq '') { &error(bad_comment); }


  if ($FORM{'comment'} ne '') {


    $count = @DATA;


    if ($count >= $max) { pop(@DATA); }


    if ($count < 1) {


      $new_code = 1;


    } else {


      ($date,$code,$re,$name,$email,$HP,$subject,$comment) = split(/\,/,$DATA[0]);


      $new_code = $code + 1;


    }


    $value = "$date_now\,$new_code\,$FORM{'res'}\,$FORM{'name'}\,$FORM{'email'}\,$FORM{'HP'}\,$FORM{'subject'}\,$FORM{'comment'}\n";
    unshift(@DATA,$value);
    if (!open(NOTE,">$datafile")) { &error(bad_file); }
    print NOTE @DATA;
    close(NOTE);
  }
  print "Location: $reload?\n\n";
  exit;
}
#==============================================================================
sub deletemode {
  $count = @DATA;
  if ($count < 1) { &html; }
  &html_head;
  print "<form action=bbs01.pl method=POST>\n";
    print "<input type=hidden name=action value=delete>\n";
    print "削除コード：<input type=text size=39 name=delcode>\n";
    print " <input type=submit value=削除><br>\n";
    print "<font size=2>スペースで区切っていくつでも同時に削除することができます。</font>\n";
  print "</form>\n";
  print "<hr>\n";
  foreach $line (@DATA) {
    ($date,$code,$re,$name,$email,$HP,$subject,$comment) = split(/\,/,$line);


    print "<table border=0 width=100%>\n";


    print "<tr>\n";


      print "<td width=5% valign=top>[$code]</td>\n";


      print "<td valign=top>\n";


        print "<font color=$link_color size=+1><b>$subject</b></font>\n";


        print " 投稿者：\n";


        if ($email ne '') { print "<B><a href=mailto:" . $email . ">" . $name . "</a></B>\n"; }


        else { print "<b>" . $name . "</b>\n"; }


        print "<font size=-1>　投稿日：" . $date . "</font><br>\n";


        print "$comment\n";


      print "</td>\n";


    print "</tr></table>\n";


    print "<hr>\n";


  }


  print "<form action=bbs01.pl method=POST>\n";


    print "<input type=hidden name=action value=delete>\n";


    print "削除コード：<input type=text size=39 name=delcode>\n";


    print "　<input type=submit value=削除><br>\n";


    print "<font size=2>スペースで区切っていくつでも同時に削除することができます。</font>\n";


  print "</form>\n";


  exit;


}


#==============================================================================


sub delete {


  $FORM{'delcode'} =~ s/　/ /g;


  @CODE = split(/ /,$FORM{'delcode'});


  $keycount = @CODE;


  #削除する番号が指定されていなければ戻る


  if ($keycount < 1) { &html; }


  foreach $line (@DATA) {


    ($date,$code,$re,$name,$email,$HP,$subject,$comment) = split(/\,/,$line);


    $match = 0;


    foreach $delcode (@CODE) {


      if ($code == $delcode) { $match = 1; }


    }


    if ($match == 0) { push (@DUMMY,$line); }


  }


  #データベースファイルを上書きする


  if (!open(DB,">$datafile")) { &error(bad_file); }


  print DB @DUMMY;


  close(DB);


  print "Location: $reload?\n\n";


}


#==============================================================================


#エラー処理


#==============================================================================


sub error {


  $error = $_[0];


  if ($error eq 'bad_file') { $msg = 'ファイルのオープン、入出力に失敗しました。'; }


  elsif ($error eq 'bad_name') { $msg = 'お名前が記入されていません'; }


  elsif ($error eq 'bad_subject') { $msg = '題名が記入されていません'; }


  elsif ($error eq 'bad_comment') { $msg = 'コメントが記入されていません'; }


  elsif ($error eq 'bad_email') { $msg = 'メールアドレスが不正です'; }


  elsif ($error eq 'bad_code') {  $msg = '削除コードが見つからないか、<br>パスワードが一致しません'; }


  elsif ($error eq 'bad_pass') {  $msg = 'パスワードが一致しません'; }


  else { $msg = '原因不明のエラーで処理を継続できません'; }


  &html_head;


  print "<P><center><font size=5><b>ＥＲＲＯＲ</b></font><BR><p>\n";


  print "<table border=4 width =70%><tr>\n";


    print "<td align=center><center><BR><P>\n";


    print "<font size=3 color=#ff0000><b><i>$msg</i></b></font><BR><P></td>\n";


  print "</tr></table>\n";


  print "</center>\n"


  &html_foot;


  print "</body></html>\n";


  exit;


}
