miniflux-legacy/app/models/remember_me.php
Frederic Guillot 82df35a59b Change the database structure to have a single database
This is a major change for the next release of Miniflux.

- There is now only one database that can supports multiple users
- There is no automated schema migration for this release
- A migration procedure is available in the ChangeLog file
2016-12-26 09:51:38 -05:00

156 lines
3.1 KiB
PHP

<?php
namespace Miniflux\Model\RememberMe;
use Miniflux\Session\SessionStorage;
use Miniflux\Helper;
use Miniflux\Model\User;
use PicoDb\Database;
const TABLE = 'remember_me';
const COOKIE_NAME = '_R_';
const EXPIRATION = 5184000;
function get_record($token, $sequence)
{
return Database::getInstance('db')
->table(TABLE)
->eq('token', $token)
->eq('sequence', $sequence)
->gt('expiration', time())
->findOne();
}
function authenticate()
{
$credentials = read_cookie();
if ($credentials !== false) {
$record = get_record($credentials['token'], $credentials['sequence']);
if ($record) {
$user = User\get_user_by_id($record['user_id']);
SessionStorage::getInstance()->setUser($user);
return true;
}
}
return false;
}
function destroy()
{
$credentials = read_cookie();
if ($credentials !== false) {
Database::getInstance('db')
->table(TABLE)
->eq('token', $credentials['token'])
->remove();
}
delete_cookie();
}
function create($user_id, $ip, $user_agent)
{
$token = hash('sha256', $user_id.$user_agent.$ip.Helper\generate_token());
$sequence = Helper\generate_token();
$expiration = time() + EXPIRATION;
cleanup();
Database::getInstance('db')
->table(TABLE)
->insert(array(
'user_id' => $user_id,
'ip' => $ip,
'user_agent' => $user_agent,
'token' => $token,
'sequence' => $sequence,
'expiration' => $expiration,
'date_creation' => time(),
));
return array(
'token' => $token,
'sequence' => $sequence,
'expiration' => $expiration,
);
}
function cleanup()
{
return Database::getInstance('db')
->table(TABLE)
->lt('expiration', time())
->remove();
}
function update($token)
{
$new_sequence = Helper\generate_token();
Database::getInstance('db')
->table(TABLE)
->eq('token', $token)
->update(array('sequence' => $new_sequence));
return $new_sequence;
}
function encode_cookie($token, $sequence)
{
return implode('|', array($token, $sequence));
}
function decode_cookie($value)
{
@list($token, $sequence) = explode('|', $value);
return array(
'token' => $token,
'sequence' => $sequence,
);
}
function has_cookie()
{
return ! empty($_COOKIE[COOKIE_NAME]);
}
function write_cookie($token, $sequence, $expiration)
{
setcookie(
COOKIE_NAME,
encode_cookie($token, $sequence),
$expiration,
BASE_URL_DIRECTORY,
null,
Helper\is_secure_connection(),
true
);
}
function read_cookie()
{
if (empty($_COOKIE[COOKIE_NAME])) {
return false;
}
return decode_cookie($_COOKIE[COOKIE_NAME]);
}
function delete_cookie()
{
setcookie(
COOKIE_NAME,
'',
time() - 3600,
BASE_URL_DIRECTORY,
null,
Helper\is_secure_connection(),
true
);
}