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

+76 -56
View File
@@ -11,16 +11,36 @@
namespace IPS\File;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use BadFunctionCallException;
use DomainException;
use InvalidArgumentException;
use IPS\File;
use IPS\Ftp as FtpClass;
use IPS\Ftp\Exception;
use IPS\Ftp\RecursiveDirectoryFtpIterator;
use IPS\Ftp\Sftp as SftpClass;
use IPS\Http\Url;
use IPS\Member;
use LogicException;
use RecursiveIteratorIterator;
use function count;
use function defined;
use function file_put_contents;
use function function_exists;
use function in_array;
use const IPS\TEMP_DIRECTORY;
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 Handler: FTP
*/
class _Ftp extends \IPS\File
class Ftp extends File
{
/* !ACP Configuration */
@@ -30,7 +50,7 @@ class _Ftp extends \IPS\File
* @param array $configuration Configuration if editing a setting, or array() if creating a setting.
* @return array
*/
public static function settings( $configuration=array() )
public static function settings( array $configuration=array() ) : array
{
return array(
'ftp_details' => array( 'type' => 'Ftp', 'options' => array( 'validate' => FALSE ) ),
@@ -43,47 +63,47 @@ class _Ftp extends \IPS\File
*
* @param array $values The submitted values
* @return void
* @throws \LogicException
* @throws LogicException
*/
public static function testSettings( &$values )
public static function testSettings( array &$values ) : void
{
if ( !\function_exists( 'ftp_connect' ) )
if ( !function_exists( 'ftp_connect' ) )
{
throw new \BadFunctionCallException( 'ftp_err_no_ext' );
throw new BadFunctionCallException( 'ftp_err_no_ext' );
}
elseif ( $values['ftp_details']['protocol'] === 'ssl_ftp' and !\function_exists( 'ftp_ssl_connect' ) )
elseif ( $values['ftp_details']['protocol'] === 'ssl_ftp' and !function_exists( 'ftp_ssl_connect' ) )
{
throw new \BadFunctionCallException( 'ftp_err_no_ssl' );
throw new BadFunctionCallException( 'ftp_err_no_ssl' );
}
elseif ( $values['ftp_details']['protocol'] === 'sftp' and !\function_exists( 'ssh2_connect' ) )
elseif ( $values['ftp_details']['protocol'] === 'sftp' and !function_exists( 'ssh2_connect' ) )
{
throw new \BadFunctionCallException( 'ftp_err_no_sftp' );
throw new BadFunctionCallException( 'ftp_err_no_sftp' );
}
$values['url'] = rtrim( $values['url'], '/' );
if ( filter_var( $values['url'], FILTER_VALIDATE_URL ) === false )
{
throw new \DomainException( \IPS\Member::loggedIn()->language()->addToStack( 'url_is_not_real', FALSE, array( 'sprintf' => array( $values['url'] ) ) ) );
throw new DomainException( Member::loggedIn()->language()->addToStack( 'url_is_not_real', FALSE, array( 'sprintf' => array( $values['url'] ) ) ) );
}
try
{
if ( $values['ftp_details']['protocol'] == 'sftp' )
{
$ftp = new \IPS\Ftp\Sftp( $values['ftp_details']['server'], $values['ftp_details']['un'], $values['ftp_details']['pw'], $values['ftp_details']['port'] );
$ftp = new SftpClass( $values['ftp_details']['server'], $values['ftp_details']['un'], $values['ftp_details']['pw'], $values['ftp_details']['port'] );
}
else
{
$ftp = new \IPS\Ftp( $values['ftp_details']['server'], $values['ftp_details']['un'], $values['ftp_details']['pw'], $values['ftp_details']['port'], ( $values['ftp_details']['protocol'] == 'ssl_ftp' ) );
$ftp = new FtpClass( $values['ftp_details']['server'], $values['ftp_details']['un'], $values['ftp_details']['pw'], $values['ftp_details']['port'], ( $values['ftp_details']['protocol'] == 'ssl_ftp' ) );
}
$ftp->chdir( $values['ftp_details']['path'] );
$filename = md5( mt_rand() ) . '.ips.txt';
$tmpFileName = tempnam( \IPS\TEMP_DIRECTORY, 'IPS' );
\file_put_contents( $tmpFileName, 'OK' );
$tmpFileName = tempnam( TEMP_DIRECTORY, 'IPS' );
file_put_contents( $tmpFileName, 'OK' );
$ftp->upload( $filename, $tmpFileName );
$result = \IPS\Http\Url::external( $values['url'] . '/' . $filename)->request()->get();
$result = Url::external( $values['url'] . '/' . $filename)->request()->get();
$ftp->delete( $filename );
unlink( $tmpFileName );
@@ -91,17 +111,17 @@ class _Ftp extends \IPS\File
{
if ( $result->httpResponseCode == 200 )
{
throw new \InvalidArgumentException( \IPS\Member::loggedIn()->language()->addToStack( 'file_storage_test_ftp_unexpected_response', FALSE, array( 'sprintf' => array( $values['ftp_details']['server'] ) ) ) );
throw new InvalidArgumentException( Member::loggedIn()->language()->addToStack( 'file_storage_test_ftp_unexpected_response', FALSE, array( 'sprintf' => array( $values['ftp_details']['server'] ) ) ) );
}
else
{
throw new \InvalidArgumentException( \IPS\Member::loggedIn()->language()->addToStack( 'file_storage_test_error_ftp', FALSE, array( 'sprintf' => array( $values['ftp_details']['server'], $result->httpResponseCode ) ) ) );
throw new InvalidArgumentException( Member::loggedIn()->language()->addToStack( 'file_storage_test_error_ftp', FALSE, array( 'sprintf' => array( $values['ftp_details']['server'], $result->httpResponseCode ) ) ) );
}
}
}
catch ( \IPS\Ftp\Exception $e )
catch ( Exception $e )
{
throw new \DomainException( 'ftp_err-' . $e->getMessage() );
throw new DomainException( 'ftp_err-' . $e->getMessage() );
}
}
@@ -111,9 +131,9 @@ class _Ftp extends \IPS\File
* @param array $settings Configuration settings
* @return string
*/
public static function displayName( $settings )
public static function displayName( array $settings ) : string
{
return \IPS\Member::loggedIn()->language()->addToStack( 'filehandler_display_name', FALSE, array( 'sprintf' => array( \IPS\Member::loggedIn()->language()->addToStack('filehandler__Ftp'), ( !empty( $settings['ftp_details']['path'] ) ) ? $settings['ftp_details']['server'] . " (" . $settings['ftp_details']['path'] . ")" : $settings['ftp_details']['server'] ) ) );
return Member::loggedIn()->language()->addToStack( 'filehandler_display_name', FALSE, array( 'sprintf' => array( Member::loggedIn()->language()->addToStack('filehandler__Ftp'), ( !empty( $settings['ftp_details']['path'] ) ) ? $settings['ftp_details']['server'] . " (" . $settings['ftp_details']['path'] . ")" : $settings['ftp_details']['server'] ) ) );
}
/* !File Handling */
@@ -124,7 +144,7 @@ class _Ftp extends \IPS\File
* @param array $configuration Storage configuration
* @return void
*/
public function __construct( $configuration )
public function __construct( array $configuration )
{
$this->container = 'monthly_' . date( 'm' ) . '_' . date( 'Y' );
parent::__construct( $configuration );
@@ -135,7 +155,7 @@ class _Ftp extends \IPS\File
*
* @return string
*/
public function baseUrl()
public function baseUrl() : string
{
return rtrim( $this->configuration['url'], '/' );
}
@@ -144,26 +164,26 @@ class _Ftp extends \IPS\File
* Save File
*
* @return void
* @throws \IPS\Ftp\Exception
* @throws Exception
*/
public function save()
public function save() : void
{
/* Get contents */
$contents = $this->contents();
/* Create a temporary file */
$tmpFileName = tempnam( \IPS\TEMP_DIRECTORY, 'IPS' );
\file_put_contents( $tmpFileName, $contents );
$tmpFileName = tempnam( TEMP_DIRECTORY, 'IPS' );
file_put_contents( $tmpFileName, $contents );
/* Move into the correct folder */
if( !\in_array( $this->container, $this->ftp()->ls() ) )
if( !in_array( $this->container, $this->ftp()->ls() ) )
{
/* Make dir */
$this->ftp()->mkdir( $this->container );
/* Stick an index.html file in */
$tmpIndexFile = tempnam( \IPS\TEMP_DIRECTORY, 'IPS' );
\file_put_contents( $tmpIndexFile, '' );
$tmpIndexFile = tempnam( TEMP_DIRECTORY, 'IPS' );
file_put_contents( $tmpIndexFile, '' );
$this->ftp()->upload( 'index.html', $tmpIndexFile );
unlink( $tmpIndexFile );
@@ -180,16 +200,16 @@ class _Ftp extends \IPS\File
unlink( $tmpFileName );
/* Set the URL */
$this->url = \IPS\Http\Url::createFromString( $this->fullyQualifiedUrl( "{$this->container}/{$this->filename}" ), FALSE );
$this->url = Url::createFromString( $this->fullyQualifiedUrl( "{$this->container}/{$this->filename}" ), FALSE );
}
/**
* Delete
*
* @return void
* @throws \IPS\Ftp\Exception
* @throws Exception
*/
public function delete()
public function delete() : void
{
/* Only issue delete command if the file (and container) exist...otherwise a delete command would fail but our intention is complete */
if( $this->checkExists( $this->container . '/' . $this->filename ) )
@@ -201,7 +221,7 @@ class _Ftp extends \IPS\File
$immediateCaller = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 1 );
$debug = array_map( function( $row ) {
return array_filter( $row, function( $key ) {
return \in_array( $key, array( 'class', 'function', 'line' ) );
return in_array( $key, array( 'class', 'function', 'line' ) );
}, ARRAY_FILTER_USE_KEY );
}, debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ) );
$this->log( "file_deletion", 'delete', $debug, 'log' );
@@ -212,14 +232,14 @@ class _Ftp extends \IPS\File
*
* @param string $container Key
* @return void
* @throws \IPS\Ftp\Exception
* @throws Exception
*/
public function deleteContainer( $container )
public function deleteContainer( string $container ) : void
{
/* Actually remove, checking if it exists before attempting */
if( $this->checkExists( $container ) )
{
$this->ftp()->rmdir( $container, TRUE );
$this->ftp()->rmdir($container, TRUE);
}
/* Log deletion request */
@@ -235,15 +255,15 @@ class _Ftp extends \IPS\File
* @param string $path Path to check
* @return bool
*/
protected function checkExists( $path )
protected function checkExists( string $path ) : bool
{
$pathBits = explode( '/', $path );
$reconstructed = array();
foreach( $pathBits as $piece )
{
$pathToCheck = \count( $reconstructed ) ? implode( '/', $reconstructed ) : '.';
if( $this->ftp()->ls( $pathToCheck ) === FALSE OR !\in_array( $piece, $this->ftp()->ls( $pathToCheck ) ) )
$pathToCheck = count( $reconstructed ) ? implode( '/', $reconstructed ) : '.';
if( $this->ftp()->ls( $pathToCheck ) === FALSE OR !in_array( $piece, $this->ftp()->ls( $pathToCheck ) ) )
{
return FALSE;
}
@@ -259,15 +279,15 @@ class _Ftp extends \IPS\File
/**
* @brief FTP Connection
*/
protected static $ftp = array();
protected static array $ftp = array();
/**
* Get FTP Connection
*
* @return \IPS\Ftp
* @throws \IPS\Ftp\Exception
* @return FtpClass
* @throws Exception
*/
protected function ftp()
protected function ftp() : FtpClass
{
$key = md5( json_encode( $this->configuration ) );
@@ -275,11 +295,11 @@ class _Ftp extends \IPS\File
{
if ( $this->configuration['ftp_details']['protocol'] == 'sftp' )
{
static::$ftp[ $key ] = new \IPS\Ftp\Sftp( $this->configuration['ftp_details']['server'], $this->configuration['ftp_details']['un'], $this->configuration['ftp_details']['pw'], $this->configuration['ftp_details']['port'] );
static::$ftp[ $key ] = new SftpClass( $this->configuration['ftp_details']['server'], $this->configuration['ftp_details']['un'], $this->configuration['ftp_details']['pw'], $this->configuration['ftp_details']['port'] );
}
else
{
static::$ftp[ $key ] = new \IPS\Ftp( $this->configuration['ftp_details']['server'], $this->configuration['ftp_details']['un'], $this->configuration['ftp_details']['pw'], $this->configuration['ftp_details']['port'], ( $this->configuration['ftp_details']['protocol'] == 'ssl_ftp' ) );
static::$ftp[ $key ] = new FtpClass( $this->configuration['ftp_details']['server'], $this->configuration['ftp_details']['un'], $this->configuration['ftp_details']['pw'], $this->configuration['ftp_details']['port'], ( $this->configuration['ftp_details']['protocol'] == 'ssl_ftp' ) );
}
static::$ftp[ $key ]->chdir( $this->configuration['ftp_details']['path'] );
@@ -294,7 +314,7 @@ class _Ftp extends \IPS\File
* @param array $engines All file storage engine extension objects
* @return array
*/
public function removeOrphanedFiles( $fileIndex, $engines )
public function removeOrphanedFiles( int $fileIndex, array $engines ) : array
{
/* Start off our results array */
$results = array(
@@ -306,7 +326,7 @@ class _Ftp extends \IPS\File
$checked = 0;
$skipped = 0;
$iterator = new \RecursiveIteratorIterator( new \IPS\Ftp\RecursiveDirectoryFtpIterator( $this->ftp(), $this->configuration['ftp_details']['path'] ) );
$iterator = new RecursiveIteratorIterator( new RecursiveDirectoryFtpIterator( $this->ftp(), $this->configuration['ftp_details']['path'] ) );
while( $iterator->valid() )
{
@@ -366,9 +386,9 @@ class _Ftp extends \IPS\File
/**
* Get filesize (in bytes)
*
* @return string|bool
* @return int|bool
*/
public function filesize()
public function filesize() : int|bool
{
if( $this->_cachedFilesize !== null )
{
@@ -390,7 +410,7 @@ class _Ftp extends \IPS\File
$this->_cachedFilesize = $size ?: parent::filesize();
}
catch( \IPS\Ftp\Exception $e )
catch( Exception $e )
{
$this->_cachedFilesize = FALSE;
}
@@ -406,10 +426,10 @@ class _Ftp extends \IPS\File
* @param int|null $throttle Throttle speed
* @return void
*/
public function printFile( $start=NULL, $length=NULL, $throttle=NULL )
public function printFile( ?int $start=NULL, ?int $length=NULL, ?int $throttle=NULL ) : void
{
/* Download the file locally */
$file = $this->ftp()->download( $this->configuration['ftp_details']['path'] . ( $this->container ? '/' . $this->container : '' ) . '/' . $this->filename, NULL, TRUE );
$file = $this->ftp()->download($this->configuration['ftp_details']['path'] . ($this->container ? '/' . $this->container : '') . '/' . $this->filename, NULL, TRUE);
/* Send the file */
$this->sendFile( $file, $start, $length, $throttle );