1
|
|
|
<?php |
2
|
|
|
/* (c) Anton Medvedev <[email protected]> |
3
|
|
|
* |
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
5
|
|
|
* file that was distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Deployer\Server\Remote; |
9
|
|
|
|
10
|
|
|
use Deployer\Server\Configuration; |
11
|
|
|
use Deployer\Server\ServerInterface; |
12
|
|
|
use phpseclib\Crypt\RSA; |
13
|
|
|
use phpseclib\Net\SFTP; |
14
|
|
|
use phpseclib\System\SSH\Agent; |
15
|
|
|
use RuntimeException; |
16
|
|
|
|
17
|
|
|
class PhpSecLib implements ServerInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var Configuration |
21
|
|
|
*/ |
22
|
|
|
private $configuration; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var SFTP |
26
|
|
|
*/ |
27
|
|
|
private $sftp; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Array of created directories during upload. |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $directories = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param Configuration $configuration |
37
|
|
|
*/ |
38
|
2 |
|
public function __construct(Configuration $configuration) |
39
|
|
|
{ |
40
|
2 |
|
$this->configuration = $configuration; |
41
|
2 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public function connect() |
47
|
|
|
{ |
48
|
|
|
$serverConfig = $this->getConfiguration(); |
49
|
|
|
$this->sftp = new SFTP($serverConfig->getHost(), $serverConfig->getPort(), 3600); |
50
|
|
|
|
51
|
|
|
switch ($serverConfig->getAuthenticationMethod()) { |
52
|
|
|
case Configuration::AUTH_BY_PASSWORD: |
|
|
|
|
53
|
|
|
|
54
|
|
|
$result = $this->sftp->login($serverConfig->getUser(), $serverConfig->getPassword()); |
55
|
|
|
|
56
|
|
|
break; |
57
|
|
|
|
58
|
|
View Code Duplication |
case Configuration::AUTH_BY_IDENTITY_FILE: |
|
|
|
|
59
|
|
|
|
60
|
|
|
$key = new RSA(); |
61
|
|
|
$key->setPassword($serverConfig->getPassPhrase()); |
62
|
|
|
$key->loadKey(file_get_contents($serverConfig->getPrivateKey())); |
63
|
|
|
|
64
|
|
|
$result = $this->sftp->login($serverConfig->getUser(), $key); |
65
|
|
|
|
66
|
|
|
break; |
67
|
|
|
|
68
|
|
View Code Duplication |
case Configuration::AUTH_BY_PEM_FILE: |
|
|
|
|
69
|
|
|
|
70
|
|
|
$key = new RSA(); |
71
|
|
|
$key->loadKey(file_get_contents($serverConfig->getPemFile())); |
72
|
|
|
$result = $this->sftp->login($serverConfig->getUser(), $key); |
73
|
|
|
|
74
|
|
|
break; |
75
|
|
|
|
76
|
|
|
case Configuration::AUTH_BY_AGENT: |
|
|
|
|
77
|
|
|
|
78
|
|
|
$key = new Agent(); |
79
|
|
|
$key->startSSHForwarding(null); |
80
|
|
|
$result = $this->sftp->login($serverConfig->getUser(), $key); |
81
|
|
|
|
82
|
|
|
break; |
83
|
|
|
|
84
|
|
View Code Duplication |
case Configuration::AUTH_BY_IDENTITY_FILE_AND_PASSWORD: |
|
|
|
|
85
|
|
|
|
86
|
|
|
$key = new RSA(); |
87
|
|
|
$key->setPassword($serverConfig->getPassPhrase()); |
88
|
|
|
$key->loadKey(file_get_contents($serverConfig->getPrivateKey())); |
89
|
|
|
|
90
|
|
|
$result = $this->sftp->login($serverConfig->getUser(), $key, $serverConfig->getPassword()); |
91
|
|
|
|
92
|
|
|
break; |
93
|
|
|
|
94
|
|
|
default: |
95
|
|
|
throw new RuntimeException('You need to specify authentication method.'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (!$result) { |
99
|
|
|
throw new RuntimeException('Unable to login with the provided credentials.'); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Check if not connected and connect. |
105
|
|
|
*/ |
106
|
|
|
public function checkConnection() |
107
|
|
|
{ |
108
|
|
|
if (null === $this->sftp) { |
109
|
|
|
$this->connect(); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
|
|
public function run($command) |
117
|
|
|
{ |
118
|
|
|
$this->checkConnection(); |
119
|
|
|
|
120
|
|
|
$result = $this->sftp->exec($command); |
121
|
|
|
|
122
|
|
|
if ($this->sftp->getExitStatus() !== 0) { |
123
|
|
|
$output = $this->sftp->getStdError() ?: $result; |
124
|
|
|
throw new \RuntimeException($output); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $result; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritdoc} |
132
|
|
|
*/ |
133
|
|
View Code Duplication |
public function upload($local, $remote) |
134
|
|
|
{ |
135
|
|
|
$this->checkConnection(); |
136
|
|
|
|
137
|
|
|
$remote = str_replace(DIRECTORY_SEPARATOR, '/', $remote); |
138
|
|
|
$dir = str_replace(DIRECTORY_SEPARATOR, '/', dirname($remote)); |
139
|
|
|
|
140
|
|
|
if (!isset($this->directories[$dir])) { |
141
|
|
|
$this->sftp->mkdir($dir, -1, true); |
142
|
|
|
$this->directories[$dir] = true; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if (!$this->sftp->put($remote, $local, SFTP::SOURCE_LOCAL_FILE)) { |
146
|
|
|
throw new \RuntimeException(implode($this->sftp->getSFTPErrors(), "\n")); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
|
|
public function download($local, $remote) |
154
|
|
|
{ |
155
|
|
|
$this->checkConnection(); |
156
|
|
|
|
157
|
|
|
if (!$this->sftp->get($remote, $local)) { |
158
|
|
|
throw new \RuntimeException(implode($this->sftp->getSFTPErrors(), "\n")); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return Configuration |
164
|
|
|
*/ |
165
|
|
|
public function getConfiguration() |
166
|
|
|
{ |
167
|
|
|
return $this->configuration; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.