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

+42 -31
View File
@@ -11,74 +11,85 @@
namespace IPS;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use IPS\Archive\Exception;
use OutOfRangeException;
use function defined;
use function dirname;
use function fclose;
use function file_put_contents;
use function fopen;
use function function_exists;
use function fwrite;
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;
}
/**
* @brief Archive Class
*/
abstract class _Archive
abstract class Archive
{
/**
* Create object from local file
*
* @param string $path Path to archive file
* @param string $containerName The root folder name which should be ignored (with trailing slash)
* @return \IPS\Archive
* @return Archive
*/
public static function fromLocalFile( $path, $containerName = '' )
public static function fromLocalFile( string $path, string $containerName = '' ) : static
{
return new static;
}
/**
* Number of files
*
* @return int
*/
abstract public function numberOfFiles();
abstract public function numberOfFiles(): int;
/**
* Get file name
*
* @param int $i File number
* @return string
* @throws \OutOfRangeException
* @throws OutOfRangeException
*/
abstract public function getFileName( $i );
abstract public function getFileName( int $i ): string;
/**
* Get file contents
*
* @param int $i File number
* @return string
* @throws \OutOfRangeException
* @throws OutOfRangeException
*/
abstract public function getFileContents( $i );
abstract public function getFileContents( int $i ): string;
/**
* @brief The root folder name which should be ignored (with trailing slash)
*/
protected $containerName = '';
protected string $containerName = '';
/**
* @brief Ignore hidden files?
*/
public $ignoreHiddenFiles = TRUE;
public bool $ignoreHiddenFiles = TRUE;
/**
* Extract
*
* @param string $destination Destination directory
* @param int|NULL $limit Number of files to extract
* @param int $offset Offset
* @param \IPS\Ftp|NULL $ftp If provided, the files will be extracted using FTP, otherwise will attempt to write manually
* @param string $destination Destination directory
* @param int|null $limit Number of files to extract
* @param int $offset Offset
* @param Ftp|NULL $ftp If provided, the files will be extracted using FTP, otherwise will attempt to write manually
* @return bool If true, all files were extracted, if false, there is more to extract
*/
public function extract( $destination, $limit=NULL, $offset=0, \IPS\Ftp $ftp = NULL )
public function extract( string $destination, int $limit=NULL, int $offset=0, Ftp $ftp = NULL ): bool
{
$done = 0;
while ( $limit === NULL or $limit > $done ) // OutOfRangeException will break if $limit is NULL
@@ -89,11 +100,11 @@ abstract class _Archive
if ( $path and mb_substr( $path, -1 ) !== '/' and ( !$this->ignoreHiddenFiles or ( mb_substr( $path, 0, 1 ) !== '.' and mb_substr( $path, 0, 9 ) !== '__MACOSX/' ) ) )
{
/* Create a directory if needed */
$dir = \dirname( $path );
$dir = dirname( $path );
$directories = array( $dir );
while ( $dir != '.' )
{
$dir = \dirname( $dir );
$dir = dirname( $dir );
if ( $dir != '.' )
{
$directories[] = $dir;
@@ -109,7 +120,7 @@ abstract class _Archive
}
else
{
@mkdir( $destination . '/' . $dir, \IPS\FOLDER_PERMISSION_NO_WRITE );
@mkdir( $destination . '/' . $dir, FOLDER_PERMISSION_NO_WRITE);
}
}
}
@@ -118,8 +129,8 @@ abstract class _Archive
$contents = $this->getFileContents( $offset + $done );
if ( $ftp )
{
$tmpFile = tempnam( \IPS\TEMP_DIRECTORY, 'IPS' );
\file_put_contents( $tmpFile, $contents );
$tmpFile = tempnam( TEMP_DIRECTORY, 'IPS' );
file_put_contents( $tmpFile, $contents );
$ftp->upload( $path, $tmpFile );
@@ -130,21 +141,21 @@ abstract class _Archive
/* Determine if the file exists (we'll want to know this later) */
$fileExists = file_exists( $destination . '/' . $path );
$fh = @\fopen( $destination . '/' . $path, 'w+' );
$fh = @fopen( $destination . '/' . $path, 'w+' );
if ( $fh === FALSE )
{
throw new \IPS\Archive\Exception( 'Error: ' . ( \IPS\IPS::$lastError ? \IPS\IPS::$lastError->getMessage() : NULL ) . ', Path:' . $destination . '/' . $path, \IPS\Archive\Exception::COULD_NOT_WRITE );
throw new Exception( 'Error: ' . ( IPS::$lastError?->getMessage() ) . ', Path:' . $destination . '/' . $path, Exception::COULD_NOT_WRITE );
}
if ( @\fwrite( $fh, $contents ) === FALSE )
if ( @fwrite( $fh, $contents ) === FALSE )
{
throw new \IPS\Archive\Exception( 'Error: ' . ( \IPS\IPS::$lastError ? \IPS\IPS::$lastError->getMessage() : NULL ) . ', Path:' . $destination . '/' . $path, \IPS\Archive\Exception::COULD_NOT_WRITE );
throw new Exception( 'Error: ' . ( IPS::$lastError?->getMessage() ) . ', Path:' . $destination . '/' . $path, Exception::COULD_NOT_WRITE );
}
else
{
/* If the file existed before we started, we should clear it from opcache if opcache is enabled */
if( $fileExists )
{
if ( \function_exists( 'opcache_invalidate' ) )
if ( function_exists( 'opcache_invalidate' ) )
{
@opcache_invalidate( $destination . '/' . $path );
}
@@ -152,16 +163,16 @@ abstract class _Archive
/* Otherwise, we should set file permissions on the file if it's brand new in case server defaults to something odd */
else
{
@chmod( $destination . '/' . $path, \IPS\FILE_PERMISSION_NO_WRITE );
@chmod( $destination . '/' . $path, FILE_PERMISSION_NO_WRITE);
}
}
@\fclose( $fh );
@fclose( $fh );
}
}
$done++;
}
catch ( \OutOfRangeException $e )
catch ( OutOfRangeException $e )
{
return TRUE;
}
+7 -3
View File
@@ -11,16 +11,20 @@
namespace IPS\Archive;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use RuntimeException;
use function defined;
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;
}
/**
* Archive Exception Class
*/
class _Exception extends \RuntimeException
class Exception extends RuntimeException
{
/**
* @brief Could not open archive
+27 -19
View File
@@ -11,9 +11,17 @@
namespace IPS\Archive\Zip;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use IPS\Archive;
use IPS\Archive\Zip;
use OutOfRangeException;
use RuntimeException;
use function defined;
use const PCLZIP_OPT_EXTRACT_AS_STRING;
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;
}
@@ -22,25 +30,25 @@ if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
/**
* @brief PclZip Zip Class
*/
class _PclZip extends \IPS\Archive\Zip
class PclZip extends Zip
{
/**
* @brief PclZip Object
*/
protected $zipArchive;
protected ?\PclZip $zipArchive;
/**
* @brief Files
*/
protected $files;
protected array|int|null $files = null;
/**
* Create object from local file
*
* @param string $path Path to archive file
* @return \IPS\Archive
* @param string $path Path to archive file
* @return Archive
*/
public static function _fromLocalFile( $path )
public static function _fromLocalFile( string $path ): Archive
{
$object = new static;
$object->zipArchive = new \PclZip( $path );
@@ -52,7 +60,7 @@ class _PclZip extends \IPS\Archive\Zip
*
* @return int
*/
public function numberOfFiles()
public function numberOfFiles(): int
{
$properties = $this->zipArchive->properties();
return $properties['nb'];
@@ -61,12 +69,12 @@ class _PclZip extends \IPS\Archive\Zip
/**
* Get file name
*
* @param int $i File number
* @param int $i File number
* @return string
* @throws \RuntimeException
* @throws \OutOfRangeException
* @throws RuntimeException
* @throws OutOfRangeException
*/
public function getFileName( $i )
public function getFileName( int $i ): string
{
if ( $this->files === NULL )
{
@@ -76,7 +84,7 @@ class _PclZip extends \IPS\Archive\Zip
/* pclZip returns 0 on an unrecoverable failure */
if ( $this->files === 0 )
{
throw new \RuntimeException;
throw new RuntimeException;
}
if ( isset( $this->files[ $i ] ) )
@@ -85,20 +93,20 @@ class _PclZip extends \IPS\Archive\Zip
}
else
{
throw new \OutOfRangeException;
throw new OutOfRangeException;
}
}
/**
* Get file contents
*
* @param int $i File number
* @param int $i File number
* @return string
* @throws \OutOfRangeException
* @throws OutOfRangeException
*/
public function getFileContents( $i )
public function getFileContents( int $i ): string
{
$content = $this->zipArchive->extractByIndex( $i, \PCLZIP_OPT_EXTRACT_AS_STRING );
$content = $this->zipArchive->extractByIndex( $i, PCLZIP_OPT_EXTRACT_AS_STRING );
return $content[0]['content'];
}
}
+13 -8
View File
@@ -11,27 +11,32 @@
namespace IPS\Archive;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use IPS\Archive;
use function defined;
use function extension_loaded;
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;
}
/**
* @brief Zip Archive Class
*/
abstract class _Zip extends \IPS\Archive
abstract class Zip extends Archive
{
/**
* Create object from local file
*
* @param string $path Path to archive file
* @param string $containerName The root folder name which should be ignored (with trailing slash)
* @return \IPS\Archive
* @param string $path Path to archive file
* @param string $containerName The root folder name which should be ignored (with trailing slash)
* @return static
*/
public static function fromLocalFile( $path, $containerName = '' )
public static function fromLocalFile( string $path, string $containerName = '' ): static
{
if ( \extension_loaded( 'Zip' ) )
if ( extension_loaded( 'Zip' ) )
{
$object = Zip\ZipArchive::_fromLocalFile( $path );
}
+23 -16
View File
@@ -11,36 +11,43 @@
namespace IPS\Archive\Zip;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use IPS\Archive;
use IPS\Archive\Exception;
use IPS\Archive\Zip;
use OutOfRangeException;
use function defined;
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;
}
/**
* @brief ZipArchive Zip Class
*/
class _ZipArchive extends \IPS\Archive\Zip
class ZipArchive extends Zip
{
/**
* @brief ZipArchive Object
*/
protected $zipArchive;
protected ?\ZipArchive $zipArchive;
/**
* Create object from local file
*
* @param string $path Path to archive file
* @return \IPS\Archive
* @param string $path Path to archive file
* @return Archive
*/
public static function _fromLocalFile( $path )
public static function _fromLocalFile( string $path ): Archive
{
$object = new static;
$object->zipArchive = new \ZipArchive;
$open = $object->zipArchive->open( $path );
if ( $open !== TRUE )
{
throw new \IPS\Archive\Exception( $open, \IPS\Archive\Exception::COULD_NOT_OPEN );
throw new Exception( $open, Exception::COULD_NOT_OPEN );
}
return $object;
}
@@ -50,7 +57,7 @@ class _ZipArchive extends \IPS\Archive\Zip
*
* @return int
*/
public function numberOfFiles()
public function numberOfFiles(): int
{
return $this->zipArchive->numFiles;
}
@@ -58,16 +65,16 @@ class _ZipArchive extends \IPS\Archive\Zip
/**
* Get file name
*
* @param int $i File number
* @param int $i File number
* @return string
* @throws \OutOfRangeException
* @throws OutOfRangeException
*/
public function getFileName( $i )
public function getFileName( int $i ): string
{
$info = $this->zipArchive->statIndex( $i );
if ( $info === FALSE )
{
throw new \OutOfRangeException;
throw new OutOfRangeException;
}
return mb_substr( $info['name'], 0, mb_strlen( $this->containerName ) ) === $this->containerName ? mb_substr( $info['name'], mb_strlen( $this->containerName ) ) : $info['name'];
@@ -76,11 +83,11 @@ class _ZipArchive extends \IPS\Archive\Zip
/**
* Get file contents
*
* @param int $i File number
* @param int $i File number
* @return string
* @throws \OutOfRangeException
* @throws OutOfRangeException
*/
public function getFileContents( $i )
public function getFileContents( int $i ): string
{
return $this->zipArchive->getFromIndex( $i );
}