Skip to content

Add create_sid to session_set_save_handler and SessionHandler #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -170,6 +170,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 @@ -142,3 +142,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 @@ -1573,7 +1573,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 @@ -1585,11 +1585,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 @@ -1657,14 +1653,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 @@ -1678,7 +1679,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 @@ -1988,13 +1989,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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably just be 1 required arg actually.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't the required arguments, it's just describing the arguments that the function takes.

The logic that determines what is required and optional is in the function itself.

If only 1 parameter is passed, it checks it is a SessionHandlerInterface, then the second param is optional. Otherwise the first 6 are required and the 7th is optional.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm aware of the arguments logic, I wrote it ;)

From Zend/zend_API.h:
#define ZEND_BEGIN_ARG_INFO_EX(name, pass_rest_by_reference, return_reference, required_num_args)

It doesn't seem to break anything though.

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 @@ -2037,6 +2039,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 @@ -2074,6 +2079,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 @@ -2087,6 +2093,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 @@ -2146,7 +2153,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 @@ -2171,7 +2178,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