bouffeatulm/inc/User.class.php

148 lines
3.9 KiB
PHP
Raw Normal View History

2013-08-07 23:29:57 +02:00
<?php
2013-08-12 09:52:50 +02:00
require_once('data/config.php');
require_once('Storage.class.php');
2013-08-07 23:29:57 +02:00
class User extends Storage {
protected $id = 0, $login, $display_name, $password, $admin;
protected $TABLE_NAME = "Users";
2013-08-07 23:29:57 +02:00
protected $fields = array(
'id'=>'key',
'login'=>'string',
'display_name'=>'string',
'password'=>'password',
2013-08-07 23:29:57 +02:00
'admin'=>'bool'
);
public function __construct() {
2013-08-07 23:29:57 +02:00
parent::__construct();
}
2013-08-26 09:52:04 +02:00
// Getters
// =======
2013-08-07 23:29:57 +02:00
public function getLogin() {
return $this->login;
}
public function getDisplayName() {
return $this->display_name;
}
2013-08-07 23:29:57 +02:00
public function getId() {
return $this->id;
}
public function getAdmin() {
return $this->admin;
}
2013-08-09 00:44:43 +02:00
2013-08-26 09:52:04 +02:00
// Setters
// =======
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 setDisplayName($display_name) {
$this->display_name = $display_name;
}
2013-08-07 23:29:57 +02:00
public function setPassword($password) {
2013-08-09 00:44:43 +02:00
$this->password = $password;
2013-08-07 23:29:57 +02:00
}
public function setAdmin($admin) {
2013-08-10 22:33:39 +02:00
$this->admin = (bool) $admin;
}
2013-08-26 09:52:04 +02:00
// Password functions
// ==================
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
2013-08-26 09:52:04 +02:00
// Check if a user exists by login and load it
// ===========================================
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-25 23:06:47 +02:00
$this->setDisplayName($user_data[0]['display_name']);
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;
}
}
2013-08-26 09:52:04 +02:00
// Session storage
// ===============
2013-08-09 00:44:43 +02:00
public function sessionStore() {
return serialize(array('id'=>$this->id, 'login'=>$this->login, 'display_name'=>$this->display_name, 'password'=>$this->password, 'admin'=>$this->admin));
2013-08-09 00:44:43 +02:00
}
public function sessionRestore($data, $serialized = false) {
2013-08-10 22:33:39 +02:00
if($serialized)
$user_data = unserialize($data);
2013-08-10 22:33:39 +02:00
else
$user_data = $data;
2013-08-09 00:44:43 +02:00
$this->setId($user_data['id']);
$this->setLogin($user_data['login']);
$this->setDisplayName($user_data['display_name']);
2013-08-09 00:44:43 +02:00
$this->setPassword($user_data['password']);
$this->setAdmin($user_data['admin']);
}
2013-08-26 09:52:04 +02:00
// Load overload => TODO
// =============
public function load_users($fields = NULL) {
$return = array();
$users = $this->load($fields);
foreach($users as $user) {
$return[$user['id']] = new User();
$return[$user['id']]->sessionRestore($user);
}
return $return;
}
public function load_user($fields = NULL) {
$fetch = $this->load($fields);
if(count($fetch) > 0) {
$this->setId($fetch[0]['id']);
$this->setLogin($fetch[0]['login']);
$this->setDisplayName($fetch[0]['display_name']);
$this->setPassword($fetch[0]['password']);
$this->setAdmin($fetch[0]['admin']);
return true;
}
else {
return false;
}
}
2013-08-26 09:52:04 +02:00
// Check wether a user already exists or not
// (a user = aunique login and display_name)
// =========================================
public function isUnique() {
2013-08-25 23:06:47 +02:00
if(count($this->load_users(array('login'=>$this->login))) == 0 && count($this->load_users(array('display_name'=>$this->display_name)))) {
return true;
}
else {
return false;
}
}
2013-08-07 23:29:57 +02:00
}