Bearsampp 2026.9.23
Loading...
Searching...
No Matches
class.action.editVhost.php
Go to the documentation of this file.
1<?php
2/*
3 *
4 * * Copyright (c) 2022-2025 Bearsampp
5 * * License: GNU General Public License version 3 or later; see LICENSE.txt
6 * * Website: https://bearsampp.com
7 * * Github: https://github.com/Bearsampp
8 *
9 */
10
16{
28 private $wbLabelExp;
29
31 const GAUGE_SAVE = 3;
32
38 protected function getGaugeSave()
39 {
40 return self::GAUGE_SAVE;
41 }
42
48 protected function getWindowTitle()
49 {
50 global $bearsamppLang;
51
52 return sprintf($bearsamppLang->getValue(Lang::EDIT_VHOST_TITLE), $this->initValue);
53 }
54
60 protected function getDialogTitle()
61 {
62 global $bearsamppLang;
63
64 return sprintf($bearsamppLang->getValue(Lang::EDIT_VHOST_TITLE), $this->initValue);
65 }
66
72 protected function getDeleteDialogTitle()
73 {
74 global $bearsamppLang;
75
77 }
78
86 protected function initializeDialog($args)
87 {
88 global $bearsamppRoot;
89
90 if (!isset($args[0]) || empty($args[0])) {
91 return false;
92 }
93
94 $filePath = Path::getVhostsPath() . '/' . $args[0] . '.conf';
95 if (!file_exists($filePath)) {
96 return false;
97 }
98
99 $fileContent = file_get_contents($filePath);
100 if (!preg_match('/ServerName\s+(.*)/', $fileContent, $matchServerName) ||
101 !preg_match('/DocumentRoot\s+"(.*)"/', $fileContent, $matchDocumentRoot)) {
102 return false;
103 }
104
105 $this->initValue = trim($matchServerName[1]);
106
107 return true;
108 }
109
117 protected function createFormFields($bearsamppWinbinder)
118 {
120
121 // Load existing vhost data
122 $filePath = Path::getVhostsPath() . '/' . $this->initValue . '.conf';
123 $fileContent = file_get_contents($filePath);
124 preg_match('/DocumentRoot\s+"(.*)"/', $fileContent, $matchDocumentRoot);
125 $initDocumentRoot = Path::formatWindowsPath(trim($matchDocumentRoot[1]));
126
127 $this->wbLabelServerName = $bearsamppWinbinder->createLabel(
128 $this->wbWindow,
130 15,
131 15,
132 85,
133 null,
134 WBC_RIGHT
135 );
136 $this->wbInputServerName = $bearsamppWinbinder->createInputText(
137 $this->wbWindow,
138 $this->initValue,
139 105,
140 13,
141 150,
142 null
143 );
144
145 $this->wbLabelDocRoot = $bearsamppWinbinder->createLabel(
146 $this->wbWindow,
148 15,
149 45,
150 85,
151 null,
152 WBC_RIGHT
153 );
154 $this->wbInputDocRoot = $bearsamppWinbinder->createInputText(
155 $this->wbWindow,
156 $initDocumentRoot,
157 105,
158 43,
159 190,
160 null,
161 null,
162 WBC_READONLY
163 );
164 $this->wbBtnDocRoot = $bearsamppWinbinder->createButton(
165 $this->wbWindow,
167 300,
168 43,
169 110
170 );
171
172 $this->wbLabelExp = $bearsamppWinbinder->createLabel(
173 $this->wbWindow,
174 sprintf($bearsamppLang->getValue(Lang::VHOST_EXP_LABEL), $this->initValue, $initDocumentRoot),
175 15,
176 80,
177 470,
178 50
179 );
180 }
181
189 protected function getFormValues($bearsamppWinbinder)
190 {
191 return [
192 'serverName' => $bearsamppWinbinder->getText($this->wbInputServerName[WinBinder::CTRL_OBJ]),
193 'documentRoot' => $bearsamppWinbinder->getText($this->wbInputDocRoot[WinBinder::CTRL_OBJ])
194 ];
195 }
196
204 protected function validateInput($values)
205 {
206 global $bearsamppLang;
207
208 if (!Util::isValidDomainName($values['serverName'])) {
209 return [
210 'valid' => false,
211 'error' => sprintf($bearsamppLang->getValue(Lang::VHOST_NOT_VALID_DOMAIN), $values['serverName'])
212 ];
213 }
214
215 return ['valid' => true];
216 }
217
225 protected function itemExists($values)
226 {
227 global $bearsamppRoot, $bearsamppLang, $bearsamppWinbinder;
228
229 // Only check if name changed
230 if ($values['serverName'] != $this->initValue && is_file(Path::getVhostsPath() . '/' . $values['serverName'] . '.conf')) {
231 $bearsamppWinbinder->messageBoxError(
232 sprintf($bearsamppLang->getValue(Lang::VHOST_ALREADY_EXISTS), $values['serverName']),
233 $this->getDialogTitle()
234 );
235
236 return true;
237 }
238
239 return false;
240 }
241
249 protected function saveItem($values)
250 {
251 global $bearsamppRoot, $bearsamppBins, $bearsamppOpenSsl;
252
253 // Remove old vhost and certificate
254 $bearsamppOpenSsl->removeCrt($this->initValue);
255 @unlink(Path::getVhostsPath() . '/' . $this->initValue . '.conf');
256
257 // Create new SSL certificate
258 if (!$bearsamppOpenSsl->createCrt($values['serverName'])) {
259 return false;
260 }
261
262 // Create new vhost configuration file
263 return file_put_contents(
264 Path::getVhostsPath() . '/' . $values['serverName'] . '.conf',
265 $bearsamppBins->getApache()->getVhostContent($values['serverName'], $values['documentRoot'])
266 ) !== false;
267 }
268
274 protected function deleteItem()
275 {
276 global $bearsamppRoot, $bearsamppOpenSsl;
277
278 return $bearsamppOpenSsl->removeCrt($this->initValue) &&
279 @unlink(Path::getVhostsPath() . '/' . $this->initValue . '.conf');
280 }
281
289 protected function getSaveSuccessMessage($values)
290 {
291 global $bearsamppLang;
292
293 return sprintf(
295 $values['serverName'],
296 $values['serverName'],
297 $values['documentRoot']
298 );
299 }
300
306 protected function getSaveErrorMessage()
307 {
308 global $bearsamppLang;
309
311 }
312
318 protected function getDeleteConfirmMessage()
319 {
320 global $bearsamppLang;
321
322 return sprintf($bearsamppLang->getValue(Lang::DELETE_VHOST), $this->initValue);
323 }
324
330 protected function getDeleteSuccessMessage()
331 {
332 global $bearsamppLang;
333
334 return sprintf($bearsamppLang->getValue(Lang::VHOST_REMOVED), $this->initValue);
335 }
336
342 protected function getDeleteErrorMessage()
343 {
345
346 return sprintf(
348 Path::getVhostsPath() . '/' . $this->initValue . '.conf'
349 );
350 }
351
357 protected function restartService()
358 {
359 global $bearsamppBins;
360 $bearsamppBins->getApache()->getService()->restart();
361 }
362
374 protected function handleCustomEvent($window, $id, $ctrl, $param1, $param2)
375 {
376 global $bearsamppLang, $bearsamppWinbinder;
377
378 $serverName = $bearsamppWinbinder->getText($this->wbInputServerName[WinBinder::CTRL_OBJ]);
379 $documentRoot = $bearsamppWinbinder->getText($this->wbInputDocRoot[WinBinder::CTRL_OBJ]);
380
381 // Handle server name input change
382 if ($id == $this->wbInputServerName[WinBinder::CTRL_ID]) {
383 $bearsamppWinbinder->setText(
384 $this->wbLabelExp[WinBinder::CTRL_OBJ],
385 sprintf($bearsamppLang->getValue(Lang::VHOST_EXP_LABEL), $serverName, $documentRoot)
386 );
387 $bearsamppWinbinder->setEnabled(
388 $this->wbBtnSave[WinBinder::CTRL_OBJ],
389 !empty($serverName)
390 );
391 }
392
393 // Handle browse button
394 if ($id == $this->wbBtnDocRoot[WinBinder::CTRL_ID]) {
395 $documentRoot = $bearsamppWinbinder->sysDlgPath(
396 $window,
398 $documentRoot
399 );
400 if ($documentRoot && is_dir($documentRoot)) {
401 $bearsamppWinbinder->setText($this->wbInputDocRoot[WinBinder::CTRL_OBJ], $documentRoot . '\\');
402 $bearsamppWinbinder->setText(
403 $this->wbLabelExp[WinBinder::CTRL_OBJ],
404 sprintf($bearsamppLang->getValue(Lang::VHOST_EXP_LABEL), $serverName, $documentRoot . '\\')
405 );
406 }
407 }
408 }
409}
410
global $bearsamppBins
global $bearsamppLang
global $bearsamppRoot
createFormFields($bearsamppWinbinder)
handleCustomEvent($window, $id, $ctrl, $param1, $param2)
getFormValues($bearsamppWinbinder)
const VHOST_EXP_LABEL
const VHOST_REMOVE_ERROR
const VHOST_SERVER_NAME_LABEL
const VHOST_CREATED_ERROR
const BUTTON_BROWSE
const VHOST_CREATED
const VHOST_NOT_VALID_DOMAIN
const DELETE_VHOST_TITLE
const VHOST_DOCUMENT_ROOT_LABEL
const DELETE_VHOST
const VHOST_ALREADY_EXISTS
const VHOST_DOC_ROOT_PATH
const VHOST_REMOVED
const EDIT_VHOST_TITLE
static formatWindowsPath($path)
static getVhostsPath($aetrayPath=false)
static isValidDomainName($domainName)