Bearsampp 2026.3.26
API documentation
Loading...
Searching...
No Matches
class.action.editAlias.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{
16 private $wbLabelName;
17 private $wbInputName;
18 private $wbLabelDest;
19 private $wbInputDest;
20 private $wbBtnDest;
21 private $wbLabelExp;
22
23 protected function getWindowTitle()
24 {
25 global $bearsamppLang;
26 return sprintf($bearsamppLang->getValue(Lang::EDIT_ALIAS_TITLE), $this->initValue);
27 }
28
29 protected function getDialogTitle()
30 {
31 global $bearsamppLang;
32 return $bearsamppLang->getValue(Lang::ADD_ALIAS_TITLE);
33 }
34
35 protected function getDeleteDialogTitle()
36 {
37 global $bearsamppLang;
39 }
40
41 protected function initializeDialog($args)
42 {
43 global $bearsamppRoot;
44
45 if (!isset($args[0]) || empty($args[0])) {
46 return false;
47 }
48
49 $filePath = $bearsamppRoot->getAliasPath() . '/' . $args[0] . '.conf';
50 if (!file_exists($filePath)) {
51 return false;
52 }
53
54 $fileContent = file_get_contents($filePath);
55 if (!preg_match('/^Alias \/' . $args[0] . ' "(.+)"/', $fileContent, $match)) {
56 return false;
57 }
58
59 $this->initValue = $args[0];
60 return true;
61 }
62
63 protected function createFormFields($bearsamppWinbinder)
64 {
66
67 // Load existing alias data
68 $filePath = $bearsamppRoot->getAliasPath() . '/' . $this->initValue . '.conf';
69 $fileContent = file_get_contents($filePath);
70 preg_match('/^Alias \/' . $this->initValue . ' "(.+)"/', $fileContent, $match);
71 $initDest = Util::formatWindowsPath($match[1]);
72 $apachePortUri = $bearsamppBins->getApache()->getPort() != 80 ? ':' . $bearsamppBins->getApache()->getPort() : '';
73
74 $this->wbLabelName = $bearsamppWinbinder->createLabel(
75 $this->wbWindow,
76 $bearsamppLang->getValue(Lang::ALIAS_NAME_LABEL) . ' :',
77 15, 15, 85, null, WBC_RIGHT
78 );
79 $this->wbInputName = $bearsamppWinbinder->createInputText(
80 $this->wbWindow,
81 $this->initValue,
82 105, 13, 150, null
83 );
84
85 $this->wbLabelDest = $bearsamppWinbinder->createLabel(
86 $this->wbWindow,
87 $bearsamppLang->getValue(Lang::ALIAS_DEST_LABEL) . ' :',
88 15, 45, 85, null, WBC_RIGHT
89 );
90 $this->wbInputDest = $bearsamppWinbinder->createInputText(
91 $this->wbWindow,
92 $initDest,
93 105, 43, 190, null, null, WBC_READONLY
94 );
95 $this->wbBtnDest = $bearsamppWinbinder->createButton(
96 $this->wbWindow,
98 300, 43, 110
99 );
100
101 $this->wbLabelExp = $bearsamppWinbinder->createLabel(
102 $this->wbWindow,
103 sprintf($bearsamppLang->getValue(Lang::ALIAS_EXP_LABEL), $apachePortUri, $this->initValue, $initDest),
104 15, 80, 470, 50
105 );
106 }
107
108 protected function getFormValues($bearsamppWinbinder)
109 {
110 return [
111 'name' => $bearsamppWinbinder->getText($this->wbInputName[WinBinder::CTRL_OBJ]),
112 'dest' => $bearsamppWinbinder->getText($this->wbInputDest[WinBinder::CTRL_OBJ])
113 ];
114 }
115
116 protected function validateInput($values)
117 {
118 global $bearsamppLang;
119
120 if (!ctype_alnum($values['name'])) {
121 return [
122 'valid' => false,
123 'error' => sprintf($bearsamppLang->getValue(Lang::ALIAS_NOT_VALID_ALPHA), $values['name'])
124 ];
125 }
126
127 return ['valid' => true];
128 }
129
130 protected function itemExists($values)
131 {
132 global $bearsamppRoot, $bearsamppLang, $bearsamppWinbinder;
133
134 // Only check if name changed
135 if ($values['name'] != $this->initValue && is_file($bearsamppRoot->getAliasPath() . '/' . $values['name'] . '.conf')) {
136 $bearsamppWinbinder->messageBoxError(
137 sprintf($bearsamppLang->getValue(Lang::ALIAS_ALREADY_EXISTS), $values['name']),
138 $this->getDialogTitle()
139 );
140 return true;
141 }
142
143 return false;
144 }
145
146 protected function saveItem($values)
147 {
149
150 return file_put_contents(
151 $bearsamppRoot->getAliasPath() . '/' . $values['name'] . '.conf',
152 $bearsamppBins->getApache()->getAliasContent($values['name'], $values['dest'])
153 ) !== false;
154 }
155
156 protected function deleteItem()
157 {
158 global $bearsamppRoot;
159
160 return @unlink($bearsamppRoot->getAliasPath() . '/' . $this->initValue . '.conf');
161 }
162
163 protected function getSaveSuccessMessage($values)
164 {
166
167 $apachePortUri = $bearsamppBins->getApache()->getPort() != 80 ? ':' . $bearsamppBins->getApache()->getPort() : '';
168 return sprintf(
170 $values['name'],
171 $apachePortUri,
172 $values['name'],
173 $values['dest']
174 );
175 }
176
177 protected function getSaveErrorMessage()
178 {
179 global $bearsamppLang;
181 }
182
183 protected function getDeleteConfirmMessage()
184 {
185 global $bearsamppLang;
186 return sprintf($bearsamppLang->getValue(Lang::DELETE_ALIAS), $this->initValue);
187 }
188
189 protected function getDeleteSuccessMessage()
190 {
191 global $bearsamppLang;
192 return sprintf($bearsamppLang->getValue(Lang::ALIAS_REMOVED), $this->initValue);
193 }
194
195 protected function getDeleteErrorMessage()
196 {
198 return sprintf(
200 $bearsamppRoot->getAliasPath() . '/' . $this->initValue . '.conf'
201 );
202 }
203
204 protected function restartService()
205 {
206 global $bearsamppBins;
207 $bearsamppBins->getApache()->getService()->restart();
208 }
209
210 protected function handleCustomEvent($window, $id, $ctrl, $param1, $param2)
211 {
212 global $bearsamppLang, $bearsamppBins, $bearsamppWinbinder;
213
214 $apachePortUri = $bearsamppBins->getApache()->getPort() != 80 ? ':' . $bearsamppBins->getApache()->getPort() : '';
215 $aliasName = $bearsamppWinbinder->getText($this->wbInputName[WinBinder::CTRL_OBJ]);
216 $aliasDest = $bearsamppWinbinder->getText($this->wbInputDest[WinBinder::CTRL_OBJ]);
217
218 // Handle name input change
219 if ($id == $this->wbInputName[WinBinder::CTRL_ID]) {
220 $bearsamppWinbinder->setText(
221 $this->wbLabelExp[WinBinder::CTRL_OBJ],
222 sprintf($bearsamppLang->getValue(Lang::ALIAS_EXP_LABEL), $apachePortUri, $aliasName, $aliasDest)
223 );
224 $bearsamppWinbinder->setEnabled(
225 $this->wbBtnSave[WinBinder::CTRL_OBJ],
226 !empty($aliasName)
227 );
228 }
229
230 // Handle browse button
231 if ($id == $this->wbBtnDest[WinBinder::CTRL_ID]) {
232 $aliasDest = $bearsamppWinbinder->sysDlgPath(
233 $window,
235 $aliasDest
236 );
237 if ($aliasDest && is_dir($aliasDest)) {
238 $bearsamppWinbinder->setText($this->wbInputDest[WinBinder::CTRL_OBJ], $aliasDest . '\\');
239 $bearsamppWinbinder->setText(
240 $this->wbLabelExp[WinBinder::CTRL_OBJ],
241 sprintf($bearsamppLang->getValue(Lang::ALIAS_EXP_LABEL), $apachePortUri, $aliasName, $aliasDest . '\\')
242 );
243 }
244 }
245 }
246}
global $bearsamppBins
global $bearsamppLang
global $bearsamppRoot
createFormFields($bearsamppWinbinder)
handleCustomEvent($window, $id, $ctrl, $param1, $param2)
getFormValues($bearsamppWinbinder)
const EDIT_ALIAS_TITLE
const ALIAS_REMOVE_ERROR
const BUTTON_BROWSE
const ADD_ALIAS_TITLE
const ALIAS_DEST_LABEL
const ALIAS_NAME_LABEL
const ALIAS_DEST_PATH
const ALIAS_CREATED
const ALIAS_ALREADY_EXISTS
const ALIAS_EXP_LABEL
const ALIAS_REMOVED
const ALIAS_NOT_VALID_ALPHA
const ALIAS_CREATED_ERROR
const DELETE_ALIAS_TITLE
const DELETE_ALIAS
static formatWindowsPath($path)