<?php

$ScriptStartTime = GetMicrotime();

if(!isset($_SESSION)) session_start();

// SQL injection hack protection
foreach($_POST as $Index => $Item)
{
  if(is_array($_POST[$Index])) 
    foreach($_POST[$Index] as $Index2 => $Item2) $_POST[$Index][$Index2] = addslashes($Item2);
  else $_POST[$Index] = addslashes($_POST[$Index]);
}
foreach($_GET as $Index => $Item) $_GET[$Index] = addslashes($_GET[$Index]);

include('config.php');
include_once('error.php');
include('databaseconection.php');
include('global_function.php');

$Database = new Database($Config['Database']['Host'], $Config['Database']['User'], $Config['Database']['Password']); 
$Database->SQLCommand('SET NAMES '.$Config['Database']['Charset']);
$Database->SelectDatabase($Config['Database']['Database']);    

$TranslationTree = GetTranslationTree();

function GetMicrotime()
{
  list($Usec, $Sec) = explode(' ', microtime());
  return ((float)$Usec + (float)$Sec);
}

function ShowShoutbox()
{
  global $Database;
  
  echo('<strong>Shoutbox: </strong><a href="action.php?action=shoutbox">Vložit</a>
  <div class="box">
  <table>');
  $ID = $Database->SQLCommand("SELECT * FROM shoutbox ORDER BY ID DESC LIMIT 10");
  while($Line = mysql_fetch_assoc($ID))
    echo('<tr><td><strong>'.$Line['user'].'</strong>: '.$Line['text'].'</td></tr>');
  echo('</table></div>');
}

function ShowLoginBox()
{
  if(Licence(LICENCE_USER)) 
  {
     // echo 'Jste přihlášen jako: <b>'.$_SESSION['User'].'</b> <a href="index.php?Logout">Odhlásit</a>';
  } else 
  {
    echo('<strong>Přihlášení:</strong>
  <form action="index.php" method="post">
  <table>
    <tr>
      <td><input type="text" name="LoginUser" size="13" /></td>
    </tr>
    <tr>
      <td><input type="password" name="LoginPass" size="13" /></td>
    </tr>
    <tr>
      <th><input type="submit" value="Přihlásit" /></th>
    </tr>
  </table>
  </form>');
  }
}

function ShowMainMenu()
{ 
  global $TranslationTree, $Config;
  
  $Menu = array
  (
    $Config['Web']['BaseURL'].'index.php' => array(LICENCE_ANONYMOUS, 'Hlavní stránka', 'Domů'),
	'http://wow.zdechov.net/forum/viewforum.php?f=22' => array(LICENCE_ANONYMOUS, 'Diskuze na téma překládání', 'Fórum projektu'),
	'javascript:void(0)' => array(LICENCE_ANONYMOUS, 'Slovník wow výrazů', 'Slovníček', 'javascript:window.open(\'dictionary.php\', \'Slovník\', \'width=400, height=390\'); return false;'),
	$Config['Web']['BaseURL'].'banners.php' => array(LICENCE_ANONYMOUS, 'Weby odkazující na tento projekt bennerem', 'Odkazují na nás'),
	$Config['Web']['BaseURL'].'statistic.php' => array(LICENCE_ANONYMOUS, 'Stav dokončení překládů', 'Stav dokončení'),
	$Config['Web']['BaseURL'].'userlist.php' => array(LICENCE_ANONYMOUS, 'Seznam registrovaných uživatelů', 'Seznam uživatelů'),
	$Config['Web']['BaseURL'].'team.php' => array(LICENCE_ANONYMOUS, 'Seznam překladatelských týmů', 'Seznam týmů'),
	$Config['Web']['BaseURL'].'download.php' => array(LICENCE_ANONYMOUS, 'Zde si můžete stáhnout přeložené texty', 'Stahování'),
	$Config['Web']['BaseURL'].'info.php' => array(LICENCE_ANONYMOUS, 'Informace k překladu hry', 'Informace'),
	$Config['Web']['BaseURL'].'registrace.php' => array(LICENCE_ANONYMOUS, 'Registrace uživatele pro překládání', 'Registrace'),
	$Config['Web']['BaseURL'].'Options.php' => array(LICENCE_USER, 'Nastavení uživatele', 'Nastavení'),
    $Config['Web']['BaseURL'].'index.php?Logout' => array(LICENCE_USER, 'Odhlášení ze systému', 'Odhlásit'),
    'http://embed.mibbit.com/?server=game.zdechov.net%3A6667&channel=%23wowpreklad&forcePrompt=true&charset=utf-8' => array(LICENCE_ANONYMOUS, 'IRC chat pro překladatele', 'Chat'),
  	$Config['Web']['BaseURL'].'log.php' => array(LICENCE_MODERATOR, 'Log dění (Pouze moderátor)', 'Záznamy'),
	$Config['Web']['BaseURL'].'import/' => array(LICENCE_ADMIN, 'Načtení zdrojových textů do databáze (pouze admin)', 'Import textů'), 
	$Config['Web']['BaseURL'].'img_level.php?nothide' => array(LICENCE_ADMIN, 'Načtení levelů a generování obrázků (pouze admin)', 'Generování lvl'), 
	//'client_files/generate_SQL.php' => array(LICENCE_ADMIN, 'Generování clientských souborů (pouze admin)', 'Generování C.S.'),
    'https://wow.zdechov.net/mysql/' => array(LICENCE_ADMIN, 'phpMyAdmin(pouze admin)', 'Správa databáze'),
  );
  //echo('Jste přihlášen jako: <strong>'.$_SESSION['User'].'</strong> 

	
  echo('<strong>Hlavní menu:</strong>
      <div class="verticalmenu"><ul>');

  foreach($Menu as $MenuTarget => $MenuItem)
  {
	if(isset($MenuItem[3])) $OnClick = ' onclick="'.$MenuItem[3].'"'; else $OnClick = '';
	if(Licence($MenuItem[0])) echo('<li><a class="verticalmenua" title="'.$MenuItem[1].'" href="'.$MenuTarget.'"'.$OnClick.'>'.$MenuItem[2].'</a></li>');
  }    
  echo('</ul></div>');
  
}

function ShowTranslatedMenu()
{
  global $TranslationTree, $Config;
  

echo('<strong>Překladové skupiny:</strong><br /><div id="TranslationMenu">');
    if(Licence(LICENCE_USER)) echo ('<a title="Všechny překlady, které jste přeložil" href="'.$Config['Web']['BaseURL'].'TranslationList.php?user='.$_SESSION['UserID'].'&amp;action=userall">Všechny moje texty</a><br />');
  foreach($TranslationTree as $Group)
  {
    echo('<div id="menuitem-group'.$Group['Id'].'" onmousemove="show(\'group'.$Group['Id'].'\')" onmouseout="hide(\'group'.$Group['Id'].'\')">
      <a href="'.$Config['Web']['BaseURL'].'TranslationList.php?group='.$Group['Id'].'&amp;action=filter">'.$Group['Name'].'</a></div>       
      <div id="group'.$Group['Id'].'" class="hidden-menu-item" onmousemove="show(\'group'.$Group['Id'].'\')" onmouseout="hide(\'group'.$Group['Id'].'\')">');
    echo('&nbsp;<a title="Zde můžete začít překládat" href="'.$Config['Web']['BaseURL'].'TranslationList.php?group='.$Group['Id'].'&amp;action=notran">Nepřeložené</a><br />
      &nbsp;<a title="Přeložené texty, můžete zde hlasovat, nebo opravovat překlady" href="'.$Config['Web']['BaseURL'].'TranslationList.php?group='.$Group['Id'].'&amp;action=Translate">Přeložené</a><br />');
    if(Licence(LICENCE_USER)) 
    {
      echo('&nbsp;<a title="Nedokončené překlady" href="'.$Config['Web']['BaseURL'].'TranslationList.php?group='.$Group['Id'].'&amp;action=mydevelop">Rozepsané</a><br />
        &nbsp;<a title="Všechny překlady, které jste přeložil" href="'.$Config['Web']['BaseURL'].'TranslationList.php?group='.$Group['Id'].'&amp;action=my">Vlastní</a><br />');
    }
    echo('&nbsp;<a title="Sestavit speciální filtr" href="'.$Config['Web']['BaseURL'].'TranslationList.php?group='.$Group['Id'].'&amp;action=filter">Filtr</a><br />');
    echo('</div>');
  }
  echo ('</div>');

}

function ShowOnlineUserList()
{
  global $Database, $Moderators;
  
  echo('Online uživatelé:<br />');
  $ID = $Database->SQLCommand("SELECT user, GM, ID FROM `user` WHERE LastLogin >= Now() - 300");
  while ($Line = mysql_fetch_assoc($ID))
  {
    if(Licence(LICENCE_MODERATOR)) $Name = '<a href="user.php?user='.$Line['ID'].'">'.$Line['user'].'</a>';
    else $Name = $Line['user'];
    if($Line['GM'] <> 0) $TextGM = '<small>('.$Moderators[$Line['GM']].')</small>';
    else $TextGM = '';
    echo('<strong>'.$Name.'</strong>'.$TextGM.'<br />');
  }
}

function ShowPage()
{
  global $Config, $Database, $RSSChannels;	
	
  echo('<?xml version="1.0" encoding="'.$Config['Web']['Charset'].'"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs" lang="cs">
<head>'.
'<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />'.
'<meta name="keywords" content="wow, quest, questy, preklad, mangos, překlad, překládání, přeložený, přeložení, čeština, world of warcraft, open source, free, addon" />'.
'<meta name="description" content="Projekt překládání textů hry World of Warcraft" />'.
'<meta name="robots" content="all" />'.
'<link rel="stylesheet" href="'.$Config['Web']['BaseURL'].'style/style.css" type="text/css" media="all" />'.
'<script type="text/javascript" src="'.$Config['Web']['BaseURL'].'style/global.js"></script>'.
'<link rel="SHORTCUT ICON" href="'.$Config['Web']['BaseURL'].'images/favicon.ico" />');
if(isset($RSSChannels)) 
foreach($RSSChannels as $Channel)
  echo(' <link rel="alternate" title="'.$Channel['Title'].'" href="'.$Channel['URL'].'" type="application/rss+xml" />');
echo('<title>Projekt překládání textů WoW</title>
</head>
<body>
<table id="bannertable"><tr>
  <td id="banner1"></td>
  <td id="banner"><div id="bannertitle">Projekt překládání textů WoW</div></td>
  <td id="banner2"></td></tr>
</table>
');

if(!array_key_exists('UserID', $_SESSION)) $_SESSION['UserID'] = '';
if(!array_key_exists('User', $_SESSION)) $_SESSION['User'] = 'NotRegistred';
if(!array_key_exists('Pass', $_SESSION)) $_SESSION['Pass'] = '';

// Přihlášení
if(array_key_exists('LoginUser', $_POST))
{
  $_SESSION['User'] = $_POST['LoginUser'];
  $_SESSION['Pass'] = $_POST['LoginPass'];

  $Line = mysql_fetch_array($Database->SQLCommand("SELECT * FROM user WHERE LOWER(user) = LOWER('".$_SESSION['User']."') AND pass = sha1('".$_SESSION['Pass']."')"));
  if(!$Line)
  {
     echo('<script> alert("Jméno nebo heslo bylo zadáno špatně!"); </script>');
     $_SESSION['UserID'] = ''; 
     $_SESSION['User'] = '';
     $_SESSION['Pass'] = '';
  } else
  {
    $_SESSION['UserID'] = $Line['ID']; 
    $_SESSION['User'] = $Line['user'];
    WriteLog('Login: '.$_SESSION['User'], 3);
  }
}

//odhlášení
if(array_key_exists('Logout', $_GET))
{
  WriteLog('Odhlášení', 3);
  $_SESSION['User'] = '';
  $_SESSION['Pass'] = '';
  $_SESSION['UserID'] = '';
}

  //záznam o aktivitě
  if(Licence(0)) $Database->SQLCommand("UPDATE user SET LastIP = '".$_SERVER['REMOTE_ADDR']."', LastLogin = now() WHERE ID = ".$_SESSION['UserID']);

  echo ('<table class="page"><tr><td class="menu">');
  ShowMainMenu();
      
  echo('<br />');    
  ShowLoginBox();
  echo('<br />');
  ShowOnlineUserList();
  echo('</td><td id="border-left"></td><td class="content">');
}

$UnitNames = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB');
 
function HumanSize($Value)
{
  global $UnitNames;

  $UnitIndex = 0;
  while($Value > 1024)
  {
    $Value = round($Value / 1024, 3);
    $UnitIndex++;
  }
  return($Value.' '.$UnitNames[$UnitIndex]);
}

function ShowFooter()
{
  global $Database, $Config, $ScriptStartTime;

  $ScriptGenerateDuration = round(GetMicrotime() - $ScriptStartTime, 2);

  echo('</td>');
  echo('<td class="menu2">');
  ShowTranslatedMenu();
  echo('<br />');
  ShowShoutBox();
  echo('</td>');
  echo ('</tr>
    <tr>
    <td colspan="4" class="page-bottom">Autoři: maron, chronos &nbsp; <a href="http://wowpreklad.zdechov.net/trac/wowpreklad/browser/trunk">Zdrojové soubory</a> &nbsp; ');
    echo('<a href="http://counter.cnw.cz/"><img src="http://counter.cnw.cz/monika.cgi?wowpreklad&amp;5&amp;000000&amp;FFFFFF&amp;on" alt="CNW:Counter" /></a>
    <script type="text/javascript">
    <!--
    document.write("<a href=\"http://counter.cnw.cz\" target=\"_parent\"><img src=\"http://counter.cnw.cz/trackit.cgi?wowpreklad&t3&" + escape(top.document.referrer) + "\" alt=\"CNW:Tracker\" border=\"0\" width=\"1\" height=\"1\"><\/a>");
    // -->
    </script>');

	echo('</td></tr>');
  if($Config['Web']['ShowRuntimeInfo'] == true) echo('<tr><td colspan="3" style="text-align: center;">Doba generování: '.$ScriptGenerateDuration.' s / '.ini_get('max_execution_time').' s &nbsp;&nbsp; Použitá paměť: '.HumanSize(memory_get_peak_usage(FALSE)).' / '.ini_get('memory_limit').'B</td></tr>');
echo('</table>
  </body>
</html>');
  $Database->Disconnect();
}

?>
