Skip to content

Commit e27481f

Browse files
committed
Add support for libmemcached encryption
1 parent e622992 commit e27481f

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed

Diff for: config.m4

+16
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,22 @@ if test "$PHP_MEMCACHED" != "no"; then
321321
AC_DEFINE(HAVE_MEMCACHED_EXIST, [1], [Whether memcached_exist is defined])
322322
fi
323323

324+
AC_CACHE_CHECK([whether memcached_set_encoding_key is defined], ac_cv_have_memcached_set_encoding_key, [
325+
AC_TRY_LINK(
326+
[ #include <libmemcached/memcached.h> ],
327+
[ memcached_set_encoding_key (NULL, NULL, 0); ],
328+
[ ac_cv_have_memcached_set_encoding_key="yes" ],
329+
[ ac_cv_have_memcached_set_encoding_key="no" ]
330+
)
331+
])
332+
333+
CFLAGS="$ORIG_CFLAGS"
334+
LIBS="$ORIG_LIBS"
335+
336+
if test "$ac_cv_have_memcached_set_encoding_key" = "yes"; then
337+
AC_DEFINE(HAVE_MEMCACHED_SET_ENCODING_KEY, [1], [Whether memcached_set_encoding_key is defined])
338+
fi
339+
324340
PHP_MEMCACHED_FILES="php_memcached.c php_libmemcached_compat.c g_fmt.c"
325341

326342
if test "$PHP_SYSTEM_FASTLZ" != "no"; then

Diff for: memcached-api.php

+4
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ class Memcached {
9696

9797
const HAVE_MSGPACK;
9898

99+
const HAVE_ENCODING;
100+
99101
/**
100102
* Feature support
101103
*/
@@ -363,6 +365,8 @@ public function isPristine( ) {}
363365

364366
public function setSaslAuthData( $username, $password ) {}
365367

368+
public function setEncodingKey( $key ) {}
369+
366370
}
367371

368372
class MemcachedException extends Exception {

Diff for: package.xml

+1
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ Fixes
159159
<file role='test' name='testdata.res'/>
160160
<file role='test' name='config.inc'/>
161161
<file role='test' name='sasl_basic.phpt'/>
162+
<file role='test' name='set_encoding_key.phpt'/>
162163
<file role='test' name='getserverbykey.phpt'/>
163164
<file role='test' name='gh_155.phpt'/>
164165
<file role='test' name='get_flags.phpt'/>

Diff for: php_memcached.c

+35
Original file line numberDiff line numberDiff line change
@@ -3265,6 +3265,32 @@ static PHP_METHOD(Memcached, setSaslAuthData)
32653265
/* }}} */
32663266
#endif /* HAVE_MEMCACHED_SASL */
32673267

3268+
#ifdef HAVE_MEMCACHED_SET_ENCODING_KEY
3269+
/* {{{ Memcached::setEncodingKey(string key)
3270+
Sets AES encryption key (libmemcached 1.0.6 and higher) */
3271+
static PHP_METHOD(Memcached, setEncodingKey)
3272+
{
3273+
MEMC_METHOD_INIT_VARS;
3274+
memcached_return status;
3275+
zend_string *key;
3276+
3277+
/* "S" */
3278+
ZEND_PARSE_PARAMETERS_START(1, 1)
3279+
Z_PARAM_STR(key)
3280+
ZEND_PARSE_PARAMETERS_END();
3281+
3282+
MEMC_METHOD_FETCH_OBJECT;
3283+
3284+
status = memcached_set_encoding_key(intern->memc, ZSTR_VAL(key), ZSTR_LEN(key));
3285+
3286+
if (s_memc_status_handle_result_code(intern, status) == FAILURE) {
3287+
RETURN_FALSE;
3288+
}
3289+
RETURN_TRUE;
3290+
}
3291+
/* }}} */
3292+
#endif /* HAVE_MEMCACHED_SET_ENCODING_KEY */
3293+
32683294
/* {{{ Memcached::getResultCode()
32693295
Returns the result code from the last operation */
32703296
static PHP_METHOD(Memcached, getResultCode)
@@ -4282,6 +4308,15 @@ static void php_memc_register_constants(INIT_FUNC_ARGS)
42824308
REGISTER_MEMC_CLASS_CONST_BOOL(HAVE_MSGPACK, 0);
42834309
#endif
42844310

4311+
/*
4312+
* Indicate whether set_encoding_key is available
4313+
*/
4314+
#ifdef HAVE_MEMCACHED_SET_ENCODING_KEY
4315+
REGISTER_MEMC_CLASS_CONST_BOOL(HAVE_ENCODING, 1);
4316+
#else
4317+
REGISTER_MEMC_CLASS_CONST_BOOL(HAVE_ENCODING, 0);
4318+
#endif
4319+
42854320
#ifdef HAVE_MEMCACHED_SESSION
42864321
REGISTER_MEMC_CLASS_CONST_BOOL(HAVE_SESSION, 1);
42874322
#else

Diff for: tests/set_encoding_key.phpt

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
Test SASL authentication
3+
--SKIPIF--
4+
<?php
5+
include dirname (__FILE__) . '/config.inc';
6+
if (!extension_loaded("memcached")) die ("skip");
7+
if (!Memcached::HAVE_ENCODING) die ("skip no set_encoding_key support enabled");
8+
?>
9+
--FILE--
10+
<?php
11+
include dirname (__FILE__) . '/config.inc';
12+
13+
var_dump ($m->setEncodingKey("Hello"));
14+
15+
$key = uniqid ('encoding_test_');
16+
var_dump ($m->set ($key, 'set using encoding'));
17+
var_dump ($m->get ($key));
18+
19+
var_dump ($m->setEncodingKey(NULL));
20+
var_dump ($m->get ($key));
21+
22+
echo "OK" . PHP_EOL;
23+
?>
24+
--EXPECT--
25+
bool(true)
26+
bool(true)
27+
string(18) "set using encoding"
28+
OK

0 commit comments

Comments
 (0)