From 7ddda5984df40b49b6583fb9a14f91c6f7843fd1 Mon Sep 17 00:00:00 2001 From: uldisn Date: Wed, 24 Apr 2024 19:51:13 +0300 Subject: [PATCH 1/2] fixed bug in connection closing Signed-off-by: uldisn --- FtpClient.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/FtpClient.php b/FtpClient.php index 30baaad..cd11af5 100644 --- a/FtpClient.php +++ b/FtpClient.php @@ -12,6 +12,9 @@ namespace yii2mod\ftp; +use Countable; +use Exception; + /** * The FTP and SSL-FTP client for PHP. * @@ -45,7 +48,7 @@ * * @see `nicolab/php-ftp-client` */ -class FtpClient implements \Countable +class FtpClient implements Countable { /** * The connection with the server @@ -88,6 +91,7 @@ public function __destruct() { if ($this->conn) { $this->ftp->close(); + $this->conn = null; } } @@ -454,7 +458,7 @@ public function remove($path, $recursive = false) } return false; - } catch (\Exception $e) { + } catch (Exception $e) { return false; } } @@ -677,7 +681,7 @@ public function rawlist($directory = '.', $recursive = false, $includeHidden = f $items = []; if (false == $recursive) { foreach ($list as $path => $item) { - $chunks = preg_split("/\s+/", $item); + $chunks = preg_split('/\s+/', $item); // if not "name" if (empty($chunks[8]) || $chunks[8] == '.' || $chunks[8] == '..') { continue; @@ -702,7 +706,7 @@ public function rawlist($directory = '.', $recursive = false, $includeHidden = f ) { continue; } - $chunks = preg_split("/\s+/", $item); + $chunks = preg_split('/\s+/', $item); // if not "name" if (empty($chunks[8]) || $chunks[8] == '.' || $chunks[8] == '..') { continue; @@ -739,7 +743,7 @@ public function parseRawList(array $rawlist) $items = []; $path = ''; foreach ($rawlist as $key => $child) { - $chunks = preg_split("/\s+/", $child); + $chunks = preg_split('/\s+/', $child); if (isset($chunks[8]) && ($chunks[8] == '.' or $chunks[8] == '..')) { continue; } From 65247bccdd14c016e23dfd6083da6e670f0050bc Mon Sep 17 00:00:00 2001 From: uldisn Date: Fri, 26 Apr 2024 13:08:30 +0300 Subject: [PATCH 2/2] added method close Signed-off-by: uldisn --- FtpClient.php | 68 ++++++++++++++++++++++++++++++++---------------- FtpException.php | 4 ++- FtpWrapper.php | 2 +- 3 files changed, 49 insertions(+), 25 deletions(-) diff --git a/FtpClient.php b/FtpClient.php index cd11af5..b550e0d 100644 --- a/FtpClient.php +++ b/FtpClient.php @@ -14,6 +14,7 @@ use Countable; use Exception; +use RuntimeException; /** * The FTP and SSL-FTP client for PHP. @@ -22,7 +23,6 @@ * @method bool cdup() cdup() Changes to the parent directory * @method bool chdir() chdir(string $directory) Changes the current directory on a FTP server * @method int chmod() chmod(int $mode, string $filename) Set permissions on a file via FTP - * @method bool close() close() Closes an FTP connection * @method bool delete() delete(string $path) Deletes a file on the FTP server * @method bool exec() exec(string $command) Requests execution of a command on the FTP server * @method bool fget() fget(resource $handle, string $remote_file, int $mode, int $resumepos = 0) Downloads a file from the FTP server and saves to an open file @@ -90,11 +90,26 @@ public function __construct($connection = null) public function __destruct() { if ($this->conn) { - $this->ftp->close(); + $this->close(); $this->conn = null; } } + /** + * @return bool + */ + public function close() + { + if (!$this->ftp) { + $this->conn = null; + return true; + } + $return = $this->ftp->close(); + $this->conn = null; + $this->ftp = null; + return $return; + } + /** * Call an internal method or a FTP method handled by the wrapper. * @@ -104,9 +119,10 @@ public function __destruct() * @param $method * @param array $arguments * + * @return mixed + * @throws FtpException * @internal param string $function * - * @return mixed */ public function __call($method, array $arguments) { @@ -177,6 +193,7 @@ public function connect($host, $ssl = false, $port = 21, $timeout = 90) * @param bool $include_hidden * * @return FtpClient + * @throws FtpException */ public function getAll($source_directory, $target_directory, $mode = FTP_BINARY, $include_hidden = false) { @@ -186,9 +203,11 @@ public function getAll($source_directory, $target_directory, $mode = FTP_BINARY, foreach ($d as $file) { $new_source_directory = $source_directory . '/' . $file['name']; $new_target_directory = $target_directory . '/' . $file['name']; - if ($file['type'] == 'directory') { + if ($file['type'] === 'directory') { if (!is_dir($new_target_directory)) { - mkdir($new_target_directory); + if (!mkdir($new_target_directory) && !is_dir($new_target_directory)) { + throw new RuntimeException(sprintf('Directory "%s" was not created', $new_target_directory)); + } } $this->getAll($new_source_directory, $new_target_directory, $mode, $include_hidden); } else { @@ -354,15 +373,16 @@ public function nlist($directory = '.', $recursive = false, $filter = 'sort') /** * Creates a directory * + * @param string $directory The directory + * @param bool $recursive + * + * @return string|false + * @throws FtpException * @see FtpClient::rmdir() * @see FtpClient::remove() * @see FtpClient::put() * @see FtpClient::putAll() * - * @param string $directory The directory - * @param bool $recursive - * - * @return string|false */ public function mkdir($directory, $recursive = false) { @@ -417,13 +437,14 @@ public function rmdir($directory, $recursive = true) /** * Empty directory * + * @param string $directory + * + * @return bool + * @throws FtpException * @see FtpClient::remove() * @see FtpClient::delete() * @see FtpClient::rmdir() * - * @param string $directory - * - * @return bool */ public function cleanDir($directory) { @@ -453,11 +474,8 @@ public function cleanDir($directory) public function remove($path, $recursive = false) { try { - if (@$this->ftp->delete($path) || ($this->isDir($path) and @$this->rmdir($path, $recursive))) { - return true; - } - - return false; + return @$this->ftp->delete($path) + || ($this->isDir($path) and @$this->rmdir($path, $recursive)); } catch (Exception $e) { return false; } @@ -494,25 +512,27 @@ public function isDir($directory) * @param string $directory * * @return bool + * @throws FtpException */ public function isEmpty($directory) { - return $this->count($directory, null, false) === 0 ? true : false; + return $this->count($directory, null, false) === 0; } /** * Scan a directory and returns the details of each item. * - * @see FtpClient::nlist() - * @see FtpClient::rawlist() - * @see FtpClient::parseRawList() - * @see FtpClient::dirSize() - * * @param string $directory * @param bool $recursive * @param bool $includeHidden * * @return array + * @throws FtpException + * @see FtpClient::rawlist() + * @see FtpClient::parseRawList() + * @see FtpClient::dirSize() + * + * @see FtpClient::nlist() */ public function scanDir($directory = '.', $recursive = false, $includeHidden = false) { @@ -527,6 +547,7 @@ public function scanDir($directory = '.', $recursive = false, $includeHidden = f * @param bool $include_hidden false by default * * @return int The size in bytes + * @throws FtpException */ public function dirSize($directory = '.', $recursive = true, $include_hidden = false) { @@ -548,6 +569,7 @@ public function dirSize($directory = '.', $recursive = true, $include_hidden = f * @param bool $include_hidden * * @return int + * @throws FtpException */ public function count($directory = '.', $type = null, $recursive = true, $include_hidden = false) { diff --git a/FtpException.php b/FtpException.php index 5316bff..94ccee9 100644 --- a/FtpException.php +++ b/FtpException.php @@ -12,9 +12,11 @@ namespace yii2mod\ftp; +use Exception; + /** * The FtpException class. Exception thrown if an error on runtime of the FTP client occurs. */ -class FtpException extends \Exception +class FtpException extends Exception { } diff --git a/FtpWrapper.php b/FtpWrapper.php index efdb03d..7eb11b7 100644 --- a/FtpWrapper.php +++ b/FtpWrapper.php @@ -89,7 +89,7 @@ public function __call($function, array $arguments) return call_user_func_array($function, $arguments); } - throw new FtpException("{$function} is not a valid FTP function"); + throw new FtpException("$function is not a valid FTP function"); } /**