Bearsampp 2026.9.23
Loading...
Searching...
No Matches
BinXlight Class Reference
Inheritance diagram for BinXlight:
Collaboration diagram for BinXlight:

Public Member Functions

 __construct ($id, $type)
 changePort ($port, $checkUsed=false, $wbProgressBar=null)
 checkPort ($port, $showWindow=false)
 getExe ()
 getLog ()
 getPort ()
 getService ()
 getUiPort ()
 rebuildConf ()
 reload ($id=null, $type=null)
 setEnable ($enabled, $showWindow=false)
 setPort ($port)
 setSslPort ($SslPort)
 setVersion ($version)
 switchVersion ($version, $showWindow=false)
Public Member Functions inherited from Module
 __toString ()
 getId ()
 getName ()
 getRelease ()
 getType ()
 getVersion ()
 getVersionList ()
 isEnable ()
 update ($sub=0, $showWindow=false)

Data Fields

const LOCAL_CFG_EXE = 'xlightExe'
const LOCAL_CFG_PORT = 'xlightPort'
const LOCAL_CFG_SSL_PORT = 'xlightSslPort'
const ROOT_CFG_ENABLE = 'xlightEnable'
const ROOT_CFG_VERSION = 'xlightVersion'
const SERVICE_NAME = 'bearsamppxlight'
const SERVICE_PARAMS = ' -startall'
Data Fields inherited from Module
 $currentPath
 $rootPath
 $symlinkPath

Protected Member Functions

 replaceAll ($params)
 updateConfig ($version=null, $sub=0, $showWindow=false)
Protected Member Functions inherited from Module
 __construct ()
 replace ($key, $value)

Private Attributes

 $exe
 $log
 $port
 $service
 $SslPort

Additional Inherited Members

Static Public Member Functions inherited from Module
static clearMemoryCache ()
static invalidateConfigCacheForPath ($sourcePath)
Protected Attributes inherited from Module
 $bearsamppConf
 $bearsamppConfRaw
 $enable
 $name
 $release = 'N/A'
 $version

Detailed Description

Class BinXlight

This class represents the Xlight FTP server module in the Bearsampp application. It handles the configuration, initialization, and management of the Xlight FTP server.

Definition at line 17 of file class.bin.xlight.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( $id,
$type )

Constructs a BinXlight object and initializes the module.

Parameters
string$idThe ID of the module.
string$typeThe type of the module.

Definition at line 78 of file class.bin.xlight.php.

79 {
80 Log::initClass($this);
81 $this->reload($id, $type);
82 }
reload($id=null, $type=null)
static initClass($classInstance)

References Module\$id, Module\$type, Log\initClass(), and reload().

Here is the call graph for this function:

Member Function Documentation

◆ changePort()

changePort ( $port,
$checkUsed = false,
$wbProgressBar = null )

Changes the port used by the Xlight FTP server.

Parameters
int$portThe new port number.
bool$checkUsedWhether to check if the port is already in use.
mixed | null$wbProgressBarThe progress bar object for UI updates (optional).
Returns
bool|int True if the port was successfully changed, false if invalid, or the process using the port.

Definition at line 218 of file class.bin.xlight.php.

219 {
220 global $bearsamppWinbinder;
221
222 if (!Util::isValidPort($port)) {
223 Log::error($this->getName() . ' port not valid: ' . $port);
224
225 return false;
226 }
227
228 $port = intval($port);
229 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
230
231 $isPortInUse = ServiceHelper::isPortInUse($port);
232 if (!$checkUsed || $isPortInUse === false) {
233 // bearsampp.conf
234 $this->setPort($port);
235 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
236
237 // conf
238 $this->update();
239 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
240
241 return true;
242 }
243
244 Log::debug($this->getName() . ' port in used: ' . $port . ' - ' . $isPortInUse);
245
246 return $isPortInUse;
247 }
static debug($data, $file=null)
static error($data, $file=null)
update($sub=0, $showWindow=false)
static isPortInUse($port)
static isValidPort($port)

