diff --git a/composer.lock b/composer.lock index 7847a5415..4d2feda9f 100644 --- a/composer.lock +++ b/composer.lock @@ -12,12 +12,12 @@ "source": { "type": "git", "url": "https://github.com/daita/my-small-php-tools.git", - "reference": "d5add0c22261ed661873b9c2e3d800ed71265902" + "reference": "844b10da6abf50044ae9c40120cb5b927352bd95" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/daita/my-small-php-tools/zipball/d5add0c22261ed661873b9c2e3d800ed71265902", - "reference": "d5add0c22261ed661873b9c2e3d800ed71265902", + "url": "https://api.github.com/repos/daita/my-small-php-tools/zipball/844b10da6abf50044ae9c40120cb5b927352bd95", + "reference": "844b10da6abf50044ae9c40120cb5b927352bd95", "shasum": "" }, "require": { @@ -40,7 +40,7 @@ } ], "description": "My small PHP Tools", - "time": "2020-11-13T23:51:01+00:00" + "time": "2020-12-02T14:37:02+00:00" } ], "packages-dev": [], diff --git a/lib/Command/CirclesTest.php b/lib/Command/CirclesTest.php index 4643ae84a..ba0605e8d 100644 --- a/lib/Command/CirclesTest.php +++ b/lib/Command/CirclesTest.php @@ -127,6 +127,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int $this->configService->setAppValue(ConfigService::TEST_NC_BASE, $input->getOption('url')); if (!$this->testRequest($output, 'GET', 'core.CSRFToken.index')) { + $this->configService->setAppValue(ConfigService::TEST_NC_BASE, ''); + return 0; } @@ -134,6 +136,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int $output, 'POST', 'circles.GlobalScale.asyncBroadcast', ['token' => 'test-dummy-token'] )) { + $this->configService->setAppValue(ConfigService::TEST_NC_BASE, ''); + return 0; } @@ -182,6 +186,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int private function testRequest(OutputInterface $o, string $type, string $route, array $args = []): bool { $request = new NC19Request('', Request::type($type)); $this->configService->configureRequest($request, $route, $args); + $request->setFollowLocation(false); $o->write('- ' . $type . ' request on ' . $request->getCompleteUrl() . ': '); $this->doRequest($request); diff --git a/lib/Command/MembersCreate.php b/lib/Command/MembersCreate.php index 08b3c3181..18edfc113 100644 --- a/lib/Command/MembersCreate.php +++ b/lib/Command/MembersCreate.php @@ -177,7 +177,6 @@ private function findUserFromLookup(string $search, string &$instance = ''): str $request = new NC19Request(ConfigService::GS_LOOKUP_USERS, Request::TYPE_GET); $this->configService->configureRequest($request); - $request->setProtocols(['https', 'http']); $request->basedOnUrl($lookup); $request->addParam('search', $search); diff --git a/lib/Controller/GlobalScaleController.php b/lib/Controller/GlobalScaleController.php index 1fb44bf71..455487e64 100644 --- a/lib/Controller/GlobalScaleController.php +++ b/lib/Controller/GlobalScaleController.php @@ -100,7 +100,7 @@ public function asyncBroadcast(string $token) { $this->async(); foreach ($wrappers as $wrapper) { try { - $this->gsUpstreamService->broadcastWrapper($wrapper, $this->request->getServerProtocol()); + $this->gsUpstreamService->broadcastWrapper($wrapper); } catch (GSStatusException $e) { } } diff --git a/lib/Controller/MembersController.php b/lib/Controller/MembersController.php index 84bad74db..a33c2818f 100644 --- a/lib/Controller/MembersController.php +++ b/lib/Controller/MembersController.php @@ -243,9 +243,6 @@ public function searchGlobal(string $search, int $order) { try { $this->mustHaveFrontEndEnabled(); - if ($search === 't') { - sleep(5); - } $result = $this->searchService->searchGlobal($search); } catch (\Exception $e) { return diff --git a/lib/Search/GlobalScaleUsers.php b/lib/Search/GlobalScaleUsers.php index 15e8f60d3..cfeb7f142 100644 --- a/lib/Search/GlobalScaleUsers.php +++ b/lib/Search/GlobalScaleUsers.php @@ -84,7 +84,6 @@ public function search($search) { $request = new NC19Request(ConfigService::GS_LOOKUP_USERS, Request::TYPE_GET); $this->configService->configureRequest($request); - $request->setProtocols(['https', 'http']); $request->basedOnUrl($lookup); $request->addParam('search', $search); diff --git a/lib/Service/ConfigService.php b/lib/Service/ConfigService.php index 5d8bca2dd..efd45b4d2 100644 --- a/lib/Service/ConfigService.php +++ b/lib/Service/ConfigService.php @@ -592,6 +592,10 @@ public function isLocalInstance(string $instance): bool { public function configureRequest(NC19Request $request, string $routeName = '', array $args = []) { $this->configureRequestAddress($request, $routeName, $args); + if ($this->getForcedNcBase() === '') { + $request->setProtocols(['https', 'http']); + } + $request->setVerifyPeer($this->getAppValue(ConfigService::CIRCLES_SELF_SIGNED) !== '1'); $request->setLocalAddressAllowed(true); } @@ -613,11 +617,9 @@ private function configureRequestAddress(NC19Request $request, string $routeName return; } - $ncBase = ($this->getAppValue(self::TEST_NC_BASE)) ? - $this->getAppValue(self::TEST_NC_BASE) : $this->getForcedNcBase(); - + $ncBase = $this->getForcedNcBase(); if ($ncBase !== '') { - $absolute = rtrim($ncBase, '/') . $this->urlGenerator->linkToRoute($routeName, $args); + $absolute = $this->cleanLinkToRoute($ncBase, $routeName, $args); } else { $absolute = $this->urlGenerator->linkToRouteAbsolute($routeName, $args); } @@ -632,6 +634,10 @@ private function configureRequestAddress(NC19Request $request, string $routeName * @return string */ private function getForcedNcBase(): string { + if ($this->getAppValue(self::TEST_NC_BASE) !== '') { + return $this->getAppValue(self::TEST_NC_BASE); + } + $fromConfig = $this->config->getSystemValue('circles.force_nc_base', ''); if ($fromConfig !== '') { return $fromConfig; @@ -640,5 +646,26 @@ private function getForcedNcBase(): string { return $this->getAppValue(self::FORCE_NC_BASE); } + + /** + * sometimes, linkToRoute will include the base path to the nextcloud which will be duplicate with ncBase + * + * @param string $ncBase + * @param string $routeName + * @param array $args + * + * @return string + */ + private function cleanLinkToRoute(string $ncBase, string $routeName, array $args): string { + $link = $this->urlGenerator->linkToRoute($routeName, $args); + $forcedPath = rtrim(parse_url($ncBase, PHP_URL_PATH), '/'); + + if ($forcedPath !== '' && strpos($link, $forcedPath) === 0) { + $ncBase = substr($ncBase, 0, -strlen($forcedPath)); + } + + return rtrim($ncBase, '/') . $link; + } + } diff --git a/lib/Service/GSUpstreamService.php b/lib/Service/GSUpstreamService.php index a64ea1051..d732360e1 100644 --- a/lib/Service/GSUpstreamService.php +++ b/lib/Service/GSUpstreamService.php @@ -162,11 +162,11 @@ public function newEvent(GSEvent $event): string { * @param GSWrapper $wrapper * @param string $protocol */ - public function broadcastWrapper(GSWrapper $wrapper, string $protocol): void { + public function broadcastWrapper(GSWrapper $wrapper): void { $status = GSWrapper::STATUS_FAILED; try { - $this->broadcastEvent($wrapper->getEvent(), $wrapper->getInstance(), $protocol); + $this->broadcastEvent($wrapper->getEvent(), $wrapper->getInstance()); $status = GSWrapper::STATUS_DONE; } catch (RequestContentException | RequestNetworkException | RequestResultSizeException | RequestServerException | RequestResultNotJsonException $e) { } @@ -192,7 +192,7 @@ public function broadcastWrapper(GSWrapper $wrapper, string $protocol): void { * @throws RequestResultSizeException * @throws RequestServerException */ - public function broadcastEvent(GSEvent $event, string $instance, string $protocol = ''): void { + public function broadcastEvent(GSEvent $event, string $instance): void { $this->signEvent($event); if ($this->configService->isLocalInstance($instance)) { @@ -202,12 +202,7 @@ public function broadcastEvent(GSEvent $event, string $instance, string $protoco $path = $this->urlGenerator->linkToRoute('circles.GlobalScale.broadcast'); $request = new NC19Request($path, Request::TYPE_POST); $this->configService->configureRequest($request); - $protocols = ['https', 'http']; - if ($protocol !== '') { - $protocols = [$protocol]; - } $request->setInstance($instance); - $request->setProtocols($protocols); } $request->setDataSerialize($event); @@ -235,14 +230,9 @@ public function confirmEvent(GSEvent &$event): void { $path = $this->urlGenerator->linkToRoute('circles.GlobalScale.event'); $request = new NC19Request($path, Request::TYPE_POST); - $request->basedOnUrl($owner->getInstance()); $this->configService->configureRequest($request); + $request->basedOnUrl($owner->getInstance()); - if ($this->get('REQUEST_SCHEME', $_SERVER) !== '') { - $request->setProtocols([$_SERVER['REQUEST_SCHEME']]); - } else { - $request->setProtocols(['https', 'http']); - } $request->setDataSerialize($event); $result = $this->retrieveJson($request); @@ -423,7 +413,6 @@ public function confirmCircleStatus(Circle $circle): bool { $path = $this->urlGenerator->linkToRoute('circles.GlobalScale.status'); $request = new NC19Request($path, Request::TYPE_POST); $this->configService->configureRequest($request); - $request->setProtocols(['https', 'http']); $request->setDataSerialize($event); $requestIssue = false; diff --git a/lib/Service/GlobalScaleService.php b/lib/Service/GlobalScaleService.php index ae153e706..2b37202cd 100644 --- a/lib/Service/GlobalScaleService.php +++ b/lib/Service/GlobalScaleService.php @@ -223,9 +223,8 @@ public function getInstances(bool $all = false): array { try { $lookup = $this->configService->getGSStatus(ConfigService::GS_LOOKUP); $request = new NC19Request(ConfigService::GS_LOOKUP_INSTANCES, Request::TYPE_POST); - $request->setProtocols(['https', 'http']); - $request->basedOnUrl($lookup); $this->configService->configureRequest($request); + $request->basedOnUrl($lookup); $request->addData('authKey', $this->configService->getGSStatus(ConfigService::GS_KEY)); try { diff --git a/lib/Service/MembersService.php b/lib/Service/MembersService.php index fb33d08d1..766b5e995 100644 --- a/lib/Service/MembersService.php +++ b/lib/Service/MembersService.php @@ -739,7 +739,6 @@ private function getGlobalScaleUserDisplayName(string $ident): string { $request = new NC19Request(ConfigService::GS_LOOKUP_USERS, Request::TYPE_GET); $this->configService->configureRequest($request); - $request->setProtocols(['https', 'http']); $request->basedOnUrl($lookup); $request->addParam('search', $ident); $request->addParam('exact', '1'); diff --git a/lib/Service/SharingFrameService.php b/lib/Service/SharingFrameService.php index 79a71f260..e38b4c989 100644 --- a/lib/Service/SharingFrameService.php +++ b/lib/Service/SharingFrameService.php @@ -305,7 +305,6 @@ public function receiveFrame($token, $uniqueId, SharingFrame $frame) { */ public function initiateShare(string $circleUniqueId, string $frameUniqueId) { $request = new NC19Request('', Request::TYPE_POST); - $request->setProtocols(['https', 'http']); $this->configService->configureRequest($request, 'circles.Shares.initShareDelivery'); $request->addParam('circleId', $circleUniqueId); $request->addParam('frameId', $frameUniqueId);