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 $publicKey = 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, 'public_key', 'setPublicKey'); |
60
|
|
|
$this->setOption($options, 'strict_host_key_checking', 'setStrictHostKeyChecking'); |
61
|
|
|
$this->setOption($options, 'user_known_hosts_file', 'setUserKnownHostFile'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param $host |
66
|
|
|
*/ |
67
|
|
|
public function setHost($host) |
68
|
|
|
{ |
69
|
|
|
$this->host = $host; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return mixed |
74
|
|
|
*/ |
75
|
|
|
public function getHost() |
76
|
|
|
{ |
77
|
|
|
return $this->host; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param $port |
82
|
|
|
* |
83
|
|
|
* @throws \InvalidArgumentException If the port is not numeric |
84
|
|
|
*/ |
85
|
|
|
public function setPort($port) |
86
|
|
|
{ |
87
|
|
|
if(!is_int($port)) |
88
|
|
|
throw new \InvalidArgumentException("SSH port must be an integer"); |
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 $publicKey |
103
|
|
|
* @throws \InvalidArgumentException |
104
|
|
|
*/ |
105
|
|
|
public function setPublicKey($publicKey) |
106
|
|
|
{ |
107
|
|
|
if(!is_readable($publicKey)) |
108
|
|
|
throw new \InvalidArgumentException("SSH public key '" .$publicKey. "' is not readable"); |
109
|
|
|
|
110
|
|
|
$this->publicKey = $publicKey; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return null |
115
|
|
|
*/ |
116
|
|
|
public function getPublicKey() |
117
|
|
|
{ |
118
|
|
|
return $this->publicKey; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param $username |
123
|
|
|
*/ |
124
|
|
|
public function setUsername($username) |
125
|
|
|
{ |
126
|
|
|
$this->username = $username; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return mixed |
131
|
|
|
*/ |
132
|
|
|
public function getUsername() |
133
|
|
|
{ |
134
|
|
|
return $this->username; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param $string |
139
|
|
|
* @throws \InvalidArgumentException |
140
|
|
|
*/ |
141
|
|
|
public function setStrictHostKeyChecking($strictHostKeyCheking) |
142
|
|
|
{ |
143
|
|
|
//dd($strictHostKeyCheking); |
144
|
|
|
if ($strictHostKeyCheking !== 'yes' && $strictHostKeyCheking !== 'no') |
145
|
|
|
throw new \InvalidArgumentException("StrictHostKeyCheking must be set to 'yes' or 'no'"); |
146
|
|
|
|
147
|
|
|
$this->strictHostKeyCheking = "StrictHostKeyChecking={$strictHostKeyCheking}"; |
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return $string |
|
|
|
|
152
|
|
|
*/ |
153
|
|
|
public function getStrictHostKeyChecking($strictHostKeyCheking) |
|
|
|
|
154
|
|
|
{ |
155
|
|
|
return $this->strictHostKeyCheking; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param $string |
160
|
|
|
*/ |
161
|
|
|
public function setUserKnownHostFile($userKnownHostsFile) |
162
|
|
|
{ |
163
|
|
|
if (!is_string($userKnownHostsFile)) |
164
|
|
|
throw new \InvalidArgumentException("UserKnownHostsFile should be set to a directory"); |
165
|
|
|
|
166
|
|
|
$this->userKnownHostsFile = "UserKnownHostsFile={$userKnownHostsFile}"; |
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return $string |
|
|
|
|
171
|
|
|
*/ |
172
|
|
|
public function getUserKnownHostFile($userKnownHostFile) |
|
|
|
|
173
|
|
|
{ |
174
|
|
|
return $this->userKnownHostFile; |
|
|
|
|
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Gets commands for this SSH connection |
179
|
|
|
* |
180
|
|
|
* @param bool $hostConnection |
181
|
|
|
* |
182
|
|
|
* @return string |
183
|
|
|
* |
184
|
|
|
* @throws \InvalidArgumentException If you don't specify a SSH username or host |
185
|
|
|
*/ |
186
|
|
|
public function getCommand($hostConnection = true) |
187
|
|
|
{ |
188
|
|
|
if(is_null($this->username)) |
189
|
|
|
throw new \InvalidArgumentException("You must specify a SSH username"); |
190
|
|
|
|
191
|
|
|
if(is_null($this->host)) |
192
|
|
|
throw new \InvalidArgumentException("You must specify a SSH host to connect"); |
193
|
|
|
|
194
|
|
|
$command = new Command($this->executable); |
195
|
|
|
|
196
|
|
|
if($this->port != 22) |
197
|
|
|
$command->addArgument("p", $this->port); |
198
|
|
|
|
199
|
|
|
if(!is_null($this->publicKey)) |
200
|
|
|
$command->addArgument("i", $this->publicKey); |
201
|
|
|
|
202
|
|
|
if (!is_null($this->strictHostKeyCheking)) |
203
|
|
|
$command->addArgument("o", $this->strictHostKeyCheking); |
204
|
|
|
|
205
|
|
|
if (!is_null($this->userKnownHostsFile)) |
206
|
|
|
$command->addArgument("o", $this->userKnownHostsFile); |
207
|
|
|
|
208
|
|
|
if($hostConnection) |
209
|
|
|
$command->addParameter($this->getHostConnection()); |
210
|
|
|
|
211
|
|
|
return $command; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Gets only connection options, without user@host string |
216
|
|
|
* |
217
|
|
|
* @return string |
218
|
|
|
*/ |
219
|
|
|
public function getConnectionOptions() |
220
|
|
|
{ |
221
|
|
|
return (string) $this->getCommand(false); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Gets only host connection, without the rest |
226
|
|
|
* of options |
227
|
|
|
* |
228
|
|
|
* @return string |
229
|
|
|
*/ |
230
|
|
|
public function getHostConnection() |
231
|
|
|
{ |
232
|
|
|
return $this->username . "@" . $this->host; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @param $executable |
237
|
|
|
*/ |
238
|
|
|
public function setExecutable($executable) |
239
|
|
|
{ |
240
|
|
|
$this->executable = $executable; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @return string |
245
|
|
|
*/ |
246
|
|
|
public function getExecutable() |
247
|
|
|
{ |
248
|
|
|
return $this->executable; |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: