<?php
###############################################################################
##             Formulaire - Information submitting module for XOOPS          ##
##                    Copyright (c) 2003 NS Tai (aka tuff)                   ##
##                       <http://www.brandycoke.com/>                        ##
###############################################################################
##                    XOOPS - PHP Content Management System                  ##
##                       Copyright (c) 2000 XOOPS.org                        ##
##                          <http://www.xoops.org/>                          ##
###############################################################################
##  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 of the License, or        ##
##  (at your option) any later version.                                      ##
##                                                                           ##
##  You may not change or alter any portion of this comment or credits       ##
##  of supporting developers from this source code or any supporting         ##
##  source code which is considered copyrighted (c) material of the          ##
##  original comment or credit authors.                                      ##
##                                                                           ##
##  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 ##
###############################################################################
##  Author of this file: NS Tai (aka tuff)                                   ##
##  URL: http://www.brandycoke.com/                                          ##
##  Project: Formulaire                                                      ##
###############################################################################
include_once '../../mainfile.php';
include_once( XOOPS_ROOT_PATH . '/modules/formmail/config.php' ) ;
include("include/common.php");

/*
$querystring = getenv("QUERY_STRING");
if (isset($querystring)) {
	$dc = explode("&",urldecode($querystring));
	foreach($dc as $k =>$v) {
		$dcw = explode("=",$v);
		if (isset($dcw[1])) {
			$spdt = preg_split("\[",$dcw[0]);
			if (isset($spdt[1])) {
				$spdtidx = split("\]",$spdt[1]);
				$idx = (int)$spdtidx[0];
				$_POST[$spdt[0]][$idx] = $dcw[1];
			} else {
				$_POST[$dcw[0]] = $dcw[1];
			}
		}
	}
}
*/

// 言語ファイルをチェックする
$language = $xoopsConfig['language'] ;
if( ! file_exists( XOOPS_ROOT_PATH . '/modules/formmail/language/'.$language.'/admin.php') )
	$language = 'english' ;
include_once( XOOPS_ROOT_PATH . '/modules/formmail/language/'.$language.'/admin.php' ) ;

// クッキーのセット
setcookie($xoopsConfig['session_name'], session_id(), time()+(60*$xoopsConfig['session_expire']), '/',  '', 0);



