<?php

$Version = '0.2';
//include_once('error.php');
include_once('config.php');
include_once('Packages/Common/Common.php');
$Database = new Database();
$Database->Connect($Options['DB_Host'], $Options['DB_User'], $Options['DB_Password'], $Options['DB_Database']);
$Database->charset('utf8');
$Database->Prefix = $DB_Prefix;

function IconedLink($Link, $Text)
{
  global $Options;

  $Extension = strtolower(substr($Link, strrpos($Link, '.') + 1));
  $IconFile = 'images/icons/'.$Extension.'.gif';
  //echo($IconFile."<br>\n");
  if(!file_exists($IconFile))
  {
    $Extension = 'blank';
    $IconFile = 'images/icons/'.$Extension.'.gif';
  }
  $Icon = '<img src="'.$Options['RootPath'].$IconFile.'" alt="'.$Extension.'"> ';
  return($Icon.'<a href="'.$Link.'">'.$Text.'</a>');
}

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

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

function ShowHeader($Path, $Title)
{
  global $Time_Start, $refresh, $Charset, $Options, $Output;

  $Time_Start = GetMicrotime();// Zjisti počáteční čas
  $Cesty = array(
  );
  $Output = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>
  <meta http-equiv="Content-Language" content="cs">
  <meta http-equiv="Content-Type" content="text/html; charset=';
  if(isset($Charset)) $Output .= 'windows-1250';
  else $Output .= 'iso-8859-2';
  $Output .= '">';
  if (isset($refresh)) $Output .= '<meta http-equiv="Refresh" content="'.$refresh."\">\n";
  //  <link href="style.css" rel="stylesheet" type="text/css">
  $Output .= '<title>'.$Options['Title'].' - '.$Title.'</title>'.
  '<link rel="stylesheet" type="text/css" href="'.$Options['RootPath'].'style.css">'.
  '<link rel="alternate" title="RSS aktuality SPS" href="'.$Options['RootPath'].'rss.php" type="application/rss+xml">'.
  '</head><body>'.
  '<div id="PageTitle">'.$Title.'</div>'.
  '<div id="Navigation"><strong>Navigace &gt;&gt; </strong>';
  $I = 1;
  foreach($Path as $Index => $Item)
  {
    $Item = substr($Options['RootPath'],0,-1).$Item;
    $Output .= '<a href="'.$Item.'">'.$Index.'</a>';
    if(count($Path) > $I) $Output .= ' &gt; ';
    $I++;
  }
  $Output .= '</div>';
}

function ShowFooter()
{
  global $Time_Start, $Version, $Output;
  $Time = floor((GetMicrotime() - $Time_Start)*100)/100;
  $Output .= '<div id="AdminInfo">| Web mistr: Jiří Hajda | e-mail: robie@centrum.cz | ICQ: 277158770 | Vygenerováno za '.$Time.' s | Verze: '.$Version.' | Naposledy aktualizováno: '.date('j.n.Y',filemtime($_SERVER['SCRIPT_FILENAME'])).' |</div>';
  //ShowArray($GLOBALS);
  $Output .= '</body></html>';
  echo($Output);
  //echo(FormatOutput($Output));
}


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

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()
{
  $IP = $_SERVER['REMOTE_ADDR'];
  if($IP == '127.0.0.1') $IP = $_SERVER['HTTP_X_FORWARDED_FOR'];
  return($IP);
}

// 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);
  }