References $port, Log\debug(), Log\error(), Module\getName(), ServiceHelper\isPortInUse(), Util\isValidPort(), setPort(), and Module\update().

Here is the call graph for this function:

◆ checkPort()

checkPort ( $port,
$showWindow = false )

Checks if a port is used by the Xlight FTP server.

Parameters
int$portThe port number to check.
bool$showWindowWhether to show a message box with the result.
Returns
bool True if the port is used by Xlight, false otherwise.

Definition at line 257 of file class.bin.xlight.php.

258 {
259 global $bearsamppLang, $bearsamppWinbinder;
260 $boxTitle = sprintf($bearsamppLang->getValue(Lang::CHECK_PORT_TITLE), $this->getName(), $port);
261
262 if (!Util::isValidPort($port)) {
263 Log::error($this->getName() . ' port not valid: ' . $port);
264
265 return false;
266 }
267
269 if (!empty($headers)) {
270 if (UtilString::contains($headers[0], 'Xlight')) {
271 Log::debug($this->getName() . ' port ' . $port . ' is used by: ' . str_replace('220 ', '', $headers[0]));
272 if ($showWindow) {
273 $bearsamppWinbinder->messageBoxInfo(
274 sprintf($bearsamppLang->getValue(Lang::PORT_USED_BY), $port, str_replace('220 ', '', $headers[0])),
275 $boxTitle
276 );
277 }
278
279 return true;
280 }
281 Log::debug($this->getName() . ' port ' . $port . ' is used by another application');
282 if ($showWindow) {
283 $bearsamppWinbinder->messageBoxWarning(
284 sprintf($bearsamppLang->getValue(Lang::PORT_NOT_USED_BY), $port),
285 $boxTitle
286 );
287 }
288 } else {
289 Log::debug($this->getName() . ' port ' . $port . ' is not used');
290 if ($showWindow) {
291 $bearsamppWinbinder->messageBoxError(
292 sprintf($bearsamppLang->getValue(Lang::PORT_NOT_USED), $port),
293 $boxTitle
294 );
295 }
296 }
297
298 return false;
299 }
global $bearsamppLang
static getHeaders($host, $port, $ssl=false)
const PORT_NOT_USED
const PORT_NOT_USED_BY
const CHECK_PORT_TITLE
const PORT_USED_BY
static contains($string, $search)
const APP_LOCALHOST
Definition root.php:21

References $bearsamppLang, $port, APP_LOCALHOST, Lang\CHECK_PORT_TITLE, UtilString\contains(), Log\debug(), Log\error(), HttpClient\getHeaders(), Module\getName(), Util\isValidPort(), Lang\PORT_NOT_USED, Lang\PORT_NOT_USED_BY, and Lang\PORT_USED_BY.

Here is the call graph for this function:

◆ getExe()

getExe ( )

Gets the executable file path for the Xlight FTP server.

Returns
string The executable file path.

Definition at line 441 of file class.bin.xlight.php.

442 {
443 return $this->exe;
444 }

References $exe.

◆ getLog()

getLog ( )

Gets the log file path for the Xlight FTP server.

Returns
string The log file path.

Definition at line 431 of file class.bin.xlight.php.

432 {
433 return $this->log;
434 }

References $log.

◆ getPort()

getPort ( )

Gets the port used by the Xlight FTP server.

Returns
int The port number.

Definition at line 471 of file class.bin.xlight.php.

472 {
473 return $this->port;
474 }

References $port.

◆ getService()

getService ( )

Gets the service object for the Xlight FTP server.

Returns
Win32Service The service object.

Definition at line 388 of file class.bin.xlight.php.

389 {
390 return $this->service;
391 }

References $service.

◆ getUiPort()

getUiPort ( )

Gets the SSL port used by the Xlight FTP server.

Returns
int The SSL port number.

Definition at line 451 of file class.bin.xlight.php.

452 {
453 return $this->SslPort;
454 }

