<?php
//===================初期設定====================
$data_file = './chat.dat'; //データファイル名
$title = 'KOMOチャット'; //タイトル名
$data_max = 30; //データ最大記録数
//===============================================

//スーパーグローバル変数対策
if(!isset($PHP_SELF)){ $PHP_SELF = $_SERVER["PHP_SELF"]; }
if(!isset(
$action)){ $action = $_POST['action']; }
if(!isset(
$name)){
    if(
$_GET['name']){
        
$name = $_GET['name'];
    }else{
        
$name = $_POST['name'];
    }
}
if(!isset(
$ncolor)){
    if(
$_GET['ncolor']){
        
$ncolor = $_GET['ncolor'];
    }else{
        
$ncolor = $_POST['ncolor'];
    }
}
if(!isset(
$refresh)){
    if(
$_GET['refresh']){
        
$refresh = $_GET['refresh'];
    }else{
        
$refresh = $_POST['refresh'];
    }
}
if(!isset(
$message)){ $message = $_POST['message']; }
//エスケープ記号対策
$name = stripslashes($name);
$message = stripslashes($message);
?>

<HTML>
<HEAD>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=x-sjis">
    <TITLE>KOMOチャット</TITLE>
    <STYLE TYPE="text/css">
    <!--
    :link     {
            Color : blue ;
            Text-Decoration : None
        }
    :active     {
            Color : blue ;
            Text-Decoration : None
        }
    :visited     {
            Color : blue ;
            Text-Decoration : None
        }
    A:hover     {
            Color : blue ;
            Text-Decoration : Underline
        }
    -->
    </STYLE>
</HEAD>
<BODY BGCOLOR="#ffffee">
<FORM ACTION="<?php echo $PHP_SELF; ?>" METHOD="POST" ENCTYPE="application/x-www-form-urlencoded">
<P><INPUT TYPE="HIDDEN" NAME="action" VALUE="regist"></P>
<P><FONT SIZE="5" COLOR="#000099">KOMOチャット</FONT></P>

<BLOCKQUOTE>
    <P><B>お名前:</B><INPUT TYPE="TEXT" NAME="name" SIZE="12" VALUE="<?php echo $name; ?>">

<?php
echo "<INPUT TYPE=RADIO NAME=ncolor VALUE='blue'" . (($ncolor == 'blue') ? ' CHECKED' : '') . "><B><FONT COLOR='blue'>青</FONT></B> <INPUT TYPE=RADIO NAME=ncolor VALUE='red'" . (($ncolor == 'red') ? ' CHECKED' : '') . "><B><FONT COLOR='red'>赤</FONT></B> <INPUT TYPE=RADIO NAME=ncolor VALUE='green'" . (($ncolor == 'green') ? ' CHECKED' : '') . "><B><FONT COLOR='green'>緑</FONT></B> <INPUT TYPE=RADIO NAME=ncolor VALUE=#996600" . (($ncolor == '#996600') ? ' CHECKED' : '') . "><B><FONT COLOR=#996600>茶</FONT></B> <INPUT TYPE=RADIO NAME=ncolor VALUE=#660099" . (($ncolor == '#660099') ? ' CHECKED' : '') . "><B><FONT COLOR=#660099>紫</FONT></B> <B>リフレッシュ時間:</B> <SELECT NAME=refresh><OPTION" . (($refresh == 60) ? ' SELECTED' : '') . ">60</OPTION><OPTION" . (($refresh == 45) ? ' SELECTED' : '') . ">45</OPTION><OPTION" . (($refresh == 30) ? ' SELECTED' : '') . ">30</OPTION></SELECT>秒\n";
?>

</P>
    <P><B>メッセージ:</B><INPUT TYPE="TEXT" NAME="message" SIZE="45"> <INPUT TYPE="SUBMIT" NAME="Submit" VALUE="送信"></P>
</BLOCKQUOTE>

<P>
<HR>
</P>

<?php
if($action == "regist"){
    if(
$message){
        
//現在時刻の取得
        
$now = date("Y/m/d H:i:s");
        
//ここから書き込みデータの調整
        
$name = htmlspecialchars($name); //特殊文字のHTMLエントリへの変換
        
$ncolor = htmlspecialchars($ncolor);
        
$message = htmlspecialchars($message);
        
$message = nl2br($message); //HTML改行文字の挿入
        
$message = str_replace("\r", "", $message); //文字列の置換
        
$message = str_replace("\n", "", $message);
        
//ログファイルの区切文字(",")と区別するために文字コード(&#44)に書き換える。
        
$name = str_replace(",", "&#44;",$name);
        
$message = str_replace(",", "&#44;",$message);
        
//配列要素を文字列により連結
        
$input_msg = implode(",", array($name,$ncolor,$message,$now));
        
$datalog = file($data_file);
        
$fp = fopen($data_file, "w");
        
rewind($fp);
        
fputs($fp, "$input_msg\n");
        
//最大記録数の調整
        
if($data_max < sizeof($datalog))
            
$msg_num = $data_max - 1;
        else
            
$msg_num = sizeof($datalog);
        for(
$i = 0; $i < $msg_num; $i++){
            
fputs($fp, $datalog[$i]);
        }
        
fclose($fp);
        unset(
$datalog);
    }
}
$datalog = file($data_file);
$msg_count = count($datalog);
for(
$i = 0; $i < $msg_count; $i++){
    list(
$name2,$ncolor2,$message2,$now2) = split( ",", $datalog[$i]);
    
//記事表示部を生成
    
print "<font color=$ncolor2><b>$name2</b></font>\n";
    print
" $message2\n";
    print
" <font size=2 color='green'>$now2</font>\n";
    print
"<hr>\n";
}
if(!
$refresh){ $refresh = 60; }
print
"<META HTTP-EQUIV=refresh CONTENT=$refresh;URL=$PHP_SELF?name=$name&ncolor=$ncolor&refresh=$refresh>\n";
?>

</FORM>
<div align="right"><font size="-1">Powered by <a href="http://www.komonet.ne.jp/" onMouseOver="this.style.color='red'" onMouseOut="this.style.color='blue'">KOMONET</a></font></div>
</BODY>
</HTML>