Skip to content

Commit 7049eaa

Browse files
authored
Merge pull request #17 from JorgenEvens/bug/memcached-sasl-auth
[memcache] disable listing keys when using Sasl
2 parents d0676ce + 8a84a24 commit 7049eaa

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

cache.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,11 @@ function __construct($search = null) { parent::__construct('user', $search); }
5151
$memcache = new \Memcached();
5252
$memcacheVersion = 'memcached';
5353
$memcache->addServer(MEMCACHE_HOST, MEMCACHE_PORT);
54-
if (!empty(MEMCACHE_USER) && !empty(MEMCACHE_PASSWORD))
54+
if (!empty(MEMCACHE_USER) && !empty(MEMCACHE_PASSWORD)) {
55+
$memcacheVersion = 'memcached-bin';
56+
$memcache->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
5557
$memcache->setSaslAuthData(MEMCACHE_USER, MEMCACHE_PASSWORD);
58+
}
5659
$memcache_stats = $memcache->getStats();
5760
} else if (extension_loaded('memcache')) {
5861
// This extension does not support SASL authentication
@@ -203,10 +206,11 @@ function memcache_get_key($key, &$found = false) {
203206

204207
function memcache_ref() {
205208
global $memcache;
209+
global $memcacheVersion;
206210

207211
// Listing keys is not supported using the legacy Memcache module
208212
// PHP 7 and newer do not support this extension anymore
209-
if (!extension_loaded('memcached'))
213+
if ($memcacheVersion != 'memcached')
210214
return array();
211215

212216
$items = $memcache->getAllKeys();
@@ -659,6 +663,10 @@ function sort_list(&$list) {
659663
</table>
660664
</div>
661665
<?php endif; ?>
666+
<?php elseif($memcacheVersion == 'memcached-bin'): ?>
667+
<p style="text-align: center">
668+
When SASL authentication is enabled on the <a href="https://pecl.php.net/package/memcached">memcached extension</a> we can not support listing keys
669+
</p>
662670
<?php else: ?>
663671
<p style="text-align: center">
664672
Legacy <a href="https://pecl.php.net/package/memcache">memcache extension</a> does not support listing keys

demo/index.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ class CacheDemoClass {
3737

3838
$memcache = new \Memcached();
3939
$memcache->addServer($memcache_host, $memcache_port);
40-
if (!empty($memcache_user) && !empty($memcache_password))
40+
if (!empty($memcache_user) && !empty($memcache_password)) {
41+
$memcache->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
4142
$memcache->setSaslAuthData($memcache_user, $memcache_password);
43+
}
4244

4345
$memcache->add('type.array', ['abc', 'def']);
4446
$memcache->add('type.string', 'hello-world');

0 commit comments

Comments
 (0)