Bearsampp
2026.9.23
Toggle main menu visibility
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
15
class
ActionEditVhost
extends
ActionDialogBase
16
{
18
private
$wbLabelServerName
;
20
private
$wbInputServerName
;
22
private
$wbLabelDocRoot
;
24
private
$wbInputDocRoot
;
26
private
$wbBtnDocRoot
;
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
76
return
$bearsamppLang
->getValue(
Lang::DELETE_VHOST_TITLE
);
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
{
119
global
$bearsamppRoot
,
$bearsamppLang
;
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,
129
$bearsamppLang
->getValue(
Lang::VHOST_SERVER_NAME_LABEL
) .
' :'
,
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,
147
$bearsamppLang
->getValue(
Lang::VHOST_DOCUMENT_ROOT_LABEL
) .
' :'
,
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,
166
$bearsamppLang
->getValue(
Lang::BUTTON_BROWSE
),
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(
294
$bearsamppLang
->getValue(
Lang::VHOST_CREATED
),
295
$values[
'serverName'
],
296
$values[
'serverName'
],
297
$values[
'documentRoot'
]
298
);
299
}
300
306
protected
function
getSaveErrorMessage
()
307
{
308
global
$bearsamppLang
;
309
310
return
$bearsamppLang
->getValue(
Lang::VHOST_CREATED_ERROR
);
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
{
344
global
$bearsamppRoot
,
$bearsamppLang
;
345
346
return
sprintf(
347
$bearsamppLang
->getValue(
Lang::VHOST_REMOVE_ERROR
),
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,
397
$bearsamppLang
->getValue(
Lang::VHOST_DOC_ROOT_PATH
),
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
$bearsamppBins
global $bearsamppBins
Definition
ajax.apache.php:16
$bearsamppLang
global $bearsamppLang
Definition
ajax.apache.php:16
$bearsamppRoot
global $bearsamppRoot
Definition
ajax.apache.php:16
ActionDialogBase
Definition
class.action.dialogBase.php:18
ActionEditVhost
Definition
class.action.editVhost.php:16
ActionEditVhost\createFormFields
createFormFields($bearsamppWinbinder)
Definition
class.action.editVhost.php:117
ActionEditVhost\$wbLabelExp
$wbLabelExp
Definition
class.action.editVhost.php:28
ActionEditVhost\getDeleteSuccessMessage
getDeleteSuccessMessage()
Definition
class.action.editVhost.php:330
ActionEditVhost\handleCustomEvent
handleCustomEvent($window, $id, $ctrl, $param1, $param2)
Definition
class.action.editVhost.php:374
ActionEditVhost\validateInput
validateInput($values)
Definition
class.action.editVhost.php:204
ActionEditVhost\getSaveSuccessMessage
getSaveSuccessMessage($values)
Definition
class.action.editVhost.php:289
ActionEditVhost\getDeleteDialogTitle
getDeleteDialogTitle()
Definition
class.action.editVhost.php:72
ActionEditVhost\getSaveErrorMessage
getSaveErrorMessage()
Definition
class.action.editVhost.php:306
ActionEditVhost\$wbInputServerName
$wbInputServerName
Definition
class.action.editVhost.php:20
ActionEditVhost\restartService
restartService()
Definition
class.action.editVhost.php:357
ActionEditVhost\getDeleteErrorMessage
getDeleteErrorMessage()
Definition
class.action.editVhost.php:342
ActionEditVhost\$wbLabelServerName
$wbLabelServerName
Definition
class.action.editVhost.php:18
ActionEditVhost\getDialogTitle
getDialogTitle()
Definition
class.action.editVhost.php:60
ActionEditVhost\saveItem
saveItem($values)
Definition
class.action.editVhost.php:249
ActionEditVhost\deleteItem
deleteItem()
Definition
class.action.editVhost.php:274
ActionEditVhost\$wbLabelDocRoot
$wbLabelDocRoot
Definition
class.action.editVhost.php:22
ActionEditVhost\$wbInputDocRoot
$wbInputDocRoot
Definition
class.action.editVhost.php:24
ActionEditVhost\getDeleteConfirmMessage
getDeleteConfirmMessage()
Definition
class.action.editVhost.php:318
ActionEditVhost\getWindowTitle
getWindowTitle()
Definition
class.action.editVhost.php:48
ActionEditVhost\getFormValues
getFormValues($bearsamppWinbinder)
Definition
class.action.editVhost.php:189
ActionEditVhost\GAUGE_SAVE
const GAUGE_SAVE
Definition
class.action.editVhost.php:31
ActionEditVhost\getGaugeSave
getGaugeSave()
Definition
class.action.editVhost.php:38
ActionEditVhost\$wbBtnDocRoot
$wbBtnDocRoot
Definition
class.action.editVhost.php:26
ActionEditVhost\initializeDialog
initializeDialog($args)
Definition
class.action.editVhost.php:86
ActionEditVhost\itemExists
itemExists($values)
Definition
class.action.editVhost.php:225
Lang\VHOST_EXP_LABEL
const VHOST_EXP_LABEL
Definition
class.lang.php:239
Lang\VHOST_REMOVE_ERROR
const VHOST_REMOVE_ERROR
Definition
class.lang.php:233
Lang\VHOST_SERVER_NAME_LABEL
const VHOST_SERVER_NAME_LABEL
Definition
class.lang.php:237
Lang\VHOST_CREATED_ERROR
const VHOST_CREATED_ERROR
Definition
class.lang.php:244
Lang\BUTTON_BROWSE
const BUTTON_BROWSE
Definition
class.lang.php:380
Lang\VHOST_CREATED
const VHOST_CREATED
Definition
class.lang.php:243
Lang\VHOST_NOT_VALID_DOMAIN
const VHOST_NOT_VALID_DOMAIN
Definition
class.lang.php:241
Lang\DELETE_VHOST_TITLE
const DELETE_VHOST_TITLE
Definition
class.lang.php:230
Lang\VHOST_DOCUMENT_ROOT_LABEL
const VHOST_DOCUMENT_ROOT_LABEL
Definition
class.lang.php:238
Lang\DELETE_VHOST
const DELETE_VHOST
Definition
class.lang.php:231
Lang\VHOST_ALREADY_EXISTS
const VHOST_ALREADY_EXISTS
Definition
class.lang.php:242
Lang\VHOST_DOC_ROOT_PATH
const VHOST_DOC_ROOT_PATH
Definition
class.lang.php:240
Lang\VHOST_REMOVED
const VHOST_REMOVED
Definition
class.lang.php:232
Lang\EDIT_VHOST_TITLE
const EDIT_VHOST_TITLE
Definition
class.lang.php:245
Path\formatWindowsPath
static formatWindowsPath($path)
Definition
class.path.php:127
Path\getVhostsPath
static getVhostsPath($aetrayPath=false)
Definition
class.path.php:1168
Util\isValidDomainName
static isValidDomainName($domainName)
Definition
class.util.php:986
WinBinder\CTRL_ID
const CTRL_ID
Definition
class.winbinder.php:21
WinBinder\CTRL_OBJ
const CTRL_OBJ
Definition
class.winbinder.php:22
sandbox
core
classes
actions
class.action.editVhost.php
Generated by
1.17.0