Bearsampp 2025.8.29
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
16{
18 private $wbWindow;
24 private $wbLabelExp;
26 private $wbBtnSave;
27 private $wbBtnDelete;
28 private $wbBtnCancel;
29
30 const GAUGE_SAVE = 3;
31 const GAUGE_DELETE = 2;
32
39 public function __construct($args)
40 {
41 global $bearsamppRoot, $bearsamppLang, $bearsamppWinbinder;
42
43 if (isset($args[0]) && !empty($args[0])) {
44 $filePath = $bearsamppRoot->getVhostsPath() . '/' . $args[0] . '.conf';
45 $fileContent = file_get_contents($filePath);
46 if (preg_match('/ServerName\s+(.*)/', $fileContent, $matchServerName) && preg_match('/DocumentRoot\s+"(.*)"/', $fileContent, $matchDocumentRoot)) {
47 $this->initServerName = trim($matchServerName[1]);
48 $initDocumentRoot = Util::formatWindowsPath(trim($matchDocumentRoot[1]));
49
50 $bearsamppWinbinder->reset();
51 $this->wbWindow = $bearsamppWinbinder->createAppWindow(sprintf($bearsamppLang->getValue(Lang::EDIT_VHOST_TITLE), $this->initServerName), 490, 200, WBC_NOTIFY, WBC_KEYDOWN | WBC_KEYUP);
52
53 $this->wbLabelServerName = $bearsamppWinbinder->createLabel($this->wbWindow, $bearsamppLang->getValue(Lang::VHOST_SERVER_NAME_LABEL) . ' :', 15, 15, 85, null, WBC_RIGHT);
54 $this->wbInputServerName = $bearsamppWinbinder->createInputText($this->wbWindow, $this->initServerName, 105, 13, 150, null);
55
56 $this->wbLabelDocRoot = $bearsamppWinbinder->createLabel($this->wbWindow, $bearsamppLang->getValue(Lang::VHOST_DOCUMENT_ROOT_LABEL) . ' :', 15, 45, 85, null, WBC_RIGHT);
57 $this->wbInputDocRoot = $bearsamppWinbinder->createInputText($this->wbWindow, $initDocumentRoot, 105, 43, 190, null, null, WBC_READONLY);
58 $this->wbBtnDocRoot = $bearsamppWinbinder->createButton($this->wbWindow, $bearsamppLang->getValue(Lang::BUTTON_BROWSE), 300, 43, 110);
59
60 $this->wbLabelExp = $bearsamppWinbinder->createLabel($this->wbWindow, sprintf($bearsamppLang->getValue(Lang::VHOST_EXP_LABEL), $this->initServerName, $initDocumentRoot), 15, 80, 470, 50);
61
62 $this->wbProgressBar = $bearsamppWinbinder->createProgressBar($this->wbWindow, self::GAUGE_SAVE + 1, 15, 137, 190);
63 $this->wbBtnSave = $bearsamppWinbinder->createButton($this->wbWindow, $bearsamppLang->getValue(Lang::BUTTON_SAVE), 215, 132);
64 $this->wbBtnDelete = $bearsamppWinbinder->createButton($this->wbWindow, $bearsamppLang->getValue(Lang::BUTTON_DELETE), 300, 132);
65 $this->wbBtnCancel = $bearsamppWinbinder->createButton($this->wbWindow, $bearsamppLang->getValue(Lang::BUTTON_CANCEL), 385, 132);
66
67 $bearsamppWinbinder->setHandler($this->wbWindow, $this, 'processWindow');
68 $bearsamppWinbinder->mainLoop();
69 $bearsamppWinbinder->reset();
70 }
71 }
72 }
73
83 public function processWindow($window, $id, $ctrl, $param1, $param2)
84 {
85 global $bearsamppRoot, $bearsamppBins, $bearsamppLang, $bearsamppOpenSsl, $bearsamppWinbinder;
86
87 $serverName = $bearsamppWinbinder->getText($this->wbInputServerName[WinBinder::CTRL_OBJ]);
88 $documentRoot = $bearsamppWinbinder->getText($this->wbInputDocRoot[WinBinder::CTRL_OBJ]);
89
90 switch ($id) {
91 case $this->wbInputServerName[WinBinder::CTRL_ID]:
92 $bearsamppWinbinder->setText(
93 $this->wbLabelExp[WinBinder::CTRL_OBJ],
94 sprintf($bearsamppLang->getValue(Lang::VHOST_EXP_LABEL), $serverName, $documentRoot)
95 );
96 $bearsamppWinbinder->setEnabled($this->wbBtnSave[WinBinder::CTRL_OBJ], empty($serverName) ? false : true);
97 break;
98 case $this->wbBtnDocRoot[WinBinder::CTRL_ID]:
99 $documentRoot = $bearsamppWinbinder->sysDlgPath($window, $bearsamppLang->getValue(Lang::VHOST_DOC_ROOT_PATH), $documentRoot);
100 if ($documentRoot && is_dir($documentRoot)) {
101 $bearsamppWinbinder->setText($this->wbInputDocRoot[WinBinder::CTRL_OBJ], $documentRoot . '\\');
102 $bearsamppWinbinder->setText(
103 $this->wbLabelExp[WinBinder::CTRL_OBJ],
104 sprintf($bearsamppLang->getValue(Lang::VHOST_EXP_LABEL), $serverName, $documentRoot . '\\')
105 );
106 // Reload to add host
108 }
109 break;
110 case $this->wbBtnSave[WinBinder::CTRL_ID]:
111 $bearsamppWinbinder->setProgressBarMax($this->wbProgressBar, self::GAUGE_SAVE + 1);
112 $bearsamppWinbinder->incrProgressBar($this->wbProgressBar);
113
114 if (!Util::isValidDomainName($serverName)) {
115 $bearsamppWinbinder->messageBoxError(
116 sprintf($bearsamppLang->getValue(Lang::VHOST_NOT_VALID_DOMAIN), $serverName),
118 $bearsamppWinbinder->resetProgressBar($this->wbProgressBar);
119 break;
120 }
121
122 if ($serverName != $this->initServerName && is_file($bearsamppRoot->getVhostsPath() . '/' . $serverName . '.conf')) {
123 $bearsamppWinbinder->messageBoxError(
124 sprintf($bearsamppLang->getValue(Lang::VHOST_ALREADY_EXISTS), $serverName),
125 sprintf($bearsamppLang->getValue(Lang::EDIT_VHOST_TITLE), $this->initServerName));
126 $bearsamppWinbinder->resetProgressBar($this->wbProgressBar);
127 break;
128 }
129
130 // Remove old vhost
131 $bearsamppOpenSsl->removeCrt($this->initServerName);
132 @unlink($bearsamppRoot->getVhostsPath() . '/' . $this->initServerName . '.conf');
133
134 if ($bearsamppOpenSsl->createCrt($serverName) && file_put_contents($bearsamppRoot->getVhostsPath() . '/' . $serverName . '.conf', $bearsamppBins->getApache()->getVhostContent($serverName, $documentRoot)) !== false) {
135 $bearsamppWinbinder->incrProgressBar($this->wbProgressBar);
136
137 $bearsamppBins->getApache()->getService()->restart();
138 $bearsamppWinbinder->incrProgressBar($this->wbProgressBar);
139
140 $bearsamppWinbinder->messageBoxInfo(
141 sprintf($bearsamppLang->getValue(Lang::VHOST_CREATED), $serverName, $serverName, $documentRoot),
142 sprintf($bearsamppLang->getValue(Lang::EDIT_VHOST_TITLE), $this->initServerName));
143 $bearsamppWinbinder->destroyWindow($window);
144 } else {
145 $bearsamppWinbinder->messageBoxError(
147 sprintf($bearsamppLang->getValue(Lang::EDIT_VHOST_TITLE), $this->initServerName));
148 $bearsamppWinbinder->resetProgressBar($this->wbProgressBar);
149 }
150 // Reload to remove host
152
153 break;
154 case $this->wbBtnDelete[WinBinder::CTRL_ID]:
155 $bearsamppWinbinder->setProgressBarMax($this->wbProgressBar, self::GAUGE_DELETE + 1);
156
157 $boxTitle = $bearsamppLang->getValue(Lang::DELETE_VHOST_TITLE);
158 $confirm = $bearsamppWinbinder->messageBoxYesNo(
159 sprintf($bearsamppLang->getValue(Lang::DELETE_VHOST), $this->initServerName),
160 $boxTitle);
161
162 $bearsamppWinbinder->incrProgressBar($this->wbProgressBar);
163
164 if ($confirm) {
165 if ($bearsamppOpenSsl->removeCrt($this->initServerName) && @unlink($bearsamppRoot->getVhostsPath() . '/' . $this->initServerName . '.conf')) {
166 $bearsamppWinbinder->incrProgressBar($this->wbProgressBar);
167
168 $bearsamppBins->getApache()->getService()->restart();
169 $bearsamppWinbinder->incrProgressBar($this->wbProgressBar);
170
171 $bearsamppWinbinder->messageBoxInfo(
172 sprintf($bearsamppLang->getValue(Lang::VHOST_REMOVED), $this->initServerName),
173 $boxTitle);
174 $bearsamppWinbinder->destroyWindow($window);
175 } else {
176 $bearsamppWinbinder->messageBoxError(
177 sprintf($bearsamppLang->getValue(Lang::VHOST_REMOVE_ERROR), $bearsamppRoot->getVhostsPath() . '/' . $this->initServerName . '.conf'),
178 $boxTitle);
179 $bearsamppWinbinder->resetProgressBar($this->wbProgressBar);
180 }
181 }
182 break;
183 case IDCLOSE:
184 case $this->wbBtnCancel[WinBinder::CTRL_ID]:
185 $bearsamppWinbinder->destroyWindow($window);
186 break;
187 }
188 }
189}
global $bearsamppBins
global $bearsamppLang
global $bearsamppRoot
processWindow($window, $id, $ctrl, $param1, $param2)
const VHOST_EXP_LABEL
const VHOST_REMOVE_ERROR
const VHOST_SERVER_NAME_LABEL
const VHOST_CREATED_ERROR
const BUTTON_BROWSE
const VHOST_CREATED
const BUTTON_CANCEL
const VHOST_NOT_VALID_DOMAIN
const DELETE_VHOST_TITLE
const BUTTON_DELETE
const VHOST_DOCUMENT_ROOT_LABEL
const DELETE_VHOST
const BUTTON_SAVE
const ADD_VHOST_TITLE
const VHOST_ALREADY_EXISTS
const VHOST_DOC_ROOT_PATH
const VHOST_REMOVED
const EDIT_VHOST_TITLE
static getActionRestart($sName)
static isValidDomainName($domainName)
static formatWindowsPath($path)