![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/app/code/Soon/DataSync/Model/Transfer/Type/ |
<?php /** * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) * @author Hervé Guétin <[email protected]> <@herveguetin> * @copyright Copyright (c) 2017 Agence Soon (http://www.agence-soon.fr) */ namespace Soon\DataSync\Model\Transfer\Type; use Magento\Framework\Filesystem\Io\Ftp as CoreFtp; use Magento\Framework\Filesystem\Io\FtpFactory; class Ftp extends TransferTypeAbstract { /** * @var string */ public $host; /** * @var int */ public $port = 21; /** * @var string */ public $user = 'anonymous'; /** * @var string */ public $password = '[email protected]'; /** * @var int */ public $timeout = 90; /** * @var int */ public $filemode = FTP_BINARY; /** * @var bool */ public $ssl; /** * @var string */ public $path; /** * @var bool */ public $passive; /** * @var string */ public $filepath = 'data'; /** * @var CoreFtp */ protected $ftp; /** * @var mixed */ private $result; /** * @var string */ private $filePattern; /** * @var string */ private $dir; public function __construct( FtpFactory $ftpFactory ) { $this->ftp = $ftpFactory->create(); } /** * @return string */ public function pull(): string { $this->run(function () { return $this->ftp->read($this->filepath()); }); return $this->result; } private function run(\Closure $closure) { $this->ftp->open($this->config()); $this->result = $closure(); $this->ftp->close(); } /** * @return array */ private function config() { $config = [ 'host' => $this->host, 'port' => $this->port, 'user' => $this->user, 'username' => $this->user, 'password' => $this->password, 'timeout' => $this->timeout, 'file_mode' => $this->filemode ]; if ($this->ssl) { $config['ssl'] = true; } if ($this->path) { $config['path'] = $this->path; } if ($this->passive) { $config['passive'] = true; } return $config; } public function push(string $content) { $this->run(function () use ($content) { $this->mkdir(); $this->ftp->write($this->filepath(), $content); }); } /** * Create full dir path on remote FTP server */ protected function mkdir() { if (strpos($this->filepath(), '/') !== false) { $pathArr = explode('/', $this->filepath()); array_pop($pathArr); $path = ''; array_map(function ($segment) use (&$path) { $path .= $segment . '/'; $this->ftp->mkdir($path); }, $pathArr); } } public function clean() { $this->run(function () { $this->ftp->rm($this->filepath()); }); } /** * @param string $path * @return array */ public function glob(string $path): array { list($this->filePattern, $this->dir) = $this->splitPath($path); $this->run(function () { if (!($this->ftp->cd($this->dir))) { throw new \Exception($this->dir . ' does not exist'); } return $this->globFiles(); }); return $this->result; } /** * @return array */ private function globFiles() { $files = []; foreach ($this->ftp->ls() as $file) { if (strpos($file['text'], '.') === false) { continue; // $file is a dir } if ($this->match($file['text'], $this->filePattern)) { $files[] = ($this->dir) ? $this->dir . '/' . $file['text'] : $file['text']; } } return $files; } /** * @return string */ protected function parseFilepath(): string { return ltrim(str_replace(self::BASE_DIR_DIRECTIVE, '', $this->filepath), '/'); } }