<?php
class Index {

  private $db;

  public function __construct() {
    $this->db = new Db_layer();
  }
  
  //---[CZ]: ochrana proti spamu
  public function spam_protect() {
    if ($_POST["robot"]=="") {
    return 1;
    } else {
    return 0;
    }
  }
  
  //---[CZ]: vypsání formátovacích tlačítek
  public function print_format_buttons() {
    echo "<input type=\"button\" class=\"button\" onclick=\"format('[b][/b]'); return false;\" value=\"strong\" />
<input type=\"button\" class=\"button\" onclick=\"format('[i][/i]'); return false;\" value=\"em\" />
<input type=\"button\" class=\"button\" onclick=\"format('[u][/u]'); return false;\" value=\"underline\" />
<input type=\"button\" class=\"button\" onclick=\"format('[code][/code]'); return false;\" value=\"code\" />
<input type=\"button\" class=\"button\" onclick=\"var url=prompt('URL:', 'http://'); var text=prompt('Text:', ''); format('[a]' + url + '[*]' + text + '[/a]'); return false;\" value=\"a href\" />";
  }

  //---[CZ]: kontrola banu
  public function check_ban() {
    $sql=$this->db->query("SELECT COUNT(*) AS c FROM kboard_banns WHERE ip='".$_SERVER["REMOTE_ADDR"]."'");
    $sql=$this->db->fetch_array($sql);
    return $sql["c"];
  }
  
    //---[CZ]: převedení POST na GET při vyhledávání
  public function search_to_get() {
    header("Location: ./index.php?action=search&w=".rawurlencode(strip_tags($_POST["search"])));
  }
  
  //---[CZ]: detaily topicu
  public function topic_details($column) {
    if (isset($_GET["topic"])) { $topic=$_GET["topic"]; }
    if (isset($_GET["t_id"])) { $topic=$_GET["t_id"]; }
    $sql=$this->db->query("SELECT $column FROM kboard_topics WHERE id=".intval($topic));
    $sql=$this->db->fetch_array($sql);
    return $this->db->unescape($sql["$column"]);
  }
  
  //---[CZ]: detaily fóra
  public function forum_details($column) {
    $sql=$this->db->query("SELECT $column FROM kboard_forums WHERE id=".intval($_GET["forum"]));
    $sql=$this->db->fetch_array($sql);
    return $this->db->unescape($sql["$column"]);
  }
  
  //---[CZ]: detaily příspěvku
  public function post_details($column) {
    $sql=$this->db->query("SELECT $column FROM kboard_posts WHERE id=".intval($_GET["p_id"]));
    $sql=$this->db->fetch_array($sql);
    return $this->db->unescape($sql["$column"]);
  }
  
  //---[CZ]: detaily fóra podle topicu
  public function topic_forum_details($column) {
    if (isset($_GET["topic"])) { $topic=$_GET["topic"]; }
    if (isset($_GET["t_id"])) { $topic=$_GET["t_id"]; }
    $sql=$this->db->query("SELECT forum FROM kboard_topics WHERE id=".intval($topic));
    $sql=$this->db->fetch_array($sql);
    $sql=$this->db->query("SELECT $column FROM kboard_forums WHERE id=".intval($sql["forum"]));
    $sql=$this->db->fetch_array($sql);
    return $this->db->unescape($sql["$column"]);
  }
  
  //---[CZ]: aktualizace zobrazení topicu
  public function update_views() {
    $views=$this->topic_details("views")+1;
    $update=$this->db->query("UPDATE kboard_topics SET views=$views WHERE id=".intval($_GET["topic"]));
    setcookie("viewed_topic_".$_GET["topic"], "1");
  }
  
  //---[CZ]: zamknutí/odemknutí topicu
  public function topic_lock() {
    $sql=$this->db->query("UPDATE kboard_topics SET locked=".intval($_GET["status"])." WHERE id=".intval($_GET["t_id"]));
    header("Location: ".$_SERVER["HTTP_REFERER"]."&mt=1");
  }
  
  //---[CZ]: změna statusu (důležitý/normální)
  public function topic_status() {
    $sql=$this->db->query("UPDATE kboard_topics SET status=".intval($_GET["status"])." WHERE id=".intval($_GET["t_id"]));
    header("Location: ".$_SERVER["HTTP_REFERER"]."&mt=1");
  }
  
