Bearsampp
2026.7.11
Toggle main menu visibility
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
17
class
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
Log::initClass
($this);
44
$this->
reload
(
$id
,
$type
);
45
}
46
53
public
function
reload
(
$id
=
null
,
$type
=
null
) {
54
global
$bearsamppRoot
,
$bearsamppConfig
,
$bearsamppLang
;
55
Log::reloadClass
($this);
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 =
Path::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
Log::info
($this->name .
' is not enabled!'
);
73
return
;
74
}
75
if
(!is_dir($this->currentPath)) {
76
Log::error
(sprintf(
$bearsamppLang
->getValue(
Lang::ERROR_FILE_NOT_FOUND
), $this->name .
' '
. $this->version, $this->currentPath));
77
return
;
78
}
79
if
(!is_dir($this->symlinkPath)) {
80
Log::error
(sprintf(
$bearsamppLang
->getValue(
Lang::ERROR_FILE_NOT_FOUND
), $this->name .
' '
. $this->version, $this->symlinkPath));
81
return
;
82
}
83
if
(!is_file($this->bearsamppConf)) {
84
Log::error
(sprintf(
$bearsamppLang
->getValue(
Lang::ERROR_CONF_NOT_FOUND
), $this->name .
' '
. $this->version, $this->bearsamppConf));
85
return
;
86
}
87
if
(!is_file($this->exe)) {
88
Log::error
(sprintf(
$bearsamppLang
->getValue(
Lang::ERROR_EXE_NOT_FOUND
), $this->name .
' '
. $this->version, $this->exe));
89
return
;
90
}
91
if
(empty($this->SslPort)) {
92
Log::error
(sprintf(
$bearsamppLang
->getValue(
Lang::ERROR_INVALID_PARAMETER
), self::LOCAL_CFG_SSL_PORT, $this->SslPort));
93
return
;
94
}
95
if
(empty($this->port)) {
96
Log::error
(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(
Path::getLogsPath
() .
'/xlight.log'
);
106
$nssm->setStderr(
Path::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(
144
Registry::HKEY_LOCAL_MACHINE
,
145
'SYSTEM\CurrentControlSet\Services\\'
. self::SERVICE_NAME .
'\Parameters'
,
146
Nssm::INFO_APP_PARAMETERS
147
);
148
if
($exists) {
149
return
$bearsamppRegistry->setExpandStringValue(
150
Registry::HKEY_LOCAL_MACHINE
,
151
'SYSTEM\CurrentControlSet\Services\\'
. self::SERVICE_NAME .
'\Parameters'
,
152
Nssm::INFO_APP_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
Log::error
($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
Log::debug
($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
Log::error
($this->
getName
() .
' port not valid: '
.
$port
);
209
return
false
;
210
}
211
212
$headers =
HttpClient::getHeaders
(
'127.0.0.1'
,
$port
);
213
if
(!empty($headers)) {
214
if
(
UtilString::contains
($headers[0],
'Xlight'
)) {
215
Log::debug
($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
Log::debug
($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
Log::debug
($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
Log::debug
(
'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
Log::debug
(($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
Log::error
(
'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
Log::error
(
'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
Log::debug
($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
Log::info
($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
}
413
$bearsamppLang
global $bearsamppLang
Definition
ajax.apache.php:16
$bearsamppRoot
global $bearsamppRoot
Definition
ajax.apache.php:16
BinXlight
Definition
class.bin.xlight.php:18
BinXlight\setPort
setPort($port)
Definition
class.bin.xlight.php:409
BinXlight\getExe
getExe()
Definition
class.bin.xlight.php:373
BinXlight\LOCAL_CFG_EXE
const LOCAL_CFG_EXE
Definition
class.bin.xlight.php:25
BinXlight\SERVICE_NAME
const SERVICE_NAME
Definition
class.bin.xlight.php:19
BinXlight\getUiPort
getUiPort()
Definition
class.bin.xlight.php:382
BinXlight\replaceAll
replaceAll($params)
Definition
class.bin.xlight.php:116
BinXlight\updateConfig
updateConfig($version=null, $sub=0, $showWindow=false)
Definition
class.bin.xlight.php:264
BinXlight\LOCAL_CFG_SSL_PORT
const LOCAL_CFG_SSL_PORT
Definition
class.bin.xlight.php:26
BinXlight\switchVersion
switchVersion($version, $showWindow=false)
Definition
class.bin.xlight.php:251
BinXlight\LOCAL_CFG_PORT
const LOCAL_CFG_PORT
Definition
class.bin.xlight.php:27
BinXlight\ROOT_CFG_ENABLE
const ROOT_CFG_ENABLE
Definition
class.bin.xlight.php:22
BinXlight\setVersion
setVersion($version)
Definition
class.bin.xlight.php:311
BinXlight\checkPort
checkPort($port, $showWindow=false)
Definition
class.bin.xlight.php:203
BinXlight\changePort
changePort($port, $checkUsed=false, $wbProgressBar=null)
Definition
class.bin.xlight.php:168
BinXlight\SERVICE_PARAMS
const SERVICE_PARAMS
Definition
class.bin.xlight.php:20
BinXlight\setEnable
setEnable($enabled, $showWindow=false)
Definition
class.bin.xlight.php:333
BinXlight\$log
$log
Definition
class.bin.xlight.php:30
BinXlight\$port
$port
Definition
class.bin.xlight.php:33
BinXlight\$service
$service
Definition
class.bin.xlight.php:29
BinXlight\reload
reload($id=null, $type=null)
Definition
class.bin.xlight.php:53
BinXlight\ROOT_CFG_VERSION
const ROOT_CFG_VERSION
Definition
class.bin.xlight.php:23
BinXlight\__construct
__construct($id, $type)
Definition
class.bin.xlight.php:42
BinXlight\$exe
$exe
Definition
class.bin.xlight.php:32
BinXlight\getService
getService()
Definition
class.bin.xlight.php:323
BinXlight\rebuildConf
rebuildConf()
Definition
class.bin.xlight.php:140
BinXlight\setSslPort
setSslPort($SslPort)
Definition
class.bin.xlight.php:391
BinXlight\$SslPort
$SslPort
Definition
class.bin.xlight.php:34
BinXlight\getLog
getLog()
Definition
class.bin.xlight.php:364
BinXlight\getPort
getPort()
Definition
class.bin.xlight.php:400
Config\DISABLED
const DISABLED
Definition
class.config.php:36
Config\ENABLED
const ENABLED
Definition
class.config.php:35
HttpClient\getHeaders
static getHeaders($host, $port, $ssl=false)
Definition
class.httpclient.php:254
Lang\ENABLE_BUNDLE_NOT_EXIST
const ENABLE_BUNDLE_NOT_EXIST
Definition
class.lang.php:363
Lang\BEARSAMPP_CONF_MALFORMED_ERROR
const BEARSAMPP_CONF_MALFORMED_ERROR
Definition
class.lang.php:172
Lang\ERROR_EXE_NOT_FOUND
const ERROR_EXE_NOT_FOUND
Definition
class.lang.php:154
Lang\ERROR_CONF_NOT_FOUND
const ERROR_CONF_NOT_FOUND
Definition
class.lang.php:153
Lang\BEARSAMPP_CONF_NOT_FOUND_ERROR
const BEARSAMPP_CONF_NOT_FOUND_ERROR
Definition
class.lang.php:171
Lang\PORT_NOT_USED
const PORT_NOT_USED
Definition
class.lang.php:181
Lang\ERROR_INVALID_PARAMETER
const ERROR_INVALID_PARAMETER
Definition
class.lang.php:156
Lang\ENABLE_TITLE
const ENABLE_TITLE
Definition
class.lang.php:362
Lang\PORT_NOT_USED_BY
const PORT_NOT_USED_BY
Definition
class.lang.php:182
Lang\XLIGHT
const XLIGHT
Definition
class.lang.php:135
Lang\SWITCH_VERSION_TITLE
const SWITCH_VERSION_TITLE
Definition
class.lang.php:161
Lang\CHECK_PORT_TITLE
const CHECK_PORT_TITLE
Definition
class.lang.php:179
Lang\PORT_USED_BY
const PORT_USED_BY
Definition
class.lang.php:180
Lang\ERROR_FILE_NOT_FOUND
const ERROR_FILE_NOT_FOUND
Definition
class.lang.php:155
Log\info
static info($data, $file=null)
Definition
class.log.php:627
Log\debug
static debug($data, $file=null)
Definition
class.log.php:616
Log\reloadClass
static reloadClass($classInstance)
Definition
class.log.php:670
Log\error
static error($data, $file=null)
Definition
class.log.php:650
Log\initClass
static initClass($classInstance)
Definition
class.log.php:660
Module
Definition
class.module.php:15
Module\$bearsamppConf
$bearsamppConf
Definition
class.module.php:31
Module\$version
$version
Definition
class.module.php:24
Module\$bearsamppConfRaw
$bearsamppConfRaw
Definition
class.module.php:32
Module\getName
getName()
Definition
class.module.php:201
Module\update
update($sub=0, $showWindow=false)
Definition
class.module.php:153
Module\$type
$type
Definition
class.module.php:20
Module\replace
replace($key, $value)
Definition
class.module.php:105
Module\$id
$id
Definition
class.module.php:21
Module\getVersion
getVersion()
Definition
class.module.php:210
Nssm
Definition
class.nssm.php:18
Nssm\INFO_APP_PARAMETERS
const INFO_APP_PARAMETERS
Definition
class.nssm.php:43
Nssm\SERVICE_DEMAND_START
const SERVICE_DEMAND_START
Definition
class.nssm.php:22
Path\getLogsPath
static getLogsPath($aetrayPath=false)
Definition
class.path.php:626
Registry\HKEY_LOCAL_MACHINE
const HKEY_LOCAL_MACHINE
Definition
class.registry.php:23
Util\installService
static installService($bin, $port, $syntaxCheckCmd, $showWindow=false)
Definition
class.util.php:1248
Util\removeService
static removeService($service, $name)
Definition
class.util.php:1331
Util\isValidPort
static isValidPort($port)
Definition
class.util.php:201
Util\isPortInUse
static isPortInUse($port)
Definition
class.util.php:1200
UtilString\contains
static contains($string, $search)
Definition
class.util.string.php:36
Win32Service
Definition
class.win32service.php:18
$bearsamppConfig
global $bearsamppConfig
Definition
homepage.php:41
APP_TITLE
const APP_TITLE
Definition
root.php:13
sandbox
core
classes
bins
class.bin.xlight.php
Generated by
1.17.0