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

+69 -63
View File
@@ -11,41 +11,47 @@
namespace IPS;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use FTP\Connection;
use IPS\Ftp\Exception;
use function defined;
use function function_exists;
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;
}
/**
* FTP Class
*/
class _Ftp
class Ftp
{
/**
* @brief Connection resource
*/
protected $ftp;
protected ?Connection $ftp = null;
/**
* Constructor
*
* @param string $host Hostname
* @param string $username Username
* @param string $password Password
* @param int $port Port
* @param bool $secure Use secure SSL-FTP connection?
* @param int $timeout Timeout (in seconds)
* @param string $host Hostname
* @param string $username Username
* @param string $password Password
* @param int $port Port
* @param bool $secure Use secure SSL-FTP connection?
* @param int $timeout Timeout (in seconds)
* @return void
* @throws \IPS\Ftp\Exception
* @throws Exception
*/
public function __construct( $host, $username, $password, $port=21, $secure=FALSE, $timeout=10 )
public function __construct( string $host, string $username, string $password, int $port=21, bool $secure = false, int $timeout=10 )
{
if ( $secure )
{
if( !\function_exists('ftp_ssl_connect') )
if( !function_exists('ftp_ssl_connect') )
{
throw new \IPS\Ftp\Exception( 'SSL_NOT_AVAILABLE' );
throw new Exception( 'SSL_NOT_AVAILABLE' );
}
$this->ftp = @ftp_ssl_connect( $host, $port, $timeout );
@@ -57,11 +63,11 @@ class _Ftp
if ( $this->ftp === FALSE )
{
throw new \IPS\Ftp\Exception( 'COULD_NOT_CONNECT' );
throw new Exception( 'COULD_NOT_CONNECT' );
}
if ( !@ftp_login( $this->ftp, $username, $password ) )
{
throw new \IPS\Ftp\Exception( 'COULD_NOT_LOGIN' );
throw new Exception( 'COULD_NOT_LOGIN' );
}
/* Typically if passive mode is required, ftp_nlist will return FALSE instead of an array */
@@ -87,15 +93,15 @@ class _Ftp
/**
* chdir
*
* @param string $dir Directory
* @param string $dir Directory
* @return void
* @throws \IPS\Ftp\Exception
* @throws Exception
*/
public function chdir( $dir )
public function chdir( string $dir ) : void
{
if ( !@ftp_chdir( $this->ftp, $dir ) )
{
throw new \IPS\Ftp\Exception( 'COULD_NOT_CHDIR' );
throw new Exception( 'COULD_NOT_CHDIR' );
}
}
@@ -103,38 +109,38 @@ class _Ftp
* cdup
*
* @return void
* @throws \IPS\Ftp\Exception
* @throws Exception
*/
public function cdup()
public function cdup() : void
{
if ( !@ftp_cdup( $this->ftp ) )
{
throw new \IPS\Ftp\Exception( 'COULD_NOT_CDUP' );
throw new Exception( 'COULD_NOT_CDUP' );
}
}
/**
* mkdir
*
* @param string $dir Directory
* @param string $dir Directory
* @return void
* @throws \IPS\Ftp\Exception
* @throws Exception
*/
public function mkdir( $dir )
public function mkdir( string $dir ) : void
{
if ( !@ftp_mkdir( $this->ftp, $dir ) )
{
throw new \IPS\Ftp\Exception( 'COULD_NOT_MKDIR' );
throw new Exception( 'COULD_NOT_MKDIR' );
}
}
/**
* ls
*
* @param string $path Argument to pass to ftp_nlist
* @param string $path Argument to pass to ftp_nlist
* @return array|bool
*/
public function ls( $path = '.' )
public function ls( string $path = '.' ): bool|array
{
return ftp_nlist( $this->ftp, $path );
}
@@ -142,11 +148,11 @@ class _Ftp
/**
* Raw list
*
* @param string $path Argument to pass to ftp_nlist
* @param bool $recursive Whether or not to list recursively
* @param string $path Argument to pass to ftp_nlist
* @param bool $recursive Whether or not to list recursively
* @return array
*/
public function rawList( $path = '.', $recursive = FALSE )
public function rawList( string $path = '.', bool $recursive = false ): array
{
return ftp_rawlist( $this->ftp, $path, $recursive );
}
@@ -154,40 +160,40 @@ class _Ftp
/**
* Upload File
*
* @param string $filename Filename to use
* @param string $file Path to local file
* @param string $filename Filename to use
* @param string $file Path to local file
* @return void
* @throws \IPS\Ftp\Exception
* @throws Exception
*/
public function upload( $filename, $file )
public function upload( string $filename, string $file ) : void
{
if ( !@ftp_put( $this->ftp, $filename, $file, FTP_BINARY ) )
{
throw new \IPS\Ftp\Exception( 'COULD_NOT_UPLOAD' );
throw new Exception( 'COULD_NOT_UPLOAD' );
}
}
/**
* Download File
*
* @param string $filename The file to download
* @param string|null $target Location to save downloaded file or NULL to return contents
* @param bool $returnPath Return the path to the downloaded file instead of the contents
* @return string File contents
* @throws \IPS\Ftp\Exception
* @param string $filename The file to download
* @param string|null $target Location to save downloaded file or NULL to return contents
* @param bool $returnPath Return the path to the downloaded file instead of the contents
* @return string|null File contents
* @throws Exception
*/
public function download( $filename, $target=NULL, $returnPath=FALSE )
public function download( string $filename, ?string $target=NULL, bool $returnPath = false ): ?string
{
$temp = FALSE;
if ( $target === NULL )
{
$temp = TRUE;
$target = tempnam( \IPS\TEMP_DIRECTORY, 'IPS' );
$target = tempnam( TEMP_DIRECTORY, 'IPS' );
}
if ( !@ftp_get( $this->ftp, $target, $filename, FTP_BINARY ) )
{
throw new \IPS\Ftp\Exception( 'COULD_NOT_DOWNLOAD' );
throw new Exception( 'COULD_NOT_DOWNLOAD' );
}
/* We use this to avoid out of memory errors - just return path */
@@ -209,41 +215,41 @@ class _Ftp
/**
* CHMOD
*
* @param string $filename The file to CHMOD
* @param int $mode Mode (in octal form)
* @return @e void
* @param string $filename The file to CHMOD
* @param int $mode Mode (in octal form)
* @return void
* @throws Exception CHMOD_ERROR
*/
public function chmod( $filename, $mode )
public function chmod( string $filename, int $mode ) : void
{
if( !@ftp_chmod( $this->ftp, $mode, $filename ) )
{
throw new \IPS\Ftp\Exception( 'CHMOD_ERROR' );
throw new Exception( 'CHMOD_ERROR' );
}
}
/**
* Delete file
*
* @param string $file Path to file
* @param string $file Path to file
* @return void
* @throws \IPS\Ftp\Exception
* @throws Exception
*/
public function delete( $file )
public function delete( string $file ) : void
{
if ( !@ftp_delete( $this->ftp, $file ) )
{
throw new \IPS\Ftp\Exception( 'COULD_NOT_DELETE' );
throw new Exception( 'COULD_NOT_DELETE' );
}
}
/**
* Get file size (if possible)
*
* @param string $file Path to file
* @return float
* @param string $file Path to file
* @return float|int
*/
public function size( $file )
public function size( string $file ): float|int
{
$size = @ftp_size( $this->ftp, $file );
@@ -265,12 +271,12 @@ class _Ftp
/**
* Delete directory
*
* @param string $dir Path to directory
* @param bool $recursive Recursive? (If FALSE and directory is not empty, operation will fail)
* @param string $dir Path to directory
* @param bool $recursive Recursive? (If FALSE and directory is not empty, operation will fail)
* @return void
* @throws \IPS\Ftp\Exception
* @throws Exception
*/
public function rmdir( $dir, $recursive=FALSE )
public function rmdir( string $dir, bool $recursive=FALSE ) : void
{
if ( $recursive )
{
@@ -282,7 +288,7 @@ class _Ftp
{
if ( $matches[1] === 'd' )
{
$this->rmdir( $matches[2], TRUE );
$this->rmdir($matches[2], TRUE);
}
else
{
@@ -295,7 +301,7 @@ class _Ftp
if ( !@ftp_rmdir( $this->ftp, $dir ) )
{
throw new \IPS\Ftp\Exception( 'COULD_NOT_DELETE' );
throw new Exception( 'COULD_NOT_DELETE' );
}
}
}