Version 5.0.0 beta 1

This commit is contained in:
Neo committed 2025-12-19 16:27:35 -08:00
1 parent 25ddeb65d6
commit 15c7beabc5
6736 files changed
+627902 -497943

No files matched your search

+58 -41
View File
@@ -11,31 +11,48 @@
namespace IPS\Data;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use IPS\Data\Cache\Exception;
use IPS\Data\Cache\None;
use IPS\DateTime;
use IPS\Settings;
use IPS\Text\Encrypt;
use OutOfRangeException;
use function count;
use function defined;
use function function_exists;
use function in_array;
use function is_array;
use const IPS\CACHE_CONFIG;
use const IPS\CACHE_METHOD;
use const IPS\CACHING_LOG;
use const IPS\TEST_CACHING;
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* Abstract Storage Class
*/
abstract class _Cache extends AbstractData
abstract class Cache extends AbstractData
{
/**
* @brief Instance
*/
protected static $instance;
protected static ?Cache $instance = NULL;
/**
* @brief Caches already retrieved this instance
*/
protected $cache = array();
protected array $cache = array();
/**
* @brief Log
*/
public $log = array();
public array $log = array();
/**
* Available Cache Store Methods
@@ -50,7 +67,7 @@ abstract class _Cache extends AbstractData
'Redis' => 'IPS\Data\Cache\Redis',
];
if( \IPS\TEST_CACHING )
if( TEST_CACHING )
{
$return['Test'] = 'IPS\Data\Cache\Test';
}
@@ -61,32 +78,32 @@ abstract class _Cache extends AbstractData
/**
* Get instance
*
* @return \IPS\Data\Cache
* @return Cache|null
*/
public static function i()
public static function i(): ?Cache
{
if( static::$instance === NULL )
{
$classname = '\IPS\Data\Cache\None';
if( isset( static::availableMethods()[ \IPS\CACHE_METHOD ] ) AND class_exists( static::availableMethods()[ \IPS\CACHE_METHOD ] ) )
if( isset( static::availableMethods()[ CACHE_METHOD ] ) AND class_exists( static::availableMethods()[ CACHE_METHOD ] ) )
{
$classname = static::availableMethods()[ \IPS\CACHE_METHOD ];
$classname = static::availableMethods()[ CACHE_METHOD ];
}
if ( $classname::supported() )
{
try
{
static::$instance = new $classname( json_decode( \IPS\CACHE_CONFIG, TRUE ) );
static::$instance = new $classname( json_decode( CACHE_CONFIG, TRUE ) );
}
catch( \IPS\Data\Cache\Exception $e )
catch( Exception $e )
{
static::$instance = new \IPS\Data\Cache\None( array() );
static::$instance = new None( array() );
}
}
else
{
static::$instance = new \IPS\Data\Cache\None( array() );
static::$instance = new None( array() );
}
}
@@ -98,7 +115,7 @@ abstract class _Cache extends AbstractData
*
* @return boolean
*/
public function checkKeys()
public function checkKeys(): bool
{
return true;
}
@@ -106,17 +123,17 @@ abstract class _Cache extends AbstractData
/**
* Store value using cache method if available or falling back to the database
*
* @param string $key Key
* @param string $key Key
* @param mixed $value Value
* @param \IPS\DateTime $expire Expiration if using database
* @param bool $fallback Use database if no caching method is available?
* @param DateTime $expire Expiration if using database
* @param bool $fallback Use database if no caching method is available?
* @return bool
*/
public function storeWithExpire( $key, $value, \IPS\DateTime $expire, $fallback=FALSE )
public function storeWithExpire( string $key, mixed $value, DateTime $expire, bool $fallback=FALSE ): bool
{
$value = array( 'value' => $value, 'expires' => $expire->getTimestamp() );
if ( \IPS\CACHING_LOG )
if ( CACHING_LOG )
{
$this->log[ microtime(true) ] = array( 'set', $key, json_encode( $value, JSON_PRETTY_PRINT ), var_export( debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ), TRUE ) );
}
@@ -135,20 +152,20 @@ abstract class _Cache extends AbstractData
/**
* Get value using cache method if available or falling back to the database
*
* @param string $key Key
* @param bool $fallback Use database if no caching method is available?
* @param string $key Key
* @param bool $fallback Use database if no caching method is available?
* @return mixed
* @throws \OutOfRangeException
* @throws OutOfRangeException
*/
public function getWithExpire( $key, $fallback=FALSE )
public function getWithExpire( string $key, bool $fallback=FALSE ): mixed
{
if ( !isset( $this->$key ) )
{
throw new \OutOfRangeException;
throw new OutOfRangeException;
}
$data = $this->$key;
if( \count( $data ) and isset( $data['value'] ) and isset( $data['expires'] ) )
if( count( $data ) and isset( $data['value'] ) and isset( $data['expires'] ) )
{
/* Is it expired? */
if( $data['expires'] AND time() < $data['expires'] )
@@ -158,13 +175,13 @@ abstract class _Cache extends AbstractData
else
{
unset( $this->$key );
throw new \OutOfRangeException;
throw new OutOfRangeException;
}
}
else
{
unset( $this->$key );
throw new \OutOfRangeException;
throw new OutOfRangeException;
}
}
@@ -173,13 +190,13 @@ abstract class _Cache extends AbstractData
*
* @return void
*/
public function clearAll()
public function clearAll() : void
{
/* cacheKeys stored md5 hashes of the correct value, and the cache
is only used if the value it returns matches the hash, so clearing
this out invalidates all caches, even if the caching engine
does not allow us to actually clear them */
\IPS\Data\Store::i()->cacheKeys = array();
Store::i()->cacheKeys = array();
}
/**
@@ -187,10 +204,10 @@ abstract class _Cache extends AbstractData
*
* @return string
*/
protected function _encryptionKey()
protected function _encryptionKey(): string
{
$password = \IPS\Settings::i()->sql_pass;
if ( \function_exists( 'openssl_digest' ) and \in_array( 'sha256', openssl_get_md_methods() ) )
$password = Settings::i()->sql_pass;
if ( function_exists( 'openssl_digest' ) and in_array( 'sha256', openssl_get_md_methods() ) )
{
$password = openssl_digest( $password, 'sha256', TRUE );
}
@@ -203,9 +220,9 @@ abstract class _Cache extends AbstractData
* @param mixed $value Value
* @return string
*/
protected function encode( $value )
protected function encode( mixed $value ): string
{
return \IPS\Text\Encrypt::fromPlaintext( json_encode( $value ) )->tag();
return Encrypt::fromPlaintext( json_encode( $value ) )->tag();
}
/**
@@ -214,22 +231,22 @@ abstract class _Cache extends AbstractData
* @param mixed $value Value
* @return mixed
*/
protected function decode( $value )
protected function decode( mixed $value ): mixed
{
return json_decode( \IPS\Text\Encrypt::fromTag( $value )->decrypt(), TRUE );
return json_decode( Encrypt::fromTag( $value )->decrypt(), TRUE );
}
/**
* Magic Method: Isset
*
* @param string $key Key
* @param string $key Key
* @return bool
*/
public function __isset( $key )
public function __isset( string $key ): bool
{
if ( parent::__isset( $key ) )
{
return ( \is_array( $this->$key ) or $this->$key === 0 ) ? true : (bool) $this->$key;
return ( is_array( $this->$key ) or $this->$key === 0 ) ? true : (bool) $this->$key;
}
return FALSE;
}