]> granicus.if.org Git - php/commitdiff
Add create_sid to session_set_save_handler and SessionHandler
authorLeigh <leight@gmail.com>
Fri, 15 Jun 2012 14:06:47 +0000 (15:06 +0100)
committerArpad Ray <arraypad@gmail.com>
Thu, 27 Jun 2013 12:06:22 +0000 (13:06 +0100)
A lot of code already existed to allow a custom create_sid handler, but
lacked a specific implementation.

Therefore I have added a 7th (optional) argument
session_set_save_handler, to allow a user function to be supplied for
session id generation.

If a create_sid function is not supplied, the default function is
called in its absence to preserve backwards compatibility.

Likewise create_sid only added to SessionHandler class, and not the
interface to maintain backwards compatibility. If the result is not
overridden, the default is called.

ext/session/mod_user.c
ext/session/mod_user_class.c
ext/session/php_session.h
ext/session/session.c

index 57d7bd0edcdfc04a40cd294a46df5a7130f5cf92..e4261df29493f103ff49d3d3757fd15fc3131a38 100644 (file)
@@ -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)                                         \
@@ -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
index 1ed1e7bbd50ac1f75568cc0e537162321f2ae895..340c2ca97acb17558b1131ac0606eede5a45080e 100644 (file)
@@ -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;
+
+       zend_parse_parameters_none();
+
+       id = PS(default_mod)->s_create_sid(&PS(mod_data), NULL TSRMLS_CC);
+
+       RETVAL_STRING(id, 1);
+       efree(id);
+       return;
+}
+/* }}} */
index adc5e70402c2ec05370697c41c59379b57d7f4d4..210043582d9af82e978a568abb5e9cd2c0923fde 100644 (file)
@@ -138,7 +138,7 @@ 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;
@@ -146,6 +146,7 @@ typedef struct _php_ps_globals {
                        zval *ps_write;
                        zval *ps_destroy;
                        zval *ps_gc;
+                       zval *ps_create_sid;
                } name;
        } mod_user_names;
        int mod_user_implemented;
@@ -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
index 5e0565253f0d1a45969da360b6879a215c9d8913..4b93ffa74ae7430c4087a3b40e8a73dd4696c761 100644 (file)
@@ -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)
 {
@@ -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;
@@ -1661,6 +1657,10 @@ 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;
        }
@@ -1668,7 +1668,8 @@ static PHP_FUNCTION(session_set_save_handler)
        /* 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);
@@ -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]);
                }
@@ -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)
@@ -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[]
@@ -2091,6 +2096,8 @@ 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)
+/* Added to the class but not the interface, to maintain backwards compatibility */
+       PHP_ME(SessionHandler, create_sid, arginfo_session_class_create_sid, ZEND_ACC_PUBLIC)
        { NULL, NULL, NULL }
 };
 /* }}} */
@@ -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;
@@ -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;