-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFsSsh.php
197 lines (174 loc) · 5.41 KB
/
FsSsh.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
namespace Depage\Fs;
class FsSsh extends Fs
{
// {{{ variables
protected $session;
protected $connection;
protected $privateKeyFile;
protected $publicKeyFile;
protected $privateKey;
protected $publicKey;
protected $fingerprint;
protected $tmp;
// }}}
// {{{ constructor
public function __construct($params = array())
{
parent::__construct($params);
$this->privateKeyFile = (isset($params['privateKeyFile'])) ? $params['privateKeyFile'] : null;
$this->publicKeyFile = (isset($params['publicKeyFile'])) ? $params['publicKeyFile'] : null;
$this->privateKey = (isset($params['privateKey'])) ? $params['privateKey'] : null;
$this->publicKey = (isset($params['publicKey'])) ? $params['publicKey'] : null;
$this->tmp = (isset($params['tmp'])) ? $params['tmp'] : null;
$this->fingerprint = (isset($params['fingerprint'])) ? $params['fingerprint'] : null;
}
// }}}
// {{{ destructor
public function __destruct()
{
$this->disconnect();
}
// }}}
// {{{ getFingerprint
public function getFingerprint()
{
$this->getConnection($fingerprint);
return $fingerprint;
}
// }}}
// {{{ getConnection
protected function getConnection(&$fingerprint = null)
{
if (!$this->connection) {
if (isset($this->url['port'])) {
$this->connection = \ssh2_connect($this->url['host'], $this->url['port']);
} else {
$this->connection = \ssh2_connect($this->url['host']);
}
}
$fingerprint = \ssh2_fingerprint($this->connection);
return $this->connection;
}
// }}}
// {{{ getSession
protected function getSession()
{
if (!$this->session) {
$connection = $this->getConnection($fingerprint);
if (
!is_null($this->fingerprint) &&
strcasecmp($this->fingerprint, $fingerprint) !== 0
) {
throw new Exceptions\FsException('SSH RSA Fingerprints don\'t match.');
}
if (
$this->privateKeyFile
|| $this->publicKeyFile
|| $this->privateKey
|| $this->publicKey
|| $this->tmp
) {
$this->authenticateByKey($connection);
} else {
$this->authenticateByPassword($connection);
}
$this->session = \ssh2_sftp($connection);
}
return $this->session;
}
// }}}
// {{{ authenticateByPassword
protected function authenticateByPassword($connection)
{
return \ssh2_auth_password(
$connection,
$this->url['user'],
$this->url['pass']
);
}
// }}}
// {{{ authenticateByKey
protected function authenticateByKey($connection)
{
if (!$this->isValidKeyCombination()) {
throw new Exceptions\FsException('Invalid SSH key combination.');
}
if ($this->privateKeyFile) {
$private = new PrivateSshKey($this->privateKeyFile);
} elseif ($this->privateKey) {
$private = new PrivateSshKey($this->privateKey, $this->tmp);
}
if ($this->publicKeyFile) {
$public = new PublicSshKey($this->publicKeyFile);
} elseif ($this->publicKey) {
$public = new PublicSshKey($this->publicKey, $this->tmp);
} else {
$public = $private->extractPublicKey($this->tmp);
}
$authenticated = \ssh2_auth_pubkey_file(
$connection,
$this->url['user'],
$public,
$private,
$this->url['pass']
);
$private->clean();
$public->clean();
return $authenticated;
}
// }}}
// {{{ isValidKeyCombination
protected function isValidKeyCombination()
{
return ($this->privateKeyFile && $this->publicKeyFile)
|| ($this->tmp && ($this->privateKeyFile || $this->privateKey));
}
// }}}
// {{{ disconnect
protected function disconnect()
{
$this->connection = null;
$this->session = null;
}
// }}}
// {{{ lateConnect
protected function lateConnect()
{
parent::lateConnect();
$this->getSession();
}
// }}}
// {{{ buildUrl
protected function buildUrl($parsed, $showPass = true)
{
$url = $parsed['scheme'] . '://';
$url .= $this->getSession();
$path = isset($parsed['path']) ? $parsed['path'] : '/';
// workaround for https://bugs.php.net/bug.php?id=64169
if ($path === '/') {
$path = '/.';
}
$url .= $path;
return $url;
}
// }}}
// {{{ rename
protected function rename($source, $target)
{
$result = true;
// workaround, rename doesn't overwrite files via ssh
if (is_file($target) && is_file($source)) {
$this->rm($target);
$result = !is_file($target);
}
if ($result) {
//parent::rename($source, $target);
\ssh2_sftp_rename($this->session, $source, $target);
$result = is_file($target);
}
return $result;
}
// }}}
}
/* vim:set ft=php sw=4 sts=4 fdm=marker : */