<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<?php
//設定用ファイル読み込み
require 'set/setting.php';
require 'set/db_connect.php';

$act_mode =  $_POST["act_mode"];
switch($act_mode){
    case "create_thread":
        frm_chk();
    break;
    case "create_res":
        frm_chk();
    break;
    case "edit_thread":
        frm_chk();
    break;
    case "del_thread":
        frm_chk();
    break;
}

//入力内容のチェック、エラーメッセージセット
function frm_chk(){
    global $act_mode;

    switch($act_mode){
        case "edit_thread":
            $edit_thread_id = $_POST["thread_id"];
            $edit_thread_pass = $_POST["edit_thread_pass"];
            require 'set/db_connect.php';
            $str_sql = "SELECT * FROM `".DB_TBL_BBSLOGS."` WHERE `".BBSLOG_ID."`='{$edit_thread_id}'";
            $edit_thread_data_rs = mysql_query($str_sql,$db);
            $edit_thread_data = mysql_fetch_assoc($edit_thread_data_rs);
            $edit_thread_pass_data = $edit_thread_data[BBSLOG_EDIT_PASS];
            $edit_thread_data_thread_id = $edit_thread_data[BBSLOG_THREAD_ID];
            if(empty($_POST["edit_thread_pass"])){
                $err_msg .= BBS_EDIT_NO_PASS_WORD_ERR;
            }else{
                if($edit_thread_pass_data != $edit_thread_pass){
                    $err_msg .= BBS_EDIT_PASS_WORD_NO_EDIT_ERR;
                }
            }
        break;
        case "del_thread":
            $del_thread_id = $_POST["thread_id"];
            $del_thread_pass = $_POST["del_thread_pass"];
            require 'set/db_connect.php';
            $str_sql = "SELECT * FROM `".DB_TBL_BBSLOGS."` WHERE `".BBSLOG_ID."`='{$del_thread_id}'";
            $del_thread_data_rs = mysql_query($str_sql,$db);
            $del_thread_data = mysql_fetch_assoc($del_thread_data_rs);
            $del_thread_pass_data = $del_thread_data[BBSLOG_EDIT_PASS];
            $del_thread_data_thread_id = $del_thread_data[BBSLOG_THREAD_ID];
            if(empty($_POST["del_thread_pass"])){
                $err_msg .= BBS_DEL_NO_PASS_WORD_ERR;
            }else{
                if($del_thread_pass_data != $del_thread_pass){
                    $err_msg .= BBS_DEL_PASS_WORD_NO_EDIT_ERR;
                }
            }
        break;        
    }

    //DELモードはPASSチェックのみ
    if($act_mode != "del_thread"){ 

        if(empty($_POST["title"])){
            $err_msg .= BBS_TITLE_ERR;
        }

        if(empty($_POST["name"])){
            $err_msg .= BBS_NAME_ERR;
        }
        
        if(!empty($_POST["mail"])){
            $mail_chk   = $_POST["mail"];
            //メールアドレスの妥当性チェック
            if(!preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/",$mail_chk)){
                $err_msg .= BBS_MAIL_ERR;
            }
        }

        //JPG以外の形式の場合
        if(is_uploaded_file($_FILES['images']['tmp_name'])){
            $upitem_chk = $_FILES['images']['name'];
            $upitem_chk = explode('.',$upitem_chk);
            if($upitem_chk[1] != jpg && $upitem_chk[1] != JPG && $upitem_chk[1] != jpeg && $upitem_chk[1] != JPEG){
            $err_msg .= BBS_IMAGES_EXTE_ERR;
        }
            $upitem_size = $_FILES['images']['size'];
            if($upitem_size > BBS_IMAGES_SIZE_LIMIT){
                $err_msg .= BBS_IMAGES_SIZE_ERR;
            } 
        }

        if(empty($_POST["comment"])){
            $err_msg .= BBS_COMMENT_ERR;
        } 

        if(empty($_POST["edit_pass"])){
            $err_msg .= BBS_EDIT_PASS_ERR;
        }else{
            $pass_chk  = $_POST["edit_pass"];
            $pass_chk_i = mb_strlen($pass_chk);
            if($pass_chk_i < BBS_PASS_WORD){
                $err_msg .= BBS_EDIT_PASS_WORD_ERR;
            }
        }

        $thread_log_id = $_POST["thread_id"];
    }
    if($err_msg == NULL){
        switch($act_mode){
            case "create_thread":
                create_new_thread();
            break;
            case "create_res":
                create_new_thread();
            break;
            case "edit_thread":
                edit_thread();
            break;
            case "del_thread":
                del_thread();
            break;
        }
    }else{
        switch($act_mode){
            case "create_thread":
                $GLOBALS['page_msg']     = "<p class='err_p'>エラー：<br />".$err_msg."</p>";
                $GLOBALS['move_page']    = "bbs.php?mode=new_thread";
                $GLOBALS['page_seconds'] = 5;
                $GLOBALS['page_title'] = "書き込みエラー";
            break;
            case "create_res":
                $GLOBALS['page_msg']     = "<p class='err_p'>エラー：<br />".$err_msg."</p>";
                $GLOBALS['move_page']    = "bbs.php?mode=show_thread&thread_id={$thread_log_id}";
                $GLOBALS['page_seconds'] = 5;
                $GLOBALS['page_title'] = "書き込みエラー";
            break;
            case "edit_thread":
                $GLOBALS['page_msg']     = "<p class='err_p'>エラー：<br />".$err_msg."</p>";
                $GLOBALS['move_page']    = "bbs.php?mode=edit_thread&thread_id={$edit_thread_data_thread_id}&bbs_id={$edit_thread_id}";
                $GLOBALS['page_seconds'] = 5;
                $GLOBALS['page_title'] = "修正エラー";
            break;
            case "del_thread":
                $GLOBALS['page_msg']     = "<p class='err_p'>エラー：<br />".$err_msg."</p>";
                $GLOBALS['move_page']    = "bbs.php?mode=edit_thread&thread_id={$del_thread_data_thread_id}&bbs_id={$del_thread_id}";
                $GLOBALS['page_seconds'] = 5;
                $GLOBALS['page_title'] = "削除エラー";
            break;
        }
    }
    //変数初期化
    $err_msg = NULL;
}

