2013-12-22 20:55:53 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Model\User;
|
|
|
|
|
|
|
|
use SimpleValidator\Validator;
|
|
|
|
use SimpleValidator\Validators;
|
2014-02-08 14:13:14 -05:00
|
|
|
use PicoDb\Database;
|
2015-08-28 21:34:34 -04:00
|
|
|
use Session;
|
2014-05-26 20:47:40 -04:00
|
|
|
use Model\Config;
|
|
|
|
use Model\RememberMe;
|
|
|
|
use Model\Database as DatabaseModel;
|
2013-12-22 20:55:53 -05:00
|
|
|
|
2015-01-22 23:12:06 +01:00
|
|
|
// Check if the user is logged in
|
2015-01-17 19:35:59 +01:00
|
|
|
function is_loggedin()
|
2014-11-18 19:00:53 -05:00
|
|
|
{
|
2015-01-22 23:12:06 +01:00
|
|
|
return ! empty($_SESSION['loggedin']);
|
2014-11-18 19:00:53 -05:00
|
|
|
}
|
|
|
|
|
2015-01-17 18:53:40 -05:00
|
|
|
// Destroy the session and the rememberMe cookie
|
2015-01-17 19:35:59 +01:00
|
|
|
function logout()
|
2014-11-18 19:00:53 -05:00
|
|
|
{
|
2015-01-17 18:53:40 -05:00
|
|
|
RememberMe\destroy();
|
|
|
|
Session\close();
|
2014-11-18 19:00:53 -05:00
|
|
|
}
|
|
|
|
|
2015-01-22 23:12:06 +01:00
|
|
|
// Get the credentials from the current selected database
|
2016-03-14 21:34:50 -04:00
|
|
|
function get_credentials()
|
2013-12-22 20:55:53 -05:00
|
|
|
{
|
2015-08-14 21:33:39 -04:00
|
|
|
return Database::getInstance('db')
|
2015-01-28 05:26:36 +01:00
|
|
|
->hashtable('settings')
|
|
|
|
->get('username', 'password');
|
2013-12-22 20:55:53 -05:00
|
|
|
}
|
|
|
|
|
2016-03-14 21:34:50 -04:00
|
|
|
// Set last login date
|
|
|
|
function set_last_login()
|
|
|
|
{
|
|
|
|
return Database::getInstance('db')
|
|
|
|
->hashtable('settings')
|
|
|
|
->put(array('last_login' => time()));
|
|
|
|
}
|
|
|
|
|
2013-12-22 20:55:53 -05:00
|
|
|
// Validate authentication
|
|
|
|
function validate_login(array $values)
|
|
|
|
{
|
|
|
|
$v = new Validator($values, array(
|
|
|
|
new Validators\Required('username', t('The user name is required')),
|
|
|
|
new Validators\MaxLength('username', t('The maximum length is 50 characters'), 50),
|
|
|
|
new Validators\Required('password', t('The password is required'))
|
|
|
|
));
|
|
|
|
|
|
|
|
$result = $v->execute();
|
|
|
|
$errors = $v->getErrors();
|
|
|
|
|
|
|
|
if ($result) {
|
2016-03-14 21:34:50 -04:00
|
|
|
$credentials = get_credentials();
|
2013-12-22 20:55:53 -05:00
|
|
|
|
2015-01-22 23:12:06 +01:00
|
|
|
if ($credentials && $credentials['username'] === $values['username'] && password_verify($values['password'], $credentials['password'])) {
|
2016-03-14 21:34:50 -04:00
|
|
|
set_last_login();
|
2015-01-22 23:12:06 +01:00
|
|
|
$_SESSION['loggedin'] = true;
|
2014-05-26 20:47:40 -04:00
|
|
|
$_SESSION['config'] = Config\get_all();
|
|
|
|
|
|
|
|
// Setup the remember me feature
|
|
|
|
if (! empty($values['remember_me'])) {
|
2015-01-22 23:12:06 +01:00
|
|
|
$cookie = RememberMe\create(DatabaseModel\select(), $values['username'], Config\get_ip_address(), Config\get_user_agent());
|
|
|
|
RememberMe\write_cookie($cookie['token'], $cookie['sequence'], $cookie['expiration']);
|
2014-05-26 20:47:40 -04:00
|
|
|
}
|
2016-04-17 19:44:45 -04:00
|
|
|
} else {
|
2013-12-22 20:55:53 -05:00
|
|
|
$result = false;
|
|
|
|
$errors['login'] = t('Bad username or password');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return array(
|
|
|
|
$result,
|
|
|
|
$errors
|
|
|
|
);
|
|
|
|
}
|