Bearsampp 2026.3.26
API documentation
Loading...
Searching...
No Matches
class.bin.memcached.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
16class BinMemcached extends Module
17{
18 const SERVICE_NAME = 'bearsamppmemcached';
19 const SERVICE_PARAMS = '-m %d -p %d -U 0 -vv';
20
21 const ROOT_CFG_ENABLE = 'memcachedEnable';
22 const ROOT_CFG_VERSION = 'memcachedVersion';
23
24 const LOCAL_CFG_EXE = 'memcachedExe';
25 const LOCAL_CFG_MEMORY = 'memcachedMemory';
26 const LOCAL_CFG_PORT = 'memcachedPort';
27
28 private $service;
29 private $log;
30
31 private $exe;
32 private $memory;
33 private $port;
34
41 public function __construct($id, $type) {
42 Util::logInitClass($this);
43 $this->reload($id, $type);
44 }
45
52 public function reload($id = null, $type = null) {
55
56 $this->name = $bearsamppLang->getValue(Lang::MEMCACHED);
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() . '/memcached.log';
63
64 if ($this->bearsamppConfRaw !== false) {
65 $this->exe = $this->symlinkPath . '/' . $this->bearsamppConfRaw[self::LOCAL_CFG_EXE];
66 $this->memory = intval($this->bearsamppConfRaw[self::LOCAL_CFG_MEMORY]);
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->memory)) {
91 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_INVALID_PARAMETER), self::LOCAL_CFG_MEMORY, $this->memory));
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->memory, $this->port));
103 $nssm->setStart(Nssm::SERVICE_DEMAND_START);
104 $nssm->setStdout($bearsamppRoot->getLogsPath() . '/memcached.out.log');
105 $nssm->setStderr($bearsamppRoot->getLogsPath() . '/memcached.err.log');
106
107 $this->service->setNssm($nssm);
108 }
109
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) {
122 case self::LOCAL_CFG_MEMORY:
123 $this->memory = intval($value);
124 break;
125 case self::LOCAL_CFG_PORT:
126 $this->port = intval($value);
127 break;
128 }
129 }
130
131 file_put_contents($this->bearsamppConf, $content);
132 }
133
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->memory, $this->port)
153 );
154 }
155
156 return false;
157 }
158
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->setPort($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
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 // Use fsockopen instead of memcache_connect to avoid file descriptor leaks in PHP 8.4+
212 $fp = @fsockopen('127.0.0.1', $port, $errno, $errstr, 1);
213 if ($fp) {
214 // Port is open, verify it's memcached by sending a simple command
215 @stream_set_timeout($fp, 1);
216 @fwrite($fp, "version\r\n");
217 $response = @fgets($fp, 128);
218 @fclose($fp);
219
220 if ($response && strpos($response, 'VERSION') === 0) {
221 // Extract version from response (format: "VERSION x.x.x")
222 $version = trim(substr($response, 8));
223 Util::logDebug($this->getName() . ' port ' . $port . ' is used by: ' . $this->getName() . ' ' . $version);
224 if ($showWindow) {
225 $bearsamppWinbinder->messageBoxInfo(
226 sprintf($bearsamppLang->getValue(Lang::PORT_USED_BY), $port, $this->getName() . ' ' . $version),
227 $boxTitle
228 );
229 }
230 return true;
231 } else {
232 // Port is open but not responding as memcached
233 Util::logDebug($this->getName() . ' port ' . $port . ' is open but not responding as memcached');
234 if ($showWindow) {
235 $bearsamppWinbinder->messageBoxWarning(
236 sprintf($bearsamppLang->getValue(Lang::PORT_NOT_USED_BY), $port),
237 $boxTitle
238 );
239 }
240 return false;
241 }
242 } else {
243 // Port is not open
244 Util::logDebug($this->getName() . ' port ' . $port . ' is not open');
245 if ($showWindow) {
246 $bearsamppWinbinder->messageBoxError(
247 sprintf($bearsamppLang->getValue(Lang::PORT_NOT_USED), $port),
248 $boxTitle
249 );
250 }
251 return false;
252 }
253 }
254
262 public function switchVersion($version, $showWindow = false) {
263 Util::logDebug('Switch ' . $this->name . ' version to ' . $version);
264 return $this->updateConfig($version, 0, $showWindow);
265 }
266
275 protected function updateConfig($version = null, $sub = 0, $showWindow = false) {
276 global $bearsamppLang, $bearsamppApps, $bearsamppWinbinder;
277
278 if (!$this->enable) {
279 return true;
280 }
281
282 $version = $version == null ? $this->version : $version;
283 Util::logDebug(($sub > 0 ? str_repeat(' ', 2 * $sub) : '') . 'Update ' . $this->name . ' ' . $version . ' config');
284
285 $boxTitle = sprintf($bearsamppLang->getValue(Lang::SWITCH_VERSION_TITLE), $this->getName(), $version);
286
287 $bearsamppConf = str_replace('memcached' . $this->getVersion(), 'memcached' . $version, $this->bearsamppConf);
288 if (!file_exists($bearsamppConf)) {
289 Util::logError('bearsampp config files not found for ' . $this->getName() . ' ' . $version);
290 if ($showWindow) {
291 $bearsamppWinbinder->messageBoxError(
292 sprintf($bearsamppLang->getValue(Lang::BEARSAMPP_CONF_NOT_FOUND_ERROR), $this->getName() . ' ' . $version),
293 $boxTitle
294 );
295 }
296 return false;
297 }
298
299 $bearsamppConfRaw = parse_ini_file($bearsamppConf);
300 if ($bearsamppConfRaw === false || !isset($bearsamppConfRaw[self::ROOT_CFG_VERSION]) || $bearsamppConfRaw[self::ROOT_CFG_VERSION] != $version) {
301 Util::logError('bearsampp config file malformed for ' . $this->getName() . ' ' . $version);
302 if ($showWindow) {
303 $bearsamppWinbinder->messageBoxError(
304 sprintf($bearsamppLang->getValue(Lang::BEARSAMPP_CONF_MALFORMED_ERROR), $this->getName() . ' ' . $version),
305 $boxTitle
306 );
307 }
308 return false;
309 }
310
311 // bearsampp.conf
312 $this->setVersion($version);
313
314 return true;
315 }
316
322 public function setVersion($version) {
323 global $bearsamppConfig;
324 $this->version = $version;
325 $bearsamppConfig->replace(self::ROOT_CFG_VERSION, $version);
326 $this->reload();
327 }
328
334 public function getService() {
335 return $this->service;
336 }
337
344 public function setEnable($enabled, $showWindow = false) {
345 global $bearsamppConfig, $bearsamppLang, $bearsamppWinbinder;
346
347 if ($enabled == Config::ENABLED && !is_dir($this->currentPath)) {
348 Util::logDebug($this->getName() . ' cannot be enabled because bundle ' . $this->getVersion() . ' does not exist in ' . $this->currentPath);
349 if ($showWindow) {
350 $bearsamppWinbinder->messageBoxError(
351 sprintf($bearsamppLang->getValue(Lang::ENABLE_BUNDLE_NOT_EXIST), $this->getName(), $this->getVersion(), $this->currentPath),
352 sprintf($bearsamppLang->getValue(Lang::ENABLE_TITLE), $this->getName())
353 );
354 }
355 $enabled = Config::DISABLED;
356 }
357
358 Util::logInfo($this->getName() . ' switched to ' . ($enabled == Config::ENABLED ? 'enabled' : 'disabled'));
359 $this->enable = $enabled == Config::ENABLED;
360 $bearsamppConfig->replace(self::ROOT_CFG_ENABLE, $enabled);
361
362 $this->reload();
363 if ($this->enable) {
364 Util::installService($this, $this->port, null, $showWindow);
365 } else {
366 Util::removeService($this->service, $this->name);
367 }
368 }
369
375 public function getLog() {
376 return $this->log;
377 }
378
384 public function getExe() {
385 return $this->exe;
386 }
387
393 public function getMemory() {
394 return $this->memory;
395 }
396
402 public function setMemory($memory) {
403 $this->replace(self::LOCAL_CFG_MEMORY, $memory);
404 }
405
411 public function getPort() {
412 return $this->port;
413 }
414
420 public function setPort($port) {
421 $this->replace(self::LOCAL_CFG_PORT, $port);
422 }
423}
global $bearsamppLang
global $bearsamppRoot
updateConfig($version=null, $sub=0, $showWindow=false)
switchVersion($version, $showWindow=false)
checkPort($port, $showWindow=false)
changePort($port, $checkUsed=false, $wbProgressBar=null)
setEnable($enabled, $showWindow=false)
reload($id=null, $type=null)
__construct($id, $type)
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 MEMCACHED
const BEARSAMPP_CONF_NOT_FOUND_ERROR
const PORT_NOT_USED
const ERROR_INVALID_PARAMETER
const ENABLE_TITLE
const PORT_NOT_USED_BY
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 removeService($service, $name)
static isValidPort($port)
static logInfo($data, $file=null)
static logDebug($data, $file=null)
static logReloadClass($classInstance)
static isPortInUse($port)
global $bearsamppConfig
Definition homepage.php:41
const APP_TITLE
Definition root.php:13