depage-fs
Loading...
Searching...
No Matches
FsSsh.php
Go to the documentation of this file.
1<?php
2
3namespace Depage\Fs;
4
5class 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 {
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) {
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 ) {
68 } else {
70 }
71
72 $this->session = \ssh2_sftp($connection);
73 }
74
75 return $this->session;
76 }
78 {
79 return \ssh2_auth_password(
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(
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 : */
rm($url)
Definition Fs.php:151
buildUrl($parsed, $showPass=true)
Definition FsSsh.php:135
isValidKeyCombination()
Definition FsSsh.php:118
__construct($params=array())
Definition FsSsh.php:15
rename($source, $target)
Hook, allows overriding of rename function.
Definition FsSsh.php:151
getConnection(&$fingerprint=null)
Definition FsSsh.php:35
authenticateByPassword($connection)
Definition FsSsh.php:77
authenticateByKey($connection)
Definition FsSsh.php:85