Apply PSR-12 code style (#3886)
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / acp / form / PackageUpdateServerAddForm.class.php
1 <?php
2
3 namespace wcf\acp\form;
4
5 use wcf\data\package\update\server\PackageUpdateServer;
6 use wcf\data\package\update\server\PackageUpdateServerAction;
7 use wcf\data\package\update\server\PackageUpdateServerList;
8 use wcf\form\AbstractForm;
9 use wcf\system\exception\UserInputException;
10 use wcf\system\request\LinkHandler;
11 use wcf\system\WCF;
12 use wcf\system\WCFACP;
13 use wcf\util\StringUtil;
14 use wcf\util\Url;
15
16 /**
17 * Shows the server add form.
18 *
19 * @author Marcel Werk
20 * @copyright 2001-2019 WoltLab GmbH
21 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
22 * @package WoltLabSuite\Core\Acp\Form
23 */
24 class PackageUpdateServerAddForm extends AbstractForm
25 {
26 /**
27 * @inheritDoc
28 */
29 public $activeMenuItem = 'wcf.acp.menu.link.package.server.add';
30
31 /**
32 * @inheritDoc
33 */
34 public $neededPermissions = ['admin.configuration.package.canEditServer'];
35
36 /**
37 * server url
38 * @var string
39 */
40 public $serverURL = '';
41
42 /**
43 * server login username
44 * @var string
45 */
46 public $loginUsername = '';
47
48 /**
49 * server login password
50 * @var string
51 */
52 public $loginPassword = '';
53
54 /**
55 * @inheritDoc
56 */
57 public function readFormParameters()
58 {
59 parent::readFormParameters();
60
61 if (isset($_POST['serverURL'])) {
62 $this->serverURL = StringUtil::trim($_POST['serverURL']);
63 }
64 if (isset($_POST['loginUsername'])) {
65 $this->loginUsername = $_POST['loginUsername'];
66 }
67 if (isset($_POST['loginPassword'])) {
68 $this->loginPassword = $_POST['loginPassword'];
69 }
70 }
71
72 /**
73 * @inheritDoc
74 */
75 public function validate()
76 {
77 parent::validate();
78
79 $this->validateServerURL();
80 }
81
82 /**
83 * Validates the server URL.
84 *
85 * @since 5.3
86 */
87 protected function validateServerURL()
88 {
89 if (empty($this->serverURL)) {
90 throw new UserInputException('serverURL');
91 }
92
93 if (!PackageUpdateServer::isValidServerURL($this->serverURL)) {
94 throw new UserInputException('serverURL', 'invalid');
95 }
96
97 if (StringUtil::endsWith(Url::parse($this->serverURL)['host'], '.woltlab.com', true)) {
98 throw new UserInputException('serverURL', 'woltlab');
99 }
100
101 if (($duplicate = $this->findDuplicateServer())) {
102 throw new UserInputException('serverURL', [
103 'duplicate' => $duplicate,
104 ]);
105 }
106 }
107
108 /**
109 * Returns the first package update server with a matching serverURL.
110 *
111 * @since 5.3
112 */
113 protected function findDuplicateServer()
114 {
115 $packageServerList = new PackageUpdateServerList();
116 $packageServerList->readObjects();
117 foreach ($packageServerList as $packageServer) {
118 if ($packageServer->serverURL == $this->serverURL) {
119 return $packageServer;
120 }
121 }
122 }
123
124 /**
125 * @inheritDoc
126 */
127 public function save()
128 {
129 parent::save();
130
131 // save server
132 $this->objectAction = new PackageUpdateServerAction([], 'create', [
133 'data' => \array_merge($this->additionalFields, [
134 'serverURL' => $this->serverURL,
135 'loginUsername' => $this->loginUsername,
136 'loginPassword' => $this->loginPassword,
137 ]),
138 ]);
139 $returnValues = $this->objectAction->executeAction();
140 $this->saved();
141
142 // reset values
143 $this->serverURL = $this->loginUsername = $this->loginPassword = '';
144
145 // show success message
146 WCF::getTPL()->assign([
147 'success' => true,
148 'objectEditLink' => LinkHandler::getInstance()->getControllerLink(
149 PackageUpdateServerEditForm::class,
150 ['id' => $returnValues['returnValues']->packageUpdateServerID]
151 ),
152 ]);
153 }
154
155 /**
156 * @inheritDoc
157 */
158 public function assignVariables()
159 {
160 parent::assignVariables();
161
162 WCF::getTPL()->assign([
163 'serverURL' => $this->serverURL,
164 'loginUsername' => $this->loginUsername,
165 'loginPassword' => $this->loginPassword,
166 'action' => 'add',
167 ]);
168 }
169
170 /**
171 * @inheritDoc
172 */
173 public function show()
174 {
175 // check master password
176 WCFACP::checkMasterPassword();
177
178 parent::show();
179 }
180 }