// view index
$myts = icms_core_Textsanitizer::getInstance();
if (!isset($_GET['id_form'])) { // id_formが設定されてないとき
	// フォームの数をカウント
	$result = $xoopsDB->query("SELECT id_form FROM ".$xoopsDB->prefix("formmail_id")." WHERE form_req='on'");
	$num_form = icms::$xoopsDB->getRowsNum($result);

	// フォームがひとつだけのときは、そのままフォームを表示
	// 2つ以上あるときは存在するリストを表示

	if ($num_form == 1) {
		list($id_form) = icms::$xoopsDB->fetchRow($result);
		$gotourl = "Location: " . XOOPS_URL."/modules/formmail/index.php?id_form=".$id_form;
		header($gotourl);
		exit();
	} else {
		$xoopsOption['template_main'] = 'formmail_index.html' ;
		include_once ICMS_ROOT_PATH . "/header.php";

		global $xoopsDB;

		$i = 1;
		$sql = "select id_form, desc_form, text_index FROM " . $xoopsDB->prefix("formmail_id") . " WHERE form_req='on' ORDER BY form_order";
		$resultx = icms::$xoopsDB->query($sql);
		while ( $row = icms::$xoopsDB->fetchArray($resultx)) {
			$form_list = array();
			$form_list['id_form'] = $myts->makeTboxData4Show($row['id_form']);
			$form_list['title'] = 	$myts->makeTboxData4Show($row['desc_form']);
			$form_list['text'] = 		$myts->makeTareaData4Show($row['text_index']);
			$xoopsTpl->append("form_list", $form_list);
			$i++;
		}
		$xoopsTpl->assign("form_title", _MD_FORM_SITENAME);
		$xoopsTpl->assign("form_subject", _MD_FORM_SUBJECT);
		$xoopsTpl->assign("formmail_credits", credits());
		include_once XOOPS_ROOT_PATH.'/footer.php';
	}

// mail form
} else { // id_formが設定されているとき

	$id_form = intval($_GET['id_form']);

	// check id_form
	$sql = "SELECT desc_form, admin, groupe, email, expe, text_index, text_form, form_req FROM ".$xoopsDB->prefix("formmail_id")." WHERE id_form=".$id_form."";
	$result = $xoopsDB->query ($sql) or die('SQL Error !<br>'.$sql.'<br>'.$xoopsDB->error());
	if ($result) {
		while ( $row = $xoopsDB->fetchArray ($result)) {
			$title = $myts->makeTboxData4Show($row['desc_form']);
			$admin = $myts->makeTboxData4Show($row['admin']);
			$groupe = $myts->makeTboxData4Show($row['groupe']);
			$email = $myts->makeTboxData4Show($row['email']);
			$expe = $myts->makeTboxData4Show($row['expe']);
			$text_index = $myts->makeTareaData4Show($row['text_index']);
			$text_form = $row['text_form'];
			$form_req = $myts->makeTboxData4Show($row['form_req']);
		}
	}

	// 表題がないときは、管理者以外は表示させない
	if (@is_object(icms::$user)) {
		$isAdmin = icms::$user->isAdmin($xoopsModule->mid());
		if ( !isset($title) || ( $form_req !== 'on' && ! $isAdmin ) ) {
			redirect_header(XOOPS_URL."/", 1,  _MD_FORM_MSG_ERROR );
			exit();
		}
	} else {
		if ( !isset($title) || ( $form_req !== 'on' ) ) {
			redirect_header(XOOPS_URL."/", 1, _MD_FORM_MSG_ERROR );
			exit();
		}
	}

	// 送信した状態かどうかをチェック
	if( empty($_POST['submit'])){
		$xoopsOption['template_main'] = 'formmail_form.html';

		include_once ICMS_ROOT_PATH . "/header.php";
		$xoopsTpl->assign("text_index", $text_index);
		$xoopsTpl->assign("text_form", $text_form);
		$xoopsTpl->assign("formmail_credits", credits());

		$criteria = new icms_db_criteria_Item('ele_display', 1);
		$criteria->setSort('ele_order');
		$criteria->setOrder('ASC');
		$elements =& $formmail_mgr->getObjects2($criteria, $id_form);

		$form = new icms_form_Theme($title, 'formmail', XOOPS_URL.'/modules/formmail/index.php?id_form='.$id_form.'&');

		// fix-form : name & email
		if (@is_object(icms::$user)) {
			$h_name = $xoopsUser->getVar("name", "E");
			if ( $xoopsModuleConfig['username_sel'] == "name" && !empty($h_name) ) {
				$name_v = $h_name;
			} else {
				$name_v = $xoopsUser->getVar("uname", "E");
			}
		} else {
			$name_v = $_POST['usersName'];
		}
		$email_v = !empty($xoopsUser) ? $xoopsUser->getVar("email", "E") : $_POST['usersEmail'];
		$name_text = new icms_form_elements_Text(_MD_FORM_LANG_NAME, "usersName", $xoopsModuleConfig['t_width'], $xoopsModuleConfig['t_max'], $name_v);
		$email_text = new icms_form_elements_Text(_MD_FORM_LANG_EMAIL, "usersEmail", $xoopsModuleConfig['t_width'], $xoopsModuleConfig['t_max'], $email_v);
		$form->addElement($name_text, true);
		$form->addElement($email_text, true);

		$count = 0;
		foreach( $elements as $i ){
			$renderer = new FormmailElementRenderer($i);
			$formaddvalue = 'ele_'.$i->getVar('ele_id') ;
			$formvalue = $_POST[$formaddvalue];
			$form_ele =& $renderer->constructElement($formaddvalue);
			if (isset($_POST[$formaddvalue])) {
				$form_ele->setValue($formvalue);
			}
			$req = intval($i->getVar('ele_req'));
			$form->addElement($form_ele, $req);
			$count++;
			unset($form_ele);
			unset($hidden);
		}

// rehacked with captcha module
		if ($formmail_spam == 1)
		{
				$xoopsTpl->assign("formmail_spam", 1);
		}
		$form->assign($xoopsTpl);

		include_once XOOPS_ROOT_PATH.'/footer.php';

	// send mail
	}else{


	// エラー判定
		if( !isset( $_POST['g-recaptcha-response'] ) )
		{
			$_POST['g-recaptcha-response'] = '' ;
		}

		// シークレットキー
			$secret_key = '6LcC-R4UAAAAAAEOS6TPMAaO-Lfci7--PsUStDOs' ;

			// エンドポイント
			$endpoint = 'https://www.google.com/recaptcha/api/siteverify?secret=' . $secret_key . '&response=' . $_POST['g-recaptcha-response'] ;

			// 判定結果の取得
			$curl = curl_init() ;
			curl_setopt( $curl , CURLOPT_URL , $endpoint ) ;
			curl_setopt( $curl , CURLOPT_SSL_VERIFYPEER , false ) ;		// 証明書の検証を行わない
			curl_setopt( $curl , CURLOPT_RETURNTRANSFER , true ) ;		// curl_execの結果を文字列で返す
			curl_setopt( $curl , CURLOPT_TIMEOUT , 5 ) ;		// タイムアウトの秒数
			$json = curl_exec( $curl ) ;
			curl_close( $curl ) ;

			$array = json_decode( $json, true);
			$myans = intval($array['success']);
			if(!$myans){
				exit;
			}


//		$myts = icms_core_Textsanitizer::getInstance();
		$msg = '';
		unset($_POST['submit']);
		foreach( $_POST as $k => $v ){
			if( preg_match('/ele_/', $k) ){
				$n = explode("_", $k);
				$ele[$n[1]] = $v;
				$id[$n[1]] = $n[1];
			}
		}
		foreach( $id as $i ){
			$element =& $formmail_mgr->get($i);
			if( !empty($ele[$i]) ){
				$ele_type = $element->getVar('ele_type');
				$ele_value = $element->getVar('ele_value');
				$ele_caption = $element->getVar('ele_caption');
				$ele_caption = stripslashes($ele_caption);
				$ele_caption = preg_replace ('/&#039/', '`', $ele_caption);
				$ele_caption = preg_replace ('/&quot;/', '`', $ele_caption);
				$msg.= "\n".$ele_caption."\n";
				switch($ele_type){
					case 'text':
						$msg.= $myts->stripSlashesGPC($ele[$i])."\n";
						break;
					case 'textarea':
						$msg.= $myts->stripSlashesGPC($ele[$i])."\n";
						break;
					case 'radio':
						$opt_count = 1;
	          foreach($ele_value as $key => $v) {
							if( $opt_count == $ele[$i] ){
								$msg.= $myts->stripSlashesGPC($key)."\n";
							}
							$opt_count++;
						}
						break;
					case 'yn':
						$v = ($ele[$i]==2) ? _NO : _YES;
						$msg.= $myts->stripSlashesGPC($v)."\n";
						break;
					case 'checkbox':
						$opt_count = 1;
						foreach($ele_value as $key => $v) {
							if( is_array($ele[$i]) ){
								if( in_array($opt_count, $ele[$i]) ){
									$msg.= $myts->stripSlashesGPC($key)."\n";
								}
								$opt_count++;
							}else{
								if( !empty($ele[$i]) ){
									$msg.= $myts->stripSlashesGPC($v['key'])."\n";
								}
							}
						}
						break;
					case 'select':
						$opt_count = 1;
						if( is_array($ele[$i]) ){
							foreach($ele_value[2] as $key => $v) {
								if( in_array($opt_count, $ele[$i]) ){
									$msg.= $myts->stripSlashesGPC($key)."\n";
								}
								$opt_count++;
							}
						}else{
							foreach($ele_value[2] as $key => $j) {
								if( $opt_count == $ele[$i] ){
									$msg.= $myts->stripSlashesGPC($key)."\n";
								}
								$opt_count++;
							}
						}
						break;
					default:
						break;
				}
			}
		}
	// 	echo nl2br($msg);


		if( is_dir(FORMMAIL_ROOT_PATH."/language/".$xoopsConfig['language']."/mail_template") ){
			$template_dir = FORMMAIL_ROOT_PATH."language/".$xoopsConfig['language']."/mail_template";
		}else{
			$template_dir = FORMMAIL_ROOT_PATH."language/english/mail_template";
		}


		$xoopsMailer = new icms_messaging_Handler();
		$xoopsMailer->setTemplateDir($template_dir);
		$xoopsMailer->setTemplate('formmail.tpl');

		// mail title
		$sendmail_title = "";
		if ( $xoopsModuleConfig['mail_title'] == '1' ) $sendmail_title .= '['.$xoopsConfig['sitename'].'] ';
		if ( !empty($xoopsModuleConfig['mail_title2']) ) $sendmail_title .= $xoopsModuleConfig['mail_title2'].' ';
		$sendmail_title .= $title;

		$xoopsMailer->setSubject( $sendmail_title );
		$usersEmail = $myts->stripSlashesGPC($_POST['usersEmail']);
		$usersName = $myts->stripSlashesGPC($_POST['usersName']);

		if( is_object($xoopsUser) ){
			$xoopsMailer->assign("UNAME", "\"".$usersName."\" <".$usersEmail."> : ".$xoopsUser->getVar("uname"));
			$xoopsMailer->assign("UID", $xoopsUser->getVar("uid"));
		}else{
			$xoopsMailer->assign("UNAME", "\"".$usersName."\" <".$usersEmail."> : ".$xoopsConfig['anonymous']);
			$xoopsMailer->assign("UID", '-');
		}
		$xoopsMailer->assign("IP", xoops_getenv('REMOTE_ADDR'));
		$xoopsMailer->assign("AGENT", xoops_getenv('HTTP_USER_AGENT'));
		$xoopsMailer->assign("MSG", $msg);
		$xoopsMailer->assign("TITLE", $title);


		// send PM && xoopsuser
		if( $xoopsModuleConfig['method'] == 'pm' && is_object($xoopsUser) ){
			$xoopsMailer->usePM();
		  	$sqlstr = "SELECT $xoopsDB->prefix" . "_users.uname AS UserName, $xoopsDB->prefix" . "_users.email AS UserEmail, $xoopsDB->prefix" . "_users.uid AS UserID FROM
				 ".$xoopsDB->prefix("groups").", ".$xoopsDB->prefix("groups_users_link").", ".$xoopsDB->prefix("users")." WHERE $xoopsDB->prefix" . "_users.uid = $xoopsDB->prefix" . "_groups_users_link.uid
				 AND $xoopsDB->prefix" . "_groups_users_link.groupid = $xoopsDB->prefix" . "_groups.groupid AND $xoopsDB->prefix" . "_groups.groupid = $groupe";

			$res = $xoopsDB->query($sqlstr);
	        while (list($UserName,$UserEmail,$UserID) = $xoopsDB->fetchRow($res)) {
				$xoopsMailer->setToEmails($UserEmail);
			//	mail($UserEmail,$subject,$msg,"From: $sender\nReply-To: $replyto\nX-Mailer: PHP/");
			}

		// send E-mail
		}else{


			$xoopsMailer->useMail();
			if( $expe == "on" ) {
				if (!empty($xoopsUser)) {
					$email_expe = $xoopsUser->getVar("email");
					$xoopsMailer->setToEmails($email_expe);
					$xoopsMailer->assign("EMAIL_EXPE", $email_expe);
				} elseif ($xoopsModuleConfig['guest_expe']) {
					$email_expe = $usersEmail;
					$xoopsMailer->setToEmails($email_expe);
					$xoopsMailer->assign("EMAIL_EXPE", $email_expe);
				} else {
					$xoopsMailer->assign("EMAIL_EXPE", " -- ");
				}
			} else {
				$xoopsMailer->assign("EMAIL_EXPE", " -- ");
			}
			$xoopsMailer->addReplyTo($usersEmail, $usersName);

			if( $admin == "on" ){
				$xoopsMailer->setToEmails($xoopsConfig['adminmail']);
				$xoopsMailer->assign("ADMINEMAIL", $xoopsConfig['adminmail']);
				$xoopsMailer->assign("EMAIL", " -- ");
				$xoopsMailer->assign("GROUPE", " -- ");
			}else{
				$xoopsMailer->assign("ADMINEMAIL", " -- ");
				$xoopsMailer->setToEmails($email);
			  	$xoopsMailer->assign("EMAIL", $email);

				if (!empty($groupe) && ($groupe != "0")){
					$sql=sprintf("SELECT name FROM ".$xoopsDB->prefix("groups")." WHERE groupid='%s'",$groupe);
					$res = $xoopsDB->query ( $sql ) or die('Erreur SQL !<br>'.$sql.'<br>'.$xoopsDB->error());
					if ( $res ) {
						while ( $row = $xoopsDB->fetchRow ( $res ) ) {
							$gr = $row[0];
	  					}
					}
					$xoopsMailer->assign("GROUPE", $gr);
				}else {
					$xoopsMailer->assign("GROUPE", " -- ");
				}

			  	$sqlstr = "SELECT $xoopsDB->prefix" . "_users.uname AS UserName, $xoopsDB->prefix" . "_users.email AS UserEmail, $xoopsDB->prefix" . "_users.uid AS UserID FROM
		            ".$xoopsDB->prefix("groups").", ".$xoopsDB->prefix("groups_users_link").", ".$xoopsDB->prefix("users")." WHERE $xoopsDB->prefix" . "_users.uid = $xoopsDB->prefix" . "_groups_users_link.uid
		            AND $xoopsDB->prefix" . "_groups_users_link.groupid = $xoopsDB->prefix" . "_groups.groupid AND $xoopsDB->prefix" . "_groups.groupid = $groupe";

				$res = $xoopsDB->query($sqlstr);
				while (list($UserName,$UserEmail,$UserID) = $xoopsDB->fetchRow($res)) {
					$xoopsMailer->setToEmails($UserEmail);
				//	mail($UserEmail,$subject,$msg,"From: $sender\nReply-To: $replyto\nX-Mailer: PHP/");
				}
			}
		}

		$rejectip[] = "46.0.0.0/8";
		$rejectip[] = "164.132.201.0/24";
		$remote_ip = $_SERVER['REMOTE_ADDR'];

		$rcount = count($rejectip);
		$sflag = 0;
		for($i=0;$i<$rcount;$i++){
			list($accept_ip, $mask) = explode('/', $rejectip[$i]);
			$accept_long = ip2long($accept_ip) >> (32 - $mask);
			$remote_long = ip2long($remote_ip) >> (32 - $mask);
			if ($accept_long == $remote_long) {
				$sflag = 1;
			}
		}

		if ($sflag) {
			// このアドレスになっている
		} else {
			echo "send send";

			$xoopsMailer->send();
		}

		$sent = sprintf(_MD_FORM_MSG_SENT, $xoopsConfig['sitename'])._MD_FORM_MSG_THANK;
		redirect_header(XOOPS_URL."/", 2, $sent);

	// 	if( !$xoopsMailer->send(true) ){
	// 		echo $xoopsMailer->getErrors();
	// 	}else{
	// 		echo $xoopsMailer->getSuccess();
	// 	}
	}
}

