Skip to content

Commit 195f172

Browse files
committed
Include cloudControl introduced functions
- Cherry-pick setSaslData and configureSasl with tests - Add deprecation warning - Add vagrant
1 parent 7ad95cc commit 195f172

File tree

7 files changed

+176
-4
lines changed

7 files changed

+176
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,4 @@ tests/*/*/*.php
6969
tests/*/*/*.exp
7070
tests/*/*/*.sh
7171
tmp-php.ini
72+
.vagrant/

Vagrantfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# -*- mode: ruby -*-
2+
# vi: set ft=ruby :
3+
4+
VAGRANTFILE_API_VERSION = "2"
5+
6+
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
7+
config.vm.box = "precise"
8+
9+
config.vm.synced_folder ".", "/vagrant_data"
10+
config.vm.provision "shell", path: "script.sh"
11+
12+
end

memcached-api.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public function getDelayed( array $keys, $with_cas = null, $value_cb = null ) {}
202202
public function getDelayedByKey( $server_key, array $keys, $with_cas = null, $value_cb = null ) {}
203203

204204
public function fetch( ) {}
205-
205+
206206
public function fetchAll( ) {}
207207

208208
public function set( $key, $value, $expiration = 0, $udf_flags = 0 ) {}
@@ -250,7 +250,7 @@ public function increment( $key, $offset = 1, $initial_value = 0, $expiry = 0) {
250250
public function decrement( $key, $offset = 1, $initial_value = 0, $expiry = 0) {}
251251

252252
public function getOption( $option ) {}
253-
253+
254254
public function setOption( $option, $value ) {}
255255

256256
public function setOptions( array $options ) {}
@@ -276,7 +276,7 @@ public function getLastDisconnectedServer( ) {}
276276
public function flush( $delay = 0 ) {}
277277

278278
public function getStats( ) {}
279-
279+
280280
public function getVersion( ) {}
281281

282282
public function getResultCode( ) {}
@@ -289,6 +289,10 @@ public function isPristine( ) {}
289289

290290
public function setSaslAuthData( $username, $password ) {}
291291

292+
public function setSaslData($username, $password) {}
293+
294+
public function configureSasl($username, $password) {}
295+
292296
}
293297

294298
class MemcachedException extends Exception {

php_memcached.c

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2749,6 +2749,81 @@ static PHP_METHOD(Memcached, setSaslAuthData)
27492749
RETURN_TRUE;
27502750
}
27512751
/* }}} */
2752+
2753+
/* {{{ Memcached::setSaslData(string user, string pass)
2754+
Sets sasl credentials (the same as Memcached::setSaslAuthData, for backward compatibility only) */
2755+
static PHP_METHOD(Memcached, setSaslData)
2756+
{
2757+
zend_error(E_DEPRECATED, "setSaslData is deprecated, use setSaslAuthData instead");
2758+
2759+
MEMC_METHOD_INIT_VARS;
2760+
memcached_return status;
2761+
2762+
char *user, *pass;
2763+
int user_len, pass_len;
2764+
2765+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &user, &user_len, &pass, &pass_len) == FAILURE) {
2766+
return;
2767+
}
2768+
2769+
if (!MEMC_G(use_sasl)) {
2770+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "SASL support (memcached.use_sasl) isn't enabled in php.ini");
2771+
RETURN_FALSE;
2772+
}
2773+
2774+
MEMC_METHOD_FETCH_OBJECT;
2775+
2776+
if (!memcached_behavior_get(m_obj->memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL)) {
2777+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "SASL is only supported with binary protocol");
2778+
RETURN_FALSE;
2779+
}
2780+
m_obj->has_sasl_data = 1;
2781+
status = memcached_set_sasl_auth_data(m_obj->memc, user, pass);
2782+
2783+
if (php_memc_handle_error(i_obj, status TSRMLS_CC) < 0) {
2784+
RETURN_FALSE;
2785+
}
2786+
RETURN_TRUE;
2787+
}
2788+
/* }}} */
2789+
2790+
/* {{{ Memcached::configureSasl(string user, string pass)
2791+
Sets sasl credentials and changes the behavior to binary protocol */
2792+
static PHP_METHOD(Memcached, configureSasl)
2793+
{
2794+
zend_error(E_DEPRECATED, "configureSasl is deprecated, use setSaslAuthData instead and set binary protocol");
2795+
2796+
MEMC_METHOD_INIT_VARS;
2797+
memcached_return status;
2798+
2799+
char *user, *pass;
2800+
int user_len, pass_len;
2801+
2802+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &user, &user_len, &pass, &pass_len) == FAILURE) {
2803+
return;
2804+
}
2805+
2806+
if (!MEMC_G(use_sasl)) {
2807+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "SASL support (memcached.use_sasl) isn't enabled in php.ini");
2808+
RETURN_FALSE;
2809+
}
2810+
2811+
MEMC_METHOD_FETCH_OBJECT;
2812+
2813+
if (memcached_behavior_set(m_obj->memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1) != MEMCACHED_SUCCESS) {
2814+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to set SASL binary protocol");
2815+
RETURN_FALSE;
2816+
}
2817+
m_obj->has_sasl_data = 1;
2818+
status = memcached_set_sasl_auth_data(m_obj->memc, user, pass);
2819+
2820+
if (php_memc_handle_error(i_obj, status TSRMLS_CC) < 0) {
2821+
RETURN_FALSE;
2822+
}
2823+
RETURN_TRUE;
2824+
}
2825+
/* }}} */
2826+
27522827
#endif /* HAVE_MEMCACHED_SASL */
27532828

