<?php
class User
{
    private $_db=null,
            $_data,
            $_table,
            $_isLoggedIn=false;
            
    public $_sessionName;

    public function __construct($table)
    {
        $this->_table=$table;

        $this->_db=Db::getInstance();
        $this->_sessionName=Config::get("session/session_name");

        if(Session::exists($this->_sessionName))
        {
        $this->_isLoggedIn=true;
        }

    }


    public function login($username=null,$password=null)
    {

        $user=$this->find($username);
        if($user)
        {
            if($this->_data["password"]===Hash::make($password,Config::get("salt/adminSalt")))
            {
                Session::put($this->_sessionName,$this->_data["id"]);
                return true;
            }

        }

        return false;
    }


    private function find($user=null)
    {

      if($this->_db->num_rows("select * from {$this->_table} where username='{$user}'")>0) {
          $this->_data=$this->_db->get_row("select * from {$this->_table} where username='{$user}'");

          return true;

      }
return false;

    }



    public function isLoggedIn()
    {
        return $this->_isLoggedIn;
    }



    public function logout()
    {
        return Session::delete(Config::get("session/session_name"));
    }

}
