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
+42
-31
@@ -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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user