//スレッド作成、レス
function create_new_thread(){
    global $act_mode;
    //id
    require 'set/db_connect.php';
    $str_sql = "SELECT COUNT(*) FROM `".DB_TBL_BBSLOGS."` WHERE `".BBSLOG_ID."`";
    $id_cnt_rs = mysql_query($str_sql,$db);
    $id_cnt_data = mysql_fetch_assoc($id_cnt_rs);
    $id_cnt = $id_cnt_data["COUNT(*)"];
    //echo $id_cnt."あああ";
    $id = $id_cnt + 1;

    switch($act_mode){
        case "create_thread":
            //スレッドid
            require 'set/db_connect.php';
            $str_sql = "SELECT COUNT(*) FROM `".DB_TBL_BBSLOGS."` WHERE `".BBSLOG_DIVISION."`='1'";
            $cnt_rs = mysql_query($str_sql,$db);
            $thread_cnt_data = mysql_fetch_assoc($cnt_rs);
            $thread_cnt = $thread_cnt_data["COUNT(*)"];
            $thread_id = $thread_cnt + 1;
            //区分：親
            $division = 1;
            //メッセージ
            $page_msg = "<p>".BBS_NEW_THREAD_CREATE_OK."</p>";
            //<title>
            $GLOBALS['page_title'] = "新規スレッド作成";
        break;
        case "create_res":
            //レスid
            $thread_res_id = $_POST['thread_id'];
            $thread_id = $thread_res_id;
            $division = 2;
            $page_msg = "<p>".BBS_NEW_RES_CREATE_OK."</p>";
            $GLOBALS['page_title'] = "レス作成";
        break;
    }
    //スレタイ
    $thread_title        = $_POST["title"];
    $thread_title        = htmlspecialchars($thread_title);
    $thread_title        = trim($thread_title);

    //投稿者名
    $thread_author        = $_POST["name"];
    $thread_author        = htmlspecialchars($thread_author);
    $thread_author        = trim($thread_author);

    //メールアドレス
    $author_mail        = $_POST["mail"];

    //Webサイト
    $author_url        = $_POST["url"];
    $author_url        = htmlspecialchars($author_url);
    $author_url        = trim($author_url);

    if(is_uploaded_file($_FILES['images']['tmp_name'])){
        $upitem      = $_FILES['images']['tmp_name'];
        $upitem_name = $_FILES['images']['name'];
        //リネーム処理
        $upitem_name = explode('.',$upitem_name);
        $bbs_counter = sprintf("%04d",$id);
        $bbs_img_name = BBS_IMAGES_DIR."/images_".$bbs_counter.".".$upitem_name[1];
        //ファイルアップロード
        move_uploaded_file($upitem,$bbs_img_name);
    }    

    $thread_comment = $_POST["comment"];
    $thread_comment = str_replace("\t", "&nbsp;", $thread_comment);
    $thread_comment = str_replace("\n", "<br/>\n", $thread_comment);
    $thread_comment = str_replace("\r", "", $thread_comment);
    $thread_comment = preg_replace('#(http?://[a-zA-Z0-9\;\:\&\_\.\?\/\#\%\&\=\-\+\~\@]+)#',
        '<a href="$1">$1</a>', $thread_comment);

    $comment_font_colors = $_POST["font_colors"];

    //編集パスワード
    $thread_edit_pass = $_POST["edit_pass"];

    //投稿者IP取得
    $ip = getenv("REMOTE_ADDR");
    $host = getenv("REMOTE_HOST");
    if ($host == null || $host == $ip)
    $host = gethostbyaddr($ip);
    $ip_ad = $host;

    $date = date("Y/m/d-H:i:s");
    $update = $date;

    //echo "{$id_cnt}＜{$id}＜{$thread_id}＜{$division}＜{$thread_title}＜{$thread_author}＜{$author_mail}＜{$thread_comment}＜{$thread_edit_pass}＜{$bbs_img_name}＜{$comment_font_colors}＜{$ip_ad}＜{$date}＜{$update}";
    //スレッド作成
    $tbl_name = DB_TBL_BBSLOGS;
    require 'set/db_connect.php';
    $str_sql = <<< ______
INSERT INTO $tbl_name
    VALUES('$id',
           '$thread_id',
           '$division',
           '$thread_title',
           '$thread_author',
           '$author_mail',
           '$author_url',
           '$bbs_img_name',
           '$thread_comment',
           '$thread_edit_pass', 
           '$comment_font_colors', 
           '$ip_ad', 
           '$date',
           '$update',
           '0');
______;
    mysql_query($str_sql,$db);
    
    $GLOBALS['page_msg']     = $page_msg;
    $GLOBALS['move_page']    = "bbs.php";
    $GLOBALS['page_seconds'] = 1;
}

