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
|
|
|
protected $privateKey; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var int |
42
|
|
|
*/ |
43
|
|
|
protected $permPublic = 0744; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var int |
47
|
|
|
*/ |
48
|
|
|
protected $permPrivate = 0700; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Connect to the server. |
52
|
|
|
*/ |
53
|
|
|
public function connect() |
54
|
|
|
{ |
55
|
|
|
$this->connection = new GdaemonFiles([ |
56
|
|
|
'host' => $this->getHost(), |
57
|
|
|
'port' => $this->getPort(), |
58
|
|
|
'username' => $this->getUsername(), |
59
|
|
|
'password' => $this->getPassword(), |
60
|
|
|
'privateKey' => $this->getPrivateKey(), |
61
|
|
|
'privateKeyPass' => $this->getPrivateKeyPass(), |
62
|
|
|
'timeout' => $this->getTimeout(), |
63
|
|
|
]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Disconnect |
68
|
|
|
*/ |
69
|
3 |
|
public function disconnect() |
70
|
|
|
{ |
71
|
3 |
|
if ($this->isConnected()) { |
72
|
3 |
|
$this->connection->disconnect(); |
73
|
1 |
|
} |
74
|
3 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return GDaemonFiles |
78
|
|
|
*/ |
79
|
63 |
|
public function getConnection() |
80
|
|
|
{ |
81
|
63 |
|
$tries = 0; |
82
|
|
|
|
83
|
63 |
|
while ( ! $this->isConnected() && $tries < 3) { |
84
|
|
|
$tries++; |
85
|
|
|
$this->disconnect(); |
86
|
|
|
$this->connect(); |
87
|
|
|
} |
88
|
|
|
|
89
|
63 |
|
return $this->connection; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param GdaemonFiles $connection |
94
|
|
|
*/ |
95
|
3 |
|
public function setConnection($connection) |
96
|
|
|
{ |
97
|
3 |
|
$this->connection = $connection; |
98
|
3 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Constructor. |
102
|
|
|
* |
103
|
|
|
* @param array $config |
104
|
|
|
*/ |
105
|
|
|
public function __construct(array $config) |
106
|
|
|
{ |
107
|
|
|
$this->safeStorage = new SafeStorage(); |
108
|
|
|
$this->setConfig($config); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Set the config. |
113
|
|
|
* |
114
|
|
|
* @param array $config |
115
|
|
|
* |
116
|
|
|
* @return $this |
117
|
|
|
*/ |
118
|
3 |
|
public function setConfig(array $config) |
119
|
|
|
{ |
120
|
3 |
|
foreach ($this->configurable as $setting) { |
|
|
|
|
121
|
3 |
|
if ( ! isset($config[$setting])) { |
122
|
3 |
|
continue; |
123
|
|
|
} |
124
|
|
|
|
125
|
3 |
|
$method = 'set' . ucfirst($setting); |
126
|
|
|
|
127
|
3 |
|
if (method_exists($this, $method)) { |
128
|
3 |
|
$this->$method($config[$setting]); |
129
|
1 |
|
} |
130
|
1 |
|
} |
131
|
|
|
|
132
|
3 |
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Disconnect on destruction. |
137
|
|
|
*/ |
138
|
|
|
public function __destruct() |
139
|
|
|
{ |
140
|
|
|
$this->disconnect(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Returns the username. |
145
|
|
|
* |
146
|
|
|
* @return string username |
147
|
|
|
*/ |
148
|
6 |
|
public function getUsername() |
149
|
|
|
{ |
150
|
6 |
|
$username = $this->safeStorage->retrieveSafely('username'); |
151
|
|
|
|
152
|
6 |
|
return $username !== null ? $username : 'anonymous'; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Set username. |
157
|
|
|
* |
158
|
|
|
* @param string $username |
159
|
|
|
* |
160
|
|
|
* @return $this |
161
|
|
|
*/ |
162
|
6 |
|
public function setUsername($username) |
163
|
|
|
{ |
164
|
6 |
|
$this->safeStorage->storeSafely('username', $username); |
165
|
|
|
|
166
|
6 |
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Returns the password. |
171
|
|
|
* |
172
|
|
|
* @return string password |
173
|
|
|
*/ |
174
|
6 |
|
public function getPassword() |
175
|
|
|
{ |
176
|
6 |
|
return $this->safeStorage->retrieveSafely('password'); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Set the password. |
181
|
|
|
* |
182
|
|
|
* @param string $password |
183
|
|
|
* |
184
|
|
|
* @return $this |
185
|
|
|
*/ |
186
|
6 |
|
public function setPassword($password) |
187
|
|
|
{ |
188
|
6 |
|
$this->safeStorage->storeSafely('password', $password); |
189
|
|
|
|
190
|
6 |
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Returns the password. |
195
|
|
|
* |
196
|
|
|
* @return string password |
197
|
|
|
*/ |
198
|
6 |
|
public function getPrivateKey() |
199
|
|
|
{ |
200
|
6 |
|
return $this->privateKey; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Set the private key (string or path to local file). |
205
|
|
|
* |
206
|
|
|
* @param string $key |
207
|
|
|
* |
208
|
|
|
* @return $this |
209
|
|
|
*/ |
210
|
6 |
|
public function setPrivateKey($key) |
211
|
|
|
{ |
212
|
6 |
|
$this->privateKey = $key; |
213
|
|
|
|
214
|
6 |
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Returns the password. |
219
|
|
|
* |
220
|
|
|
* @return string password |
221
|
|
|
*/ |
222
|
6 |
|
public function getPrivateKeyPass() |
223
|
|
|
{ |
224
|
6 |
|
return $this->safeStorage->retrieveSafely('privateKeyPass'); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Set the password. |
229
|
|
|
* |
230
|
|
|
* @param string $password |
|
|
|
|
231
|
|
|
* |
232
|
|
|
* @return $this |
233
|
|
|
*/ |
234
|
6 |
|
public function setPrivateKeyPass($privateKeyPass) |
235
|
|
|
{ |
236
|
6 |
|
$this->safeStorage->storeSafely('privateKeyPass', $privateKeyPass); |
237
|
|
|
|
238
|
6 |
|
return $this; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Returns the host. |
243
|
|
|
* |
244
|
|
|
* @return string |
245
|
|
|
*/ |
246
|
6 |
|
public function getHost() |
247
|
|
|
{ |
248
|
6 |
|
return $this->host; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Set the host. |
253
|
|
|
* |
254
|
|
|
* @param string $host |
255
|
|
|
* |
256
|
|
|
* @return $this |
257
|
|
|
*/ |
258
|
6 |
|
public function setHost($host) |
259
|
|
|
{ |
260
|
6 |
|
$this->host = $host; |
261
|
|
|
|
262
|
6 |
|
return $this; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Returns the port. |
267
|
|
|
* |
268
|
|
|
* @return int |
269
|
|
|
*/ |
270
|
6 |
|
public function getPort() |
271
|
|
|
{ |
272
|
6 |
|
return $this->port; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Set the port. |
277
|
|
|
* |
278
|
|
|
* @param int|string $port |
279
|
|
|
* |
280
|
|
|
* @return $this |
281
|
|
|
*/ |
282
|
6 |
|
public function setPort($port) |
283
|
|
|
{ |
284
|
6 |
|
$this->port = (int) $port; |
285
|
|
|
|
286
|
6 |
|
return $this; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Returns the amount of seconds before the connection will timeout. |
291
|
|
|
* |
292
|
|
|
* @return int |
293
|
|
|
*/ |
294
|
6 |
|
public function getTimeout() |
295
|
|
|
{ |
296
|
6 |
|
return $this->timeout; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Set the amount of seconds before the connection should timeout. |
301
|
|
|
* |
302
|
|
|
* @param int $timeout |
303
|
|
|
* |
304
|
|
|
* @return $this |
305
|
|
|
*/ |
306
|
3 |
|
public function setTimeout($timeout) |
307
|
|
|
{ |
308
|
3 |
|
$this->timeout = (int) $timeout; |
309
|
|
|
|
310
|
3 |
|
return $this; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Returns the root folder to work from. |
315
|
|
|
* |
316
|
|
|
* @return string |
317
|
|
|
*/ |
318
|
3 |
|
public function getRoot() |
319
|
|
|
{ |
320
|
3 |
|
return $this->getPathPrefix(); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Set the root folder to work from. |
325
|
|
|
* |
326
|
|
|
* @param string $root |
327
|
|
|
* |
328
|
|
|
* @return $this |
329
|
|
|
*/ |
330
|
3 |
|
public function setRoot($root) |
331
|
|
|
{ |
332
|
3 |
|
$this->setPathPrefix($root); |
333
|
3 |
|
return $this; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Normalize a listing response. |
338
|
|
|
* |
339
|
|
|
* @param string $path |
340
|
|
|
* @param array $object |
341
|
|
|
* |
342
|
|
|
* @return array |
343
|
|
|
*/ |
344
|
3 |
|
protected function normalizeListingObject($path, array $object) |
345
|
|
|
{ |
346
|
3 |
|
$type = $object['type']; |
347
|
3 |
|
$timestamp = $object['mtime']; |
348
|
|
|
|
349
|
3 |
|
if ($type === 'dir') { |
350
|
3 |
|
return compact('path', 'timestamp', 'type'); |
351
|
|
|
} |
352
|
|
|
|
353
|
3 |
|
$visibility = $object['permissions'] & (0777 ^ $this->permPrivate) |
354
|
3 |
|
? AdapterInterface::VISIBILITY_PUBLIC |
355
|
3 |
|
: AdapterInterface::VISIBILITY_PRIVATE; |
356
|
|
|
|
357
|
3 |
|
$size = (int) $object['size']; |
358
|
|
|
|
359
|
3 |
|
return compact('path', 'timestamp', 'type', 'visibility', 'size'); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Check if a connection is active. |
364
|
|
|
* |
365
|
|
|
* @return bool |
366
|
|
|
*/ |
367
|
|
|
abstract public function isConnected(); |
368
|
|
|
} |
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: