|
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
|
|
|
public function connect() |
|
72
|
|
|
{ |
|
73
|
|
|
$this->connection = new GdaemonFiles([ |
|
74
|
|
|
'host' => $this->getHost(), |
|
75
|
|
|
'port' => $this->getPort(), |
|
76
|
|
|
'username' => $this->getUsername(), |
|
77
|
|
|
'password' => $this->getPassword(), |
|
78
|
|
|
'localCertificate' => $this->getLocalCertificate(), |
|
79
|
|
|
'serverCertificate' => $this->getServerCertificate(), |
|
80
|
|
|
'privateKey' => $this->getPrivateKey(), |
|
81
|
|
|
'privateKeyPass' => $this->getPrivateKeyPass(), |
|
82
|
|
|
'timeout' => $this->getTimeout(), |
|
83
|
|
|
]); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Disconnect |
|
88
|
|
|
*/ |
|
89
|
3 |
|
public function disconnect() |
|
90
|
|
|
{ |
|
91
|
3 |
|
if ($this->isConnected()) { |
|
92
|
3 |
|
$this->connection->disconnect(); |
|
93
|
1 |
|
} |
|
94
|
3 |
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return GDaemonFiles |
|
98
|
|
|
*/ |
|
99
|
63 |
|
public function getConnection() |
|
100
|
|
|
{ |
|
101
|
63 |
|
$tries = 0; |
|
102
|
|
|
|
|
103
|
63 |
|
while ( ! $this->isConnected() && $tries < 3) { |
|
104
|
|
|
$tries++; |
|
105
|
|
|
$this->disconnect(); |
|
106
|
|
|
$this->connect(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
63 |
|
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
|
|
|
public function __construct(array $config) |
|
126
|
|
|
{ |
|
127
|
|
|
$this->safeStorage = new SafeStorage(); |
|
128
|
|
|
$this->setConfig($config); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Set the config. |
|
133
|
|
|
* |
|
134
|
|
|
* @param array $config |
|
135
|
|
|
* |
|
136
|
|
|
* @return $this |
|
137
|
|
|
*/ |
|
138
|
3 |
|
public function setConfig(array $config) |
|
139
|
|
|
{ |
|
140
|
3 |
|
foreach ($this->configurable as $setting) { |
|
141
|
3 |
|
if ( ! isset($config[$setting])) { |
|
142
|
3 |
|
continue; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
3 |
|
$method = 'set' . ucfirst($setting); |
|
146
|
|
|
|
|
147
|
3 |
|
if (method_exists($this, $method)) { |
|
148
|
3 |
|
$this->$method($config[$setting]); |
|
149
|
1 |
|
} |
|
150
|
1 |
|
} |
|
151
|
|
|
|
|
152
|
3 |
|
return $this; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Disconnect on destruction. |
|
157
|
|
|
*/ |
|
158
|
|
|
public function __destruct() |
|
159
|
|
|
{ |
|
160
|
|
|
$this->disconnect(); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Returns the username. |
|
165
|
|
|
* |
|
166
|
|
|
* @return string username |
|
167
|
|
|
*/ |
|
168
|
6 |
|
public function getUsername() |
|
169
|
|
|
{ |
|
170
|
6 |
|
$username = $this->safeStorage->retrieveSafely('username'); |
|
171
|
|
|
|
|
172
|
6 |
|
return $username !== null ? $username : 'anonymous'; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Set username. |
|
177
|
|
|
* |
|
178
|
|
|
* @param string $username |
|
179
|
|
|
* |
|
180
|
|
|
* @return $this |
|
181
|
|
|
*/ |
|
182
|
6 |
|
public function setUsername($username) |
|
183
|
|
|
{ |
|
184
|
6 |
|
$this->safeStorage->storeSafely('username', $username); |
|
185
|
|
|
|
|
186
|
6 |
|
return $this; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Returns the password. |
|
191
|
|
|
* |
|
192
|
|
|
* @return string password |
|
193
|
|
|
*/ |
|
194
|
6 |
|
public function getPassword() |
|
195
|
|
|
{ |
|
196
|
6 |
|
return $this->safeStorage->retrieveSafely('password'); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Set the password. |
|
201
|
|
|
* |
|
202
|
|
|
* @param string $password |
|
203
|
|
|
* |
|
204
|
|
|
* @return $this |
|
205
|
|
|
*/ |
|
206
|
6 |
|
public function setPassword($password) |
|
207
|
|
|
{ |
|
208
|
6 |
|
$this->safeStorage->storeSafely('password', $password); |
|
209
|
|
|
|
|
210
|
6 |
|
return $this; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Returns the password. |
|
215
|
|
|
* |
|
216
|
|
|
* @return string password |
|
217
|
|
|
*/ |
|
218
|
6 |
|
public function getPrivateKey() |
|
219
|
|
|
{ |
|
220
|
6 |
|
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
|
6 |
|
public function setPrivateKey($key) |
|
231
|
|
|
{ |
|
232
|
6 |
|
$this->privateKey = $key; |
|
233
|
|
|
|
|
234
|
6 |
|
return $this; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Returns the password. |
|
239
|
|
|
* |
|
240
|
|
|
* @return string password |
|
241
|
|
|
*/ |
|
242
|
6 |
|
public function getPrivateKeyPass() |
|
243
|
|
|
{ |
|
244
|
6 |
|
return $this->safeStorage->retrieveSafely('privateKeyPass'); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Set the password. |
|
249
|
|
|
* |
|
250
|
|
|
* @param string $password |
|
|
|
|
|
|
251
|
|
|
* |
|
252
|
|
|
* @return $this |
|
253
|
|
|
*/ |
|
254
|
6 |
|
public function setPrivateKeyPass($privateKeyPass) |
|
255
|
|
|
{ |
|
256
|
6 |
|
$this->safeStorage->storeSafely('privateKeyPass', $privateKeyPass); |
|
257
|
|
|
|
|
258
|
6 |
|
return $this; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Returns the host. |
|
263
|
|
|
* |
|
264
|
|
|
* @return string |
|
265
|
|
|
*/ |
|
266
|
6 |
|
public function getHost() |
|
267
|
|
|
{ |
|
268
|
6 |
|
return $this->host; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* Set the host. |
|
273
|
|
|
* |
|
274
|
|
|
* @param string $host |
|
275
|
|
|
* |
|
276
|
|
|
* @return $this |
|
277
|
|
|
*/ |
|
278
|
6 |
|
public function setHost($host) |
|
279
|
|
|
{ |
|
280
|
6 |
|
$this->host = $host; |
|
281
|
|
|
|
|
282
|
6 |
|
return $this; |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* Returns the port. |
|
287
|
|
|
* |
|
288
|
|
|
* @return int |
|
289
|
|
|
*/ |
|
290
|
6 |
|
public function getPort() |
|
291
|
|
|
{ |
|
292
|
6 |
|
return $this->port; |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* Set the port. |
|
297
|
|
|
* |
|
298
|
|
|
* @param int|string $port |
|
299
|
|
|
* |
|
300
|
|
|
* @return $this |
|
301
|
|
|
*/ |
|
302
|
6 |
|
public function setPort($port) |
|
303
|
|
|
{ |
|
304
|
6 |
|
$this->port = (int) $port; |
|
305
|
|
|
|
|
306
|
6 |
|
return $this; |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
/** |
|
310
|
|
|
* Returns the amount of seconds before the connection will timeout. |
|
311
|
|
|
* |
|
312
|
|
|
* @return int |
|
313
|
|
|
*/ |
|
314
|
6 |
|
public function getTimeout() |
|
315
|
|
|
{ |
|
316
|
6 |
|
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
|
3 |
|
public function getRoot() |
|
339
|
|
|
{ |
|
340
|
3 |
|
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
|
3 |
|
public function setRoot($root) |
|
351
|
|
|
{ |
|
352
|
3 |
|
$this->setPathPrefix($root); |
|
353
|
3 |
|
return $this; |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
/** |
|
357
|
|
|
* @return string |
|
358
|
|
|
*/ |
|
359
|
3 |
|
public function getLocalCertificate() |
|
360
|
|
|
{ |
|
361
|
3 |
|
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
|
3 |
|
public function getServerCertificate() |
|
378
|
|
|
{ |
|
379
|
3 |
|
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
|
3 |
|
protected function normalizeListingObject($path, array $object) |
|
401
|
|
|
{ |
|
402
|
3 |
|
$type = $object['type']; |
|
403
|
3 |
|
$timestamp = $object['mtime']; |
|
404
|
|
|
|
|
405
|
3 |
|
if ($type === 'dir') { |
|
406
|
3 |
|
return compact('path', 'timestamp', 'type'); |
|
407
|
|
|
} |
|
408
|
|
|
|
|
409
|
3 |
|
$visibility = $object['permissions'] & (0777 ^ $this->permPrivate) |
|
410
|
3 |
|
? AdapterInterface::VISIBILITY_PUBLIC |
|
411
|
3 |
|
: AdapterInterface::VISIBILITY_PRIVATE; |
|
412
|
|
|
|
|
413
|
3 |
|
$size = (int) $object['size']; |
|
414
|
|
|
|
|
415
|
3 |
|
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
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.