<?php

define('PAGE_NOT_FOUND', 'Stránka nenalezena');

class Page extends Module
{
  var $TimeStart;
  var $FormatHTML = false;
  var $ShowRuntimeInfo = false;
  var $ClearPage = false;
  var $PathTree = array('Rozcestník',
    'index.php' => '',
    'missing.php' => '',
    'manualy.php' => 'Manuály',
    'is' => array('',
      'index.php' => 'Správa dat',
      'topologie.php' => 'Topologie sítě',
    ),
    'network.php' => 'Technické informace',
    'map' => array('',
      'index.php' => 'Mapa sítě',
    ),
    'tv' => array('',
      'index.php' => 'Síťová televize',
    ),
    'network' => array('',
      'administration.php' => 'Administrace sítě',
      'dostupnost.php' => 'Dostupnost zařízení',
      'restart.php' => 'Restart služeb',
      'user_hosts.php' => 'Registrované počítače',
    ),
    'nabidky_pripojeni.php' => 'Nabídky připojení',
    'telseznam.php' => 'Telefonní seznam',
    'merak.php' => 'Měřič spotřeby',
    'tkr.php' => 'Kanály kabelové televize',
    'LogShow.php' => 'Záznamy operací',
    'share' => array('Prohledat sdílení',
      'index.php' => '',
      'firefox.php' => 'Integrace do prohlížeče',
    ),
    'hostlist.php' => 'Seznam počítačů',
    'userlist.php' => 'Seznam uživatelů',
    'farnost' => array('',
      'index.php' => 'Rozpis mší',
    ),
    'finance' => array('Finance',
      'index.php' => '',
      'spotreba.php' => 'Spotřeba',
      'zivnost.php' => 'Daňová evidence',
      'prepocet.php' => 'Přepočet financí',
      'changes.php' => 'Změny',
      'tarify.php' => 'Tarify',
      'clenove.php' => 'Seznam členů',
      'zarizeni.php' => 'Zařízení',
      'strom.php' => 'Strom zařízení',
      'srovnani.php' => 'Porovnání tarifů s konkurencí',
      'bills_manage.php' => 'Faktury',
      'monthly_overall.php' => 'Měsíční přehledy',
      'manage.php' => 'Správa financí',
      'faktury' => array('',
        'index.php' => 'Faktury za internet',
      ),
    ),
    'statistic' => array('',
      'trafficview.php' => 'Vytížení internetu',
      'index.php' => 'Statistika',
      'trafficview2.php' => 'Statistika',
      'bandwidth.php' => 'Zatížení internetu',
      'bandwidth_full.php' => 'Zatížení internetu všech počítačů',
    ),
    'fotbal.php' => 'Fotbal',
    'webcam' => array('',
       'index.php' => 'Web kamera',
    ),
    'navody.php' => 'Návody',
    'ukoly.php' => 'Plánované projekty',
    'otevreno.php' => 'Otvírací doby',
    'aktuality' => array('',
      'index.php' => 'Aktuality',
      'subscription.php' => 'RSS aktuality',
    ),
    'sluzby.php' => 'Služby',
    'history.php' => 'Historie',
    'docs' => array('',
      'zapisy' => array('',
        'zapisy.php' => 'Zápisy ze schůzí',
      ),
    ),
    'jidelna' => array('',
      'index.php' => 'Jídelníček',
      'menuedit.php' => 'Editace jídelníčku',
    ),
    'backup' => array('',
      'index.php' => 'Nastavení zálohování',
    ),
    'mapa.php' => 'Mapa webu',
  );
  
  function __construct()
  {
    global $Config;
    
    $this->FormatHTML = $Config['Web']['FormatHTML'];
    $this->ShowRuntimeInfo = $Config['Web']['ShowRuntimeInfo'];
  }

  function SystemMessage($Title, $Text)
  { 
    return('<table align="center"><tr><td><div class="SystemMessage"><h3>'.$Title.'</h3><div>'.$Text.'</div></div></td></tr></table>');
    //ShowFooter();
    //die();
  }

  function ShowHeader($Title, $Path)
  {
    $ScriptName = $_SERVER['SCRIPT_NAME'];
    while(strpos($ScriptName, '//') !== false)
      $ScriptName = str_replace('//', '/', $ScriptName);
    $PathTreePath = '/';
    $PathTreeItem = $this->PathTree;
    $Navigation = '<a href="'.$this->System->Config['Web']['RootFolder'].$PathTreePath.'">'.$PathTreeItem[0].'</a> &gt; ';
    $ScriptName = substr($ScriptName, strlen($this->System->Config['Web']['RootFolder']));
    $ScriptNameParts = explode('/', $ScriptName);
    array_shift($ScriptNameParts);
    foreach($ScriptNameParts as $ScriptNamePart)
    { 
      if(array_key_exists($ScriptNamePart, $PathTreeItem))
      {
        if(is_array($PathTreeItem[$ScriptNamePart])) 
        {
          $PathTreeItem = $PathTreeItem[$ScriptNamePart];
          $PathTreePath .= $ScriptNamePart.'/';
          if($PathTreeItem[0] != '')
            $Navigation .= '<a href="'.$this->System->Config['Web']['RootFolder'].$PathTreePath.'">'.$PathTreeItem[0].'</a> &gt; ';
        } else 
        {
          if($PathTreeItem[$ScriptNamePart] != '')
            $Navigation .= '<a href="'.$this->System->Config['Web']['RootFolder'].$PathTreePath.$ScriptNamePart.'">'.$PathTreeItem[$ScriptNamePart].'</a> &gt; ';
        }
      } 
    } 
    $Navigation = substr($Navigation, 0, -6);

    $BodyParam = '';
    if(isset($this->Load)) $BodyParam .= ' onload="'.$this->Load.'"';
    if(isset($this->Unload)) $BodyParam .= ' onunload="'.$this->Unload.'"';
    $Output = '<?xml version="1.0" encoding="'.$this->System->Config['Web']['Charset'].'"?>'."\n".
    '<!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><link rel="stylesheet" href="'.$this->System->Link('/style/').$this->System->Config['Web']['Style'].'/style.css" type="text/css" media="all" />'.
    '<meta http-equiv="content-type" content="application/xhtml+xml; charset='.$this->System->Config['Web']['Charset'].'" />'.
    '<script type="text/javascript" src="'.$this->System->Link('/style/').$this->System->Config['Web']['Style'].'/global.js"></script>'.
    '<title>'.$this->System->Config['Web']['Title'].' - '.$Path.'</title>
    </head><body'.$BodyParam.'>
    <div id="Title">'.$Title.'</div>
    <div class="Navigation"><span class="MenuItem"><strong>Navigace :: </strong> '.$Navigation.'</span><div class="MenuItem2">';
    if($this->System->Config['Web']['UserSupport'] == 1)
    {
      if($this->System->Modules['User']->User['Id'] == null)
        $Output .= '<a href="'.$this->System->Link('/?Action=LoginForm').'">Přihlášení</a> <a href="'.$this->System->Link('/?Action=UserRegister').'">Registrace</a>'; 
        else $Output .= $this->System->Modules['User']->User['Name'].' <a href="'.$this->System->Link('/?Action=Logout').'">Odhlásit</a>';
   } else $Output .= '&nbsp;';
// <a href="'.$this->System->Config['Web']['RootFolder'].'/?Action=UserOptions">Nastavení</a>'; 
    $Output .= '</div></div>';
    return($Output);
  }

  function ShowFooter()
  {
  global $ScriptTimeStart;
    $Time = round(GetMicrotime() - $ScriptTimeStart, 2);
    $Output = '<div id="Footer">
   <i>| Správa webu: '.$this->System->Config['Web']['Admin'].' | e-mail: '.$this->System->Config['Web']['AdminEmail'].' |';
    if($this->ShowRuntimeInfo == true) $Output .= ' Doba generování: '.$Time.' s / '.ini_get('max_execution_time').' s | Použitá paměť: '.HumanSize(memory_get_peak_usage(FALSE)).' / '.ini_get('memory_limit').'B |';
  $Output .= '</i></div></body></html>';
    return($Output); 
  }

  function GetOutput()
  { 
    $Output = $this->Show();
    if($this->ClearPage == false) 
    {
      $Output = $this->ShowHeader($this->FullTitle, $this->ShortTitle).$Output;
      $Output .= $this->ShowFooter();
      if($this->FormatHTML == true) $Output = $this->FormatOutput($Output);
    }
    echo($Output);
  }

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

?>
