Bearsampp 2025.8.29
Loading...
Searching...
No Matches
class.bin.xlight.php
Go to the documentation of this file.
1<?php
2/*
3 *
4 * * Copyright (c) 2021-2024 Bearsampp
5 * * License: GNU General Public License version 3 or later; see LICENSE.txt
6 * * Website: https://bearsampp.com
7 * * Github: https://github.com/Bearsampp
8 *
9 */
10
17class BinXlight extends Module
18{
19 const SERVICE_NAME = 'bearsamppxlight';
20 const SERVICE_PARAMS = ' -startall';
21
22 const ROOT_CFG_ENABLE = 'xlightEnable';
23 const ROOT_CFG_VERSION = 'xlightVersion';
24
25 const LOCAL_CFG_EXE = 'xlightExe';
26 const LOCAL_CFG_SSL_PORT = 'xlightSslPort';
27 const LOCAL_CFG_PORT = 'xlightPort';
28
29 private $service;
30 private $log;
31
32 private $exe;
33 private $port;
34 private $SslPort;
35
42 public function __construct($id, $type) {
43 Util::logInitClass($this);
44 $this->reload($id, $type);
45 }
46
53 public function reload($id = null, $type = null) {
56
57 $this->name = $bearsamppLang->getValue(Lang::XLIGHT);
58 $this->version = $bearsamppConfig->getRaw(self::ROOT_CFG_VERSION);
59 parent::reload($id, $type);
60
61 $this->enable = $this->enable && $bearsamppConfig->getRaw(self::ROOT_CFG_ENABLE);
62 $this->service = new Win32Service(self::SERVICE_NAME);
63 $this->log = $bearsamppRoot->getLogsPath() . '/xlight.log';
64
65 if ($this->bearsamppConfRaw !== false) {
66 $this->exe = $this->symlinkPath . '/' . $this->bearsamppConfRaw[self::LOCAL_CFG_EXE];
67 $this->SslPort = intval($this->bearsamppConfRaw[self::LOCAL_CFG_SSL_PORT]);
68 $this->port = intval($this->bearsamppConfRaw[self::LOCAL_CFG_PORT]);
69 }
70
71 if (!$this->enable) {
72 Util::logInfo($this->name . ' is not enabled!');
73 return;
74 }
75 if (!is_dir($this->currentPath)) {
76 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_FILE_NOT_FOUND), $this->name . ' ' . $this->version, $this->currentPath));
77 return;
78 }
79 if (!is_dir($this->symlinkPath)) {
80 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_FILE_NOT_FOUND), $this->name . ' ' . $this->version, $this->symlinkPath));
81 return;
82 }
83 if (!is_file($this->bearsamppConf)) {
84 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_CONF_NOT_FOUND), $this->name . ' ' . $this->version, $this->bearsamppConf));
85 return;
86 }
87 if (!is_file($this->exe)) {
88 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_EXE_NOT_FOUND), $this->name . ' ' . $this->version, $this->exe));
89 return;
90 }
91 if (empty($this->SslPort)) {
92 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_INVALID_PARAMETER), self::LOCAL_CFG_SSL_PORT, $this->SslPort));
93 return;
94 }
95 if (empty($this->port)) {
96 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_INVALID_PARAMETER), self::LOCAL_CFG_PORT, $this->port));
97 return;
98 }
99
100 $nssm = new Nssm(self::SERVICE_NAME);
101 $nssm->setDisplayName(APP_TITLE . ' ' . $this->getName());
102 $nssm->setBinPath($this->exe);
103 $nssm->setParams(sprintf(self::SERVICE_PARAMS, $this->SslPort, $this->port));
104 $nssm->setStart(Nssm::SERVICE_DEMAND_START);
105 $nssm->setStdout($bearsamppRoot->getLogsPath() . '/xlight.log');
106 $nssm->setStderr($bearsamppRoot->getLogsPath() . '/xlight.error.log');
107
108 $this->service->setNssm($nssm);
109 }
110
116 protected function replaceAll($params) {
117 $content = file_get_contents($this->bearsamppConf);
118
119 foreach ($params as $key => $value) {
120 $content = preg_replace('|' . $key . ' = .*|', $key . ' = ' . '"' . $value.'"', $content);
121 $this->bearsamppConfRaw[$key] = $value;
122 switch ($key) {
123 case self::LOCAL_CFG_SSL_PORT:
124 $this->SslPort = intval($value);
125 break;
126 case self::LOCAL_CFG_PORT:
127 $this->port = intval($value);
128 break;
129 }
130 }
131
132 file_put_contents($this->bearsamppConf, $content);
133 }
134
140 public function rebuildConf() {
141 global $bearsamppRegistry;
142
143 $exists = $bearsamppRegistry->exists(
145 'SYSTEM\CurrentControlSet\Services\\' . self::SERVICE_NAME . '\Parameters',
147 );
148 if ($exists) {
149 return $bearsamppRegistry->setExpandStringValue(
151 'SYSTEM\CurrentControlSet\Services\\' . self::SERVICE_NAME . '\Parameters',
153 sprintf(self::SERVICE_PARAMS, $this->SslPort, $this->port)
154 );
155 }
156
157 return false;
158 }
159
168 public function changePort($port, $checkUsed = false, $wbProgressBar = null) {
169 global $bearsamppWinbinder;
170
171 if (!Util::isValidPort($port)) {
172 Util::logError($this->getName() . ' port not valid: ' . $port);
173 return false;
174 }
175
176 $port = intval($port);
177 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
178
179 $isPortInUse = Util::isPortInUse($port);
180 if (!$checkUsed || $isPortInUse === false) {
181 // bearsampp.conf
182 $this->setPort($port);
183 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
184
185 // conf
186 $this->update();
187 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
188
189 return true;
190 }
191
192 Util::logDebug($this->getName() . ' port in used: ' . $port . ' - ' . $isPortInUse);
193 return $isPortInUse;
194 }
195
203 public function checkPort($port, $showWindow = false) {
204 global $bearsamppLang, $bearsamppWinbinder;
205 $boxTitle = sprintf($bearsamppLang->getValue(Lang::CHECK_PORT_TITLE), $this->getName(), $port);
206
207 if (!Util::isValidPort($port)) {
208 Util::logError($this->getName() . ' port not valid: ' . $port);
209 return false;
210 }
211
212 $headers = Util::getHeaders('127.0.0.1', $port);
213 if (!empty($headers)) {
214 if (Util::contains($headers[0], 'Xlight')) {
215 Util::logDebug($this->getName() . ' port ' . $port . ' is used by: ' . str_replace('220 ', '', $headers[0]));
216 if ($showWindow) {
217 $bearsamppWinbinder->messageBoxInfo(
218 sprintf($bearsamppLang->getValue(Lang::PORT_USED_BY), $port, str_replace('220 ', '', $headers[0])),
219 $boxTitle
220 );
221 }
222 return true;
223 }
224 Util::logDebug($this->getName() . ' port ' . $port . ' is used by another application');
225 if ($showWindow) {
226 $bearsamppWinbinder->messageBoxWarning(
227 sprintf($bearsamppLang->getValue(Lang::PORT_NOT_USED_BY), $port),
228 $boxTitle
229 );
230 }
231 } else {
232 Util::logDebug($this->getName() . ' port ' . $port . ' is not used');
233 if ($showWindow) {
234 $bearsamppWinbinder->messageBoxError(
235 sprintf($bearsamppLang->getValue(Lang::PORT_NOT_USED), $port),
236 $boxTitle
237 );
238 }
239 }
240
241 return false;
242 }
243
251 public function switchVersion($version, $showWindow = false) {
252 Util::logDebug('Switch ' . $this->name . ' version to ' . $version);
253 return $this->updateConfig($version, 0, $showWindow);
254 }
255
264 protected function updateConfig($version = null, $sub = 0, $showWindow = false) {
265 global $bearsamppLang, $bearsamppWinbinder;
266
267 if (!$this->enable) {
268 return true;
269 }
270
271 $version = $version == null ? $this->version : $version;
272 Util::logDebug(($sub > 0 ? str_repeat(' ', 2 * $sub) : '') . 'Update ' . $this->name . ' ' . $version . ' config');
273
274 $boxTitle = sprintf($bearsamppLang->getValue(Lang::SWITCH_VERSION_TITLE), $this->getName(), $version);
275
276 $bearsamppConf = str_replace('xlight' . $this->getVersion(), 'xlight' . $version, $this->bearsamppConf);
277 if (!file_exists($bearsamppConf)) {
278 Util::logError('bearsampp config files not found for ' . $this->getName() . ' ' . $version);
279 if ($showWindow) {
280 $bearsamppWinbinder->messageBoxError(
281 sprintf($bearsamppLang->getValue(Lang::BEARSAMPP_CONF_NOT_FOUND_ERROR), $this->getName() . ' ' . $version),
282 $boxTitle
283 );
284 }
285 return false;
286 }
287
288 $bearsamppConfRaw = parse_ini_file($bearsamppConf);
289 if ($bearsamppConfRaw === false || !isset($bearsamppConfRaw[self::ROOT_CFG_VERSION]) || $bearsamppConfRaw[self::ROOT_CFG_VERSION] != $version) {
290 Util::logError('bearsampp config file malformed for ' . $this->getName() . ' ' . $version);
291 if ($showWindow) {
292 $bearsamppWinbinder->messageBoxError(
293 sprintf($bearsamppLang->getValue(Lang::BEARSAMPP_CONF_MALFORMED_ERROR), $this->getName() . ' ' . $version),
294 $boxTitle
295 );
296 }
297 return false;
298 }
299
300 // bearsampp.conf
301 $this->setVersion($version);
302
303 return true;
304 }
305
311 public function setVersion($version) {
312 global $bearsamppConfig;
313 $this->version = $version;
314 $bearsamppConfig->replace(self::ROOT_CFG_VERSION, $version);
315 $this->reload();
316 }
317
323 public function getService() {
324 return $this->service;
325 }
326
333 public function setEnable($enabled, $showWindow = false) {
334 global $bearsamppConfig, $bearsamppLang, $bearsamppWinbinder;
335
336 if ($enabled == Config::ENABLED && !is_dir($this->currentPath)) {
337 Util::logDebug($this->getName() . ' cannot be enabled because bundle ' . $this->getVersion() . ' does not exist in ' . $this->currentPath);
338 if ($showWindow) {
339 $bearsamppWinbinder->messageBoxError(
340 sprintf($bearsamppLang->getValue(Lang::ENABLE_BUNDLE_NOT_EXIST), $this->getName(), $this->getVersion(), $this->currentPath),
341 sprintf($bearsamppLang->getValue(Lang::ENABLE_TITLE), $this->getName())
342 );
343 }
344 $enabled = Config::DISABLED;
345 }
346
347 Util::logInfo($this->getName() . ' switched to ' . ($enabled == Config::ENABLED ? 'enabled' : 'disabled'));
348 $this->enable = $enabled == Config::ENABLED;
349 $bearsamppConfig->replace(self::ROOT_CFG_ENABLE, $enabled);
350
351 $this->reload();
352 if ($this->enable) {
353 Util::installService($this, $this->port, null, $showWindow);
354 } else {
355 Util::removeService($this->service, $this->name);
356 }
357 }
358
364 public function getLog() {
365 return $this->log;
366 }
367
373 public function getExe() {
374 return $this->exe;
375 }
376
382 public function getUiPort() {
383 return $this->SslPort;
384 }
385
391 public function setSslPort($SslPort) {
392 $this->replace(self::LOCAL_CFG_SSL_PORT, $SslPort);
393 }
394
400 public function getPort() {
401 return $this->port;
402 }
403
409 public function setPort($port) {
410 $this->replace(self::LOCAL_CFG_PORT, $port);
411 }
412}
global $bearsamppLang
global $bearsamppRoot
replaceAll($params)
updateConfig($version=null, $sub=0, $showWindow=false)
const LOCAL_CFG_SSL_PORT
switchVersion($version, $showWindow=false)
const ROOT_CFG_ENABLE
setVersion($version)
checkPort($port, $showWindow=false)
changePort($port, $checkUsed=false, $wbProgressBar=null)
setEnable($enabled, $showWindow=false)
reload($id=null, $type=null)
const ROOT_CFG_VERSION
__construct($id, $type)
setSslPort($SslPort)
const DISABLED
const ENABLED
const ENABLE_BUNDLE_NOT_EXIST
const BEARSAMPP_CONF_MALFORMED_ERROR
const ERROR_EXE_NOT_FOUND
const ERROR_CONF_NOT_FOUND
const BEARSAMPP_CONF_NOT_FOUND_ERROR
const PORT_NOT_USED
const ERROR_INVALID_PARAMETER
const ENABLE_TITLE
const PORT_NOT_USED_BY
const XLIGHT
const SWITCH_VERSION_TITLE
const CHECK_PORT_TITLE
const PORT_USED_BY
const ERROR_FILE_NOT_FOUND
update($sub=0, $showWindow=false)
replace($key, $value)
const INFO_APP_PARAMETERS
const SERVICE_DEMAND_START
const HKEY_LOCAL_MACHINE
static logError($data, $file=null)
static installService($bin, $port, $syntaxCheckCmd, $showWindow=false)
static logInitClass($classInstance)
static getHeaders($host, $port, $ssl=false)
static removeService($service, $name)
static isValidPort($port)
static logInfo($data, $file=null)
static contains($string, $search)
static logDebug($data, $file=null)
static logReloadClass($classInstance)
static isPortInUse($port)
global $bearsamppConfig
Definition homepage.php:27
const APP_TITLE
Definition root.php:13