Files
IPB/system/Archive/Archive.php
T
2025-12-19 05:32:19 -08:00

117 lines
2.8 KiB
PHP

<?php
/**
* @brief Archive Class
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - SVN_YYYY Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Social Suite
* @since 28 Jul 2015
* @version SVN_VERSION_NUMBER
*/
namespace IPS;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* @brief Archive Class
*/
abstract class _Archive
{
/**
* @brief The root folder name which should be ignored (with trailing slash)
*/
protected $containerName = '';
/**
* 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
*/
public static function fromLocalFile( $path, $containerName = '' )
{
return new static;
}
/**
* Number of files
*
* @return int
*/
abstract public function numberOfFiles();
/**
* Get file name
*
* @param int $i File number
* @return string
* @throws \OutOfRangeException
*/
abstract public function getFileName( $i );
/**
* Get file contents
*
* @param int $i File number
* @return string
* @throws \OutOfRangeException
*/
abstract public function getFileContents( $i );
/**
* Extract
*
* @param string $destination Destination directory
* @param int|NULL $limit Number of files to extract
* @param int $offset Offset
* @retrun bool If true, all files were extracted, if false, there is more to extract
*/
public function extract( $destination, $limit=NULL, $offset=0 )
{
$done = 0;
while ( $limit === NULL or $limit > $done ) // OutOfRangeException will break if $limit is NULL
{
try
{
$path = $this->getFileName( $offset + $done );
if ( $path and mb_substr( $path, -1 ) !== '/' )
{
/* Create a directory if needed */
$dir = dirname( $path );
if ( !is_dir( $dir ) )
{
@mkdir( $dir, \IPS\FOLDER_PERMISSION_NO_WRITE );
}
/* Write contents */
$contents = $this->getFileContents( $offset + $done );
$fh = @\fopen( $destination . '/' . $path, 'w+' );
if ( $fh === FALSE )
{
throw new \IPS\Archive\Exception( '', \IPS\Archive\Exception::COULD_NOT_WRITE );
}
if ( @\fwrite( $fh, $contents ) === FALSE )
{
throw new \IPS\Archive\Exception( '', \IPS\Archive\Exception::COULD_NOT_WRITE );
}
@\fclose( $fh );
}
$done++;
}
catch ( \OutOfRangeException $e )
{
return TRUE;
}
}
return FALSE;
}
}