bludit/kernel/dbusers.class.php

173 lines
3.9 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-08-26 05:42:32 +02:00
'role'=> array('inFile'=>false, 'value'=>'editor'),
'picture'=> array('inFile'=>false, 'value'=>''),
2015-05-05 03:00:01 +02:00
'password'=> array('inFile'=>false, 'value'=>''),
2015-08-26 05:42:32 +02:00
'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')
2015-05-05 03:00:01 +02:00
);
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
}
}
//Profile picture upload
$imageFormat = pathinfo($_FILES['profilePicture']['name'],PATHINFO_EXTENSION);
$target_file = 'content/pictures/'.$args['username'].'.'.$imageFormat;
if (move_uploaded_file($_FILES['profilePicture']['tmp_name'], $target_file)) {
$user['picture'] = $imageFormat;
} else {
'Error occured when trying to upload your file.';
}
// Saving modification time
$user['updated'] = Date::unixTime();
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;
}
}