<?php

include('global.php');

class Index extends Page
{
  var $FullTitle = 'Seznam serverů';
  var $ShortTitle = 'Seznam serverů';
  
  function ShowServerListOnRow($Row)
  {
    $Row['NetworkPortRealmd'] = '<a href="realmlist.php?Id='.$Row['Id'].'">'.$Row['NetworkPortRealmd'].'</a>';
    if($Row['Homepage'] != '') $Row['Name'] = '<a href="'.$Row['Homepage'].'">'.$Row['Name'].'</a>';
    unset($Row['Homepage']);
    return($Row);
  }
  
  function ShowServerList()
  {
    global $Config;
    
    $Server = new Server($this->Database, 0);
    $Server->UpdateServerList();
    $Output = '<h4>Seznam serverů</h4>';
    $Table = new Table('ServerList');
    $Table->OnRow = array($this, 'ShowServerListOnRow');
    $Table->Definition['Items']['Id'] = array('Type' => 'Hidden', 'Caption' => '', 'Default' => '');
    $Table->LoadValuesFromDatabase($this->Database);
    $Table->Definition['Items']['Actions'] = array('Type' => 'String', 'Caption' => '', 'Default' => '');
    foreach($Table->Values as $Index => $Item)
    {
     $Table->Values[$Index]['Actions'] = '<a href="?Action=ServerShow&amp;Id='.$Item['Id'].'">Podrobnosti</a>';
     unset($Table->Values[$Index]['Id']);
    }
    $Output .= $Table->Show();
    if($this->System->Modules['User']->User['Role'] >= USER_ROLE_USER) 
    {
      $DbResult = $this->Database->query('SELECT COUNT(*) FROM Server');
      $DbRow = $DbResult->fetch_row();
      $ServerCount = $DbRow[0];
      $DbResult = $this->Database->query('SELECT COUNT(*) FROM Server WHERE User='.$this->System->Modules['User']->User['Id']);
      $DbRow = $DbResult->fetch_row();
      if(($ServerCount < $Config['MaxServerCount']) and ($DbRow[0] == 0))
        $Output .= '<br /><div style="text-align: center;"><a href="?Action=ServerAdd">Vytvořit nový server</a></dev>';      
    }
    return($Output);
  }
  
  function ShowServer($Id)
  {
    global $Config;
    
    $Server = new Server($this->Database, $Id);
    if(isset($Server->Server))
    {
    $Output = '<h4>Podrobnosti serveru</h4>';
    $Form = new Form('ServerView');
    $Form->LoadValuesFromDatabase($Id);
    $Form->Definition['Items']['Realmlist'] = array('Type' => 'String', 'Caption' => 'Adresa k připojení', 'Default' => '');    
    $Form->Definition['Items']['Uptime'] = array('Type' => 'String', 'Caption' => 'Doba běhu', 'Default' => '');
    $Form->Definition['Items']['UsedMemory'] = array('Type' => 'String', 'Caption' => 'Použitá paměť', 'Default' => '');    
    $Form->Definition['Items']['AccountCount'] = array('Type' => 'String', 'Caption' => 'Počet účtů', 'Default' => '');    
    $Form->Definition['Items']['CharacterCount'] = array('Type' => 'String', 'Caption' => 'Počet postav', 'Default' => '');    
    $Form->Definition['Items']['CharacterOnlineCount'] = array('Type' => 'String', 'Caption' => 'Počet hráčů online', 'Default' => '');    
    $ServerStatus = $Server->GetState();
    $Form->Values['Realmlist'] = $Config['Web']['Host'].':'.$Form->Values['NetworkPortRealmd'];
    unset($Form->Definition['Items']['NetworkPortRealmd']);
    $Form->Values['Uptime'] = $this->System->AddPrefixMultipliers($ServerStatus['Uptime'], '', 4, 'Time');
    $Form->Values['UsedMemory'] = $this->System->AddPrefixMultipliers($ServerStatus['UsedMemory'] * 1024, 'B', 4, 'Binary');
    $Form->Values['AccountCount'] = $ServerStatus['AccountCount'];
    $Form->Values['CharacterCount'] = $ServerStatus['CharacterCount'];
    $Form->Values['CharacterOnlineCount'] = $ServerStatus['CharacterOnlineCount'];
    $Output .= $Form->ShowTable();
    $Output .= '<div style="text-align: center">';
    if((($this->System->Modules['User']->User['Role'] >= USER_ROLE_USER) and ($this->System->Modules['User']->User['Id'] == $Server->Server['User'])) or ($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR))
    {
      $Output .= '<br /><a href="http://'.$this->System->Config['Web']['Host'].'/mysql/">Správa databáze</a> ';
      if(array_key_exists('Id', $Server->Server['Database']) and ($Server->Server['Lock'] == 0))
      {
        if($ServerStatus['RealmdPortState'] == true) $Output .= ' <a href="?Action=ServerStop&amp;Id='.$Server->Id.'">Zastavit</a>  <a href="?Action=ServerRestart&amp;Id='.$Server->Id.'">Restartovat</a>';
          else $Output .= ' <a href="?Action=ServerStart&amp;Id='.$Server->Id.'">Spustit</a>';
      }
      if($Server->Server['Lock'] == 0)
      {
        $Output .= ' <a href="?Action=ServerEdit&amp;Id='.$Server->Id.'">Upravit základní nastavení</a>';
        $Output .= ' <a href="?Action=ServerEditDetailed&amp;Id='.$Server->Id.'">Upravit podrobné nastavení</a>';
      }
      //$Output .= ' <a href="?Action=ServerDatabaseImport&amp;Id='.$Server->Id.'">Načtení čisté databáze</a>';
    }
    $Output .= ' <a href="?Action=GameAccountRegister&amp;Id='.$Server->Id.'">Vytvoření herního účtu</a>';
    $Output .= '</div>';
    } else $Output = $this->SystemMessage('Zobrazení serveru', 'Server se zadaným id nenalezen.');
    return($Output);
  }
  
