3 namespace wcf\data\package
;
5 use wcf\data\AbstractDatabaseObjectAction
;
6 use wcf\system\database\util\PreparedStatementConditionBuilder
;
7 use wcf\system\exception\SystemException
;
8 use wcf\system\io\RemoteFile
;
9 use wcf\system\request\LinkHandler
;
11 use wcf\util\HTTPRequest
;
15 * Executes package-related actions.
17 * @author Alexander Ebert
18 * @copyright 2001-2019 WoltLab GmbH
19 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
20 * @package WoltLabSuite\Core\Data\Package
22 * @method Package create()
23 * @method PackageEditor[] getObjects()
24 * @method PackageEditor getSingleObject()
26 class PackageAction
extends AbstractDatabaseObjectAction
31 protected $className = PackageEditor
::class;
36 protected $permissionsCreate = ['admin.configuration.package.canInstallPackage'];
41 protected $permissionsDelete = ['admin.configuration.package.canUninstallPackage'];
46 protected $permissionsUpdate = ['admin.configuration.package.canUpdatePackage'];
51 protected $requireACP = ['searchForPurchasedItems'];
54 * Validates parameters to search for purchased items in the WoltLab Plugin-Store.
56 public function validateSearchForPurchasedItems()
58 WCF
::getSession()->checkPermissions([
59 'admin.configuration.package.canInstallPackage',
60 'admin.configuration.package.canUpdatePackage',
63 $this->readString('password', true);
64 $this->readString('username', true);
66 if (empty($this->parameters
['username'])) {
67 $conditions = new PreparedStatementConditionBuilder();
68 $conditions->add("serverURL REGEXP ?", ['https?://store\.woltlab\.com/[a-z]+/']);
69 $conditions->add("loginUsername <> ''");
70 $conditions->add("loginPassword <> ''");
72 // check if user has already provided credentials
73 $sql = "SELECT loginUsername, loginPassword
74 FROM wcf" . WCF_N
. "_package_update_server
76 $statement = WCF
::getDB()->prepareStatement($sql, 1);
77 $statement->execute($conditions->getParameters());
78 $row = $statement->fetchArray();
79 if (!empty($row['loginUsername']) && !empty($row['loginPassword'])) {
80 $this->parameters
['password'] = $row['loginPassword'];
81 $this->parameters
['username'] = $row['loginUsername'];
87 * Searches for purchased items in the WoltLab Plugin-Store.
90 * @throws SystemException
92 public function searchForPurchasedItems()
94 if (!RemoteFile
::supportsSSL()) {
96 'noSSL' => WCF
::getLanguage()->get('wcf.acp.pluginStore.api.noSSL'),
100 if (empty($this->parameters
['username']) ||
empty($this->parameters
['password'])) {
102 'template' => $this->renderAuthorizationDialog(false),
106 $request = new HTTPRequest('https://api.woltlab.com/1.1/customer/purchases/list.json', [
109 'username' => $this->parameters
['username'],
110 'password' => $this->parameters
['password'],
111 'wcfVersion' => WCF_VERSION
,
115 $reply = $request->getReply();
116 $response = JSON
::decode($reply['body']);
118 $code = $response['status'] ??
500;
121 if (empty($response['products'])) {
123 'noResults' => WCF
::getLanguage()->getDynamicVariable('wcf.acp.pluginStore.purchasedItems.noResults'),
126 WCF
::getSession()->register('__pluginStoreProducts', $response['products']);
127 WCF
::getSession()->register('__pluginStoreWcfMajorReleases', $response['wcfMajorReleases']);
130 'redirectURL' => LinkHandler
::getInstance()->getLink('PluginStorePurchasedItems'),
135 // authentication error
138 'template' => $this->renderAuthorizationDialog(true),
142 // any other kind of errors
144 throw new SystemException(WCF
::getLanguage()->getDynamicVariable(
145 'wcf.acp.pluginStore.api.error',
153 * Renders the authentication dialog.
155 * @param bool $rejected
158 protected function renderAuthorizationDialog($rejected)
160 WCF
::getTPL()->assign([
161 'rejected' => $rejected,
164 return WCF
::getTPL()->fetch('pluginStoreAuthorization');