Bearsampp
2026.7.11
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-2024 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
{
16
private
$wbLabelServerName
;
17
private
$wbInputServerName
;
18
private
$wbLabelDocRoot
;
19
private
$wbInputDocRoot
;
20
private
$wbBtnDocRoot
;
21
private
$wbLabelExp
;
22
23
protected
function
getWindowTitle
()
24
{
25
global
$bearsamppLang
;
26
return
$bearsamppLang
->getValue(
Lang::ADD_VHOST_TITLE
);
27
}
28
29
protected
function
getDialogTitle
()
30
{
31
global
$bearsamppLang
;
32
return
$bearsamppLang
->getValue(
Lang::ADD_VHOST_TITLE
);
33
}
34
35
protected
function
getDeleteDialogTitle
()
36
{
37
// Not used in add mode
38
return
''
;
39
}
40
41
protected
function
createFormFields
($bearsamppWinbinder)
42
{
43
global
$bearsamppRoot
,
$bearsamppLang
;
44
45
$initServerName =
'test.local'
;
46
$initDocumentRoot =
Path::formatWindowsPath
(
Path::getWwwPath
()) .
'\\'
. $initServerName;
47
48
$this->wbLabelServerName = $bearsamppWinbinder->createLabel(
49
$this->wbWindow,
50
$bearsamppLang
->getValue(
Lang::VHOST_SERVER_NAME_LABEL
) .
' :'
,
51
15, 15, 85,
null
, WBC_RIGHT
52
);
53
$this->wbInputServerName = $bearsamppWinbinder->createInputText(
54
$this->wbWindow,
55
$initServerName,
56
105, 13, 150,
null
57
);
58
59
$this->wbLabelDocRoot = $bearsamppWinbinder->createLabel(
60
$this->wbWindow,
61
$bearsamppLang
->getValue(
Lang::VHOST_DOCUMENT_ROOT_LABEL
) .
' :'
,
62
15, 45, 85,
null
, WBC_RIGHT
63
);
64
$this->wbInputDocRoot = $bearsamppWinbinder->createInputText(
65
$this->wbWindow,
66
$initDocumentRoot,
67
105, 43, 190,
null
,
null
, WBC_READONLY
68
);
69
$this->wbBtnDocRoot = $bearsamppWinbinder->createButton(
70
$this->wbWindow,
71
$bearsamppLang
->getValue(
Lang::BUTTON_BROWSE
),
72
300, 43, 110
73
);
74
75
$this->wbLabelExp = $bearsamppWinbinder->createLabel(
76
$this->wbWindow,
77
sprintf(
$bearsamppLang
->getValue(
Lang::VHOST_EXP_LABEL
), $initServerName, $initDocumentRoot),
78
15, 80, 470, 50
79
);
80
}
81
82
protected
function
getFormValues
($bearsamppWinbinder)
83
{
84
return
[
85
'serverName'
=> $bearsamppWinbinder->getText($this->wbInputServerName[
WinBinder::CTRL_OBJ
]),
86
'documentRoot'
=> $bearsamppWinbinder->getText($this->wbInputDocRoot[
WinBinder::CTRL_OBJ
])
87
];
88
}
89
90
protected
function
validateInput
($values)
91
{
92
global
$bearsamppLang
;
93
94
if
(!
Util::isValidDomainName
($values[
'serverName'
])) {
95
return
[
96
'valid'
=>
false
,
97
'error'
=> sprintf(
$bearsamppLang
->getValue(
Lang::VHOST_NOT_VALID_DOMAIN
), $values[
'serverName'
])
98
];
99
}
100
101
return
[
'valid'
=>
true
];
102
}
103
104
protected
function
itemExists
($values)
105
{
106
global
$bearsamppRoot
,
$bearsamppLang
, $bearsamppWinbinder;
107
108
if
(is_file(
Path::getVhostsPath
() .
'/'
. $values[
'serverName'
] .
'.conf'
)) {
109
$bearsamppWinbinder->messageBoxError(
110
sprintf(
$bearsamppLang
->getValue(
Lang::VHOST_ALREADY_EXISTS
), $values[
'serverName'
]),
111
$this->getDialogTitle()
112
);
113
return
true
;
114
}
115
116
return
false
;
117
}
118
119
protected
function
saveItem
($values)
120
{
121
global
$bearsamppRoot
,
$bearsamppBins
, $bearsamppOpenSsl;
122
123
// Create SSL certificate
124
if
(!$bearsamppOpenSsl->createCrt($values[
'serverName'
])) {
125
return
false
;
126
}
127
128
// Create vhost configuration file
129
return
file_put_contents(
130
Path::getVhostsPath
() .
'/'
. $values[
'serverName'
] .
'.conf'
,
131
$bearsamppBins
->getApache()->getVhostContent($values[
'serverName'
], $values[
'documentRoot'
])
132
) !==
false
;
133
}
134
135
protected
function
deleteItem
()
136
{
137
// Not used in add mode
138
return
false
;
139
}
140
141
protected
function
getSaveSuccessMessage
($values)
142
{
143
global
$bearsamppLang
;
144
return
sprintf(
145
$bearsamppLang
->getValue(
Lang::VHOST_CREATED
),
146
$values[
'serverName'
],
147
$values[
'serverName'
],
148
$values[
'documentRoot'
]
149
);
150
}
151
152
protected
function
getSaveErrorMessage
()
153
{
154
global
$bearsamppLang
;
155
return
$bearsamppLang
->getValue(
Lang::VHOST_CREATED_ERROR
);
156
}
157
158
protected
function
getDeleteConfirmMessage
()
159
{
160
// Not used in add mode
161
return
''
;
162
}
163
164
protected
function
getDeleteSuccessMessage
()
165
{
166
// Not used in add mode
167
return
''
;
168
}
169
170
protected
function
getDeleteErrorMessage
()
171
{
172
// Not used in add mode
173
return
''
;
174
}
175
176
protected
function
restartService
()
177
{
178
global
$bearsamppBins
;
179
$bearsamppBins
->getApache()->getService()->restart();
180
}
181
182
protected
function
handleCustomEvent
($window, $id, $ctrl, $param1, $param2)
183
{
184
global
$bearsamppLang
, $bearsamppWinbinder;
185
186
$serverName = $bearsamppWinbinder->getText($this->wbInputServerName[
WinBinder::CTRL_OBJ
]);
187
$documentRoot = $bearsamppWinbinder->getText($this->wbInputDocRoot[
WinBinder::CTRL_OBJ
]);
188
189
// Handle server name input change
190
if
($id == $this->wbInputServerName[
WinBinder::CTRL_ID
]) {
191
$bearsamppWinbinder->setText(
192
$this->wbLabelExp[
WinBinder::CTRL_OBJ
],
193
sprintf(
$bearsamppLang
->getValue(
Lang::VHOST_EXP_LABEL
), $serverName, $documentRoot)
194
);
195
$bearsamppWinbinder->setEnabled(
196
$this->wbBtnSave[
WinBinder::CTRL_OBJ
],
197
!empty($serverName)
198
);
199
}
200
201
// Handle browse button
202
if
($id == $this->wbBtnDocRoot[
WinBinder::CTRL_ID
]) {
203
$documentRoot = $bearsamppWinbinder->sysDlgPath(
204
$window,
205
$bearsamppLang
->getValue(
Lang::VHOST_DOC_ROOT_PATH
),
206
$documentRoot
207
);
208
if
($documentRoot && is_dir($documentRoot)) {
209
$bearsamppWinbinder->setText($this->wbInputDocRoot[
WinBinder::CTRL_OBJ
], $documentRoot .
'\\'
);
210
$bearsamppWinbinder->setText(
211
$this->wbLabelExp[
WinBinder::CTRL_OBJ
],
212
sprintf(
$bearsamppLang
->getValue(
Lang::VHOST_EXP_LABEL
), $serverName, $documentRoot .
'\\'
)
213
);
214
}
215
}
216
}
217
}
218
$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:41
ActionAddVhost\$wbLabelExp
$wbLabelExp
Definition
class.action.addVhost.php:21
ActionAddVhost\getDeleteSuccessMessage
getDeleteSuccessMessage()
Definition
class.action.addVhost.php:164
ActionAddVhost\handleCustomEvent
handleCustomEvent($window, $id, $ctrl, $param1, $param2)
Definition
class.action.addVhost.php:182
ActionAddVhost\validateInput
validateInput($values)
Definition
class.action.addVhost.php:90
ActionAddVhost\getSaveSuccessMessage
getSaveSuccessMessage($values)
Definition
class.action.addVhost.php:141
ActionAddVhost\getDeleteDialogTitle
getDeleteDialogTitle()
Definition
class.action.addVhost.php:35
ActionAddVhost\getSaveErrorMessage
getSaveErrorMessage()
Definition
class.action.addVhost.php:152
ActionAddVhost\$wbInputServerName
$wbInputServerName
Definition
class.action.addVhost.php:17
ActionAddVhost\restartService
restartService()
Definition
class.action.addVhost.php:176
ActionAddVhost\getDeleteErrorMessage
getDeleteErrorMessage()
Definition
class.action.addVhost.php:170
ActionAddVhost\$wbLabelServerName
$wbLabelServerName
Definition
class.action.addVhost.php:16
ActionAddVhost\getDialogTitle
getDialogTitle()
Definition
class.action.addVhost.php:29
ActionAddVhost\saveItem
saveItem($values)
Definition
class.action.addVhost.php:119
ActionAddVhost\deleteItem
deleteItem()
Definition
class.action.addVhost.php:135
ActionAddVhost\$wbLabelDocRoot
$wbLabelDocRoot
Definition
class.action.addVhost.php:18
ActionAddVhost\$wbInputDocRoot
$wbInputDocRoot
Definition
class.action.addVhost.php:19
ActionAddVhost\getDeleteConfirmMessage
getDeleteConfirmMessage()
Definition
class.action.addVhost.php:158
ActionAddVhost\getWindowTitle
getWindowTitle()
Definition
class.action.addVhost.php:23
ActionAddVhost\getFormValues
getFormValues($bearsamppWinbinder)
Definition
class.action.addVhost.php:82
ActionAddVhost\$wbBtnDocRoot
$wbBtnDocRoot
Definition
class.action.addVhost.php:20
ActionAddVhost\itemExists
itemExists($values)
Definition
class.action.addVhost.php:104
ActionDialogBase
Definition
class.action.dialogBase.php:18
Lang\VHOST_EXP_LABEL
const VHOST_EXP_LABEL
Definition
class.lang.php:238
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\VHOST_DOCUMENT_ROOT_LABEL
const VHOST_DOCUMENT_ROOT_LABEL
Definition
class.lang.php:237
Lang\ADD_VHOST_TITLE
const ADD_VHOST_TITLE
Definition
class.lang.php:235
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
Path\getWwwPath
static getWwwPath($aetrayPath=false)
Definition
class.path.php:1078
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.addVhost.php
Generated by
1.17.0