  function ShowEmulatorList()
  {
    global $Config;
    
    $Output = '<h4>Seznam verzí emulátoru</h4>';
    $Table = new Table('EmulatorList');
    $Table->Definition['Items']['Id'] = array('Type' => 'Hidden', 'Caption' => '', 'Default' => '');
    $Table->LoadValuesFromDatabase($this->Database);
    $Table->Definition['Items']['Actions'] = array('Type' => 'String', 'Caption' => '', 'Default' => '');
    foreach($Table->Values as $Index => $Item)
    {
      $Table->Values[$Index]['Actions'] = '<a href="?Action=EmulatorShow&amp;Id='.$Item['Id'].'">Podrobnosti</a>';
      unset($Table->Values[$Index]['Id']);
    }
    $Output .= $Table->Show();
    if($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR) 
    {
      $Output .= '<br /><div style="text-align: center;"><a href="?Action=EmulatorAdd">Přidat emulátor</a></dev>';      
    }
    return($Output);
  }

  function ShowUpdateList($ServerId)
  {
    global $Config;
    
    if($this->System->Modules['User']->User['Role'] >= USER_ROLE_USER)
    {
      $Server = new Server($this->Database, $_GET['Id']);
      if(($this->System->Modules['User']->User['Id'] == $Server->Server['User']) or ($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR))
      {
        $Server = new Server($this->Database, $ServerId);
        $Output = '<h4>Seznam dostupných aktulizací</h4>';
        $Output .= 'Před provedením aktualizace bude server zastaven a provedena záloha databáze.';
        $Table = new Table('EmulatorList');
        $Table->Definition['Table'] = substr($Table->Definition['Table'], 0, -1).' AND `Emulator`.`Revision` > '.$Server->Server['Database']['Emulator']['Revision'].' ORDER BY `Revision`)';
        $Table->Definition['Items']['Id'] = array('Type' => 'Hidden', 'Caption' => '', 'Default' => '');
        $Table->LoadValuesFromDatabase($this->Database);
        $Table->Definition['Items']['Actions'] = array('Type' => 'String', 'Caption' => '', 'Default' => '');
        foreach($Table->Values as $Index => $Item)
        {
          $Table->Values[$Index]['Actions'] = '<a href="?Action=EmulatorShow&amp;Id='.$Item['Id'].'">Podrobnosti</a>';
          if($Server->Server['Lock'] == 0) $Table->Values[$Index]['Actions'] .= ' <a href="?Action=Update&amp;Server='.$ServerId.'&amp;Update='.$Item['Id'].'">Aktualizovat</a>';
          unset($Table->Values[$Index]['Id']);
        }
        $Output .= $Table->Show();
      } else $this->SystemMessage('Dostupné aktualizace', 'Nemáte oprávnění');
    } else $Output = USER_BAD_ROLE;    
    return($Output);
  }
  
  function EmulatorShow($Id)
  {
    //$Server = new Server($this->Database, $Id);
    $Output = '<h4>Podrobnosti emulátoru</h4>';
    $Form = new Form('EmulatorItem');
    $Form->LoadValuesFromDatabase($Id);
    $Output .= $Form->ShowTable();
    $Output .= '<div style="text-align: center">';
    $Emulator = new Emulator($this->Database, $Id);
    if($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR)
    {
      if($Emulator->Emulator['Lock'] == 0) $Output .= ' <a href="?Action=EmulatorDownload&amp;Id='.$Id.'">Stáhnout</a>';
      if($Emulator->Emulator['Lock'] == 0) $Output .= ' <a href="?Action=EmulatorCompile&amp;Id='.$Id.'">Přeložit</a>';
    }
    $Output .= '</div>';
    return($Output);
  }
  
  function ShowBackupListOnRow($Row)
  {
    //$Row['Name'] = '<a href="?Action=EmulatorShow&amp;Id='.$Row['Id'].'">'.$Row['Name'].'</a>';
    return($Row);
  }
  
  function ShowBackupList($ServerId)
  {
    global $Config;
    
    if($this->System->Modules['User']->User['Role'] >= USER_ROLE_USER)
    {
      $Server = new Server($this->Database, $_GET['Id']);
      if(($this->System->Modules['User']->User['Id'] == $Server->Server['User']) or ($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR))
      {
        $Output = '<h4>Dostupné zálohy</h4>';
        $Table = new Table('BackupList');
        $Table->OnRow = array($this, 'ShowBackupListOnRow');
        $Table->Definition['Table'] = '(SELECT * FROM `Backup` WHERE `Server` = '.$Server->Id.')';
        $Table->Definition['Items']['Id'] = array('Type' => 'Hidden', 'Caption' => 'Id', 'Default' => '');
        $Table->Definition['Items']['Lock'] = array('Type' => 'Hidden', 'Caption' => 'Zámek', 'Default' => '');
        $Table->LoadValuesFromDatabase($this->Database);
        $Table->Definition['Items']['Actions'] = array('Type' => 'String', 'Caption' => '', 'Default' => '');
        foreach($Table->Values as $Index => $Value)
        {
          $Table->Values[$Index]['Actions'] = '';
          if($Value['Lock'] == 0) $Table->Values[$Index]['Actions'] = '<a href="?Action=BackupDownload&amp;Id='.$Value['Id'].'">Stáhnout</a>';
          if(($Server->Server['Lock'] == 0) and ($Value['Lock'] == 0)) $Table->Values[$Index]['Actions'] .= ' <a href="?Action=BackupRestore&amp;Id='.$Value['Id'].'">Obnovit</a>';
          unset($Table->Values[$Index]['Id']);
          unset($Table->Values[$Index]['Lock']);
        }
        $Output .= $Table->Show();
        if($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR) 
        {
          if($Server->Server['Lock'] == 0) $Output .= '<br /><div style="text-align: center;"><a href="?Action=BackupAdd&amp;Id='.$ServerId.'">Zálohovat</a></dev>';      
        }
      } else $Output = $this->SystemMessage('Zastavení serveru', 'Nemáte oprávnění');
    } else $Output = USER_BAD_ROLE;    
    return($Output);
  }

