2013-08-07 23:29:57 +02:00
|
|
|
<?php
|
|
|
|
require_once('config.php');
|
2013-08-08 22:55:12 +02:00
|
|
|
require_once('Storage.class.php');
|
2013-08-07 23:29:57 +02:00
|
|
|
|
|
|
|
class User extends Storage {
|
2013-08-08 22:55:12 +02:00
|
|
|
protected $id, $login, $password, $admin;
|
|
|
|
protected $TABLE_NAME = "Users";
|
2013-08-07 23:29:57 +02:00
|
|
|
protected $fields = array(
|
|
|
|
'id'=>'key',
|
2013-08-08 22:55:12 +02:00
|
|
|
'login'=>'string',
|
|
|
|
'password'=>'password',
|
2013-08-07 23:29:57 +02:00
|
|
|
'admin'=>'bool'
|
|
|
|
);
|
|
|
|
|
2013-08-08 22:55:12 +02:00
|
|
|
public function __construct() {
|
2013-08-07 23:29:57 +02:00
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getLogin() {
|
|
|
|
return $this->login;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getId() {
|
|
|
|
return $this->id;
|
|
|
|
}
|
2013-08-08 22:55:12 +02:00
|
|
|
|
|
|
|
public function getAdmin() {
|
|
|
|
return $this->admin;
|
|
|
|
}
|
2013-08-09 00:44:43 +02:00
|
|
|
|
|
|
|
public function setId($id) {
|
2013-08-10 22:33:39 +02:00
|
|
|
$this->id = (int) $id;
|
2013-08-09 00:44:43 +02:00
|
|
|
}
|
|
|
|
|
2013-08-07 23:29:57 +02:00
|
|
|
public function setLogin($login) {
|
|
|
|
$this->login = $login;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setPassword($password) {
|
2013-08-09 00:44:43 +02:00
|
|
|
$this->password = $password;
|
2013-08-07 23:29:57 +02:00
|
|
|
}
|
|
|
|
|
2013-08-08 22:55:12 +02:00
|
|
|
public function setAdmin($admin) {
|
2013-08-10 22:33:39 +02:00
|
|
|
$this->admin = (bool) $admin;
|
2013-08-08 22:55:12 +02:00
|
|
|
}
|
|
|
|
|
2013-08-07 23:29:57 +02:00
|
|
|
public function encrypt($text) {
|
|
|
|
return crypt($text, SALT);
|
|
|
|
}
|
|
|
|
|
2013-08-09 00:44:43 +02:00
|
|
|
public function checkPassword($password) {
|
2013-08-07 23:29:57 +02:00
|
|
|
return User::encrypt($password) == $this->password;
|
|
|
|
}
|
2013-08-09 00:44:43 +02:00
|
|
|
|
|
|
|
public function exists() {
|
|
|
|
$user_data = $this->load(array('login'=>$this->login));
|
|
|
|
if(count($user_data) == 1) {
|
2013-08-10 22:33:39 +02:00
|
|
|
$this->setId($user_data[0]['id']);
|
2013-08-09 00:44:43 +02:00
|
|
|
$this->setAdmin($user_data[0]['admin']);
|
|
|
|
$this->setPassword($user_data[0]['password']);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function sessionStore() {
|
|
|
|
return serialize(array('id'=>$this->id, 'login'=>$this->login, 'password'=>$this->password, 'admin'=>$this->admin));
|
|
|
|
}
|
|
|
|
|
2013-08-10 22:33:39 +02:00
|
|
|
public function sessionRestore($data, $serialized) {
|
|
|
|
if($serialized)
|
|
|
|
$user_data = unserialize($serialized_data);
|
|
|
|
else
|
|
|
|
$user_data = $data;
|
2013-08-09 00:44:43 +02:00
|
|
|
|
|
|
|
$this->setId($user_data['id']);
|
|
|
|
$this->setLogin($user_data['login']);
|
|
|
|
$this->setPassword($user_data['password']);
|
|
|
|
$this->setAdmin($user_data['admin']);
|
|
|
|
}
|
2013-08-07 23:29:57 +02:00
|
|
|
}
|