Skip to content

Commit

Permalink
Merge PR 109 - Add create_sid to session_set_save_handler and Session…
Browse files Browse the repository at this point in the history
…Handler

Allows user session handlers to create session IDs by adding an optional
7th argument to session_set_save_handler() and a create_sid() method
to SessionHandler.
  • Loading branch information
arraypad committed Jun 27, 2013
2 parents 60bbc78 + 6809c38 commit b66c14b
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 14 deletions.
35 changes: 34 additions & 1 deletion ext/session/mod_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "mod_user.h"

ps_module ps_mod_user = {
PS_MOD(user)
PS_MOD_SID(user)
};

#define SESS_ZVAL_LONG(val, a) \
Expand Down Expand Up @@ -183,6 +183,39 @@ PS_GC_FUNC(user)
FINISH;
}

PS_CREATE_SID_FUNC(user)
{
/* maintain backwards compatibility */
if (PSF(create_sid) != NULL) {
zval *args[1];
char *id = NULL;
STDVARS;

retval = ps_call_handler(PSF(create_sid), 0, NULL TSRMLS_CC);

if (retval) {
if (Z_TYPE_P(retval) == IS_STRING) {
id = estrndup(Z_STRVAL_P(retval), Z_STRLEN_P(retval));
}
zval_ptr_dtor(&retval);
}
else {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "No session id returned by function");
return NULL;
}

if (!id) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Session id must be a string");
return NULL;
}

return id;
}

/* function as defined by PS_MOD */
return php_session_create_id(mod_data, newlen TSRMLS_CC);
}

/*
* Local variables:
* tab-width: 4
Expand Down
2 changes: 1 addition & 1 deletion ext/session/mod_user.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
extern ps_module ps_mod_user;
#define ps_user_ptr &ps_mod_user

PS_FUNCS(user);
PS_FUNCS_SID(user);

#endif
16 changes: 16 additions & 0 deletions ext/session/mod_user_class.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,19 @@ PHP_METHOD(SessionHandler, gc)
RETVAL_BOOL(SUCCESS == PS(default_mod)->s_gc(&PS(mod_data), maxlifetime, &nrdels TSRMLS_CC));
}
/* }}} */

/* {{{ proto char SessionHandler::create_sid()
Wraps the old create_sid handler */
PHP_METHOD(SessionHandler, create_sid)
{
char *id;

if (zend_parse_parameters_none() == FAILURE) {
return;
}

id = PS(default_mod)->s_create_sid(&PS(mod_data), NULL TSRMLS_CC);

RETURN_STRING(id, 0);
}
/* }}} */
4 changes: 3 additions & 1 deletion ext/session/php_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,15 @@ typedef struct _php_ps_globals {
int module_number;
long cache_expire;
union {
zval *names[6];
zval *names[7];
struct {
zval *ps_open;
zval *ps_close;
zval *ps_read;
zval *ps_write;
zval *ps_destroy;
zval *ps_gc;
zval *ps_create_sid;
} name;
} mod_user_names;
int mod_user_implemented;
Expand Down Expand Up @@ -283,5 +284,6 @@ extern PHP_METHOD(SessionHandler, read);
extern PHP_METHOD(SessionHandler, write);
extern PHP_METHOD(SessionHandler, destroy);
extern PHP_METHOD(SessionHandler, gc);
extern PHP_METHOD(SessionHandler, create_sid);

#endif
29 changes: 18 additions & 11 deletions ext/session/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -1577,7 +1577,7 @@ static PHP_FUNCTION(session_module_name)
}
/* }}} */

/* {{{ proto void session_set_save_handler(string open, string close, string read, string write, string destroy, string gc)
/* {{{ proto void session_set_save_handler(string open, string close, string read, string write, string destroy, string gc, string create_sid)
Sets user-level functions */
static PHP_FUNCTION(session_set_save_handler)
{
Expand All @@ -1589,11 +1589,7 @@ static PHP_FUNCTION(session_set_save_handler)
RETURN_FALSE;
}

if (argc != 1 && argc != 2 && argc != 6) {
WRONG_PARAM_COUNT;
}