function credits ()
{
$credits = "<div style='text-align: right; font-size: x-small; font-style: italic;'>Formmail 1.3 hacked by <a href='http://www.chushokigyo.net/' target='_blank'>Chushokigyo.net</a></div><div style='text-align: right; font-size: 6pt; font-style: italic;'>
	Powered by FormMail 1.0beta by Tom <a href='http://malaika.s31.xrea.com/' target='_blank'>Malaika System</a>Based on Formulaire 1.0 &copy; 2003 <a href='http://www.xoops-themes.com/' target='_blank'>xoops-themes&middot;com</a> / Liaise 1.0b5 by NS Tai (aka tuff) <a href='http://www.brandycoke.com/'>BRANDYCOKE&middot;COM</a>
	/ MyMenu by Marcel Widmer(http://www.coaching-forum.net) </div>";
	return $credits;
}
#==========================================================================
# 引数処理(GET文字列作成)
#-------------------------------------------------------------------------
function make_get_str($post,$flg=true) {
	$str = "";
	foreach($post as $key =>$val) {
		if (is_array($val)) {
			foreach($val as $k =>$v) {
				$str .= $key."[".$k."]=".$v."&";
			}
		} else {
			$str .= $key."=".$val."&";
		}
	}
	if ($str != "") {
		$str = substr($str, 0, strlen($str)-1);
	}
	if ($flg) {
		return htmlentities(urlencode($str),ENT_COMPAT,"EUC-JP");
	} else {
		return $str;
	}
}
?>
