2024.8.23
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
10/**
11 * Class ActionEditAlias
12 * Handles the editing of Apache aliases within the Bearsampp application.
13 */
15{
16 private $initName;
17 private $wbWindow;
18 private $wbLabelName;
19 private $wbInputName;
20 private $wbLabelDest;
21 private $wbInputDest;
22 private $wbBtnDest;
23 private $wbLabelExp;
25 private $wbBtnSave;
26 private $wbBtnDelete;
27 private $wbBtnCancel;
28
29 const GAUGE_SAVE = 2;
30 const GAUGE_DELETE = 2;
31
32 /**
33 * ActionEditAlias constructor.
34 * Initializes the alias editing window and its components.
35 *
36 * @param array $args Command line arguments passed to the script.
37 */
38 public function __construct($args)
39 {
40 global $bearsamppRoot, $bearsamppLang, $bearsamppBins, $bearsamppWinbinder;
41
42 if (isset($args[0]) && !empty($args[0])) {
43 $filePath = $bearsamppRoot->getAliasPath() . '/' . $args[0] . '.conf';
44 $fileContent = file_get_contents($filePath);
45 if (preg_match('/^Alias \/' . $args[0] . ' "(.+)"/', $fileContent, $match)) {
46 $this->initName = $args[0];
47 $initDest = Util::formatWindowsPath($match[1]);
48 $apachePortUri = $bearsamppBins->getApache()->getPort() != 80 ? ':' . $bearsamppBins->getApache()->getPort() : '';
49
50 $bearsamppWinbinder->reset();
51 $this->wbWindow = $bearsamppWinbinder->createAppWindow(sprintf($bearsamppLang->getValue(Lang::EDIT_ALIAS_TITLE), $this->initName), 490, 200, WBC_NOTIFY, WBC_KEYDOWN | WBC_KEYUP);
52
53 $this->wbLabelName = $bearsamppWinbinder->createLabel($this->wbWindow, $bearsamppLang->getValue(Lang::ALIAS_NAME_LABEL) . ' :', 15, 15, 85, null, WBC_RIGHT);
54 $this->wbInputName = $bearsamppWinbinder->createInputText($this->wbWindow, $this->initName, 105, 13, 150, null);
55
56 $this->wbLabelDest = $bearsamppWinbinder->createLabel($this->wbWindow, $bearsamppLang->getValue(Lang::ALIAS_DEST_LABEL) . ' :', 15, 45, 85, null, WBC_RIGHT);
57 $this->wbInputDest = $bearsamppWinbinder->createInputText($this->wbWindow, $initDest, 105, 43, 190, null, null, WBC_READONLY);
58 $this->wbBtnDest = $bearsamppWinbinder->createButton($this->wbWindow, $bearsamppLang->getValue(Lang::BUTTON_BROWSE), 300, 43, 110);
59
60 $this->wbLabelExp = $bearsamppWinbinder->createLabel($this->wbWindow, sprintf($bearsamppLang->getValue(Lang::ALIAS_EXP_LABEL), $apachePortUri, $this->initName, $initDest), 15, 80, 470, 50);
61
62 $this->wbProgressBar = $bearsamppWinbinder->createProgressBar($this->wbWindow, self::GAUGE_SAVE + 1, 15, 137, 190);
63 $this->wbBtnSave = $bearsamppWinbinder->createButton($this->wbWindow, $bearsamppLang->getValue(Lang::BUTTON_SAVE), 215, 132);
64 $this->wbBtnDelete = $bearsamppWinbinder->createButton($this->wbWindow, $bearsamppLang->getValue(Lang::BUTTON_DELETE), 300, 132);
65 $this->wbBtnCancel = $bearsamppWinbinder->createButton($this->wbWindow, $bearsamppLang->getValue(Lang::BUTTON_CANCEL), 385, 132);
66
67 $bearsamppWinbinder->setHandler($this->wbWindow, $this, 'processWindow');
68 $bearsamppWinbinder->mainLoop();
69 $bearsamppWinbinder->reset();
70 }
71 }
72 }
73
74 /**
75 * Processes window events and handles user interactions.
76 *
77 * @param resource $window The window resource.
78 * @param int $id The control ID.
79 * @param resource $ctrl The control resource.
80 * @param mixed $param1 Additional parameter 1.
81 * @param mixed $param2 Additional parameter 2.
82 */
83 public function processWindow($window, $id, $ctrl, $param1, $param2)
84 {
85 global $bearsamppRoot, $bearsamppBins, $bearsamppLang, $bearsamppWinbinder;
86
87 $apachePortUri = $bearsamppBins->getApache()->getPort() != 80 ? ':' . $bearsamppBins->getApache()->getPort() : '';
88 $aliasName = $bearsamppWinbinder->getText($this->wbInputName[WinBinder::CTRL_OBJ]);
89 $aliasDest = $bearsamppWinbinder->getText($this->wbInputDest[WinBinder::CTRL_OBJ]);
90
91 switch ($id) {
92 case $this->wbInputName[WinBinder::CTRL_ID]:
93 $bearsamppWinbinder->setText(
94 $this->wbLabelExp[WinBinder::CTRL_OBJ],
95 sprintf($bearsamppLang->getValue(Lang::ALIAS_EXP_LABEL), $apachePortUri, $aliasName, $aliasDest)
96 );
97 $bearsamppWinbinder->setEnabled($this->wbBtnSave[WinBinder::CTRL_OBJ], empty($aliasName) ? false : true);
98 break;
99 case $this->wbBtnDest[WinBinder::CTRL_ID]:
100 $aliasDest = $bearsamppWinbinder->sysDlgPath($window, $bearsamppLang->getValue(Lang::ALIAS_DEST_PATH), $aliasDest);
101 if ($aliasDest && is_dir($aliasDest)) {
102 $bearsamppWinbinder->setText($this->wbInputDest[WinBinder::CTRL_OBJ], $aliasDest . '\\');
103 $bearsamppWinbinder->setText(
104 $this->wbLabelExp[WinBinder::CTRL_OBJ],
105 sprintf($bearsamppLang->getValue(Lang::ALIAS_EXP_LABEL), $apachePortUri, $aliasName, $aliasDest . '\\')
106 );
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 (!ctype_alnum($aliasName)) {
114 $bearsamppWinbinder->messageBoxError(
115 sprintf($bearsamppLang->getValue(Lang::ALIAS_NOT_VALID_ALPHA), $aliasName),
117 $bearsamppWinbinder->resetProgressBar($this->wbProgressBar);
118 break;
119 }
120
121 if ($aliasName != $this->initName && is_file($bearsamppRoot->getAliasPath() . '/' . $aliasName . '.conf')) {
122 $bearsamppWinbinder->messageBoxError(
123 sprintf($bearsamppLang->getValue(Lang::ALIAS_ALREADY_EXISTS), $aliasName),
125 $bearsamppWinbinder->resetProgressBar($this->wbProgressBar);
126 break;
127 }
128 if (file_put_contents($bearsamppRoot->getAliasPath() . '/' . $aliasName . '.conf', $bearsamppBins->getApache()->getAliasContent($aliasName, $aliasDest)) !== false) {
129 $bearsamppWinbinder->incrProgressBar($this->wbProgressBar);
130
131 $bearsamppBins->getApache()->getService()->restart();
132 $bearsamppWinbinder->incrProgressBar($this->wbProgressBar);
133
134 $bearsamppWinbinder->messageBoxInfo(
135 sprintf($bearsamppLang->getValue(Lang::ALIAS_CREATED), $aliasName, $apachePortUri, $aliasName, $aliasDest),
137 $bearsamppWinbinder->destroyWindow($window);
138 } else {
139 $bearsamppWinbinder->messageBoxError($bearsamppLang->getValue(Lang::ALIAS_CREATED_ERROR), $bearsamppLang->getValue(Lang::ADD_ALIAS_TITLE));
140 $bearsamppWinbinder->resetProgressBar($this->wbProgressBar);
141 }
142 break;
143 case $this->wbBtnDelete[WinBinder::CTRL_ID]:
144 $bearsamppWinbinder->setProgressBarMax($this->wbProgressBar, self::GAUGE_DELETE + 1);
145
146 $boxTitle = $bearsamppLang->getValue(Lang::DELETE_ALIAS_TITLE);
147 $confirm = $bearsamppWinbinder->messageBoxYesNo(
148 sprintf($bearsamppLang->getValue(Lang::DELETE_ALIAS), $this->initName),
149 $boxTitle);
150
151 $bearsamppWinbinder->incrProgressBar($this->wbProgressBar);
152
153 if ($confirm) {
154 if (@unlink($bearsamppRoot->getAliasPath() . '/' . $this->initName . '.conf')) {
155 $bearsamppWinbinder->incrProgressBar($this->wbProgressBar);
156
157 $bearsamppBins->getApache()->getService()->restart();
158 $bearsamppWinbinder->incrProgressBar($this->wbProgressBar);
159
160 $bearsamppWinbinder->messageBoxInfo(
161 sprintf($bearsamppLang->getValue(Lang::ALIAS_REMOVED), $this->initName),
162 $boxTitle);
163 $bearsamppWinbinder->destroyWindow($window);
164 } else {
165 $bearsamppWinbinder->messageBoxError(
166 sprintf($bearsamppLang->getValue(Lang::ALIAS_REMOVE_ERROR), $bearsamppRoot->getAliasPath() . '/' . $this->initName . '.conf'),
167 $boxTitle);
168 $bearsamppWinbinder->resetProgressBar($this->wbProgressBar);
169 }
170 }
171 break;
172 case IDCLOSE:
173 case $this->wbBtnCancel[WinBinder::CTRL_ID]:
174 $bearsamppWinbinder->destroyWindow($window);
175 break;
176 }
177 }
178}
global $bearsamppBins
global $bearsamppLang
global $bearsamppRoot
processWindow($window, $id, $ctrl, $param1, $param2)
const ALIAS_DEST_PATH
const ALIAS_DEST_LABEL
const ADD_ALIAS_TITLE
const ALIAS_CREATED
const BUTTON_CANCEL
const ALIAS_NOT_VALID_ALPHA
const ALIAS_NAME_LABEL
const BUTTON_SAVE
const ALIAS_EXP_LABEL
const ALIAS_REMOVE_ERROR
const EDIT_ALIAS_TITLE
const ALIAS_REMOVED
const ALIAS_ALREADY_EXISTS
const ALIAS_CREATED_ERROR
const DELETE_ALIAS
const BUTTON_BROWSE
const DELETE_ALIAS_TITLE
const BUTTON_DELETE
static formatWindowsPath($path)