<?php
/**
* Product: Katyshop
* @version 0.3.2.1
* @author Catalin Hulea - hide@address.com
* @copyright Copyright (C) 2007 Catalin Hulea
* @license GNU General Public License version 3
* You can find a copy of GNU GPL v3 at this path: /docs/LICENSE
* @link https://sourceforge.net/projects/katyshop
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
class Visitor extends LogicObject
{
var $ip = "";
var $logged_in = 0;
function Visitor()
{
parent::LogicObject();
$this->ip = $_SERVER['REMOTE_ADDR'];
}
function getInstanceProps($myClass = "")
{
if(empty($myClass))
$myClass = __CLASS__;
return parent::getInstanceProps($myClass);
}
/**
* @static
* @return Visitor
*/
function getInstance()
{
global $Application;
$user = $Application->user;
// first try to get the user from global scope
if(is_a($user, "Visitor"))
return $user;
// $user was not yet declared, try to get it from session
$user = SessionHandler::get("Application_user");
if(!is_a($user, "User") && !empty($_COOKIE["username"]) && !empty($_COOKIE["login_code"]))
{
// is not logged in, try to login from cookie (remember password)
$user = Visitor::loginFromCookie($_COOKIE["username"], $_COOKIE["login_code"]);
}
// if there is no user on the session, create a new Visitor
if(!is_a($user, "User"))
$user = new Visitor();
return $user;
}
function login($username, $password, $rememberPassword)
{
$user = new Visitor();
$db = Application::getDb();
$superadmin = Application::getConfigValue("superadmin");
if($username == $superadmin["username"])
{
$user = new User();
$user->copyFromArray($superadmin);
$user->acc_type = "admin";
$user = Factory::instantiateUser($user);
$user->login($username, $password, 0);
}
elseif($db->tbUser->usernameExists($username))
{
$user = $db->tbUser->getUserByUsername($username);
$user = Factory::instantiateUser($user);
$user->login($username, $password, $rememberPassword);
}
else
{
Application::addError("Acest username nu exista");
}
SessionHandler::set("Application_user", $user);
return $user;
}
function loginFromCookie($username, $login_code)
{
$user = new Visitor();
$db = Application::getDb();
if($db->tbUser->usernameExists($username))
{
$user = $db->tbUser->getUserByUsername($username);
$user = Factory::instantiateUser($user);
$user->loginFromCookie($username, $login_code);
}
return $user;
}
function logout()
{
setcookie("username", "", time() - 60 * 60 * 24 * 14, "/");
setcookie("login_code", "", time() - 60 * 60 * 24 * 14, "/");
$db = Application::getDb();
$user = Application::getUser();
if(is_a($user, "User"))
$db->tbUser->setLoginCode($user->id, "");
$user = new Visitor();
SessionHandler::set("Application_user", $user);
return $user;
}
function isUserLoggedIn()
{
return (is_a($this, "User") && $this->logged_in == 1);
}
function isPersonLoggedIn()
{
return (is_a($this, "UserPerson") && $this->logged_in == 1);
}
function isCompanyLoggedIn()
{
return (is_a($this, "UserCompany") && $this->logged_in == 1);
}
function isAdminLoggedIn()
{
return (is_a($this, "Admin") && $this->logged_in == 1);
}
function isSuperadmin()
{
return (is_a($this, "Admin") && $this->is_superadmin == 1);
}
function toStr($humanReadable = false, $brief = false)
{
if($humanReadable)
{
$s = "Utilizator neautentificat cu adresa de IP {$this->ip}";
return $s;
}
else
{
return parent::toStr();
}
}
}
?>