
<?php
  $PswdFile = $_GET['pswdfile'];
  $QuestLength = $_GET['questlength'];
  $LetterFile = "../txt/".str_replace('pswd', 'onepassd_letter', $PswdFile);
  $Question = GetQuestion("../txt/".$PswdFile, $QuestLength, 'pswd');
  if(empty($_POST))
  {
    $Letter = @file($LetterFile);
    if(!empty($Letter))
    {
      $MessSubject = trim($Letter[0]);
      $EmailFrom = trim($Letter[1]);
      $MessText = base64_decode(trim($Letter[2]));
    }
  }
  elseif(!empty($_POST['SaveSettings']))
  {
    //инициализируем переменные
    $MessSubject = $_POST['MessSubject'];
	  $EmailFrom = $_POST['EmailFrom'];
	  $MessText = $_POST['MessText'];
    //проверяем их
      //MessSubject
    if($MessSubject == '')
      $Errors[] = $Err_NoMessSubject; else
    {
      $MessSubject = CorrectInput($MessSubject);
      $MessSubject = substr($MessSubject, 0, 60);
    }
      //Email
    if(empty($EmailFrom))
      $Errors[] = $Err_NoEmailFrom;
    elseif (!preg_match("/[0-9a-z_]+@[0-9a-z_^\.]+\.[a-z]{2,3}/i", $EmailFrom))
      $Errors[] = $Err_opfa_IncorrEmail;
      //текст сообщения
    if($MessText == '')
      $Errors[] = $Err_NoMessText;
    else {
      $MessText = CorrectInput($MessText);
      $MessText = substr($MessText, 0, 4000);
      $MessText = base64_encode($MessText);
    }
    //формируем строку для записи в файл  emailsettings.inc
    if(empty($Errors))
    {
      $Str = $MessSubject."\n".$EmailFrom."\n".$MessText."\n";
      SaveStringInFile($LetterFile, $Str, 'w');
      $MessText = base64_decode($MessText);
      Refresh("changepassword_emailset&pswdfile={$PswdFile}&questlength={$QuestLength}");

      //$Saved = true;
    }
  }
 // if($Saved !== true)

?>

<?php

   print "<form action='' method=post>";
   print "<h4>{$Heading_LetterInvitation}</h4>";
   print "<h5><i>{$Question}</i></h5>";
   //ошибки
   ShowErrors($Errors, '100%', '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_opfa_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_opfa_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_opfa_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>";

?>