<?php

//**********************************************************************************************
//　複数の送信先を設定・選択できるフォームメール - FormMails ver 1.1 - (2009/2/7)
//　Copyright (C) webcloset.net All rights reserved.
//　http://www.webcloset.net/
//**********************************************************************************************

include("config/config.php");


switch($_POST['step']){

case 'check':

check();

$_POST['name'] = strcode($_POST['name']);
$_POST['tel'] = strcode($_POST['tel']);
$_POST['email'] = strcode($_POST['email']);
$_POST['jyusho'] = strcode($_POST['jyusho']);
$_POST['jyusho02'] = strcode($_POST['jyusho02']);
$_POST['jyusho03'] = strcode($_POST['jyusho03']);
$_POST['jyusho04'] = strcode($_POST['jyusho04']);
$_POST['message'] = strcode($_POST['message']);
$_POST['renraku'] = strcode($_POST['renraku']);

$_POST['message'] = str_replace("\r\n", "<br>", $_POST['message']);
$_POST['message'] = str_replace("\r", "<br>", $_POST['message']);
$_POST['message'] = str_replace("\n", "<br>", $_POST['message']);

$tohtml = checkfrom();
include(CHECKPAGE);
html($check_html.index($COPYRIGHT));

break;

case 'end':

check();

$_POST['name'] = codestr($_POST['name']);
$_POST['tel'] = codestr($_POST['tel']);
$_POST['email'] = codestr($_POST['email']);
$_POST['jyusho'] = codestr($_POST['jyusho']);
$_POST['jyusho02'] = codestr($_POST['jyusho02']);
$_POST['jyusho03'] = codestr($_POST['jyusho03']);
$_POST['jyusho04'] = codestr($_POST['jyusho04']);
$_POST['message'] = codestr($_POST['message']);
$_POST['renraku'] = codestr($_POST['renraku']);

$_POST['message'] = str_replace("<br>", "\n", $_POST['message']);

if(function_exists("mb_convert_kana")){
$_POST['name'] = mb_convert_kana($_POST['name'],"K", CHARACTERCODE);
$_POST['jyusho'] = mb_convert_kana($_POST['jyusho'],"K", CHARACTERCODE);
$_POST['jyusho02'] = mb_convert_kana($_POST['jyusho02'],"K", CHARACTERCODE);
$_POST['jyusho03'] = mb_convert_kana($_POST['jyusho03'],"K", CHARACTERCODE);
$_POST['jyusho04'] = mb_convert_kana($_POST['jyusho04'],"K", CHARACTERCODE);
$_POST['message'] = mb_convert_kana($_POST['message'],"K", CHARACTERCODE);
}

tomails();

html($ENDMESSAGE);
break;

default:

$tohtml = indexfrom();

$namemax = NAMEMAX / 2;
$emailmax = EMAILMAX;
$messagemax = MESSAGEMAX / 2;

include(INDEXPAGE);

html($index_html.index($COPYRIGHT));

break;
}


function indexfrom(){
global $_POST;

$maillist = file(MAILLIST);
$mailtonum = count($maillist);

if($mailtonum >= 2){

if(SELECTTYPE == 1){
$tohtml .= "<tr><td class=\"tabletitle\">".MAILTOTATLE."</td>\n<td class=\"tableform\">\n<select name=\"topost[]\">\n<option selected value=\"\">　　お選びください　　</option>\n";
for($i=0;$i<$mailtonum;$i++){

$to = explode("<>",$maillist[$i]);
$toname = chop($to[1]);
if(!$toname)continue;
$tonum = $i+1;
$tohtml .= "<option value=\"$tonum\">$toname</option>\n";
}
$tohtml .= "</select>\n</td>\n</tr>\n";

}elseif(SELECTTYPE == 2){
$tohtml .= "<tr><td class=\"tabletitle\">".MAILTOTATLE."</td>\n<td class=\"tableform\">\n";

$checkde = " checked";
$brfrg = 1;
for($i=0;$i<$mailtonum;$i++){

$to = explode("<>",$maillist[$i]);
$toname = chop($to[1]);
if(!$toname)continue;
$tonum = $i+1;
if($brfrg == BRSET){$br = "<br>";$brfrg = 1;}else{$br = "　";$brfrg++;}
$tohtml .= "<input name=\"topost[]\" type=\"radio\" value=\"$tonum\"$checkde>$toname$br\n";
$checkde = "";
}
$tohtml .= "</td>\n</tr>\n";


}elseif(SELECTTYPE == 3){
$tohtml .= "<tr><td class=\"tabletitle\">".MAILTOTATLE."</td>\n<td class=\"tableform\">\n";

$brfrg = 1;
for($i=0;$i<$mailtonum;$i++){

$to = explode("<>",$maillist[$i]);
$toname = chop($to[1]);
if(!$toname)continue;
$tonum = $i+1;
if($brfrg == BRSET){$br = "<br>";$brfrg = 1;}else{$br = "　";$brfrg++;}
$tohtml .= "<input type=\"checkbox\" name=\"topost[]\" value=\"$tonum\">$toname$br\n";

}
$tohtml .= "</td>\n</tr>\n";


}

return $tohtml;

}elseif($mailtonum == 1){

$tohtml .= "<input type=\"hidden\" name=\"topost[]\" value=\"1\">\n";
return $tohtml;

}elseif($mailtonum == 0){

echo "送信先のメールアドレスが設定されていません。";
exit;
}
}


