Bearsampp 2026.9.23
Loading...
Searching...
No Matches
class.action.addAlias.php
Go to the documentation of this file.
1<?php
2/*
3 * Copyright (c) 2021-2026 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{
17 private $wbLabelName;
19 private $wbInputName;
21 private $wbLabelDest;
23 private $wbInputDest;
25 private $wbBtnDest;
27 private $wbLabelExp;
28
34 protected function getWindowTitle()
35 {
36 global $bearsamppLang;
37
38 return $bearsamppLang->getValue(Lang::ADD_ALIAS_TITLE);
39 }
40
46 protected function getDialogTitle()
47 {
48 global $bearsamppLang;
49
50 return $bearsamppLang->getValue(Lang::ADD_ALIAS_TITLE);
51 }
52
58 protected function getDeleteDialogTitle()
59 {
60 // Not used in add mode
61 return '';
62 }
63
71 protected function createFormFields($bearsamppWinbinder)
72 {
74
75 $initName = 'test';
76 $initDest = 'C:\\';
77 $apachePortUri = $bearsamppBins->getApache()->getPort() != 80 ? ':' . $bearsamppBins->getApache()->getPort() : '';
78
79 $this->wbLabelName = $bearsamppWinbinder->createLabel(
80 $this->wbWindow,
81 $bearsamppLang->getValue(Lang::ALIAS_NAME_LABEL) . ' :',
82 15,
83 15,
84 85,
85 null,
86 WBC_RIGHT
87 );
88 $this->wbInputName = $bearsamppWinbinder->createInputText(
89 $this->wbWindow,
90 $initName,
91 105,
92 13,
93 150,
94 null
95 );
96
97 $this->wbLabelDest = $bearsamppWinbinder->createLabel(
98 $this->wbWindow,
99 $bearsamppLang->getValue(Lang::ALIAS_DEST_LABEL) . ' :',
100 15,
101 45,
102 85,
103 null,
104 WBC_RIGHT
105 );
106 $this->wbInputDest = $bearsamppWinbinder->createInputText(
107 $this->wbWindow,
108 $initDest,
109 105,
110 43,
111 190,
112 null,
113 null,
114 WBC_READONLY
115 );
116 $this->wbBtnDest = $bearsamppWinbinder->createButton(
117 $this->wbWindow,
119 300,
120 43,
121 110
122 );
123
124 $this->wbLabelExp = $bearsamppWinbinder->createLabel(
125 $this->wbWindow,
126 sprintf($bearsamppLang->getValue(Lang::ALIAS_EXP_LABEL), $apachePortUri, $initName, $initDest),
127 15,
128 80,
129 470,
130 50
131 );
132 }
133
141 protected function getFormValues($bearsamppWinbinder)
142 {
143 return [
144 'name' => $bearsamppWinbinder->getText($this->wbInputName[WinBinder::CTRL_OBJ]),
145 'dest' => $bearsamppWinbinder->getText($this->wbInputDest[WinBinder::CTRL_OBJ])
146 ];
147 }
148
156 protected function validateInput($values)
157 {
158 global $bearsamppLang;
159
160 if (!ctype_alnum($values['name'])) {
161 return [
162 'valid' => false,
163 'error' => sprintf($bearsamppLang->getValue(Lang::ALIAS_NOT_VALID_ALPHA), $values['name'])
164 ];
165 }
166
167 return ['valid' => true];
168 }
169
177 protected function itemExists($values)
178 {
179 global $bearsamppRoot, $bearsamppLang, $bearsamppWinbinder;
180
181 if (is_file(Path::getAliasPath() . '/' . $values['name'] . '.conf')) {
182 $bearsamppWinbinder->messageBoxError(
183 sprintf($bearsamppLang->getValue(Lang::ALIAS_ALREADY_EXISTS), $values['name']),
184 $this->getDialogTitle()
185 );
186
187 return true;
188 }
189
190 return false;
191 }
192
200 protected function saveItem($values)
201 {
203
204 return file_put_contents(
205 Path::getAliasPath() . '/' . $values['name'] . '.conf',
206 $bearsamppBins->getApache()->getAliasContent($values['name'], $values['dest'])
207 ) !== false;
208 }
209
215 protected function deleteItem()
216 {
217 // Not used in add mode
218 return false;
219 }
220
228 protected function getSaveSuccessMessage($values)
229 {
231
232 $apachePortUri = $bearsamppBins->getApache()->getPort() != 80 ? ':' . $bearsamppBins->getApache()->getPort() : '';
233
234 return sprintf(
236 $values['name'],
237 $apachePortUri,
238 $values['name'],
239 $values['dest']
240 );
241 }
242
248 protected function getSaveErrorMessage()
249 {
250 global $bearsamppLang;
251
253 }
254
260 protected function getDeleteConfirmMessage()
261 {
262 // Not used in add mode
263 return '';
264 }
265
271 protected function getDeleteSuccessMessage()
272 {
273 // Not used in add mode
274 return '';
275 }
276
282 protected function getDeleteErrorMessage()
283 {
284 // Not used in add mode
285 return '';
286 }
287
293 protected function restartService()
294 {
295 global $bearsamppBins;
296 $bearsamppBins->getApache()->getService()->restart();
297 }
298
310 protected function handleCustomEvent($window, $id, $ctrl, $param1, $param2)
311 {
312 global $bearsamppLang, $bearsamppBins, $bearsamppWinbinder;
313
314 $apachePortUri = $bearsamppBins->getApache()->getPort() != 80 ? ':' . $bearsamppBins->getApache()->getPort() : '';
315 $aliasName = $bearsamppWinbinder->getText($this->wbInputName[WinBinder::CTRL_OBJ]);
316 $aliasDest = $bearsamppWinbinder->getText($this->wbInputDest[WinBinder::CTRL_OBJ]);
317
318 // Handle name input change
319 if ($id == $this->wbInputName[WinBinder::CTRL_ID]) {
320 $bearsamppWinbinder->setText(
321 $this->wbLabelExp[WinBinder::CTRL_OBJ],
322 sprintf($bearsamppLang->getValue(Lang::ALIAS_EXP_LABEL), $apachePortUri, $aliasName, $aliasDest)
323 );
324 $bearsamppWinbinder->setEnabled(
325 $this->wbBtnSave[WinBinder::CTRL_OBJ],
326 !empty($aliasName)
327 );
328 }
329
330 // Handle browse button
331 if ($id == $this->wbBtnDest[WinBinder::CTRL_ID]) {
332 $aliasDest = $bearsamppWinbinder->sysDlgPath(
333 $window,
335 $aliasDest
336 );
337 if ($aliasDest && is_dir($aliasDest)) {
338 $bearsamppWinbinder->setText($this->wbInputDest[WinBinder::CTRL_OBJ], $aliasDest . '\\');
339 $bearsamppWinbinder->setText(
340 $this->wbLabelExp[WinBinder::CTRL_OBJ],
341 sprintf($bearsamppLang->getValue(Lang::ALIAS_EXP_LABEL), $apachePortUri, $aliasName, $aliasDest . '\\')
342 );
343 }
344 }
345 }
346}
347
global $bearsamppBins
global $bearsamppLang
global $bearsamppRoot
createFormFields($bearsamppWinbinder)
handleCustomEvent($window, $id, $ctrl, $param1, $param2)
getFormValues($bearsamppWinbinder)
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_NOT_VALID_ALPHA
const ALIAS_CREATED_ERROR
static getAliasPath($aetrayPath=false)