<?php

include('inc/registration_limit.php');

$RegistrationLimit = new RegistrationLimit($db, $server, $Config);

function sha_password($account, $password)
{
  $account = strtoupper($account);
  $password = strtoupper($password);   
  return(sha1($account.':'.$password));
}

if(array_key_exists('account', $_POST)) $account = $_POST['account']; else $account = '';
if(array_key_exists('heslo1', $_POST)) $password1 = $_POST['heslo1']; else $password1 = '';
if(array_key_exists('heslo2', $_POST)) $password2 = $_POST['heslo2']; else $password2 = '';
if(array_key_exists('email', $_POST)) $email = $_POST['email']; else $email = '';
if(array_key_exists('expansion', $_POST)) $expansion = $_POST['expansion']; else $expansion = '';
if(array_key_exists('agree', $_POST)) $agree = $_POST['agree']; else $agree = '';

echo('<div style="margin-left:auto; margin-right:auto; text-align:center; align: center;">'.
  '<div style="margin-left:auto; margin-right:auto; text-align:center; align: center;">'.
  '<img style="align: center" src="./imgs/logoreg.png" alt="logo" /></div>'.
  '<form method="post" action="?page=register">'.
  '<table style="margin-left:auto; margin-right:auto; text-align:center;">'.
  '<tr><td>Účet:</td><td><input type="text" name="account" value="'.$account.'" class="textinput"/></td></tr>'.
  '<tr><td>Heslo:</td><td><input type="password" name="heslo1" value="'.$password1.'" class="textinput"/></td></tr>'.
  '<tr><td>Heslo znovu:</td><td><input type="password" name="heslo2" value="'.$password2.'" class="textinput"/></td></tr>'.
  '<tr><td>Email:</td><td><input type="text" name="email" value="'.$email.'" class="textinput"/></td></tr>'.
  '<tr><td>Typ účtu:</td><td><select name="expansion" value="'.$expansion.'">'.
  '<option value="">-- vyber typ účtu --</option>'.
  '<option value="0">Klasický</option>'.
  '<option value="1">Rozšíření TBC</option>'.
  '<option value="2">Rozšíření WoTLK</option>'.
  '</select></td></tr>'.
  '<tr><td>Pravidla <a href="'.$Config['Web']['BaseURL'].'forum/viewtopic.php?f=11&amp;t=2066">ČTI</a></td><td>Souhlasím <input type="checkbox" name="agree" value="on" /></td><td></td></tr>'.
  '<tr><td></td><td><input type="submit" value="Registrovat" /></td></tr></table>'.
  '</form>'.
  '<br />');

//if($RegistrationLimit->GetFreeRegistrationCount() == 0)
//{
//  echo('<div class="h10">Registrace pro dnešní den vyčerpány</div>');
//  echo('<div class="h10">Dnes bylo '.$RegistrationLimit->GetPerDeyRegistrationCount().' volných registrací</div>');
//} else
{
  //echo('<div class="h11">Pro tento den je povoleno '.$RegistrationLimit->GetPerDeyRegistrationCount().' volných registrací</div>');
  //echo('<div class="h11">Zbývá ještě '.$RegistrationLimit->GetFreeRegistrationCount().' volných registrací</div>');
  echo('<br />');

  if(($account == '') or ($password1 == '') or ($password2 == '') or ($email == '') or ($expansion == '') or ($agree == ''))
  {
    echo('<div class="h10">Je nutno vyplnit všechny údaje</div>');
  } else
  {
    if($password1 != $password2) echo('Zadaná hesla se neshodují');
    else
    {
      $password = $password1;
      $account = strtoupper($account);
      $heslo = sha_password($account, $password);

      $db->select_db($Config['Mangos']['DatabaseRealmd']);
      $DbResult = $db->query('SELECT `id`, `username`, `sha_pass_hash` FROM `account` WHERE `username`="'.$account.'"');
      if($DbResult->num_rows > 0)
      {
        echo('<div class="h10">Účet již existuje</div>');
      } else
      {       
        $DbResult = $db->query('INSERT INTO `account` (`username`,`sha_pass_hash`,`email`,`expansion`) values ("'.$account.'","'.$heslo.'","'.$email.'","'.$expansion.'")');
        echo('<div class="h10">Účet '.$account.' byl vytvořen!</div>');
      }
    }
  }
}
echo('</div><p></p>');

?>