References $SslPort.

◆ rebuildConf()

rebuildConf ( )

Rebuilds the configuration in the Windows Registry.

Returns
bool True if the configuration was successfully rebuilt, false otherwise.

Definition at line 188 of file class.bin.xlight.php.

189 {
190 global $bearsamppRegistry;
191
192 $exists = $bearsamppRegistry->exists(
194 'SYSTEM\CurrentControlSet\Services\\' . self::SERVICE_NAME . '\Parameters',
196 );
197 if ($exists) {
198 return $bearsamppRegistry->setExpandStringValue(
200 'SYSTEM\CurrentControlSet\Services\\' . self::SERVICE_NAME . '\Parameters',
202 sprintf(self::SERVICE_PARAMS, $this->SslPort, $this->port)
203 );
204 }
205
206 return false;
207 }
const INFO_APP_PARAMETERS
const HKEY_LOCAL_MACHINE

References Registry\HKEY_LOCAL_MACHINE, and Nssm\INFO_APP_PARAMETERS.

◆ reload()

reload ( $id = null,
$type = null )

Reloads the module configuration based on the provided ID and type.

Parameters
string | null$idThe ID of the module. If null, the current ID is used.
string | null$typeThe type of the module. If null, the current type is used.

Reimplemented from Module.

Definition at line 90 of file class.bin.xlight.php.

91 {
93 Log::reloadClass($this);
94
95 $this->name = $bearsamppLang->getValue(Lang::XLIGHT);
96 $this->version = $bearsamppConfig->getRaw(self::ROOT_CFG_VERSION);
97 parent::reload($id, $type);
98
99 $this->enable = $this->enable && $bearsamppConfig->getRaw(self::ROOT_CFG_ENABLE);
100 $this->service = new Win32Service(self::SERVICE_NAME);
101 $this->log = Path::getLogsPath() . '/xlight.log';
102
103 if ($this->bearsamppConfRaw !== false) {
104 $this->exe = $this->symlinkPath . '/' . $this->bearsamppConfRaw[self::LOCAL_CFG_EXE];
105 $this->SslPort = intval($this->bearsamppConfRaw[self::LOCAL_CFG_SSL_PORT]);
106 $this->port = intval($this->bearsamppConfRaw[self::LOCAL_CFG_PORT]);
107 }
108
109 if (!$this->enable) {
110 Log::info($this->name . ' is not enabled!');
111
112 return;
113 }
114 if (!is_dir($this->currentPath)) {
115 Log::error(sprintf($bearsamppLang->getValue(Lang::ERROR_FILE_NOT_FOUND), $this->name . ' ' . $this->version, $this->currentPath));
116
117 return;
118 }
119 if (!is_dir($this->symlinkPath)) {
120 Log::error(sprintf($bearsamppLang->getValue(Lang::ERROR_FILE_NOT_FOUND), $this->name . ' ' . $this->version, $this->symlinkPath));
121
122 return;
123 }
124 if (!is_file($this->bearsamppConf)) {
125 Log::error(sprintf($bearsamppLang->getValue(Lang::ERROR_CONF_NOT_FOUND), $this->name . ' ' . $this->version, $this->bearsamppConf));
126
127 return;
128 }
129 if (!is_file($this->exe)) {
130 Log::error(sprintf($bearsamppLang->getValue(Lang::ERROR_EXE_NOT_FOUND), $this->name . ' ' . $this->version, $this->exe));
131
132 return;
133 }
134 if (empty($this->SslPort)) {
135 Log::error(sprintf($bearsamppLang->getValue(Lang::ERROR_INVALID_PARAMETER), self::LOCAL_CFG_SSL_PORT, $this->SslPort));
136
137 return;
138 }
139 if (empty($this->port)) {
140 Log::error(sprintf($bearsamppLang->getValue(Lang::ERROR_INVALID_PARAMETER), self::LOCAL_CFG_PORT, $this->port));
141
142 return;
143 }
144
145 $nssm = new Nssm(self::SERVICE_NAME);
146 $nssm->setDisplayName(APP_TITLE . ' ' . $this->getName());
147 $nssm->setBinPath($this->exe);
148 $nssm->setParams(sprintf(self::SERVICE_PARAMS, $this->SslPort, $this->port));
149 $nssm->setStart(Nssm::SERVICE_DEMAND_START);
150 $nssm->setStdout(Path::getLogsPath() . '/xlight.log');
151 $nssm->setStderr(Path::getLogsPath() . '/xlight.error.log');
152
153 $this->service->setNssm($nssm);
154 }
global $bearsamppRoot
const ERROR_EXE_NOT_FOUND
const ERROR_CONF_NOT_FOUND
const ERROR_INVALID_PARAMETER
const XLIGHT
const ERROR_FILE_NOT_FOUND
static info($data, $file=null)
static reloadClass($classInstance)
const SERVICE_DEMAND_START
static getLogsPath($aetrayPath=false)
global $bearsamppConfig
Definition homepage.php:46
const APP_TITLE
Definition root.php:19

