99 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			99 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
namespace App\Core;
 | 
						|
 | 
						|
use \Exception;
 | 
						|
use \InvalidArgumentException;
 | 
						|
 | 
						|
/**
 | 
						|
 * Handles anything to do with sessions
 | 
						|
 */
 | 
						|
class Session
 | 
						|
{
 | 
						|
    public function __construct()
 | 
						|
    {
 | 
						|
        // start new session if there is none
 | 
						|
        if (session_status() === PHP_SESSION_NONE)
 | 
						|
        {
 | 
						|
            session_start();
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    public function has(string $key): bool
 | 
						|
    {
 | 
						|
        return array_key_exists($key, $_SESSION);
 | 
						|
    }
 | 
						|
 | 
						|
    public function get(string $key): mixed
 | 
						|
    {
 | 
						|
        if ($this->has($key))
 | 
						|
        {
 | 
						|
            return $_SESSION[$key];
 | 
						|
        }
 | 
						|
        return NULL;
 | 
						|
    }
 | 
						|
 | 
						|
    public function set(string $key, mixed $value): void 
 | 
						|
    {
 | 
						|
        $_SESSION[$key] = $value;
 | 
						|
    }
 | 
						|
 | 
						|
    public function remove(string $key): void
 | 
						|
    {
 | 
						|
        if ($this->has($key))
 | 
						|
        {
 | 
						|
            unset($_SESSION[$key]);
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    public function clear(): void
 | 
						|
    {
 | 
						|
        session_unset();
 | 
						|
    }
 | 
						|
 | 
						|
    // TODO: throwaway code; rewrite for readability and also implement proper flashing by removing messages after one request
 | 
						|
    public function flash(string $msg, string $type = 'info', bool $unsafe = FALSE): void
 | 
						|
    {
 | 
						|
        $types = [
 | 
						|
            "info",
 | 
						|
            "success",
 | 
						|
            "danger",
 | 
						|
            "warning"
 | 
						|
        ];
 | 
						|
        if (!in_array($type, $types)) {
 | 
						|
            throw new InvalidArgumentException("Flash type: \"$type\" does not exist");
 | 
						|
        }
 | 
						|
        $key = 'flashed_messages';
 | 
						|
        if (!$this->has($key))
 | 
						|
        {
 | 
						|
            $this->set($key, []);
 | 
						|
        }
 | 
						|
        if (count($this->get($key)) >= 100)
 | 
						|
        {
 | 
						|
            $this->remove($key);
 | 
						|
            throw new Exception('Too many flashed messages!');
 | 
						|
        }
 | 
						|
        $msgs = $this->get($key);
 | 
						|
        $msgs[] = [
 | 
						|
            "message"  => ($unsafe) ? $msg : htmlspecialchars($msg),
 | 
						|
            "type" => $type
 | 
						|
        ];
 | 
						|
        $this->set(
 | 
						|
            $key,
 | 
						|
            $msgs
 | 
						|
        );
 | 
						|
    }
 | 
						|
 | 
						|
    public function getFlashedMessages(): array
 | 
						|
    {
 | 
						|
        $key = 'flashed_messages';
 | 
						|
        if ($this->has($key))
 | 
						|
        {
 | 
						|
            $msgs = $this->get($key);
 | 
						|
            $this->remove($key);
 | 
						|
            return $msgs;
 | 
						|
        }
 | 
						|
        return [];
 | 
						|
    }
 | 
						|
    // END TODO;
 | 
						|
}
 |