bludit/kernel/dbusers.class.php

147 lines
3.2 KiB
PHP
Raw Normal View History

2015-05-05 03:00:01 +02:00
<?php defined('BLUDIT') or die('Bludit CMS.');
class dbUsers extends dbJSON
{
private $dbFields = array(
'firstName'=> array('inFile'=>false, 'value'=>''),
'lastName'=> array('inFile'=>false, 'value'=>''),
'username'=> array('inFile'=>false, 'value'=>''),
2015-05-15 00:07:45 +02:00
'role'=> array('inFile'=>false, 'value'=>'editor'),
2015-05-05 03:00:01 +02:00
'password'=> array('inFile'=>false, 'value'=>''),
2015-05-15 00:07:45 +02:00
'salt'=> array('inFile'=>false, 'value'=>'!Pink Floyd!Welcome to the machine!'),
2015-05-05 03:00:01 +02:00
'email'=> array('inFile'=>false, 'value'=>''),
'registered'=> array('inFile'=>false, 'value'=>0)
);
function __construct()
{
parent::__construct(PATH_DATABASES.'users.php');
}
// Return an array with the username databases
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;
}
// Return TRUE if the user exists, FALSE otherwise.
public function userExists($username)
{
return isset($this->db[$username]);
}
public function getAll()
{
return $this->db;
}
2015-06-10 00:55:23 +02:00
public function setPassword($args)
{
$salt = Text::randomText(SALT_LENGTH);
$hash = sha1($args['password'].$salt);
$args['salt'] = $salt;
$args['password'] = $hash;
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;
}
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;
}
// The current unix time stamp.
$dataForDb['registered'] = Date::unixTime();
// 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;
}
}