<?php

$TimeStart = time();

// SQL injection hack protection
foreach($_POST as $Index => $Item) $_POST[$Index] = addslashes($Item);
foreach($_GET as $Index => $Item) $_GET[$Index] = addslashes($Item);

session_start();
include('config.php');
include('database.php');
//$Database = new Database($Config['Database']['Host'], $Config['Database']['User'], $Config['Database']['Password'], $Config['Database']['Database']);
//$Database->Prefix = $Config['Database']['Prefix'];
//$Database->charset($Config['Database']['Charset']);

$MonthNames = array('', 'Leden', 'Únor', 'Březen', 'Duben', 'Květen', 'Červen', 'Červenec', 'Srpen', 'Září', 'Říjen', 'Listopad', 'Prosinec');

function GetMicrotime()
{
  list($Usec, $Sec) = explode(" ", microtime());
  return ((float)$Usec + (float)$Sec);
}

function ShowArray($Pole)
{
  echo('<pre style="font-size: 8pt;">');
  print_r($Pole);
  echo('</pre>');
}

function TimeToMysqlDateTime($Time)
{
  return(date('Y-m-d H:i:s', $Time));
}

function MysqlDateTimeToTime($Time)
{
  $Parts = explode(' ', $Time);
  $DateParts = explode('-', $Parts[0]);
  $TimeParts = explode(':', $Parts[1]);
  $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[2], $DateParts[0]);
  return($Result);
}

function MysqlDateToTime($Time)
{
  return(MysqlDateTimeToTime($Time.' 0:0:0'));
}

function HumanDate($Time)
{
  $Date = explode(' ', $Time);
  $Parts = explode('-', $Date[0]);
  if($Date != '0000-00-00') return(($Parts[2]*1).'.'.($Parts[1]*1).'.'.$Parts[0]);
  else return('&nbsp;');
}

// Zobrazení číselný seznamu stránek
function PagesList($URL,$Page,$TotalCount,$CountPerPage)
{
  $Count = ceil($TotalCount/$CountPerPage);
  $Around = 10;
  $Result = '';
  if($Count>1)
  {
    if($Page>0)
    {
      $Result.= '<a href="'.$URL.'0">&lt;&lt;</a> ';
      $Result.= '<a href="'.$URL.($Page-1).'">&lt;</a> ';
    }
    $PagesMax = $Count-1;
    $PagesMin = 0;
    if($PagesMax>($Page+$Around)) $PagesMax = $Page+$Around;
    if($PagesMin<($Page-$Around))
    {
      $Result.= ' .. ';
      $PagesMin = $Page-$Around;
    }
    for($i=$PagesMin;$i<=$PagesMax;$i++)
    {
      if($i==$Page) $Result.= '<strong>';
      $Result.= '<a href="'.$URL.$i.'">'.($i+1).'</a> ';
      if($i==$Page) $Result.= '</strong>';
    } 
    if($PagesMax<($Count-1)) $Result .= ' .. ';
    if($Page<($Count-1)) 
    {
      $Result.= '<a href="'.$URL.($Page+1).'">&gt;</a> ';
      $Result.= '<a href="'.$URL.($Count-1).'">&gt;&gt;</a>';
    }
  }
  return($Result);
}

function GetRemoteAddress()
{
  if(array_key_exists('HTTP_X_FORWARDED_FOR',$_SERVER)) $IP = $_SERVER['HTTP_X_FORWARDED_FOR'] ;
  else if(array_key_exists('REMOTE_ADDR', $_SERVER)) $IP = $_SERVER['REMOTE_ADDR'];
  else $IP = '0.0.0.0';
  return($IP);
}

function IsInternetAddr()
{
  $RemoteAddr = GetRemoteAddress();
  $RemoteAddr = explode('.', $RemoteAddr);
  return(!(($RemoteAddr[0] == 192) and ($RemoteAddr[1] == 168)));
}

function ShowPage($Title, $Content)
{
  global $Config, $TimeStart;

  $Output = '<?xml version="1.0" encoding="'.$Config['Web']['Charset'].'"?>'."\n".
  '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'."\n".
  '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs" lang="cs">'."\n".
  '<head><link rel="stylesheet" href="'.$Config['Web']['RootFolder'].'style.css" type="text/css" media="all" />'."\n".
  '<script type="text/javascript" src="'.$Config['Web']['RootFolder'].'global.js"></script>'."\n".
  '<title>'.$Config['Web']['Title'].' - '.$Title.'</title>'."\n".
  '</head><body>'."\n";
  //'<div id="Title">'.$Title.'</div>';
  $Time = floor((GetMicrotime() - $TimeStart) * 100) / 100;
  $Output .= $Content.'<div id="Footer">
  <i>| Správa webu: Jiří Hajda | e-mail: robie@centrum.cz | Vygenerováno za '.$Time.' s | Použitá paměť:  '.HumanSize(memory_get_peak_usage(FALSE)).' |</i>
  </div>';
  //ShowArray($GLOBALS);
  $Output .= '</body></html>';
  echo($Output);
}

$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]);
}

?>