  //---[CZ]: registrace nového uživatele
  public function registration() {
    if ($this->spam_protect()==1) {
    $check=$this->db->query("SELECT id FROM ".TABLE_USERS." WHERE login='".$this->db->escape($_POST["login"])."'");
    $check=$this->db->fetch_array($check);

    if ($check["id"]=="") {

    if ($_POST["login"]!="" AND preg_match("/^[0-9a-zA-Z]{3,128}$/", $_POST["login"]) AND $_POST["password"]!="") {

    $sql=$this->db->query("INSERT INTO ".TABLE_USERS." (login, password, regdate, mail_messages) values ('".$this->db->escape($_POST["login"])."', '".SHA1($_POST["password"])."', ".time().", 1)");

    header("Location: ./index.php?mt=1");

    } else { header("Location: ./index.php?mt=0"); }

    } else { header("Location: ./index.php?mt=0"); }
    }
  }
  
  //---[CZ]: detaily profilu
  public function profile_details($column) {
    if (TABLE_USERS=="kcms_users") { $sign_if=""; }
    if (TABLE_USERS=="kboard_users") { $sign_if=", sign"; }
    
    $sql=$this->db->query("SELECT $column$sign_if FROM ".TABLE_USERS." WHERE id=".intval($_GET["profile"]));
    $sql=$this->db->fetch_array($sql);
    return $this->db->unescape($sql["$column"]);
  }
  
  //---[CZ]: nový příspěvek
  public function new_post($paging_number) {
    if ($_POST["text"]!="" AND $this->spam_protect()==1) {

    if (isset($_SESSION["user_id"])) { $author=$_SESSION["user_id"]; } else { $author=0; }

    $text=htmlspecialchars($_POST["text"], ENT_NOQUOTES);
    $text=str_replace("\r\n", "<br />", $text);

    $insert=$this->db->query("INSERT INTO kboard_posts (author, text, timestamp, topic, forum, ip) values (".intval($author).", '".$this->db->escape($text)."', ".time().", ".intval($_GET["t_id"]).", ".intval($_GET["f_id"]).", '".$_SERVER["REMOTE_ADDR"]."')");

    $last_id=$this->db->query("SELECT id, timestamp FROM kboard_posts ORDER BY id DESC");
    $last_id=$this->db->fetch_array($last_id);

    $update=$this->db->query("UPDATE kboard_topics SET lastpost_id=".$last_id["id"].", lastpost_timestamp=".$last_id["timestamp"]." WHERE id=".$_GET["t_id"]);

    $select=$this->db->query("SELECT COUNT(*) AS c FROM kboard_posts WHERE topic=".intval($_GET["t_id"]));
    $select=$this->db->fetch_array($select);

    $pages=ceil($select["c"]/$paging_number);

    header("Location: ./index.php?topic=".$_GET["t_id"]."&page=$pages&mt=1#".$last_id["id"]);

    } else { header("Location: ./index.php?topic=".$_GET["t_id"]."&mt=0"); }
  }
  
  //---[CZ]: zjištění autora postu/topicu pro ověření práv/autorství
  public function id_info() {
    if (isset($_GET["t_id"])) {
    $id_info=$this->db->query("SELECT author FROM kboard_topics WHERE id=".intval($_GET["t_id"]));
    $id_info=$this->db->fetch_array($id_info);
    }
    if (isset($_GET["p_id"])) {
    $id_info=$this->db->query("SELECT author FROM kboard_posts WHERE id=".intval($_GET["p_id"]));
    $id_info=$this->db->fetch_array($id_info);
    }
    if (isset($_GET["t_id"]) OR isset($_GET["p_id"])) { return $id_info["author"]; }
  }
  
  //---[CZ]: editace příspěvku
  public function edit_post($paging_number) {
    if ($_POST["text"]!="") {

    $text=htmlspecialchars($_POST["text"], ENT_NOQUOTES);
    $text=str_replace("\r\n", "<br />", $text);

    $update=$this->db->query("UPDATE kboard_posts SET text='".$this->db->escape($text)."' WHERE id=".intval($_GET["p_id"]));

    $select=$this->db->query("SELECT COUNT(*) AS c FROM kboard_posts WHERE topic=".intval($_GET["t_id"]));
    $select=$this->db->fetch_array($select);

    $pages=ceil($select["c"]/$paging_number);

    header("Location: ./index.php?topic=".$_GET["t_id"]."&page=$pages&mt=1#".$_GET["p_id"]);

    } else { header("Location: ./index.php?action=edit_post&p_id=".$_GET["p_id"]."&mt=0"); }
  }
  
