1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of rsync-lib |
5
|
|
|
* |
6
|
|
|
* (c) Alberto Fernández <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace AFM\Rsync; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Abstract SSH connection command. Note that if you |
16
|
|
|
* don't specify a public key, you will be prompted for |
17
|
|
|
* the remote server password |
18
|
|
|
* |
19
|
|
|
* @author Alberto <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class SSH extends AbstractProtocol |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $executable = "ssh"; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $host; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
protected $port = 22; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $username; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var null |
45
|
|
|
*/ |
46
|
|
|
protected $privateKey = null; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Injects and validates config |
50
|
|
|
* |
51
|
|
|
* @param array $options |
52
|
|
|
*/ |
53
|
|
|
public function __construct(Array $options = array()) |
54
|
|
|
{ |
55
|
|
|
$this->setOption($options, 'executable', 'setExecutable'); |
56
|
|
|
$this->setOption($options, 'host', 'setHost'); |
57
|
|
|
$this->setOption($options, 'port', 'setPort'); |
58
|
|
|
$this->setOption($options, 'username', 'setUsername'); |
59
|
|
|
$this->setOption($options, 'private_key', 'setPrivateKey'); |
60
|
|
|
$this->setOption($options, 'public_key', 'setPrivateKey'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param $host |
65
|
|
|
*/ |
66
|
|
|
public function setHost($host) |
67
|
|
|
{ |
68
|
|
|
$this->host = $host; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return mixed |
73
|
|
|
*/ |
74
|
|
|
public function getHost() |
75
|
|
|
{ |
76
|
|
|
return $this->host; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param $port |
81
|
|
|
* |
82
|
|
|
* @throws \InvalidArgumentException If the port is not numeric |
83
|
|
|
*/ |
84
|
|
|
public function setPort($port) |
85
|
|
|
{ |
86
|
|
|
if (!is_int($port)) { |
87
|
|
|
throw new \InvalidArgumentException("SSH port must be an integer"); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->port = $port; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return int |
95
|
|
|
*/ |
96
|
|
|
public function getPort() |
97
|
|
|
{ |
98
|
|
|
return $this->port; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param $privateKey |
103
|
|
|
* @throws \InvalidArgumentException |
104
|
|
|
*/ |
105
|
|
|
public function setPrivateKey($privateKey) |
106
|
|
|
{ |
107
|
|
|
if (!is_readable($privateKey)) { |
108
|
|
|
throw new \InvalidArgumentException("SSH private key '".$privateKey."' is not readable"); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$this->privateKey = $privateKey; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
public function getPrivateKey() |
118
|
|
|
{ |
119
|
|
|
return $this->privateKey; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param $username |
124
|
|
|
*/ |
125
|
|
|
public function setUsername($username) |
126
|
|
|
{ |
127
|
|
|
$this->username = $username; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
public function getUsername() |
134
|
|
|
{ |
135
|
|
|
return $this->username; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Gets commands for this SSH connection |
140
|
|
|
* |
141
|
|
|
* @param bool $hostConnection |
142
|
|
|
* |
143
|
|
|
* @return string |
144
|
|
|
* |
145
|
|
|
* @throws \InvalidArgumentException If you don't specify a SSH username or host |
146
|
|
|
*/ |
147
|
|
|
public function getCommand($hostConnection = true) |
148
|
|
|
{ |
149
|
|
|
if (is_null($this->username)) { |
150
|
|
|
throw new \InvalidArgumentException("You must specify a SSH username"); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if (is_null($this->host)) { |
154
|
|
|
throw new \InvalidArgumentException("You must specify a SSH host to connect"); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$command = new Command($this->executable); |
158
|
|
|
|
159
|
|
|
if ($this->port != 22) { |
160
|
|
|
$command->addArgument("p", $this->port); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
if (!is_null($this->privateKey)) { |
164
|
|
|
$command->addArgument("i", $this->privateKey); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
if ($hostConnection) { |
168
|
|
|
$command->addParameter($this->getHostConnection()); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $command; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Gets only connection options, without user@host string |
176
|
|
|
* |
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
|
|
public function getConnectionOptions() |
180
|
|
|
{ |
181
|
|
|
return (string)$this->getCommand(false); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Gets only host connection, without the rest |
186
|
|
|
* of options |
187
|
|
|
* |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
|
|
public function getHostConnection() |
191
|
|
|
{ |
192
|
|
|
return $this->username."@".$this->host; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param $executable |
197
|
|
|
*/ |
198
|
|
|
public function setExecutable($executable) |
199
|
|
|
{ |
200
|
|
|
$this->executable = $executable; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @return string |
205
|
|
|
*/ |
206
|
|
|
public function getExecutable() |
207
|
|
|
{ |
208
|
|
|
return $this->executable; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @deprecated |
213
|
|
|
*/ |
214
|
|
|
public function setPublicKey($privateKey) |
215
|
|
|
{ |
216
|
|
|
$this->setPrivateKey($privateKey); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @deprecated |
221
|
|
|
*/ |
222
|
|
|
public function getPublicKey() |
223
|
|
|
{ |
224
|
|
|
return $this->getPrivateKey(); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|