Location: PHPKode > projects > Katyshop > katyshop_ro_0.3.2.1/classes/logic/User.php
<?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/>.
 *
 */

require_once(dirname(__FILE__) . "/Visitor.php");

class User extends Visitor
{
	// database stored properties
	var $id = 0;
	var $username = ""; // unique
	var $password = ""; // only password can be edited from profile
	var $email = ""; // unique
	var $acc_type = ""; // person, company
	var $active = 0; // is this account active? if not, it's not allowed to login
	var $activation_code = ""; // this is sent the first time on mail, and user must use it to activate his account
	var $login_code = ""; // this is used by "remember my password" feature, sent by cookie and used instead of password

	function User()
	{
		parent::Visitor();
		$this->activation_code = Tools::getRandomChars(10);
	}

	function getInstanceProps($myClass = "")
	{
		if(empty($myClass))
			$myClass = __CLASS__;
		return parent::getInstanceProps($myClass);
	}

	function validateCommonFields()
	{
		$errors = array();
		if(!Tools::validateAlphanumeric($this->username))
			$errors[] = "Username-ul poate contine numai litere, numere si caracterul underscore";
		if(strlen($this->username) < 5 || strlen($this->username) > 20)
			$errors[] = "Username-ul poate avea intre 5 si 20 caractere";
		if(strlen($this->password) < 5 || strlen($this->password) > 255)
			$errors[] = "Parola trebuie sa aiba intre 5 si 255 caractere";
		if(!Tools::validateEmail($this->email) || strlen($this->email) > 255)
			$errors[] = "Adresa de email nu este valida";
		if(!in_array($this->acc_type, array("person", "company", "admin")))
			$errors[] = "Va rugam sa alegeti un tip valid de cont";
		return $errors;
	}

	function validateRegister($confirmPassword)
	{
		$db = Application::getDb();
		$errors = $this->validateCommonFields();
		if($this->password != $confirmPassword)
			$errors[] = "Cele doua parole nu se potrivesc";

		if($db->tbUser->usernameExists($this->username))
			$errors[] = "Acest username este deja folosit de altcineva, va rugam sa incercati din nou";
		if($db->tbUser->emailExists($this->email))
			$errors[] = "Aceasta adresa de email a fost folosita pentru a crea un alt cont";

		Application::appendErrors($errors);
		return (count($errors) == 0);
	}

	function validateUpdate($oldPassword, $newPassword, $confirmPassword)
	{
		$errors = $this->validateCommonFields();
		if(!empty($newPassword))
		{
			if($this->password != md5($oldPassword))
				$errors[] = "Parola veche nu este corecta";
			if($newPassword != $confirmPassword)
				$errors[] = "Noua parola si parola de confirmare nu sunt la fel";
			if(strlen($newPassword) < 5 || strlen($newPassword) > 255)
				$errors[] = "Noua parola trebuie sa aiba intre 5 si 255 caractere";
		}

		Application::appendErrors($errors);
		return (count($errors) == 0);
	}

	function login($username, $password, $rememberPassword)
	{
		if($this->username == $username && md5($password) == $this->password && $this->active == 1)
		{
			$this->logged_in = 1;
			if ($rememberPassword == 1 && !$this->isSuperadmin())
			{
				$login_code = Tools::getRandomChars(20);
				$this->login_code = md5($login_code);
				$db = Application::getDb();
				$db->tbUser->updateObj($this);
				setcookie("username", $this->username, time() + 60 * 60 * 24 * 14, "/");
				setcookie("login_code", $login_code, time() + 60 * 60 * 24 * 14, "/");
			}
			return true;
		}
		else
		{
			$this->logged_in = 0;
			if(md5($password) != $this->password)
				Application::addError("Parola nu este corecta");
			elseif (!$this->wasActivated())
				Application::addError("Trebuie intai sa activati contul inainte de a va putea loga");
			elseif ($this->isDeactivated())
				Application::addError("Contul dumneavoastra a fost dezactivat, contactati va rugam personalul de suport pentru mai multe detalii");
		}
		return ($this->logged_in == 1);
	}

	function loginFromCookie($username, $login_code)
	{
		if($this->username == $username && md5($login_code) == $this->login_code && $this->active == 1)
		{
			$this->logged_in = 1;
			return true;
		}
		else
		{
			$this->logged_in = 0;
		}
		return ($this->logged_in == 1);
	}

	/**
	 * is this user active, can he login?
	 */
	function isActive()
	{
		return ($this->active == "1");
	}

	/**
	 * did the user ever entered the activation code?
	 */
	function wasActivated()
	{
		return (strlen($this->activation_code) == 0);
	}

	/**
	 * is this user deactivated by an admin?
	 */
	function isDeactivated()
	{
		return (!$this->isActive() && $this->wasActivated());
	}

	function activate()
	{
		$this->active = "1";
		$this->activation_code = "";
	}

	function toStr($humanReadable = false, $brief = false)
	{
		if($humanReadable)
		{
			if($brief)
			{
				$s = "
ID client: {$this->id}";
			}
			else
			{
				$s = "
Cont de tip User, cu adresa de IP {$this->ip} si detaliile:
ID: {$this->id}
Username: {$this->username}
Email: {$this->email}
Tip cont: {$this->acc_type}
Cont activ: {$this->active} (1 = activ, 0 = inactiv)
Cod de activare: {$this->activation_code}
";
			}
			return $s;
		}
		else
		{
			return parent::toStr();
		}
	}

}









?>
Return current item: Katyshop