
Adjusted log severity Improved error reporting Improved token handling Improved Config Form handling
137 lines
4.2 KiB
PHP
137 lines
4.2 KiB
PHP
<?php
|
|
/**
|
|
* functions/session.php
|
|
*
|
|
* This class is used for management of session data.
|
|
*
|
|
* @version 3.0
|
|
* @author Joey Kimsey <Joey@thetempusproject.com>
|
|
* @link https://TheTempusProject.com/Core
|
|
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
|
*/
|
|
namespace TheTempusProject\Bedrock\Functions;
|
|
|
|
use TheTempusProject\Canary\Bin\Canary as Debug;
|
|
|
|
class Session {
|
|
/**
|
|
* Checks if a session exists.
|
|
*
|
|
* @param {string} [$name] - The name of the session being checked for.
|
|
* @return {bool}
|
|
*/
|
|
public static function exists( $name ) {
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
session_start();
|
|
}
|
|
if ( ! Check::sessionName( $name ) ) {
|
|
return false;
|
|
}
|
|
$sessionName = DEFAULT_SESSION_PREFIX . $name;
|
|
if ( isset( $_SESSION[ $sessionName ] ) ) {
|
|
return true;
|
|
}
|
|
Debug::log( "Session::exists - Session not found: $sessionName" );
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Retrieves the value of a session if it exists
|
|
*
|
|
* @param {string} [$name] - The name of the session variable you are trying to retrieve.
|
|
* @return {string|bool} - Returns the data from the session or false if nothing is found..
|
|
*/
|
|
public static function get( $name ) {
|
|
if ( ! Check::sessionName( $name ) ) {
|
|
return false;
|
|
}
|
|
$sessionName = DEFAULT_SESSION_PREFIX . $name;
|
|
if ( self::exists( $name ) ) {
|
|
return $_SESSION[ $sessionName ];
|
|
}
|
|
Debug::log( "Session::get - Session not found: $sessionName" );
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Creates a session.
|
|
*
|
|
* @param {string} [$name] - Session name.
|
|
* @param {string} [$data] - Session data.
|
|
* @return {bool}
|
|
*/
|
|
public static function put( $name, $data ) {
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
session_start();
|
|
}
|
|
if ( ! Check::sessionName( $name ) ) {
|
|
return false;
|
|
}
|
|
$sessionName = DEFAULT_SESSION_PREFIX . $name;
|
|
$_SESSION[ $sessionName ] = $data;
|
|
Debug::log( "Session::get - Created/Updated: $sessionName" );
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Deletes the specified session.
|
|
*
|
|
* @param {string} [$name] - The name of the session to be destroyed.
|
|
* @return {bool}
|
|
*/
|
|
public static function delete( $name ) {
|
|
if ( ! Check::sessionName( $name ) ) {
|
|
return false;
|
|
}
|
|
$sessionName = DEFAULT_SESSION_PREFIX . $name;
|
|
if ( self::exists( $name ) ) {
|
|
unset( $_SESSION[$sessionName] );
|
|
Debug::info( "Session::delete - Deleted $sessionName" );
|
|
return true;
|
|
}
|
|
Debug::error( "Session::delete - Session not found." );
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Intended as a self-destruct session. If the specified session does not
|
|
* exist, it is created. If the specified session does exist, it will be
|
|
* destroyed and returned.
|
|
*
|
|
* @param {string} [$name] - Session name to be created or checked
|
|
* @param {string} [$data] - The string to be used if session needs to be created. (optional)
|
|
* @return bool|string - Returns bool if creating, and a string if the check is successful.
|
|
*/
|
|
public static function checkFlash( $name ) {
|
|
if ( ! Check::sessionName( $name ) ) {
|
|
return false;
|
|
}
|
|
if ( self::exists( $name ) ) {
|
|
Debug::log("Session::flash - Exists");
|
|
$session = self::get( $name );
|
|
self::delete( $name );
|
|
return $session;
|
|
}
|
|
return;
|
|
}
|
|
|
|
public static function flash( $name, $data = null ) {
|
|
if ( ! Check::sessionName( $name ) ) {
|
|
return false;
|
|
}
|
|
if ( ! empty( $data ) ) {
|
|
self::put( $name, $data );
|
|
Debug::log("Session::flash - Session created.");
|
|
return true;
|
|
}
|
|
if ( self::exists( $name ) ) {
|
|
Debug::log("Session::flash - Exists");
|
|
$session = self::get( $name );
|
|
self::delete( $name );
|
|
return $session;
|
|
}
|
|
Debug::error("Session::flash - null return");
|
|
return;
|
|
}
|
|
}
|