Version 5.0.0 beta 1
This commit is contained in:
1 parent
25ddeb65d6
commit
15c7beabc5
6736 files changed
+627902
-497943
No files matched your search
@@ -11,23 +11,38 @@
|
||||
namespace IPS\Data\Store;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use DirectoryIterator;
|
||||
use DomainException;
|
||||
use IPS\Data\Store;
|
||||
use IPS\Helpers\Form\Text;
|
||||
use IPS\Request;
|
||||
use ParseError;
|
||||
use UnderflowException;
|
||||
use function defined;
|
||||
use function file_put_contents;
|
||||
use function function_exists;
|
||||
use const IPS\IPS_FILE_PERMISSION;
|
||||
use const IPS\IPS_FOLDER_PERMISSION;
|
||||
use const IPS\SUITE_UNIQUE_KEY;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* File System Storage Class
|
||||
*/
|
||||
class _FileSystem extends \IPS\Data\Store
|
||||
class FileSystem extends Store
|
||||
{
|
||||
/**
|
||||
* Server supports this method?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function supported()
|
||||
public static function supported(): bool
|
||||
{
|
||||
return TRUE;
|
||||
}
|
||||
@@ -38,23 +53,23 @@ class _FileSystem extends \IPS\Data\Store
|
||||
* @param array $configuration Existing settings
|
||||
* @return array \IPS\Helpers\Form\FormAbstract elements
|
||||
*/
|
||||
public static function configuration( $configuration )
|
||||
public static function configuration( array $configuration ): array
|
||||
{
|
||||
return array(
|
||||
'path' => new \IPS\Helpers\Form\Text( 'datastore_filesystem_path', ( isset( $configuration['path'] ) ) ? rtrim( str_replace( '{root}', \IPS\ROOT_PATH, $configuration['path'] ), '/' ) : \IPS\ROOT_PATH . '/datastore', FALSE, array(), function( $val )
|
||||
'path' => new Text( 'datastore_filesystem_path', ( isset( $configuration['path'] ) ) ? rtrim( str_replace( '{root}', \IPS\ROOT_PATH, $configuration['path'] ), '/' ) : \IPS\ROOT_PATH . '/datastore', FALSE, array(), function( $val )
|
||||
{
|
||||
if ( \IPS\Request::i()->datastore_method === 'FileSystem' )
|
||||
if ( Request::i()->datastore_method === 'FileSystem' )
|
||||
{
|
||||
if ( !is_dir( $val ) and is_writable( $val ) )
|
||||
{
|
||||
mkdir( $val );
|
||||
chmod( $val, \IPS\IPS_FOLDER_PERMISSION );
|
||||
\file_put_contents( $val . '/index.html', '' );
|
||||
chmod( $val, IPS_FOLDER_PERMISSION );
|
||||
file_put_contents( $val . '/index.html', '' );
|
||||
}
|
||||
|
||||
if ( !is_dir( $val ) or !is_writable( $val ) )
|
||||
{
|
||||
throw new \DomainException( 'datastore_filesystem_path_err' );
|
||||
throw new DomainException( 'datastore_filesystem_path_err' );
|
||||
}
|
||||
}
|
||||
} )
|
||||
@@ -64,7 +79,7 @@ class _FileSystem extends \IPS\Data\Store
|
||||
/**
|
||||
* @brief Storage Path
|
||||
*/
|
||||
public $_path;
|
||||
public string $_path;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
@@ -72,7 +87,7 @@ class _FileSystem extends \IPS\Data\Store
|
||||
* @param array $configuration Configuration
|
||||
* @return void
|
||||
*/
|
||||
public function __construct( $configuration )
|
||||
public function __construct( array $configuration )
|
||||
{
|
||||
$this->_path = rtrim( str_replace( '{root}', \IPS\ROOT_PATH, $configuration['path'] ), '/' );
|
||||
|
||||
@@ -86,7 +101,7 @@ class _FileSystem extends \IPS\Data\Store
|
||||
/**
|
||||
* @brief Cache
|
||||
*/
|
||||
protected static $cache = array();
|
||||
protected static array $cache = array();
|
||||
|
||||
/**
|
||||
* Abstract Method: Get
|
||||
@@ -94,24 +109,24 @@ class _FileSystem extends \IPS\Data\Store
|
||||
* @param string $key Key
|
||||
* @return string Value from the _datastore
|
||||
*/
|
||||
public function get( $key )
|
||||
public function get( string $key ): string
|
||||
{
|
||||
if ( !isset( static::$cache[ $key ] ) )
|
||||
{
|
||||
if ( @filesize( $this->_path . '/' . $key . '.' . \IPS\SUITE_UNIQUE_KEY . '.php' ) )
|
||||
if ( @filesize( $this->_path . '/' . $key . '.' . SUITE_UNIQUE_KEY . '.php' ) )
|
||||
{
|
||||
try
|
||||
{
|
||||
static::$cache[ $key ] = require( $this->_path . '/' . $key . '.' . \IPS\SUITE_UNIQUE_KEY . '.php' );
|
||||
static::$cache[ $key ] = require( $this->_path . '/' . $key . '.' . SUITE_UNIQUE_KEY . '.php' );
|
||||
}
|
||||
catch ( \ParseError $e )
|
||||
catch ( ParseError $e )
|
||||
{
|
||||
throw new \UnderflowException;
|
||||
throw new UnderflowException;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \UnderflowException;
|
||||
throw new UnderflowException;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,11 +136,11 @@ class _FileSystem extends \IPS\Data\Store
|
||||
/**
|
||||
* Abstract Method: Set
|
||||
*
|
||||
* @param string $key Key
|
||||
* @param string $value Value
|
||||
* @param string $key Key
|
||||
* @param string $value Value
|
||||
* @return bool
|
||||
*/
|
||||
public function set( $key, $value )
|
||||
public function set( string $key, string $value ): bool
|
||||
{
|
||||
$contents = <<<CONTENTS
|
||||
<?php
|
||||
@@ -136,24 +151,24 @@ VALUE;
|
||||
|
||||
CONTENTS;
|
||||
|
||||
$result = (bool) @\file_put_contents( $this->_path . '/' . $key . '.' . \IPS\SUITE_UNIQUE_KEY . '.php', $contents, LOCK_EX );
|
||||
$result = (bool) @file_put_contents( $this->_path . '/' . $key . '.' . SUITE_UNIQUE_KEY . '.php', $contents, LOCK_EX );
|
||||
|
||||
/* Sometimes LOCK_EX is unavailable and throws file_put_contents(): Exclusive locks are not supported for this stream.
|
||||
While we would prefer an exclusive lock, it would be better to write the file if possible. */
|
||||
if( !$result )
|
||||
{
|
||||
@unlink( $this->_path . '/' . $key . '.' . \IPS\SUITE_UNIQUE_KEY . '.php' );
|
||||
$result = (bool) @\file_put_contents( $this->_path . '/' . $key . '.' . \IPS\SUITE_UNIQUE_KEY . '.php', $contents );
|
||||
@unlink( $this->_path . '/' . $key . '.' . SUITE_UNIQUE_KEY . '.php' );
|
||||
$result = (bool) @file_put_contents( $this->_path . '/' . $key . '.' . SUITE_UNIQUE_KEY . '.php', $contents );
|
||||
}
|
||||
|
||||
@chmod( $this->_path . '/' . $key . '.' . \IPS\SUITE_UNIQUE_KEY . '.php', \IPS\IPS_FILE_PERMISSION );
|
||||
@chmod( $this->_path . '/' . $key . '.' . SUITE_UNIQUE_KEY . '.php', IPS_FILE_PERMISSION );
|
||||
|
||||
static::$cache[ $key ] = $value;
|
||||
|
||||
/* Clear zend opcache if enabled */
|
||||
if ( \function_exists( 'opcache_invalidate' ) )
|
||||
if ( function_exists( 'opcache_invalidate' ) )
|
||||
{
|
||||
@opcache_invalidate( $this->_path . '/' . $key . '.' . \IPS\SUITE_UNIQUE_KEY . '.php' );
|
||||
@opcache_invalidate( $this->_path . '/' . $key . '.' . SUITE_UNIQUE_KEY . '.php' );
|
||||
}
|
||||
|
||||
return $result;
|
||||
@@ -165,7 +180,7 @@ CONTENTS;
|
||||
* @param string $key Key
|
||||
* @return bool
|
||||
*/
|
||||
public function exists( $key )
|
||||
public function exists( string $key ): bool
|
||||
{
|
||||
if( isset( static::$cache[ $key ] ) )
|
||||
{
|
||||
@@ -178,7 +193,7 @@ CONTENTS;
|
||||
$this->get( $key );
|
||||
return TRUE;
|
||||
}
|
||||
catch ( \UnderflowException $e )
|
||||
catch ( UnderflowException $e )
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
@@ -191,12 +206,12 @@ CONTENTS;
|
||||
* @param string $key Key
|
||||
* @return bool
|
||||
*/
|
||||
public function delete( $key )
|
||||
public function delete( string $key ): bool
|
||||
{
|
||||
$return = false;
|
||||
if ( file_exists( $this->_path . '/' . $key . '.' . \IPS\SUITE_UNIQUE_KEY . '.php' ) )
|
||||
if ( file_exists( $this->_path . '/' . $key . '.' . SUITE_UNIQUE_KEY . '.php' ) )
|
||||
{
|
||||
$return = @unlink( $this->_path . '/' . $key . '.' . \IPS\SUITE_UNIQUE_KEY . '.php' );
|
||||
$return = @unlink( $this->_path . '/' . $key . '.' . SUITE_UNIQUE_KEY . '.php' );
|
||||
}
|
||||
|
||||
if( array_key_exists( $key, static::$cache ) )
|
||||
@@ -205,9 +220,9 @@ CONTENTS;
|
||||
}
|
||||
|
||||
/* Clear zend opcache if enabled */
|
||||
if ( \function_exists( 'opcache_invalidate' ) )
|
||||
if ( function_exists( 'opcache_invalidate' ) )
|
||||
{
|
||||
@opcache_invalidate( $this->_path . '/' . $key . '.' . \IPS\SUITE_UNIQUE_KEY . '.php' );
|
||||
@opcache_invalidate( $this->_path . '/' . $key . '.' . SUITE_UNIQUE_KEY . '.php' );
|
||||
}
|
||||
|
||||
return $return;
|
||||
@@ -219,9 +234,9 @@ CONTENTS;
|
||||
* @param NULL|string $exclude Key to exclude (keep)
|
||||
* @return void
|
||||
*/
|
||||
public function clearAll( $exclude=NULL )
|
||||
public function clearAll( string $exclude=NULL ) : void
|
||||
{
|
||||
foreach ( new \DirectoryIterator( $this->_path ) as $file )
|
||||
foreach ( new DirectoryIterator( $this->_path ) as $file )
|
||||
{
|
||||
if ( !$file->isDot() and ( mb_substr( $file, -9 ) === '.ipsstore' or ( mb_substr( $file, -4 ) === '.php' and $file != $exclude . '.php' ) ) )
|
||||
{
|
||||
@@ -240,7 +255,7 @@ CONTENTS;
|
||||
/* Clear zend opcache if enabled - we call reset here since we're wiping multiple files
|
||||
and since this gets called as part of a general 'rebuild everything', a reset is
|
||||
probably a good idea anyway */
|
||||
if ( \function_exists( 'opcache_reset' ) )
|
||||
if ( function_exists( 'opcache_reset' ) )
|
||||
{
|
||||
@opcache_reset();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user