1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Knik\Flysystem\Gameap; |
4
|
|
|
|
5
|
|
|
use League\Flysystem\Adapter\AbstractAdapter; |
6
|
|
|
use League\Flysystem\SafeStorage; |
7
|
|
|
use League\Flysystem\AdapterInterface; |
8
|
|
|
|
9
|
|
|
use Knik\Gameap\GdaemonFiles; |
10
|
|
|
|
11
|
|
|
abstract class GameapAbstractAdapter extends AbstractAdapter |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var GdaemonFiles |
15
|
|
|
*/ |
16
|
|
|
protected $connection; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $host; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
protected $port = 31717; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var int |
30
|
|
|
*/ |
31
|
|
|
protected $timeout = 10; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var SafeStorage |
35
|
|
|
*/ |
36
|
|
|
protected $safeStorage; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $privateKey; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $serverCertificate; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
protected $localCertificate; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var int |
55
|
|
|
*/ |
56
|
|
|
protected $permPublic = 0744; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var int |
60
|
|
|
*/ |
61
|
|
|
protected $permPrivate = 0700; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var array |
65
|
|
|
*/ |
66
|
|
|
protected $configurable = []; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Connect to the server. |
70
|
|
|
*/ |
71
|
3 |
|
public function connect() |
72
|
|
|
{ |
73
|
3 |
|
$this->connection = new GdaemonFiles([ |
74
|
3 |
|
'host' => $this->getHost(), |
75
|
3 |
|
'port' => $this->getPort(), |
76
|
3 |
|
'username' => $this->getUsername(), |
77
|
3 |
|
'password' => $this->getPassword(), |
78
|
3 |
|
'localCertificate' => $this->getLocalCertificate(), |
79
|
3 |
|
'serverCertificate' => $this->getServerCertificate(), |
80
|
3 |
|
'privateKey' => $this->getPrivateKey(), |
81
|
3 |
|
'privateKeyPass' => $this->getPrivateKeyPass(), |
82
|
3 |
|
'timeout' => $this->getTimeout(), |
83
|
2 |
|
]); |
84
|
1 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Disconnect |
88
|
|
|
*/ |
89
|
6 |
|
public function disconnect() |
90
|
|
|
{ |
91
|
6 |
|
if ($this->isConnected()) { |
92
|
3 |
|
$this->connection->disconnect(); |
93
|
2 |
|
} |
94
|
6 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return GDaemonFiles |
98
|
|
|
*/ |
99
|
70 |
|
public function getConnection() |
100
|
|
|
{ |
101
|
70 |
|
$tries = 0; |
102
|
|
|
|
103
|
70 |
|
while ( ! $this->isConnected() && $tries < 3) { |
104
|
3 |
|
$tries++; |
105
|
3 |
|
$this->disconnect(); |
106
|
3 |
|
$this->connect(); |
107
|
2 |
|
} |
108
|
|
|
|
109
|
70 |
|
return $this->connection; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param GdaemonFiles $connection |
114
|
|
|
*/ |
115
|
3 |
|
public function setConnection($connection) |
116
|
|
|
{ |
117
|
3 |
|
$this->connection = $connection; |
118
|
3 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Constructor. |
122
|
|
|
* |
123
|
|
|
* @param array $config |
124
|
|
|
*/ |
125
|
3 |
|
public function __construct(array $config) |
126
|
|
|
{ |
127
|
3 |
|
$this->safeStorage = new SafeStorage(); |
128
|
3 |
|
$this->setConfig($config); |
129
|
3 |
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Set the config. |
133
|
|
|
* |
134
|
|
|
* @param array $config |
135
|
|
|
* |
136
|
|
|
* @return $this |
137
|
|
|
*/ |
138
|
6 |
|
public function setConfig(array $config) |
139
|
|
|
{ |
140
|
6 |
|
foreach ($this->configurable as $setting) { |
141
|
6 |
|
if ( ! isset($config[$setting])) { |
142
|
6 |
|
continue; |
143
|
|
|
} |
144
|
|
|
|
145
|
6 |
|
$method = 'set' . ucfirst($setting); |
146
|
|
|
|
147
|
6 |
|
if (method_exists($this, $method)) { |
148
|
6 |
|
$this->$method($config[$setting]); |
149
|
4 |
|
} |
150
|
4 |
|
} |
151
|
|
|
|
152
|
6 |
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Disconnect on destruction. |
157
|
|
|
*/ |
158
|
3 |
|
public function __destruct() |
159
|
|
|
{ |
160
|
3 |
|
$this->disconnect(); |
161
|
3 |
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Returns the username. |
165
|
|
|
* |
166
|
|
|
* @return string username |
167
|
|
|
*/ |
168
|
12 |
|
public function getUsername() |
169
|
|
|
{ |
170
|
12 |
|
$username = $this->safeStorage->retrieveSafely('username'); |
171
|
|
|
|
172
|
12 |
|
return $username !== null ? $username : 'anonymous'; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Set username. |
177
|
|
|
* |
178
|
|
|
* @param string $username |
179
|
|
|
* |
180
|
|
|
* @return $this |
181
|
|
|
*/ |
182
|
9 |
|
public function setUsername($username) |
183
|
|
|
{ |
184
|
9 |
|
$this->safeStorage->storeSafely('username', $username); |
185
|
|
|
|
186
|
9 |
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Returns the password. |
191
|
|
|
* |
192
|
|
|
* @return string password |
193
|
|
|
*/ |
194
|
12 |
|
public function getPassword() |
195
|
|
|
{ |
196
|
12 |
|
return $this->safeStorage->retrieveSafely('password'); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Set the password. |
201
|
|
|
* |
202
|
|
|
* @param string $password |
203
|
|
|
* |
204
|
|
|
* @return $this |
205
|
|
|
*/ |
206
|
9 |
|
public function setPassword($password) |
207
|
|
|
{ |
208
|
9 |
|
$this->safeStorage->storeSafely('password', $password); |
209
|
|
|
|
210
|
9 |
|
return $this; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Returns the password. |
215
|
|
|
* |
216
|
|
|
* @return string password |
217
|
|
|
*/ |
218
|
12 |
|
public function getPrivateKey() |
219
|
|
|
{ |
220
|
12 |
|
return $this->privateKey; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Set the private key (string or path to local file). |
225
|
|
|
* |
226
|
|
|
* @param string $key |
227
|
|
|
* |
228
|
|
|
* @return $this |
229
|
|
|
*/ |
230
|
9 |
|
public function setPrivateKey($key) |
231
|
|
|
{ |
232
|
9 |
|
$this->privateKey = $key; |
233
|
|
|
|
234
|
9 |
|
return $this; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Returns the password. |
239
|
|
|
* |
240
|
|
|
* @return string password |
241
|
|
|
*/ |
242
|
12 |
|
public function getPrivateKeyPass() |
243
|
|
|
{ |
244
|
12 |
|
return $this->safeStorage->retrieveSafely('privateKeyPass'); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Set the password. |
249
|
|
|
* |
250
|
|
|
* @param string $password |
|
|
|
|
251
|
|
|
* |
252
|
|
|
* @return $this |
253
|
|
|
*/ |
254
|
9 |
|
public function setPrivateKeyPass($privateKeyPass) |
255
|
|
|
{ |
256
|
9 |
|
$this->safeStorage->storeSafely('privateKeyPass', $privateKeyPass); |
257
|
|
|
|
258
|
9 |
|
return $this; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Returns the host. |
263
|
|
|
* |
264
|
|
|
* @return string |
265
|
|
|
*/ |
266
|
9 |
|
public function getHost() |
267
|
|
|
{ |
268
|
9 |
|
return $this->host; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Set the host. |
273
|
|
|
* |
274
|
|
|
* @param string $host |
275
|
|
|
* |
276
|
|
|
* @return $this |
277
|
|
|
*/ |
278
|
9 |
|
public function setHost($host) |
279
|
|
|
{ |
280
|
9 |
|
$this->host = $host; |
281
|
|
|
|
282
|
9 |
|
return $this; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Returns the port. |
287
|
|
|
* |
288
|
|
|
* @return int |
289
|
|
|
*/ |
290
|
9 |
|
public function getPort() |
291
|
|
|
{ |
292
|
9 |
|
return $this->port; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Set the port. |
297
|
|
|
* |
298
|
|
|
* @param int|string $port |
299
|
|
|
* |
300
|
|
|
* @return $this |
301
|
|
|
*/ |
302
|
9 |
|
public function setPort($port) |
303
|
|
|
{ |
304
|
9 |
|
$this->port = (int) $port; |
305
|
|
|
|
306
|
9 |
|
return $this; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Returns the amount of seconds before the connection will timeout. |
311
|
|
|
* |
312
|
|
|
* @return int |
313
|
|
|
*/ |
314
|
9 |
|
public function getTimeout() |
315
|
|
|
{ |
316
|
9 |
|
return $this->timeout; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Set the amount of seconds before the connection should timeout. |
321
|
|
|
* |
322
|
|
|
* @param int $timeout |
323
|
|
|
* |
324
|
|
|
* @return $this |
325
|
|
|
*/ |
326
|
3 |
|
public function setTimeout($timeout) |
327
|
|
|
{ |
328
|
3 |
|
$this->timeout = (int) $timeout; |
329
|
|
|
|
330
|
3 |
|
return $this; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Returns the root folder to work from. |
335
|
|
|
* |
336
|
|
|
* @return string |
337
|
|
|
*/ |
338
|
6 |
|
public function getRoot() |
339
|
|
|
{ |
340
|
6 |
|
return $this->getPathPrefix(); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Set the root folder to work from. |
345
|
|
|
* |
346
|
|
|
* @param string $root |
347
|
|
|
* |
348
|
|
|
* @return $this |
349
|
|
|
*/ |
350
|
6 |
|
public function setRoot($root) |
351
|
|
|
{ |
352
|
6 |
|
$this->setPathPrefix($root); |
353
|
6 |
|
return $this; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* @return string |
358
|
|
|
*/ |
359
|
6 |
|
public function getLocalCertificate() |
360
|
|
|
{ |
361
|
6 |
|
return $this->localCertificate; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* @param $localCertificate |
366
|
|
|
* @return $this |
367
|
|
|
*/ |
368
|
3 |
|
public function setLocalCertificate($localCertificate) |
369
|
|
|
{ |
370
|
3 |
|
$this->localCertificate = $localCertificate; |
371
|
3 |
|
return $this; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* @return string |
376
|
|
|
*/ |
377
|
6 |
|
public function getServerCertificate() |
378
|
|
|
{ |
379
|
6 |
|
return $this->serverCertificate; |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* @param $serverCertificate |
384
|
|
|
* @return $this |
385
|
|
|
*/ |
386
|
3 |
|
public function setServerCertificate($serverCertificate) |
387
|
|
|
{ |
388
|
3 |
|
$this->serverCertificate = $serverCertificate; |
389
|
3 |
|
return $this; |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* Normalize a listing response. |
394
|
|
|
* |
395
|
|
|
* @param string $path |
396
|
|
|
* @param array $object |
397
|
|
|
* |
398
|
|
|
* @return array |
399
|
|
|
*/ |
400
|
6 |
|
protected function normalizeListingObject($path, array $object) |
401
|
|
|
{ |
402
|
6 |
|
$type = $object['type']; |
403
|
6 |
|
$timestamp = $object['mtime']; |
404
|
|
|
|
405
|
6 |
|
if ($type === 'dir') { |
406
|
6 |
|
return compact('path', 'timestamp', 'type'); |
407
|
|
|
} |
408
|
|
|
|
409
|
6 |
|
$visibility = $object['permissions'] & (0777 ^ $this->permPrivate) |
410
|
6 |
|
? AdapterInterface::VISIBILITY_PUBLIC |
411
|
6 |
|
: AdapterInterface::VISIBILITY_PRIVATE; |
412
|
|
|
|
413
|
6 |
|
$size = (int) $object['size']; |
414
|
|
|
|
415
|
6 |
|
return compact('path', 'timestamp', 'type', 'visibility', 'size'); |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* Check if a connection is active. |
420
|
|
|
* |
421
|
|
|
* @return bool |
422
|
|
|
*/ |
423
|
|
|
abstract public function isConnected(); |
424
|
|
|
} |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.