if (argc <= 2) {
if (argc > 0 && argc <= 2) {
zval *obj = NULL, *callback = NULL;
zend_uint func_name_len;
char *func_name;
Expand Down Expand Up @@ -1661,14 +1657,19 @@ static PHP_FUNCTION(session_set_save_handler)
RETURN_TRUE;
}

if (argc != 6 && argc != 7) {
WRONG_PARAM_COUNT;
}

if (zend_parse_parameters(argc TSRMLS_CC, "+", &args, &num_args) == FAILURE) {
return;
}

/* remove shutdown function */
remove_user_shutdown_function("session_shutdown", sizeof("session_shutdown") TSRMLS_CC);

for (i = 0; i < 6; i++) {
/* at this point argc can only be 6 or 7 */
for (i = 0; i < argc; i++) {
if (!zend_is_callable(*args[i], 0, &name TSRMLS_CC)) {
efree(args);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Argument %d is not a valid callback", i+1);
Expand All @@ -1682,7 +1683,7 @@ static PHP_FUNCTION(session_set_save_handler)
zend_alter_ini_entry("session.save_handler", sizeof("session.save_handler"), "user", sizeof("user")-1, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
}

for (i = 0; i < 6; i++) {
for (i = 0; i < argc; i++) {
if (PS(mod_user_names).names[i] != NULL) {
zval_ptr_dtor(&PS(mod_user_names).names[i]);
}
Expand Down Expand Up @@ -1992,13 +1993,14 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_session_void, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_session_set_save_handler, 0, 0, 6)
ZEND_BEGIN_ARG_INFO_EX(arginfo_session_set_save_handler, 0, 0, 7)
ZEND_ARG_INFO(0, open)
ZEND_ARG_INFO(0, close)
ZEND_ARG_INFO(0, read)
ZEND_ARG_INFO(0, write)
ZEND_ARG_INFO(0, destroy)
ZEND_ARG_INFO(0, gc)
ZEND_ARG_INFO(0, create_sid)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_session_cache_limiter, 0, 0, 0)
Expand Down Expand Up @@ -2041,6 +2043,9 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_session_class_gc, 0)
ZEND_ARG_INFO(0, maxlifetime)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(arginfo_session_class_create_sid, 0)
ZEND_END_ARG_INFO()
/* }}} */

/* {{{ session_functions[]
Expand Down Expand Up @@ -2078,6 +2083,7 @@ static const zend_function_entry php_session_iface_functions[] = {
PHP_ABSTRACT_ME(SessionHandlerInterface, write, arginfo_session_class_write)
PHP_ABSTRACT_ME(SessionHandlerInterface, destroy, arginfo_session_class_destroy)
PHP_ABSTRACT_ME(SessionHandlerInterface, gc, arginfo_session_class_gc)
PHP_ABSTRACT_ME(SessionHandlerInterface, create_sid, arginfo_session_class_create_sid)
{ NULL, NULL, NULL }
};
/* }}} */
Expand All @@ -2091,6 +2097,7 @@ static const zend_function_entry php_session_class_functions[] = {
PHP_ME(SessionHandler, write, arginfo_session_class_write, ZEND_ACC_PUBLIC)
PHP_ME(SessionHandler, destroy, arginfo_session_class_destroy, ZEND_ACC_PUBLIC)
PHP_ME(SessionHandler, gc, arginfo_session_class_gc, ZEND_ACC_PUBLIC)
PHP_ME(SessionHandler, create_sid, arginfo_session_class_create_sid, ZEND_ACC_PUBLIC)
{ NULL, NULL, NULL }
};
/* }}} */
Expand Down Expand Up @@ -2150,7 +2157,7 @@ static PHP_RSHUTDOWN_FUNCTION(session) /* {{{ */
php_rshutdown_session_globals(TSRMLS_C);

/* this should NOT be done in php_rshutdown_session_globals() */
for (i = 0; i < 6; i++) {
for (i = 0; i < 7; i++) {
if (PS(mod_user_names).names[i] != NULL) {
zval_ptr_dtor(&PS(mod_user_names).names[i]);
PS(mod_user_names).names[i] = NULL;
Expand All @@ -2175,7 +2182,7 @@ static PHP_GINIT_FUNCTION(ps) /* {{{ */
ps_globals->default_mod = NULL;
ps_globals->mod_user_implemented = 0;
ps_globals->mod_user_is_open = 0;
for (i = 0; i < 6; i++) {
for (i = 0; i < 7; i++) {
ps_globals->mod_user_names.names[i] = NULL;
}
ps_globals->http_session_vars = NULL;
Expand Down
4 changes: 4 additions & 0 deletions ext/session/tests/session_set_save_handler_class_002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ class MySession2 extends SessionHandler {
}
return true;
}

public function create_sid() {
return parent::create_sid();
}
}

$handler = new MySession2;
Expand Down
4 changes: 4 additions & 0 deletions ext/session/tests/session_set_save_handler_iface_001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ class MySession2 implements SessionHandlerInterface {
}
return true;
}

public function create_sid() {
return md5(mt_rand());
}
}

$handler = new MySession2;
Expand Down

0 comments on commit b66c14b

Please sign in to comment.