<?php

session_start();
include('config.php');
include('database.php');
include('error.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']);

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

$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 ToVpnIp($Host)
{ 
  if($Host['external_ip'] == '') 
  {
    $Parts = explode('.', $Host['IP']);
    return('10.0.'.$Parts[2].'.'.$Parts[3]);
  } else 
  {
    return($Host['external_ip']);
  }
}

function HumanDate($Date)
{
  $Parts = explode('-',$Date);
  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 $IP = $_SERVER['REMOTE_ADDR'];
  //if($IP == '127.0.0.1') 
  return($IP);
}

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

// Funkce formatovani vystupu
function FormatOutput($s)
{
    $out = '';
    $nn = 0;
    $n = 0;
    while($s!='')
    {
      $start = strpos($s,'<');
  	  $end = strpos($s,'>');
    	if($start != 0) 
    	{
	      $end = $start-1;
	      $start = 0;
	    }
	    $line = trim(substr($s,$start,$end+1));
	    if(strlen($line)>0) 
	    if($line[0] == '<')
	    {
  	    if($s[$start+1] == '/') 
	      {
  	      $n = $n - 2;
		      $nn = $n;
	      } else 
	      {
     	    if(strpos($line,' ')) $cmd = substr($line,1,strpos($line,' ')-1);
	        else $cmd = substr($line,1,strlen($line)-2);
          //echo('['.$cmd.']');
		      if(strpos($s,'</'.$cmd.'>')) $n = $n + 2;
	      }   
	    }// else $line = '['.$line.']';
      //if($line != '') echo(htmlspecialchars(str_repeat(' ',$nn).$line."\n"));
	    if($line != '') $out .= (str_repeat(' ',$nn).$line."\n");
	    $s = substr($s,$end+1,strlen($s));
	    $nn = $n;
    }
    return($out);
}
 
?>
