Bearsampp 2026.9.23
Loading...
Searching...
No Matches
class.action.editAlias.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 sprintf($bearsamppLang->getValue(Lang::EDIT_ALIAS_TITLE), $this->initValue);
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 global $bearsamppLang;
61
63 }
64
72 protected function initializeDialog($args)
73 {
74 global $bearsamppRoot;
75
76 if (!isset($args[0]) || empty($args[0])) {
77 return false;
78 }
79
80 $filePath = Path::getAliasPath() . '/' . $args[0] . '.conf';
81 if (!file_exists($filePath)) {
82 return false;
83 }
84
85 $fileContent = file_get_contents($filePath);
86 if (!preg_match('/^Alias \/' . $args[0] . ' "(.+)"/', $fileContent, $match)) {
87 return false;
88 }
89
90 $this->initValue = $args[0];
91
92 return true;
93 }
94
102 protected function createFormFields($bearsamppWinbinder)
103 {
105
106 // Load existing alias data
107 $filePath = Path::getAliasPath() . '/' . $this->initValue . '.conf';
108 $fileContent = file_get_contents($filePath);
109 preg_match('/^Alias \/' . $this->initValue . ' "(.+)"/', $fileContent, $match);
110 $initDest = Path::formatWindowsPath($match[1]);
111 $apachePortUri = $bearsamppBins->getApache()->getPort() != 80 ? ':' . $bearsamppBins->getApache()->getPort() : '';
112
113 $this->wbLabelName = $bearsamppWinbinder->createLabel(
114 $this->wbWindow,
115 $bearsamppLang->getValue(Lang::ALIAS_NAME_LABEL) . ' :',
116 15,
117 15,
118 85,
119 null,
120 WBC_RIGHT
121 );
122 $this->wbInputName = $bearsamppWinbinder->createInputText(
123 $this->wbWindow,
124 $this->initValue,
125 105,
126 13,
127 150,
128 null
129 );
130
131 $this->wbLabelDest = $bearsamppWinbinder->createLabel(
132 $this->wbWindow,
133 $bearsamppLang->getValue(Lang::ALIAS_DEST_LABEL) . ' :',
134 15,
135 45,
136 85,
137 null,
138 WBC_RIGHT
139 );
140 $this->wbInputDest = $bearsamppWinbinder->createInputText(
141 $this->wbWindow,
142 $initDest,
143 105,
144 43,
145 190,
146 null,
147 null,
148 WBC_READONLY
149 );
150 $this->wbBtnDest = $bearsamppWinbinder->createButton(
151 $this->wbWindow,
153 300,
154 43,
155 110
156 );
157
158 $this->wbLabelExp = $bearsamppWinbinder->createLabel(
159 $this->wbWindow,
160 sprintf($bearsamppLang->getValue(Lang::ALIAS_EXP_LABEL), $apachePortUri, $this->initValue, $initDest),
161 15,
162 80,
163 470,
164 50
165 );
166 }
167
175 protected function getFormValues($bearsamppWinbinder)
176 {
177 return [
178 'name' => $bearsamppWinbinder->getText($this->wbInputName[WinBinder::CTRL_OBJ]),
179 'dest' => $bearsamppWinbinder->getText($this->wbInputDest[WinBinder::CTRL_OBJ])
180 ];
181 }
182
190 protected function validateInput($values)
191 {
192 global $bearsamppLang;
193
194 if (!ctype_alnum($values['name'])) {
195 return [
196 'valid' => false,
197 'error' => sprintf($bearsamppLang->getValue(Lang::ALIAS_NOT_VALID_ALPHA), $values['name'])
198 ];
199 }
200
201 return ['valid' => true];
202 }
203
211 protected function itemExists($values)
212 {
213 global $bearsamppRoot, $bearsamppLang, $bearsamppWinbinder;
214
215 // Only check if name changed
216 if ($values['name'] != $this->initValue && is_file(Path::getAliasPath() . '/' . $values['name'] . '.conf')) {
217 $bearsamppWinbinder->messageBoxError(
218 sprintf($bearsamppLang->getValue(Lang::ALIAS_ALREADY_EXISTS), $values['name']),
219 $this->getDialogTitle()
220 );
221
222 return true;
223 }
224
225 return false;
226 }
227
235 protected function saveItem($values)
236 {
238
239 return file_put_contents(
240 Path::getAliasPath() . '/' . $values['name'] . '.conf',
241 $bearsamppBins->getApache()->getAliasContent($values['name'], $values['dest'])
242 ) !== false;
243 }
244
250 protected function deleteItem()
251 {
252 global $bearsamppRoot;
253
254 return @unlink(Path::getAliasPath() . '/' . $this->initValue . '.conf');
255 }
256
264 protected function getSaveSuccessMessage($values)
265 {
267
268 $apachePortUri = $bearsamppBins->getApache()->getPort() != 80 ? ':' . $bearsamppBins->getApache()->getPort() : '';
269
270 return sprintf(
272 $values['name'],
273 $apachePortUri,
274 $values['name'],
275 $values['dest']
276 );
277 }
278
284 protected function getSaveErrorMessage()
285 {
286 global $bearsamppLang;
287
289 }
290
296 protected function getDeleteConfirmMessage()
297 {
298 global $bearsamppLang;
299
300 return sprintf($bearsamppLang->getValue(Lang::DELETE_ALIAS), $this->initValue);
301 }
302
308 protected function getDeleteSuccessMessage()
309 {
310 global $bearsamppLang;
311
312 return sprintf($bearsamppLang->getValue(Lang::ALIAS_REMOVED), $this->initValue);
313 }
314
320 protected function getDeleteErrorMessage()
321 {
323
324 return sprintf(
326 Path::getAliasPath() . '/' . $this->initValue . '.conf'
327 );
328 }
329
335 protected function restartService()
336 {
337 global $bearsamppBins;
338 $bearsamppBins->getApache()->getService()->restart();
339 }
340
352 protected function handleCustomEvent($window, $id, $ctrl, $param1, $param2)
353 {
354 global $bearsamppLang, $bearsamppBins, $bearsamppWinbinder;
355
356 $apachePortUri = $bearsamppBins->getApache()->getPort() != 80 ? ':' . $bearsamppBins->getApache()->getPort() : '';
357 $aliasName = $bearsamppWinbinder->getText($this->wbInputName[WinBinder::CTRL_OBJ]);
358 $aliasDest = $bearsamppWinbinder->getText($this->wbInputDest[WinBinder::CTRL_OBJ]);
359
360 // Handle name input change
361 if ($id == $this->wbInputName[WinBinder::CTRL_ID]) {
362 $bearsamppWinbinder->setText(
363 $this->wbLabelExp[WinBinder::CTRL_OBJ],
364 sprintf($bearsamppLang->getValue(Lang::ALIAS_EXP_LABEL), $apachePortUri, $aliasName, $aliasDest)
365 );
366 $bearsamppWinbinder->setEnabled(
367 $this->wbBtnSave[WinBinder::CTRL_OBJ],
368 !empty($aliasName)
369 );
370 }
371
372 // Handle browse button
373 if ($id == $this->wbBtnDest[WinBinder::CTRL_ID]) {
374 $aliasDest = $bearsamppWinbinder->sysDlgPath(
375 $window,
377 $aliasDest
378 );
379 if ($aliasDest && is_dir($aliasDest)) {
380 $bearsamppWinbinder->setText($this->wbInputDest[WinBinder::CTRL_OBJ], $aliasDest . '\\');
381 $bearsamppWinbinder->setText(
382 $this->wbLabelExp[WinBinder::CTRL_OBJ],
383 sprintf($bearsamppLang->getValue(Lang::ALIAS_EXP_LABEL), $apachePortUri, $aliasName, $aliasDest . '\\')
384 );
385 }
386 }
387 }
388}
389
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 getAliasPath($aetrayPath=false)
static formatWindowsPath($path)