Apply PSR-12 code style (#3886)
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / package / PackageAction.class.php
CommitLineData
158bd3ca 1<?php
a9229942 2
158bd3ca 3namespace wcf\data\package;
a9229942 4
158bd3ca 5use wcf\data\AbstractDatabaseObjectAction;
93c9c717 6use wcf\system\database\util\PreparedStatementConditionBuilder;
eb0f6246 7use wcf\system\exception\SystemException;
e7714ce1 8use wcf\system\io\RemoteFile;
eb0f6246 9use wcf\system\request\LinkHandler;
317c8af5 10use wcf\system\WCF;
e7714ce1 11use wcf\util\HTTPRequest;
079b9ff4 12use wcf\util\JSON;
158bd3ca
TD
13
14/**
15 * Executes package-related actions.
a9229942
TD
16 *
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
21 *
22 * @method Package create()
23 * @method PackageEditor[] getObjects()
24 * @method PackageEditor getSingleObject()
158bd3ca 25 */
a9229942
TD
26class PackageAction extends AbstractDatabaseObjectAction
27{
28 /**
29 * @inheritDoc
30 */
31 protected $className = PackageEditor::class;
32
33 /**
34 * @inheritDoc
35 */
36 protected $permissionsCreate = ['admin.configuration.package.canInstallPackage'];
37
38 /**
39 * @inheritDoc
40 */
41 protected $permissionsDelete = ['admin.configuration.package.canUninstallPackage'];
42
43 /**
44 * @inheritDoc
45 */
46 protected $permissionsUpdate = ['admin.configuration.package.canUpdatePackage'];
47
48 /**
49 * @inheritDoc
50 */
51 protected $requireACP = ['searchForPurchasedItems'];
52
53 /**
54 * Validates parameters to search for purchased items in the WoltLab Plugin-Store.
55 */
56 public function validateSearchForPurchasedItems()
57 {
58 WCF::getSession()->checkPermissions([
59 'admin.configuration.package.canInstallPackage',
60 'admin.configuration.package.canUpdatePackage',
61 ]);
62
63 $this->readString('password', true);
64 $this->readString('username', true);
65
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 <> ''");
71
72 // check if user has already provided credentials
73 $sql = "SELECT loginUsername, loginPassword
74 FROM wcf" . WCF_N . "_package_update_server
75 " . $conditions;
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'];
82 }
83 }
84 }
85
86 /**
87 * Searches for purchased items in the WoltLab Plugin-Store.
88 *
89 * @return string[]
90 * @throws SystemException
91 */
92 public function searchForPurchasedItems()
93 {
94 if (!RemoteFile::supportsSSL()) {
95 return [
96 'noSSL' => WCF::getLanguage()->get('wcf.acp.pluginStore.api.noSSL'),
97 ];
98 }
99
100 if (empty($this->parameters['username']) || empty($this->parameters['password'])) {
101 return [
102 'template' => $this->renderAuthorizationDialog(false),
103 ];
104 }
105
106 $request = new HTTPRequest('https://api.woltlab.com/1.1/customer/purchases/list.json', [
107 'method' => 'POST',
108 ], [
109 'username' => $this->parameters['username'],
110 'password' => $this->parameters['password'],
111 'wcfVersion' => WCF_VERSION,
112 ]);
113
114 $request->execute();
115 $reply = $request->getReply();
116 $response = JSON::decode($reply['body']);
117
118 $code = $response['status'] ?? 500;
119 switch ($code) {
120 case 200:
121 if (empty($response['products'])) {
122 return [
123 'noResults' => WCF::getLanguage()->getDynamicVariable('wcf.acp.pluginStore.purchasedItems.noResults'),
124 ];
125 } else {
126 WCF::getSession()->register('__pluginStoreProducts', $response['products']);
127 WCF::getSession()->register('__pluginStoreWcfMajorReleases', $response['wcfMajorReleases']);
128
129 return [
130 'redirectURL' => LinkHandler::getInstance()->getLink('PluginStorePurchasedItems'),
131 ];
132 }
133 break;
134
135 // authentication error
136 case 401:
137 return [
138 'template' => $this->renderAuthorizationDialog(true),
139 ];
140 break;
141
142 // any other kind of errors
143 default:
144 throw new SystemException(WCF::getLanguage()->getDynamicVariable(
145 'wcf.acp.pluginStore.api.error',
146 ['status' => $code]
147 ));
148 break;
149 }
150 }
151
152 /**
153 * Renders the authentication dialog.
154 *
155 * @param bool $rejected
156 * @return string
157 */
158 protected function renderAuthorizationDialog($rejected)
159 {
160 WCF::getTPL()->assign([
161 'rejected' => $rejected,
162 ]);
163
164 return WCF::getTPL()->fetch('pluginStoreAuthorization');
165 }
dcb3a44c 166}