Bearsampp 2026.3.26
API documentation
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
15{
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 {
44
45 $initServerName = 'test.local';
46 $initDocumentRoot = Util::formatWindowsPath($bearsamppRoot->getWwwPath()) . '\\' . $initServerName;
47
48 $this->wbLabelServerName = $bearsamppWinbinder->createLabel(
49 $this->wbWindow,
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,
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,
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($bearsamppRoot->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 $bearsamppRoot->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(
146 $values['serverName'],
147 $values['serverName'],
148 $values['documentRoot']
149 );
150 }
151
152 protected function getSaveErrorMessage()
153 {
154 global $bearsamppLang;
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,
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}
global $bearsamppBins
global $bearsamppLang
global $bearsamppRoot
createFormFields($bearsamppWinbinder)
handleCustomEvent($window, $id, $ctrl, $param1, $param2)
getFormValues($bearsamppWinbinder)
const VHOST_EXP_LABEL
const VHOST_SERVER_NAME_LABEL
const VHOST_CREATED_ERROR
const BUTTON_BROWSE
const VHOST_CREATED
const VHOST_NOT_VALID_DOMAIN
const VHOST_DOCUMENT_ROOT_LABEL
const ADD_VHOST_TITLE
const VHOST_ALREADY_EXISTS
const VHOST_DOC_ROOT_PATH
static isValidDomainName($domainName)
static formatWindowsPath($path)