<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
require_once '../inc/common.inc.php';
require_once '../inc/mailer.class.php';

function filterNotSaftChar($filterStr, $source, $replace = '***') 
{
  $array = explode("|", $filterStr);
  foreach ( $array as $key => $value )
  {
    $source = str_replace($value, $replace, $source);
  }
  return $source;
}

if ('add' == $action)
{
  if (! empty($kuWebsiteMessageSpecialWord)) 
  {
    $username = filterNotSaftChar($kuWebsiteMessageSpecialWord, $username);
    $messagecontent = filterNotSaftChar($kuWebsiteMessageSpecialWord, $messagecontent);
  }
  $ipaddress = $_SERVER['REMOTE_ADDR'];
  $type1 = $menuid;
  $type2 = isset($secondMenuNav) ? $secondMenuNav : 0;
  $type3 = isset($thirdMenuNav) ? $thirdMenuNav : 0;
  $strSql = "insert into {$configTableHead}message (messagetitle, username, telephone, email, qq, messagecontent, ipaddress, addtime, lang, type1, type2, type3) values ('{$messagetitle}', '{$username}', '{$telephone}','{$email}', '{$qq}', '{$messagecontent }', '{$ipaddress}', now(), '{$kuWebsiteCurrLanguage}', '{$type1}', '{$type2}', '{$type3}');";
  //echo $strSql;
  if ($dbInstance->query($strSql))
  {
    if (1 == $kuWebsiteMessagePostEmail) 
    {
      $strSendName = $username;
      $strSubject = 'Customer';
      $strEmail = $email;
      $strTel = $telephone;
      $strEnquiry = $messagecontent;
      //echo "$strSendName.'%%'.$strSubject.'%%'.$strEmail.'%%'.$strTel.'%%'.$strEnquiry", '<br />'; //for test 
      
      
      $arrayReceiverEmail = explode("|", $kuWebsiteMessageAcceptEmailAddress); 
      //print_r($arrayReceiverEmail) . '<br />';
      $errorMessage = '';
      foreach ( $arrayReceiverEmail as $key => $emailValue ) 
      {
        $strReceiver = $emailValue;
        //$strReceiver = '8558864436@qq.com';
        $strSubject = "An enquiry from $strSubject";
        $strDate = date('Y-m-d', time());
        $strMessage = "Starting Date: $strDate\n" . "Send Name: $strSendName\n" . "Email: {$strEmail}\n" . "Phone: {$strTel}\n" . "Message:" . $strEnquiry . "\n";
        //echo $strMessage.'<br />';
        $strHeader = array("To: " . $strReceiver, "From: " . $strEmail, "Cc: ", "Bcc: ");
        $strHeaderString = join("\r\n", $strHeader);
        //echo "$strReceiver, $strSubject, $strMessage, $strHeaderString";    //for test
        if(1 == $kuWebsiteMailType)
        {
          KuwebsMailer::smtpMail($strEmail, $strReceiver, $strSubject, $strMessage);
        }
        else
        {
          @mail($strReceiver, $strSubject, $strMessage, $strHeaderString);
        }
      }

      if (1 == $kuWebsiteMessageAutoRevert) 
      {
        $strSendName = $kuWebsiteTitle;
        $strSubject = $kuWebsiteMessageTitle ?  $kuWebsiteMessageTitle : 'An reply from '.$kuWebsiteTitle;
        $strEmail = $kuWebsiteMessageAutoRevertEmailAddress; 
        $strTel = '0755-36943966';
        $strMessage = $kuWebsiteMessageContent ? $kuWebsiteMessageContent : 'Thanks for your message, we will contact you asap. Any question pls visit: '.$kuHttpPath;
        //echo "$strSendName$strSubject$strEmail$strTel$strEnquiry"; //for test 
        
        
        $strHeader = array("To: " . $strReceiver, "From: " . $strEmail, "Cc: ", "Bcc: ");
        $strHeaderString = join("\r\n", $strHeader);
        if(1 == $kuWebsiteMailType)
        {
          KuwebsMailer::smtpMail($strEmail, $email, $strSubject, $strMessage);
        }
        else
        {
          @mail($email, $strSubject, $strMessage, $strHeaderString);
        }
      }
    }
    echo "<script>alert('{$kuCaptionMessageSendSuccess}'); window.location.href='index.php?lang={$lang}&menuid={$menuid}';</script>";
    exit;
  }
  echo "<script>alert('{$kuCaptionMessageSendFailed}'); window.location.href='add.php?lang={$lang}&menuid={$menuid}';</script>";
}
?>