//修正
function edit_thread(){
    global $act_mode;
    $page_msg = "書き込み内容を修正しました。";
    //id
    //echo $id_cnt."あああ";

    //スレタイ
    $thread_title        = $_POST["title"];
    $thread_title        = htmlspecialchars($thread_title);
    $thread_title        = trim($thread_title);

    //投稿者名
    $thread_author        = $_POST["name"];
    $thread_author        = htmlspecialchars($thread_author);
    $thread_author        = trim($thread_author);

    //メールアドレス
    $author_mail        = $_POST["mail"];

    //Webサイト
    $author_url        = $_POST["url"];
    $author_url        = htmlspecialchars($author_url);
    $author_url        = trim($author_url);

    if(is_uploaded_file($_FILES['images']['tmp_name'])){
        $upitem      = $_FILES['images']['tmp_name'];
        $upitem_name = $_FILES['images']['name'];
        //リネーム処理
        $upitem_name = explode('.',$upitem_name);
        $bbs_counter = sprintf("%04d",$id);
        $bbs_img_name = BBS_IMAGES_DIR."/images_".$bbs_counter.".".$upitem_name[1];
        //ファイルアップロード
        move_uploaded_file($upitem,$bbs_img_name);
    }else{
        $bbs_img_name = $_POST["edit_images"];
    }

    $thread_comment = $_POST["comment"];
    $thread_comment = str_replace("\t", "&nbsp;", $thread_comment);
    $thread_comment = str_replace("\n", "<br/>\n", $thread_comment);
    $thread_comment = str_replace("\r", "", $thread_comment);
    $thread_comment = preg_replace('#(http?://[a-zA-Z0-9\;\:\&\_\.\?\/\#\%\&\=\-\+\~\@]+)#',
        '<a href="$1">$1</a>', $thread_comment);

    $comment_font_colors = $_POST["font_colors"];

    //編集パスワード
    $thread_edit_pass = $_POST["edit_pass"];

    //投稿者IP取得
    $ip = getenv("REMOTE_ADDR");
    $host = getenv("REMOTE_HOST");
    if ($host == null || $host == $ip)
    $host = gethostbyaddr($ip);
    $ip_ad = $host;

    $update = date("Y/m/d-H:i:s");

    //スレッド修正
    $tbl_name       = DB_TBL_BBSLOGS;
    $edit_thread_id = $_POST["thread_id"];
    $flg01          = BBSLOG_ID;
    require 'set/db_connect.php';
    $str_sql = <<< ______
UPDATE `$tbl_name` 
    SET `title`     ='$thread_title', 
        `name`      ='$thread_author', 
        `mail`      ='$author_mail', 
        `url`       ='$author_url', 
        `images`    ='$bbs_img_name', 
        `comment`   ='$thread_comment', 
        `edit_pass` ='$thread_edit_pass', 
        `font`      ='$comment_font_colors', 
        `ip_ad`     ='$ip_ad', 
        `update`    ='$update'  
  WHERE `$flg01`    ='$edit_thread_id';
______;
    mysql_query($str_sql,$db);
    
    $GLOBALS['page_msg']     = $page_msg;
    $GLOBALS['move_page']    = "bbs.php";
    $GLOBALS['page_seconds'] = 1;
}