  function ShowClientListOnRow($Row)
  {
    $Row['Version'] = '<a href="http://www.wowwiki.com/Patch_'.$Row['Version'].'">'.$Row['Version'].'</a>';
    return($Row);
  }
  
  function ShowClientList()
  {
    global $Config;
    
    $Output = '<h4>Verze herního klienta</h4>';
    $Table = new Table('ClientList');
    $Table->OnRow = array($this, 'ShowClientListOnRow');
    $Table->LoadValuesFromDatabase($this->Database);
    $Output .= $Table->Show();
    if($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR) 
    {
      //$Output .= '<br /><div style="text-align: center;"><a href="?Action=BackupAdd">Zálohovat</a></dev>';      
    }
    return($Output);
  }

  function ShowTaskListOnRow($Row)
  {
    $Row['Duration'] = $this->System->AddPrefixMultipliers($Row['Duration'], '', 4, 'Time');
    return($Row);
  }
  
  function ShowTaskList()
  {
    global $Config;
    
    if($this->System->Modules['User']->User['Role'] >= USER_ROLE_USER)
    {
      $Output = '<h4>Fronta úloh</h4>';
      $Table = new Table('TaskList');
      $Table->OnRow = array($this, 'ShowTaskListOnRow');
      $Table->Definition['Table'] = '(SELECT (COALESCE(UNIX_TIMESTAMP(TimeEnd), UNIX_TIMESTAMP(NOW())) - UNIX_TIMESTAMP(TimeStart)) AS Duration, TimeCreate, Title, State FROM Task WHERE User='.$this->System->Modules['User']->User['Id'].' ORDER BY Id DESC)';
      $Table->LoadValuesFromDatabase($this->Database);
      $Output .= $Table->Show();
      $Output .= '<br /><div style="text-align: center;"><a href="?Action=TaskList">Obnovit pohled</a></dev>';      
    } else $Output .= USER_BAD_ROLE;
    return($Output);
  }

  function ShowDebugListOnRow($Row)
  {
    $Row['Time'] = '<a href="?Action=ServerDebug&amp;Id='.$Row['Id'].'&amp;Show=Backtrace">'.str_replace(' ', '&nbsp;', $Row['Time']).'</a>';
    $Row['Uptime'] = TimeToHumanTime($Row['Uptime']);
    return($Row);
  }
  
  function ShowDebugList()
  {
    global $Config;
    
    if($this->System->Modules['User']->User['Role'] >= USER_ROLE_USER)
    {
      $Server = new Server($this->Database, $_GET['Id']);
      if(($this->System->Modules['User']->User['Id'] == $Server->Server['User']) or ($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR))
      {
        $Output = '<h4>Ladící záznamy restartů</h4>';
        $Table = new Table('DebugList');
        $Table->OnRow = array($this, 'ShowDebugListOnRow');
        $Table->Definition['Table'] = '(SELECT * FROM Debug WHERE Server='.$_GET['Id'].')';
        $Table->LoadValuesFromDatabase($this->Database);
        $Output .= $Table->Show();
        if($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR) 
        {
          //$Output .= '<br /><div style="text-align: center;"><a href="?Action=BackupAdd">Zálohovat</a></dev>';      
        }
      } else $this->SystemMessage('Ladící záznamy', 'Nemáte oprávnění');
    } else $Output .= USER_BAD_ROLE;
    return($Output);
  }
  
  function ShowLoginForm()
  {
    $Form = new Form('UserLogin');
    $Form->OnSubmit = '?Action=Login';
    $Output = $Form->ShowEditForm();
    $Output .= '<div class="Centred"><a href="?Action=UserRegister">Registrovat se</a> '.
    '<a href="?Action=PasswordRecovery">Obnova zapomenutého hesla</a></div>';
    return($Output);
  }
  
  function ImportDatabase()
  {
    if($this->System->Modules['User']->User['Role'] >= USER_ROLE_USER)
    {
      $Server = new Server($this->Database, $_GET['Id']);
      if(($this->System->Modules['User']->User['Id'] == $Server->Server['User']) or ($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR))
      {
        $Server->ImportDatabase(true);
        $Output .= $this->SystemMessage('Import čisté databáze', 'Úloha zařazena do fronty');
        $Output .= $this->ShowTaskList();
      } else $this->SystemMessage('Import databáze', 'Nemáte oprávnění');
    } else $Output .= USER_BAD_ROLE;
  }
  
  function ShowWelcome()
  {
    $Output = '<p>Vítejte v projektu zaměřeném na free hosting WoW serverů.</p>'.
    '<strong>Použité technologie:</strong><br />'.
    '<ul>'.
    '<li><a href="http://www.worldofwarcraft.com/">World of Warcraft</a> - fantasy MMORPG</li>'.
    '<li><a href="http://www.getmangos.com/">MaNGOS</a> - MMORPG server</li>'.
    '<li><a href="http://www.udbforums.org/">UDB</a> - databáze pro MaNGOS</li>'.
    '<li><a href="http://www.scriptdev2.com/">ScriptDev2</a> - doplňkový skriptovací systém pro MaNGOS</li>'.
    '</ul>';
    return($Output);
  }

