Bearsampp 2026.9.23
Loading...
Searching...
No Matches
class.action.addVhost.php
Go to the documentation of this file.
1<?php
2/*
3 * Copyright (c) 2021-2026 Bearsampp
4 * License: GNU General Public License version 3 or later; see LICENSE.txt
5 * Author: Bear
6 * Website: https://bearsampp.com
7 * Github: https://github.com/Bearsampp
8 */
9
15{
27 private $wbLabelExp;
28
34 protected function getWindowTitle()
35 {
36 global $bearsamppLang;
37
38 return $bearsamppLang->getValue(Lang::ADD_VHOST_TITLE);
39 }
40
46 protected function getDialogTitle()
47 {
48 global $bearsamppLang;
49
50 return $bearsamppLang->getValue(Lang::ADD_VHOST_TITLE);
51 }
52
58 protected function getDeleteDialogTitle()
59 {
60 // Not used in add mode
61 return '';
62 }
63
71 protected function createFormFields($bearsamppWinbinder)
72 {
74
75 $initServerName = 'test.local';
76 $initDocumentRoot = Path::formatWindowsPath(Path::getWwwPath()) . '\\' . $initServerName;
77
78 $this->wbLabelServerName = $bearsamppWinbinder->createLabel(
79 $this->wbWindow,
81 15,
82 15,
83 85,
84 null,
85 WBC_RIGHT
86 );
87 $this->wbInputServerName = $bearsamppWinbinder->createInputText(
88 $this->wbWindow,
89 $initServerName,
90 105,
91 13,
92 150,
93 null
94 );
95
96 $this->wbLabelDocRoot = $bearsamppWinbinder->createLabel(
97 $this->wbWindow,
99 15,
100 45,
101 85,
102 null,
103 WBC_RIGHT
104 );
105 $this->wbInputDocRoot = $bearsamppWinbinder->createInputText(
106 $this->wbWindow,
107 $initDocumentRoot,
108 105,
109 43,
110 190,
111 null,
112 null,
113 WBC_READONLY
114 );
115 $this->wbBtnDocRoot = $bearsamppWinbinder->createButton(
116 $this->wbWindow,
118 300,
119 43,
120 110
121 );
122
123 $this->wbLabelExp = $bearsamppWinbinder->createLabel(
124 $this->wbWindow,
125 sprintf($bearsamppLang->getValue(Lang::VHOST_EXP_LABEL), $initServerName, $initDocumentRoot),
126 15,
127 80,
128 470,
129 50
130 );
131 }
132
140 protected function getFormValues($bearsamppWinbinder)
141 {
142 return [
143 'serverName' => $bearsamppWinbinder->getText($this->wbInputServerName[WinBinder::CTRL_OBJ]),
144 'documentRoot' => $bearsamppWinbinder->getText($this->wbInputDocRoot[WinBinder::CTRL_OBJ])
145 ];
146 }
147
155 protected function validateInput($values)
156 {
157 global $bearsamppLang;
158
159 if (!Util::isValidDomainName($values['serverName'])) {
160 return [
161 'valid' => false,
162 'error' => sprintf($bearsamppLang->getValue(Lang::VHOST_NOT_VALID_DOMAIN), $values['serverName'])
163 ];
164 }
165
166 return ['valid' => true];
167 }
168
176 protected function itemExists($values)
177 {
178 global $bearsamppRoot, $bearsamppLang, $bearsamppWinbinder;
179
180 if (is_file(Path::getVhostsPath() . '/' . $values['serverName'] . '.conf')) {
181 $bearsamppWinbinder->messageBoxError(
182 sprintf($bearsamppLang->getValue(Lang::VHOST_ALREADY_EXISTS), $values['serverName']),
183 $this->getDialogTitle()
184 );
185
186 return true;
187 }
188
189 return false;
190 }
191
199 protected function saveItem($values)
200 {
201 global $bearsamppRoot, $bearsamppBins, $bearsamppOpenSsl;
202
203 // Create SSL certificate
204 if (!$bearsamppOpenSsl->createCrt($values['serverName'])) {
205 return false;
206 }
207
208 // Create vhost configuration file
209 return file_put_contents(
210 Path::getVhostsPath() . '/' . $values['serverName'] . '.conf',
211 $bearsamppBins->getApache()->getVhostContent($values['serverName'], $values['documentRoot'])
212 ) !== false;
213 }
214
220 protected function deleteItem()
221 {
222 // Not used in add mode
223 return false;
224 }
225
233 protected function getSaveSuccessMessage($values)
234 {
235 global $bearsamppLang;
236
237 return sprintf(
239 $values['serverName'],
240 $values['serverName'],
241 $values['documentRoot']
242 );
243 }
244
250 protected function getSaveErrorMessage()
251 {
252 global $bearsamppLang;
253
255 }
256
262 protected function getDeleteConfirmMessage()
263 {
264 // Not used in add mode
265 return '';
266 }
267
273 protected function getDeleteSuccessMessage()
274 {
275 // Not used in add mode
276 return '';
277 }
278
284 protected function getDeleteErrorMessage()
285 {
286 // Not used in add mode
287 return '';
288 }
289
295 protected function restartService()
296 {
297 global $bearsamppBins;
298 $bearsamppBins->getApache()->getService()->restart();
299 }
300
312 protected function handleCustomEvent($window, $id, $ctrl, $param1, $param2)
313 {
314 global $bearsamppLang, $bearsamppWinbinder;
315
316 $serverName = $bearsamppWinbinder->getText($this->wbInputServerName[WinBinder::CTRL_OBJ]);
317 $documentRoot = $bearsamppWinbinder->getText($this->wbInputDocRoot[WinBinder::CTRL_OBJ]);
318
319 // Handle server name input change
320 if ($id == $this->wbInputServerName[WinBinder::CTRL_ID]) {
321 $bearsamppWinbinder->setText(
322 $this->wbLabelExp[WinBinder::CTRL_OBJ],
323 sprintf($bearsamppLang->getValue(Lang::VHOST_EXP_LABEL), $serverName, $documentRoot)
324 );
325 $bearsamppWinbinder->setEnabled(
326 $this->wbBtnSave[WinBinder::CTRL_OBJ],
327 !empty($serverName)
328 );
329 }
330
331 // Handle browse button
332 if ($id == $this->wbBtnDocRoot[WinBinder::CTRL_ID]) {
333 $documentRoot = $bearsamppWinbinder->sysDlgPath(
334 $window,
336 $documentRoot
337 );
338 if ($documentRoot && is_dir($documentRoot)) {
339 $bearsamppWinbinder->setText($this->wbInputDocRoot[WinBinder::CTRL_OBJ], $documentRoot . '\\');
340 $bearsamppWinbinder->setText(
341 $this->wbLabelExp[WinBinder::CTRL_OBJ],
342 sprintf($bearsamppLang->getValue(Lang::VHOST_EXP_LABEL), $serverName, $documentRoot . '\\')
343 );
344 }
345 }
346 }
347}
348
global $bearsamppBins
global $bearsamppLang
global $bearsamppRoot
createFormFields($bearsamppWinbinder)
handleCustomEvent($window, $id, $ctrl, $param1, $param2)
getFormValues($bearsamppWinbinder)
const VHOST_EXP_LABEL
const VHOST_SERVER_NAME_LABEL
const VHOST_CREATED_ERROR
const BUTTON_BROWSE
const VHOST_CREATED
const VHOST_NOT_VALID_DOMAIN
const VHOST_DOCUMENT_ROOT_LABEL
const ADD_VHOST_TITLE
const VHOST_ALREADY_EXISTS
const VHOST_DOC_ROOT_PATH
static getWwwPath($aetrayPath=false)
static formatWindowsPath($path)
static getVhostsPath($aetrayPath=false)
static isValidDomainName($domainName)