//削除
function del_thread(){
    global $act_mode;
    $page_msg = "書き込み内容を削除しました。";

    //削除者IP取得
    $ip = getenv("REMOTE_ADDR");
    $host = getenv("REMOTE_HOST");
    if ($host == null || $host == $ip)
    $host = gethostbyaddr($ip);
    $ip_ad = $host;

    $update = date("Y/m/d-H:i:s");

    //スレッド削除フラグ
    $tbl_name       = DB_TBL_BBSLOGS;
    $del_thread_id = $_POST["thread_id"];
    $flg01          = BBSLOG_ID;
    require 'set/db_connect.php';
    $str_sql = <<< ______
UPDATE `$tbl_name` 
    SET `ip_ad`    ='$ip_ad', 
        `update`    ='$update', 
        `del_flg`   ='1'  
  WHERE `$flg01`    ='$del_thread_id';
______;
    mysql_query($str_sql,$db);
    
    $GLOBALS['page_msg']     = $page_msg;
    $GLOBALS['move_page']    = "bbs.php";
    $GLOBALS['page_seconds'] = 1;
}



echo "<meta http-equiv='refresh' content='{$page_seconds}; url={$move_page}'>\n";

echo "<title>掲示板-{$page_title}</title>\n";
?>
</head>
<link href="css/bbs.css" rel="stylesheet" type="text/css" />
<body>
<div class='action_msg_block'>
<?php
echo $page_msg;
echo "自動でページが戻らない場合→<a href='bbs.php'>こちら</a>";
?>
</div>
</body>
</html>