From 55272d3df8c8ee58c4a77eb7860f05882eaa441b Mon Sep 17 00:00:00 2001 From: Sascha Schumann Date: Fri, 17 Sep 1999 05:40:59 +0000 Subject: [PATCH] add user-level callbacks --- ChangeLog | 1 + ext/session/Makefile.am | 2 +- ext/session/mod_user.c | 188 ++++++++++++++++++++++++++++++++++++++ ext/session/mod_user.h | 39 ++++++++ ext/session/modules.c | 2 + ext/session/php_session.h | 1 + ext/session/session.c | 65 +++++++++---- 7 files changed, 280 insertions(+), 18 deletions(-) create mode 100644 ext/session/mod_user.c create mode 100644 ext/session/mod_user.h diff --git a/ChangeLog b/ChangeLog index 7fedf7dfcb..eecac500ef 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,7 @@ PHP 4.0 CHANGE LOG ChangeLog ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ?? ?? 1999, Version 4.0 Beta 3 +- Added user-level callbacks for session module (Sascha) - Added support for unknown POST content types (Zeev) - Added "wddx" serialization handler for session module (Sascha) (automatically enabled, if you compile with --with-wddx) diff --git a/ext/session/Makefile.am b/ext/session/Makefile.am index b1a8250d50..b9fd66bd67 100644 --- a/ext/session/Makefile.am +++ b/ext/session/Makefile.am @@ -2,5 +2,5 @@ INCLUDES=@INCLUDES@ -I@top_srcdir@ -I@top_srcdir@/libzend noinst_LIBRARIES=libphpext_session.a -libphpext_session_a_SOURCES=session.c mod_files.c mod_mm.c +libphpext_session_a_SOURCES=session.c mod_files.c mod_mm.c mod_user.c diff --git a/ext/session/mod_user.c b/ext/session/mod_user.c new file mode 100644 index 0000000000..2647766d33 --- /dev/null +++ b/ext/session/mod_user.c @@ -0,0 +1,188 @@ +/* + +----------------------------------------------------------------------+ + | PHP version 4.0 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997, 1998, 1999 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 2.0 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available at through the world-wide-web at | + | http://www.php.net/license/2_0.txt. | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Sascha Schumann | + +----------------------------------------------------------------------+ + */ + +#include "php.h" +#include "php_session.h" +#include "mod_user.h" + +ps_module ps_mod_user = { + PS_MOD(user) +}; + +#define ZVAL_LONG(val, a) \ +{ \ + MAKE_STD_ZVAL(a); \ + a->type = IS_LONG; \ + a->value.lval = val; \ +} + +#define ZVAL_STRING(vl, a) \ +{ \ + int len = strlen(vl); \ + MAKE_STD_ZVAL(a); \ + a->type = IS_STRING; \ + a->value.str.len = len; \ + a->value.str.val = estrndup(vl, len); \ +} + +#define ZVAL_STRINGN(vl, ln, a) \ +{ \ + MAKE_STD_ZVAL(a); \ + a->type = IS_STRING; \ + a->value.str.len = ln; \ + a->value.str.val = estrndup(vl, ln); \ +} + + +static zval *ps_call_handler(char *name, int argc, zval **argv) +{ + int i; + zval *retval = NULL; + ELS_FETCH(); + + printf("calling %s (argc=%d)\n", name, argc); + + if(name) { + zval *func; + + ZVAL_STRING(name, func); + MAKE_STD_ZVAL(retval); + + if(call_user_function(EG(function_table), NULL, func, retval, + argc, argv) == FAILURE) { + zval_dtor(retval); + efree(retval); + retval = NULL; + } + zval_dtor(func); + efree(func); + } + + for(i = 0; i < argc; i++) { + zval_dtor(argv[i]); + efree(argv[i]); + } + + return retval; +} + +#define STDVARS \ + zval *retval; \ + int ret = FAILURE; \ + ps_user *mdata = PS_GET_MOD_DATA(); \ + if(!mdata) return FAILURE + +#define PSF(a) mdata->name.ps_##a + +#define FINISH \ + if(retval) { \ + convert_to_long(retval); \ + ret = retval->value.lval; \ + zval_dtor(retval); \ + efree(retval); \ + } \ + return ret + +PS_OPEN_FUNC(user) +{ + zval *args[2]; + STDVARS; + + ZVAL_STRING(save_path, args[0]); + ZVAL_STRING(session_name, args[1]); + + retval = ps_call_handler(PSF(open), 2, args); + + FINISH; +} + +PS_CLOSE_FUNC(user) +{ + int i; + STDVARS; + + retval = ps_call_handler(PSF(close), 0, NULL); + + for(i = 0; i < 6; i++) { + efree(mdata->names[i]); + } + efree(mdata); + + PS_SET_MOD_DATA(NULL); + + FINISH; +} + +PS_READ_FUNC(user) +{ + zval *args[1]; + STDVARS; + + ZVAL_STRING(key, args[0]); + + retval = ps_call_handler(PSF(read), 1, args); + + if(retval) { + if(retval->type == IS_STRING) { + *val = estrndup(retval->value.str.val, retval->value.str.len); + *vallen = retval->value.str.len; + ret = SUCCESS; + } + zval_dtor(retval); + efree(retval); + } + + return ret; +} + +PS_WRITE_FUNC(user) +{ + zval *args[2]; + STDVARS; + + ZVAL_STRING(key, args[0]); + ZVAL_STRINGN(val, vallen, args[1]); + + retval = ps_call_handler(PSF(write), 2, args); + + FINISH; +} + +PS_DESTROY_FUNC(user) +{ + zval *args[1]; + STDVARS; + + ZVAL_STRING(key, args[0]); + + retval = ps_call_handler(PSF(destroy), 1, args); + + FINISH; +} + +PS_GC_FUNC(user) +{ + zval *args[1]; + STDVARS; + + ZVAL_LONG(maxlifetime, args[0]); + + retval = ps_call_handler(PSF(gc), 1, args); + + FINISH; +} diff --git a/ext/session/mod_user.h b/ext/session/mod_user.h new file mode 100644 index 0000000000..3d91050ab7 --- /dev/null +++ b/ext/session/mod_user.h @@ -0,0 +1,39 @@ +/* + +----------------------------------------------------------------------+ + | PHP version 4.0 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997, 1998, 1999 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 2.0 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available at through the world-wide-web at | + | http://www.php.net/license/2_0.txt. | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Sascha Schumann | + +----------------------------------------------------------------------+ + */ + +#ifndef MOD_USER_H +#define MOD_USER_H + +typedef union { + char *names[6]; + struct { + char *ps_open; + char *ps_close; + char *ps_read; + char *ps_write; + char *ps_destroy; + char *ps_gc; + } name; +} ps_user; + +extern ps_module ps_mod_user; +#define ps_user_ptr &ps_mod_user + +PS_FUNCS(user); + +#endif diff --git a/ext/session/modules.c b/ext/session/modules.c index 86613696bb..f866a42333 100644 --- a/ext/session/modules.c +++ b/ext/session/modules.c @@ -5,9 +5,11 @@ #include "mod_files.h" #include "mod_mm.h" +#include "mod_user.h" static ps_module *ps_modules[] = { ps_files_ptr, ps_mm_ptr, + ps_user_ptr, }; diff --git a/ext/session/php_session.h b/ext/session/php_session.h index f828f27932..10fd555b54 100644 --- a/ext/session/php_session.h +++ b/ext/session/php_session.h @@ -104,6 +104,7 @@ PHP_FUNCTION(session_encode); PHP_FUNCTION(session_start); PHP_FUNCTION(session_destroy); PHP_FUNCTION(session_unset); +PHP_FUNCTION(session_set_save_handler); #ifdef ZTS #define PSLS_D php_ps_globals *ps_globals diff --git a/ext/session/session.c b/ext/session/session.c index 98ba5ae03f..c6c0febc86 100644 --- a/ext/session/session.c +++ b/ext/session/session.c @@ -17,10 +17,6 @@ +----------------------------------------------------------------------+ */ -/* - * TODO: - * - userland callback functions for ps_module - */ #if !(WIN32|WINNT) #include #else @@ -61,6 +57,7 @@ function_entry session_functions[] = { PHP_FE(session_start, NULL) PHP_FE(session_destroy, NULL) PHP_FE(session_unset, NULL) + PHP_FE(session_set_save_handler, NULL) {0} }; @@ -83,6 +80,7 @@ PS_SERIALIZER_FUNCS(php); PS_SERIALIZER_FUNCS(wddx); #endif + const static ps_serializer ps_serializers[] = { #ifdef WDDX_SERIALIZER PS_SERIALIZER_ENTRY(wddx), @@ -91,20 +89,21 @@ const static ps_serializer ps_serializers[] = { {0} }; -static int php_minit_session(INIT_FUNC_ARGS); -static int php_rinit_session(INIT_FUNC_ARGS); -static int php_mshutdown_session(SHUTDOWN_FUNC_ARGS); -static int php_rshutdown_session(SHUTDOWN_FUNC_ARGS); -static void php_info_isapi(ZEND_MODULE_INFO_FUNC_ARGS); +PHP_MINIT_FUNCTION(session); +PHP_RINIT_FUNCTION(session); +PHP_MSHUTDOWN_FUNCTION(session); +PHP_RSHUTDOWN_FUNCTION(session); +PHP_MINFO_FUNCTION(session); + static void php_rinit_session_globals(PSLS_D); static void php_rshutdown_session_globals(PSLS_D); zend_module_entry session_module_entry = { "session", session_functions, - php_minit_session, php_mshutdown_session, - php_rinit_session, php_rshutdown_session, - php_info_isapi, + PHP_MINIT(session), PHP_MSHUTDOWN(session), + PHP_RINIT(session), PHP_RSHUTDOWN(session), + PHP_MINFO(session), STANDARD_MODULE_PROPERTIES, }; @@ -611,6 +610,38 @@ PHP_FUNCTION(session_module_name) } /* }}} */ +/* {{{ proto void session_set_save_handler(string open, string close, string read, string write, string destroy, string gc) + sets user-level functions */ +PHP_FUNCTION(session_set_save_handler) +{ + zval **args[6]; + int i; + ps_user *mdata; + PSLS_FETCH(); + + if(ARG_COUNT(ht) != 6 || getParametersArrayEx(6, args) == FAILURE) { + WRONG_PARAM_COUNT; + } + + if(PS(nr_open_sessions) > 0) { + RETURN_FALSE; + } + + PS(mod) = _php_find_ps_module("user" PSLS_CC); + + mdata = emalloc(sizeof *mdata); + + for(i = 0; i < 6; i++) { + convert_to_string_ex(args[i]); + mdata->names[i] = estrdup((*args[i])->value.str.val); + } + + PS(mod_data) = (void *) mdata; + + RETURN_TRUE; +} +/* }}} */ + /* {{{ proto string session_save_path([string newname]) return the current save path passed to module_name. if newname is given, the save path is replaced with newname */ PHP_FUNCTION(session_save_path) @@ -846,7 +877,7 @@ static void php_rshutdown_session_globals(PSLS_D) zend_hash_destroy(&PS(vars)); } -int php_rinit_session(INIT_FUNC_ARGS) +PHP_RINIT_FUNCTION(session) { PSLS_FETCH(); @@ -865,7 +896,7 @@ int php_rinit_session(INIT_FUNC_ARGS) return SUCCESS; } -int php_rshutdown_session(SHUTDOWN_FUNC_ARGS) +PHP_RSHUTDOWN_FUNCTION(session) { PSLS_FETCH(); @@ -877,7 +908,7 @@ int php_rshutdown_session(SHUTDOWN_FUNC_ARGS) return SUCCESS; } -int php_minit_session(INIT_FUNC_ARGS) +PHP_MINIT_FUNCTION(session) { #ifdef ZTS php_ps_globals *ps_globals; @@ -891,14 +922,14 @@ int php_minit_session(INIT_FUNC_ARGS) return SUCCESS; } -int php_mshutdown_session(SHUTDOWN_FUNC_ARGS) +PHP_MSHUTDOWN_FUNCTION(session) { UNREGISTER_INI_ENTRIES(); return SUCCESS; } -static void php_info_isapi(ZEND_MODULE_INFO_FUNC_ARGS) +PHP_MINFO_FUNCTION(session) { DISPLAY_INI_ENTRIES(); } -- 2.40.0