Add new classes and methods for session management

This commit is contained in:
netkas 2024-09-13 13:52:38 -04:00
parent b9c84aeb27
commit 764ec51fa4
12 changed files with 1026 additions and 28 deletions

View file

@ -0,0 +1,108 @@
<?php
namespace Socialbox\Classes;
use InvalidArgumentException;
use RuntimeException;
class Utilities
{
/**
* Decodes a JSON string into an associative array, throws an exception if the JSON is invalid
*
* @param string $json The JSON string to decode
* @return array The decoded associative array
* @throws InvalidArgumentException If the JSON is invalid
*/
public static function jsonDecode(string $json): array
{
$decoded = json_decode($json, true);
if (json_last_error() !== JSON_ERROR_NONE)
{
throw match (json_last_error())
{
JSON_ERROR_DEPTH => new InvalidArgumentException("JSON decoding failed: Maximum stack depth exceeded"),
JSON_ERROR_STATE_MISMATCH => new InvalidArgumentException("JSON decoding failed: Underflow or the modes mismatch"),
JSON_ERROR_CTRL_CHAR => new InvalidArgumentException("JSON decoding failed: Unexpected control character found"),
JSON_ERROR_SYNTAX => new InvalidArgumentException("JSON decoding failed: Syntax error, malformed JSON"),
JSON_ERROR_UTF8 => new InvalidArgumentException("JSON decoding failed: Malformed UTF-8 characters, possibly incorrectly encoded"),
default => new InvalidArgumentException("JSON decoding failed: Unknown error"),
};
}
return $decoded;
}
/**
* Encodes the given data in Base64.
*
* @param string $data The data to be encoded.
* @return string The Base64 encoded string.
* @throws InvalidArgumentException if the encoding fails.
*/
public static function base64encode(string $data): string
{
$encoded = base64_encode($data);
if (!$encoded)
{
throw new InvalidArgumentException('Failed to encode data in Base64');
}
return $encoded;
}
/**
* Decodes a Base64 encoded string.
*
* @param string $data The Base64 encoded data to be decoded.
* @return string The decoded data.
* @throws InvalidArgumentException If decoding fails.
*/
public static function base64decode(string $data): string
{
$decoded = base64_decode($data, true);
if ($decoded === false)
{
throw new InvalidArgumentException('Failed to decode data from Base64');
}
return $decoded;
}
/**
* Returns the request headers as an associative array
*
* @return array
*/
public static function getRequestHeaders(): array
{
// Check if function getallheaders() exists
if (function_exists('getallheaders'))
{
$headers = getallheaders();
}
else
{
// Fallback for servers where getallheaders() is not available
$headers = [];
foreach ($_SERVER as $key => $value)
{
if (str_starts_with($key, 'HTTP_'))
{
// Convert header names to the normal HTTP format
$headers[str_replace('_', '-', strtolower(substr($key, 5)))] = $value;
}
}
}
if($headers === false)
{
throw new RuntimeException('Failed to get request headers');
}
return $headers;
}
}