  //---[CZ]: editace topicu
  public function edit_topic() {
    if ($_POST["name"]!="") {
    $update=$this->db->query("UPDATE kboard_topics SET name='".$this->db->escape(htmlspecialchars($_POST["name"], ENT_NOQUOTES))."' WHERE id=".intval($_GET["t_id"]));
    header("Location: ./index.php?topic=".$_GET["t_id"]."&mt=1");
    } else { header("Location: ./index.php?action=edit_topic&t_id=".$_GET["t_id"]."&mt=0"); }
  }
  
  //---[CZ]: smazání příspěvku
  public function delete_post() {
    $topic_id=$this->db->query("SELECT topic FROM kboard_posts WHERE id=".intval($_GET["p_id"]));
    $topic_id=$this->db->fetch_array($topic_id);

    $delete=$this->db->query("DELETE FROM kboard_posts WHERE id=".intval($_GET["p_id"]));

    $last_id=$this->db->query("SELECT id, timestamp FROM kboard_posts WHERE topic=".$topic_id["topic"]." ORDER BY id DESC");
    $last_id=$this->db->fetch_array($last_id);

    if ($last_id["id"]=="") {
    $update=$this->db->query("UPDATE kboard_topics SET lastpost_id=0, lastpost_timestamp=0 WHERE id=".$topic_id["topic"]);
    } else { $update=$this->db->query("UPDATE kboard_topics SET lastpost_id=".$last_id["id"].", lastpost_timestamp=".$last_id["timestamp"]." WHERE id=".$topic_id["topic"]); }

    header("Location: ./index.php?topic=".$_GET["t_id"]."&mt=1");
  }
  
  //---[CZ]: smazání topicu
  public function delete_topic() {
    $delete=$this->db->query("DELETE FROM kboard_topics WHERE id=".intval($_GET["t_id"]));
    $delete=$this->db->query("DELETE FROM kboard_posts WHERE topic=".intval($_GET["t_id"]));
    header("Location: ./index.php?forum=".$_GET["f_id"]."&mt=1");
  }
  
  //---[CZ]: odpověď na příspěvek
  public function response($paging_number) {
    if ($_POST["text"]!="" AND $this->spam_protect()==1) {

    if (isset($_SESSION["user_id"])) { $author=$_SESSION["user_id"]; } else { $author=0; }

    $text=htmlspecialchars($_POST["text"], ENT_NOQUOTES);
    $text=str_replace("\r\n", "<br />", $text);

    $insert=$this->db->query("INSERT INTO kboard_posts (author, response, text, timestamp, topic, forum, ip) values (".intval($author).", ".intval($_GET["p_id"]).", '".$this->db->escape($text)."', ".time().", ".intval($_GET["t_id"]).", ".intval($_GET["f_id"]).", '".$_SERVER["REMOTE_ADDR"]."')");

    $last_id=$this->db->query("SELECT id, timestamp FROM kboard_posts ORDER BY id DESC");
    $last_id=$this->db->fetch_array($last_id);

    $update=$this->db->query("UPDATE kboard_topics SET lastpost_id=".$last_id["id"].", lastpost_timestamp=".$last_id["timestamp"]." WHERE id=".$_GET["t_id"]);

    $select=$this->db->query("SELECT COUNT(*) AS c FROM kboard_posts WHERE topic=".intval($_GET["t_id"]));
    $select=$this->db->fetch_array($select);

    $pages=ceil($select["c"]/$paging_number);

    header("Location: ./index.php?topic=".$_GET["t_id"]."&page=$pages&mt=1#".$last_id["id"]);

    } else { header("Location: ./index.php?action=response&p_id=".$_GET["p_id"]."&t_id=".$_GET["t_id"]."&f_id=".$_GET["f_id"]."&mt=0"); }
  }
  
