Bearsampp 2026.5.5
Loading...
Searching...
No Matches
class.action.changePort.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 $bin;
17 private $currentPort;
19
20 private $wbWindow;
21
23
24 private $wbLabelPort;
25 private $wbInputPort;
26
28 private $wbBtnFinish;
29 private $wbBtnCancel;
30
37 public function __construct($args)
38 {
39 global $bearsamppLang, $bearsamppBins, $bearsamppWinbinder;
40
41 if ( isset( $args[0] ) && !empty( $args[0] ) ) {
42 $bin = $bearsamppBins->getBinByName($args[0]);
43 if ($bin !== null) {
44 $this->bin = $bin;
45 // Mailpit exposes an SMTP port separately; all other services use getPort()
46 $this->currentPort = ($args[0] == $bearsamppBins->getMailpit()->getName())
47 ? $bin->getSmtpPort()
48 : $bin->getPort();
49 } else {
50 $this->bin = $bearsamppBins->getApache();
51 $this->currentPort = $bearsamppBins->getApache()->getPort();
52 }
53 $this->cntProcessActions = 3;
54
55 $bearsamppWinbinder->reset();
56 $this->wbWindow = $bearsamppWinbinder->createAppWindow( sprintf( $bearsamppLang->getValue( Lang::CHANGE_PORT_TITLE ), $args[0] ), 380, 170, WBC_NOTIFY, WBC_KEYDOWN | WBC_KEYUP );
57
58 $this->wbLabelCurrent = $bearsamppWinbinder->createLabel(
59 $this->wbWindow,
60 sprintf( $bearsamppLang->getValue( Lang::CHANGE_PORT_CURRENT_LABEL ), $args[0], $this->currentPort ), 15, 15, 350
61 );
62
63 $this->wbLabelPort = $bearsamppWinbinder->createLabel( $this->wbWindow, $bearsamppLang->getValue( Lang::CHANGE_PORT_NEW_LABEL ) . ' :', 15, 45, 85, null, WBC_RIGHT );
64 $this->wbInputPort = $bearsamppWinbinder->createInputText( $this->wbWindow, $this->currentPort, 105, 43, 50, null, 5, WBC_NUMBER );
65
66 $this->wbProgressBar = $bearsamppWinbinder->createProgressBar( $this->wbWindow, $this->cntProcessActions + 1, 15, 107, 170 );
67 $this->wbBtnFinish = $bearsamppWinbinder->createButton( $this->wbWindow, $bearsamppLang->getValue( Lang::BUTTON_FINISH ), 190, 102 );
68 $this->wbBtnCancel = $bearsamppWinbinder->createButton( $this->wbWindow, $bearsamppLang->getValue( Lang::BUTTON_CANCEL ), 277, 102 );
69
70 $bearsamppWinbinder->setHandler( $this->wbWindow, $this, 'processWindow' );
71 $bearsamppWinbinder->setFocus( $this->wbInputPort[WinBinder::CTRL_OBJ] );
72 $bearsamppWinbinder->mainLoop();
73 $bearsamppWinbinder->reset();
74 }
75 }
76
86 public function processWindow($window, $id, $ctrl, $param1, $param2)
87 {
88 global $bearsamppLang, $bearsamppWinbinder;
89 $boxTitle = sprintf( $bearsamppLang->getValue( Lang::CHANGE_PORT_TITLE ), $this->bin );
90 $port = $bearsamppWinbinder->getText( $this->wbInputPort[WinBinder::CTRL_OBJ] );
91
92 switch ( $id ) {
93 case $this->wbInputPort[WinBinder::CTRL_ID]:
94 $bearsamppWinbinder->setEnabled( $this->wbBtnFinish[WinBinder::CTRL_OBJ], empty( $port ) ? false : true );
95 break;
96 case $this->wbBtnFinish[WinBinder::CTRL_ID]:
97 $bearsamppWinbinder->incrProgressBar( $this->wbProgressBar );
98 if ( $port == $this->currentPort ) {
99 $bearsamppWinbinder->messageBoxWarning( $bearsamppLang->getValue( Lang::CHANGE_PORT_SAME_ERROR ), $boxTitle );
100 $bearsamppWinbinder->resetProgressBar( $this->wbProgressBar );
101 break;
102 }
103 $changePort = $this->bin->changePort( $port, true, $this->wbProgressBar );
104 if ( $changePort === true ) {
105 Util::updateLoadingText('Restarting ' . $this->bin->getName() . '...');
106 $this->bin->getService()->restart();
107
108 $bearsamppWinbinder->messageBoxInfo(
109 sprintf( $bearsamppLang->getValue( Lang::PORT_CHANGED ), $this->bin, $port ),
110 $boxTitle
111 );
112 $bearsamppWinbinder->destroyWindow( $window );
113 }
114 else {
115 $bearsamppWinbinder->messageBoxError(
116 sprintf( $bearsamppLang->getValue( Lang::PORT_NOT_USED_BY ), $port, $changePort ),
117 $boxTitle
118 );
119 $bearsamppWinbinder->resetProgressBar( $this->wbProgressBar );
120 }
121 break;
122 case IDCLOSE:
123 case $this->wbBtnCancel[WinBinder::CTRL_ID]:
124 $bearsamppWinbinder->destroyWindow( $window );
125 break;
126 }
127 }
128}
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 updateLoadingText($text)