Move the handling of the license data into a separate API
authorAlexander Ebert <ebert@woltlab.com>
Fri, 22 Sep 2023 15:31:03 +0000 (17:31 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Fri, 22 Sep 2023 15:31:03 +0000 (17:31 +0200)
wcfsetup/install/files/lib/acp/page/LicensePage.class.php
wcfsetup/install/files/lib/system/package/license/LicenseApi.class.php [new file with mode: 0644]

index ef23f57517fbdfe2bbeee82b443b40133121f76b..f7ffccdfa0f4ccda2648c1748f2d3b64e38d1757 100644 (file)
@@ -2,9 +2,6 @@
 
 namespace wcf\acp\page;
 
-use CuyZ\Valinor\Mapper\Source\Source;
-use CuyZ\Valinor\MapperBuilder;
-use GuzzleHttp\Psr7\Request;
 use Laminas\Diactoros\Response\RedirectResponse;
 use wcf\acp\form\LicenseEditForm;
 use wcf\data\package\Package;
@@ -13,7 +10,7 @@ use wcf\data\package\update\PackageUpdateAction;
 use wcf\data\package\update\server\PackageUpdateServer;
 use wcf\page\AbstractPage;
 use wcf\system\database\util\PreparedStatementConditionBuilder;
-use wcf\system\io\HttpFactory;
+use wcf\system\package\license\LicenseApi;
 use wcf\system\request\LinkHandler;
 use wcf\system\WCF;
 
@@ -31,9 +28,11 @@ final class LicensePage extends AbstractPage
 
     public $neededPermissions = ['admin.configuration.package.canInstallPackage'];
 
+    private LicenseApi $licenseApi;
+
     private array $licenseData;
 
-    private string $licenseNumber;
+    private int $licenseNumber;
 
     private array $installedPackages;
 
@@ -41,8 +40,6 @@ final class LicensePage extends AbstractPage
 
     private array $packageUpdates = [];
 
-    private PackageUpdateServer $updateServer;
-
     private array $requiresLicenseExtension = [];
 
     private const CURRENT_MAJOR = '6.0';
@@ -51,9 +48,9 @@ final class LicensePage extends AbstractPage
     {
         parent::readData();
 
-        $this->updateServer = PackageUpdateServer::getWoltLabUpdateServer();
+        $this->licenseApi = new LicenseApi();
 
-        if (!$this->hasLicenseCredentials()) {
+        if (!$this->licenseApi->hasLicenseCredentials()) {
             return new RedirectResponse(
                 LinkHandler::getInstance()->getControllerLink(
                     LicenseEditForm::class,
@@ -66,7 +63,10 @@ final class LicensePage extends AbstractPage
 
         (new PackageUpdateAction([], 'refreshDatabase'))->executeAction();
 
-        $this->licenseData = $this->fetchLicenseData();
+        $this->licenseData = $this->licenseApi->fetchLicenseData();
+        if (isset($this->licenseData['license']['licenseID'])) {
+            $this->licenseNumber = $this->licenseData['license']['licenseID'];
+        }
 
         $identifiers = \array_merge(
             \array_keys($this->licenseData['woltlab']),
@@ -119,16 +119,6 @@ final class LicensePage extends AbstractPage
         }
     }
 
-    private function hasLicenseCredentials(): bool
-    {
-        $authData = $this->updateServer->getAuthData();
-        if (empty($authData['username']) || empty($authData['password'])) {
-            return false;
-        }
-
-        return true;
-    }
-
     public function assignVariables()
     {
         parent::assignVariables();
@@ -625,47 +615,4 @@ final class LicensePage extends AbstractPage
 
         return $packageUpdates;
     }
-
-    // This code was stolen from "FirstTimeSetupLicenseForm" and
-    // should propably be moved into a helper class. We might even want to refresh
-    // the data with requests to the package servers to implicitly fetch the
-    // latest purchases.
-    private function fetchLicenseData(): array|object
-    {
-        $authData = $this->updateServer->getAuthData();
-        $this->licenseNumber = $authData['username'];
-
-        $request = new Request(
-            'POST',
-            'https://api.woltlab.com/2.1/customer/license/list.json',
-            [
-                'content-type' => 'application/x-www-form-urlencoded',
-            ],
-            \http_build_query([
-                'licenseNo' => $this->licenseNumber,
-                'serialNo' => $authData['password'],
-                'instanceId' => \hash_hmac('sha256', 'api.woltlab.com', \WCF_UUID),
-            ], '', '&', \PHP_QUERY_RFC1738)
-        );
-
-        $response = HttpFactory::makeClientWithTimeout(5)->send($request);
-        return (new MapperBuilder())
-            ->allowSuperfluousKeys()
-            ->mapper()
-            ->map(
-                <<<'EOT'
-                    array {
-                        status: 200,
-                        license: array {
-                            authCode?: string,
-                            type: string,
-                            expiryDates?: array<string, int>,
-                        },
-                        pluginstore: array<string, string>,
-                        woltlab: array<string, string>,
-                    }
-                    EOT,
-                Source::json($response->getBody())
-            );
-    }
 }
diff --git a/wcfsetup/install/files/lib/system/package/license/LicenseApi.class.php b/wcfsetup/install/files/lib/system/package/license/LicenseApi.class.php
new file mode 100644 (file)
index 0000000..eafd783
--- /dev/null
@@ -0,0 +1,81 @@
+<?php
+
+namespace wcf\system\package\license;
+
+use CuyZ\Valinor\Mapper\Source\Source;
+use CuyZ\Valinor\MapperBuilder;
+use GuzzleHttp\Psr7\Request;
+use wcf\data\package\update\server\PackageUpdateServer;
+use wcf\system\io\HttpFactory;
+
+/**
+ * Provides access to the license data.
+ *
+ * @author Alexander Ebert
+ * @copyright 2001-2023 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @since 6.0
+ */
+final class LicenseApi
+{
+    private readonly PackageUpdateServer $packageUpdateServer;
+
+    public function __construct()
+    {
+        $this->packageUpdateServer = PackageUpdateServer::getWoltLabUpdateServer();
+    }
+
+    public function fetchLicenseData(): array|object
+    {
+        if (!$this->hasLicenseCredentials()) {
+            // TODO
+            throw new \Exception("no credentials");
+        }
+
+        $authData = $this->packageUpdateServer->getAuthData();
+
+        $request = new Request(
+            'POST',
+            'https://api.woltlab.com/2.1/customer/license/list.json',
+            [
+                'content-type' => 'application/x-www-form-urlencoded',
+            ],
+            \http_build_query([
+                'licenseNo' => $authData['username'],
+                'serialNo' => $authData['password'],
+                'instanceId' => \hash_hmac('sha256', 'api.woltlab.com', \WCF_UUID),
+            ], '', '&', \PHP_QUERY_RFC1738)
+        );
+
+        $response = HttpFactory::makeClientWithTimeout(5)->send($request);
+        return (new MapperBuilder())
+            ->allowSuperfluousKeys()
+            ->mapper()
+            ->map(
+                <<<'EOT'
+                    array {
+                        status: 200,
+                        license: array {
+                            authCode?: string,
+                            licenseID?: int,
+                            type: string,
+                            expiryDates?: array<string, int>,
+                        },
+                        pluginstore: array<string, string>,
+                        woltlab: array<string, string>,
+                    }
+                    EOT,
+                Source::json($response->getBody())
+            );
+    }
+
+    public function hasLicenseCredentials(): bool
+    {
+        $authData = $this->packageUpdateServer->getAuthData();
+        if (empty($authData['username']) || empty($authData['password'])) {
+            return false;
+        }
+
+        return true;
+    }
+}