<?php
class Profile extends Index {

  public function __construct() { parent::__construct(); }
  
  //---[CZ]: počet dat
  public function get_count($table, $author) {
  
    $sql=$this->db->query("SELECT id FROM $table WHERE author=$author");
    return $this->db->num_rows($sql);
    
  }
  
  //---[CZ]: hodnost
  public function rank($rights) {
  
    if ($rights==1) { return "redaktor"; }
    if ($rights==2) { return "korektor"; }
    if ($rights==3) { return "administrátor"; }
    if ($rights==4) { return "sakra administrátor"; }
    if ($rights==5) { return "kurva moc administrátor"; }
    
  }
  
  //---[CZ]: uložení profilu
  public function update() {
  
    if (preg_match("/^[0-9a-zA-Z]{3,128}$/", $_POST["login"])) {

    if (!isset($_POST["delete_avatar"])) { $_POST["delete_avatar"]=0; }
    if (!isset($_POST["mail_messages"])) { $_POST["mail_messages"]=0; }
    
    if ($this->login->check_access("users")==1) {
    
    $sql=$this->db->query("SELECT id FROM ".TABLE_USERS." WHERE login='".$this->db->escape($_GET["profile"])."'");
    $sql=$this->db->fetch_array($sql);
    
    $id=$sql["id"];
    
    } else {
    
    $id=$_SESSION["user_id"];
    
    }

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

    $sql=$this->db->query("UPDATE ".TABLE_USERS." SET
    realname='".htmlspecialchars($_POST["realname"])."',
    age='".intval($_POST["age"])."',
    about='$about',
    sign='$sign',
    mail='".htmlspecialchars($_POST["mail"])."',
    jabber='".htmlspecialchars($_POST["jabber"])."',
    skype='".htmlspecialchars($_POST["skype"])."',
    msn='".htmlspecialchars($_POST["msn"])."',
    icq='".intval($_POST["icq"])."',
    mail_messages=".intval($_POST["mail_messages"])."
    WHERE id=$id");
    
    if ($_FILES["file"]["name"]!="") {
    
    $explode=explode(".", $_FILES["file"]["name"]);

    if ($explode[1]=="jpg" OR $explode[1]=="JPG") {
    $img_data=imagecreatefromjpeg($_FILES["file"]["tmp_name"]);
    }

    if ($explode[1]=="png" OR $explode[1]=="PNG") {
    $img_data=imagecreatefrompng($_FILES["file"]["tmp_name"]);
    }

    if ($explode[1]=="gif" OR $explode[1]=="GIF") {
    $img_data=imagecreatefromgif($_FILES["file"]["tmp_name"]);
    }
    
    if (imagesx($img_data)>imagesy($img_data)) {
    $i=120/imagesx($img_data);
    $height=$i*imagesy($img_data);
    $width=120;
    } else {
    $i=90/imagesx($img_data);
    $height=$i*imagesy($img_data);
    $width=90;
    }

    $create=imagecreatetruecolor($width, $height);
    imagecopyresampled($create, $img_data, 0, 0, 0, 0, $width, $height, imagesx($img_data), imagesy($img_data));

    if ($explode[1]=="jpg" OR $explode[1]=="JPG") {
    imagejpeg($create, "./avatars/$id.".$explode[1], 100);
    }
    if ($explode[1]=="png" OR $explode[1]=="PNG") {
    imagepng($create, "./avatars/$id.".$explode[1], 0);
    }
    if ($explode[1]=="gif" OR $explode[1]=="GIF") {
    imagegif($create, "./avatars/$id.".$explode[1]);
    }
    
    $sql=$this->db->query("UPDATE ".TABLE_USERS." SET avatar='".$explode[1]."' WHERE id=$id");
    
    imagedestroy($img_data);
  
    }
    
    if ($_POST["delete_avatar"]==1) {
    $check=$this->db->query("SELECT avatar FROM ".TABLE_USERS." WHERE id=$id");
    $check=$this->db->fetch_array($check);
    unlink("./avatars/$id.".$check["avatar"]);
    $sql=$this->db->query("UPDATE ".TABLE_USERS." SET avatar='' WHERE id=$id");
    }
    
    $old=$this->db->query("SELECT login, password FROM ".TABLE_USERS." WHERE id=$id");
    $old=$this->db->fetch_array($old);

    if ($old["login"]!=$_POST["login"]) {
    $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"]=="" AND preg_match("/^[0-9a-zA-Z]{3,128}$/", $_POST["login"])) {
    $sql=$this->db->query("UPDATE ".TABLE_USERS." SET login='".$this->db->escape(htmlspecialchars($_POST["login"]))."' WHERE id=$id");
    }
    }

    if ($_POST["new_password1"]!="" AND SHA1($_POST["old_password"])==$old["password"] AND $_POST["new_password1"]==$_POST["new_password2"]) {
    $sql=$this->db->query("UPDATE ".TABLE_USERS." SET password='".SHA1($_POST["new_password1"])."' WHERE id=$id");
    }
    
    if ($old["login"]!=$_POST["login"]) {
    $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"]=="" AND preg_match("/^[0-9a-zA-Z]{3,128}$/", $_POST["login"])) {
    $sql=$this->db->query("UPDATE ".TABLE_USERS." SET login='".$this->db->escape(htmlspecialchars($_POST["login"]))."' WHERE id=$id");
    }
    header("Location: /profiles/".$_POST["login"]."/");
    }
    
    }
    
  }
  
  //---[CZ]: vypsání stránky
  public function get_page() {
  
    if (isset($_SESSION["user_id"])) {
  
    $sql=$this->db->query("SELECT login FROM ".TABLE_USERS." WHERE id=".$_SESSION["user_id"]);
    $sql=$this->db->fetch_array($sql);
    
    if ($sql["login"]==$_GET["profile"]) { $access=1; } else { $access=0; } 
  
    } else { $access=0; }
  
    if ($this->login->check_access("users")==1 OR $access==1) {
    
    $item=$this->db->query("SELECT id, login, mail, realname, age, jabber, skype, msn, about, sign, icq, avatar, mail_messages FROM ".TABLE_USERS." WHERE login='".$_GET["profile"]."'");
    $item=$this->db->fetch_array($item);
    
    $about=str_replace("<br />", "\r\n", $item["about"]);
    $sign=str_replace("<br />", "\r\n", $item["sign"]);
  
    echo "\n\n<form action=\"".$this->urls->profile($_GET["profile"])."\" method=\"post\" enctype=\"multipart/form-data\">
    
    <h4>".LANG_PROFILE."</h4>
    
    <div>
    
    <table>
    <tr>
    <td style=\"width: 44%;\"><strong>".LANG_USERNAME.":</strong><br /><input type=\"text\" name=\"login\" value=\"".$item["login"]."\" style=\"width: 200px;\" /></td>
    <td style=\"width: 44%;\"><strong>".LANG_REAL_NAME.":</strong><br /><input type=\"text\" name=\"realname\" value=\"".$item["realname"]."\" style=\"width: 200px;\" /></td>
    <td style=\"width: 12%;\"><strong>".LANG_AGE.":</strong><br /><input type=\"text\" name=\"age\" value=\"".$item["age"]."\" style=\"width: 50px;\" /></td>
    </tr>
    </table>
    
    </div>
    
    <div>
    
    <p><strong>".LANG_ABOUT_USER.":</strong><br /><textarea name=\"about\" style=\"height: 80px; width: 500px;\">$about</textarea></p>
    
    <p><strong>".LANG_SIGNATURE.":</strong><br /><textarea name=\"sign\" style=\"height: 80px; width: 500px;\">$sign</textarea></p>
    
    </div>
    
    <div>
    
    <table>
    <tr>
    <td style=\"width: 50%;\"><strong>".LANG_EMAIL.":</strong><br /><input type=\"text\" name=\"mail\" value=\"".$item["mail"]."\" style=\"width: 200px;\" /></td>
    <td style=\"width: 50%;\"><strong>".LANG_JABBER.":</strong><br /><input type=\"text\" name=\"jabber\" value=\"".$item["jabber"]."\" style=\"width: 200px;\" /></td>
    </tr>
    <tr>
    <td style=\"width: 50%;\"><strong>".LANG_WINDOWS_LIVE_MESSENGER.":</strong><br /><input type=\"text\" name=\"msn\" value=\"".$item["msn"]."\" style=\"width: 200px;\" /></td>
    <td style=\"width: 50%;\"><strong>".LANG_SKYPE.":</strong><br /><input type=\"text\" name=\"skype\" value=\"".$item["skype"]."\" style=\"width: 200px;\" /></td>
    </tr>
    <tr>
    <td style=\"width: 50%;\"><strong>".LANG_ICQ.":</strong><br /><input type=\"text\" name=\"icq\" value=\"".$item["icq"]."\" style=\"width: 200px;\" /></td>
    <td style=\"width: 50%;\"></td>
    </tr>
    </table>
    
    </div>
    
    <div>
    
    <table>
    <tr>
    <td style=\"width: 60%; vertical-align: top;\"><strong>".LANG_AVATAR.":</strong><br /><input type=\"file\" name=\"file\" style=\"width: 300px;\" /></td>
    <td style=\"width: 20%; text-align: center; vertical-align: top;\">";
    
    if ($item["avatar"]!="") { echo "<img src=\"/avatars/".$item["id"].".".$item["avatar"]."\" alt=\"".$item["avatar"]."\" style=\"width: 36px;\" />"; }
    
    echo "</td>
    <td style=\"width: 20%; vertical-align: top;\">";
    
    if ($item["avatar"]!="") { echo "<input type=\"checkbox\" name=\"delete_avatar\" value=\"1\" /> ".LANG_DELETE; }
    
    echo "</td>
    
    </tr>
    </table>
    
    </div>
    
    <h4>".LANG_OPTIONS."</h4>
    
    <div>
    
    <p><input type=\"checkbox\" name=\"mail_messages\" value=\"1\""; if ($item["mail_messages"]==1) { echo " checked=\"checked\""; } echo " /> ".LANG_I_AGREE_WITH_THE_SENDING_OF_INFORMATIVE_MAILS_FROM_THIS_WEBSITE."</p>
    
    </div>
    
    <h4>".LANG_CHANGE_OF_PASSWORD."</h4>
    
    <div>
    
    <table>
    <tr>
    <td style=\"width: 50%;\"><strong>".LANG_CURRENT_PASSWORD.":</strong><br /><input type=\"text\" name=\"old_password\" style=\"width: 200px;\" /></td>
    <td style=\"width: 50%;\"></td>
    </tr>
    <tr>
    <td style=\"width: 50%;\"><strong>".LANG_NEW_PASSWORD.":</strong><br /><input type=\"text\" name=\"new_password1\" style=\"width: 200px;\" /></td>
    <td style=\"width: 50%;\"><strong>".LANG_PASSWORD_AGAIN.":</strong><br /><input type=\"text\" name=\"new_password2\" style=\"width: 200px;\" /></td>
    </tr>
    </table>
    
    </div>
    
    <div>
    
    <p><input type=\"submit\" name=\"submit\" value=\"".LANG_SAVE_CHANGES."\" /></p>
    
    </div>
    
    </form>";
    
    } else {
  
    $item=$this->db->query("SELECT id, login, rights, mail, regdate, lastvisit, realname, age, jabber, skype, msn, about, sign, icq, avatar FROM ".TABLE_USERS." WHERE login='".htmlspecialchars($_GET["profile"])."'");
    $item=$this->db->fetch_array($item);
  
    echo "<h4>".LANG_ABOUT_USER."</h4>";
    
    if ($item["avatar"]!="") { echo "<img src=\"/avatars/".$item["id"].".".$item["avatar"]."\" alt=\"".$item["login"]."\" style=\"float: right;\" />"; }
    
    echo "<p><strong>".LANG_USERNAME.":</strong> ".$item["login"]."</p>
    
    <p><strong>".LANG_REGISTRATION_DATE.":</strong> ".date("j.n.Y", $item["regdate"])."</p>
    
    <p><strong>".LANG_LAST_LOGIN.":</strong> ".date("j.n.Y", $item["lastvisit"])."</p>
    
    <p><strong>".LANG_REAL_NAME.":</strong> ".$item["realname"]."</p>";
    
    if ($item["age"]!="") { echo "<p><strong>".LANG_AGE.":</strong> ".$item["age"]."</p>"; }
    
    echo "<p><strong>".LANG_RIGHTS.":</strong> ".$this->rank($item["rights"])."</p>";
    
    if ($item["about"]!="") { echo "<p><strong>".LANG_ABOUT_USER.":</strong> ".$item["about"]."</p>"; }
    
    if ($item["sign"]!="") { echo "<p><strong>".LANG_SIGNATURE.":</strong> ".$item["sign"]."</p>"; }
    
    echo "<p><strong>".LANG_ARTICLES.":</strong> ".$this->get_count("kcms_articles", $item["id"])."</p>
    
    <p><strong>".LANG_COMMENTS.":</strong> ".$this->get_count("kcms_comments", $item["id"])."</p>";
    
    echo "\n\n<h4>".LANG_CONTACT."</h4>";
    
    if ($item["mail"]!="") { echo "<p><strong>".LANG_EMAIL.":</strong> ".str_replace("@", " [at] ", $item["mail"])."</p>"; }
    
    if ($item["jabber"]!="") { echo "<p><strong>".LANG_JABBER.":</strong> ".str_replace("@", " [at] ", $item["jabber"])."</p>"; }
    
    if ($item["msn"]!="") { echo "<p><strong>".LANG_WINDOWS_LIVE_MESSENGER.":</strong> ".str_replace("@", " [at] ", $item["msn"])."</p>"; }
    
    if ($item["icq"]!=0) { echo "<p><strong>".LANG_ICQ.":</strong> ".$item["icq"]."</p>"; }
    
    if ($item["skype"]!="") { echo "<p><strong>".LANG_SKYPE.":</strong> ".$item["skype"]."</p>"; }
    
    }
    
  }

}
?>