<?php
session_start();
require_once '../inc/common.inc.php';
require_once '../inc/mailer.class.php';

echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
function filterNotSaftChar($filterStr, $source, $replace = '***') 
{
  $array = explode("|", $filterStr);
  foreach ( $array as $key => $value )
  {
    $source = str_replace($value, $replace, $source);
  }
  return $source;
}

if ('add' == $action)
{
  $userid = $_SESSION['userid'];
  if (! empty($kuWebsiteFeedbackSpecialWord)) 
  {
    $feedbackname = filterNotSaftChar($kuWebsiteFeedbackSpecialWord, $feedbackname);
    $feedbacktitle = filterNotSaftChar($kuWebsiteFeedbackSpecialWord, $feedbacktitle);
    $feedbackcontent = filterNotSaftChar($kuWebsiteFeedbackSpecialWord, $feedbackcontent);
  }
  $ipaddress = $_SERVER['REMOTE_ADDR'];
  $strSql = "insert into {$configTableHead}feedback (feedbacktype, objectid, feedbacktitle, feedbackcontent, feedbackname, feedbackemail, feedbackmobile, feedbackaddress, feedbackpostcode, qq, userid, ipaddress, addtime, lang) 
            values ('{$feedbacktype}', '{$objectid}', '{$feedbacktitle}', '{$feedbackcontent}', '{$feedbackname}', '{$feedbackemail}', '{$feedbackmobile}', '{$feedbackaddress}', '{$feedbackpostcode}', '{$qq}', '{$userid}', '{$ipaddress}', now(), '{$kuWebsiteCurrLanguage}');";
  //echo $strSql;
  if ($dbInstance->query($strSql))
  {
    if (1 == $kuWebsiteFeedbackPostEmail) 
    {
      $strSendName = $feedbackname;
      $strSubject = 'Customer';
      $strEmail = $feedbackemail;
      $strTel = $feedbackmobile;
      $strEnquiry = $feedbackcontent;
      //echo "$strSendName.'%%'.$strSubject.'%%'.$strEmail.'%%'.$strTel.'%%'.$strEnquiry", '<br />'; //for test 
      
      
      $arrayReceiverEmail = explode("|", $kuWebsiteFeedbackAcceptEmailAddress); 
      //print_r($arrayReceiverEmail) . '<br />';
      $errorFeedback = '';
      foreach ( $arrayReceiverEmail as $key => $emailValue ) 
      {
        $strReceiver = $emailValue;
        //$strReceiver = '8558864436@qq.com';
        $strSubject = $feedbacktitle ? $feedbacktitle : "An enquiry from $strSubject";
        $strDate = date('Y-m-d', time());
        $strFeedback = "Starting Date: $strDate\n" . "Send Name: $strSendName\n" . "Email: {$strEmail}\n" . "Phone: {$strTel}\n" . "Feedback:" . $strEnquiry . "\n";
        //echo $strFeedback.'<br />';
        $strHeader = array("To: " . $strReceiver, "From: " . $strEmail, "Cc: ", "Bcc: ");
        $strHeaderString = join("\r\n", $strHeader);
        //echo "$strReceiver, $strSubject, $strFeedback, $strHeaderString";    //for test
        if(1 == $kuWebsiteMailType)
        {
          KuwebsMailer::smtpMail($strEmail, $strReceiver, $strSubject, $strFeedback);
        }
        else
        {
          @mail($strReceiver, $strSubject, $strFeedback, $strHeaderString);
        }
      }

      if (1 == $kuWebsiteFeedbackAutoRevert) 
      {
        $strSendName = $kuWebsiteTitle;
        $strSubject = $kuWebsiteFeedbackTitle ?  $kuWebsiteFeedbackTitle : 'An reply from '.$kuWebsiteTitle;
        $strEmail = $kuWebsiteFeedbackAutoRevertEmailAddress; 
        $strTel = '0755-36943966';
        $strFeedback = $kuWebsiteFeedbackContent ? $kuWebsiteFeedbackContent : '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, $strFeedback);
        }
        else
        {
          @mail($email, $strSubject, $strFeedback, $strHeaderString);
        }
      }
    }
    echo "<script>alert('{$kuCaptionFeedbackSendSuccess}'); window.location.href='{$backUrl}';</script>";
    exit;
  }
  echo "<script>alert('{$kuCaptionFeedbackSendFailed}'); window.location.href='{$backUrl}';</script>";
  exit;
}
else
{
  $backUrl = $_SERVER["HTTP_REFERER"];
  if(empty($backUrl))$backUrl = $kuHttpPath;
  if(0 >= intval($feedbacktype) || 0 >= intval($objectid))
  {
    echo "<script>window.location.href='{$backUrl}';</script>";
    exit;
  }
  
  $fType = $arrAllModule[$feedbacktype];
  $strSql = "select * from {$configTableHead}{$fType} where id={$objectid}";
  $objectRow = $dbInstance->getOne($strSql);
  $objectTitle = $objectRow[$fType.'title'];
  $objectUrl = "{$kuHttpPathUrl}{$fType}/{$fType}.php?lang={$kuWebsiteCurrLanguage}&itemid={$objectid}";

  
  $kuStrBlockContact = $kuBlock[2];


  include_once KUWEBS_ABSOLUTE_PATH."inc/headermenu.php"; 
  include_once KUWEBS_ABSOLUTE_PATH."inc/footer.php";
  include_once template('feedback');
}
?>