|
5 | 5 | namespace Shopware\App\SDK\Registration; |
6 | 6 |
|
7 | 7 | use Http\Discovery\Psr17Factory; |
| 8 | +use Nyholm\Psr7\Uri; |
8 | 9 | use Psr\EventDispatcher\EventDispatcherInterface; |
9 | 10 | use Psr\Http\Message\RequestInterface; |
10 | 11 | use Psr\Http\Message\ResponseInterface; |
@@ -62,32 +63,33 @@ public function register(RequestInterface $request): ResponseInterface |
62 | 63 | $this->shopSecretGeneratorInterface->generate() |
63 | 64 | ); |
64 | 65 |
|
65 | | - $this->eventDispatcher?->dispatch(new BeforeRegistrationStartsEvent($request, $shop)); |
| 66 | + $sanitizedShop = $this->getSanitizedShop($shop); |
| 67 | + $this->eventDispatcher?->dispatch(new BeforeRegistrationStartsEvent($request, $sanitizedShop)); |
66 | 68 |
|
67 | | - $this->shopRepository->createShop($shop); |
| 69 | + $this->shopRepository->createShop($sanitizedShop); |
68 | 70 | } else { |
69 | 71 | $shop->setShopUrl($queries['shop-url']); |
70 | 72 |
|
71 | | - $this->eventDispatcher?->dispatch(new BeforeRegistrationStartsEvent($request, $shop)); |
| 73 | + $sanitizedShop = $this->getSanitizedShop($shop); |
| 74 | + $this->eventDispatcher?->dispatch(new BeforeRegistrationStartsEvent($request, $sanitizedShop)); |
72 | 75 |
|
73 | | - $this->shopRepository->updateShop($shop); |
| 76 | + $this->shopRepository->updateShop($sanitizedShop); |
74 | 77 | } |
75 | 78 |
|
76 | 79 | $this->logger->info('Shop registration request received', [ |
77 | | - 'shop-id' => $shop->getShopId(), |
78 | | - 'shop-url' => $shop->getShopUrl(), |
| 80 | + 'shop-id' => $sanitizedShop->getShopId(), |
| 81 | + 'shop-url' => $sanitizedShop->getShopUrl(), |
79 | 82 | ]); |
80 | 83 |
|
81 | 84 | $psrFactory = new Psr17Factory(); |
82 | 85 |
|
83 | 86 | $data = [ |
| 87 | + // old shop is needed because the shop url is not sanitized |
84 | 88 | 'proof' => $this->responseSigner->getRegistrationSignature($this->appConfiguration, $shop), |
85 | 89 | 'confirmation_url' => $this->appConfiguration->getRegistrationConfirmUrl(), |
86 | 90 | 'secret' => $shop->getShopSecret(), |
87 | 91 | ]; |
88 | 92 |
|
89 | | - $this->fixShopUrlInDatabase($shop); |
90 | | - |
91 | 93 | $response = $psrFactory->createResponse(200); |
92 | 94 |
|
93 | 95 | return $response |
@@ -145,34 +147,30 @@ public function registerConfirm(RequestInterface $request): ResponseInterface |
145 | 147 |
|
146 | 148 | private function sanitizeShopUrl(string $shopUrl): string |
147 | 149 | { |
148 | | - $parsedUrl = parse_url($shopUrl); |
| 150 | + $uri = new Uri($shopUrl); |
149 | 151 |
|
150 | | - $protocol = $parsedUrl['scheme'] ?? ''; |
151 | | - $host = $parsedUrl['host'] ?? ''; |
152 | | - $path = $parsedUrl['path'] ?? ''; |
153 | | - $port = $parsedUrl['port'] ?? ''; |
| 152 | + $protocol = $uri->getScheme(); |
| 153 | + $host = $uri->getHost(); |
| 154 | + $path = $uri->getPath(); |
| 155 | + $port = $uri->getPort(); |
154 | 156 |
|
155 | 157 | /** @var string $normalizedPath */ |
156 | 158 | $normalizedPath = preg_replace('#/{2,}#', '/', $path); |
157 | 159 | $normalizedPath = rtrim($normalizedPath, '/'); |
158 | 160 |
|
159 | | - return sprintf( |
160 | | - '%s://%s%s%s', |
161 | | - $protocol, |
162 | | - $host, |
163 | | - $port ? ':' . $port : null, |
164 | | - $normalizedPath |
165 | | - ); |
| 161 | + $url = $protocol . '://' . $host; |
| 162 | + if ($port) { |
| 163 | + $url .= ':' . $port; |
| 164 | + } |
| 165 | + $url .= $normalizedPath; |
| 166 | + |
| 167 | + return $url; |
166 | 168 | } |
167 | 169 |
|
168 | | - private function fixShopUrlInDatabase(ShopInterface $shop): void |
| 170 | + private function getSanitizedShop(ShopInterface $shop): ShopInterface |
169 | 171 | { |
170 | | - $sanitizedShopUrl = $this->sanitizeShopUrl($shop->getShopUrl()); |
| 172 | + $sanitizedShop = clone $shop; |
171 | 173 |
|
172 | | - |
173 | | - if ($shop->getShopUrl() !== $sanitizedShopUrl) { |
174 | | - $shop->setShopUrl($sanitizedShopUrl); |
175 | | - $this->shopRepository->updateShop($shop); |
176 | | - } |
| 174 | + return $sanitizedShop->setShopUrl($this->sanitizeShopUrl($shop->getShopUrl())); |
177 | 175 | } |
178 | 176 | } |
0 commit comments