Bearsampp 2026.3.26
API documentation
Loading...
Searching...
No Matches
class.action.addAlias.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 $bearsamppLang->getValue(Lang::ADD_ALIAS_TITLE);
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 // Not used in add mode
38 return '';
39 }
40
41 protected function createFormFields($bearsamppWinbinder)
42 {
44
45 $initName = 'test';
46 $initDest = 'C:\\';
47 $apachePortUri = $bearsamppBins->getApache()->getPort() != 80 ? ':' . $bearsamppBins->getApache()->getPort() : '';
48
49 $this->wbLabelName = $bearsamppWinbinder->createLabel(
50 $this->wbWindow,
51 $bearsamppLang->getValue(Lang::ALIAS_NAME_LABEL) . ' :',
52 15, 15, 85, null, WBC_RIGHT
53 );
54 $this->wbInputName = $bearsamppWinbinder->createInputText(
55 $this->wbWindow,
56 $initName,
57 105, 13, 150, null
58 );
59
60 $this->wbLabelDest = $bearsamppWinbinder->createLabel(
61 $this->wbWindow,
62 $bearsamppLang->getValue(Lang::ALIAS_DEST_LABEL) . ' :',
63 15, 45, 85, null, WBC_RIGHT
64 );
65 $this->wbInputDest = $bearsamppWinbinder->createInputText(
66 $this->wbWindow,
67 $initDest,
68 105, 43, 190, null, null, WBC_READONLY
69 );
70 $this->wbBtnDest = $bearsamppWinbinder->createButton(
71 $this->wbWindow,
73 300, 43, 110
74 );
75
76 $this->wbLabelExp = $bearsamppWinbinder->createLabel(
77 $this->wbWindow,
78 sprintf($bearsamppLang->getValue(Lang::ALIAS_EXP_LABEL), $apachePortUri, $initName, $initDest),
79 15, 80, 470, 50
80 );
81 }
82
83 protected function getFormValues($bearsamppWinbinder)
84 {
85 return [
86 'name' => $bearsamppWinbinder->getText($this->wbInputName[WinBinder::CTRL_OBJ]),
87 'dest' => $bearsamppWinbinder->getText($this->wbInputDest[WinBinder::CTRL_OBJ])
88 ];
89 }
90
91 protected function validateInput($values)
92 {
93 global $bearsamppLang;
94
95 if (!ctype_alnum($values['name'])) {
96 return [
97 'valid' => false,
98 'error' => sprintf($bearsamppLang->getValue(Lang::ALIAS_NOT_VALID_ALPHA), $values['name'])
99 ];
100 }
101
102 return ['valid' => true];
103 }
104
105 protected function itemExists($values)
106 {
107 global $bearsamppRoot, $bearsamppLang, $bearsamppWinbinder;
108
109 if (is_file($bearsamppRoot->getAliasPath() . '/' . $values['name'] . '.conf')) {
110 $bearsamppWinbinder->messageBoxError(
111 sprintf($bearsamppLang->getValue(Lang::ALIAS_ALREADY_EXISTS), $values['name']),
112 $this->getDialogTitle()
113 );
114 return true;
115 }
116
117 return false;
118 }
119
120 protected function saveItem($values)
121 {
123
124 return file_put_contents(
125 $bearsamppRoot->getAliasPath() . '/' . $values['name'] . '.conf',
126 $bearsamppBins->getApache()->getAliasContent($values['name'], $values['dest'])
127 ) !== false;
128 }
129
130 protected function deleteItem()
131 {
132 // Not used in add mode
133 return false;
134 }
135
136 protected function getSaveSuccessMessage($values)
137 {
139
140 $apachePortUri = $bearsamppBins->getApache()->getPort() != 80 ? ':' . $bearsamppBins->getApache()->getPort() : '';
141 return sprintf(
143 $values['name'],
144 $apachePortUri,
145 $values['name'],
146 $values['dest']
147 );
148 }
149
150 protected function getSaveErrorMessage()
151 {
152 global $bearsamppLang;
154 }
155
156 protected function getDeleteConfirmMessage()
157 {
158 // Not used in add mode
159 return '';
160 }
161
162 protected function getDeleteSuccessMessage()
163 {
164 // Not used in add mode
165 return '';
166 }
167
168 protected function getDeleteErrorMessage()
169 {
170 // Not used in add mode
171 return '';
172 }
173
174 protected function restartService()
175 {
176 global $bearsamppBins;
177 $bearsamppBins->getApache()->getService()->restart();
178 }
179
180 protected function handleCustomEvent($window, $id, $ctrl, $param1, $param2)
181 {
182 global $bearsamppLang, $bearsamppBins, $bearsamppWinbinder;
183
184 $apachePortUri = $bearsamppBins->getApache()->getPort() != 80 ? ':' . $bearsamppBins->getApache()->getPort() : '';
185 $aliasName = $bearsamppWinbinder->getText($this->wbInputName[WinBinder::CTRL_OBJ]);
186 $aliasDest = $bearsamppWinbinder->getText($this->wbInputDest[WinBinder::CTRL_OBJ]);
187
188 // Handle name input change
189 if ($id == $this->wbInputName[WinBinder::CTRL_ID]) {
190 $bearsamppWinbinder->setText(
191 $this->wbLabelExp[WinBinder::CTRL_OBJ],
192 sprintf($bearsamppLang->getValue(Lang::ALIAS_EXP_LABEL), $apachePortUri, $aliasName, $aliasDest)
193 );
194 $bearsamppWinbinder->setEnabled(
195 $this->wbBtnSave[WinBinder::CTRL_OBJ],
196 !empty($aliasName)
197 );
198 }
199
200 // Handle browse button
201 if ($id == $this->wbBtnDest[WinBinder::CTRL_ID]) {
202 $aliasDest = $bearsamppWinbinder->sysDlgPath(
203 $window,
205 $aliasDest
206 );
207 if ($aliasDest && is_dir($aliasDest)) {
208 $bearsamppWinbinder->setText($this->wbInputDest[WinBinder::CTRL_OBJ], $aliasDest . '\\');
209 $bearsamppWinbinder->setText(
210 $this->wbLabelExp[WinBinder::CTRL_OBJ],
211 sprintf($bearsamppLang->getValue(Lang::ALIAS_EXP_LABEL), $apachePortUri, $aliasName, $aliasDest . '\\')
212 );
213 }
214 }
215 }
216}
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