<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache" );
header("Content-type: text/x-json");
include($_SERVER['DOCUMENT_ROOT'] .'/script/HeaderInc.php');
if('XMLHttpRequest' == $_SERVER['HTTP_X_REQUESTED_WITH']){
  //SiteLibs::GetVarDump($_POST);
  $ArrPost = $_POST;
  array_walk($_POST, 'SiteLibs::ConvertArrayIconv', $MS_SP->Sp_Config['charset']);
  if (true === isset($_POST['users_id']) && true === is_numeric($_POST['users_id']) && true === isset($_SESSION['SessionAuth']['id']) && true === is_numeric($_SESSION['SessionAuth']['id'])){
    $ObjMessageMod = new MessageMod($MS_SP);
    $_POST['users_id_from'] = $_SESSION['SessionAuth']['id'];
    $VarResult = $ObjMessageMod->Add($_POST);
    
    $ObjUsersMod = new UsersMod($MS_SP);
    $ArrUsersFrom = $ObjUsersMod->GetFromDb('SqlAll', 'GetRow', "a.users_id = '". $_SESSION['SessionAuth']['id'] ."'");
    $ArrUsersTo = $ObjUsersMod->GetFromDb('SqlAll', 'GetRow', "a.users_id = '". $_POST['users_id'] ."'");
//    SiteLibs::GetVarDump($ArrUsersFrom['users_email']);
//    SiteLibs::GetVarDump($ArrUsersTo['users_email']);
    if(true === $VarResult){
      if(true === isset($ArrUsersFrom['users_email']) && '' != $ArrUsersFrom['users_email'] &&
         true === isset($ArrUsersTo['users_email']) && '' != $ArrUsersTo['users_email'] &&
         '' != $MS_SP->SP_IpsPref['IpsPrefSmtpServer']['ipsprefvalue_setvalue']
        ){

        $ObjMailMod = new MailMod($MS_SP);
        $ObjMailMod->AddressFrom = $ArrUsersFrom['users_email'];
        $ObjMailMod->AddressSubject = SiteLibs::ConvertArrayIconvBack($_POST['message_subject'], '', $MS_SP->Sp_Config['charset']);
        $ObjMailMod->AddressMailServer = $MS_SP->SP_IpsPref['IpsPrefSmtpServer']['ipsprefvalue_setvalue'];
        //foreach ($ArrUsers as $key => $value) {
        $ObjMailMod->AddressTo = $ArrUsersTo['users_email'];
        array_walk($ArrUsersFrom, 'SiteLibs::ConvertArrayIconvBack');
        array_walk($ArrUsersTo, 'SiteLibs::ConvertArrayIconvBack');
        $smarty->assign('ArrUsersFrom', $ArrUsersFrom);
        $smarty->assign('ArrUsersTo', $ArrUsersTo);
        $smarty->assign('ArrPost', $ArrPost);
        $VarHtml = $smarty->fetch('Message/Message.tpl');
        
          $ObjMailMod->MailSend($VarHtml);
          $ArrReturn = array('message' => $MS_SP->Sp_LangMessage['message']['MessMessageSendSuccess']);
      }
      else{
        $ArrReturn = array('message' => $MS_SP->Sp_LangMessage['message']['MessMessageSaveSuccess']);
      }
    }
    else{
      $ArrReturn = array('error' => $MS_SP->Sp_LangMessage['error']['ErrFuncExecute']);
    }
  }
}
else{
  $ArrReturn = array('error' => $MS_SP->Sp_LangMessage['error']['ErrFuncAccessDenied']);
}
array_walk($ArrReturn, 'SiteLibs::ConvertArrayIconvBack', $MS_SP->Sp_Config['charset']);
echo json_encode($ArrReturn);
include($_SERVER['DOCUMENT_ROOT'] .'/script/FooterInc.php');
?>