2015-05-05 03:00:01 +02:00
|
|
|
<?php defined('BLUDIT') or die('Bludit CMS.');
|
|
|
|
|
|
|
|
class dbUsers extends dbJSON
|
|
|
|
{
|
2015-11-20 04:21:39 +01:00
|
|
|
public $dbFields = array(
|
|
|
|
'firstName'=> array('inFile'=>false, 'value'=>''),
|
|
|
|
'lastName'=> array('inFile'=>false, 'value'=>''),
|
|
|
|
'username'=> array('inFile'=>false, 'value'=>''),
|
|
|
|
'role'=> array('inFile'=>false, 'value'=>'editor'),
|
|
|
|
'password'=> array('inFile'=>false, 'value'=>''),
|
|
|
|
'salt'=> array('inFile'=>false, 'value'=>'!Pink Floyd!Welcome to the machine!'),
|
|
|
|
'email'=> array('inFile'=>false, 'value'=>''),
|
|
|
|
'registered'=> array('inFile'=>false, 'value'=>'1985-03-15 10:00'),
|
|
|
|
'tokenEmail'=> array('inFile'=>false, 'value'=>''),
|
2015-12-12 00:11:53 +01:00
|
|
|
'tokenEmailTTL'=> array('inFile'=>false, 'value'=>'2009-03-15 14:00'),
|
2016-02-14 01:15:19 +01:00
|
|
|
'twitter'=> array('inFile'=>false, 'value'=>''),
|
|
|
|
'facebook'=> array('inFile'=>false, 'value'=>''),
|
|
|
|
'googlePlus'=> array('inFile'=>false, 'value'=>''),
|
|
|
|
'instagram'=> array('inFile'=>false, 'value'=>'')
|
2015-05-05 03:00:01 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct(PATH_DATABASES.'users.php');
|
|
|
|
}
|
|
|
|
|
2016-01-01 00:31:51 +01:00
|
|
|
public function getUser($username)
|
|
|
|
{
|
|
|
|
$User = new User();
|
|
|
|
|
|
|
|
if($this->userExists($username))
|
|
|
|
{
|
|
|
|
$User->setField('username', $username);
|
|
|
|
|
|
|
|
foreach($this->db[$username] as $key=>$value) {
|
|
|
|
$User->setField($key, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $User;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-20 04:21:39 +01:00
|
|
|
public function getAll()
|
|
|
|
{
|
|
|
|
return $this->db;
|
|
|
|
}
|
|
|
|
|
2015-10-20 05:14:28 +02:00
|
|
|
// Return an array with the username databases, filtered by username.
|
2015-07-15 01:57:18 +02:00
|
|
|
public function getDb($username)
|
2015-05-05 03:00:01 +02:00
|
|
|
{
|
|
|
|
if($this->userExists($username))
|
|
|
|
{
|
|
|
|
$user = $this->db[$username];
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-24 01:23:33 +02:00
|
|
|
// Return the username associated to an email, if the email does not exists return FALSE.
|
2015-10-20 05:14:28 +02:00
|
|
|
public function getByEmail($email)
|
|
|
|
{
|
2015-10-24 01:23:33 +02:00
|
|
|
foreach($this->db as $username=>$values) {
|
|
|
|
if($values['email']==$email) {
|
|
|
|
return $username;
|
2015-10-20 05:14:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-02 15:00:25 +02:00
|
|
|
// Return first the username associated with the role 'admin', if a user with 'admin' role not found return FALSE.
|
|
|
|
public function getAdminUser()
|
|
|
|
{
|
|
|
|
foreach($this->db as $username=>$values) {
|
|
|
|
if($values['role']=='admin') {
|
|
|
|
return $username;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-05-05 03:00:01 +02:00
|
|
|
// Return TRUE if the user exists, FALSE otherwise.
|
|
|
|
public function userExists($username)
|
|
|
|
{
|
|
|
|
return isset($this->db[$username]);
|
|
|
|
}
|
|
|
|
|
2015-10-20 05:14:28 +02:00
|
|
|
public function generateTokenEmail($username)
|
|
|
|
{
|
|
|
|
// Random hash
|
|
|
|
$token = sha1(Text::randomText(SALT_LENGTH).time());
|
|
|
|
$this->db[$username]['tokenEmail'] = $token;
|
|
|
|
|
2015-10-24 01:23:33 +02:00
|
|
|
// Token time to live, defined by TOKEN_EMAIL_TTL
|
|
|
|
$this->db[$username]['tokenEmailTTL'] = Date::currentOffset(DB_DATE_FORMAT, TOKEN_EMAIL_TTL);
|
2015-10-20 05:14:28 +02:00
|
|
|
|
|
|
|
// Save the database
|
|
|
|
if( $this->save() === false ) {
|
|
|
|
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $token;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setPassword($username, $password)
|
2015-06-10 00:55:23 +02:00
|
|
|
{
|
|
|
|
$salt = Text::randomText(SALT_LENGTH);
|
2015-10-19 00:45:58 +02:00
|
|
|
$hash = sha1($password.$salt);
|
2015-06-10 00:55:23 +02:00
|
|
|
|
2015-10-20 05:14:28 +02:00
|
|
|
$args['username'] = $username;
|
|
|
|
$args['salt'] = $salt;
|
|
|
|
$args['password'] = $hash;
|
2015-06-10 00:55:23 +02:00
|
|
|
|
|
|
|
return $this->set($args);
|
|
|
|
}
|
|
|
|
|
2016-09-07 02:14:57 +02:00
|
|
|
// Disable the user
|
|
|
|
public function disableUser($username)
|
|
|
|
{
|
|
|
|
$args['username'] = $username;
|
|
|
|
$args['password'] = '!';
|
|
|
|
|
|
|
|
return $this->set($args);
|
|
|
|
}
|
|
|
|
|
2015-05-05 03:00:01 +02:00
|
|
|
public function set($args)
|
|
|
|
{
|
2015-05-15 00:07:45 +02:00
|
|
|
$dataForDb = array();
|
|
|
|
|
2015-07-15 01:57:18 +02:00
|
|
|
$user = $this->getDb($args['username']);
|
2015-05-05 03:00:01 +02:00
|
|
|
|
2015-05-15 00:07:45 +02:00
|
|
|
if($user===false)
|
|
|
|
{
|
|
|
|
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to get the username '.$args['username']);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify arguments with the database fields.
|
2015-05-05 03:00:01 +02:00
|
|
|
foreach($args as $field=>$value)
|
|
|
|
{
|
|
|
|
if( isset($this->dbFields[$field]) )
|
|
|
|
{
|
2015-07-15 01:57:18 +02:00
|
|
|
// Sanitize.
|
2015-05-15 00:07:45 +02:00
|
|
|
$tmpValue = Sanitize::html($value);
|
|
|
|
|
2015-07-15 01:57:18 +02:00
|
|
|
// Set type.
|
2015-05-15 00:07:45 +02:00
|
|
|
settype($tmpValue, gettype($this->dbFields[$field]['value']));
|
2015-05-05 03:00:01 +02:00
|
|
|
|
2015-05-15 00:07:45 +02:00
|
|
|
$user[$field] = $tmpValue;
|
2015-05-05 03:00:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-15 00:07:45 +02:00
|
|
|
// Save the database
|
|
|
|
$this->db[$args['username']] = $user;
|
|
|
|
if( $this->save() === false ) {
|
|
|
|
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-05 03:00:01 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-22 05:15:02 +02:00
|
|
|
public function delete($username)
|
|
|
|
{
|
|
|
|
unset($this->db[$username]);
|
2015-08-26 05:42:32 +02:00
|
|
|
|
2015-07-22 05:15:02 +02:00
|
|
|
if( $this->save() === false ) {
|
|
|
|
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-05 03:00:01 +02:00
|
|
|
public function add($args)
|
|
|
|
{
|
|
|
|
$dataForDb = array();
|
|
|
|
|
|
|
|
// Verify arguments with the database fields.
|
|
|
|
foreach($this->dbFields as $field=>$options)
|
|
|
|
{
|
|
|
|
// If the user send the field.
|
|
|
|
if( isset($args[$field]) )
|
|
|
|
{
|
2015-05-15 00:07:45 +02:00
|
|
|
// Sanitize if will be saved on database.
|
|
|
|
if( !$options['inFile'] ) {
|
2015-05-05 03:00:01 +02:00
|
|
|
$tmpValue = Sanitize::html($args[$field]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$tmpValue = $args[$field];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Uses a default value for the field.
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$tmpValue = $options['value'];
|
|
|
|
}
|
|
|
|
|
2015-05-15 00:07:45 +02:00
|
|
|
// Set type
|
|
|
|
settype($tmpValue, gettype($options['value']));
|
|
|
|
|
|
|
|
// Save on database
|
2015-05-05 03:00:01 +02:00
|
|
|
$dataForDb[$field] = $tmpValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the user alredy exists.
|
|
|
|
if( $this->userExists($dataForDb['username']) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-26 05:42:32 +02:00
|
|
|
// Current date.
|
|
|
|
$dataForDb['registered'] = Date::current(DB_DATE_FORMAT);
|
2015-05-05 03:00:01 +02:00
|
|
|
|
|
|
|
// Password
|
2015-05-31 03:06:55 +02:00
|
|
|
$dataForDb['salt'] = Text::randomText(SALT_LENGTH);
|
2015-05-05 03:00:01 +02:00
|
|
|
$dataForDb['password'] = sha1($dataForDb['password'].$dataForDb['salt']);
|
|
|
|
|
|
|
|
// Save the database
|
|
|
|
$this->db[$dataForDb['username']] = $dataForDb;
|
2015-05-15 00:07:45 +02:00
|
|
|
if( $this->save() === false ) {
|
|
|
|
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-05 03:00:01 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-11-28 15:47:03 +01:00
|
|
|
}
|