sftpbrowser/src/Service/SftpService.php

173 lines
5.2 KiB
PHP

<?php
namespace App\Service;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Filesystem\Filesystem;
class SftpService
{
private $rootPath;
private $host;
private $port;
private $user;
private $password;
private $folder;
private $connection = null;
private $sftp = null;
public function __construct($rootPath,ContainerInterface $container)
{
$this->rootPath = $rootPath;
$this->host = $container->getParameter('sftpHost');
$this->port = $container->getParameter('sftpPort');
$this->user = $container->getParameter('sftpUser');
$this->password = $container->getParameter('sftpPassword');
$this->folder = $container->getParameter('sftpFolder');
if(!empty($this->folder)) $this->folder=$this->folder."/";
}
public function connect()
{
if (!$this->connection || !$this->sftp) {
$connection = @ssh2_connect($this->host, $this->port);
if (!$connection) {
throw new \Exception(sprintf('Could not connect to %s on port %s.', $this->host, $this->port));
} else {
if (!@ssh2_auth_password($connection, $this->user, $this->password)) {
throw new \Exception(sprintf('Could not authenticate with username %s ', $this->user));
} else {
$sftp = @ssh2_sftp($connection);
if (!$sftp) {
throw new \Exception('Could not initialize SFTP subsystem.');
} else {
$this->connection = $connection;
$this->sftp = $sftp;
return $this->connection;
}
}
}
} else {
return $this->connection;
}
}
public function disconnect()
{
if ($this->connection) {
ssh2_exec($this->connection, 'exit;');
unset($this->connection);
}
}
public function uploadFile($localFile, $remoteFolder)
{
$remoteFolder=substr($remoteFolder, 1);
$baseName=basename($localFile);
$sftp = $this->sftp;
$remotePath = 'ssh2.sftp://'.join(DIRECTORY_SEPARATOR, [$sftp, $this->folder.$remoteFolder."/".$baseName]);
$stream = @fopen($remotePath, 'w');
if (!$stream) {
return false;
}
$localStream = fopen($localFile, 'r');
while ($chunk = fread($localStream, 8192)) {
fwrite($stream, $chunk);
}
@fclose($localStream);
@fclose($stream);
return true;
}
public function downloadFile($remoteFile)
{
// On enlève le premier /
$remoteFile=substr($remoteFile, 1);
$baseName=basename($remoteFile);
// Construire le nom du fichier local
$localFile=$this->rootPath."/var/tmp/".$baseName;
// Générer le répertoire temporaire de téléchargement
$filesystem = new Filesystem();
$filesystem->mkdir($this->rootPath."/var/tmp");
// Télécharger le fichier
$sftp = $this->sftp;
$remotePath = 'ssh2.sftp://'.join(DIRECTORY_SEPARATOR, [intval($sftp), $this->folder.$remoteFile]);
$remoteFilesize = filesize($remotePath);
$stream = @fopen($remotePath, 'r');
if (!$stream) {
return false;
}
else {
$localStream = fopen($localFile, 'w');
while ($chunk = fread($stream, 8192)) {
fwrite($localStream, $chunk);
}
@fclose($localStream);
}
@fclose($stream);
// Controler que le fichier est correcte
$localFilesize = filesize($localFile);
if ($remoteFilesize !== $localFilesize) {
return false;
}
return $localFile;
}
public function deleteFile($remoteFile)
{
$sftp = $this->sftp;
$remotePath = 'ssh2.sftp://'.join(DIRECTORY_SEPARATOR, [$sftp, $this->folder.$remoteFile]);
rmdir($remotePath);
unlink($remotePath);
return true;
}
public function ls($remoteDir, $filter = '*')
{
$sftp = $this->sftp;
$dir = 'ssh2.sftp://'.join(DIRECTORY_SEPARATOR, [$sftp, $this->folder.$remoteDir]);
$return = [];
$return["folders"] = [];
$return["files"] = [];
$handle = opendir($dir);
if (!$handle) {
throw new \Exception(sprintf('Cannot access remote directory "%s" !', $this->folder.$remoteDir));
}
while (false !== ($file = readdir($handle))) {
if ('.' != substr("$file", 0, 1)) {
if (is_dir(join(DIRECTORY_SEPARATOR, [$dir, $file]))) {
array_push($return["folders"],$file);
} elseif (fnmatch($filter, $file)) {
array_push($return["files"],$file);
}
}
}
closedir($handle);
return $return;
}
public function mkdir($remoteDir)
{
return ssh2_sftp_mkdir($this->sftp, $this->folder.$remoteDir, 0777, true);
}
public function rename($oldFile,$newFile)
{
return ssh2_sftp_rename($this->sftp, $this->folder.$oldFile, $this->folder.$newFile);
}
}