References $bearsamppConfig, $bearsamppLang, $bearsamppRoot, Module\$id, Module\$type, APP_TITLE, Log\error(), Lang\ERROR_CONF_NOT_FOUND, Lang\ERROR_EXE_NOT_FOUND, Lang\ERROR_FILE_NOT_FOUND, Lang\ERROR_INVALID_PARAMETER, Path\getLogsPath(), Module\getName(), Log\info(), Log\reloadClass(), Nssm\SERVICE_DEMAND_START, and Lang\XLIGHT.

Referenced by __construct(), setEnable(), and setVersion().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ replaceAll()

replaceAll ( $params)
protected

Replaces multiple key-value pairs in the configuration file.

Parameters
array$paramsAn associative array of key-value pairs to replace.

Reimplemented from Module.

Definition at line 161 of file class.bin.xlight.php.

162 {
163 $content = file_get_contents($this->bearsamppConf);
164
165 foreach ($params as $key => $value) {
166 $content = preg_replace('|' . $key . ' = .*|', $key . ' = ' . '"' . $value . '"', $content);
167 $this->bearsamppConfRaw[$key] = $value;
168 switch ($key) {
169 case self::LOCAL_CFG_SSL_PORT:
170 $this->SslPort = intval($value);
171 break;
172 case self::LOCAL_CFG_PORT:
173 $this->port = intval($value);
174 break;
175 }
176 }
177
178 file_put_contents($this->bearsamppConf, $content);
179
180 self::invalidateConfigCacheForPath($this->bearsamppConf);
181 }
static invalidateConfigCacheForPath($sourcePath)

References Module\invalidateConfigCacheForPath().

Here is the call graph for this function:

◆ setEnable()

setEnable ( $enabled,
$showWindow = false )

Enables or disables the Xlight FTP server.

Parameters
bool$enabledWhether to enable or disable the server.
bool$showWindowWhether to show a message box with the result.

Definition at line 399 of file class.bin.xlight.php.

400 {
401 global $bearsamppConfig, $bearsamppLang, $bearsamppWinbinder;
402
403 if ($enabled == Config::ENABLED && !is_dir($this->currentPath)) {
404 Log::debug($this->getName() . ' cannot be enabled because bundle ' . $this->getVersion() . ' does not exist in ' . $this->currentPath);
405 if ($showWindow) {
406 $bearsamppWinbinder->messageBoxError(
407 sprintf($bearsamppLang->getValue(Lang::ENABLE_BUNDLE_NOT_EXIST), $this->getName(), $this->getVersion(), $this->currentPath),
408 sprintf($bearsamppLang->getValue(Lang::ENABLE_TITLE), $this->getName())
409 );
410 }
411 $enabled = Config::DISABLED;
412 }
413
414 Log::info($this->getName() . ' switched to ' . ($enabled == Config::ENABLED ? 'enabled' : 'disabled'));
415 $this->enable = $enabled == Config::ENABLED;
416 $bearsamppConfig->replace(self::ROOT_CFG_ENABLE, $enabled);
417
418 $this->reload();
419 if ($this->enable) {
420 ServiceHelper::installService($this, $this->port, null, $showWindow);
421 } else {
422 ServiceHelper::removeService($this->service, $this->name);
423 }
424 }
const DISABLED
const ENABLED
const ENABLE_BUNDLE_NOT_EXIST
const ENABLE_TITLE
static installService($bin, $port, $syntaxCheckCmd, $showWindow=false)
static removeService($service, $name)

