1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Circles - Bring cloud-users closer together. |
6
|
|
|
* |
7
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
8
|
|
|
* later. See the COPYING file. |
9
|
|
|
* |
10
|
|
|
* @author Maxence Lange <[email protected]> |
11
|
|
|
* @copyright 2017 |
12
|
|
|
* @license GNU AGPL version 3 or any later version |
13
|
|
|
* |
14
|
|
|
* This program is free software: you can redistribute it and/or modify |
15
|
|
|
* it under the terms of the GNU Affero General Public License as |
16
|
|
|
* published by the Free Software Foundation, either version 3 of the |
17
|
|
|
* License, or (at your option) any later version. |
18
|
|
|
* |
19
|
|
|
* This program is distributed in the hope that it will be useful, |
20
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
21
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22
|
|
|
* GNU Affero General Public License for more details. |
23
|
|
|
* |
24
|
|
|
* You should have received a copy of the GNU Affero General Public License |
25
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
26
|
|
|
* |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
namespace OCA\Circles\Command; |
31
|
|
|
|
32
|
|
|
use daita\MySmallPhpTools\Exceptions\RequestNetworkException; |
33
|
|
|
use daita\MySmallPhpTools\Model\Nextcloud\nc22\NC22Request; |
34
|
|
|
use daita\MySmallPhpTools\Model\Request; |
35
|
|
|
use daita\MySmallPhpTools\Traits\Nextcloud\nc22\TNC22Request; |
36
|
|
|
use daita\MySmallPhpTools\Traits\TArrayTools; |
37
|
|
|
use daita\MySmallPhpTools\Traits\TStringTools; |
38
|
|
|
use Exception; |
39
|
|
|
use OC\Core\Command\Base; |
40
|
|
|
use OCA\Circles\AppInfo\Application; |
41
|
|
|
use OCA\Circles\AppInfo\Capabilities; |
42
|
|
|
use OCA\Circles\Exceptions\FederatedEventException; |
43
|
|
|
use OCA\Circles\Exceptions\FederatedItemException; |
44
|
|
|
use OCA\Circles\Exceptions\InitiatorNotConfirmedException; |
45
|
|
|
use OCA\Circles\Exceptions\OwnerNotFoundException; |
46
|
|
|
use OCA\Circles\Exceptions\RemoteInstanceException; |
47
|
|
|
use OCA\Circles\Exceptions\RemoteNotFoundException; |
48
|
|
|
use OCA\Circles\Exceptions\RemoteResourceNotFoundException; |
49
|
|
|
use OCA\Circles\Exceptions\UnknownRemoteException; |
50
|
|
|
use OCA\Circles\FederatedItems\LoopbackTest; |
51
|
|
|
use OCA\Circles\Model\Federated\FederatedEvent; |
52
|
|
|
use OCA\Circles\Service\ConfigService; |
53
|
|
|
use OCA\Circles\Service\FederatedEventService; |
54
|
|
|
use OCA\Circles\Service\RemoteService; |
55
|
|
|
use OCA\Circles\Service\RemoteStreamService; |
56
|
|
|
use OCA\Circles\Service\RemoteUpstreamService; |
57
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
58
|
|
|
use Symfony\Component\Console\Input\InputOption; |
59
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
60
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
61
|
|
|
use Symfony\Component\Console\Question\Question; |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Class CirclesCheck |
66
|
|
|
* |
67
|
|
|
* @package OCA\Circles\Command |
68
|
|
|
*/ |
69
|
|
|
class CirclesCheck extends Base { |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
use TStringTools; |
73
|
|
|
use TArrayTools; |
74
|
|
|
use TNC22Request; |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
static $checks = [ |
78
|
|
|
'internal', |
79
|
|
|
'frontal', |
80
|
|
|
'loopback' |
81
|
|
|
]; |
82
|
|
|
|
83
|
|
|
/** @var Capabilities */ |
84
|
|
|
private $capabilities; |
85
|
|
|
|
86
|
|
|
/** @var FederatedEventService */ |
87
|
|
|
private $federatedEventService; |
88
|
|
|
|
89
|
|
|
/** @var RemoteService */ |
90
|
|
|
private $remoteService; |
91
|
|
|
|
92
|
|
|
/** @var RemoteStreamService */ |
93
|
|
|
private $remoteStreamService; |
94
|
|
|
|
95
|
|
|
/** @var RemoteUpstreamService */ |
96
|
|
|
private $remoteUpstreamService; |
97
|
|
|
|
98
|
|
|
/** @var ConfigService */ |
99
|
|
|
private $configService; |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
/** @var int */ |
103
|
|
|
private $delay = 5; |
104
|
|
|
|
105
|
|
|
/** @var array */ |
106
|
|
|
private $sessions = []; |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* CirclesCheck constructor. |
111
|
|
|
* |
112
|
|
|
* @param Capabilities $capabilities |
113
|
|
|
* @param FederatedEvent $federatedEventService |
114
|
|
|
* @param RemoteService $remoteService |
115
|
|
|
* @param RemoteStreamService $remoteStreamService |
116
|
|
|
* @param RemoteUpstreamService $remoteUpstreamService |
117
|
|
|
* @param ConfigService $configService |
118
|
|
|
*/ |
119
|
|
|
public function __construct( |
120
|
|
|
Capabilities $capabilities, |
121
|
|
|
FederatedEventService $federatedEventService, |
122
|
|
|
RemoteService $remoteService, |
123
|
|
|
RemoteStreamService $remoteStreamService, |
124
|
|
|
RemoteUpstreamService $remoteUpstreamService, |
125
|
|
|
ConfigService $configService |
126
|
|
|
) { |
127
|
|
|
parent::__construct(); |
128
|
|
|
|
129
|
|
|
$this->capabilities = $capabilities; |
130
|
|
|
$this->federatedEventService = $federatedEventService; |
131
|
|
|
$this->remoteService = $remoteService; |
132
|
|
|
$this->remoteStreamService = $remoteStreamService; |
133
|
|
|
$this->remoteUpstreamService = $remoteUpstreamService; |
134
|
|
|
$this->configService = $configService; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
protected function configure() { |
139
|
|
|
parent::configure(); |
140
|
|
|
$this->setName('circles:check') |
141
|
|
|
->setDescription('Checking your configuration') |
142
|
|
|
->addOption('delay', 'd', InputOption::VALUE_REQUIRED, 'delay before checking result') |
143
|
|
|
->addOption('capabilities', '', InputOption::VALUE_NONE, 'listing app\'s capabilities') |
144
|
|
|
->addOption('type', '', InputOption::VALUE_REQUIRED, 'configuration to check', '') |
145
|
|
|
->addOption('test', '', InputOption::VALUE_REQUIRED, 'specify an url to test', ''); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param InputInterface $input |
151
|
|
|
* @param OutputInterface $output |
152
|
|
|
* |
153
|
|
|
* @return int |
154
|
|
|
* @throws Exception |
155
|
|
|
*/ |
156
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int { |
157
|
|
|
if ($input->getOption('capabilities')) { |
158
|
|
|
$capabilities = $this->getArray('circles', $this->capabilities->getCapabilities(true)); |
159
|
|
|
$output->writeln(json_encode($capabilities, JSON_PRETTY_PRINT)); |
160
|
|
|
|
161
|
|
|
return 0; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if ($input->getOption('delay')) { |
165
|
|
|
$this->delay = (int)$input->getOption('delay'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$this->configService->setAppValue(ConfigService::TEST_NC_BASE, ''); |
169
|
|
|
$test = $input->getOption('test'); |
170
|
|
|
$type = $input->getOption('type'); |
171
|
|
|
if ($test !== '' && $type === '') { |
172
|
|
|
throw new Exception('Please specify a --type for the test'); |
173
|
|
|
} |
174
|
|
|
if ($test !== '' && !in_array($type, self::$checks)) { |
175
|
|
|
throw new Exception('Unknown type: ' . implode(', ', self::$checks)); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
// $this->configService->setAppValue(ConfigService::TEST_NC_BASE, $test); |
179
|
|
|
|
180
|
|
|
if ($type === '' || $type === 'loopback') { |
181
|
|
|
$output->writeln('### Checking <info>loopback</info> address.'); |
182
|
|
|
$this->checkLoopback($input, $output, $test); |
183
|
|
|
$output->writeln(''); |
184
|
|
|
$output->writeln(''); |
185
|
|
|
} |
186
|
|
|
if ($type === '' || $type === 'internal') { |
187
|
|
|
$output->writeln('### Testing <info>internal</info> address.'); |
188
|
|
|
$this->checkInternal($input, $output, $test); |
189
|
|
|
$output->writeln(''); |
190
|
|
|
$output->writeln(''); |
191
|
|
|
} |
192
|
|
|
if ($type === '' || $type === 'frontal') { |
193
|
|
|
$output->writeln('### Testing <info>frontal</info> address.'); |
194
|
|
|
$this->checkFrontal($input, $output, $test); |
195
|
|
|
$output->writeln(''); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
|
199
|
|
|
// |
200
|
|
|
// if (!$this->testRequest($output, 'GET', 'core.CSRFToken.index')) { |
201
|
|
|
// $this->configService->setAppValue(ConfigService::TEST_NC_BASE, ''); |
202
|
|
|
// |
203
|
|
|
// return 0; |
204
|
|
|
// } |
205
|
|
|
// |
206
|
|
|
// if (!$this->testRequest( |
207
|
|
|
// $output, 'POST', 'circles.EventWrapper.asyncBroadcast', |
208
|
|
|
// ['token' => 'test-dummy-token'] |
209
|
|
|
// )) { |
210
|
|
|
// $this->configService->setAppValue(ConfigService::TEST_NC_BASE, ''); |
211
|
|
|
// |
212
|
|
|
// return 0; |
213
|
|
|
// } |
214
|
|
|
// |
215
|
|
|
// $test = new GSEvent(GSEvent::TEST, true, true); |
216
|
|
|
// $test->setAsync(true); |
217
|
|
|
// $token = $this->gsUpstreamService->newEvent($test); |
218
|
|
|
// |
219
|
|
|
// $output->writeln('- Async request is sent, now waiting ' . $this->delay . ' seconds'); |
220
|
|
|
// sleep($this->delay); |
221
|
|
|
// $output->writeln('- Pause is over, checking results for ' . $token); |
222
|
|
|
// |
223
|
|
|
// $wrappers = $this->gsUpstreamService->getEventsByToken($token); |
224
|
|
|
// |
225
|
|
|
// $result = []; |
226
|
|
|
// $instances = array_merge($this->globalScaleService->getInstances(true)); |
227
|
|
|
// foreach ($wrappers as $wrapper) { |
228
|
|
|
// $result[$wrapper->getInstance()] = $wrapper->getEvent(); |
229
|
|
|
// } |
230
|
|
|
// |
231
|
|
|
// $localLooksGood = false; |
232
|
|
|
// foreach ($instances as $instance) { |
233
|
|
|
// $output->write($instance . ' '); |
234
|
|
|
// if (array_key_exists($instance, $result) |
235
|
|
|
// && $result[$instance]->getResult() |
236
|
|
|
// ->gInt('status') === 1) { |
237
|
|
|
// $output->writeln('<info>ok</info>'); |
238
|
|
|
// if ($this->configService->isLocalInstance($instance)) { |
239
|
|
|
// $localLooksGood = true; |
240
|
|
|
// } |
241
|
|
|
// } else { |
242
|
|
|
// $output->writeln('<error>fail</error>'); |
243
|
|
|
// } |
244
|
|
|
// } |
245
|
|
|
// |
246
|
|
|
// $this->configService->setAppValue(ConfigService::TEST_NC_BASE, ''); |
247
|
|
|
// |
248
|
|
|
// if ($localLooksGood) { |
249
|
|
|
// $this->saveUrl($input, $output, $input->getOption('url')); |
250
|
|
|
// } |
251
|
|
|
|
252
|
|
|
return 0; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @param InputInterface $input |
258
|
|
|
* @param OutputInterface $output |
259
|
|
|
* @param string $test |
260
|
|
|
* |
261
|
|
|
* @throws Exception |
262
|
|
|
*/ |
263
|
|
|
private function checkLoopback(InputInterface $input, OutputInterface $output, string $test = ''): void { |
264
|
|
|
$output->writeln('. The <info>loopback</info> setting is mandatory and can be checked locally.'); |
265
|
|
|
$output->writeln( |
266
|
|
|
'. The address you need to define here must be a reachable url of your Nextcloud from the hosting server itself.' |
267
|
|
|
); |
268
|
|
|
$output->writeln( |
269
|
|
|
'. By default, the App will use the entry \'overwrite.cli.url\' from \'config/config.php\'.' |
270
|
|
|
); |
271
|
|
|
|
272
|
|
|
if ($test === '') { |
273
|
|
|
$test = $this->configService->getLoopbackPath(); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
$output->writeln(''); |
277
|
|
|
$output->writeln('* testing current address: ' . $test); |
278
|
|
|
|
279
|
|
|
try { |
280
|
|
|
$this->setupLoopback($input, $output, $test); |
281
|
|
|
$output->writeln('* <info>Loopback</info> address looks good'); |
282
|
|
|
$output->writeln('saving'); |
283
|
|
|
} catch (Exception $e) { |
284
|
|
|
$output->writeln('<error>' . $e->getMessage() . '</error>'); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
$output->writeln('- You do not have a valid <info>loopback</info> address setup right now.'); |
288
|
|
|
|
289
|
|
|
$helper = $this->getHelper('question'); |
290
|
|
|
while (true) { |
291
|
|
|
$question = new Question( |
292
|
|
|
'<info>Please write down a new loopback address to test</info>: ', '' |
293
|
|
|
); |
294
|
|
|
|
295
|
|
|
$loopback = $helper->ask($input, $output, $question); |
296
|
|
|
if (is_null($loopback) || $loopback === '') { |
297
|
|
|
$output->writeln('exiting.'); |
298
|
|
|
throw new Exception('Your Circles App is not fully configured.'); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
try { |
302
|
|
|
[$scheme, $cloudId] = $this->parseAddress($loopback); |
|
|
|
|
303
|
|
|
} catch (Exception $e) { |
304
|
|
|
$output->writeln('<error>format must be http[s]://domain.name[:post]</error>'); |
305
|
|
|
continue; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
$loopback = $scheme . '://' . $cloudId; |
309
|
|
|
$output->write('* testing address: ' . $loopback . ' '); |
310
|
|
|
|
311
|
|
|
if ($this->testLoopback($input, $output, $loopback)) { |
312
|
|
|
$output->writeln('<info>ok</info>'); |
313
|
|
|
$output->writeln('saving.'); |
314
|
|
|
$this->configService->setAppValue(ConfigService::LOOPBACK_CLOUD_SCHEME, $scheme); |
315
|
|
|
$this->configService->setAppValue(ConfigService::LOOPBACK_CLOUD_ID, $cloudId); |
316
|
|
|
$output->writeln( |
317
|
|
|
'- Address <info>' . $loopback . '</info> is now used as <info>loopback</info>' |
318
|
|
|
); |
319
|
|
|
|
320
|
|
|
return; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
$output->writeln('<error>fail</error>'); |
324
|
|
|
} |
325
|
|
|
// while(true) { |
326
|
|
|
|
327
|
|
|
// } |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* @throws Exception |
333
|
|
|
*/ |
334
|
|
|
private function setupLoopback(InputInterface $input, OutputInterface $output, string $address): void { |
335
|
|
|
$e = null; |
336
|
|
|
try { |
337
|
|
|
[$scheme, $cloudId] = $this->parseAddress($address); |
|
|
|
|
338
|
|
|
|
339
|
|
|
$this->configService->setAppValue(ConfigService::LOOPBACK_TMP_SCHEME, $scheme); |
340
|
|
|
$this->configService->setAppValue(ConfigService::LOOPBACK_TMP_ID, $cloudId); |
341
|
|
|
if (!$this->testLoopback($input, $output, $address)) { |
342
|
|
|
throw new Exception('fail'); |
343
|
|
|
} |
344
|
|
|
} catch (Exception $e) { |
|
|
|
|
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
$this->configService->setAppValue(ConfigService::LOOPBACK_TMP_SCHEME, ''); |
348
|
|
|
$this->configService->setAppValue(ConfigService::LOOPBACK_TMP_ID, ''); |
349
|
|
|
|
350
|
|
|
if (!is_null($e)) { |
351
|
|
|
throw $e; |
352
|
|
|
} |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* @param InputInterface $input |
358
|
|
|
* @param OutputInterface $output |
359
|
|
|
* @param string $address |
360
|
|
|
* |
361
|
|
|
* @return bool |
362
|
|
|
* @throws RequestNetworkException |
363
|
|
|
* @throws FederatedEventException |
364
|
|
|
* @throws FederatedItemException |
365
|
|
|
* @throws InitiatorNotConfirmedException |
366
|
|
|
* @throws OwnerNotFoundException |
367
|
|
|
* @throws RemoteInstanceException |
368
|
|
|
* @throws RemoteNotFoundException |
369
|
|
|
* @throws RemoteResourceNotFoundException |
370
|
|
|
* @throws UnknownRemoteException |
371
|
|
|
*/ |
372
|
|
|
private function testLoopback(InputInterface $input, OutputInterface $output, string $address): bool { |
373
|
|
|
if (!$this->testRequest($output, 'GET', 'core.CSRFToken.index')) { |
374
|
|
|
return false; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
if (!$this->testRequest( |
378
|
|
|
$output, 'POST', 'circles.EventWrapper.asyncBroadcast', |
379
|
|
|
['token' => 'test-dummy-token'] |
380
|
|
|
)) { |
381
|
|
|
return false; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
$test = new FederatedEvent(LoopbackTest::class); |
385
|
|
|
$test->setAsync(true); |
386
|
|
|
$this->federatedEventService->newEvent($test); |
387
|
|
|
|
388
|
|
|
// |
389
|
|
|
// $output->writeln('- Async request is sent, now waiting ' . $this->delay . ' seconds'); |
390
|
|
|
// sleep($this->delay); |
391
|
|
|
// $output->writeln('- Pause is over, checking results for ' . $token); |
392
|
|
|
// |
393
|
|
|
// $wrappers = $this->gsUpstreamService->getEventsByToken($token); |
394
|
|
|
// |
395
|
|
|
// $result = []; |
396
|
|
|
// $instances = array_merge($this->globalScaleService->getInstances(true)); |
397
|
|
|
// foreach ($wrappers as $wrapper) { |
398
|
|
|
// $result[$wrapper->getInstance()] = $wrapper->getEvent(); |
399
|
|
|
// } |
400
|
|
|
// |
401
|
|
|
// $localLooksGood = false; |
402
|
|
|
// foreach ($instances as $instance) { |
403
|
|
|
// $output->write($instance . ' '); |
404
|
|
|
// if (array_key_exists($instance, $result) |
405
|
|
|
// && $result[$instance]->getResult() |
406
|
|
|
// ->gInt('status') === 1) { |
407
|
|
|
// $output->writeln('<info>ok</info>'); |
408
|
|
|
// if ($this->configService->isLocalInstance($instance)) { |
409
|
|
|
// $localLooksGood = true; |
410
|
|
|
// } |
411
|
|
|
// } else { |
412
|
|
|
// $output->writeln('<error>fail</error>'); |
413
|
|
|
// } |
414
|
|
|
// } |
415
|
|
|
// |
416
|
|
|
// $this->configService->setAppValue(ConfigService::TEST_NC_BASE, ''); |
417
|
|
|
// |
418
|
|
|
// if ($localLooksGood) { |
419
|
|
|
// $this->saveUrl($input, $output, $input->getOption('url')); |
420
|
|
|
// } |
421
|
|
|
|
422
|
|
|
if ($address === 'http://orange.local') { |
423
|
|
|
return true; |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
return false; |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* @param InputInterface $input |
432
|
|
|
* @param OutputInterface $output |
433
|
|
|
* @param string $test |
434
|
|
|
*/ |
435
|
|
|
private function checkInternal(InputInterface $input, OutputInterface $output, string $test): void { |
436
|
|
|
$output->writeln( |
437
|
|
|
'. The <info>internal</info> setting is mandatory only if you are willing to use Circles in a GlobalScale setup on a local network.' |
438
|
|
|
); |
439
|
|
|
$output->writeln( |
440
|
|
|
'. The address you need to define here is the local address of your Nextcloud, reachable by all other instances of our GlobalScale.' |
441
|
|
|
); |
442
|
|
|
|
443
|
|
|
|
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
|
447
|
|
|
/** |
448
|
|
|
* @param InputInterface $input |
449
|
|
|
* @param OutputInterface $output |
450
|
|
|
* @param string $test |
451
|
|
|
*/ |
452
|
|
|
private function checkFrontal(InputInterface $input, OutputInterface $output, string $test): void { |
453
|
|
|
$output->writeln('. The <info>frontal</info> setting is optional.'); |
454
|
|
|
$output->writeln( |
455
|
|
|
'. The purpose of this address is for your Federated Circle to reach other instances of Nextcloud over the Internet.' |
456
|
|
|
); |
457
|
|
|
$output->writeln( |
458
|
|
|
'. The address you need to define here must be reachable from the Internet.' |
459
|
|
|
); |
460
|
|
|
$output->writeln( |
461
|
|
|
'. By default, this feature is disabled (and is considered ALPHA in Nextcloud 22).' |
462
|
|
|
); |
463
|
|
|
|
464
|
|
|
$question = new ConfirmationQuestion( |
465
|
|
|
'- <comment>Do you want to enable this feature ?</comment> (y/N) ', false, '/^(y|Y)/i' |
466
|
|
|
); |
467
|
|
|
|
468
|
|
|
$helper = $this->getHelper('question'); |
469
|
|
|
if (!$helper->ask($input, $output, $question)) { |
470
|
|
|
$output->writeln('skipping.'); |
471
|
|
|
|
472
|
|
|
return; |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
|
476
|
|
|
while (true) { |
477
|
|
|
|
478
|
|
|
$question = new Question( |
479
|
|
|
'<info>Please write down a new frontal address to test</info>: ', '' |
480
|
|
|
); |
481
|
|
|
|
482
|
|
|
$frontal = $helper->ask($input, $output, $question); |
483
|
|
|
if (is_null($frontal) || $frontal === '') { |
484
|
|
|
$output->writeln('skipping.'); |
485
|
|
|
|
486
|
|
|
return; |
487
|
|
|
} |
488
|
|
|
|
489
|
|
|
try { |
490
|
|
|
[$scheme, $cloudId] = $this->parseAddress($frontal); |
|
|
|
|
491
|
|
|
} catch (Exception $e) { |
492
|
|
|
$output->writeln('<error>format must be http[s]://domain.name[:post]</error>'); |
493
|
|
|
continue; |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
$frontal = $scheme . '://' . $cloudId; |
497
|
|
|
break; |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
$question = new ConfirmationQuestion( |
501
|
|
|
'<comment>Do you want to check the validity of this frontal address?</comment> (y/N) ', false, |
502
|
|
|
'/^(y|Y)/i' |
503
|
|
|
); |
504
|
|
|
|
505
|
|
|
if ($helper->ask($input, $output, $question)) { |
506
|
|
|
$output->writeln( |
507
|
|
|
'You will need to run this <info>curl</info> command from a remote terminal and paste its result: ' |
508
|
|
|
); |
509
|
|
|
$output->writeln( |
510
|
|
|
' curl ' . $frontal . '/.well-known/webfinger?resource=http://nextcloud.com/' |
|
|
|
|
511
|
|
|
); |
512
|
|
|
|
513
|
|
|
$question = new Question('result: ', ''); |
514
|
|
|
$pasteWebfinger = $helper->ask($input, $output, $question); |
515
|
|
|
|
516
|
|
|
echo '__ ' . $pasteWebfinger; |
517
|
|
|
|
518
|
|
|
$output->writeln('TESTING !!'); |
519
|
|
|
$output->writeln('TESTING !!'); |
520
|
|
|
$output->writeln('TESTING !!'); |
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
$output->writeln('saved'); |
524
|
|
|
|
525
|
|
|
|
526
|
|
|
// $output->writeln('. 1) The automatic way, requiring a valid remote instance of Nextcloud.'); |
527
|
|
|
// $output->writeln( |
528
|
|
|
// '. 2) The manual way, using the <comment>curl</comment> command from a remote terminal.' |
529
|
|
|
// ); |
530
|
|
|
// $output->writeln( |
531
|
|
|
// '. If you prefer the automatic way, you will need to enter the valid remote instance of Nextcloud you want to use.' |
532
|
|
|
// ); |
533
|
|
|
// $output->writeln('. If you want the manual way, just enter an empty field.'); |
534
|
|
|
// $output->writeln(''); |
535
|
|
|
// $output->writeln( |
536
|
|
|
// '. If you do not known a valid remote instance of Nextcloud, you can use <comment>\'https://circles.artificial-owl.com/\'</comment>' |
537
|
|
|
// ); |
538
|
|
|
// $output->writeln( |
539
|
|
|
// '. Please note that no critical information will be shared during the process, and any data (ie. public key and address)' |
540
|
|
|
// ); |
541
|
|
|
// $output->writeln( |
542
|
|
|
// ' generated during the process will be wiped of the remote instance after few minutes.' |
543
|
|
|
// ); |
544
|
|
|
// $output->writeln(''); |
545
|
|
|
|
546
|
|
|
// $question = new Question( |
547
|
|
|
// '- <comment>Which remote instance of Nextcloud do you want to use in order to test your setup:</comment> (empty to bypass this step): ' |
548
|
|
|
// ); |
549
|
|
|
// $helper = $this->getHelper('question'); |
550
|
|
|
// $remote = $helper->ask($input, $output, $question); |
551
|
|
|
// if (is_null($frontal) || $frontal === '') { |
552
|
|
|
// $output->writeln('skipping.'); |
553
|
|
|
// |
554
|
|
|
// return; |
555
|
|
|
// } |
556
|
|
|
// |
557
|
|
|
// $output->writeln('. The confirmation step is optional and can be done in 2 different ways:'); |
558
|
|
|
// |
559
|
|
|
// |
560
|
|
|
// $output->writeln(''); |
561
|
|
|
// $question = new Question( |
562
|
|
|
// '- <comment>Enter the <info>frontal</info> address you want to be used to identify your instance of Nextcloud over the Internet</comment>: ' |
563
|
|
|
// ); |
564
|
|
|
// $helper = $this->getHelper('question'); |
565
|
|
|
// $frontal = $helper->ask($input, $output, $question); |
566
|
|
|
|
567
|
|
|
// while (true) { |
568
|
|
|
// $question = new Question( |
569
|
|
|
// '<info>Please write down a new frontal address to test</info>: ', '' |
570
|
|
|
// ); |
571
|
|
|
// |
572
|
|
|
// $frontal = $helper->ask($input, $output, $question); |
573
|
|
|
// if (is_null($frontal) || $frontal === '') { |
574
|
|
|
// $output->writeln('skipping.'); |
575
|
|
|
// |
576
|
|
|
// return; |
577
|
|
|
// } |
578
|
|
|
// |
579
|
|
|
// try { |
580
|
|
|
// [$scheme, $cloudId] = $this->parseAddress($test); |
581
|
|
|
// } catch (Exception $e) { |
582
|
|
|
// $output->writeln('<error>format must be http[s]://domain.name[:post]</error>'); |
583
|
|
|
// continue; |
584
|
|
|
// } |
585
|
|
|
// |
586
|
|
|
// $frontal = $scheme . '://' . $cloudId; |
587
|
|
|
// |
588
|
|
|
// $output->write('* testing address: ' . $frontal . ' '); |
589
|
|
|
// |
590
|
|
|
// if ($remote === '') { |
591
|
|
|
// $output->writeln('remote empty, please run this curl request and paste the result in here'); |
592
|
|
|
// $output->writeln( |
593
|
|
|
// ' curl ' . $frontal . '/.well-known/webfinger?resource=http://nextcloud.com/' |
594
|
|
|
// ); |
595
|
|
|
// $question = new Question('result: ', ''); |
596
|
|
|
// |
597
|
|
|
// $resultWebfinger = $helper->ask($input, $output, $question); |
598
|
|
|
// |
599
|
|
|
// } else { |
600
|
|
|
// } |
601
|
|
|
// } |
602
|
|
|
|
603
|
|
|
|
604
|
|
|
// if ($remote === '') { |
605
|
|
|
// $output->writeln(''); |
606
|
|
|
// } |
607
|
|
|
|
608
|
|
|
// $output->writeln(''); |
609
|
|
|
// $output->writeln( |
610
|
|
|
// '. By default, this feature is disabled. You will need to setup a valid entry to enabled it.' |
611
|
|
|
// ); |
612
|
|
|
|
613
|
|
|
// $output->writeln(''); |
614
|
|
|
// $output->write('* testing current address: ' . $this->configService->getLoopbackPath() . ' '); |
615
|
|
|
|
616
|
|
|
|
617
|
|
|
// $this->configService->getAppValue(ConfigService::CHECK_FRONTAL_USING); |
618
|
|
|
} |
619
|
|
|
|
620
|
|
|
|
621
|
|
|
/** |
622
|
|
|
* @param OutputInterface $o |
|
|
|
|
623
|
|
|
* @param string $type |
624
|
|
|
* @param string $route |
625
|
|
|
* @param array $args |
626
|
|
|
* |
627
|
|
|
* @return bool |
628
|
|
|
* @throws RequestNetworkException |
629
|
|
|
*/ |
630
|
|
|
private function testRequest( |
631
|
|
|
OutputInterface $output, |
632
|
|
|
string $type, |
633
|
|
|
string $route, |
634
|
|
|
array $args = [] |
635
|
|
|
): bool { |
636
|
|
|
$request = new NC22Request('', Request::type($type)); |
637
|
|
|
$this->configService->configureLoopbackRequest($request, $route, $args); |
638
|
|
|
$request->setFollowLocation(false); |
639
|
|
|
|
640
|
|
|
$output->write('- ' . $type . ' request on ' . $request->getCompleteUrl() . ': '); |
641
|
|
|
$this->doRequest($request); |
642
|
|
|
|
643
|
|
|
$color = 'error'; |
644
|
|
|
$result = $request->getResult(); |
645
|
|
|
if ($result->getStatusCode() === 200) { |
646
|
|
|
$color = 'info'; |
647
|
|
|
} |
648
|
|
|
|
649
|
|
|
$output->writeln('<' . $color . '>' . $result->getStatusCode() . '</' . $color . '>'); |
650
|
|
|
|
651
|
|
|
if ($result->getStatusCode() === 200) { |
652
|
|
|
return true; |
653
|
|
|
} |
654
|
|
|
|
655
|
|
|
return false; |
656
|
|
|
} |
657
|
|
|
|
658
|
|
|
|
659
|
|
|
/** |
660
|
|
|
* @param InputInterface $input |
661
|
|
|
* @param OutputInterface $output |
662
|
|
|
* @param string $address |
663
|
|
|
*/ |
664
|
|
|
private function saveUrl(InputInterface $input, OutputInterface $output, string $address): void { |
665
|
|
|
if ($address === '') { |
666
|
|
|
return; |
667
|
|
|
} |
668
|
|
|
|
669
|
|
|
$output->writeln(''); |
670
|
|
|
$output->writeln( |
671
|
|
|
'The address <info>' . $address . '</info> seems to reach your local Nextcloud.' |
672
|
|
|
); |
673
|
|
|
|
674
|
|
|
$helper = $this->getHelper('question'); |
675
|
|
|
$output->writeln(''); |
676
|
|
|
$question = new ConfirmationQuestion( |
677
|
|
|
'<info>Do you want to store this address in database ?</info> (y/N) ', false, '/^(y|Y)/i' |
678
|
|
|
); |
679
|
|
|
|
680
|
|
|
if (!$helper->ask($input, $output, $question)) { |
681
|
|
|
$output->writeln('Configuration NOT saved'); |
682
|
|
|
|
683
|
|
|
return; |
684
|
|
|
} |
685
|
|
|
|
686
|
|
|
$this->configService->setAppValue(ConfigService::FORCE_NC_BASE, $address); |
687
|
|
|
$output->writeln( |
688
|
|
|
'New configuration <info>' . Application::APP_ID . '.' . ConfigService::FORCE_NC_BASE . '=\'' |
689
|
|
|
. $address . '\'</info> stored in database' |
690
|
|
|
); |
691
|
|
|
} |
692
|
|
|
|
693
|
|
|
|
694
|
|
|
/** |
695
|
|
|
* @param string $test |
696
|
|
|
* |
697
|
|
|
* @return array |
698
|
|
|
* @throws Exception |
699
|
|
|
*/ |
700
|
|
|
private function parseAddress(string $test): array { |
701
|
|
|
$scheme = parse_url($test, PHP_URL_SCHEME); |
702
|
|
|
$cloudId = parse_url($test, PHP_URL_HOST); |
703
|
|
|
$cloudIdPort = parse_url($test, PHP_URL_PORT); |
704
|
|
|
|
705
|
|
|
if (is_null($scheme) || is_null($cloudId)) { |
706
|
|
|
throw new Exception(); |
707
|
|
|
} |
708
|
|
|
|
709
|
|
|
if (!is_null($cloudIdPort)) { |
710
|
|
|
$cloudId = $cloudId . ':' . $cloudIdPort; |
711
|
|
|
} |
712
|
|
|
|
713
|
|
|
return [$scheme, $cloudId]; |
714
|
|
|
} |
715
|
|
|
|
716
|
|
|
} |
717
|
|
|
|
718
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.