<?php

class Html
{
  private $values;
  private $db;

  public function __construct($values, $db)
  {
    $this->db = &$db;
    // default config
    $this->html_cfg = array
    (
/*      "charset" => "utf-8",
      "author" => "Zork",
      "copyright" => "(C) 2008 Heroes of Fantasy, All rights reserved.",
      "favicon" => "imgs/fav",
      "title" => "Heroes of Fantasy",
      "description" => "Heroes of Fantasy free WoW server běžící na programu MaNGOS",
      "keywords" => "wowserver, world of warcraft, free, wow, server, hof, heroes of fantasy, zdechov, mangos"
*/    );

    if((count($values) > 1) or ($values[0] != 0) and ($values[0] != 0))
    {
      $result_cfg = array_merge ($this->html_cfg, $values);
      foreach($result_cfg as $name => $value)
        $this->{$name} = $value;
    } else
    {
      foreach($this->html_cfg as $name => $value)
        $this->{$name} = $value;
    }
  }

  private function CfgChck($value)
  {
    if(isset($this->{$value}))
    {
      return($this->{$value});
    } else
    {
      return('Undefined');
    }
  }

  public function Start()
  {
    global $Config;
    
    $code = '<?xml version="1.0" encoding="utf-8"?>'.
    '<!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='.$Config['Web']['Charset'].'" />'.
    '<meta http-equiv="content-Language" content="cs" />'.
    '<meta name="robots" content="index,follow" />'.
    '<meta name="googlebot" content="index,follow,snippet,archive" />'.
    '<link href="imgs/fav.ico" rel="shortcut icon" />'.
    '<title>'.$Config['Web']['Title'].'</title>'.
    '<meta name="description" content="'.$Config['Web']['Description'].'" />'.
    '<meta name="keywords" content="'.$Config['Web']['Keywords'].'" />'.
    '<link rel="stylesheet" type="text/css" href="styles/style.css" />'.
    '<link href="inc/teamspeakdisplay/teamspeakdisplay.css" rel="stylesheet" type="text/css" />'.
    '<link rel="alternate" title="Články" href="rss.php?category=articles" type="application/rss+xml" />'.
    '<link rel="alternate" title="Finanční přispěvky" href="rss.php?category=finance" type="application/rss+xml" />'.
    '<script type="text/javascript" src="styles/global.js"></script>'.
    '<!--[if IE]><link rel="stylesheet" type="text/css" href="styles/ie.css" /><![endif]-->'.
    '</head>'.
    '<body>';
    $this->TimerStart();
    echo($code);
  }

  private function TimerStart()
  {
    $time_start = microtime(); 
    $time_start = explode(' ', $time_start); 
    $time_start = $time_start[1] + $time_start[0]; 
    $this->time_start = $time_start;
  }

  private function TimerStop()
  {
    $round = 100000;
    $time_start = $this->time_start;
    $time_stop = microtime();
    $time_stop = explode(' ', $time_stop); 
    $time_stop = $time_stop[1] + $time_stop[0]; 
    $deltatime = $time_stop - $time_start; 
    $deltatime = (round(($deltatime * $round))) / $round; 
    return($deltatime);
  } 
  
  public function Stop()
  {
    $time = $this->TimerStop();
    $code = '<!-- Vygenerováno za : ".$time." s -->'.
    '</body></html>';
    echo($code);
  }

  public function Pager($get, $dir_pages, $def)
  {
    $anti_ref = array
    (
      'server' => 5,
      'events' => 2,
      'aukce' => 2,
      'gmlog' => 300,
      'online' => 2,
    );
    $site_show = $dir_pages.'/'.$get.'.php';
    if(isset($anti_ref[$get]))
    {
      if(!$this->Antirefresh($anti_ref[$get], $get))
      {          
        if(($site_show != '') and is_readable($site_show)) return($site_show);
          else return('./'.$dir_pages.'/'.$def.'.php');
      } else
      {
        $this->RefreshMsg($this -> Antirefresh($anti_ref[$get], $get), $get);
        return($dir_pages.'/free.php');
      }
    } else
    {
      if($site_show != '' AND is_readable($site_show)) return($site_show);
        else return($dir_pages.'/'.$def.'.php');
    }
  }

  public function Antirefresh($value, $page)
  {
    $hash = md5($page);
    if(isset($_SESSION['rf'.$hash.'rsh']))
    {
      $refresh = $_SESSION['rf'.$hash.'rsh'] + $value;
      if(time() < $refresh)
      {
        $dif = $refresh - time();
        return($dif);
      }
    }
    $_SESSION['rf'.$hash.'rsh'] = time();
  }

