1
|
|
|
<?php |
2
|
|
|
namespace LibSSH2; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Configuration class. |
6
|
|
|
* |
7
|
|
|
* Remote resource configuration settings. |
8
|
|
|
* |
9
|
|
|
* @package LibSSH2 |
10
|
|
|
*/ |
11
|
|
|
class Configuration |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Username. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $username; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* User password. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $password; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* RSA public key. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $publickey; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* RSA private key. |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $privatekey; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Passphrase. |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $passphrase; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Hostname. |
51
|
|
|
* |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
protected $host; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Port. |
58
|
|
|
* |
59
|
|
|
* @var integer |
60
|
|
|
*/ |
61
|
|
|
protected $port = 22; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Remote connection methods. |
65
|
|
|
* |
66
|
|
|
* @var array |
67
|
|
|
*/ |
68
|
|
|
protected $methods = []; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* SSH tunnel requested. |
72
|
|
|
* |
73
|
|
|
* @var boolean |
74
|
|
|
*/ |
75
|
|
|
protected $tunnel = false; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* SSH tunnel hostname. |
79
|
|
|
* |
80
|
|
|
* @var string |
81
|
|
|
*/ |
82
|
|
|
protected $tunnel_host; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* SSH tunnel port. |
86
|
|
|
* |
87
|
|
|
* @var int |
88
|
|
|
*/ |
89
|
|
|
protected $tunnel_port = 22; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Sets username. |
93
|
|
|
* |
94
|
|
|
* @param string $username username |
95
|
|
|
* @return object \LibSSH2\Configuration object |
96
|
|
|
*/ |
97
|
|
|
final public function set_username($username) |
98
|
|
|
{ |
99
|
|
|
$this->username = $username; |
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Sets user password. |
105
|
|
|
* |
106
|
|
|
* @param string $password password |
107
|
|
|
* @return object \LibSSH2\Configuration object |
108
|
|
|
*/ |
109
|
|
|
final public function set_password($password) |
110
|
|
|
{ |
111
|
|
|
$this->password = $password; |
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Sets RSA public key. |
117
|
|
|
* |
118
|
|
|
* @param string $publickey RSA public key |
119
|
|
|
* @return object \LibSSH2\Configuration object |
120
|
|
|
*/ |
121
|
|
|
final public function set_publickey($public_key) |
122
|
|
|
{ |
123
|
|
|
$this->publickey = $public_key; |
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Sets RSA private key. |
129
|
|
|
* |
130
|
|
|
* @param string $privatekey RSA private key |
131
|
|
|
* @return object \LibSSH2\Configuration object |
132
|
|
|
*/ |
133
|
|
|
final public function set_privatekey($private_key) |
134
|
|
|
{ |
135
|
|
|
$this->privatekey = $private_key; |
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Sets passphrase. |
141
|
|
|
* |
142
|
|
|
* @param string $passphrase passphrase |
143
|
|
|
* @return object \LibSSH2\Configuration object |
144
|
|
|
*/ |
145
|
|
|
final public function set_passphrase($passphrase) |
146
|
|
|
{ |
147
|
|
|
$this->passphrase = $passphrase; |
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Sets hostname. |
153
|
|
|
* |
154
|
|
|
* @param string $host hostname |
155
|
|
|
* @return object \LibSSH2\Configuration object |
156
|
|
|
*/ |
157
|
|
|
final public function set_host($host) |
158
|
|
|
{ |
159
|
|
|
$this->host = $host; |
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Sets port. |
165
|
|
|
* |
166
|
|
|
* @param int $port port |
167
|
|
|
* @return object \LibSSH2\Configuration object |
168
|
|
|
*/ |
169
|
|
|
final public function set_port($port) |
170
|
|
|
{ |
171
|
|
|
$this->port = $port; |
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Sets methods. |
177
|
|
|
* |
178
|
|
|
* @param array $methods remote connection methods |
179
|
|
|
* @return object \LibSSH2\Configuration object |
180
|
|
|
*/ |
181
|
|
|
final public function set_methods(array $methods = null) |
182
|
|
|
{ |
183
|
|
|
if ($methods !== null) |
184
|
|
|
{ |
185
|
|
|
$this->methods = $methods; |
186
|
|
|
} |
187
|
|
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Sets tunnel. |
192
|
|
|
* |
193
|
|
|
* @return object \LibSSH2\Configuration object |
194
|
|
|
*/ |
195
|
|
|
final public function set_tunnel() |
196
|
|
|
{ |
197
|
|
|
$this->tunnel = true; |
198
|
|
|
return $this; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Sets tunnel hostname. |
203
|
|
|
* |
204
|
|
|
* @param string $hostname hostname |
205
|
|
|
* @return object \LibSSH2\Configuration object |
206
|
|
|
*/ |
207
|
|
|
final public function set_tunnel_host($host) |
208
|
|
|
{ |
209
|
|
|
$this->set_tunnel(); |
210
|
|
|
$this->tunnel_host = $host; |
211
|
|
|
return $this; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Sets tunnel port. |
216
|
|
|
* |
217
|
|
|
* @param int $port port |
218
|
|
|
* @return object \LibSSH2\Configuration object |
219
|
|
|
*/ |
220
|
|
|
final public function set_tunnel_port($port) |
221
|
|
|
{ |
222
|
|
|
$this->port = $port; |
223
|
|
|
return $this; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Returns username. |
228
|
|
|
* |
229
|
|
|
* @return string username |
230
|
|
|
*/ |
231
|
|
|
final public function get_username() |
232
|
|
|
{ |
233
|
|
|
if (!isset($this->username)) |
234
|
|
|
{ |
235
|
|
|
throw new \RuntimeException('A username is required to authenticate to the remote server.'); |
236
|
|
|
} |
237
|
|
|
return $this->username; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Returns user's password. |
242
|
|
|
* |
243
|
|
|
* @return string password |
244
|
|
|
*/ |
245
|
|
|
final public function get_password() |
246
|
|
|
{ |
247
|
|
|
if (!isset($this->password)) |
248
|
|
|
{ |
249
|
|
|
throw new \RuntimeException('A password is required to authenticate to the remote server.'); |
250
|
|
|
} |
251
|
|
|
return $this->password; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Returns RSA public key. |
256
|
|
|
* |
257
|
|
|
* @return string RSA public key |
258
|
|
|
*/ |
259
|
|
|
final public function get_publickey() |
260
|
|
|
{ |
261
|
|
|
if (!isset($this->publickey)) |
262
|
|
|
{ |
263
|
|
|
throw new \RuntimeException('No public RSA key found.'); |
264
|
|
|
} |
265
|
|
|
return $this->publickey; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Returns RSA private key. |
270
|
|
|
* |
271
|
|
|
* @return string privatekey |
272
|
|
|
*/ |
273
|
|
|
final public function get_privatekey() |
274
|
|
|
{ |
275
|
|
|
if (!isset($this->privatekey)) |
276
|
|
|
{ |
277
|
|
|
throw new \RuntimeException('No private RSA key found.'); |
278
|
|
|
} |
279
|
|
|
return $this->privatekey; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Returns passphrase. |
284
|
|
|
* |
285
|
|
|
* @return string passphrase |
286
|
|
|
*/ |
287
|
|
|
final public function get_passphrase() |
288
|
|
|
{ |
289
|
|
|
return (!isset($this->passphrase)) ? '' : $this->passphrase; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Returns hostname. |
294
|
|
|
* |
295
|
|
|
* @return string hostname |
296
|
|
|
*/ |
297
|
|
|
final public function get_host() |
298
|
|
|
{ |
299
|
|
|
if (!isset($this->host)) |
300
|
|
|
{ |
301
|
|
|
throw new \RuntimeException('Unable to create remote connection; no hostname was set.'); |
302
|
|
|
} |
303
|
|
|
return $this->host; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Returns port. |
308
|
|
|
* |
309
|
|
|
* @return int port |
310
|
|
|
*/ |
311
|
|
|
final public function get_port() |
312
|
|
|
{ |
313
|
|
|
return $this->port; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Returns remote connection methods. |
318
|
|
|
* |
319
|
|
|
* @return array methods |
320
|
|
|
*/ |
321
|
|
|
final public function get_methods() |
322
|
|
|
{ |
323
|
|
|
if (isset($this->methods) || !empty($this->methods)) |
324
|
|
|
{ |
325
|
|
|
return $this->methods; |
326
|
|
|
} |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Returns tunnel. |
331
|
|
|
* |
332
|
|
|
* @return boolean |
333
|
|
|
*/ |
334
|
|
|
final public function get_tunnel() |
335
|
|
|
{ |
336
|
|
|
return $this->tunnel; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Returns tunnel hostname. |
341
|
|
|
* |
342
|
|
|
* @return string tunnel hostname |
343
|
|
|
*/ |
344
|
|
|
final public function get_tunnel_host() |
345
|
|
|
{ |
346
|
|
|
if (!isset($this->tunnel_host)) |
347
|
|
|
{ |
348
|
|
|
throw new \RuntimeException('A valid hostname must be set prior to attempting a tunnel connection.'); |
349
|
|
|
} |
350
|
|
|
return $this->tunnel_host; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Returns tunnel port. |
355
|
|
|
* |
356
|
|
|
* @return int port |
357
|
|
|
*/ |
358
|
|
|
final public function get_tunnel_port() |
359
|
|
|
{ |
360
|
|
|
return $this->tunnel_port; |
361
|
|
|
} |
362
|
|
|
} |
363
|
|
|
|