bouffeatulm/inc/User.class.php

202 lines
5.8 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, $email, $display_name, $password, $admin, $json_token, $notifications, $stay_signed_in_token;
protected $TABLE_NAME = "Users";
2013-08-07 23:29:57 +02:00
protected $fields = array(
'id'=>'key',
'login'=>'string',
'email'=>'string',
'display_name'=>'string',
'password'=>'password',
'admin'=>'bool',
'json_token'=>'string',
'notifications'=>'int',
'stay_signed_in_token'=>'string'
2013-08-07 23:29:57 +02:00
);
public function __construct() {
2013-08-07 23:29:57 +02:00
parent::__construct();
$stay_signed_in_token = 0;
2013-08-07 23:29:57 +02:00
}
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() {
2014-08-30 23:39:41 +02:00
return (!empty($this->display_name) ? $this->display_name : $this->login);
}
2013-08-07 23:29:57 +02:00
public function getId() {
return $this->id;
}
public function getEmail() {
return $this->email;
}
public function getAdmin() {
return $this->admin;
}
2013-08-09 00:44:43 +02:00
public function getJsonToken() {
return $this->json_token;
}
public function getNotifications() {
return $this->notifications;
}
public function getStaySignedInToken() {
return $this->stay_signed_in_token;
}
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 setEmail($email) {
if(filter_var($email, FILTER_VALIDATE_EMAIL) !== false) {
$this->email = $email;
return true;
}
else {
return false;
}
}
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;
}
public function setJsonToken($token) {
$this->json_token = $token;
}
public function setNotifications($notifications) {
switch($notifications) {
case 1: // Nothing
$this->notifications = 1;
break;
case 2: // Global paybacks only
$this->notifications = 2;
break;
2014-08-30 23:21:27 +02:00
case 3: // Everything concerning you
$this->notifications = 3;
break;
default:
$this->notifications = 3;
break;
}
}
public function setStaySignedInToken($token) {
$this->stay_signed_in_token = $token;
}
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
// JSON token functions
// ====================
public function newJsonToken() {
$this->json_token = md5(uniqid(mt_rand(), true));
}
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), true);
2014-08-30 23:21:27 +02:00
if(count($user_data) == 1) {
return $user_data;
2013-08-09 00:44:43 +02:00
}
else {
return false;
}
}
2013-08-26 09:52:04 +02:00
// Session storage
// ===============
2013-09-06 23:21:05 +02:00
public function sessionStore($serialize = true) {
if($serialize) {
return serialize(array('id'=>$this->id, 'login'=>$this->login, 'email'=>$this->email, 'display_name'=>$this->display_name, 'password'=>$this->password, 'admin'=>$this->admin, 'json_token'=>$this->json_token, 'notifications'=>$this->notifications, 'stay_signed_in_token'=>$this->stay_signed_in_token));
2013-09-06 23:21:05 +02:00
}
else {
return array('id'=>$this->id, 'login'=>$this->login, 'email'=>$this->email, 'display_name'=>$this->display_name, 'password'=>$this->password, 'admin'=>$this->admin, 'json_token'=>$this->json_token, 'notifications'=>$this->notifications, 'stay_signed_in_token'=>$this->stay_signed_in_token);
2013-09-06 23:21:05 +02:00
}
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->setEmail($user_data['email']);
$this->setDisplayName($user_data['display_name']);
2013-08-09 00:44:43 +02:00
$this->setPassword($user_data['password']);
$this->setAdmin($user_data['admin']);
$this->setJsonToken($user_data['json_token']);
$this->setNotifications($user_data['notifications']);
$this->setStaySignedInToken($user_data['stay_signed_in_token']);
2013-08-09 00:44:43 +02:00
}
2014-08-30 23:21:27 +02:00
// Check wether a user already exists or not
// (a user = a unique login and display_name)
2013-08-26 09:52:04 +02:00
// =========================================
public function isUnique() {
if($this->load(array('login'=>$this->login)) === false && $this->load(array('display_name'=>$this->display_name)) === false) {
return true;
}
else {
return false;
}
}
2013-08-26 21:21:52 +02:00
// Maps htmlspecialchars on the class before display
// =================================================
public function secureDisplay() {
$this->id = (int) $this->id;
$this->login = htmlspecialchars($this->login);
$this->email = htmlspecialchars($this->email);
2013-08-26 21:21:52 +02:00
$this->display_name = htmlspecialchars($this->display_name);
$this->admin = (int) $this->admin;
$this->json_token = htmlspecialchars($this->json_token);
$this->notifications = (int) $this->notifications;
2013-08-26 21:21:52 +02:00
return $this;
}
2013-08-07 23:29:57 +02:00
}