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