References $bearsamppConfig, $bearsamppLang, Log\debug(), Config\DISABLED, Lang\ENABLE_BUNDLE_NOT_EXIST, Lang\ENABLE_TITLE, Config\ENABLED, Module\getName(), Module\getVersion(), Log\info(), ServiceHelper\installService(), reload(), and ServiceHelper\removeService().

Here is the call graph for this function:

◆ setPort()

setPort ( $port)

Sets the port for the Xlight FTP server.

Parameters
int$portThe port number.

Definition at line 481 of file class.bin.xlight.php.

482 {
483 $this->replace(self::LOCAL_CFG_PORT, $port);
484 }
replace($key, $value)

References $port, and Module\replace().

Referenced by changePort().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setSslPort()

setSslPort ( $SslPort)

Sets the SSL port for the Xlight FTP server.

Parameters
int$SslPortThe SSL port number.

Definition at line 461 of file class.bin.xlight.php.

462 {
463 $this->replace(self::LOCAL_CFG_SSL_PORT, $SslPort);
464 }

References $SslPort, and Module\replace().

Here is the call graph for this function:

◆ setVersion()

setVersion ( $version)

Sets the version of the Xlight FTP server.

Parameters
string$versionThe version to set.

Reimplemented from Module.

Definition at line 375 of file class.bin.xlight.php.

376 {
377 global $bearsamppConfig;
378 $this->version = $version;
379 $bearsamppConfig->replace(self::ROOT_CFG_VERSION, $version);
380 $this->reload();
381 }

References $bearsamppConfig, Module\$version, and reload().

Referenced by updateConfig().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ switchVersion()

switchVersion ( $version,
$showWindow = false )

Switches the version of the Xlight FTP server.

Parameters
string$versionThe version to switch to.
bool$showWindowWhether to show a message box with the result.
Returns
bool True if the version was successfully switched, false otherwise.

Definition at line 309 of file class.bin.xlight.php.

310 {
311 Log::debug('Switch ' . $this->name . ' version to ' . $version);
312
313 return $this->updateConfig($version, 0, $showWindow);
314 }
updateConfig($version=null, $sub=0, $showWindow=false)

References Module\$version, Log\debug(), and updateConfig().

Here is the call graph for this function:

◆ updateConfig()

updateConfig ( $version = null,
$sub = 0,
$showWindow = false )
protected

Updates the configuration of the Xlight FTP server.

Parameters
string | null$versionThe version to update to. If null, the current version is used.
int$subThe sub-level for logging indentation.
bool$showWindowWhether to show a message box with the result.
Returns
bool True if the configuration was successfully updated, false otherwise.

Reimplemented from Module.

Definition at line 325 of file class.bin.xlight.php.

