* NINOMIYA Yuuki * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. * * $Id: index.php,v 1.3 2009-03-11 01:54:35 gm Exp $ * * pFormMail-G Version 1.1.1 */ /* ----------------- * ユーザ設定 * ----------------- */ // メール送信先アドレス define(MAIL_TO, 'from_web@tohoair.co.jp'); // メール送信元アドレス define(MAIL_FROM, 'from_web@tohoair.co.jp'); // メールのサブジェクト define(SUBJECT, '英語のフォームからの問い合せ'); // 送信終了時にリダイレクトする URI の指定 define(DONE_URI, 'finished.html'); // 必須項目 $REQUIRED_PARAMETERS = array('Organization', 'YourName', 'Country', 'E-mail', 'Phone', 'StreetAddress', 'Comments'); // メールアドレスとしての形式を検査する項目 $EMAIL_PARAMETERS = array('E-mail'); // 控えを送信するメールアドレスを記載したパラメータ名 (空の場合はメールを送らない) define(COPY_ADDRESS_PARAMETER, ''); // ここで記載したパラメータ名の値をメールの送信元とする (空の場合は MAIL_FROM を送信元とする) define(MAIL_FROM_PARAMETER, ''); // 画像認証のパラメータ名 (空の場合は画像認証を使用しない) define(CAPTCHA_PARAMETER, 'ImageCapture'); // 入力画面テンプレート HTML ファイルの指定 define(INPUT_TEMPLATE, 'input.html'); // 確認画面テンプレート HTML ファイルの指定 define(CONFIRM_TEMPLATE, 'confirm.html'); // エラー画面テンプレート HTML ファイルの指定 define(ERROR_TEMPLATE, 'error.html'); // メールテンプレートファイルの指定 define(MAIL_TEMPLATE, 'mail.txt'); /* ////////////////////////////////////////////////////////////////// * プログラム本体 * ////////////////////////////////////////////////////////////////// */ mb_language("japanese"); mb_internal_encoding("EUC-JP"); session_cache_limiter('private_no_expire') ; session_start(); $mode = $_GET['mode']; $params = array(); foreach ($_POST as $key => $value) { $params[$key] = $value; } $params['USER_AGENT'] = $_SERVER['HTTP_USER_AGENT']; $params['REMOTE_ADDR'] = $_SERVER['REMOTE_ADDR']; $params['REMOTE_HOST'] = $_SERVER['REMOTE_HOST']; if ($params['REMOTE_HOST'] == '') { $params['REMOTE_HOST'] = $params['REMOTE_ADDR']; } $params['DATE'] = date('Y/m/d(D) H:i:s'); if ($mode == 'confirm') { validate($params); show_confirm_page($params); } else if ($mode == 'submit') { validate($params); submit($params); header('Location: ' . DONE_URI); exit; } else { show_input_form(); } /* ////////////////////////////////////////////////////////////////// * 関数 * ////////////////////////////////////////////////////////////////// */ function show_input_form() { $fp = fopen(INPUT_TEMPLATE, 'r'); if (!$fp) { show_error('Can not open mail template.'); } while (!feof($fp)) { $line = fgets($fp, 4096); $line = str_replace('$captcha$', '', $line); echo $line; } fclose($fp); } function show_confirm_page($params) { $fp = fopen(CONFIRM_TEMPLATE, 'r'); if (!$fp) { show_error('Can not open confirmation template.'); } $hidden = generate_hidden_parameter($params); while (!feof($fp)) { $line = fgets($fp, 4096); $line = replace_parameter($line, $params); $line = str_replace('$hidden$', $hidden, $line); echo $line; } fclose($fp); } function submit($params) { $fp = fopen(MAIL_TEMPLATE, 'r'); if (!$fp) { show_error('Can not open mail template.'); } $body = ''; while (!feof($fp)) { $line = fgets($fp, 4096); $line = replace_parameter($line, $params, false); $body .= $line; } fclose($fp); $additional_headers = "From: " . get_from_address($params) . "\n"; mb_send_mail(MAIL_TO, SUBJECT, $body, $additional_headers); $copy_address = get_copy_address($params); if ($copy_address != '') { mb_send_mail($copy_address, SUBJECT, $body, $additional_headers); } if (isset($_SESSION['captcha_keystring'])) { unset($_SESSION['captcha_keystring']); } } function show_error($message) { $fp = fopen(ERROR_TEMPLATE, 'r'); if (!$fp) { die(); } while (!feof($fp)) { $line = fgets($fp, 4096); $line = str_replace('$error$', $message, $line); echo $line; } fclose($fp); die(); } function replace_parameter($str, $params, $escape = true) { foreach ($params as $key => $value) { if (!is_array($value)) { /* 渡ってきたパラメータが配列でなければ、$key$ を value に置換する */ if ($escape) { $value = nl2br(htmlspecialchars($value)); } $str = str_replace('$' . $key . '$', $value, $str); } else { /* 配列であれば、以下で展開する */ $array_result = ''; if (ereg('(.*)#' . $key . '\.foreach\((.*)\)(.*)', $str, $regs)) { /* #キー.foreach(ループ内容) を置換 */ $prefix = $regs[1]; $array_content = $regs[2]; $suffix = $regs[3]; $i = 0; foreach ($value as $array_key => $array_value) { if ($escape) { $array_value = nl2br(htmlspecialchars($array_value)); } if (ereg("([0-9]*)\.(.*)", $array_key, $regs)) { $order = $regs[1]; $actual_key = $regs[2]; ${$key}[$order]['__key'] = $actual_key; ${$key}[$order]['__value'] = $array_value; } else { ${$key}[$i]['__key'] = $array_key; ${$key}[$i]['__value'] = $array_value; } $i++; } ksort(${$key}, SORT_NUMERIC); foreach(${$key} as $v) { if (array_key_exists('__key', $v)) { $array_result .= str_replace('$' . $key . '[].value$', $v['__value'], str_replace('$' . $key . '[].key$', $v['__key'], $array_content)); } else { $array_result .= str_replace('$' . $key . '[].value$', $v['__value'], $array_content); } } $str = $prefix . str_replace('\n', "\n", $array_result) . $suffix; } } } return $str; } function generate_hidden_parameter($params) { $hidden = ''; foreach ($params as $key => $value) { if (is_array($value)) { foreach ($value as $k => $v) { $v = htmlspecialchars($v); $hidden .= "\n"; } } else { $value = htmlspecialchars($value); $hidden .= "\n"; } } return $hidden; } function get_copy_address($params) { if (COPY_ADDRESS_PARAMETER == '') { return ''; } if ($params[COPY_ADDRESS_PARAMETER] != '') { return $params[COPY_ADDRESS_PARAMETER]; } return ''; } function get_from_address($params) { if (MAIL_FROM_PARAMETER == '') { return MAIL_FROM; } if ($params[MAIL_FROM_PARAMETER] != '') { return $params[MAIL_FROM_PARAMETER]; } return ''; } function validate($params) { global $REQUIRED_PARAMETERS; global $EMAIL_PARAMETERS; $error_occured = false; $message = ''; foreach ($REQUIRED_PARAMETERS as $key) { if ($params[$key] == '') { $error_occured = true; $message .= "${key}is required.
\n"; } } foreach ($EMAIL_PARAMETERS as $key) { if ($params[$key] != '' && !preg_match("/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+$/", $params[$key])) { $error_occured = true; $message .= "${key} is not proper email address.
\n"; } } if (CAPTCHA_PARAMETER != '' && (!isset($_SESSION['captcha_keystring']) || ($_SESSION['captcha_keystring'] != $params[CAPTCHA_PARAMETER]))) { $error_occured = true; $message .= "The image and the text do not match.
\n"; } if ($error_occured) { show_error($message); } } ?>