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