
<?php
  //Инициализируем переменные
  $PswdFile = $_GET['pswdfile'];
  $QuestLength=$_GET['questlength'];
  $Source=$_GET['source'];
  $Action=$_GET['action'];
  $StrLen = $_GET['strlen'];
  $Question = getquestion($PswdFile, $QuestLength);
   //выбираем из файла массив с логинами, паролями и адресами
  $fp = fopen($File_Tmp, 'r');
  $Serialized = fread($fp, $StrLen);
  fclose($fp);
  $Generated = unserialize($Serialized);
  if(empty($_GET['conf']))
  {
    if($Action == 'supply')
      $Caption = "{$Conf_gen_SupplyDataBase} <br> <b>{$Question}</b>";
    elseif($Action == 'rewrite')
      $Caption = "{$Conf_gen_RewriteDataBase} <br> <b>{$Question}</b>";
    print "<br><br>";
    print "<div align=center>";
      print "{$Caption}<br><br>";
      print "<a href=index.php?page=ch_passwords_gener_updatepswd&conf=1&pswdfile={$PswdFile}&questlength=$QuestLength&source={$Source}&action={$Action}&strlen={$StrLen}>{$Cpt_Yes}</a>&nbsp;&nbsp;&nbsp;";
      print "<a href=index.php?page=ch_passwords&pswdfile={$PswdFile}&questlength={$QuestLength}>{$Cpt_No}</a>";
    print "</div><br>";
    print "<table cellspacing=0 cellpadding=0 border=0 align=center>";
    if($Source == 1)
      for($i = 0; $i < count($Generated); $i = $i + 3)
	    {
	      print "<tr><td><b>".$Generated[$i]."</b></td></tr>";
	      print "<tr><td>".$Generated[($i+1)]."</td></tr>";
	      print "<tr><td>".$Generated[($i+2)]."</td></tr>";

	    }
    elseif($Source == 2)
      for($i = 0; $i < count($Generated); $i = $i + 2)
	    {
	      print "<tr><td><b>".$Generated[$i]."</b></td></tr>";
	      print "<tr><td>".$Generated[($i+1)]."</td></tr>";

	    }
    print "</table>";
  }


  if($_GET['conf'] == 1)
  {
	  //если источник логинов - база участников
	  if($Source == 1)
	  {
	    for($i = 0; $i < count($Generated); $i = $i + 3)
	    {
        $Login = trim($Generated[$i]);
        $Password = trim($Generated[($i+2)]);
        $Password = md5($Password);
        $ToWrite .= $Login."\n".$Password."\n";
	    }
    }
    elseif($Source == 2)
    {
      for($i = 0; $i < count($Generated); $i = $i + 2)
	    {
        $Login = trim($Generated[$i]);
        $Password = trim($Generated[($i+1)]);
        $Password = md5($Password);
        $ToWrite .= $Login."\n".$Password."\n";
	    }
    }
    if($Action == 'supply')
    {
      $Users = @file($PswdFile);
      if(!empty($Users))
        $UsersStr = implode('', $Users);
      $Str = $ToWrite.$UsersStr;
      SaveStringInFile($PswdFile, $Str, 'w');
    } //if($Action == 'supply')
    elseif($Action == 'rewrite')
      SaveStringInFile($PswdFile, $ToWrite, 'w');
    if(file_exists($LetterFile) && ($Source != 2))
      Refresh("ch_passwords_gener_sendpswd&pswdfile={$PswdFile}&questlength=$QuestLength");
    else
      Refresh("ch_passwords&pswdfile={$PswdFile}&questlength={$QuestLength}");
  }

?>