| 1 | <?php |
| 2 | |
| 3 | namespace wcf\system\package; |
| 4 | |
| 5 | use wcf\data\package\update\server\PackageUpdateServer; |
| 6 | use wcf\system\exception\UserException; |
| 7 | use wcf\system\WCF; |
| 8 | |
| 9 | /** |
| 10 | * Handles the case that the credentials for update server are either missing or invalid. |
| 11 | * |
| 12 | * @author Alexander Ebert |
| 13 | * @copyright 2001-2024 WoltLab GmbH |
| 14 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
| 15 | */ |
| 16 | final class PackageUpdateUnauthorizedException extends UserException |
| 17 | { |
| 18 | /** |
| 19 | * @param string[] $responseHeaders |
| 20 | * @param mixed[] $packageUpdateVersion |
| 21 | */ |
| 22 | public function __construct( |
| 23 | private readonly int $responseStatusCode, |
| 24 | private readonly array $responseHeaders, |
| 25 | private readonly string $responseMessage, |
| 26 | private readonly PackageUpdateServer $updateServer, |
| 27 | private readonly array $packageUpdateVersion = [] |
| 28 | ) { |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Returns the rendered template. |
| 33 | */ |
| 34 | public function getRenderedTemplate(): string |
| 35 | { |
| 36 | $requiresPaidUpgrade = false; |
| 37 | if ($this->updateServer->isWoltLabStoreServer() && !empty($this->packageUpdateVersion['pluginStoreFileID'])) { |
| 38 | $requiresPaidUpgrade = ($this->responseHeaders['wcf-update-server-requires-paid-upgrade'][0] ?? '') === 'true'; |
| 39 | } |
| 40 | |
| 41 | if ($requiresPaidUpgrade) { |
| 42 | WCF::getTPL()->assign([ |
| 43 | 'packageName' => $this->packageUpdateVersion['packageName'], |
| 44 | 'pluginStoreFileID' => $this->packageUpdateVersion['pluginStoreFileID'], |
| 45 | ]); |
| 46 | |
| 47 | return WCF::getTPL()->fetch('packageUpdateUnauthorizedPaidUpgrade'); |
| 48 | } |
| 49 | |
| 50 | $authInsufficient = (($this->responseHeaders['wcf-update-server-auth'][0] ?? '') === 'unauthorized'); |
| 51 | if ($authInsufficient && !empty($this->packageUpdateVersion['pluginStoreFileID'])) { |
| 52 | $hasOnlyTrustedServers = true; |
| 53 | foreach (PackageUpdateServer::getActiveUpdateServers() as $updateServer) { |
| 54 | if (!$updateServer->isWoltLabUpdateServer() && !$updateServer->isWoltLabStoreServer()) { |
| 55 | $hasOnlyTrustedServers = false; |
| 56 | break; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | if ($hasOnlyTrustedServers) { |
| 61 | WCF::getTPL()->assign([ |
| 62 | 'packageName' => $this->packageUpdateVersion['packageName'], |
| 63 | 'pluginStoreFileID' => $this->packageUpdateVersion['pluginStoreFileID'], |
| 64 | ]); |
| 65 | |
| 66 | return WCF::getTPL()->fetch('packageUpdateUnauthorizedPurchaseRequired'); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | WCF::getTPL()->assign([ |
| 71 | 'authInsufficient' => $authInsufficient, |
| 72 | 'packageUpdateVersion' => $this->packageUpdateVersion, |
| 73 | 'updateServer' => $this->updateServer, |
| 74 | 'serverAuthData' => $this->updateServer->getAuthData(), |
| 75 | 'requiresPaidUpgrade' => $requiresPaidUpgrade, |
| 76 | 'responseStatusCode' => $this->responseStatusCode, |
| 77 | 'responseHeaders' => $this->responseHeaders, |
| 78 | 'responseMessage' => $this->responseMessage, |
| 79 | ]); |
| 80 | |
| 81 | return WCF::getTPL()->fetch('packageUpdateUnauthorized'); |
| 82 | } |
| 83 | |
| 84 | public function getResponseMessage(): string |
| 85 | { |
| 86 | return $this->responseMessage; |
| 87 | } |
| 88 | } |