Bearsampp
2026.9.23
Toggle main menu visibility
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
14
class
ActionAddVhost
extends
ActionDialogBase
15
{
17
private
$wbLabelServerName
;
19
private
$wbInputServerName
;
21
private
$wbLabelDocRoot
;
23
private
$wbInputDocRoot
;
25
private
$wbBtnDocRoot
;
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
{
73
global
$bearsamppRoot
,
$bearsamppLang
;
74
75
$initServerName =
'test.local'
;
76
$initDocumentRoot =
Path::formatWindowsPath
(
Path::getWwwPath
()) .
'\\'
. $initServerName;
77
78
$this->wbLabelServerName = $bearsamppWinbinder->createLabel(
79
$this->wbWindow,
80
$bearsamppLang
->getValue(
Lang::VHOST_SERVER_NAME_LABEL
) .
' :'
,
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,
98
$bearsamppLang
->getValue(
Lang::VHOST_DOCUMENT_ROOT_LABEL
) .
' :'
,
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,
117
$bearsamppLang
->getValue(
Lang::BUTTON_BROWSE
),
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(
238
$bearsamppLang
->getValue(
Lang::VHOST_CREATED
),
239
$values[
'serverName'
],
240
$values[
'serverName'
],
241
$values[
'documentRoot'
]
242
);
243
}
244
250
protected
function
getSaveErrorMessage
()
251
{
252
global
$bearsamppLang
;
253
254
return
$bearsamppLang
->getValue(
Lang::VHOST_CREATED_ERROR
);
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,
335
$bearsamppLang
->getValue(
Lang::VHOST_DOC_ROOT_PATH
),
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
$bearsamppBins
global $bearsamppBins
Definition
ajax.apache.php:16
$bearsamppLang
global $bearsamppLang
Definition
ajax.apache.php:16
$bearsamppRoot
global $bearsamppRoot
Definition
ajax.apache.php:16
ActionAddVhost
Definition
class.action.addVhost.php:15
ActionAddVhost\createFormFields
createFormFields($bearsamppWinbinder)
Definition
class.action.addVhost.php:71
ActionAddVhost\$wbLabelExp
$wbLabelExp
Definition
class.action.addVhost.php:27
ActionAddVhost\getDeleteSuccessMessage
getDeleteSuccessMessage()
Definition
class.action.addVhost.php:273
ActionAddVhost\handleCustomEvent
handleCustomEvent($window, $id, $ctrl, $param1, $param2)
Definition
class.action.addVhost.php:312
ActionAddVhost\validateInput
validateInput($values)
Definition
class.action.addVhost.php:155
ActionAddVhost\getSaveSuccessMessage
getSaveSuccessMessage($values)
Definition
class.action.addVhost.php:233
ActionAddVhost\getDeleteDialogTitle
getDeleteDialogTitle()
Definition
class.action.addVhost.php:58
ActionAddVhost\getSaveErrorMessage
getSaveErrorMessage()
Definition
class.action.addVhost.php:250
ActionAddVhost\$wbInputServerName
$wbInputServerName
Definition
class.action.addVhost.php:19
ActionAddVhost\restartService
restartService()
Definition
class.action.addVhost.php:295
ActionAddVhost\getDeleteErrorMessage
getDeleteErrorMessage()
Definition
class.action.addVhost.php:284
ActionAddVhost\$wbLabelServerName
$wbLabelServerName
Definition
class.action.addVhost.php:17
ActionAddVhost\getDialogTitle
getDialogTitle()
Definition
class.action.addVhost.php:46
ActionAddVhost\saveItem
saveItem($values)
Definition
class.action.addVhost.php:199
ActionAddVhost\deleteItem
deleteItem()
Definition
class.action.addVhost.php:220
ActionAddVhost\$wbLabelDocRoot
$wbLabelDocRoot
Definition
class.action.addVhost.php:21
ActionAddVhost\$wbInputDocRoot
$wbInputDocRoot
Definition
class.action.addVhost.php:23
ActionAddVhost\getDeleteConfirmMessage
getDeleteConfirmMessage()
Definition
class.action.addVhost.php:262
ActionAddVhost\getWindowTitle
getWindowTitle()
Definition
class.action.addVhost.php:34
ActionAddVhost\getFormValues
getFormValues($bearsamppWinbinder)
Definition
class.action.addVhost.php:140
ActionAddVhost\$wbBtnDocRoot
$wbBtnDocRoot
Definition
class.action.addVhost.php:25
ActionAddVhost\itemExists
itemExists($values)
Definition
class.action.addVhost.php:176
ActionDialogBase
Definition
class.action.dialogBase.php:18
Lang\VHOST_EXP_LABEL
const VHOST_EXP_LABEL
Definition
class.lang.php:239
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\VHOST_DOCUMENT_ROOT_LABEL
const VHOST_DOCUMENT_ROOT_LABEL
Definition
class.lang.php:238
Lang\ADD_VHOST_TITLE
const ADD_VHOST_TITLE
Definition
class.lang.php:236
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
Path\getWwwPath
static getWwwPath($aetrayPath=false)
Definition
class.path.php:1254
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.addVhost.php
Generated by
1.17.0