1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SF2Helpers\SFTPBundle\SFTP; |
4
|
|
|
|
5
|
|
|
class SFTP implements ConnectionInterface, ResourceTransferInterface |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Connection instance |
9
|
|
|
* |
10
|
|
|
* @var $connection |
11
|
|
|
*/ |
12
|
|
|
private $connection; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Established sftp resource |
16
|
|
|
* |
17
|
|
|
* @var $sftp |
18
|
|
|
*/ |
19
|
|
|
private $sftp; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @inheritdoc |
23
|
|
|
*/ |
24
|
|
|
public function connect($host, $port) |
25
|
|
|
{ |
26
|
|
|
$this->connection = ssh2_connect($host, $port); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @inheritdoc |
31
|
|
|
*/ |
32
|
|
|
public function login($username, $password = null) |
33
|
|
|
{ |
34
|
|
|
if (!$this->connection) { |
35
|
|
|
throw new \LogicException('Establish connection with server before login'); |
36
|
|
|
} |
37
|
|
|
is_null($password) ? ssh2_auth_agent($this->connection, $username) : ssh2_auth_password($this->connection, $username, $password); |
38
|
|
|
$this->sftp = ssh2_sftp($this->connection); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritdoc |
43
|
|
|
*/ |
44
|
|
|
public function loginWithKey($username, $pubkeyfile, $privkeyfile, $passphrase = null) |
45
|
|
|
{ |
46
|
|
|
if (!$this->connection) { |
47
|
|
|
throw new \LogicException('Establish connection with server before login'); |
48
|
|
|
} |
49
|
|
|
ssh2_auth_pubkey_file($this->connection, $username, $pubkeyfile, $privkeyfile, $passphrase); |
50
|
|
|
$this->sftp = ssh2_sftp($this->connection); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritdoc |
55
|
|
|
*/ |
56
|
|
|
public function fetchFrom($remoteFile, $localFile) |
57
|
|
|
{ |
58
|
|
|
$remoteData = file_get_contents('ssh2.sftp://' . intval($this->sftp) . $remoteFile); |
59
|
|
|
if (!$remoteData) { |
60
|
|
|
throw new \Exception('File can`t be loaded from the SFTP server.'); |
61
|
|
|
} |
62
|
|
|
file_put_contents($localFile, $remoteData); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritdoc |
67
|
|
|
*/ |
68
|
|
|
public function sendTo($localFile, $remoteFile) |
69
|
|
|
{ |
70
|
|
|
if (!file_exists($localFile)) { |
71
|
|
|
throw new \Exception('Local file doesn\'t exists.'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$sftp = "ssh2.sftp://" . intval($this->sftp); |
75
|
|
|
$data = file_get_contents($localFile); |
76
|
|
|
|
77
|
|
|
if (!file_put_contents($sftp . $remoteFile, $data)) { |
78
|
|
|
throw new \Exception('File could not be uploaded to the SFTP server.'); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @inheritdoc |
84
|
|
|
*/ |
85
|
|
|
public function getRemoteFilesList($dir) |
86
|
|
|
{ |
87
|
|
|
$handle = opendir("ssh2.sftp://" . intval($this->sftp) . $dir); |
88
|
|
|
$files = array(); |
89
|
|
|
|
90
|
|
|
if (is_bool($handle)) { |
91
|
|
|
throw new \Exception('Could not read files list from remote SFTP server. Check your connection.'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
while (false !== ($entry = readdir($handle))) { |
95
|
|
|
$files[] = $entry; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $files; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @inheritdoc |
103
|
|
|
*/ |
104
|
|
|
public function disconnect() |
105
|
|
|
{ |
106
|
|
|
unset($this->connection); |
107
|
|
|
unset($this->sftp); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|