laipower/wp-content/plugins/static-html-output-plugin/plugin/FTP/FtpClient.php

2 lines
8.8 KiB
PHP
Raw Normal View History

2020-04-07 13:03:04 +00:00
<?php
namespace FtpClient; use \Countable; class FtpClient implements Countable { protected $conn; private $ftp; public function __construct($connection = null) { if (!extension_loaded('ftp')) { throw new FtpException('FTP extension is not loaded!'); } if ($connection) { $this->conn = $connection; } $this->setWrapper(new FtpWrapper($this->conn)); } public function __destruct() { if ($this->conn) { $this->ftp->close(); } } public function __call($method, array $arguments) { return $this->ftp->__call($method, $arguments); } public function help() { return $this->ftp->raw('help'); } public function connect($host, $ssl = false, $port = 21, $timeout = 90) { if ($ssl) { $this->conn = @$this->ftp->ssl_connect($host, $port, $timeout); } else { $this->conn = @$this->ftp->connect($host, $port, $timeout); } if (!$this->conn) { throw new FtpException('Unable to connect'); } return $this; } public function close() { if ($this->conn) { $this->ftp->close(); $this->conn = null; } } public function getConnection() { return $this->conn; } public function getWrapper() { return $this->ftp; } public function login($username = 'anonymous', $password = '') { $result = $this->ftp->login($username, $password); if ($result === false) { throw new FtpException('Login incorrect'); } return $this; } public function modifiedTime($remoteFile, $format = null) { $time = $this->ftp->mdtm($remoteFile); if ($time !== -1 && $format !== null) { return date($format, $time); } return $time; } public function up() { $result = @$this->ftp->cdup(); if ($result === false) { throw new FtpException('Unable to get parent folder'); } return $this; } public function nlist($directory = '.', $recursive = false, $filter = 'sort') { if (!$this->isDir($directory)) { throw new FtpException('"'.$directory.'" is not a directory'); } $files = $this->ftp->nlist($directory); if ($files === false) { throw new FtpException('Unable to list directory'); } $result = array(); $dir_len = strlen($directory); if (false !== ($kdot = array_search('.', $files))) { unset($files[$kdot]); } if(false !== ($kdot = array_search('..', $files))) { unset($files[$kdot]); } if (!$recursive) { foreach ($files as $file) { $result[] = $directory.'/'.$file; } $filter($result); return $result; } $flatten = function (array $arr) use (&$flatten) { $flat = array(); foreach ($arr as $k => $v) { if (is_array($v)) { $flat = array_merge($flat, $flatten($v)); } else { $flat[] = $v; } } return $flat; }; foreach ($files as $file) { $file = $directory.'/'.$file; if (0 === strpos($file, $directory, $dir_len)) { $file = substr($file, $dir_len); } if ($this->isDir($file)) { $result[] = $file; $items = $flatten($this->nlist($file, true, $filter)); foreach ($items as $item) { $result[] = $item; } } else { $result[] = $file; } } $result = array_unique($result); $filter($result); return $result; } public function mkdir($directory, $recursive = false) { if (!$recursive or $this->isDir($directory)) { return $this->ftp->mkdir($directory); } $result = false; $pwd = $this->ftp->pwd(); $parts = explode('/', $directory); foreach ($parts as $part) { if ($part == '') { continue; } if (!@$this->ftp->chdir($part)) { $result = $this->ftp->mkdir($part); $this->ftp->chdir($part); } } $this->ftp->chdir($pwd); return $result; } public function rmdir($directory, $recursive = true) { if ($recursive) { $files = $this->nlist($directory, false, 'rsort'); foreach ($files as $file) { $this->remove($file, true); } } return $this->ftp->rmdir($directory); } public function cleanDir($directory) { if(!$files = $this->nlist($directory)) { return $this->isEmpty($directory); } foreach ($files as $file) { $this->remove($file, true); } return $this->isEmpty($directory); } public function remove($path, $recursive = false) { try { if (@$this->ftp->delete($path) or ($this->isDir($path) and @$this->rmdir($path, $recursive))) { return true; } return false; } catch (\Exception $e) { return false; } } public function isDir($directory) { $pwd = $this->ftp->pwd(); if ($pwd === false) { throw new FtpException('Unable to resolve the current directory'); } if (@$this->ftp->