-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathCirclesCheck.php
More file actions
888 lines (734 loc) · 26.1 KB
/
CirclesCheck.php
File metadata and controls
888 lines (734 loc) · 26.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
<?php
declare(strict_types=1);
/**
* Circles - Bring cloud-users closer together.
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Maxence Lange <maxence@artificial-owl.com>
* @copyright 2017
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Circles\Command;
use ArtificialOwl\MySmallPhpTools\Exceptions\RequestNetworkException;
use ArtificialOwl\MySmallPhpTools\Exceptions\SignatoryException;
use ArtificialOwl\MySmallPhpTools\Model\Nextcloud\nc22\NC22Request;
use ArtificialOwl\MySmallPhpTools\Model\Request;
use ArtificialOwl\MySmallPhpTools\Model\SimpleDataStore;
use ArtificialOwl\MySmallPhpTools\Traits\Nextcloud\nc22\TNC22Request;
use ArtificialOwl\MySmallPhpTools\Traits\TArrayTools;
use ArtificialOwl\MySmallPhpTools\Traits\TStringTools;
use Exception;
use OC;
use OC\AppConfig;
use OC\Core\Command\Base;
use OCA\Circles\AppInfo\Application;
use OCA\Circles\AppInfo\Capabilities;
use OCA\Circles\Exceptions\FederatedEventException;
use OCA\Circles\Exceptions\FederatedItemException;
use OCA\Circles\Exceptions\InitiatorNotConfirmedException;
use OCA\Circles\Exceptions\OwnerNotFoundException;
use OCA\Circles\Exceptions\RemoteInstanceException;
use OCA\Circles\Exceptions\RemoteNotFoundException;
use OCA\Circles\Exceptions\RemoteResourceNotFoundException;
use OCA\Circles\Exceptions\RequestBuilderException;
use OCA\Circles\Exceptions\UnknownInterfaceException;
use OCA\Circles\Exceptions\UnknownRemoteException;
use OCA\Circles\FederatedItems\LoopbackTest;
use OCA\Circles\Model\Federated\FederatedEvent;
use OCA\Circles\Service\ConfigService;
use OCA\Circles\Service\FederatedEventService;
use OCA\Circles\Service\InterfaceService;
use OCA\Circles\Service\RemoteService;
use OCA\Circles\Service\RemoteStreamService;
use OCA\Circles\Service\RemoteUpstreamService;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;
/**
* Class CirclesCheck
*
* @package OCA\Circles\Command
*/
class CirclesCheck extends Base {
use TStringTools;
use TArrayTools;
use TNC22Request;
public static $checks = [
'internal',
'frontal',
'loopback'
];
/** @var Capabilities */
private $capabilities;
/** @var InterfaceService */
private $interfaceService;
/** @var FederatedEventService */
private $federatedEventService;
/** @var RemoteService */
private $remoteService;
/** @var RemoteStreamService */
private $remoteStreamService;
/** @var RemoteUpstreamService */
private $remoteUpstreamService;
/** @var ConfigService */
private $configService;
/** @var array */
private $sessions = [];
/**
* CirclesCheck constructor.
*
* @param Capabilities $capabilities
* @param InterfaceService $interfaceService
* @param FederatedEventService $federatedEventService
* @param RemoteService $remoteService
* @param RemoteStreamService $remoteStreamService
* @param RemoteUpstreamService $remoteUpstreamService
* @param ConfigService $configService
*/
public function __construct(
Capabilities $capabilities,
InterfaceService $interfaceService,
FederatedEventService $federatedEventService,
RemoteService $remoteService,
RemoteStreamService $remoteStreamService,
RemoteUpstreamService $remoteUpstreamService,
ConfigService $configService
) {
parent::__construct();
$this->capabilities = $capabilities;
$this->interfaceService = $interfaceService;
$this->federatedEventService = $federatedEventService;
$this->remoteService = $remoteService;
$this->remoteStreamService = $remoteStreamService;
$this->remoteUpstreamService = $remoteUpstreamService;
$this->configService = $configService;
}
protected function configure() {
parent::configure();
$this->setName('circles:check')
->setDescription('Checking your configuration')
->addOption('capabilities', '', InputOption::VALUE_NONE, 'listing app\'s capabilities')
->addOption('type', '', InputOption::VALUE_REQUIRED, 'configuration to check', '')
->addOption('alpha', '', InputOption::VALUE_NONE, 'allow ALPHA features')
->addOption('test', '', InputOption::VALUE_REQUIRED, 'specify an url to test', '');
}
/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int
* @throws Exception
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
if ($input->getOption('capabilities')) {
$capabilities = $this->getArray('circles', $this->capabilities->getCapabilities(true));
$output->writeln(json_encode($capabilities, JSON_PRETTY_PRINT));
return 0;
}
$this->configService->setAppValue(ConfigService::TEST_NC_BASE, '');
$test = $input->getOption('test');
$type = $input->getOption('type');
if ($test !== '' && $type === '') {
throw new Exception('Please specify a --type for the test');
}
if ($test !== '' && !in_array($type, self::$checks)) {
throw new Exception('Unknown type: ' . implode(', ', self::$checks));
}
// $this->configService->setAppValue(ConfigService::TEST_NC_BASE, $test);
if ($type === '' || $type === 'loopback') {
$output->writeln('### Checking <info>loopback</info> address.');
$this->checkLoopback($input, $output, $test);
$output->writeln('');
$output->writeln('');
}
if ($type === '' || $type === 'internal') {
$output->writeln('### Testing <info>internal</info> address.');
$this->checkInternal($input, $output, $test);
$output->writeln('');
$output->writeln('');
}
if (!$input->getOption('alpha')) {
return 0;
}
if ($type === '' || $type === 'frontal') {
$output->writeln('### Testing <info>frontal</info> address.');
$this->checkFrontal($input, $output, $test);
$output->writeln('');
}
return 0;
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @param string $test
*
* @throws Exception
*/
private function checkLoopback(InputInterface $input, OutputInterface $output, string $test = ''): void {
$output->writeln('. The <info>loopback</info> setting is mandatory and can be checked locally.');
$output->writeln(
'. The address you need to define here must be a reachable url of your Nextcloud from the hosting server itself.'
);
$output->writeln(
'. By default, the App will use the entry \'overwrite.cli.url\' from \'config/config.php\'.'
);
$notDefault = false;
if ($test === '') {
$test = $this->configService->getLoopbackPath();
} else {
$notDefault = true;
}
$output->writeln('');
$output->writeln('* testing current address: ' . $test);
try {
$this->setupLoopback($input, $output, $test);
$output->writeln('* <info>Loopback</info> address looks good');
if ($notDefault) {
$this->saveLoopback($input, $output, $test);
}
return;
} catch (Exception $e) {
}
$output->writeln('');
$output->writeln('- <comment>You do not have a valid loopback address setup right now.</comment>');
$output->writeln('');
$helper = $this->getHelper('question');
while (true) {
$question = new Question(
'<info>Please write down a new loopback address to test</info>: ', ''
);
$loopback = $helper->ask($input, $output, $question);
if (is_null($loopback) || $loopback === '') {
$output->writeln('exiting.');
throw new Exception('Your Circles App is not fully configured.');
}
try {
[$scheme, $cloudId, $path] = $this->parseAddress($loopback);
} catch (Exception $e) {
$output->writeln('<error>format must be http[s]://domain.name[:post][/path]</error>');
continue;
}
$loopback = rtrim($scheme . '://' . $cloudId . $path, '/');
$output->writeln('* testing address: ' . $loopback . ' ');
try {
$this->setupLoopback($input, $output, $loopback);
$this->saveLoopback($input, $output, $loopback);
return;
} catch (Exception $e) {
$output->writeln('');
}
}
}
/**
* @throws Exception
*/
private function setupLoopback(InputInterface $input, OutputInterface $output, string $address): void {
$e = null;
try {
[$scheme, $cloudId, $path] = $this->parseAddress($address);
$this->configService->setAppValue(ConfigService::LOOPBACK_TMP_SCHEME, $scheme);
$this->configService->setAppValue(ConfigService::LOOPBACK_TMP_ID, $cloudId);
$this->configService->setAppValue(ConfigService::LOOPBACK_TMP_PATH, $path);
if (!$this->testLoopback($input, $output)) {
throw new Exception();
}
} catch (Exception $e) {
}
$this->configService->setAppValue(ConfigService::LOOPBACK_TMP_SCHEME, '');
$this->configService->setAppValue(ConfigService::LOOPBACK_TMP_ID, '');
$this->configService->setAppValue(ConfigService::LOOPBACK_TMP_PATH, '');
if (!is_null($e)) {
throw $e;
}
}
/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @return bool
* @throws FederatedEventException
* @throws FederatedItemException
* @throws InitiatorNotConfirmedException
* @throws OwnerNotFoundException
* @throws RemoteInstanceException
* @throws RemoteNotFoundException
* @throws RemoteResourceNotFoundException
* @throws RequestBuilderException
* @throws UnknownRemoteException
*/
private function testLoopback(InputInterface $input, OutputInterface $output): bool {
if (!$this->testRequest($output, 'GET', 'core.CSRFToken.index')) {
return false;
}
if (!$this->testRequest(
$output, 'POST', 'circles.EventWrapper.asyncBroadcast',
['token' => 'test-dummy-token']
)) {
return false;
}
$timer = round(microtime(true) * 1000);
$output->write('- Creating async FederatedEvent ');
$test = new FederatedEvent(LoopbackTest::class);
$this->federatedEventService->newEvent($test);
$output->writeln(
'<info>' . $test->getWrapperToken() . '</info> ' .
'(took ' . (round(microtime(true) * 1000) - $timer) . 'ms)'
);
$output->writeln('- Waiting for async process to finish (5s)');
sleep(5);
$output->write('- Checking status on FederatedEvent ');
$wrappers = $this->remoteUpstreamService->getEventsByToken($test->getWrapperToken());
if (count($wrappers) !== 1) {
$output->writeln('<error>Event created too many Wrappers</error>');
return false;
}
$wrapper = array_shift($wrappers);
$checkVerify = $wrapper->getEvent()->getData()->gInt('verify');
if ($checkVerify === LoopbackTest::VERIFY) {
$output->write('<info>verify=' . $checkVerify . '</info> ');
} else {
$output->writeln('<error>verify=' . $checkVerify . '</error>');
return false;
}
$checkManage = $wrapper->getResult()->gInt('manage');
if ($checkManage === LoopbackTest::MANAGE) {
$output->write('<info>manage=' . $checkManage . '</info> ');
} else {
$output->writeln('<error>manage=' . $checkManage . '</error>');
return false;
}
$output->writeln('');
return true;
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @param string $loopback
*
* @throws Exception
*/
private function saveLoopback(InputInterface $input, OutputInterface $output, string $loopback): void {
[$scheme, $cloudId, $path] = $this->parseAddress($loopback);
$question = new ConfirmationQuestion(
'- Do you want to save <info>'
. $loopback
. '</info> as your <info>loopback</info> address ? (y/N) ',
false, '/^(y|Y)/i'
);
$helper = $this->getHelper('question');
if (!$helper->ask($input, $output, $question)) {
$output->writeln('skipping.');
return;
}
$this->configService->setAppValue(ConfigService::LOOPBACK_CLOUD_SCHEME, $scheme);
$this->configService->setAppValue(ConfigService::LOOPBACK_CLOUD_ID, $cloudId);
$this->configService->setAppValue(ConfigService::LOOPBACK_CLOUD_PATH, $path);
$output->writeln(
'- Address <info>' . $loopback . '</info> is now used as <info>loopback</info>'
);
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @param string $test
*
* @throws SignatoryException
* @throws UnknownInterfaceException
* @throws Exception
*/
private function checkInternal(InputInterface $input, OutputInterface $output, string $test): void {
$output->writeln(
'. The <info>internal</info> setting is mandatory only if you are willing to use Circles in a GlobalScale setup on a local network.'
);
$output->writeln(
'. The address you need to define here is the local address of your Nextcloud, reachable by all other instances of our GlobalScale.'
);
$question = new ConfirmationQuestion(
'- <comment>Do you want to enable this feature ?</comment> (y/N) ', false, '/^(y|Y)/i'
);
$helper = $this->getHelper('question');
if (!$helper->ask($input, $output, $question)) {
$output->writeln('skipping.');
return;
}
while (true) {
$output->writeln('');
$question = new Question(
'<info>Please write down a new internal address to test</info>: ', ''
);
$internal = $helper->ask($input, $output, $question);
if (is_null($internal) || $internal === '') {
$output->writeln('skipping.');
return;
}
try {
[$scheme, $cloudId, $path] = $this->parseAddress($internal);
} catch (Exception $e) {
$output->writeln('<error>format must be http[s]://domain.name[:post][/path]</error>');
continue;
}
$internal = rtrim($scheme . '://' . $cloudId, '/');
$fullInternal = rtrim($scheme . '://' . $cloudId . $path, '/');
$question = new ConfirmationQuestion(
'<comment>Do you want to check the validity of this internal address?</comment> (Y/n) ', true,
'/^(y|Y)/i'
);
if ($helper->ask($input, $output, $question)) {
$testToken = $this->token();
$this->configService->setAppValue(ConfigService::IFACE_TEST_ID, $cloudId);
$this->configService->setAppValue(ConfigService::IFACE_TEST_SCHEME, $scheme);
$this->configService->setAppValue(ConfigService::IFACE_TEST_PATH, $path);
$this->configService->setAppValue(ConfigService::IFACE_TEST_TOKEN, $testToken);
$output->writeln('');
$output->writeln(
'You will need to run this <info>curl</info> command from a terminal on your local network and paste its result: '
);
$output->writeln(
' curl -L "' . $internal
. '/.well-known/webfinger?resource=http://nextcloud.com/&test='
. $testToken . '"'
);
$output->writeln('paste the result here: ');
$question = new Question('', '');
$pastedWebfinger = new SimpleDataStore();
$pastedWebfinger->json(trim($helper->ask($input, $output, $question)));
if ($pastedWebfinger->g('subject') !== Application::APP_SUBJECT) {
$output->writeln('<error>Cannot extract SUBJECT from the pasted data</error>');
continue;
}
$pastedHref = '';
foreach ($pastedWebfinger->gArray('links') as $link) {
$entry = new SimpleDataStore($link);
if ($entry->g('rel') === Application::APP_REL) {
$pastedHref = $entry->g('href');
}
}
if ($pastedHref === '') {
$output->writeln('<error>Cannot retrieve HREF from the pasted data</error>');
continue;
}
$href = $this->interfaceService->getCloudPath(
'circles.Remote.appService',
[],
InterfaceService::IFACE_TEST
);
if ($pastedHref !== $href) {
$output->writeln(
'<error>The returned data (' . $pastedHref . ') are not the one expected: </error>'
. $href
);
continue;
}
$output->writeln('');
$output->writeln('First step seems fine.');
$output->writeln(
'Now, please run this <info>curl</info> command from a terminal on your local network and paste its result: '
);
$output->writeln(
' curl -L "' . $pastedHref . '?test=' . $testToken . '" -H "Accept: application/json"'
);
$output->writeln('paste the result here: ');
$question = new Question('', '');
$pastedSignatory = new SimpleDataStore();
$pastedSignatory->json(trim($helper->ask($input, $output, $question)));
// small hack to refresh the cached config
OC::$server->get(AppConfig::class)->clearCachedConfig();
$this->interfaceService->setCurrentInterface(InterfaceService::IFACE_TEST);
$appSignatory = $this->remoteStreamService->getAppSignatory(false);
if ($appSignatory->getUid(true) !== $pastedSignatory->g('uid')
|| $appSignatory->getRoot() !== $pastedSignatory->g('root')) {
$output->writeln(
'<error>The returned data ('
. $pastedSignatory->g('uid') . '/' . $pastedSignatory->g('root')
. ') are not the one expected: </error>'
. $appSignatory->getUid(true) . '/' . $appSignatory->getRoot()
);
continue;
}
$output->writeln('* <info>Internal</info> address looks good');
}
$this->saveInternal($input, $output, $fullInternal);
return;
}
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @param string $internal
*
* @throws Exception
*/
private function saveInternal(InputInterface $input, OutputInterface $output, string $internal): void {
[$scheme, $cloudId, $path] = $this->parseAddress($internal);
$output->writeln('');
$question = new ConfirmationQuestion(
'- Do you want to save <info>'
. $internal
. '</info> as your <info>internal</info> address ? (y/N) ',
false, '/^(y|Y)/i'
);
$helper = $this->getHelper('question');
if (!$helper->ask($input, $output, $question)) {
$output->writeln('skipping.');
return;
}
$this->configService->setAppValue(ConfigService::INTERNAL_CLOUD_SCHEME, $scheme);
$this->configService->setAppValue(ConfigService::INTERNAL_CLOUD_ID, $cloudId);
$this->configService->setAppValue(ConfigService::INTERNAL_CLOUD_PATH, $path);
$output->writeln(
'- Address <info>' . $internal . '</info> is now used as <info>internal</info>'
);
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @param string $test
*/
private function checkFrontal(InputInterface $input, OutputInterface $output, string $test): void {
$output->writeln('. The <info>frontal</info> setting is optional.');
$output->writeln(
'. The purpose of this address is for your Federated Circle to reach other instances of Nextcloud over the Internet.'
);
$output->writeln(
'. The address you need to define here must be reachable from the Internet.'
);
$output->writeln(
'. By default, this feature is disabled (and is considered ALPHA in Nextcloud 22).'
);
$question = new ConfirmationQuestion(
'- <comment>Do you want to enable this feature ?</comment> (y/N) ', false, '/^(y|Y)/i'
);
$helper = $this->getHelper('question');
if (!$helper->ask($input, $output, $question)) {
$output->writeln('skipping.');
return;
}
while (true) {
$question = new Question(
'<info>Please write down a new frontal address to test</info>: ', ''
);
$frontal = $helper->ask($input, $output, $question);
if (is_null($frontal) || $frontal === '') {
$output->writeln('skipping.');
return;
}
try {
[$scheme, $cloudId, $path] = $this->parseAddress($frontal);
} catch (Exception $e) {
$output->writeln('<error>format must be http[s]://domain.name[:post][/path]</error>');
continue;
}
$frontal = rtrim($scheme . '://' . $cloudId . $path, '/');
break;
}
$question = new ConfirmationQuestion(
'<comment>Do you want to check the validity of this frontal address?</comment> (y/N) ', false,
'/^(y|Y)/i'
);
if ($helper->ask($input, $output, $question)) {
$output->writeln(
'You will need to run this <info>curl</info> command from a remote terminal and paste its result: '
);
$output->writeln(
' curl ' . $frontal . '/.well-known/webfinger?resource=http://nextcloud.com/'
);
$question = new Question('result: ', '');
$pasteWebfinger = $helper->ask($input, $output, $question);
echo '__ ' . $pasteWebfinger;
$output->writeln('TESTING !!');
$output->writeln('TESTING !!');
$output->writeln('TESTING !!');
}
$output->writeln('saved');
// $output->writeln('. 1) The automatic way, requiring a valid remote instance of Nextcloud.');
// $output->writeln(
// '. 2) The manual way, using the <comment>curl</comment> command from a remote terminal.'
// );
// $output->writeln(
// '. If you prefer the automatic way, you will need to enter the valid remote instance of Nextcloud you want to use.'
// );
// $output->writeln('. If you want the manual way, just enter an empty field.');
// $output->writeln('');
// $output->writeln(
// '. If you do not known a valid remote instance of Nextcloud, you can use <comment>\'https://circles.artificial-owl.com/\'</comment>'
// );
// $output->writeln(
// '. Please note that no critical information will be shared during the process, and any data (ie. public key and address)'
// );
// $output->writeln(
// ' generated during the process will be wiped of the remote instance after few minutes.'
// );
// $output->writeln('');
// $question = new Question(
// '- <comment>Which remote instance of Nextcloud do you want to use in order to test your setup:</comment> (empty to bypass this step): '
// );
// $helper = $this->getHelper('question');
// $remote = $helper->ask($input, $output, $question);
// if (is_null($frontal) || $frontal === '') {
// $output->writeln('skipping.');
//
// return;
// }
//
// $output->writeln('. The confirmation step is optional and can be done in 2 different ways:');
//
//
// $output->writeln('');
// $question = new Question(
// '- <comment>Enter the <info>frontal</info> address you want to be used to identify your instance of Nextcloud over the Internet</comment>: '
// );
// $helper = $this->getHelper('question');
// $frontal = $helper->ask($input, $output, $question);
// while (true) {
// $question = new Question(
// '<info>Please write down a new frontal address to test</info>: ', ''
// );
//
// $frontal = $helper->ask($input, $output, $question);
// if (is_null($frontal) || $frontal === '') {
// $output->writeln('skipping.');
//
// return;
// }
//
// try {
// [$scheme, $cloudId] = $this->parseAddress($test);
// } catch (Exception $e) {
// $output->writeln('<error>format must be http[s]://domain.name[:post]</error>');
// continue;
// }
//
// $frontal = $scheme . '://' . $cloudId;
//
// $output->write('* testing address: ' . $frontal . ' ');
//
// if ($remote === '') {
// $output->writeln('remote empty, please run this curl request and paste the result in here');
// $output->writeln(
// ' curl ' . $frontal . '/.well-known/webfinger?resource=http://nextcloud.com/'
// );
// $question = new Question('result: ', '');
//
// $resultWebfinger = $helper->ask($input, $output, $question);
//
// } else {
// }
// }
// if ($remote === '') {
// $output->writeln('');
// }
// $output->writeln('');
// $output->writeln(
// '. By default, this feature is disabled. You will need to setup a valid entry to enabled it.'
// );
// $output->writeln('');
// $output->write('* testing current address: ' . $this->configService->getLoopbackPath() . ' ');
// $this->configService->getAppValue(ConfigService::CHECK_FRONTAL_USING);
}
/**
* @param OutputInterface $o
* @param string $type
* @param string $route
* @param array $args
*
* @return bool
*/
private function testRequest(
OutputInterface $output,
string $type,
string $route,
array $args = []
): bool {
$request = new NC22Request('', Request::type($type));
$this->configService->configureLoopbackRequest($request, $route, $args);
$request->setFollowLocation(false);
if ($request->getType() !== Request::TYPE_GET) {
$request->setDataSerialize(new SimpleDataStore(['empty' => 1]));
}
$output->write('- ' . $type . ' request on ' . $request->getCompleteUrl() . ': ');
try {
$this->doRequest($request);
$result = $request->getResult();
$color = 'error';
if ($result->getStatusCode() === 200) {
$color = 'info';
}
$output->writeln('<' . $color . '>' . $result->getStatusCode() . '</' . $color . '>');
if ($result->getStatusCode() === 200) {
return true;
}
} catch (RequestNetworkException $e) {
$output->writeln('<error>fail</error>');
}
return false;
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @param string $address
*/
private function saveUrl(InputInterface $input, OutputInterface $output, string $address): void {
if ($address === '') {
return;
}
$output->writeln('');
$output->writeln(
'The address <info>' . $address . '</info> seems to reach your local Nextcloud.'
);
$helper = $this->getHelper('question');
$output->writeln('');
$question = new ConfirmationQuestion(
'<info>Do you want to store this address in database ?</info> (y/N) ', false, '/^(y|Y)/i'
);
if (!$helper->ask($input, $output, $question)) {
$output->writeln('Configuration NOT saved');
return;
}
$this->configService->setAppValue(ConfigService::FORCE_NC_BASE, $address);
$output->writeln(
'New configuration <info>' . Application::APP_ID . '.' . ConfigService::FORCE_NC_BASE . '=\''
. $address . '\'</info> stored in database'
);
}
/**
* @param string $test
*
* @return array
* @throws Exception
*/
private function parseAddress(string $test): array {
$scheme = parse_url($test, PHP_URL_SCHEME);
$cloudId = parse_url($test, PHP_URL_HOST);
$cloudIdPort = parse_url($test, PHP_URL_PORT);
$path = parse_url($test, PHP_URL_PATH);
if (is_bool($scheme) || is_bool($cloudId) || is_null($scheme) || is_null($cloudId)) {
throw new Exception();
}
if (is_null($path) || is_bool($path)) {
$path = '';
}
$path = rtrim($path, '/');
if (!is_null($cloudIdPort)) {
$cloudId = $cloudId . ':' . $cloudIdPort;
}
return [$scheme, $cloudId, $path];
}
}