  public function RefreshMsg($dif, $get)
  {
    if($dif != 0)
    {
      if($dif > 60)
      {
        $dif = round($dif / 60).' min';
        $refresh = 30;
      } else
      {
        $dif = $dif.' s';
        $refresh = 2;
      }
      echo('<center><h3>Refresh omezení</h3></center>
          Stránka nepovoluje tak krátký čas pro refresh (znovunačtení).<br /> 
          Další načtení stránky <b>?page='.$get.'</b> bude možné za <b>'.$dif.'</b>.<br />
          Pokud tento čas vyčkáte budete přesměrován automaticky !
         <meta http-equiv="refresh" content="'.$refresh.';url=">');
    }
  }

  public function PrgStart()
  {
    echo('<table align="center" width="346" height="417" border="0" style="background-image: url(imgs/web/prg.gif);">
          <tr height="80">
            <td width="20"></td>
            <td align="center">
            <h3>Správa účtu</h3>
            </td>
            <td width="20"></td>
          </tr>
          <tr>
            <td width="20"></td>
            <td align="center" valign="top">');  
  }

  public function PrgStop()
  {
    echo('</td>
        <td width="20"></td>
      </tr>
      <tr height="30">
        <td width="20"></td>
        <td></td>
        <td width="20"></td>
      </tr>
      </table>');
  }

  public function ShowNews($kategorie)
  {
 	  include('inc/db.php');
		global $db_webu;
		
    echo('<table class="NewsCategoryTable">'.
      '<tr>'.
      '<td><a href="?page=servernews">Server novinky</a></td>'.
      '<td><a href="?page=gmnews">GM novinky</a></td>'.
      '<td><a href="?page=gamenews">In-game novinky</a></td>'.
      '<td><a href="?page=articles">Články</a></td>'.
      '<td><a href="?page=main">Vše</a></td>'.
      '</tr></table><br />');
    $this->QuickNew();

    if($kategorie == "all")
    {
      $where = " ";
    } else
    {
      $where = "WHERE `category` = '".$kategorie."'";
    } 
		
    mysql_connect($db_server, $db_user, $db_pass);
    mysql_select_db($db_webu);
    $limit = 5;
    
    $pocet = mysql_num_rows(mysql_query("SELECT id FROM `articles`".$where."ORDER BY `date` DESC;"));
    $offset = ($_GET["offset"] ? $_GET["offset"] : $pocet);

    if($pocet != 0)
    {
		  mysql_query('SET NAMES utf8');
      $result = mysql_query("SELECT * FROM articles $where ORDER BY date DESC, id DESC LIMIT $limit OFFSET " . ($pocet - $offset));
      while($row = mysql_fetch_array($result))
      {
        $row['text'] = str_replace('<br>', '<br />', $row['text']);
        switch ($row["category"]) 
        {
          case 1:
            $cat = "Server novinky";
            $link = "servernews";
            break;
          case 2:
            $cat = "GM novinky";
            $link = "gmnews";
            break;
          case 3:
            $cat = "In-game novinky";
            $link = "gamenews";
            break;
          case 4:
            $cat = "Články";
            $link = "articles";
            break;
        }
        $date_arr = explode(' ', $row['date']);
        $date = explode('-', $date_arr[0]);
        $time = mktime(0, 0, 0, $date[1], $date[2], $date[0]);
        $human_date = date ("d.m.Y", $time);
        echo('<table class="NewsQuickInfoTable">'.
          '<tr>'.
          '<td><strong>'.$row["title"].'</strong></td>'.
          '<td style="width: 100px">'.$human_date.'</td>'.
          '</tr>'.
          '<tr>'.
          '<td colspan="2">'.$row["text"].'</td>'.
          '</tr>'.
          '<tr>'.
          '<td>Sekce : <a href="?page='.$link.'">'.$cat.'</a></td>'.
          '<td>'.$row['autor'].'</td>'.
          '</tr>'.
          '</table>'.
          '<br />');
      }
      echo('<p style="text-align: center">');
      if($offset < $pocet)
      {
        echo("<a href='$_SERVER[PHP_SELF]" . ($offset + $limit < $pocet ? "?offset=" . ($offset + $limit) : "") . "'><-- novější</a>\n &nbsp;");
      }
      if($offset > $limit)
      {
        echo('&nbsp;<a href="'.$_SERVER['PHP_SELF'].'?offset='.($offset - $limit).'">starší --></a>');
      }
      echo('</p>');
    } else
    {
      echo('<h3 style="text-align: center">Žádný článek v této kategorii !</h3>');
    }
  }

  function QuickNew()
  {
    global $server, $Config;

    if(isset($_COOKIE['hof-quick']) AND $_COOKIE['hof-quick'] == 'no')
    {
    } else
    {
      echo('<table class="NewsQuickInfoTable">'.
        '<tr>'.
        '<td><strong>Rychlý přehled</strong></td>'.
        '<td style="width: 100px;">'.date('d.m.Y', time()).'</td>'.
        '</tr>'.
        '<tr>'.
        '<td colspan="2">'.
        '<strong>Server : </strong>'.$server->ServerImg($server->ServerOnline($Config['Web']['WoWServerAddr'], 8085)).'<b> (<a href="?page=online">'.$server->MangosOnline().' hráčů</a>) | Patch '.$Config['Mangos']['ClientVersion'].' | <a href="'.$Config['Web']['BaseURL'].'/pages/realmlist.wtf">Realmlist</a> | Více info <a href="?page=server">zde</a></b>'.
        '</td></tr></table><br />');
    }
  }  
}

?>
