2016-08-07 14:23:02 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Holds information about the currently active session
|
|
|
|
* @package Sakura
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Sakura;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Information about the current active user and session.
|
|
|
|
* @package Sakura
|
|
|
|
* @author Julian van de Groep <me@flash.moe>
|
|
|
|
*/
|
|
|
|
class CurrentSession
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The user object of the currently active user.
|
|
|
|
* @var User
|
|
|
|
*/
|
|
|
|
public static $user = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The currently active session object.
|
|
|
|
* @var Session
|
|
|
|
*/
|
|
|
|
public static $session = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepare the current session backend.
|
|
|
|
* @param int $user
|
|
|
|
* @param string $session
|
|
|
|
* @param string $ip
|
|
|
|
*/
|
2016-12-04 16:33:52 +00:00
|
|
|
public static function start(int $user, string $session, string $ip): void
|
2016-08-07 14:23:02 +00:00
|
|
|
{
|
|
|
|
// Check if a PHP session was already started and if not start one
|
|
|
|
if (session_status() !== PHP_SESSION_ACTIVE) {
|
|
|
|
session_start();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a session object
|
|
|
|
self::$session = new Session($session);
|
|
|
|
|
|
|
|
// Create a user object
|
|
|
|
$user = User::construct($user);
|
|
|
|
|
|
|
|
// Check if the session exists and check if the user is activated
|
|
|
|
if (self::$session->validate($user->id, $ip)
|
2016-11-01 21:14:02 +00:00
|
|
|
&& $user->activated) {
|
2016-08-07 14:23:02 +00:00
|
|
|
// Assign the user object
|
|
|
|
self::$user = $user;
|
|
|
|
} else {
|
|
|
|
self::$user = User::construct(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop the current session
|
|
|
|
*/
|
2016-12-04 16:33:52 +00:00
|
|
|
public static function stop(): void
|
2016-08-07 14:23:02 +00:00
|
|
|
{
|
|
|
|
self::$session->delete();
|
|
|
|
session_regenerate_id(true);
|
|
|
|
session_destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new sakura session.
|
|
|
|
* @param int $user
|
|
|
|
* @param string $ip
|
|
|
|
* @param string $country
|
|
|
|
* @param string $agent
|
|
|
|
* @param bool $remember
|
|
|
|
* @param int $length
|
|
|
|
* @return Session
|
|
|
|
*/
|
2016-12-04 16:33:52 +00:00
|
|
|
public static function create(int $user, string $ip, string $country, string $agent = null, bool $remember = false, int $length = 604800)
|
2016-08-07 14:23:02 +00:00
|
|
|
{
|
|
|
|
return Session::create($user, $ip, $country, $agent, $remember, $length);
|
|
|
|
}
|
|
|
|
}
|