
<?php
  if(empty($_POST))
  {
    if(!file_exists($LetterFile))
    {
      include('emailsettings.inc');
      $MessText = base64_decode($MessText);
    }
    else {
      $Letter = file($LetterFile);
      $MessSubject = trim($Letter[0]);
      $EmailFrom = trim($Letter[1]);
      $MessText = base64_decode(trim($Letter[2]));
    }
  }
  if(!empty($_POST['SaveSettings']))
  {
    //инициализируем переменные
    $MessSubject = $_POST['MessSubject'];
	  $EmailFrom = $_POST['EmailFrom'];
	  $MessText = $_POST['MessText'];
    //проверяем их
      //MessSubject
    if($MessSubject == '')
      $Errors[] = $Err_lapfe_NoMessSubject;
      //Email
    elseif(empty($EmailFrom))
      $Errors[] = $Err_lapfe_NoEmailFrom;
    elseif (!preg_match("/^[-0-9a-z_]+@[-0-9a-z_^\.]+\.[a-z]{2,3}$/i", $EmailFrom))
      $Errors[] = $Err_lapfe_IncorrEmail;
      //текст сообщения
    elseif($MessText == '')
      $Errors[] = $Err_lapfe_NoMessText;
    elseif(strpos($MessText, '%%Login%%') === false)
      $Errors[] = $Err_lapfe_NoLoginInMessage;
    elseif(strpos($MessText, '%%Password%%') === false)
      $Errors[] = $Err_lapfe_NoPasswordInMessage;
    else {
      $MessSubject = CorrectInput($MessSubject);
      $MessSubject = substr($MessSubject, 0, 60);
      $MessText = CorrectInput($MessText);
      $MessText = substr($MessText, 0, 4000);
      $MessText = base64_encode($MessText);
    }
    //формируем строку для записи в файл  emailsettings.inc
    if(empty($Errors))
    {
      $Str = $MessSubject."\n".$EmailFrom."\n".$MessText;
      SaveStringInFile($LetterFile, $Str, 'w');
      Refresh("ch_passwords_emailset&pswdfile={$PswdFile}&questlength={$QuestLength}'");
      //$Saved = true;
    }
  }
 // if($Saved !== true)

?>

<?php


   //форма
   print "<form action='' method=post>";
   print "<h5>{$Heading_lapfe_LetterInvitation}</h5>";
   print "<h5><i>{$Question}</i></h5>";
   //подсказки
   if($ShowTips == 'yes')
      ShowTips($Page);
   //ошибки
   ShowErrors($Errors, '383', 'VeryMiniField');

   //форма
   print "<fieldset class=VeryMiniField align=center>";
   print "<table class=General align=center border=0 cellspacing=0 cellpadding=0 width=60%>";
     print "<tr>";
       print "<td>{$Cpt_lapfe_MessSubject}</td>";
     print "</tr>";
     print "<tr>";
       print "<td><input type='text' name='MessSubject' value='{$MessSubject}' size=60></td>";
     print "</tr>";
     print "<tr>";
       print "<td>{$Cpt_lapfe_EmailFrom}</td>";
     print "</tr>";
     print "<tr>";
       print "<td><input type='text' name='EmailFrom' value='{$EmailFrom}' size=60></td>";
     print "</tr>";
     print "<tr>";
       print "<td>{$Cpt_lapfe_MessText}</td>";
     print "</tr>";
     print "<tr>";
       print "<td><textarea name=MessText cols=45 rows=8>{$MessText}</textarea></td>";
     print "</tr>";
     print "<tr align=center><td>";
       print "<input type=button class=buttonsettings value='{$Cpt_Back}' onClick='history.back()'>";
       print "<input class=buttonsettings type=submit value='{$Cpt_Save}' name = 'SaveSettings'>";
     print "</td></tr>";
   print "</table>";
   print "</fieldset>";
   print "</form>";

?>