Merge pull request #5673 from WoltLab/license-page-graceful
authorTim Düsterhus <duesterhus@woltlab.com>
Fri, 29 Sep 2023 13:51:45 +0000 (15:51 +0200)
committerGitHub <noreply@github.com>
Fri, 29 Sep 2023 13:51:45 +0000 (15:51 +0200)
Gracefully handle failed license update requests in LicensePage

1  2 
wcfsetup/install/files/lib/acp/page/LicensePage.class.php

index 1bfaad9bf4b20fd2e53dd571f7633a9ec4e4f98e,3b468064d0fd611f4d3208071f1cc2d57b91de91..74ca898fc6b15fb5c2c74f7e336f83d8393a67c6
@@@ -9,9 -9,11 +9,11 @@@ use wcf\data\package\update\PackageUpda
  use wcf\data\package\update\server\PackageUpdateServer;
  use wcf\page\AbstractPage;
  use wcf\system\database\util\PreparedStatementConditionBuilder;
+ use wcf\system\exception\NamedUserException;
+ use wcf\system\package\license\exception\ParsingFailed;
  use wcf\system\package\license\LicenseApi;
  use wcf\system\package\license\LicenseData;
 +use wcf\system\package\PackageUpdateDispatcher;
  use wcf\system\request\LinkHandler;
  use wcf\system\WCF;
  
@@@ -58,11 -60,32 +60,32 @@@ final class LicensePage extends Abstrac
              );
          }
  
 -        (new PackageUpdateAction([], 'refreshDatabase'))->executeAction();
 +        PackageUpdateDispatcher::getInstance()->refreshPackageDatabase();
  
          $licenseApi = new LicenseApi();
-         $this->licenseData = $licenseApi->fetchFromRemote();
-         $licenseApi->updateLicenseFile($this->licenseData);
+         try {
+             $this->licenseData = $licenseApi->readFromFile();
+             if (
+                 $this->licenseData === null
+                 // Cache valid license data for 2 minutes.
+                 || $this->licenseData->creationDate->getTimestamp() < (\TIME_NOW - 2 * 60)
+             ) {
+                 $this->licenseData = $licenseApi->fetchFromRemote();
+                 $licenseApi->updateLicenseFile($this->licenseData);
+             }
+         } catch (ParsingFailed $e) {
+             if (\ENABLE_DEBUG_MODE && \ENABLE_DEVELOPER_TOOLS) {
+                 throw $e;
+             }
+             throw new NamedUserException(WCF::getLanguage()->getDynamicVariable(
+                 'wcf.acp.license.error.parsingFailed',
+                 [
+                     'licenseData' => $this->licenseData,
+                 ]
+             ));
+         }
  
          $identifiers = \array_merge(
              \array_keys($this->licenseData->woltlab),