Bearsampp
2026.7.11
Toggle main menu visibility
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
16
class
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
Log::initClass
($this);
43
$this->
reload
(
$id
,
$type
);
44
}
45
52
public
function
reload
(
$id
=
null
,
$type
=
null
) {
53
global
$bearsamppRoot
,
$bearsamppConfig
,
$bearsamppLang
;
54
Log::reloadClass
($this);
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 =
Path::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
Log::info
($this->name .
' is not enabled!'
);
72
return
;
73
}
74
if
(!is_dir($this->currentPath)) {
75
Log::error
(sprintf(
$bearsamppLang
->getValue(
Lang::ERROR_FILE_NOT_FOUND
), $this->name .
' '
. $this->version, $this->currentPath));
76
return
;
77
}
78
if
(!is_dir($this->symlinkPath)) {
79
Log::error
(sprintf(
$bearsamppLang
->getValue(
Lang::ERROR_FILE_NOT_FOUND
), $this->name .
' '
. $this->version, $this->symlinkPath));
80
return
;
81
}
82
if
(!is_file($this->bearsamppConf)) {
83
Log::error
(sprintf(
$bearsamppLang
->getValue(
Lang::ERROR_CONF_NOT_FOUND
), $this->name .
' '
. $this->version, $this->bearsamppConf));
84
return
;
85
}
86
if
(!is_file($this->exe)) {
87
Log::error
(sprintf(
$bearsamppLang
->getValue(
Lang::ERROR_EXE_NOT_FOUND
), $this->name .
' '
. $this->version, $this->exe));
88
return
;
89
}
90
if
(empty($this->memory)) {
91
Log::error
(sprintf(
$bearsamppLang
->getValue(
Lang::ERROR_INVALID_PARAMETER
), self::LOCAL_CFG_MEMORY, $this->memory));
92
return
;
93
}
94
if
(empty($this->port)) {
95
Log::error
(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(
Path::getLogsPath
() .
'/memcached.out.log'
);
105
$nssm->setStderr(
Path::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(
143
Registry::HKEY_LOCAL_MACHINE
,
144
'SYSTEM\CurrentControlSet\Services\\'
. self::SERVICE_NAME .
'\Parameters'
,
145
Nssm::INFO_APP_PARAMETERS
146
);
147
if
($exists) {
148
return
$bearsamppRegistry->setExpandStringValue(
149
Registry::HKEY_LOCAL_MACHINE
,
150
'SYSTEM\CurrentControlSet\Services\\'
. self::SERVICE_NAME .
'\Parameters'
,
151
Nssm::INFO_APP_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
Log::error
($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
Log::debug
($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
Log::error
($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
Log::debug
($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
Log::debug
($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
Log::debug
($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
Log::debug
(
'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
Log::debug
(($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
Log::error
(
'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
Log::error
(
'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
Log::debug
($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
Log::info
($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
}
424
$bearsamppLang
global $bearsamppLang
Definition
ajax.apache.php:16
$bearsamppRoot
global $bearsamppRoot
Definition
ajax.apache.php:16
$response
$response
Definition
ajax.apply.moduleconfig.php:26
BinMemcached
Definition
class.bin.memcached.php:17
BinMemcached\setPort
setPort($port)
Definition
class.bin.memcached.php:420
BinMemcached\getExe
getExe()
Definition
class.bin.memcached.php:384
BinMemcached\LOCAL_CFG_EXE
const LOCAL_CFG_EXE
Definition
class.bin.memcached.php:24
BinMemcached\getMemory
getMemory()
Definition
class.bin.memcached.php:393
BinMemcached\setMemory
setMemory($memory)
Definition
class.bin.memcached.php:402
BinMemcached\SERVICE_NAME
const SERVICE_NAME
Definition
class.bin.memcached.php:18
BinMemcached\replaceAll
replaceAll($params)
Definition
class.bin.memcached.php:115
BinMemcached\updateConfig
updateConfig($version=null, $sub=0, $showWindow=false)
Definition
class.bin.memcached.php:275
BinMemcached\LOCAL_CFG_MEMORY
const LOCAL_CFG_MEMORY
Definition
class.bin.memcached.php:25
BinMemcached\$memory
$memory
Definition
class.bin.memcached.php:32
BinMemcached\switchVersion
switchVersion($version, $showWindow=false)
Definition
class.bin.memcached.php:262
BinMemcached\LOCAL_CFG_PORT
const LOCAL_CFG_PORT
Definition
class.bin.memcached.php:26
BinMemcached\ROOT_CFG_ENABLE
const ROOT_CFG_ENABLE
Definition
class.bin.memcached.php:21
BinMemcached\setVersion
setVersion($version)
Definition
class.bin.memcached.php:322
BinMemcached\checkPort
checkPort($port, $showWindow=false)
Definition
class.bin.memcached.php:202
BinMemcached\changePort
changePort($port, $checkUsed=false, $wbProgressBar=null)
Definition
class.bin.memcached.php:167
BinMemcached\SERVICE_PARAMS
const SERVICE_PARAMS
Definition
class.bin.memcached.php:19
BinMemcached\setEnable
setEnable($enabled, $showWindow=false)
Definition
class.bin.memcached.php:344
BinMemcached\$log
$log
Definition
class.bin.memcached.php:29
BinMemcached\$port
$port
Definition
class.bin.memcached.php:33
BinMemcached\$service
$service
Definition
class.bin.memcached.php:28
BinMemcached\reload
reload($id=null, $type=null)
Definition
class.bin.memcached.php:52
BinMemcached\ROOT_CFG_VERSION
const ROOT_CFG_VERSION
Definition
class.bin.memcached.php:22
BinMemcached\__construct
__construct($id, $type)
Definition
class.bin.memcached.php:41
BinMemcached\$exe
$exe
Definition
class.bin.memcached.php:31
BinMemcached\getService
getService()
Definition
class.bin.memcached.php:334
BinMemcached\rebuildConf
rebuildConf()
Definition
class.bin.memcached.php:139
BinMemcached\getLog
getLog()
Definition
class.bin.memcached.php:375
BinMemcached\getPort
getPort()
Definition
class.bin.memcached.php:411
Config\DISABLED
const DISABLED
Definition
class.config.php:36
Config\ENABLED
const ENABLED
Definition
class.config.php:35
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\MEMCACHED
const MEMCACHED
Definition
class.lang.php:129
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\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
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.memcached.php
Generated by
1.17.0