2024.8.23
Loading...
Searching...
No Matches
class.bin.xlight.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
10/**
11 * Class BinXlight
12 *
13 * This class represents the Xlight FTP server module in the Bearsampp application.
14 * It handles the configuration, initialization, and management of the Xlight FTP server.
15 */
16class BinXlight extends Module
17{
18 const SERVICE_NAME = 'bearsamppxlight';
19 const SERVICE_PARAMS = ' -startall';
20
21 const ROOT_CFG_ENABLE = 'xlightEnable';
22 const ROOT_CFG_VERSION = 'xlightVersion';
23
24 const LOCAL_CFG_EXE = 'xlightExe';
25 const LOCAL_CFG_SSL_PORT = 'xlightSslPort';
26 const LOCAL_CFG_PORT = 'xlightPort';
27
28 private $service;
29 private $log;
30
31 private $exe;
32 private $port;
33 private $SslPort;
34
35 /**
36 * Constructs a BinXlight object and initializes the module.
37 *
38 * @param string $id The ID of the module.
39 * @param string $type The type of the module.
40 */
41 public function __construct($id, $type) {
42 Util::logInitClass($this);
43 $this->reload($id, $type);
44 }
45
46 /**
47 * Reloads the module configuration based on the provided ID and type.
48 *
49 * @param string|null $id The ID of the module. If null, the current ID is used.
50 * @param string|null $type The type of the module. If null, the current type is used.
51 */
52 public function reload($id = null, $type = null) {
55
56 $this->name = $bearsamppLang->getValue(Lang::XLIGHT);
57 $this->version = $bearsamppConfig->getRaw(self::ROOT_CFG_VERSION);
58 parent::reload($id, $type);
59
60 $this->enable = $this->enable && $bearsamppConfig->getRaw(self::ROOT_CFG_ENABLE);
61 $this->service = new Win32Service(self::SERVICE_NAME);
62 $this->log = $bearsamppRoot->getLogsPath() . '/xlight.log';
63
64 if ($this->bearsamppConfRaw !== false) {
65 $this->exe = $this->symlinkPath . '/' . $this->bearsamppConfRaw[self::LOCAL_CFG_EXE];
66 $this->SslPort = intval($this->bearsamppConfRaw[self::LOCAL_CFG_SSL_PORT]);
67 $this->port = intval($this->bearsamppConfRaw[self::LOCAL_CFG_PORT]);
68 }
69
70 if (!$this->enable) {
71 Util::logInfo($this->name . ' is not enabled!');
72 return;
73 }
74 if (!is_dir($this->currentPath)) {
75 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_FILE_NOT_FOUND), $this->name . ' ' . $this->version, $this->currentPath));
76 return;
77 }
78 if (!is_dir($this->symlinkPath)) {
79 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_FILE_NOT_FOUND), $this->name . ' ' . $this->version, $this->symlinkPath));
80 return;
81 }
82 if (!is_file($this->bearsamppConf)) {
83 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_CONF_NOT_FOUND), $this->name . ' ' . $this->version, $this->bearsamppConf));
84 return;
85 }
86 if (!is_file($this->exe)) {
87 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_EXE_NOT_FOUND), $this->name . ' ' . $this->version, $this->exe));
88 return;
89 }
90 if (empty($this->SslPort)) {
91 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_INVALID_PARAMETER), self::LOCAL_CFG_SSL_PORT, $this->SslPort));
92 return;
93 }
94 if (empty($this->port)) {
95 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_INVALID_PARAMETER), self::LOCAL_CFG_PORT, $this->port));
96 return;
97 }
98
99 $nssm = new Nssm(self::SERVICE_NAME);
100 $nssm->setDisplayName(APP_TITLE . ' ' . $this->getName());
101 $nssm->setBinPath($this->exe);
102 $nssm->setParams(sprintf(self::SERVICE_PARAMS, $this->SslPort, $this->port));
103 $nssm->setStart(Nssm::SERVICE_DEMAND_START);
104 $nssm->setStdout($bearsamppRoot->getLogsPath() . '/xlight.log');
105 $nssm->setStderr($bearsamppRoot->getLogsPath() . '/xlight.error.log');
106
107 $this->service->setNssm($nssm);
108 }
109
110 /**
111 * Replaces multiple key-value pairs in the configuration file.
112 *
113 * @param array $params An associative array of key-value pairs to replace.
114 */
115 protected function replaceAll($params) {
116 $content = file_get_contents($this->bearsamppConf);
117
118 foreach ($params as $key => $value) {
119 $content = preg_replace('|' . $key . ' = .*|', $key . ' = ' . '"' . $value.'"', $content);
120 $this->bearsamppConfRaw[$key] = $value;
121 switch ($key) {
123 $this->SslPort = intval($value);
124 break;
126 $this->port = intval($value);
127 break;
128 }
129 }
130
131 file_put_contents($this->bearsamppConf, $content);
132 }
133
134 /**
135 * Rebuilds the configuration in the Windows Registry.
136 *
137 * @return bool True if the configuration was successfully rebuilt, false otherwise.
138 */
139 public function rebuildConf() {
140 global $bearsamppRegistry;
141
142 $exists = $bearsamppRegistry->exists(
144 'SYSTEM\CurrentControlSet\Services\\' . self::SERVICE_NAME . '\Parameters',
146 );
147 if ($exists) {
148 return $bearsamppRegistry->setExpandStringValue(
150 'SYSTEM\CurrentControlSet\Services\\' . self::SERVICE_NAME . '\Parameters',
152 sprintf(self::SERVICE_PARAMS, $this->SslPort, $this->port)
153 );
154 }
155
156 return false;
157 }
158
159 /**
160 * Changes the port used by the Xlight FTP server.
161 *
162 * @param int $port The new port number.
163 * @param bool $checkUsed Whether to check if the port is already in use.
164 * @param mixed|null $wbProgressBar The progress bar object for UI updates (optional).
165 * @return bool|int True if the port was successfully changed, false if invalid, or the process using the port.
166 */
167 public function changePort($port, $checkUsed = false, $wbProgressBar = null) {
168 global $bearsamppWinbinder;
169
170 if (!Util::isValidPort($port)) {
171 Util::logError($this->getName() . ' port not valid: ' . $port);
172 return false;
173 }
174
175 $port = intval($port);
176 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
177
178 $isPortInUse = Util::isPortInUse($port);
179 if (!$checkUsed || $isPortInUse === false) {
180 // bearsampp.conf
181 $this->setSmtpPort($port);
182 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
183
184 // conf
185 $this->update();
186 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
187
188 return true;
189 }
190
191 Util::logDebug($this->getName() . ' port in used: ' . $port . ' - ' . $isPortInUse);
192 return $isPortInUse;
193 }
194
195 /**
196 * Checks if a port is used by the Xlight FTP server.
197 *
198 * @param int $port The port number to check.
199 * @param bool $showWindow Whether to show a message box with the result.
200 * @return bool True if the port is used by Xlight, false otherwise.
201 */
202 public function checkPort($port, $showWindow = false) {
203 global $bearsamppLang, $bearsamppWinbinder;
204 $boxTitle = sprintf($bearsamppLang->getValue(Lang::CHECK_PORT_TITLE), $this->getName(), $port);
205
206 if (!Util::isValidPort($port)) {
207 Util::logError($this->getName() . ' port not valid: ' . $port);
208 return false;
209 }
210
211 $headers = Util::getHeaders('127.0.0.1', $port);
212 if (!empty($headers)) {
213 if (Util::contains($headers[0], 'Xlight')) {
214 Util::logDebug($this->getName() . ' port ' . $port . ' is used by: ' . str_replace('220 ', '', $headers[0]));
215 if ($showWindow) {
216 $bearsamppWinbinder->messageBoxInfo(
217 sprintf($bearsamppLang->getValue(Lang::PORT_USED_BY), $port, str_replace('220 ', '', $headers[0])),
218 $boxTitle
219 );
220 }
221 return true;
222 }
223 Util::logDebug($this->getName() . ' port ' . $port . ' is used by another application');
224 if ($showWindow) {
225 $bearsamppWinbinder->messageBoxWarning(
226 sprintf($bearsamppLang->getValue(Lang::PORT_NOT_USED_BY), $port),
227 $boxTitle
228 );
229 }
230 } else {
231 Util::logDebug($this->getName() . ' port ' . $port . ' is not used');
232 if ($showWindow) {
233 $bearsamppWinbinder->messageBoxError(
234 sprintf($bearsamppLang->getValue(Lang::PORT_NOT_USED), $port),
235 $boxTitle
236 );
237 }
238 }
239
240 return false;
241 }
242
243 /**
244 * Switches the version of the Xlight FTP server.
245 *
246 * @param string $version The version to switch to.
247 * @param bool $showWindow Whether to show a message box with the result.
248 * @return bool True if the version was successfully switched, false otherwise.
249 */
250 public function switchVersion($version, $showWindow = false) {
251 Util::logDebug('Switch ' . $this->name . ' version to ' . $version);
252 return $this->updateConfig($version, 0, $showWindow);
253 }
254
255 /**
256 * Updates the configuration of the Xlight FTP server.
257 *
258 * @param string|null $version The version to update to. If null, the current version is used.
259 * @param int $sub The sub-level for logging indentation.
260 * @param bool $showWindow Whether to show a message box with the result.
261 * @return bool True if the configuration was successfully updated, false otherwise.
262 */
263 protected function updateConfig($version = null, $sub = 0, $showWindow = false) {
264 global $bearsamppLang, $bearsamppWinbinder;
265
266 if (!$this->enable) {
267 return true;
268 }
269
270 $version = $version == null ? $this->version : $version;
271 Util::logDebug(($sub > 0 ? str_repeat(' ', 2 * $sub) : '') . 'Update ' . $this->name . ' ' . $version . ' config');
272
273 $boxTitle = sprintf($bearsamppLang->getValue(Lang::SWITCH_VERSION_TITLE), $this->getName(), $version);
274
275 $bearsamppConf = str_replace('xlight' . $this->getVersion(), 'xlight' . $version, $this->bearsamppConf);
276 if (!file_exists($bearsamppConf)) {
277 Util::logError('bearsampp config files not found for ' . $this->getName() . ' ' . $version);
278 if ($showWindow) {
279 $bearsamppWinbinder->messageBoxError(
280 sprintf($bearsamppLang->getValue(Lang::BEARSAMPP_CONF_NOT_FOUND_ERROR), $this->getName() . ' ' . $version),
281 $boxTitle
282 );
283 }
284 return false;
285 }
286
287 $bearsamppConfRaw = parse_ini_file($bearsamppConf);
288 if ($bearsamppConfRaw === false || !isset($bearsamppConfRaw[self::ROOT_CFG_VERSION]) || $bearsamppConfRaw[self::ROOT_CFG_VERSION] != $version) {
289 Util::logError('bearsampp config file malformed for ' . $this->getName() . ' ' . $version);
290 if ($showWindow) {
291 $bearsamppWinbinder->messageBoxError(
292 sprintf($bearsamppLang->getValue(Lang::BEARSAMPP_CONF_MALFORMED_ERROR), $this->getName() . ' ' . $version),
293 $boxTitle
294 );
295 }
296 return false;
297 }
298
299 // bearsampp.conf
300 $this->setVersion($version);
301
302 return true;
303 }
304
305 /**
306 * Sets the version of the Xlight FTP server.
307 *
308 * @param string $version The version to set.
309 */
310 public function setVersion($version) {
311 global $bearsamppConfig;
312 $this->version = $version;
313 $bearsamppConfig->replace(self::ROOT_CFG_VERSION, $version);
314 $this->reload();
315 }
316
317 /**
318 * Gets the service object for the Xlight FTP server.
319 *
320 * @return Win32Service The service object.
321 */
322 public function getService() {
323 return $this->service;
324 }
325
326 /**
327 * Enables or disables the Xlight FTP server.
328 *
329 * @param bool $enabled Whether to enable or disable the server.
330 * @param bool $showWindow Whether to show a message box with the result.
331 */
332 public function setEnable($enabled, $showWindow = false) {
333 global $bearsamppConfig, $bearsamppLang, $bearsamppWinbinder;
334
335 if ($enabled == Config::ENABLED && !is_dir($this->currentPath)) {
336 Util::logDebug($this->getName() . ' cannot be enabled because bundle ' . $this->getVersion() . ' does not exist in ' . $this->currentPath);
337 if ($showWindow) {
338 $bearsamppWinbinder->messageBoxError(
339 sprintf($bearsamppLang->getValue(Lang::ENABLE_BUNDLE_NOT_EXIST), $this->getName(), $this->getVersion(), $this->currentPath),
340 sprintf($bearsamppLang->getValue(Lang::ENABLE_TITLE), $this->getName())
341 );
342 }
343 $enabled = Config::DISABLED;
344 }
345
346 Util::logInfo($this->getName() . ' switched to ' . ($enabled == Config::ENABLED ? 'enabled' : 'disabled'));
347 $this->enable = $enabled == Config::ENABLED;
348 $bearsamppConfig->replace(self::ROOT_CFG_ENABLE, $enabled);
349
350 $this->reload();
351 if ($this->enable) {
352 Util::installService($this, $this->port, null, $showWindow);
353 } else {
354 Util::removeService($this->service, $this->name);
355 }
356 }
357
358 /**
359 * Gets the log file path for the Xlight FTP server.
360 *
361 * @return string The log file path.
362 */
363 public function getLog() {
364 return $this->log;
365 }
366
367 /**
368 * Gets the executable file path for the Xlight FTP server.
369 *
370 * @return string The executable file path.
371 */
372 public function getExe() {
373 return $this->exe;
374 }
375
376 /**
377 * Gets the SSL port used by the Xlight FTP server.
378 *
379 * @return int The SSL port number.
380 */
381 public function getUiPort() {
382 return $this->SslPort;
383 }
384
385 /**
386 * Sets the SSL port for the Xlight FTP server.
387 *
388 * @param int $SslPort The SSL port number.
389 */
390 public function setSslPort($SslPort) {
391 $this->replace(self::LOCAL_CFG_SSL_PORT, $SslPort);
392 }
393
394 /**
395 * Gets the port used by the Xlight FTP server.
396 *
397 * @return int The port number.
398 */
399 public function getPort() {
400 return $this->port;
401 }
402
403 /**
404 * Sets the port for the Xlight FTP server.
405 *
406 * @param int $port The port number.
407 */
408 public function setPort($port) {
409 $this->replace(self::LOCAL_CFG_PORT, $port);
410 }
411}
global $bearsamppLang
global $bearsamppRoot
const ROOT_CFG_ENABLE
updateConfig($version=null, $sub=0, $showWindow=false)
replaceAll($params)
setVersion($version)
changePort($port, $checkUsed=false, $wbProgressBar=null)
__construct($id, $type)
const ROOT_CFG_VERSION
const LOCAL_CFG_SSL_PORT
checkPort($port, $showWindow=false)
reload($id=null, $type=null)
setSslPort($SslPort)
switchVersion($version, $showWindow=false)
setEnable($enabled, $showWindow=false)
const DISABLED
const ENABLED
const ENABLE_TITLE
const PORT_NOT_USED_BY
const ERROR_FILE_NOT_FOUND
const XLIGHT
const ERROR_INVALID_PARAMETER
const BEARSAMPP_CONF_NOT_FOUND_ERROR
const CHECK_PORT_TITLE
const ENABLE_BUNDLE_NOT_EXIST
const ERROR_CONF_NOT_FOUND
const SWITCH_VERSION_TITLE
const ERROR_EXE_NOT_FOUND
const PORT_USED_BY
const PORT_NOT_USED
const BEARSAMPP_CONF_MALFORMED_ERROR
update($sub=0, $showWindow=false)
replace($key, $value)
const SERVICE_DEMAND_START
const INFO_APP_PARAMETERS
const HKEY_LOCAL_MACHINE
static logReloadClass($classInstance)
static getHeaders($host, $port, $ssl=false)
static contains($string, $search)
static logError($data, $file=null)
static isValidPort($port)
static installService($bin, $port, $syntaxCheckCmd, $showWindow=false)
static removeService($service, $name)
static logDebug($data, $file=null)
static logInitClass($classInstance)
static isPortInUse($port)
static logInfo($data, $file=null)
global $bearsamppConfig
Definition homepage.php:26
const APP_TITLE
Definition root.php:12