326 {
327 global $bearsamppLang, $bearsamppWinbinder;
328
329 if (!$this->enable) {
330 return true;
331 }
332
333 $version = $version == null ? $this->version : $version;
334 Log::debug(($sub > 0 ? str_repeat(' ', 2 * $sub) : '') . 'Update ' . $this->name . ' ' . $version . ' config');
335
336 $boxTitle = sprintf($bearsamppLang->getValue(Lang::SWITCH_VERSION_TITLE), $this->getName(), $version);
337
338 $bearsamppConf = str_replace('xlight' . $this->getVersion(), 'xlight' . $version, $this->bearsamppConf);
339 if (!file_exists($bearsamppConf)) {
340 Log::error('bearsampp config files not found for ' . $this->getName() . ' ' . $version);
341 if ($showWindow) {
342 $bearsamppWinbinder->messageBoxError(
343 sprintf($bearsamppLang->getValue(Lang::BEARSAMPP_CONF_NOT_FOUND_ERROR), $this->getName() . ' ' . $version),
344 $boxTitle
345 );
346 }
347
348 return false;
349 }
350
351 $bearsamppConfRaw = parse_ini_file($bearsamppConf);
352 if ($bearsamppConfRaw === false || !isset($bearsamppConfRaw[self::ROOT_CFG_VERSION]) || $bearsamppConfRaw[self::ROOT_CFG_VERSION] != $version) {
353 Log::error('bearsampp config file malformed for ' . $this->getName() . ' ' . $version);
354 if ($showWindow) {
355 $bearsamppWinbinder->messageBoxError(
356 sprintf($bearsamppLang->getValue(Lang::BEARSAMPP_CONF_MALFORMED_ERROR), $this->getName() . ' ' . $version),
357 $boxTitle
358 );
359 }
360
361 return false;
362 }
363
364 // bearsampp.conf
365 $this->setVersion($version);
366
367 return true;
368 }
setVersion($version)
const BEARSAMPP_CONF_MALFORMED_ERROR
const BEARSAMPP_CONF_NOT_FOUND_ERROR
const SWITCH_VERSION_TITLE

References Module\$bearsamppConf, Module\$bearsamppConfRaw, $bearsamppLang, Module\$version, Lang\BEARSAMPP_CONF_MALFORMED_ERROR, Lang\BEARSAMPP_CONF_NOT_FOUND_ERROR, Log\debug(), Log\error(), Module\getName(), Module\getVersion(), setVersion(), and Lang\SWITCH_VERSION_TITLE.

Referenced by switchVersion().

Here is the call graph for this function:
Here is the caller graph for this function:

Field Documentation

◆ $exe

$exe
private

Definition at line 62 of file class.bin.xlight.php.

Referenced by getExe().

◆ $log

$log
private

Definition at line 57 of file class.bin.xlight.php.

Referenced by getLog().

◆ $port

$port
private

Definition at line 66 of file class.bin.xlight.php.

Referenced by changePort(), checkPort(), getPort(), and setPort().

◆ $service

$service
private

Definition at line 53 of file class.bin.xlight.php.

Referenced by getService().

◆ $SslPort

$SslPort
private

Definition at line 70 of file class.bin.xlight.php.

Referenced by getUiPort(), and setSslPort().

◆ LOCAL_CFG_EXE

const LOCAL_CFG_EXE = 'xlightExe'

Configuration key for the Xlight executable in the local configuration.

Definition at line 40 of file class.bin.xlight.php.

◆ LOCAL_CFG_PORT

const LOCAL_CFG_PORT = 'xlightPort'

Configuration key for the Xlight port in the local configuration.

Definition at line 48 of file class.bin.xlight.php.

◆ LOCAL_CFG_SSL_PORT

const LOCAL_CFG_SSL_PORT = 'xlightSslPort'

Configuration key for the Xlight SSL port in the local configuration.

Definition at line 44 of file class.bin.xlight.php.

◆ ROOT_CFG_ENABLE

const ROOT_CFG_ENABLE = 'xlightEnable'

Configuration key for the Xlight enable flag in the root configuration.

Definition at line 31 of file class.bin.xlight.php.

◆ ROOT_CFG_VERSION

const ROOT_CFG_VERSION = 'xlightVersion'

Configuration key for the Xlight version in the root configuration.

Definition at line 35 of file class.bin.xlight.php.

◆ SERVICE_NAME

◆ SERVICE_PARAMS

const SERVICE_PARAMS = ' -startall'

The command line parameters passed to the Xlight service.

Definition at line 26 of file class.bin.xlight.php.


The documentation for this class was generated from the following file: