Bearsampp
2026.7.11
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
{
17
private
$wbLabelServerName
;
18
private
$wbInputServerName
;
19
private
$wbLabelDocRoot
;
20
private
$wbInputDocRoot
;
21
private
$wbBtnDocRoot
;
22
private
$wbLabelExp
;
23
24
const
GAUGE_SAVE
= 3;
// Override: needs extra step for SSL cert regeneration
25
26
protected
function
getGaugeSave
()
27
{
28
return
self::GAUGE_SAVE;
29
}
30
31
protected
function
getWindowTitle
()
32
{
33
global
$bearsamppLang
;
34
return
sprintf(
$bearsamppLang
->getValue(
Lang::EDIT_VHOST_TITLE
), $this->initValue);
35
}
36
37
protected
function
getDialogTitle
()
38
{
39
global
$bearsamppLang
;
40
return
sprintf(
$bearsamppLang
->getValue(
Lang::EDIT_VHOST_TITLE
), $this->initValue);
41
}
42
43
protected
function
getDeleteDialogTitle
()
44
{
45
global
$bearsamppLang
;
46
return
$bearsamppLang
->getValue(
Lang::DELETE_VHOST_TITLE
);
47
}
48
49
protected
function
initializeDialog
($args)
50
{
51
global
$bearsamppRoot
;
52
53
if
(!isset($args[0]) || empty($args[0])) {
54
return
false
;
55
}
56
57
$filePath =
Path::getVhostsPath
() .
'/'
. $args[0] .
'.conf'
;
58
if
(!file_exists($filePath)) {
59
return
false
;
60
}
61
62
$fileContent = file_get_contents($filePath);
63
if
(!preg_match(
'/ServerName\s+(.*)/'
, $fileContent, $matchServerName) ||
64
!preg_match(
'/DocumentRoot\s+"(.*)"/'
, $fileContent, $matchDocumentRoot)) {
65
return
false
;
66
}
67
68
$this->initValue = trim($matchServerName[1]);
69
return
true
;
70
}
71
72
protected
function
createFormFields
($bearsamppWinbinder)
73
{
74
global
$bearsamppRoot
,
$bearsamppLang
;
75
76
// Load existing vhost data
77
$filePath =
Path::getVhostsPath
() .
'/'
. $this->initValue .
'.conf'
;
78
$fileContent = file_get_contents($filePath);
79
preg_match(
'/DocumentRoot\s+"(.*)"/'
, $fileContent, $matchDocumentRoot);
80
$initDocumentRoot =
Path::formatWindowsPath
(trim($matchDocumentRoot[1]));
81
82
$this->wbLabelServerName = $bearsamppWinbinder->createLabel(
83
$this->wbWindow,
84
$bearsamppLang
->getValue(
Lang::VHOST_SERVER_NAME_LABEL
) .
' :'
,
85
15, 15, 85,
null
, WBC_RIGHT
86
);
87
$this->wbInputServerName = $bearsamppWinbinder->createInputText(
88
$this->wbWindow,
89
$this->initValue,
90
105, 13, 150,
null
91
);
92
93
$this->wbLabelDocRoot = $bearsamppWinbinder->createLabel(
94
$this->wbWindow,
95
$bearsamppLang
->getValue(
Lang::VHOST_DOCUMENT_ROOT_LABEL
) .
' :'
,
96
15, 45, 85,
null
, WBC_RIGHT
97
);
98
$this->wbInputDocRoot = $bearsamppWinbinder->createInputText(
99
$this->wbWindow,
100
$initDocumentRoot,
101
105, 43, 190,
null
,
null
, WBC_READONLY
102
);
103
$this->wbBtnDocRoot = $bearsamppWinbinder->createButton(
104
$this->wbWindow,
105
$bearsamppLang
->getValue(
Lang::BUTTON_BROWSE
),
106
300, 43, 110
107
);
108
109
$this->wbLabelExp = $bearsamppWinbinder->createLabel(
110
$this->wbWindow,
111
sprintf(
$bearsamppLang
->getValue(
Lang::VHOST_EXP_LABEL
), $this->initValue, $initDocumentRoot),
112
15, 80, 470, 50
113
);
114
}
115
116
protected
function
getFormValues
($bearsamppWinbinder)
117
{
118
return
[
119
'serverName'
=> $bearsamppWinbinder->getText($this->wbInputServerName[
WinBinder::CTRL_OBJ
]),
120
'documentRoot'
=> $bearsamppWinbinder->getText($this->wbInputDocRoot[
WinBinder::CTRL_OBJ
])
121
];
122
}
123
124
protected
function
validateInput
($values)
125
{
126
global
$bearsamppLang
;
127
128
if
(!
Util::isValidDomainName
($values[
'serverName'
])) {
129
return
[
130
'valid'
=>
false
,
131
'error'
=> sprintf(
$bearsamppLang
->getValue(
Lang::VHOST_NOT_VALID_DOMAIN
), $values[
'serverName'
])
132
];
133
}
134
135
return
[
'valid'
=>
true
];
136
}
137
138
protected
function
itemExists
($values)
139
{
140
global
$bearsamppRoot
,
$bearsamppLang
, $bearsamppWinbinder;
141
142
// Only check if name changed
143
if
($values[
'serverName'
] != $this->initValue && is_file(
Path::getVhostsPath
() .
'/'
. $values[
'serverName'
] .
'.conf'
)) {
144
$bearsamppWinbinder->messageBoxError(
145
sprintf(
$bearsamppLang
->getValue(
Lang::VHOST_ALREADY_EXISTS
), $values[
'serverName'
]),
146
$this->getDialogTitle()
147
);
148
return
true
;
149
}
150
151
return
false
;
152
}
153
154
protected
function
saveItem
($values)
155
{
156
global
$bearsamppRoot
,
$bearsamppBins
, $bearsamppOpenSsl;
157
158
// Remove old vhost and certificate
159
$bearsamppOpenSsl->removeCrt($this->initValue);
160
@unlink(
Path::getVhostsPath
() .
'/'
. $this->initValue .
'.conf'
);
161
162
// Create new SSL certificate
163
if
(!$bearsamppOpenSsl->createCrt($values[
'serverName'
])) {
164
return
false
;
165
}
166
167
// Create new vhost configuration file
168
return
file_put_contents(
169
Path::getVhostsPath
() .
'/'
. $values[
'serverName'
] .
'.conf'
,
170
$bearsamppBins
->getApache()->getVhostContent($values[
'serverName'
], $values[
'documentRoot'
])
171
) !==
false
;
172
}
173
174
protected
function
deleteItem
()
175
{
176
global
$bearsamppRoot
, $bearsamppOpenSsl;
177
178
return
$bearsamppOpenSsl->removeCrt($this->initValue) &&
179
@unlink(
Path::getVhostsPath
() .
'/'
. $this->initValue .
'.conf'
);
180
}
181
182
protected
function
getSaveSuccessMessage
($values)
183
{
184
global
$bearsamppLang
;
185
return
sprintf(
186
$bearsamppLang
->getValue(
Lang::VHOST_CREATED
),
187
$values[
'serverName'
],
188
$values[
'serverName'
],
189
$values[
'documentRoot'
]
190
);
191
}
192
193
protected
function
getSaveErrorMessage
()
194
{
195
global
$bearsamppLang
;
196
return
$bearsamppLang
->getValue(
Lang::VHOST_CREATED_ERROR
);
197
}
198
199
protected
function
getDeleteConfirmMessage
()
200
{
201
global
$bearsamppLang
;
202
return
sprintf(
$bearsamppLang
->getValue(
Lang::DELETE_VHOST
), $this->initValue);
203
}
204
205
protected
function
getDeleteSuccessMessage
()
206
{
207
global
$bearsamppLang
;
208
return
sprintf(
$bearsamppLang
->getValue(
Lang::VHOST_REMOVED
), $this->initValue);
209
}
210
211
protected
function
getDeleteErrorMessage
()
212
{
213
global
$bearsamppRoot
,
$bearsamppLang
;
214
return
sprintf(
215
$bearsamppLang
->getValue(
Lang::VHOST_REMOVE_ERROR
),
216
Path::getVhostsPath
() .
'/'
. $this->initValue .
'.conf'
217
);
218
}
219
220
protected
function
restartService
()
221
{
222
global
$bearsamppBins
;
223
$bearsamppBins
->getApache()->getService()->restart();
224
}
225
226
protected
function
handleCustomEvent
($window, $id, $ctrl, $param1, $param2)
227
{
228
global
$bearsamppLang
, $bearsamppWinbinder;
229
230
$serverName = $bearsamppWinbinder->getText($this->wbInputServerName[
WinBinder::CTRL_OBJ
]);
231
$documentRoot = $bearsamppWinbinder->getText($this->wbInputDocRoot[
WinBinder::CTRL_OBJ
]);
232
233
// Handle server name input change
234
if
($id == $this->wbInputServerName[
WinBinder::CTRL_ID
]) {
235
$bearsamppWinbinder->setText(
236
$this->wbLabelExp[
WinBinder::CTRL_OBJ
],
237
sprintf(
$bearsamppLang
->getValue(
Lang::VHOST_EXP_LABEL
), $serverName, $documentRoot)
238
);
239
$bearsamppWinbinder->setEnabled(
240
$this->wbBtnSave[
WinBinder::CTRL_OBJ
],
241
!empty($serverName)
242
);
243
}
244
245
// Handle browse button
246
if
($id == $this->wbBtnDocRoot[
WinBinder::CTRL_ID
]) {
247
$documentRoot = $bearsamppWinbinder->sysDlgPath(
248
$window,
249
$bearsamppLang
->getValue(
Lang::VHOST_DOC_ROOT_PATH
),
250
$documentRoot
251
);
252
if
($documentRoot && is_dir($documentRoot)) {
253
$bearsamppWinbinder->setText($this->wbInputDocRoot[
WinBinder::CTRL_OBJ
], $documentRoot .
'\\'
);
254
$bearsamppWinbinder->setText(
255
$this->wbLabelExp[
WinBinder::CTRL_OBJ
],
256
sprintf(
$bearsamppLang
->getValue(
Lang::VHOST_EXP_LABEL
), $serverName, $documentRoot .
'\\'
)
257
);
258
}
259
}
260
}
261
}
262
$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:72
ActionEditVhost\$wbLabelExp
$wbLabelExp
Definition
class.action.editVhost.php:22
ActionEditVhost\getDeleteSuccessMessage
getDeleteSuccessMessage()
Definition
class.action.editVhost.php:205
ActionEditVhost\handleCustomEvent
handleCustomEvent($window, $id, $ctrl, $param1, $param2)
Definition
class.action.editVhost.php:226
ActionEditVhost\validateInput
validateInput($values)
Definition
class.action.editVhost.php:124
ActionEditVhost\getSaveSuccessMessage
getSaveSuccessMessage($values)
Definition
class.action.editVhost.php:182
ActionEditVhost\getDeleteDialogTitle
getDeleteDialogTitle()
Definition
class.action.editVhost.php:43
ActionEditVhost\getSaveErrorMessage
getSaveErrorMessage()
Definition
class.action.editVhost.php:193
ActionEditVhost\$wbInputServerName
$wbInputServerName
Definition
class.action.editVhost.php:18
ActionEditVhost\restartService
restartService()
Definition
class.action.editVhost.php:220
ActionEditVhost\getDeleteErrorMessage
getDeleteErrorMessage()
Definition
class.action.editVhost.php:211
ActionEditVhost\$wbLabelServerName
$wbLabelServerName
Definition
class.action.editVhost.php:17
ActionEditVhost\getDialogTitle
getDialogTitle()
Definition
class.action.editVhost.php:37
ActionEditVhost\saveItem
saveItem($values)
Definition
class.action.editVhost.php:154
ActionEditVhost\deleteItem
deleteItem()
Definition
class.action.editVhost.php:174
ActionEditVhost\$wbLabelDocRoot
$wbLabelDocRoot
Definition
class.action.editVhost.php:19
ActionEditVhost\$wbInputDocRoot
$wbInputDocRoot
Definition
class.action.editVhost.php:20
ActionEditVhost\getDeleteConfirmMessage
getDeleteConfirmMessage()
Definition
class.action.editVhost.php:199
ActionEditVhost\getWindowTitle
getWindowTitle()
Definition
class.action.editVhost.php:31
ActionEditVhost\getFormValues
getFormValues($bearsamppWinbinder)
Definition
class.action.editVhost.php:116
ActionEditVhost\GAUGE_SAVE
const GAUGE_SAVE
Definition
class.action.editVhost.php:24
ActionEditVhost\getGaugeSave
getGaugeSave()
Definition
class.action.editVhost.php:26
ActionEditVhost\$wbBtnDocRoot
$wbBtnDocRoot
Definition
class.action.editVhost.php:21
ActionEditVhost\initializeDialog
initializeDialog($args)
Definition
class.action.editVhost.php:49
ActionEditVhost\itemExists
itemExists($values)
Definition
class.action.editVhost.php:138
Lang\VHOST_EXP_LABEL
const VHOST_EXP_LABEL
Definition
class.lang.php:238
Lang\VHOST_REMOVE_ERROR
const VHOST_REMOVE_ERROR
Definition
class.lang.php:232
Lang\VHOST_SERVER_NAME_LABEL
const VHOST_SERVER_NAME_LABEL
Definition
class.lang.php:236
Lang\VHOST_CREATED_ERROR
const VHOST_CREATED_ERROR
Definition
class.lang.php:243
Lang\BUTTON_BROWSE
const BUTTON_BROWSE
Definition
class.lang.php:373
Lang\VHOST_CREATED
const VHOST_CREATED
Definition
class.lang.php:242
Lang\VHOST_NOT_VALID_DOMAIN
const VHOST_NOT_VALID_DOMAIN
Definition
class.lang.php:240
Lang\DELETE_VHOST_TITLE
const DELETE_VHOST_TITLE
Definition
class.lang.php:229
Lang\VHOST_DOCUMENT_ROOT_LABEL
const VHOST_DOCUMENT_ROOT_LABEL
Definition
class.lang.php:237
Lang\DELETE_VHOST
const DELETE_VHOST
Definition
class.lang.php:230
Lang\VHOST_ALREADY_EXISTS
const VHOST_ALREADY_EXISTS
Definition
class.lang.php:241
Lang\VHOST_DOC_ROOT_PATH
const VHOST_DOC_ROOT_PATH
Definition
class.lang.php:239
Lang\VHOST_REMOVED
const VHOST_REMOVED
Definition
class.lang.php:231
Lang\EDIT_VHOST_TITLE
const EDIT_VHOST_TITLE
Definition
class.lang.php:244
Path\formatWindowsPath
static formatWindowsPath($path)
Definition
class.path.php:118
Path\getVhostsPath
static getVhostsPath($aetrayPath=false)
Definition
class.path.php:994
Util\isValidDomainName
static isValidDomainName($domainName)
Definition
class.util.php:1233
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