
<?php
////////////////////////////////////////////////////////////////////////////////
//модуль генерирует пароли
////////////////////////////////////////////////////////////////////////////////
?>
<?php
  if(!empty($_POST['Generate']))
  {
    $Source = $_POST['source'];
    $Length = trim($_POST['passwdlength']);
    $Action = $_POST['action'];
    //верхний и нижний предел для генератора паролей
    $Length = $Length - 5;
    //обрабатываем базу участников и генерируем пароли

    if($Source == 'PollUsersDataBase')
      $SourceFile = $UsersDatabase; else
      $SourceFile = "../txt/".$Source;
    $Users = @file($SourceFile);
      if(empty($Users))
      {
        print "{$Err_gen_IncorrImportSource} <a href='index.php?page=ch_passwords&pswdfile={$PswdFile}&questlength={$QuestLength}'>({$Cpt_Back})</a>";
        print "</body></html>";
        exit;
      }

    for($i = 0; $i < count($Users); $i = $i + 2)
    {
      $Login = trim($Users[$i])."\n";
      if($Source != 'PollUsersDataBase')
        $Login = str_replace(' (%%%voted%%%)', '', $Login);
      $Generated[] = $Login;
      if($Source == 'PollUsersDataBase')
        $Generated[] = trim($Users[($i+1)])."\n";
      $Password = substr($Users[$i], 0, 5);
      $Password = trim($Password);
      if(strlen($Password) < 5)
      {
        while(strlen($Password) != 5)
          $Password = $Password.'0';
      }
      while(strlen($NumStr) != $Length)
        $NumStr .= rand(0, 9);
      $Password .= $NumStr;
      $Generated[] = $Password."\n";
      $NumStr = '';
    }   //for($i = 0; $i < count($Users); $i = $i + 2)
    $GenStr = serialize($Generated);
    $StrLen = strlen($GenStr);
    SaveStringInFile($File_Tmp, $GenStr, 'w');                                          //1 - база участн
    if($Source == 'PollUsersDataBase')
      Refresh("ch_passwords_gener_updatepswd&pswdfile={$PswdFile}&questlength=$QuestLength&source=1&action={$Action}&strlen={$StrLen}");
    else
      Refresh("ch_passwords_gener_updatepswd&pswdfile={$PswdFile}&questlength=$QuestLength&source=2&action={$Action}&strlen={$StrLen}");
  }  //if(!empty($_POST['Generate']))
?>