Bearsampp 2025.8.29
Loading...
Searching...
No Matches
BinXlight Class Reference
Inheritance 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 ()
 getCurrentPath ()
 getId ()
 getName ()
 getRelease ()
 getRootPath ()
 getSymlinkPath ()
 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
const BUNDLE_RELEASE = 'bundleRelease'

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

Protected Attributes inherited from Module
 $bearsamppConf
 $bearsamppConfRaw
 $currentPath
 $enable
 $name
 $release = 'N/A'
 $rootPath
 $symlinkPath
 $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 42 of file class.bin.xlight.php.

42 {
43 Util::logInitClass($this);
44 $this->reload($id, $type);
45 }
reload($id=null, $type=null)
static logInitClass($classInstance)

References Module\$id, Module\$type, Util\logInitClass(), and reload().

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 168 of file class.bin.xlight.php.

168 {
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 }
update($sub=0, $showWindow=false)
static logError($data, $file=null)
static isValidPort($port)
static logDebug($data, $file=null)
static isPortInUse($port)

References $port, Module\getName(), Util\isPortInUse(), Util\isValidPort(), Util\logDebug(), Util\logError(), setPort(), and Module\update().

◆ 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 203 of file class.bin.xlight.php.

203 {
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 }
global $bearsamppLang
const PORT_NOT_USED
const PORT_NOT_USED_BY
const CHECK_PORT_TITLE
const PORT_USED_BY
static getHeaders($host, $port, $ssl=false)
static contains($string, $search)

References $bearsamppLang, $port, Lang\CHECK_PORT_TITLE, Util\contains(), Util\getHeaders(), Module\getName(), Util\isValidPort(), Util\logDebug(), Util\logError(), Lang\PORT_NOT_USED, Lang\PORT_NOT_USED_BY, and Lang\PORT_USED_BY.

◆ getExe()

getExe ( )

Gets the executable file path for the Xlight FTP server.

Returns
string The executable file path.

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

373 {
374 return $this->exe;
375 }

References $exe.

◆ getLog()

getLog ( )

Gets the log file path for the Xlight FTP server.

Returns
string The log file path.

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

364 {
365 return $this->log;
366 }

References $log.

◆ getPort()

getPort ( )

Gets the port used by the Xlight FTP server.

Returns
int The port number.

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

400 {
401 return $this->port;
402 }

References $port.

◆ getService()

getService ( )

Gets the service object for the Xlight FTP server.

Returns
Win32Service The service object.

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

323 {
324 return $this->service;
325 }

References $service.

◆ getUiPort()

getUiPort ( )

Gets the SSL port used by the Xlight FTP server.

Returns
int The SSL port number.

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

382 {
383 return $this->SslPort;
384 }

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 140 of file class.bin.xlight.php.

140 {
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 }
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 53 of file class.bin.xlight.php.

53 {
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 }
global $bearsamppRoot
const ERROR_EXE_NOT_FOUND
const ERROR_CONF_NOT_FOUND
const ERROR_INVALID_PARAMETER
const XLIGHT
const ERROR_FILE_NOT_FOUND
const SERVICE_DEMAND_START
static logInfo($data, $file=null)
static logReloadClass($classInstance)
global $bearsamppConfig
Definition homepage.php:27
const APP_TITLE
Definition root.php:13

References $bearsamppConfig, $bearsamppLang, $bearsamppRoot, Module\$id, Module\$type, APP_TITLE, Lang\ERROR_CONF_NOT_FOUND, Lang\ERROR_EXE_NOT_FOUND, Lang\ERROR_FILE_NOT_FOUND, Lang\ERROR_INVALID_PARAMETER, Module\getName(), Util\logError(), Util\logInfo(), Util\logReloadClass(), Nssm\SERVICE_DEMAND_START, and Lang\XLIGHT.

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

◆ 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 116 of file class.bin.xlight.php.

116 {
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 }

◆ 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 333 of file class.bin.xlight.php.

333 {
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 }
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, Config\DISABLED, Lang\ENABLE_BUNDLE_NOT_EXIST, Lang\ENABLE_TITLE, Config\ENABLED, Module\getName(), Module\getVersion(), Util\installService(), Util\logDebug(), Util\logInfo(), reload(), and Util\removeService().

◆ setPort()

setPort ( $port)

Sets the port for the Xlight FTP server.

Parameters
int$portThe port number.

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

409 {
410 $this->replace(self::LOCAL_CFG_PORT, $port);
411 }
replace($key, $value)

References $port, and Module\replace().

Referenced by changePort().

◆ setSslPort()

setSslPort ( $SslPort)

Sets the SSL port for the Xlight FTP server.

Parameters
int$SslPortThe SSL port number.

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

391 {
392 $this->replace(self::LOCAL_CFG_SSL_PORT, $SslPort);
393 }

References $SslPort, and Module\replace().

◆ setVersion()

setVersion ( $version)

Sets the version of the Xlight FTP server.

Parameters
string$versionThe version to set.

Reimplemented from Module.

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

311 {
312 global $bearsamppConfig;
313 $this->version = $version;
314 $bearsamppConfig->replace(self::ROOT_CFG_VERSION, $version);
315 $this->reload();
316 }

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

Referenced by updateConfig().

◆ 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 251 of file class.bin.xlight.php.

251 {
252 Util::logDebug('Switch ' . $this->name . ' version to ' . $version);
253 return $this->updateConfig($version, 0, $showWindow);
254 }
updateConfig($version=null, $sub=0, $showWindow=false)

References Module\$version, Util\logDebug(), and updateConfig().

◆ 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 264 of file class.bin.xlight.php.

264 {
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 }
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, Module\getName(), Module\getVersion(), Util\logDebug(), Util\logError(), setVersion(), and Lang\SWITCH_VERSION_TITLE.

Referenced by switchVersion().

Field Documentation

◆ $exe

$exe
private

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

Referenced by getExe().

◆ $log

$log
private

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

Referenced by getLog().

◆ $port

$port
private

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

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

◆ $service

$service
private

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

Referenced by getService().

◆ $SslPort

$SslPort
private

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

Referenced by getUiPort(), and setSslPort().

◆ LOCAL_CFG_EXE

const LOCAL_CFG_EXE = 'xlightExe'

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

◆ LOCAL_CFG_PORT

const LOCAL_CFG_PORT = 'xlightPort'

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

◆ LOCAL_CFG_SSL_PORT

const LOCAL_CFG_SSL_PORT = 'xlightSslPort'

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

◆ ROOT_CFG_ENABLE

const ROOT_CFG_ENABLE = 'xlightEnable'

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

◆ ROOT_CFG_VERSION

const ROOT_CFG_VERSION = 'xlightVersion'

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

◆ SERVICE_NAME

◆ SERVICE_PARAMS

const SERVICE_PARAMS = ' -startall'

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


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