  function UserMenu()
  {
    $Output = '<strong>Uživatelské menu</strong><br />';
    if($this->System->Modules['User']->User['Id'] != $this->System->Modules['User']->AnonymousUserId) 
    {
      $DbResult = $this->Database->query('SELECT COUNT(*) FROM Server WHERE User='.$this->System->Modules['User']->User['Id']);
      $DbRow = $DbResult->fetch_row();
      if($DbRow[0] > 0)
      { 
        $DbResult = $this->Database->query('SELECT Id FROM Server WHERE User='.$this->System->Modules['User']->User['Id']);
        $Server = $DbResult->fetch_assoc();
        $Output .= '<div><a href="?Action=ServerShow&amp;Id='.$Server['Id'].'">Můj server</a></div>';
        $Output .= '<div><a href="?Action=DebugList&amp;Id='.$Server['Id'].'">Ladící záznamy</a></div>';
        $Output .= '<div><a href="?Action=BackupList&amp;Id='.$Server['Id'].'">Zálohy</a></div>';
        $Output .= '<div><a href="?Action=TaskList">Fronta úloh</a></div>';
        $Output .= '<div><a href="?Action=UpdateList&amp;Id='.$Server['Id'].'">Dostupné aktualizace</a></div>';
      } else $Output .= '<div><a href="?Action=ServerAdd">Vytvořit vlastní server</a></div>';
      if($this->System->Modules['User']->User['Role'] == USER_ROLE_ADMINISTRATOR) 
      {
        $Output .= '<div><a href="?Action=NewsAdd">Přidat aktualitu</a></div>';
      }
    }
    return($Output);
  }
  
  function ShowInfo()
  {
    global $Config;
    
    $Output = '<h4>Informace</h4>'.
    '<p>Tento free hosting WoW serverů vám nabízí zdarma vytvoření vlastního malého serveru. Vytvoření a správa serveru nikdy nebyly jednodušší.</p>'.
    '<strong>Pro koho je hosting určen?</strong>'.
    '<ul>'.
    '<li>Pro ty, kteří se chtějí starat o vlastní server, ale nemají dostatek financí nebo prostoru pro jeho provozování.</li>'.
    '<li>Pro ty, kteří si chtějí vyzkoušet, jaké je to být administrátorem či GM na svém serveru.</li>'.
    '</ul><br />'.
    '<strong>Co získáte registrací?</strong>'.
    '<ul>'.
    '<li>Přístup k vlastnímu nepřetržitě běžícímu WoW serveru.</li>'.
    '<li>Možnost si vybrat jednu z nabízených kombinací verze emulátoru, databáze a klienta.</li>'.
    '<li>Možnost provádět aktualizace serveru na pár kliknutí.</li>'.
    '<li>Periodické noční zálohování a možnost ruční obnovy.</li>'.
    '<li>Max. 50 online hrajících hráčů.</li>'.
    '<li>Neomezený počet účtů a postav.</li>'.
    '<li>Možnost stáhnout si celou databázi serveru a v případě zájmů hráčů tak přejít na vlastní výkonější vyhrazený server.</li>'.
    '<li>Automatické restartování serveru při selhání a automatický záznam ladících informací o pádu.</li>'.
    '</ul><br />'.
    '<p>Vámi vytvořený server může být bez předchozího upozornění smazán, např. pokud nebude již využíván nebo bude potřeba snížit vytížení hostingu.</p>'.
    'Technická podpora: '.$Config['Web']['AdminEmail'];
    return($Output);
  }
    
  function Show()
  {
    global $Config;
    
    $Output = '';
    if(array_key_exists('Action', $_GET))
    {
      if($_GET['Action'] == 'LoginForm')
      {
        $Output .= $this->ShowLoginForm();
      } else
      if($_GET['Action'] == 'Login')
      {
        $Form = new Form('UserLogin');
        $Form->OnSubmit = '?Action=Login';
        $Form->LoadValuesFromForm();
        $Result = $this->System->Modules['User']->Login($Form->Values['Username'], $Form->Values['Password']);
        $Output = $this->SystemMessage('Přihlášení', $Result);
        if($Result <> USER_LOGGED_IN)
        {
          $Form->Values['Password'] = '';
          $Output .= $Form->ShowEditForm();
          $Output .= '<div class="Centred"><a href="?Action=UserRegister">Registrovat se</a> '.
 	        '<a href="?Action=PasswordRecovery">Obnova zapomenutého hesla</a></div>';
        }
      } else 
      if($_GET['Action'] == 'Logout')
      {
        $Output .= $this->SystemMessage('Odhlášení', $this->System->Modules['User']->Logout());
        $Output .= $this->ShowLoginForm();
      } else
      if($_GET['Action'] == 'UserOptions')
      { 
        $UserOptions = new Form('UserOptions');
        $UserOptions->LoadValuesFromDatabase($this->System->Modules['User']->User['Id']);
        $UserOptions->OnSubmit = '?Action=UserOptionsSave';
        $Output .= $UserOptions->ShowEditForm();
      } else
      if($_GET['Action'] == 'UserOptionsSave')
      {
        $UserOptions = new Form('UserOptions', array());
        $UserOptions->LoadValuesFromForm();
        $UserOptions->SaveValuesToDatabase($this->System->Modules['User']->User['Id']);
        $Output .= $this->SystemMessage('Nastavení', 'Nastavení uloženo.');
        $this->System->Modules['Log']->NewRecord('User', 'Nastavení uživatele změněno', $UserOptions->Values['Name']);
        $UserOptions->LoadValuesFromDatabase($this->System->Modules['User']->User['Id']);
        $UserOptions->OnSubmit = '?Action=UserOptionsSave';
        $Output .= $UserOptions->ShowEditForm();
      } else
      if($_GET['Action'] == 'UserRegister')
      {
        $Form = new Form('UserRegister');
        $Form->LoadValuesFromForm();
        $Form->OnSubmit = '?Action=UserRegisterSave';
        $Output = 'Vyplňte správně požadované údaje. Na zadaný email vám bude zaslán aktivační email.';
        $Output .= $Form->ShowEditForm();
      } else
      if($_GET['Action'] == 'UserRegisterConfirm')
      {
        $Output .= $this->SystemMessage('Potvrzení registrace', $this->System->Modules['User']->RegisterConfirm($_GET['User'], $_GET['H']));
        $Output .= $this->ShowLoginForm();
      } else
      if($_GET['Action'] == 'PasswordRecovery')
      {
        $Form = new Form('PasswordRecovery');
        $Form->OnSubmit = '?Action=PasswordRecovery2';
        $Output .= $Form->ShowEditForm();
      } else
      if($_GET['Action'] == 'PasswordRecovery2')
      {
        $Form = new Form('PasswordRecovery');
        $Form->LoadValuesFromForm();
        $Result = $this->System->Modules['User']->PasswordRecoveryRequest($Form->Values['Name'], $Form->Values['Email']);
        $Output .= $this->SystemMessage('Obnova hesla', $Result);
        if($Result <> USER_PASSWORD_RECOVERY_SUCCESS)
        {
          $Output .= $Form->ShowEditForm();
        }        
      } else
      if($_GET['Action'] == 'PasswordRecoveryConfirm')
      {
        $Output .= $this->SystemMessage('Obnova hesla', $this->System->Modules['User']->PasswordRecoveryConfirm($_GET['User'], $_GET['H'], $_GET['P']));
        $Output .= $this->ShowLoginForm();
      } else
      if($_GET['Action'] == 'UserRegisterSave')
      {
        $Form = new Form('UserRegister', array());
        $Form->LoadValuesFromForm();
        $Result = $this->System->Modules['User']->Register($Form->Values['Login'], $Form->Values['Password'], $Form->Values['Password2'], $Form->Values['Email'], $Form->Values['Name']);
        $Output .= $this->SystemMessage('Registrace nového účtu', $Result);
        if($Result <> USER_REGISTRATED) 
        {
          $Form->OnSubmit = '?Action=UserRegisterSave';
          $Output .= $Form->ShowEditForm();
        }
      } else
      if($_GET['Action'] == 'ServerAdd')
      {
        if($this->System->Modules['User']->User['Role'] >= USER_ROLE_USER)
        {
          $DbResult = $this->Database->query('SELECT COUNT(*) FROM Server');
          $DbRow = $DbResult->fetch_row();
          $ServerCount = $DbRow[0];
          if($ServerCount < $Config['MaxServerCount'])
          {
            $DbResult = $this->Database->query('SELECT COUNT(*) FROM Server WHERE User='.$this->System->Modules['User']->User['Id']);
            $DbRow = $DbResult->fetch_row();
            if($DbRow[0] > 0) $Output .= $this->SystemMessage('Nový server', 'Lze vytvořit pouze jeden server pro každý uživatelský účet.');
            else 
            {
              $Form = new Form('ServerCreate');
              $Form->LoadValuesFromForm();
              $Form->OnSubmit = '?Action=ServerCreate';
              $Output .= 'Tento formulář vám umožní si vytvořit nový server. Pečlivě vyplňte požadované údaje.';
              $Output .= $Form->ShowEditForm();
            }
          } else $Output .= $this->SystemMessage('Nový server', 'Nelze vytvářet další servery');
        } else $Output .= USER_BAD_ROLE;
      } else
      if($_GET['Action'] == 'ServerCreate')
      {
        if($this->System->Modules['User']->User['Role'] >= USER_ROLE_USER)
        {
          $DbResult = $this->Database->query('SELECT COUNT(*) FROM Server WHERE User='.$this->System->Modules['User']->User['Id']);
          $DbRow = $DbResult->fetch_row();
          if($DbRow[0] > 0) $Output .= $this->SystemMessage('Nový server', 'Lze vytvořit pouze jeden server pro každý uživatelský účet.');
          else 
          {
            $DbResult = $this->Database->query('SELECT MAX(NetworkPortRealmd), MAX(NetworkPortWorldd) FROM Server');
            $DbRow = $DbResult->fetch_row();
            $NewPortRealmd = $DbRow[0] + 1;
            if($NewPortRealmd < $Config['BaseNetworkPortRealmd']) $NewPortRealmd = $Config['BaseNetworkPortRealmd'];
            $NewPortWorldd = $DbRow[1] + 1;
            if($NewPortWorldd < $Config['BaseNetworkPortWorldd']) $NewPortWorldd = $Config['BaseNetworkPortWorldd'];
            
            $Form = new Form('ServerCreate', array());
            $Form->LoadValuesFromForm();
            $this->Database->insert('Server', array('Name' => $Form->Values['Name'], 'Description' => $Form->Values['Description'], 'User' => $this->System->Modules['User']->User['Id'], 'Motd' => $Form->Values['Motd'], 'Homepage' => $Form->Values['Homepage'], 'Type' => $Form->Values['Type'], 'Database' => $Form->Values['Database'], 'NetworkPortWorldd' => $NewPortWorldd, 'NetworkPortRealmd' => $NewPortRealmd, 'Lock' => 1, 'TimeCreate' => 'NOW()'));
            $LastInsertId = $this->Database->insert_id;
            $Server = new Server($this->Database, $LastInsertId);
            $Server->CreateDatabase();            
            $Output .= $this->SystemMessage('Vytvoření serveru', $Server->ImportDatabase(true));
            $Output .= $this->ShowTaskList();
            //$UserOptions->LoadValuesFromDatabase($this->System->Modules['User']->User['Id']);
            //$Form->OnSubmit = '?Action=ServerCreate';
            //$Output .= $Form->ShowEditForm();
          }
        } else $Output .= USER_BAD_ROLE;
      } else
      if($_GET['Action'] == 'ServerShow')
      {
        if(array_key_exists('Id', $_GET)) $Output .= $this->ShowServer($_GET['Id']);
        else $this->SystemMessage('Zobrazení serveru', 'Nebylo zadáno Id');
      } else
      if($_GET['Action'] == 'ServerEdit')
      {
        $Server = new Server($this->Database, $_GET['Id']);
        if(($this->System->Modules['User']->User['Id'] == $Server->Server['User']) or ($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR))
        {
          $Form = new Form('ServerEdit');
          $Form->LoadValuesFromDatabase($_GET['Id']);
          $Form->Values['ServerId'] = $_GET['Id'];
          $Form->OnSubmit = '?Action=ServerSave';
          $Output .= $Form->ShowEditForm();
        } else $this->SystemMessage('Nastavení serveru', 'Nemáte oprávnění');
      } else
      if($_GET['Action'] == 'ServerSave')
      {
        $Server = new Server($this->Database, $_POST['ServerId']);
        if(($this->System->Modules['User']->User['Id'] == $Server->Server['User']) or ($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR))
        {
          $Form = new Form('ServerEdit', array());
          $Form->LoadValuesFromForm();
          $ServerId = $_POST['ServerId'];
          unset($Form->Values['ServerId']);
          $Output .= $this->SystemMessage('Upravit server', 'Nastavení serveru uloženo.');
          $Form->SaveValuesToDatabase($_POST['ServerId']);
          $Form->OnSubmit = '?Action=ServerSave';
        
          $Server = new Server($this->Database, $_POST['ServerId']);
          $Server->SaveConfiguration();
          $Form->Values['ServerId'] = $ServerId;
          $Output .= $Form->ShowEditForm();
        } else $this->SystemMessage('Nastavení serveru', 'Nemáte oprávnění');
      } else
      if($_GET['Action'] == 'ServerEditDetailed')
      {
        $Server = new Server($this->Database, $_GET['Id']);
        if(($this->System->Modules['User']->User['Id'] == $Server->Server['User']) or ($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR))
        {
          $Form = new Form('ServerSettingDetailed');
          $EmulatorConfig = new MangosConfigurationFile($this->Database);
          $EmulatorConfig->Load('../server/'.$Server->Id.'/etc/mangosd.conf');
          $Form->Values = $EmulatorConfig->ParameterList;
          $Form->Values['ServerId'] = $_GET['Id'];
          $Form->OnSubmit = '?Action=ServerSaveDetailed';
          $Output .= $Form->ShowEditForm();
        } else $this->SystemMessage('Nastavení serveru', 'Nemáte oprávnění');
      } else
      if($_GET['Action'] == 'ServerSaveDetailed')
      {
        $Server = new Server($this->Database, $_POST['ServerId']);
        if(($this->System->Modules['User']->User['Id'] == $Server->Server['User']) or ($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR))
        {
          $Form = new Form('ServerSettingDetailed', array());
          $Form->LoadValuesFromForm();
          $EmulatorConfig = new MangosConfigurationFile($this->Database);
          $EmulatorConfig->Load('../server/'.$Server->Id.'/etc/mangosd.conf');
          foreach($Form->Values as $Index => $Item)
            $EmulatorConfig->ParameterList[$Index] = $Item;
          $EmulatorConfig->Save('../server/'.$Server->Id.'/etc/mangosd.conf');
          $ServerId = $_POST['ServerId'];
          unset($Form->Values['ServerId']);
          $Output .= $this->SystemMessage('Upravit server', 'Nastavení serveru uloženo.');
          //$Form->SaveValuesToDatabase($_POST['ServerId']);
          $Form->OnSubmit = '?Action=ServerSaveDetailed';
        
          $Server = new Server($this->Database, $_POST['ServerId']);
          $Server->SaveConfiguration();
          $Form->Values['ServerId'] = $ServerId;
          $Output .= $Form->ShowEditForm();
        } else $this->SystemMessage('Nastavení serveru', 'Nemáte oprávnění');
      } else
      if($_GET['Action'] == 'ServerStart')
      {
        if($this->System->Modules['User']->User['Role'] >= USER_ROLE_USER)
        {
          $Server = new Server($this->Database, $_GET['Id']);
          if(($this->System->Modules['User']->User['Id'] == $Server->Server['User']) or ($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR))
          {
            $Output .= $this->SystemMessage('Spuštění serveru', $Server->Start());
            $Output .= $this->ShowTaskList();
          } else $this->SystemMessage('Spuštění serveru', 'Nemáte oprávnění');
        } else $Output .= USER_BAD_ROLE;
      } else
      if($_GET['Action'] == 'ServerStop')
      {
        if($this->System->Modules['User']->User['Role'] >= USER_ROLE_USER)
        {
          $Server = new Server($this->Database, $_GET['Id']);
          if(($this->System->Modules['User']->User['Id'] == $Server->Server['User']) or ($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR))
          {
            $Output .= $this->SystemMessage('Zastavení serveru', $Server->Stop());
            $Output .= $this->ShowTaskList();
          } else $this->SystemMessage('Zastavení serveru', 'Nemáte oprávnění');
        } else $Output .= USER_BAD_ROLE;
      } else
      if($_GET['Action'] == 'ServerDatabaseImport')
      {
        $this->ImportDatabase();
      } else
      if($_GET['Action'] == 'EmulatorShow')
      {
        if(array_key_exists('Id', $_GET)) 
        {
          $Output .= $this->EmulatorShow($_GET['Id']);
        } else $Output .= $this->SystemMessage('Překlad emulátoru', 'Nebylo zadáno Id');
      } else
      if($_GET['Action'] == 'EmulatorList')
      {
        $Output .= $this->ShowEmulatorList();
      } else
      if($_GET['Action'] == 'EmulatorCompile')
      {
        if(array_key_exists('Id', $_GET)) 
        {
          $Emulator = new Emulator($this->Database, $_GET['Id']);
          $Emulator->Compile();       
          $Output .= $this->SystemMessage('Překlad emulátoru', 'Úloha zařazena do fronty');
          $Output .= $this->ShowTaskList();
        } else $Output .= $this->SystemMessage('Překlad emulátoru', 'Nebylo zadáno Id');
      } else
      if($_GET['Action'] == 'EmulatorDownload')
      {
        if(array_key_exists('Id', $_GET)) 
        {
          $Emulator = new Emulator($this->Database, $_GET['Id']);
          $Emulator->Download();       
          $Output .= $this->SystemMessage('Stažení emulátoru', 'Úloha zařazena do fronty');
          $Output .= $this->ShowTaskList();
        } else $Output .= $this->SystemMessage('Stažení emulátoru', 'Nebylo zadáno Id');
      } else
      if($_GET['Action'] == 'ServerDebug')
      {
        if($this->System->Modules['User']->User['Role'] >= USER_ROLE_USER)
        {
          $Server = new Server($this->Database, $_GET['Id']);
          if(($this->System->Modules['User']->User['Id'] == $Server->Server['User']) or ($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR))
          {
            $Output .= '<div>Ladící informace serveru</div>'; 
            $MangosDebug = new MangosDebug($this->Database);
            $Output .= $MangosDebug->Show($Server->Id);
          } else $this->SystemMessage('Ladící informace', 'Nemáte oprávnění');
        } else $Output .= USER_BAD_ROLE;
      } else      
      if($_GET['Action'] == 'ClientList')
      {
        $Output .= $this->ShowClientList();
      } else     
      if($_GET['Action'] == 'BackupList')
      {
        $Output .= $this->ShowBackupList($_GET['Id']);
      } else     
      if($_GET['Action'] == 'DebugList')
      {
        $Output .= $this->ShowDebugList($_GET['Id']);
      } else     
      if($_GET['Action'] == 'UpdateList')
      {
        $Output .= $this->ShowUpdateList($_GET['Id']);
      } else     
      if($_GET['Action'] == 'GameAccountRegister')
      {
        $Form = new Form('GameAccountNew');
        $Form->LoadValuesFromForm();
        $Form->Values['ServerId'] = $_GET['Id'];
        $Form->OnSubmit = '?Action=GameAccountRegister2';
        $Output .= $Form->ShowEditForm();
      } else
      if($_GET['Action'] == 'GameAccountRegister2')
      {
        $Form = new Form('GameAccountNew');
        $Form->LoadValuesFromForm();
        $Server = new Server($this->Database, $Form->Values['ServerId']);
        $Output .= $this->SystemMessage('Vytvoření herního účtu', $Server->NewAccount($Form->Values['Name'], $Form->Values['Password'], $Form->Values['Password2'], $Form->Values['Email'], $Form->Values['Expansion']));
        $Output .= '<a href="?Action=GameAccountRegister&amp;Id='.$Form->Values['ServerId'].'">Zpět k zadání údajů</a>';
      } else      
      if($_GET['Action'] == 'EmulatorAdd')
      {
        if($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR)
        {
          $Form = new Form('EmulatorItem');
          $Form->LoadValuesFromForm();
          $Form->OnSubmit = '?Action=EmulatorAdd2';
          $Output .= $Form->ShowEditForm();
        } else $Output .= USER_BAD_ROLE;
      } else
      if($_GET['Action'] == 'EmulatorAdd2')
      {
        if($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR)
        {
          $Form = new Form('EmulatorItem');
          $Form->LoadValuesFromForm();
          $Form->SaveValuesToDatabase(0);
          $Output .= $this->SystemMessage('Přidání nového emulátoru', 'Emulátor přidán.');
        } else $Output .= USER_BAD_ROLE;
      } else      
      if($_GET['Action'] == 'Info')
      {
        $Output = $this->ShowInfo();
      } else      
      if($_GET['Action'] == 'BackupAdd')
      {
        if(!array_key_exists('Id', $_GET)) $Output .= $this->SystemMessage('Ladící informace', 'Nebylo zadáno Id serveru');
        else if($this->System->Modules['User']->User['Role'] >= USER_ROLE_USER)
        {
          $Server = new Server($this->Database, $_GET['Id']);
          if(($this->System->Modules['User']->User['Id'] == $Server->Server['User']) or ($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR))
          {
            $Backup = new Backup($this->Database, 0);
            $Output .= $this->SystemMessage('Ruční zálohování', $Backup->Create($Server->Id));
            $Output .= $this->ShowTaskList();
          } else $this->SystemMessage('Ladící informace', 'Nemáte oprávnění');
        } else $Output .= USER_BAD_ROLE;
      } else      
      if($_GET['Action'] == 'BackupRestore')
      {
        if(!array_key_exists('Id', $_GET)) $Output .= $this->SystemMessage('Obnovení ze zálohy', 'Nebylo zadáno Id zálohy');
        else if($this->System->Modules['User']->User['Role'] >= USER_ROLE_USER)
        {
          $Backup = new Backup($this->Database, $_GET['Id']);
          $Server = new Server($this->Database, $Backup->Backup['Server']);
          if(($this->System->Modules['User']->User['Id'] == $Server->Server['User']) or ($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR))
          {
            $Output .= $this->SystemMessage('Obnovení ze zálohy', $Backup->Restore());
            $Output .= $this->ShowTaskList();
          } else $this->SystemMessage('Obnovení ze zálohy', 'Nemáte oprávnění');
        } else $Output .= USER_BAD_ROLE;
      } else      
      if($_GET['Action'] == 'BackupDownload')
      {
        if(!array_key_exists('Id', $_GET)) $Output .= $this->SystemMessage('Stažení souboru zálohy', 'Nebylo zadáno Id zálohy');
        else if($this->System->Modules['User']->User['Role'] >= USER_ROLE_USER)
        {
          $Backup = new Backup($this->Database, $_GET['Id']);
          $Server = new Server($this->Database, $Backup->Backup['Server']);
          if(($this->System->Modules['User']->User['Id'] == $Server->Server['User']) or ($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR))
          {
            Header('Content-Type: application/x-tar-gz');
            Header('Content-Disposition: attachment; filename="wowhosting-'.$Backup->Id.'.tar.bz2"');
            echo(file_get_contents('../backup/wowhosting-'.$Backup->Id.'.tar.bz2'));
            exit;
          } else $this->SystemMessage('Stažení souboru zálohy', 'Nemáte oprávnění');
        } else $Output .= USER_BAD_ROLE;
      } else      
      if($_GET['Action'] == 'TaskList')
      {
        $Output .= $this->ShowTaskList();
      } else      
      if($_GET['Action'] == 'Update')
      {
        if(!array_key_exists('Server', $_GET)) $Output .= $this->SystemMessage('Aktualizace serveru', 'Nebylo zadáno Id serveru');
        else if(!array_key_exists('Update', $_GET)) $Output .= $this->SystemMessage('Aktualizace serveru', 'Nebylo zadáno Id aktualizace');
        else if($this->System->Modules['User']->User['Role'] >= USER_ROLE_USER)
        {
          $Server = new Server($this->Database, $_GET['Server']);
          if(($this->System->Modules['User']->User['Id'] == $Server->Server['User']) or ($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR))
          {
            $Output .= $this->SystemMessage('Aktualizace serveru', $Server->Update($_GET['Update']));
            $Output .= $this->ShowTaskList();
          } else $this->SystemMessage('Aktualizace serveru', 'Nemáte oprávnění');
        } else $Output .= USER_BAD_ROLE;
      } else      
      if($_GET['Action'] == 'NewsAdd')
      {
        if($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR)
        {
          $Form = new Form('News');
          $Form->OnSubmit = '?Action=NewsAdd2';
          $Output = $Form->ShowEditForm();
        } else $Output .= USER_BAD_ROLE;
      } else      
      if($_GET['Action'] == 'NewsAdd2')
      {
        if($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR)
        {
          $Form = new Form('News');
          $Form->LoadValuesFromForm();
          $Form->Values['Time'] = 'NOW()';
          $Form->Values['User'] = $this->System->Modules['User']->User['Id'];
          $Form->SaveValuesToDatabase(0);
          $Output = $this->SystemMessage('Nová aktualita', 'Přidáno');
        } else $Output .= USER_BAD_ROLE;
      } else      
      if($_GET['Action'] == 'State')
      {
        $Platform = new Platform($this->Database);
        $State = $Platform->State();
        $Output = '<h4>Stav systému</h4>'.
        '<table class="WideTable">'.
        '<tr><th>Veličina</th><th>Hodnota</th></tr>'.
        '<tr><td>Doba běhu serveru</td><td>'.$this->System->AddPrefixMultipliers($State['Uptime'], '', 4, 'Time').'</td></tr>'.
        '<tr><td>Použitá/celková paměť</td><td>'.$this->System->AddPrefixMultipliers($State['MemoryUsed'], 'B', 4, 'Binary').' / '.$this->System->AddPrefixMultipliers($State['MemoryTotal'], 'B', 4, 'Binary').'</td></tr>'.
        '<tr><td>Počet serverů aktivních/všech/maxiální</td><td>'.$State['ServerOnlineCount'].' / '.$State['ServerCount'].' / '.$State['ServerMaxCount'].'</td></tr>'.
        '<tr><td>Počet emulátorů</td><td>'.$State['EmulatorCount'].'</td></tr>'.
        '<tr><td>Počet uživatelů</td><td>'.$State['UserCount'].'</td></tr>'.
        '<tr><td>Počet databází</td><td>'.$State['DatabaseCount'].'</td></tr>'.
        '<tr><td>Počet záloh</td><td>'.$State['BackupCount'].'</td></tr>'.
        '<tr><td>Čekajících/všech úloh ve frontě</td><td>'.$State['TaskQueued'].' / '.$State['TaskCount'].'</td></tr>'.
        '</table>';
      } else
      if($_GET['Action'] == 'ServerList')
      {
        $Output .= $this->ShowServerList();
      } else
      if($_GET['Action'] == 'Test')
      {
        //$Emulator = new Emulator($this->Database, 2);
        //  $Emulator->Compile();
        //$Server = new Server($this->Database, 1);
        //$Output .= $Server->ImportDatabase();
        //$Config = new MangosConfigurationFile($this->Database);
        //$Config->Load('../emu/mangos/7681/etc/mangosd.conf');
        //$Config->Save('../server/1/etc/mangosd.conf');
      } else
      {
        $Output .= $this->ShowWelcome();
      }
    } else 
    {
      $Output .= $this->ShowWelcome();
    }
    $Content = $Output;
    $Output = '<table class="BasicTable"><tr>';
    if($this->System->Modules['User']->User['Id'] != $this->System->Modules['User']->AnonymousUserId) 
      $Output .= '<td class="UserMenu">'.$this->UserMenu().'</td>';
    $Output .= '<td class="Content">'.$Content.'</td>';
    if(!array_key_exists('Action', $_GET))
    {
      $Output .= '<td class="News"><strong>Aktuálně:</strong><br />';
      $DbResult = $this->Database->query('SELECT * FROM News');
      while($DbRow = $DbResult->fetch_assoc())
      {
        $Output .= '<div><strong>'.$DbRow['Title'].'</strong>('.HumanDate($DbRow['Time']).')<br />'.$DbRow['Content'].'</div>';
      }
      $Output .= '</td>';
    }
    $Output .= '</tr></table>';
    return($Output);
  }
}

$System->AddModule(new Index());
$System->Modules['Index']->GetOutput();

?>
