Bearsampp 2026.9.23
Loading...
Searching...
No Matches
class.action.changePort.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 $bin;
19 private $currentPort;
22
24 private $wbWindow;
25
28
30 private $wbLabelPort;
32 private $wbInputPort;
33
37 private $wbBtnFinish;
39 private $wbBtnCancel;
40
47 public function __construct($args)
48 {
49 global $bearsamppLang, $bearsamppBins, $bearsamppWinbinder;
50
51 if (isset($args[0]) && !empty($args[0])) {
52 $bin = $bearsamppBins->getBinByName($args[0]);
53 if ($bin !== null) {
54 $this->bin = $bin;
55 // Mailpit exposes an SMTP port separately; all other services use getPort()
56 $this->currentPort = ($args[0] == $bearsamppBins->getMailpit()->getName())
57 ? $bin->getSmtpPort()
58 : $bin->getPort();
59 } else {
60 $this->bin = $bearsamppBins->getApache();
61 $this->currentPort = $bearsamppBins->getApache()->getPort();
62 }
63 $this->cntProcessActions = 3;
64
65 $bearsamppWinbinder->reset();
66 $this->wbWindow = $bearsamppWinbinder->createAppWindow(
67 sprintf($bearsamppLang->getValue(Lang::CHANGE_PORT_TITLE), $args[0]),
68 380,
69 170,
70 WBC_NOTIFY,
71 WBC_KEYDOWN | WBC_KEYUP
72 );
73
74 $this->wbLabelCurrent = $bearsamppWinbinder->createLabel(
75 $this->wbWindow,
76 sprintf($bearsamppLang->getValue(Lang::CHANGE_PORT_CURRENT_LABEL), $args[0], $this->currentPort),
77 15,
78 15,
79 350
80 );
81
82 $this->wbLabelPort = $bearsamppWinbinder->createLabel($this->wbWindow, $bearsamppLang->getValue(Lang::CHANGE_PORT_NEW_LABEL) . ' :', 15, 45, 85, null, WBC_RIGHT);
83 $this->wbInputPort = $bearsamppWinbinder->createInputText($this->wbWindow, $this->currentPort, 105, 43, 50, null, 5, WBC_NUMBER);
84
85 $this->wbProgressBar = $bearsamppWinbinder->createProgressBar($this->wbWindow, $this->cntProcessActions + 1, 15, 107, 170);
86 $this->wbBtnFinish = $bearsamppWinbinder->createButton($this->wbWindow, $bearsamppLang->getValue(Lang::BUTTON_FINISH), 190, 102);
87 $this->wbBtnCancel = $bearsamppWinbinder->createButton($this->wbWindow, $bearsamppLang->getValue(Lang::BUTTON_CANCEL), 277, 102);
88
89 $bearsamppWinbinder->setHandler($this->wbWindow, $this, 'processWindow');
90 $bearsamppWinbinder->setFocus($this->wbInputPort[WinBinder::CTRL_OBJ]);
91 $bearsamppWinbinder->mainLoop();
92 $bearsamppWinbinder->reset();
93 }
94 }
95
105 public function processWindow($window, $id, $ctrl, $param1, $param2)
106 {
107 global $bearsamppLang, $bearsamppWinbinder;
108 $boxTitle = sprintf($bearsamppLang->getValue(Lang::CHANGE_PORT_TITLE), $this->bin);
109 $port = $bearsamppWinbinder->getText($this->wbInputPort[WinBinder::CTRL_OBJ]);
110
111 switch ($id) {
112 case $this->wbInputPort[WinBinder::CTRL_ID]:
113 $bearsamppWinbinder->setEnabled($this->wbBtnFinish[WinBinder::CTRL_OBJ], empty($port) ? false : true);
114 break;
115 case $this->wbBtnFinish[WinBinder::CTRL_ID]:
116 $bearsamppWinbinder->incrProgressBar($this->wbProgressBar);
117 if ($port == $this->currentPort) {
118 $bearsamppWinbinder->messageBoxWarning($bearsamppLang->getValue(Lang::CHANGE_PORT_SAME_ERROR), $boxTitle);
119 $bearsamppWinbinder->resetProgressBar($this->wbProgressBar);
120 break;
121 }
122 $changePort = $this->bin->changePort($port, true, $this->wbProgressBar);
123 if ($changePort === true) {
124 Util::updateLoadingText('Restarting ' . $this->bin->getName() . '...');
125 $this->bin->getService()->restart();
126
127 // Rebuild tray/menu links (including localhost URL) with the new port values.
128 $menuContent = TplApp::process();
129 if ($menuContent !== false) {
130 file_put_contents(Path::getIniFilePath(), Util::utf8ToCp1252($menuContent));
131 }
132
133 $bearsamppWinbinder->messageBoxInfo(
134 sprintf($bearsamppLang->getValue(Lang::PORT_CHANGED), $this->bin, $port),
135 $boxTitle
136 );
137 $bearsamppWinbinder->destroyWindow($window);
138 } else {
139 $bearsamppWinbinder->messageBoxError(
140 sprintf($bearsamppLang->getValue(Lang::PORT_NOT_USED_BY), $port, $changePort),
141 $boxTitle
142 );
143 $bearsamppWinbinder->resetProgressBar($this->wbProgressBar);
144 }
145 break;
146 case IDCLOSE:
147 case $this->wbBtnCancel[WinBinder::CTRL_ID]:
148 $bearsamppWinbinder->destroyWindow($window);
149 break;
150 }
151 }
152}
153
global $bearsamppBins
global $bearsamppLang
$port
processWindow($window, $id, $ctrl, $param1, $param2)
const CHANGE_PORT_SAME_ERROR
const BUTTON_CANCEL
const CHANGE_PORT_CURRENT_LABEL
const CHANGE_PORT_TITLE
const CHANGE_PORT_NEW_LABEL
const PORT_CHANGED
const BUTTON_FINISH
const PORT_NOT_USED_BY
static getIniFilePath($aetrayPath=false)
static process()
static utf8ToCp1252($data)
static updateLoadingText($text)