This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/app/CurrentSession.php

82 lines
2 KiB
PHP
Raw Permalink Normal View History

<?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
{
// 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)
&& $user->activated) {
// 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
{
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-07 23:34:59 +00:00
public static function create(int $user, string $ip, string $country, string $agent = null, int $length = 604800)
{
2016-12-07 23:34:59 +00:00
return Session::create($user, $ip, $country, $agent, $length);
}
}