function checkfrom(){
global $_POST;

$maillist = file(MAILLIST);
$topost = $_POST['topost'];


if(2 <= count($maillist)){

$tohtml[0] .= "<tr><td class=\"tabletitle\">".MAILTOTATLE."</td>\n<td class=\"tableform\">\n";

for($i=0;$i<count($topost);$i++){
$tonum = $topost[$i]-1;
$to = explode("<>",$maillist[$tonum]);
$to[1] = chop($to[1]);
$tohtml[0] .= " $to[1]<br>\n";
$tohtml[1] .= "<input type=\"hidden\" name=\"topost[]\" value=\"$topost[$i]\">\n";
}
$tohtml[0] .= "</td>\n</tr>\n";

}else{

for($i=0;$i<count($topost);$i++){
$tonum = $topost[$i]-1;
$to = explode("<>",$maillist[$tonum]);
$to[1] = chop($to[1]);
$tohtml[1] .= "<input type=\"hidden\" name=\"topost[]\" value=\"$topost[$i]\">\n";
}
}

return $tohtml;
}


function error($html){

$html = "<div class=\"maintable\" style=\"text-align:center;color:#CC0000;\">".$html."<hr size=\"1\" style=\"color:#DDDDDD;\">\n<input type=button value=\"戻る\" onClick=\"history.back()\" class=\"submit\"></div>\n";

include(TEMPLATE);
exit();
}


function html($html){
include(TEMPLATE);
exit();
}


function check(){
global $_POST;

if(NAME){
if(!$_POST['name']){
error("氏名を入力いただいたかご確認ください");
}
}

if(strlen($_POST['name']) > NAMEMAX){
$namemax = NAMEMAX / 2;
error("氏名の文字数オーバーです。入力可能な文字数は ".$namemax." 文字です。");
}

if(!$_POST['email']){
error("メールアドレスを入力いただいたかご確認ください");
}

if(strlen($_POST['email']) > EMAILMAX){
$emailmax = EMAILMAX;
error("メールアドレスの文字数オーバーです。入力可能な文字数は ".$emailmax." 文字です。");
}

if(!preg_match('/[\w\d\._-]+\@[\w\d\._-]+\.[a-zA-Z]{2,3}/', $_POST['email'])){
error("メールアドレスの入力に誤りがないかご確認ください");
}

if(!$_POST['jyusho']){
error("住所（都道府県）を入力いただいたかご確認ください");
}

if(!$_POST['message']){
error("お問い合わせ内容を入力いただいたかご確認ください");
}

if(strlen($_POST['message']) > MESSAGEMAX){
$messagemax = MESSAGEMAX / 2;
error("お問い合わせ内容の文字数オーバーです。入力可能な文字数は ".$messagemax." 文字です。");
}


if(1 > $_POST['topost'][0]){
error(MAILTOTATLE."を選択いただいたかご確認ください");
}


if(!preg_match("/^[0-9]+$/", $_POST['topost'][0])){
error(MAILTOTATLE."エラー");
}
}


function strcode($str){

if(function_exists("mb_convert_encoding")){
$str = mb_convert_encoding($str, CHARACTERCODE, "auto");
}

if(get_magic_quotes_gpc()){
$str = stripslashes($str);
}

$str = htmlspecialchars($str);



$str = chop($str);

return $str;
}


function codestr($str){


if(function_exists("mb_convert_encoding")){
$str = mb_convert_encoding($str, CHARACTERCODE, "auto");
}

if(get_magic_quotes_gpc()){
$str = stripslashes($str);
}



$str = str_replace("&amp;", "&", $str);
$str = str_replace("&lt;", "<", $str);
$str = str_replace("&gt;", ">", $str);
$str = str_replace("&quot;", "\"", $str);

$str = chop($str);

return $str;
}


function tomails(){
global $_POST;


$date = gmdate("Y/m/d(D) H:i",time()+9*60*60);
$ip = $_SERVER["REMOTE_ADDR"];

include(MAILTOFILE);

$maillist = file(MAILLIST);
$topost = $_POST['topost'];


for($i=0;$i<count($topost);$i++){
$tonum = $topost[$i]-1;
$to = explode("<>",$maillist[$tonum]);
$name = chop($to[1]);
$maintoname .= "（${name}）";
$toname = "（${name}）";
mb_language("japanese");
mb_internal_encoding(CHARACTERCODE);
$from = "From: ".$_POST['email']."\r\n";
$tonamesubject = SUBJECT."　".$toname;
$rcd = @mb_send_mail($to[0], $tonamesubject, $mailcont, $from);
if(!$rcd){html("送信エラー");}
}

if(MAILTOMAIN){
$from = "From: ".MAILTOMAIN."\r\n";
$tonamesubject_main = SUBJECT."　".$maintoname;
$rcd = @mb_send_mail(MAILTOMAIN, $tonamesubject_main, $mailcont, $from);
if(!$rcd){html("送信エラー");}
}

if(RETURNMAIL){
include(MAILRETURNFILE);
if(RETURNADDRESS){
$toreturn = RETURNADDRESS;
}else{
$toreturn = $to[0];
}
$from = "From: ".mb_encode_mimeheader(SITENAME).$toreturn."\r\n";
$rcd = @mb_send_mail($_POST['email'], RETURNSUBJECT, $mailcont, $from);
if(!$rcd){html("送信エラー");}
}

}


function index($html){

if(!$html || !ereg("ebc", $html) || !ereg("ttp", $html))exit();

return $html;
}



?>