  //---[CZ]: nový topic
  public function new_topic() {
    if ($_POST["title"]!="" AND $_POST["title"]!=LANG_TOPIC_NAME AND $_POST["text"]!="" AND $this->spam_protect()==1) {

    $text=htmlspecialchars($_POST["text"], ENT_NOQUOTES);
    $text=str_replace("\r\n", "<br />", $text);

    if (isset($_SESSION["user_id"])) { $author=$_SESSION["user_id"]; } else { $author=0; }

    $insert=$this->db->query("INSERT INTO kboard_topics (name, author, forum, timestamp) values ('".$this->db->escape(htmlspecialchars($_POST["title"], ENT_NOQUOTES))."', ".intval($author).", ".intval($_GET["f_id"]).", ".time().")");

    $lasttopic_id=$this->db->query("SELECT id FROM kboard_topics ORDER BY id DESC");
    $lasttopic_id=$this->db->fetch_array($lasttopic_id);

    $insert=$this->db->query("INSERT INTO kboard_posts (author, text, timestamp, topic, forum, ip) values (".intval($author).", '".$this->db->escape($text)."', ".time().", ".$lasttopic_id["id"].", ".intval($_GET["f_id"]).", '".$_SERVER["REMOTE_ADDR"]."')");

    $lastpost_id=$this->db->query("SELECT id, timestamp FROM kboard_posts ORDER BY id DESC");
    $lastpost_id=$this->db->fetch_array($lastpost_id);

    $update=$this->db->query("UPDATE kboard_topics SET lastpost_id=".$lastpost_id["id"].", lastpost_timestamp=".$lastpost_id["timestamp"]." WHERE id=".$lasttopic_id["id"]);

    header("Location: ./index.php?topic=".$lasttopic_id["id"]."&mt=1");

    } else { header("Location: ./index.php?forum=".$_GET["f_id"]."&mt=0"); }
  }
  
  //---[CZ]: obsahu tagu title
  public function title_tag() {
    if ($_GET["action"]=="search") { echo " &raquo; ".LANG_SEARCH_RESULTS." \""; echo $_GET["w"]; echo "\""; }
    if (isset($_GET["profile"])) { echo " &raquo; ".LANG_USER_PROFILE." ".$this->profile_details("login"); }
    if (isset($_GET["forum"])) { echo " &raquo; ".$this->forum_details("name"); }
    if (isset($_GET["topic"])) { echo " &raquo; ".$this->topic_forum_details("name"); }
    if (isset($_GET["topic"])) { echo " &raquo; ".$this->topic_details("name"); }
  }
  
  //---[CZ]: obsah tagu h1
  public function h1_tag() {
    echo "<a href=\"./index.php\">".LANG_FORUM_INDEX."</a>";
    if ($_GET["action"]=="search") { echo " &raquo; ".LANG_SEARCH_RESULTS." \""; echo $_GET["w"]; echo "\""; }
    if (isset($_GET["profile"])) { echo " &raquo; <a href=\"./index.php?profile=".$_GET["profile"]."\">".LANG_USER_PROFILE." ".$this->profile_details("login")."</a>"; }
    if (isset($_GET["forum"])) { echo " &raquo; <a href=\"./index.php?forum=".$this->forum_details("id")."\">".$this->forum_details("name")."</a>"; }
    if (isset($_GET["topic"])) { echo " &raquo; <a href=\"./index.php?forum=".$this->topic_forum_details("id")."\">".$this->topic_forum_details("name")."</a>"; }
    if (isset($_GET["topic"])) { echo " &raquo; <a href=\"./index.php?topic=".$_GET["topic"]."\">".$this->topic_details("name")."</a>"; }
  }
  
  //---[CZ]: údaje o přihlášeném uživateli
  public function logged_user_details($column) {
    $sql=$this->db->query("SELECT $column FROM ".TABLE_USERS." WHERE id=".intval($_SESSION["user_id"]));
    $sql=$this->db->fetch_array($sql);
    return $this->db->unescape($sql["$column"]);
  }
  
  //---[CZ]: dolní statistiky
  public function bottom_stats($table) {
    $sql=$this->db->query("SELECT COUNT(*) AS c FROM $table");
    $sql=$this->db->fetch_array($sql);
    return $this->db->unescape($sql["c"]);
  }

}
?>