<?

class StartPageInterface {


    function StartPageInterface() {
        $this->db = new mysql_db();
        if(!$this->db->connect(_DB_HOST, _DB_NAME, _DB_USER, _DB_PASS)) {
            die("StartPageInterface: DB connection error.");
        }
    }


    // function result: user id
    function create_user($email, $pwd) {
        $sql = "
            INSERT INTO
                "._DB_TABLE_PREFIX."users
                (email, password)
            VALUES
                ('".addslashes($email)."', '".addslashes($pwd)."')";
        $this->db->query($sql);

        return $this->db->getLastId();
    }



    function update_user($id, $email, $pwd) {
        $sql = "
            UPDATE
                "._DB_TABLE_PREFIX."users
            SET
                email = '".addslashes($email)."',
                password = '".addslashes($pwd)."'
            WHERE
                id = '".addslashes($id)."'
            LIMIT 1";
        $this->db->query($sql);
    }



    function delete_user($id) {
        $sql = "
            DELETE FROM
                "._DB_TABLE_PREFIX."users
            WHERE
                id = '".addslashes($id)."'
            LIMIT 1";
        $this->db->query($sql);
    }



    function login($email, $pwd) {
        $sql = "
            SELECT 
                id
            FROM
                "._DB_TABLE_PREFIX."users
            WHERE
                email = '".addslashes($email)."' AND 
                password = '".addslashes($pwd)."'
            LIMIT 1";
        $res = $this->db->query($sql);
        if(isset($res["id"])) {
            session_start();
            $_SESSION["logged_user"] = array("id" => $res["id"],
                                             "email" => $email,
                                             "password" => $pwd);
            $_SESSION['user_id'] = $res["id"];
        }
    }



    function logout() {
        setcookie("useremail", false);
        setcookie("userpassword", false);
        session_start();
        unset($_SESSION["logged_user"]);
        unset($_SESSION["user_id"]);
        session_destroy();
    }

}

?>