27542829
/* {{{ Memcached::getResultCode()
@@ -4040,6 +4115,16 @@ ZEND_BEGIN_ARG_INFO(arginfo_setSaslAuthData, 0)
40404115
ZEND_ARG_INFO(0, password)
40414116
ZEND_END_ARG_INFO()
40424117

4118+
ZEND_BEGIN_ARG_INFO(arginfo_setSaslData, 0)
4119+
ZEND_ARG_INFO(0, username)
4120+
ZEND_ARG_INFO(0, password)
4121+
ZEND_END_ARG_INFO()
4122+
4123+
ZEND_BEGIN_ARG_INFO(arginfo_configureSasl, 0)
4124+
ZEND_ARG_INFO(0, username)
4125+
ZEND_ARG_INFO(0, password)
4126+
ZEND_END_ARG_INFO()
4127+
40434128
ZEND_BEGIN_ARG_INFO(arginfo_setOption, 0)
40444129
ZEND_ARG_INFO(0, option)
40454130
ZEND_ARG_INFO(0, value)
@@ -4143,7 +4228,9 @@ static zend_function_entry memcached_class_methods[] = {
41434228
MEMC_ME(setOptions, arginfo_setOptions)
41444229
MEMC_ME(setBucket, arginfo_setBucket)
41454230
#ifdef HAVE_MEMCACHED_SASL
4146-
MEMC_ME(setSaslAuthData, arginfo_setSaslAuthData)
4231+
MEMC_ME(setSaslAuthData, arginfo_setSaslAuthData)
4232+
MEMC_ME(setSaslData, arginfo_setSaslData)
4233+
MEMC_ME(configureSasl, arginfo_configureSasl)
41474234
#endif
41484235
MEMC_ME(isPersistent, arginfo_isPersistent)
41494236
MEMC_ME(isPristine, arginfo_isPristine)

script.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
cd /tmp
2+
export LC_ALL="en_US.UTF-8"
3+
locale-gen en_US.UTF-8
4+
5+
# libmemcache
6+
apt-get install -y libsasl2-dev
7+
wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz
8+
tar -zxvf libmemcached-1.0.18.tar.gz
9+
cd libmemcached-1.0.18
10+
./configure
11+
make
12+
make install
13+
cd ..
14+
15+
# php-memcached
16+
apt-get install -y git
17+
apt-get install -y php5-dev
18+
apt-get install -y pkg-config
19+
20+
git clone /vagrant_data php-memcached
21+
cd php-memcached
22+
23+
phpize
24+
./configure
25+
make
26+
27+
apt-get install -y php5-cli
28+
#make test
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
password = yourpassword
2+
servers = yourhost
3+
username = yourusername
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--TEST--
2+
set sasl authentification
3+
--FILE--
4+
<?php
5+
$config = parse_ini_file('./tests/setSaslAuth/set_sasl_auth.ini');
6+
if(empty($config)){
7+
die('To set the authentification data please provide them in the set_sasl_auth.ini file');
8+
}
9+
10+
$server = $config['servers'];
11+
$user = $config['username'];
12+
$password = $config['password'];
13+
14+
$m = new Memcached();
15+
$m->addServer($server, 11211);
16+
17+
$m->set('visitorcount', 0);
18+
$m->increment('visitorcount');
19+
echo sprintf(">%d<\n", $m->get('visitorcount'));
20+
21+
// with authentification
22+
var_dump($m->setOption(Memcached::OPT_BINARY_PROTOCOL, true));
23+
var_dump($m->setSaslAuthData($user, $password));
24+
var_dump($m->configureSasl($user, $password));
25+
var_dump($m->setSaslData($user, $password));
26+
27+
$m->set('private_visitorcount', 0);
28+
$m->increment('private_visitorcount');
29+
echo sprintf(">%d<", $m->get('private_visitorcount'));
30+
?>
31+
--EXPECT--
32+
>0<
33+
bool(true)
34+
bool(true)
35+
bool(true)
36+
bool(true)
37+
>1<

0 commit comments

Comments
 (0)