cvs: php-src(PHP_5_2) /ext/session php_session.h session.c /ext/session/tests 008-php4.2.3.phpt 014.phpt session_decode_variation3.phpt
session_encode_error2.phpt session_encode_variation1.phpt session_encode_variation2.phpt session_encode_variation6.phpt session_encode_variation8.phpt

From: Date: Sun, 26 Apr 2009 01:20:57 +0000
Subject: cvs: php-src(PHP_5_2) /ext/session php_session.h session.c /ext/session/tests 008-php4.2.3.phpt 014.phpt session_decode_variation3.phpt
session_encode_error2.phpt session_encode_variation1.phpt session_encode_variation2.phpt session_encode_variation6.phpt session_encode_variation8.phpt
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-57266@lists.php.net to get a copy of this message
jani Sun Apr 26 01:20:57 2009 UTC Modified files: (Branch: PHP_5_2) /php-src/ext/session php_session.h session.c /php-src/ext/session/tests 008-php4.2.3.phpt 014.phpt session_decode_variation3.phpt session_encode_error2.phpt session_encode_variation1.phpt session_encode_variation2.phpt session_encode_variation6.phpt session_encode_variation8.phpt Log: MF53: Sync the session.c file structure and some bug fixes forgotten to merge.

http://cvs.php.net/viewvc.cgi/php-src/ext/session/php_session.h?r1=1.101.2.2.2.7&r2=1.101.2.2.2.8&diff_format=u Index: php-src/ext/session/php_session.h diff -u php-src/ext/session/php_session.h:1.101.2.2.2.7 php-src/ext/session/php_session.h:1.101.2.2.2.8 --- php-src/ext/session/php_session.h:1.101.2.2.2.7 Wed Dec 31 11:17:43 2008 +++ php-src/ext/session/php_session.h Sun Apr 26 01:20:56 2009 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_session.h,v 1.101.2.2.2.7 2008/12/31 11:17:43 sebastian Exp $ */ +/* $Id: php_session.h,v 1.101.2.2.2.8 2009/04/26 01:20:56 jani Exp $ */ #ifndef PHP_SESSION_H #define PHP_SESSION_H @@ -134,26 +134,6 @@ extern zend_module_entry session_module_entry; #define phpext_session_ptr &session_module_entry -PHP_FUNCTION(session_name); -PHP_FUNCTION(session_module_name); -PHP_FUNCTION(session_save_path); -PHP_FUNCTION(session_id); -PHP_FUNCTION(session_regenerate_id); -PHP_FUNCTION(session_decode); -PHP_FUNCTION(session_register); -PHP_FUNCTION(session_unregister); -PHP_FUNCTION(session_is_registered); -PHP_FUNCTION(session_encode); -PHP_FUNCTION(session_start); -PHP_FUNCTION(session_destroy); -PHP_FUNCTION(session_unset); -PHP_FUNCTION(session_set_save_handler); -PHP_FUNCTION(session_cache_expire); -PHP_FUNCTION(session_cache_limiter); -PHP_FUNCTION(session_set_cookie_params); -PHP_FUNCTION(session_get_cookie_params); -PHP_FUNCTION(session_write_close); - #ifdef ZTS #define PS(v) TSRMG(ps_globals_id, php_ps_globals *, v) #else @@ -229,7 +209,7 @@ (key_type = zend_hash_get_current_key_ex(_ht, &key, &key_length, &num_key, 0, NULL)) != HASH_KEY_NON_EXISTANT; \ zend_hash_move_forward(_ht)) { \ if (key_type == HASH_KEY_IS_LONG) { \ - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Skipping numeric key %ld.", num_key); \ + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Skipping numeric key %ld", num_key); \ continue; \ } \ key_length--; \ http://cvs.php.net/viewvc.cgi/php-src/ext/session/session.c?r1=1.417.2.8.2.46&r2=1.417.2.8.2.47&diff_format=u Index: php-src/ext/session/session.c diff -u php-src/ext/session/session.c:1.417.2.8.2.46 php-src/ext/session/session.c:1.417.2.8.2.47 --- php-src/ext/session/session.c:1.417.2.8.2.46 Wed Dec 31 11:17:43 2008 +++ php-src/ext/session/session.c Sun Apr 26 01:20:56 2009 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: session.c,v 1.417.2.8.2.46 2008/12/31 11:17:43 sebastian Exp $ */ +/* $Id: session.c,v 1.417.2.8.2.47 2009/04/26 01:20:56 jani Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -43,7 +43,7 @@ #include "ext/date/php_date.h" #include "ext/standard/php_lcg.h" #include "ext/standard/url_scanner_ex.h" -#include "ext/standard/php_rand.h" /* for RAND_MAX */ +#include "ext/standard/php_rand.h" /* for RAND_MAX */ #include "ext/standard/info.h" #include "ext/standard/php_smart_str.h" #include "ext/standard/url.h" @@ -55,268 +55,84 @@ #include "mod_mm.h" #endif -/* {{{ session_functions[] - */ -zend_function_entry session_functions[] = { - PHP_FE(session_name, NULL) - PHP_FE(session_module_name, NULL) - PHP_FE(session_save_path, NULL) - PHP_FE(session_id, NULL) - PHP_FE(session_regenerate_id, NULL) - PHP_FE(session_decode, NULL) - PHP_FE(session_register, NULL) - PHP_FE(session_unregister, NULL) - PHP_FE(session_is_registered, NULL) - PHP_FE(session_encode, NULL) - PHP_FE(session_start, NULL) - PHP_FE(session_destroy, NULL) - PHP_FE(session_unset, NULL) - PHP_FE(session_set_save_handler, NULL) - PHP_FE(session_cache_limiter, NULL) - PHP_FE(session_cache_expire, NULL) - PHP_FE(session_set_cookie_params, NULL) - PHP_FE(session_get_cookie_params, NULL) - PHP_FE(session_write_close, NULL) - PHP_FALIAS(session_commit, session_write_close, NULL) - {NULL, NULL, NULL} -}; -/* }}} */ - PHPAPI ZEND_DECLARE_MODULE_GLOBALS(ps); -static PHP_GINIT_FUNCTION(ps); -#define SESSION_CHECK_ACTIVE_STATE \ - if (PS(session_status) == php_session_active) { \ - php_error_docref(NULL TSRMLS_CC, E_WARNING, "A session is active. You cannot change the session module's ini settings at this time."); \ - return FAILURE; \ - } \ +/* *********** + * Helpers * + *********** */ -static PHP_INI_MH(OnUpdateSaveHandler) -{ - ps_module *tmp; - SESSION_CHECK_ACTIVE_STATE; +#ifdef NETWARE +# define SESS_SB_MTIME(sb) ((sb).st_mtime.tv_sec) +#else +# define SESS_SB_MTIME(sb) ((sb).st_mtime) +#endif - tmp = _php_find_ps_module(new_value TSRMLS_CC); +#define IF_SESSION_VARS() \ + if (PS(http_session_vars) && PS(http_session_vars)->type == IS_ARRAY) - if (PG(modules_activated) && !tmp) { - int err_type; - if (stage == ZEND_INI_STAGE_RUNTIME) { - err_type = E_WARNING; - } else { - err_type = E_ERROR; - } - php_error_docref(NULL TSRMLS_CC, err_type, "Cannot find save handler %s", new_value); - return FAILURE; +#define SESSION_CHECK_ACTIVE_STATE \ + if (PS(session_status) == php_session_active) { \ + php_error_docref(NULL TSRMLS_CC, E_WARNING, "A session is active. You cannot change the session module's ini settings at this time"); \ + return FAILURE; \ } - PS(mod) = tmp; - - return SUCCESS; -} -static PHP_INI_MH(OnUpdateTransSid) +/* Dispatched by RINIT and by php_session_destroy */ +static inline void php_rinit_session_globals(TSRMLS_D) /* {{{ */ { - SESSION_CHECK_ACTIVE_STATE; - - if (!strncasecmp(new_value, "on", sizeof("on"))) { - PS(use_trans_sid) = (zend_bool) 1; - } else { - PS(use_trans_sid) = (zend_bool) atoi(new_value); - } + PS(id) = NULL; + PS(session_status) = php_session_none; + PS(mod_data) = NULL; - return SUCCESS; + PS(http_session_vars) = NULL; } +/* }}} */ -static PHP_INI_MH(OnUpdateSerializer) +/* Dispatched by RSHUTDOWN and by php_session_destroy */ +static inline void php_rshutdown_session_globals(TSRMLS_D) /* {{{ */ { - const ps_serializer *tmp; - SESSION_CHECK_ACTIVE_STATE; - - tmp = _php_find_ps_serializer(new_value TSRMLS_CC); - - if (PG(modules_activated) && !tmp) { - int err_type; - if (stage == ZEND_INI_STAGE_RUNTIME) { - err_type = E_WARNING; - } else { - err_type = E_ERROR; - } - php_error_docref(NULL TSRMLS_CC, err_type, "Cannot find serialization handler %s", new_value); - return FAILURE; + if (PS(http_session_vars)) { + zval_ptr_dtor(&PS(http_session_vars)); + PS(http_session_vars) = NULL; } - PS(serializer) = tmp; - - return SUCCESS; -} - -static PHP_INI_MH(OnUpdateSaveDir) -{ - /* Only do the safemode/open_basedir check at runtime */ - if (stage == PHP_INI_STAGE_RUNTIME || stage == PHP_INI_STAGE_HTACCESS) { - char *p; - - if (memchr(new_value, '\0', new_value_length) != NULL) { - return FAILURE; - } - - if ((p = zend_memrchr(new_value, ';', new_value_length))) { - p++; - } else { - p = new_value; - } - - if (PG(safe_mode) && (!php_checkuid(p, NULL, CHECKUID_CHECK_FILE_AND_DIR))) { - return FAILURE; - } - if (PG(open_basedir) && php_check_open_basedir(p TSRMLS_CC)) { - return FAILURE; - } + if (PS(mod_data)) { + zend_try { + PS(mod)->s_close(&PS(mod_data) TSRMLS_CC); + } zend_end_try(); + } + if (PS(id)) { + efree(PS(id)); } - OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC); - return SUCCESS; } - -/* {{{ PHP_INI - */ -PHP_INI_BEGIN() - STD_PHP_INI_BOOLEAN("session.bug_compat_42", "1", PHP_INI_ALL, OnUpdateBool, bug_compat, php_ps_globals, ps_globals) - STD_PHP_INI_BOOLEAN("session.bug_compat_warn", "1", PHP_INI_ALL, OnUpdateBool, bug_compat_warn, php_ps_globals, ps_globals) - STD_PHP_INI_ENTRY("session.save_path", "", PHP_INI_ALL, OnUpdateSaveDir,save_path, php_ps_globals, ps_globals) - STD_PHP_INI_ENTRY("session.name", "PHPSESSID", PHP_INI_ALL, OnUpdateString, session_name, php_ps_globals, ps_globals) - PHP_INI_ENTRY("session.save_handler", "files", PHP_INI_ALL, OnUpdateSaveHandler) - STD_PHP_INI_BOOLEAN("session.auto_start", "0", PHP_INI_ALL, OnUpdateBool, auto_start, php_ps_globals, ps_globals) - STD_PHP_INI_ENTRY("session.gc_probability", "1", PHP_INI_ALL, OnUpdateLong, gc_probability, php_ps_globals, ps_globals) - STD_PHP_INI_ENTRY("session.gc_divisor", "100", PHP_INI_ALL, OnUpdateLong, gc_divisor, php_ps_globals, ps_globals) - STD_PHP_INI_ENTRY("session.gc_maxlifetime", "1440", PHP_INI_ALL, OnUpdateLong, gc_maxlifetime, php_ps_globals, ps_globals) - PHP_INI_ENTRY("session.serialize_handler", "php", PHP_INI_ALL, OnUpdateSerializer) - STD_PHP_INI_ENTRY("session.cookie_lifetime", "0", PHP_INI_ALL, OnUpdateLong, cookie_lifetime, php_ps_globals, ps_globals) - STD_PHP_INI_ENTRY("session.cookie_path", "/", PHP_INI_ALL, OnUpdateString, cookie_path, php_ps_globals, ps_globals) - STD_PHP_INI_ENTRY("session.cookie_domain", "", PHP_INI_ALL, OnUpdateString, cookie_domain, php_ps_globals, ps_globals) - STD_PHP_INI_BOOLEAN("session.cookie_secure", "", PHP_INI_ALL, OnUpdateBool, cookie_secure, php_ps_globals, ps_globals) - STD_PHP_INI_BOOLEAN("session.cookie_httponly", "", PHP_INI_ALL, OnUpdateBool, cookie_httponly, php_ps_globals, ps_globals) - STD_PHP_INI_BOOLEAN("session.use_cookies", "1", PHP_INI_ALL, OnUpdateBool, use_cookies, php_ps_globals, ps_globals) - STD_PHP_INI_BOOLEAN("session.use_only_cookies", "0", PHP_INI_ALL, OnUpdateBool, use_only_cookies, php_ps_globals, ps_globals) - STD_PHP_INI_ENTRY("session.referer_check", "", PHP_INI_ALL, OnUpdateString, extern_referer_chk, php_ps_globals, ps_globals) - STD_PHP_INI_ENTRY("session.entropy_file", "", PHP_INI_ALL, OnUpdateString, entropy_file, php_ps_globals, ps_globals) - STD_PHP_INI_ENTRY("session.entropy_length", "0", PHP_INI_ALL, OnUpdateLong, entropy_length, php_ps_globals, ps_globals) - STD_PHP_INI_ENTRY("session.cache_limiter", "nocache", PHP_INI_ALL, OnUpdateString, cache_limiter, php_ps_globals, ps_globals) - STD_PHP_INI_ENTRY("session.cache_expire", "180", PHP_INI_ALL, OnUpdateLong, cache_expire, php_ps_globals, ps_globals) - PHP_INI_ENTRY("session.use_trans_sid", "0", PHP_INI_ALL, OnUpdateTransSid) - STD_PHP_INI_ENTRY("session.hash_function", "0", PHP_INI_ALL, OnUpdateLong, hash_func, php_ps_globals, ps_globals) - STD_PHP_INI_ENTRY("session.hash_bits_per_character", "4", PHP_INI_ALL, OnUpdateLong, hash_bits_per_character, php_ps_globals, ps_globals) - - /* Commented out until future discussion */ - /* PHP_INI_ENTRY("session.encode_sources", "globals,track", PHP_INI_ALL, NULL) */ -PHP_INI_END() /* }}} */ -PS_SERIALIZER_FUNCS(php); -PS_SERIALIZER_FUNCS(php_binary); - -#define MAX_SERIALIZERS 10 -#define PREDEFINED_SERIALIZERS 2 - -static ps_serializer ps_serializers[MAX_SERIALIZERS + 1] = { - PS_SERIALIZER_ENTRY(php), - PS_SERIALIZER_ENTRY(php_binary) -}; - -#define MAX_MODULES 10 -#define PREDEFINED_MODULES 2 - -static ps_module *ps_modules[MAX_MODULES + 1] = { - ps_files_ptr, - ps_user_ptr -}; - -#define IF_SESSION_VARS() \ - if (PS(http_session_vars) && PS(http_session_vars)->type == IS_ARRAY) - -PHPAPI int php_session_register_serializer(const char *name, - int (*encode)(PS_SERIALIZER_ENCODE_ARGS), - int (*decode)(PS_SERIALIZER_DECODE_ARGS)) +static int php_session_destroy(TSRMLS_D) /* {{{ */ { - int ret = -1; - int i; + int retval = SUCCESS; - for (i = 0; i < MAX_SERIALIZERS; i++) { - if (ps_serializers[i].name == NULL) { - ps_serializers[i].name = name; - ps_serializers[i].encode = encode; - ps_serializers[i].decode = decode; - ps_serializers[i + 1].name = NULL; - ret = 0; - break; - } + if (PS(session_status) != php_session_active) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Trying to destroy uninitialized session"); + return FAILURE; } - return ret; -} - -PHPAPI int php_session_register_module(ps_module *ptr) -{ - int ret = -1; - int i; - - for (i = 0; i < MAX_MODULES; i++) { - if (!ps_modules[i]) { - ps_modules[i] = ptr; - ret = 0; - break; - } + if (PS(mod)->s_destroy(&PS(mod_data), PS(id) TSRMLS_CC) == FAILURE) { + retval = FAILURE; + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Session object destruction failed"); } - return ret; -} - -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(TSRMLS_D); -static void php_rshutdown_session_globals(TSRMLS_D); -static int php_session_destroy(TSRMLS_D); - -zend_module_entry session_module_entry = { - STANDARD_MODULE_HEADER, - "session", - session_functions, - PHP_MINIT(session), PHP_MSHUTDOWN(session), - PHP_RINIT(session), PHP_RSHUTDOWN(session), - PHP_MINFO(session), - NO_VERSION_YET, - PHP_MODULE_GLOBALS(ps), - PHP_GINIT(ps), - NULL, - NULL, - STANDARD_MODULE_PROPERTIES_EX -}; - -#ifdef COMPILE_DL_SESSION -ZEND_GET_MODULE(session) -#endif - -typedef struct { - char *name; - void (*func)(TSRMLS_D); -} php_session_cache_limiter_t; - -#define CACHE_LIMITER(name) _php_cache_limiter_##name -#define CACHE_LIMITER_FUNC(name) static void CACHE_LIMITER(name)(TSRMLS_D) -#define CACHE_LIMITER_ENTRY(name) { #name, CACHE_LIMITER(name) }, - -#define ADD_HEADER(a) sapi_add_header(a, strlen(a), 1); + php_rshutdown_session_globals(TSRMLS_C); + php_rinit_session_globals(TSRMLS_C); -#define MAX_STR 512 + return retval; +} +/* }}} */ -PHPAPI void php_add_session_var(char *name, size_t namelen TSRMLS_DC) +PHPAPI void php_add_session_var(char *name, size_t namelen TSRMLS_DC) /* {{{ */ { zval **sym_track = NULL; IF_SESSION_VARS() { - zend_hash_find(Z_ARRVAL_P(PS(http_session_vars)), name, namelen + 1, + zend_hash_find(Z_ARRVAL_P(PS(http_session_vars)), name, namelen + 1, (void *) &sym_track); } else { return; @@ -358,17 +174,18 @@ } } } +/* }}} */ -PHPAPI void php_set_session_var(char *name, size_t namelen, zval *state_val, php_unserialize_data_t *var_hash TSRMLS_DC) +PHPAPI void php_set_session_var(char *name, size_t namelen, zval *state_val, php_unserialize_data_t *var_hash TSRMLS_DC) /* {{{ */ { if (PG(register_globals)) { zval **old_symbol; - if (zend_hash_find(&EG(symbol_table),name,namelen+1,(void *)&old_symbol) == SUCCESS) { + if (zend_hash_find(&EG(symbol_table),name,namelen+1,(void *)&old_symbol) == SUCCESS) { if ((Z_TYPE_PP(old_symbol) == IS_ARRAY && Z_ARRVAL_PP(old_symbol) == &EG(symbol_table)) || *old_symbol == PS(http_session_vars)) { return; } - /* + /* * A global symbol with the same name exists already. That * symbol might have been created by other means (e.g. $_GET). * @@ -380,12 +197,11 @@ * session-vars won't work because of this very reason! */ - REPLACE_ZVAL_VALUE(old_symbol,state_val,1); /* * The following line will update the reference table used for - * unserialization. It is optional, because some storage + * unserialization. It is optional, because some storage * formats may not be able to represent references. */ @@ -401,13 +217,14 @@ zend_set_hash_symbol(state_val, name, namelen, PZVAL_IS_REF(state_val), 1, Z_ARRVAL_P(PS(http_session_vars))); } } +/* }}} */ -PHPAPI int php_get_session_var(char *name, size_t namelen, zval ***state_var TSRMLS_DC) +PHPAPI int php_get_session_var(char *name, size_t namelen, zval ***state_var TSRMLS_DC) /* {{{ */ { int ret = FAILURE; IF_SESSION_VARS() { - ret = zend_hash_find(Z_ARRVAL_P(PS(http_session_vars)), name, + ret = zend_hash_find(Z_ARRVAL_P(PS(http_session_vars)), name, namelen+1, (void **) state_var); /* @@ -418,7 +235,7 @@ * we prefer the same key in the global sym table */ - if (PG(register_globals) && ret == SUCCESS + if (PG(register_globals) && ret == SUCCESS && Z_TYPE_PP(*state_var) == IS_NULL) { zval **tmp; @@ -428,187 +245,17 @@ } } } - return ret; } +/* }}} */ -#define PS_BIN_NR_OF_BITS 8 -#define PS_BIN_UNDEF (1<<(PS_BIN_NR_OF_BITS-1)) -#define PS_BIN_MAX (PS_BIN_UNDEF-1) - -PS_SERIALIZER_ENCODE_FUNC(php_binary) +static void php_session_track_init(TSRMLS_D) /* {{{ */ { - smart_str buf = {0}; - php_serialize_data_t var_hash; - PS_ENCODE_VARS; - - PHP_VAR_SERIALIZE_INIT(var_hash); + zval *session_vars = NULL; - PS_ENCODE_LOOP( - if (key_length > PS_BIN_MAX) continue; - smart_str_appendc(&buf, (unsigned char) key_length); - smart_str_appendl(&buf, key, key_length); - - php_var_serialize(&buf, struc, &var_hash TSRMLS_CC); - } else { - if (key_length > PS_BIN_MAX) continue; - smart_str_appendc(&buf, (unsigned char) (key_length & PS_BIN_UNDEF)); - smart_str_appendl(&buf, key, key_length); - ); - - if (newlen) *newlen = buf.len; - *newstr = buf.c; - PHP_VAR_SERIALIZE_DESTROY(var_hash); - - return SUCCESS; -} - -PS_SERIALIZER_DECODE_FUNC(php_binary) -{ - const char *p; - char *name; - const char *endptr = val + vallen; - zval *current; - int namelen; - int has_value; - php_unserialize_data_t var_hash; - - PHP_VAR_UNSERIALIZE_INIT(var_hash); - - for (p = val; p < endptr; ) { - zval **tmp; - namelen = *p & (~PS_BIN_UNDEF); - - if (namelen < 0 || namelen > PS_BIN_MAX || (p + namelen) >= endptr) { - return FAILURE; - } - - has_value = *p & PS_BIN_UNDEF ? 0 : 1; - - name = estrndup(p + 1, namelen); - - p += namelen + 1; - - if (zend_hash_find(&EG(symbol_table), name, namelen + 1, (void **) &tmp) == SUCCESS) { - if ((Z_TYPE_PP(tmp) == IS_ARRAY && Z_ARRVAL_PP(tmp) == &EG(symbol_table)) || *tmp == PS(http_session_vars)) { - efree(name); - continue; - } - } - - if (has_value) { - ALLOC_INIT_ZVAL(current); - if (php_var_unserialize(&current, (const unsigned char **) &p, (const unsigned char *) endptr, &var_hash TSRMLS_CC)) { - php_set_session_var(name, namelen, current, &var_hash TSRMLS_CC); - } - zval_ptr_dtor(&current); - } - PS_ADD_VARL(name, namelen); - efree(name); - } - - PHP_VAR_UNSERIALIZE_DESTROY(var_hash); - - return SUCCESS; -} - -#define PS_DELIMITER '|' -#define PS_UNDEF_MARKER '!' - -PS_SERIALIZER_ENCODE_FUNC(php) -{ - smart_str buf = {0}; - php_serialize_data_t var_hash; - PS_ENCODE_VARS; - - PHP_VAR_SERIALIZE_INIT(var_hash); - - PS_ENCODE_LOOP( - smart_str_appendl(&buf, key, key_length); - if (memchr(key, PS_DELIMITER, key_length)) { - PHP_VAR_SERIALIZE_DESTROY(var_hash); - smart_str_free(&buf); - return FAILURE; - } - smart_str_appendc(&buf, PS_DELIMITER); - - php_var_serialize(&buf, struc, &var_hash TSRMLS_CC); - } else { - smart_str_appendc(&buf, PS_UNDEF_MARKER); - smart_str_appendl(&buf, key, key_length); - smart_str_appendc(&buf, PS_DELIMITER); - ); - - if (newlen) *newlen = buf.len; - *newstr = buf.c; - - PHP_VAR_SERIALIZE_DESTROY(var_hash); - return SUCCESS; -} - -PS_SERIALIZER_DECODE_FUNC(php) -{ - const char *p, *q; - char *name; - const char *endptr = val + vallen; - zval *current; - int namelen; - int has_value; - php_unserialize_data_t var_hash; - - PHP_VAR_UNSERIALIZE_INIT(var_hash); - - p = val; - - while (p < endptr) { - zval **tmp; - q = p; - while (*q != PS_DELIMITER) - if (++q >= endptr) goto break_outer_loop; - - if (p[0] == PS_UNDEF_MARKER) { - p++; - has_value = 0; - } else { - has_value = 1; - } - - namelen = q - p; - name = estrndup(p, namelen); - q++; - - if (zend_hash_find(&EG(symbol_table), name, namelen + 1, (void **) &tmp) == SUCCESS) { - if ((Z_TYPE_PP(tmp) == IS_ARRAY && Z_ARRVAL_PP(tmp) == &EG(symbol_table)) || *tmp == PS(http_session_vars)) { - goto skip; - } - } - - if (has_value) { - ALLOC_INIT_ZVAL(current); - if (php_var_unserialize(&current, (const unsigned char **) &q, (const unsigned char *) endptr, &var_hash TSRMLS_CC)) { - php_set_session_var(name, namelen, current, &var_hash TSRMLS_CC); - } - zval_ptr_dtor(&current); - } - PS_ADD_VARL(name, namelen); -skip: - efree(name); - p = q; - } -break_outer_loop: - - PHP_VAR_UNSERIALIZE_DESTROY(var_hash); - - return SUCCESS; -} - -static void php_session_track_init(TSRMLS_D) -{ - zval *session_vars = NULL; - - /* Unconditionally destroy existing arrays -- possible dirty data */ - zend_delete_global_variable("HTTP_SESSION_VARS", sizeof("HTTP_SESSION_VARS")-1 TSRMLS_CC); - zend_delete_global_variable("_SESSION", sizeof("_SESSION")-1 TSRMLS_CC); + /* Unconditionally destroy existing arrays -- possible dirty data */ + zend_delete_global_variable("HTTP_SESSION_VARS", sizeof("HTTP_SESSION_VARS")-1 TSRMLS_CC); + zend_delete_global_variable("_SESSION", sizeof("_SESSION")-1 TSRMLS_CC); if (PS(http_session_vars)) { zval_ptr_dtor(&PS(http_session_vars)); @@ -626,37 +273,38 @@ ZEND_SET_GLOBAL_VAR_WITH_LENGTH("_SESSION", sizeof("_SESSION"), PS(http_session_vars), 2, 1); } } +/* }}} */ -static char *php_session_encode(int *newlen TSRMLS_DC) +static char *php_session_encode(int *newlen TSRMLS_DC) /* {{{ */ { char *ret = NULL; IF_SESSION_VARS() { if (!PS(serializer)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown session.serialize_handler. Failed to encode session object."); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown session.serialize_handler. Failed to encode session object"); ret = NULL; - } - else if (PS(serializer)->encode(&ret, newlen TSRMLS_CC) == FAILURE) + } else if (PS(serializer)->encode(&ret, newlen TSRMLS_CC) == FAILURE) { ret = NULL; + } } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot encode non-existent session."); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot encode non-existent session"); } - return ret; } +/* }}} */ -static void php_session_decode(const char *val, int vallen TSRMLS_DC) +static void php_session_decode(const char *val, int vallen TSRMLS_DC) /* {{{ */ { if (!PS(serializer)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown session.serialize_handler. Failed to decode session object."); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown session.serialize_handler. Failed to decode session object"); return; } if (PS(serializer)->decode(val, vallen TSRMLS_CC) == FAILURE) { php_session_destroy(TSRMLS_C); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to decode session object. Session has been destroyed."); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to decode session object. Session has been destroyed"); } } - +/* }}} */ /* * Note that we cannot use the BASE64 alphabet here, because @@ -672,7 +320,7 @@ }; /* returns a pointer to the byte after the last valid character in out */ -static char *bin_to_readable(char *in, size_t inlen, char *out, char nbits) +static char *bin_to_readable(char *in, size_t inlen, char *out, char nbits) /* {{{ */ { unsigned char *p, *q; unsigned short w; @@ -708,8 +356,9 @@ *out = '\0'; return out; } +/* }}} */ -PHPAPI char *php_session_create_id(PS_CREATE_SID_ARGS) +PHPAPI char *php_session_create_id(PS_CREATE_SID_ARGS) /* {{{ */ { PHP_MD5_CTX md5_context; PHP_SHA1_CTX sha1_context; @@ -726,31 +375,31 @@ if (zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void **) &array) == SUCCESS && - Z_TYPE_PP(array) == IS_ARRAY && - zend_hash_find(Z_ARRVAL_PP(array), "REMOTE_ADDR", + Z_TYPE_PP(array) == IS_ARRAY && + zend_hash_find(Z_ARRVAL_PP(array), "REMOTE_ADDR", sizeof("REMOTE_ADDR"), (void **) &token) == SUCCESS) { remote_addr = Z_STRVAL_PP(token); } /* maximum 15+19+19+10 bytes */ - spprintf(&buf, 0, "%.15s%ld%ld%0.8F", remote_addr ? remote_addr : "", + spprintf(&buf, 0, "%.15s%ld%ld%0.8F", remote_addr ? remote_addr : "", tv.tv_sec, (long int)tv.tv_usec, php_combined_lcg(TSRMLS_C) * 10); switch (PS(hash_func)) { - case PS_HASH_FUNC_MD5: - PHP_MD5Init(&md5_context); - PHP_MD5Update(&md5_context, (unsigned char *) buf, strlen(buf)); - digest_len = 16; - break; - case PS_HASH_FUNC_SHA1: - PHP_SHA1Init(&sha1_context); - PHP_SHA1Update(&sha1_context, (unsigned char *) buf, strlen(buf)); - digest_len = 20; - break; - default: - php_error_docref(NULL TSRMLS_CC, E_ERROR, "Invalid session hash function"); - efree(buf); - return NULL; + case PS_HASH_FUNC_MD5: + PHP_MD5Init(&md5_context); + PHP_MD5Update(&md5_context, (unsigned char *) buf, strlen(buf)); + digest_len = 16; + break; + case PS_HASH_FUNC_SHA1: + PHP_SHA1Init(&sha1_context); + PHP_SHA1Update(&sha1_context, (unsigned char *) buf, strlen(buf)); + digest_len = 20; + break; + default: + php_error_docref(NULL TSRMLS_CC, E_ERROR, "Invalid session hash function"); + efree(buf); + return NULL; } if (PS(entropy_length) > 0) { @@ -767,12 +416,12 @@ if (n <= 0) break; switch (PS(hash_func)) { - case PS_HASH_FUNC_MD5: - PHP_MD5Update(&md5_context, rbuf, n); - break; - case PS_HASH_FUNC_SHA1: - PHP_SHA1Update(&sha1_context, rbuf, n); - break; + case PS_HASH_FUNC_MD5: + PHP_MD5Update(&md5_context, rbuf, n); + break; + case PS_HASH_FUNC_SHA1: + PHP_SHA1Update(&sha1_context, rbuf, n); + break; } to_read -= n; } @@ -781,12 +430,12 @@ } switch (PS(hash_func)) { - case PS_HASH_FUNC_MD5: - PHP_MD5Final(digest, &md5_context); - break; - case PS_HASH_FUNC_SHA1: - PHP_SHA1Final(digest, &sha1_context); - break; + case PS_HASH_FUNC_MD5: + PHP_MD5Final(digest, &md5_context); + break; + case PS_HASH_FUNC_SHA1: + PHP_SHA1Final(digest, &sha1_context); + break; } if (PS(hash_bits_per_character) < 4 @@ -797,143 +446,540 @@ } j = (int) (bin_to_readable((char *)digest, digest_len, buf, PS(hash_bits_per_character)) - buf); - if (newlen) + if (newlen) { *newlen = j; + } + return buf; } +/* }}} */ + +static void php_session_initialize(TSRMLS_D) /* {{{ */ +{ + char *val; + int vallen; + + /* check session name for invalid characters */ + if (PS(id) && strpbrk(PS(id), "\r\n\t <>'\"\\")) { + efree(PS(id)); + PS(id) = NULL; + } + + if (!PS(mod)) { + php_error_docref(NULL TSRMLS_CC, E_ERROR, "No storage module chosen - failed to initialize session"); + return; + } + + /* Open session handler first */ + if (PS(mod)->s_open(&PS(mod_data), PS(save_path), PS(session_name) TSRMLS_CC) == FAILURE) { + php_error_docref(NULL TSRMLS_CC, E_ERROR, "Failed to initialize storage module: %s (path: %s)", PS(mod)->s_name, PS(save_path)); + return; + } + + /* If there is no ID, use session module to create one */ + if (!PS(id)) { +new_session: + PS(id) = PS(mod)->s_create_sid(&PS(mod_data), NULL TSRMLS_CC); + if (PS(use_cookies)) { + PS(send_cookie) = 1; + } + } + + /* Read data */ + /* Question: if you create a SID here, should you also try to read data? + * I'm not sure, but while not doing so will remove one session operation + * it could prove usefull for those sites which wish to have "default" + * session information + */ + php_session_track_init(TSRMLS_C); + PS(invalid_session_id) = 0; + if (PS(mod)->s_read(&PS(mod_data), PS(id), &val, &vallen TSRMLS_CC) == SUCCESS) { + php_session_decode(val, vallen TSRMLS_CC); + efree(val); + } else if (PS(invalid_session_id)) { /* address instances where the session read fails due to an invalid id */ + PS(invalid_session_id) = 0; + efree(PS(id)); + PS(id) = NULL; + goto new_session; + } +} +/* }}} */ + +static int migrate_global(HashTable *ht, HashPosition *pos TSRMLS_DC) /* {{{ */ +{ + char *str; + uint str_len; + ulong num_key; + int n; + zval **val; + int ret = 0; + + n = zend_hash_get_current_key_ex(ht, &str, &str_len, &num_key, 0, pos); + + switch (n) { + case HASH_KEY_IS_STRING: + if (zend_hash_find(&EG(symbol_table), str, str_len, + (void **) &val) == SUCCESS + && val && Z_TYPE_PP(val) != IS_NULL) { + ZEND_SET_SYMBOL_WITH_LENGTH(ht, str, str_len, *val, + (*val)->refcount + 1 , 1); + ret = 1; + } + break; + case HASH_KEY_IS_LONG: + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "The session bug compatibility code will not " + "try to locate the global variable $%lu due to its " + "numeric nature", num_key); + break; + } + return ret; +} +/* }}} */ + +static void php_session_save_current_state(TSRMLS_D) /* {{{ */ +{ + int ret = FAILURE; + + IF_SESSION_VARS() { + if (PS(bug_compat) && !PG(register_globals)) { + HashTable *ht = Z_ARRVAL_P(PS(http_session_vars)); + HashPosition pos; + zval **val; + int do_warn = 0; + + zend_hash_internal_pointer_reset_ex(ht, &pos); + + while (zend_hash_get_current_data_ex(ht, + (void **) &val, &pos) != FAILURE) { + if (Z_TYPE_PP(val) == IS_NULL) { + if (migrate_global(ht, &pos TSRMLS_CC)) + do_warn = 1; + } + zend_hash_move_forward_ex(ht, &pos); + } + + if (do_warn && PS(bug_compat_warn)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively"); + } + } + + if (PS(mod_data)) { + char *val; + int vallen; + + val = php_session_encode(&vallen TSRMLS_CC); + if (val) { + ret = PS(mod)->s_write(&PS(mod_data), PS(id), val, vallen TSRMLS_CC); + efree(val); + } else { + ret = PS(mod)->s_write(&PS(mod_data), PS(id), "", 0 TSRMLS_CC); + } + } + + if (ret == FAILURE) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to write session data (%s). Please " + "verify that the current setting of session.save_path " + "is correct (%s)", + PS(mod)->s_name, + PS(save_path)); + } + } + + if (PS(mod_data)) { + PS(mod)->s_close(&PS(mod_data) TSRMLS_CC); + } +} +/* }}} */ + +/* ************************* + * INI Settings/Handlers * + ************************* */ + +static PHP_INI_MH(OnUpdateSaveHandler) /* {{{ */ +{ + ps_module *tmp; + SESSION_CHECK_ACTIVE_STATE; + + tmp = _php_find_ps_module(new_value TSRMLS_CC); + + if (PG(modules_activated) && !tmp) { + int err_type; + + if (stage == ZEND_INI_STAGE_RUNTIME) { + err_type = E_WARNING; + } else { + err_type = E_ERROR; + } + if (stage != ZEND_INI_STAGE_DEACTIVATE) { + php_error_docref(NULL TSRMLS_CC, err_type, "Cannot find save handler '%s'", new_value); + } + return FAILURE; + } + PS(mod) = tmp; + + return SUCCESS; +} +/* }}} */ + +static PHP_INI_MH(OnUpdateSerializer) /* {{{ */ +{ + const ps_serializer *tmp; + SESSION_CHECK_ACTIVE_STATE; + + tmp = _php_find_ps_serializer(new_value TSRMLS_CC); + + if (PG(modules_activated) && !tmp) { + int err_type; + + if (stage == ZEND_INI_STAGE_RUNTIME) { + err_type = E_WARNING; + } else { + err_type = E_ERROR; + } + if (stage != ZEND_INI_STAGE_DEACTIVATE) { + php_error_docref(NULL TSRMLS_CC, err_type, "Cannot find serialization handler '%s'", new_value); + } + return FAILURE; + } + PS(serializer) = tmp; + + return SUCCESS; +} +/* }}} */ + +static PHP_INI_MH(OnUpdateTransSid) /* {{{ */ +{ + SESSION_CHECK_ACTIVE_STATE; + + if (!strncasecmp(new_value, "on", sizeof("on"))) { + PS(use_trans_sid) = (zend_bool) 1; + } else { + PS(use_trans_sid) = (zend_bool) atoi(new_value); + } + + return SUCCESS; +} +/* }}} */ + +static PHP_INI_MH(OnUpdateSaveDir) /* {{{ */ +{ + /* Only do the safemode/open_basedir check at runtime */ + if (stage == PHP_INI_STAGE_RUNTIME || stage == PHP_INI_STAGE_HTACCESS) { + char *p; + + if (memchr(new_value, '\0', new_value_length) != NULL) { + return FAILURE; + } + + if ((p = zend_memrchr(new_value, ';', new_value_length))) { + p++; + } else { + p = new_value; + } + + if (PG(safe_mode) && (!php_checkuid(p, NULL, CHECKUID_CHECK_FILE_AND_DIR))) { + return FAILURE; + } + + if (PG(open_basedir) && php_check_open_basedir(p TSRMLS_CC)) { + return FAILURE; + } + } + OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC); + return SUCCESS; +} +/* }}} */ + +/* {{{ PHP_INI + */ +PHP_INI_BEGIN() + STD_PHP_INI_BOOLEAN("session.bug_compat_42", "1", PHP_INI_ALL, OnUpdateBool, bug_compat, php_ps_globals, ps_globals) + STD_PHP_INI_BOOLEAN("session.bug_compat_warn", "1", PHP_INI_ALL, OnUpdateBool, bug_compat_warn, php_ps_globals, ps_globals) + STD_PHP_INI_ENTRY("session.save_path", "", PHP_INI_ALL, OnUpdateSaveDir,save_path, php_ps_globals, ps_globals) + STD_PHP_INI_ENTRY("session.name", "PHPSESSID", PHP_INI_ALL, OnUpdateString, session_name, php_ps_globals, ps_globals) + PHP_INI_ENTRY("session.save_handler", "files", PHP_INI_ALL, OnUpdateSaveHandler) + STD_PHP_INI_BOOLEAN("session.auto_start", "0", PHP_INI_ALL, OnUpdateBool, auto_start, php_ps_globals, ps_globals) + STD_PHP_INI_ENTRY("session.gc_probability", "1", PHP_INI_ALL, OnUpdateLong, gc_probability, php_ps_globals, ps_globals) + STD_PHP_INI_ENTRY("session.gc_divisor", "100", PHP_INI_ALL, OnUpdateLong, gc_divisor, php_ps_globals, ps_globals) + STD_PHP_INI_ENTRY("session.gc_maxlifetime", "1440", PHP_INI_ALL, OnUpdateLong, gc_maxlifetime, php_ps_globals, ps_globals) + PHP_INI_ENTRY("session.serialize_handler", "php", PHP_INI_ALL, OnUpdateSerializer) + STD_PHP_INI_ENTRY("session.cookie_lifetime", "0", PHP_INI_ALL, OnUpdateLong, cookie_lifetime, php_ps_globals, ps_globals) + STD_PHP_INI_ENTRY("session.cookie_path", "/", PHP_INI_ALL, OnUpdateString, cookie_path, php_ps_globals, ps_globals) + STD_PHP_INI_ENTRY("session.cookie_domain", "", PHP_INI_ALL, OnUpdateString, cookie_domain, php_ps_globals, ps_globals) + STD_PHP_INI_BOOLEAN("session.cookie_secure", "", PHP_INI_ALL, OnUpdateBool, cookie_secure, php_ps_globals, ps_globals) + STD_PHP_INI_BOOLEAN("session.cookie_httponly", "", PHP_INI_ALL, OnUpdateBool, cookie_httponly, php_ps_globals, ps_globals) + STD_PHP_INI_BOOLEAN("session.use_cookies", "1", PHP_INI_ALL, OnUpdateBool, use_cookies, php_ps_globals, ps_globals) + STD_PHP_INI_BOOLEAN("session.use_only_cookies", "0", PHP_INI_ALL, OnUpdateBool, use_only_cookies, php_ps_globals, ps_globals) + STD_PHP_INI_ENTRY("session.referer_check", "", PHP_INI_ALL, OnUpdateString, extern_referer_chk, php_ps_globals, ps_globals) + STD_PHP_INI_ENTRY("session.entropy_file", "", PHP_INI_ALL, OnUpdateString, entropy_file, php_ps_globals, ps_globals) + STD_PHP_INI_ENTRY("session.entropy_length", "0", PHP_INI_ALL, OnUpdateLong, entropy_length, php_ps_globals, ps_globals) + STD_PHP_INI_ENTRY("session.cache_limiter", "nocache", PHP_INI_ALL, OnUpdateString, cache_limiter, php_ps_globals, ps_globals) + STD_PHP_INI_ENTRY("session.cache_expire", "180", PHP_INI_ALL, OnUpdateLong, cache_expire, php_ps_globals, ps_globals) + PHP_INI_ENTRY("session.use_trans_sid", "0", PHP_INI_ALL, OnUpdateTransSid) + STD_PHP_INI_ENTRY("session.hash_function", "0", PHP_INI_ALL, OnUpdateLong, hash_func, php_ps_globals, ps_globals) + STD_PHP_INI_ENTRY("session.hash_bits_per_character", "4", PHP_INI_ALL, OnUpdateLong, hash_bits_per_character, php_ps_globals, ps_globals) + + /* Commented out until future discussion */ + /* PHP_INI_ENTRY("session.encode_sources", "globals,track", PHP_INI_ALL, NULL) */ +PHP_INI_END() +/* }}} */ + +/* *************** + * Serializers * + *************** */ + +#define PS_BIN_NR_OF_BITS 8 +#define PS_BIN_UNDEF (1<<(PS_BIN_NR_OF_BITS-1)) +#define PS_BIN_MAX (PS_BIN_UNDEF-1) + +PS_SERIALIZER_ENCODE_FUNC(php_binary) /* {{{ */ +{ + smart_str buf = {0}; + php_serialize_data_t var_hash; + PS_ENCODE_VARS; + + PHP_VAR_SERIALIZE_INIT(var_hash); + + PS_ENCODE_LOOP( + if (key_length > PS_BIN_MAX) continue; + smart_str_appendc(&buf, (unsigned char) key_length); + smart_str_appendl(&buf, key, key_length); + php_var_serialize(&buf, struc, &var_hash TSRMLS_CC); + } else { + if (key_length > PS_BIN_MAX) continue; + smart_str_appendc(&buf, (unsigned char) (key_length & PS_BIN_UNDEF)); + smart_str_appendl(&buf, key, key_length); + ); + + if (newlen) { + *newlen = buf.len; + } + smart_str_0(&buf); + *newstr = buf.c; + PHP_VAR_SERIALIZE_DESTROY(var_hash); + + return SUCCESS; +} +/* }}} */ + +PS_SERIALIZER_DECODE_FUNC(php_binary) /* {{{ */ +{ + const char *p; + char *name; + const char *endptr = val + vallen; + zval *current; + int namelen; + int has_value; + php_unserialize_data_t var_hash; + + PHP_VAR_UNSERIALIZE_INIT(var_hash); + + for (p = val; p < endptr; ) { + zval **tmp; + namelen = ((unsigned char)(*p)) & (~PS_BIN_UNDEF); + + if (namelen < 0 || namelen > PS_BIN_MAX || (p + namelen) >= endptr) { + return FAILURE; + } + + has_value = *p & PS_BIN_UNDEF ? 0 : 1; + + name = estrndup(p + 1, namelen); + + p += namelen + 1; + + if (zend_hash_find(&EG(symbol_table), name, namelen + 1, (void **) &tmp) == SUCCESS) { + if ((Z_TYPE_PP(tmp) == IS_ARRAY && Z_ARRVAL_PP(tmp) == &EG(symbol_table)) || *tmp == PS(http_session_vars)) { + efree(name); + continue; + } + } + + if (has_value) { + ALLOC_INIT_ZVAL(current); + if (php_var_unserialize(&current, (const unsigned char **) &p, (const unsigned char *) endptr, &var_hash TSRMLS_CC)) { + php_set_session_var(name, namelen, current, &var_hash TSRMLS_CC); + } + zval_ptr_dtor(&current); + } + PS_ADD_VARL(name, namelen); + efree(name); + } + + PHP_VAR_UNSERIALIZE_DESTROY(var_hash); + + return SUCCESS; +} +/* }}} */ + +#define PS_DELIMITER '|' +#define PS_UNDEF_MARKER '!' + +PS_SERIALIZER_ENCODE_FUNC(php) /* {{{ */ +{ + smart_str buf = {0}; + php_serialize_data_t var_hash; + PS_ENCODE_VARS; + + PHP_VAR_SERIALIZE_INIT(var_hash); + + PS_ENCODE_LOOP( + smart_str_appendl(&buf, key, key_length); + if (memchr(key, PS_DELIMITER, key_length)) { + PHP_VAR_SERIALIZE_DESTROY(var_hash); + smart_str_free(&buf); + return FAILURE; + } + smart_str_appendc(&buf, PS_DELIMITER); + + php_var_serialize(&buf, struc, &var_hash TSRMLS_CC); + } else { + smart_str_appendc(&buf, PS_UNDEF_MARKER); + smart_str_appendl(&buf, key, key_length); + smart_str_appendc(&buf, PS_DELIMITER); + ); + + if (newlen) { + *newlen = buf.len; + } + smart_str_0(&buf); + *newstr = buf.c; + + PHP_VAR_SERIALIZE_DESTROY(var_hash); + return SUCCESS; +} +/* }}} */ -static void php_session_initialize(TSRMLS_D) +PS_SERIALIZER_DECODE_FUNC(php) /* {{{ */ { - char *val; - int vallen; + const char *p, *q; + char *name; + const char *endptr = val + vallen; + zval *current; + int namelen; + int has_value; + php_unserialize_data_t var_hash; - /* check session name for invalid characters */ - if (PS(id) && strpbrk(PS(id), "\r\n\t <>'\"\\")) { - efree(PS(id)); - PS(id) = NULL; - } + PHP_VAR_UNSERIALIZE_INIT(var_hash); - if (!PS(mod)) { - php_error_docref(NULL TSRMLS_CC, E_ERROR, "No storage module chosen - failed to initialize session."); - return; - } + p = val; - /* Open session handler first */ - if (PS(mod)->s_open(&PS(mod_data), PS(save_path), PS(session_name) TSRMLS_CC) == FAILURE) { - php_error_docref(NULL TSRMLS_CC, E_ERROR, "Failed to initialize storage module: %s (path: %s)", PS(mod)->s_name, PS(save_path)); - return; - } + while (p < endptr) { + zval **tmp; + q = p; + while (*q != PS_DELIMITER) { + if (++q >= endptr) goto break_outer_loop; + } + if (p[0] == PS_UNDEF_MARKER) { + p++; + has_value = 0; + } else { + has_value = 1; + } - /* If there is no ID, use session module to create one */ - if (!PS(id)) { -new_session: - PS(id) = PS(mod)->s_create_sid(&PS(mod_data), NULL TSRMLS_CC); - if (PS(use_cookies)) { - PS(send_cookie) = 1; + namelen = q - p; + name = estrndup(p, namelen); + q++; + + if (zend_hash_find(&EG(symbol_table), name, namelen + 1, (void **) &tmp) == SUCCESS) { + if ((Z_TYPE_PP(tmp) == IS_ARRAY && Z_ARRVAL_PP(tmp) == &EG(symbol_table)) || *tmp == PS(http_session_vars)) { + goto skip; + } } - } - /* Read data */ - /* Question: if you create a SID here, should you also try to read data? - * I'm not sure, but while not doing so will remove one session operation - * it could prove usefull for those sites which wish to have "default" - * session information - */ - php_session_track_init(TSRMLS_C); - PS(invalid_session_id) = 0; - if (PS(mod)->s_read(&PS(mod_data), PS(id), &val, &vallen TSRMLS_CC) == SUCCESS) { - php_session_decode(val, vallen TSRMLS_CC); - efree(val); - } else if (PS(invalid_session_id)) { /* address instances where the session read fails due to an invalid id */ - PS(invalid_session_id) = 0; - efree(PS(id)); - PS(id) = NULL; - goto new_session; + if (has_value) { + ALLOC_INIT_ZVAL(current); + if (php_var_unserialize(&current, (const unsigned char **) &q, (const unsigned char *) endptr, &var_hash TSRMLS_CC)) { + php_set_session_var(name, namelen, current, &var_hash TSRMLS_CC); + } + zval_ptr_dtor(&current); + } + PS_ADD_VARL(name, namelen); +skip: + efree(name); + + p = q; } +break_outer_loop: + + PHP_VAR_UNSERIALIZE_DESTROY(var_hash); + + return SUCCESS; } +/* }}} */ -static int migrate_global(HashTable *ht, HashPosition *pos TSRMLS_DC) -{ - char *str; - uint str_len; - ulong num_key; - int n; - zval **val; - int ret = 0; +#define MAX_SERIALIZERS 10 +#define PREDEFINED_SERIALIZERS 2 - n = zend_hash_get_current_key_ex(ht, &str, &str_len, &num_key, 0, pos); +static ps_serializer ps_serializers[MAX_SERIALIZERS + 1] = { + PS_SERIALIZER_ENTRY(php), + PS_SERIALIZER_ENTRY(php_binary) +}; - switch (n) { - case HASH_KEY_IS_STRING: - if (zend_hash_find(&EG(symbol_table), str, str_len, - (void **) &val) == SUCCESS - && val && Z_TYPE_PP(val) != IS_NULL) { - ZEND_SET_SYMBOL_WITH_LENGTH(ht, str, str_len, *val, - (*val)->refcount + 1 , 1); - ret = 1; - } - break; - case HASH_KEY_IS_LONG: - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "The session bug compatibility code will not " - "try to locate the global variable $%lu due to its " - "numeric nature.", num_key); +PHPAPI int php_session_register_serializer( + const char *name, + int (*encode)(PS_SERIALIZER_ENCODE_ARGS), + int (*decode)(PS_SERIALIZER_DECODE_ARGS)) /* {{{ */ +{ + int ret = -1; + int i; + + for (i = 0; i < MAX_SERIALIZERS; i++) { + if (ps_serializers[i].name == NULL) { + ps_serializers[i].name = name; + ps_serializers[i].encode = encode; + ps_serializers[i].decode = decode; + ps_serializers[i + 1].name = NULL; + ret = 0; break; + } } - return ret; } +/* }}} */ -static void php_session_save_current_state(TSRMLS_D) -{ - int ret = FAILURE; +/* ******************* + * Storage Modules * + ******************* */ - IF_SESSION_VARS() { - if (PS(bug_compat) && !PG(register_globals)) { - HashTable *ht = Z_ARRVAL_P(PS(http_session_vars)); - HashPosition pos; - zval **val; - int do_warn = 0; +#define MAX_MODULES 10 +#define PREDEFINED_MODULES 2 - zend_hash_internal_pointer_reset_ex(ht, &pos); +static ps_module *ps_modules[MAX_MODULES + 1] = { + ps_files_ptr, + ps_user_ptr +}; - while (zend_hash_get_current_data_ex(ht, - (void **) &val, &pos) != FAILURE) { - if (Z_TYPE_PP(val) == IS_NULL) { - if (migrate_global(ht, &pos TSRMLS_CC)) - do_warn = 1; - } - zend_hash_move_forward_ex(ht, &pos); - } +PHPAPI int php_session_register_module(ps_module *ptr) /* {{{ */ +{ + int ret = -1; + int i; - if (do_warn && PS(bug_compat_warn)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively."); - } + for (i = 0; i < MAX_MODULES; i++) { + if (!ps_modules[i]) { + ps_modules[i] = ptr; + ret = 0; + break; } + } + return ret; +} +/* }}} */ - if (PS(mod_data)) { - char *val; - int vallen; - - val = php_session_encode(&vallen TSRMLS_CC); - if (val) { - ret = PS(mod)->s_write(&PS(mod_data), PS(id), val, vallen TSRMLS_CC); - efree(val); - } else { - ret = PS(mod)->s_write(&PS(mod_data), PS(id), "", 0 TSRMLS_CC); - } - } +/* ****************** + * Cache Limiters * + ****************** */ - if (ret == FAILURE) - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to write session data (%s). Please " - "verify that the current setting of session.save_path " - "is correct (%s)", - PS(mod)->s_name, - PS(save_path)); - } +typedef struct { + char *name; + void (*func)(TSRMLS_D); +} php_session_cache_limiter_t; - if (PS(mod_data)) - PS(mod)->s_close(&PS(mod_data) TSRMLS_CC); -} +#define CACHE_LIMITER(name) _php_cache_limiter_##name +#define CACHE_LIMITER_FUNC(name) static void CACHE_LIMITER(name)(TSRMLS_D) +#define CACHE_LIMITER_ENTRY(name) { #name, CACHE_LIMITER(name) }, +#define ADD_HEADER(a) sapi_add_header(a, strlen(a), 1); +#define MAX_STR 512 static char *month_names[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", @@ -944,7 +990,7 @@ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }; -static void strcpy_gmt(char *ubuf, time_t *when) +static inline void strcpy_gmt(char *ubuf, time_t *when) /* {{{ */ { char buf[MAX_STR]; struct tm tm, *res; @@ -958,15 +1004,16 @@ } n = slprintf(buf, sizeof(buf), "%s, %02d %s %d %02d:%02d:%02d GMT", /* SAFE */ - week_days[tm.tm_wday], tm.tm_mday, - month_names[tm.tm_mon], tm.tm_year + 1900, - tm.tm_hour, tm.tm_min, + week_days[tm.tm_wday], tm.tm_mday, + month_names[tm.tm_mon], tm.tm_year + 1900, + tm.tm_hour, tm.tm_min, tm.tm_sec); memcpy(ubuf, buf, n); ubuf[n] = '\0'; } +/* }}} */ -static void last_modified(TSRMLS_D) +static inline void last_modified(TSRMLS_D) /* {{{ */ { const char *path; struct stat sb; @@ -980,16 +1027,14 @@ #define LAST_MODIFIED "Last-Modified: " memcpy(buf, LAST_MODIFIED, sizeof(LAST_MODIFIED) - 1); -#ifdef NETWARE - strcpy_gmt(buf + sizeof(LAST_MODIFIED) - 1, &(sb.st_mtime.tv_sec)); -#else - strcpy_gmt(buf + sizeof(LAST_MODIFIED) - 1, &sb.st_mtime); -#endif + strcpy_gmt(buf + sizeof(LAST_MODIFIED) - 1, &SESS_SB_MTIME(sb)); ADD_HEADER(buf); } } +/* }}} */ -CACHE_LIMITER_FUNC(public) +#define EXPIRES "Expires: " +CACHE_LIMITER_FUNC(public) /* {{{ */ { char buf[MAX_STR + 1]; struct timeval tv; @@ -997,7 +1042,6 @@ gettimeofday(&tv, NULL); now = tv.tv_sec + PS(cache_expire) * 60; -#define EXPIRES "Expires: " memcpy(buf, EXPIRES, sizeof(EXPIRES) - 1); strcpy_gmt(buf + sizeof(EXPIRES) - 1, &now); ADD_HEADER(buf); @@ -1007,8 +1051,9 @@ last_modified(TSRMLS_C); } +/* }}} */ -CACHE_LIMITER_FUNC(private_no_expire) +CACHE_LIMITER_FUNC(private_no_expire) /* {{{ */ { char buf[MAX_STR + 1]; @@ -1017,21 +1062,26 @@ last_modified(TSRMLS_C); } +/* }}} */ -CACHE_LIMITER_FUNC(private) +CACHE_LIMITER_FUNC(private) /* {{{ */ { ADD_HEADER("Expires: Thu, 19 Nov 1981 08:52:00 GMT"); CACHE_LIMITER(private_no_expire)(TSRMLS_C); } +/* }}} */ -CACHE_LIMITER_FUNC(nocache) +CACHE_LIMITER_FUNC(nocache) /* {{{ */ { ADD_HEADER("Expires: Thu, 19 Nov 1981 08:52:00 GMT"); + /* For HTTP/1.1 conforming clients and the rest (MSIE 5) */ ADD_HEADER("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0"); + /* For HTTP/1.0 conforming clients */ ADD_HEADER("Pragma: no-cache"); } +/* }}} */ static php_session_cache_limiter_t php_session_cache_limiters[] = { CACHE_LIMITER_ENTRY(public) @@ -1041,7 +1091,7 @@ {0} }; -static int php_session_cache_limiter(TSRMLS_D) +static int php_session_cache_limiter(TSRMLS_D) /* {{{ */ { php_session_cache_limiter_t *lim; @@ -1069,6 +1119,11 @@ return -1; } +/* }}} */ + +/* ********************* + * Cookie Management * + ********************* */ #define COOKIE_SET_COOKIE "Set-Cookie: " #define COOKIE_EXPIRES "; expires=" @@ -1077,7 +1132,7 @@ #define COOKIE_SECURE "; secure" #define COOKIE_HTTPONLY "; HttpOnly" -static void php_session_send_cookie(TSRMLS_D) +static void php_session_send_cookie(TSRMLS_D) /* {{{ */ { smart_str ncookie = {0}; char *date_fmt = NULL; @@ -1147,41 +1202,44 @@ header, probably sent with setcookie() will be replaced! */ sapi_add_header_ex(ncookie.c, ncookie.len, 0, 0 TSRMLS_CC); } +/* }}} */ -PHPAPI ps_module *_php_find_ps_module(char *name TSRMLS_DC) +PHPAPI ps_module *_php_find_ps_module(char *name TSRMLS_DC) /* {{{ */ { ps_module *ret = NULL; ps_module **mod; int i; - for (i = 0, mod = ps_modules; i < MAX_MODULES; i++, mod++) + for (i = 0, mod = ps_modules; i < MAX_MODULES; i++, mod++) { if (*mod && !strcasecmp(name, (*mod)->s_name)) { ret = *mod; break; } - + } return ret; } +/* }}} */ -PHPAPI const ps_serializer *_php_find_ps_serializer(char *name TSRMLS_DC) +PHPAPI const ps_serializer *_php_find_ps_serializer(char *name TSRMLS_DC) /* {{{ */ { const ps_serializer *ret = NULL; const ps_serializer *mod; - for (mod = ps_serializers; mod->name; mod++) + for (mod = ps_serializers; mod->name; mod++) { if (!strcasecmp(name, mod->name)) { ret = mod; break; } - + } return ret; } +/* }}} */ #define PPID2SID \ convert_to_string((*ppid)); \ PS(id) = estrndup(Z_STRVAL_PP(ppid), Z_STRLEN_PP(ppid)) -static void php_session_reset_id(TSRMLS_D) +static void php_session_reset_id(TSRMLS_D) /* {{{ */ { int module_number = PS(module_number); @@ -1210,45 +1268,55 @@ php_url_scanner_add_var(PS(session_name), strlen(PS(session_name)), PS(id), strlen(PS(id)), 1 TSRMLS_CC); } } +/* }}} */ -PHPAPI void php_session_start(TSRMLS_D) +PHPAPI void php_session_start(TSRMLS_D) /* {{{ */ { zval **ppid; zval **data; - char *p; + char *p, *value; int nrand; int lensess; PS(apply_trans_sid) = PS(use_trans_sid); - if (PS(session_status) != php_session_none) { - if (PS(session_status) == php_session_disabled) { - char *value; + switch (PS(session_status)) { + case php_session_active: + php_error(E_NOTICE, "A session had already been started - ignoring session_start()"); + return; + break; + case php_session_disabled: value = zend_ini_string("session.save_handler", sizeof("session.save_handler"), 0); - - if (value) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot find save handler %s", value); + if (!PS(mod) && value) { + PS(mod) = _php_find_ps_module(value TSRMLS_CC); + if (!PS(mod)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot find save handler '%s' - session startup failed", value); + return; + } } - else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot find unknown save handler"); + value = zend_ini_string("session.serialize_handler", sizeof("session.serialize_handler"), 0); + if (!PS(serializer) && value) { + PS(serializer) = _php_find_ps_serializer(value TSRMLS_CC); + if (!PS(serializer)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot find serialization handler '%s' - session startup failed", value); + return; + } } - return; - } + PS(session_status) = php_session_none; + /* fallthrough */ - php_error(E_NOTICE, "A session had already been started - ignoring session_start()"); - return; - } else { - PS(define_sid) = 1; - PS(send_cookie) = 1; + default: + case php_session_none: + PS(define_sid) = 1; + PS(send_cookie) = 1; } lensess = strlen(PS(session_name)); - /* * Cookies are preferred, because initially - * cookie and get variables will be available. + * cookie and get variables will be available. */ if (!PS(id)) { @@ -1301,7 +1369,6 @@ PS(id) = estrndup(p, q - p); PS(send_cookie) = 0; } - } /* check whether the current request was referred to by @@ -1318,15 +1385,17 @@ efree(PS(id)); PS(id) = NULL; PS(send_cookie) = 1; - if (PS(use_trans_sid)) + if (PS(use_trans_sid)) { PS(apply_trans_sid) = 1; + } } php_session_initialize(TSRMLS_C); if (!PS(use_cookies) && PS(send_cookie)) { - if (PS(use_trans_sid)) + if (PS(use_trans_sid)) { PS(apply_trans_sid) = 1; + } PS(send_cookie) = 0; } @@ -1342,38 +1411,42 @@ nrand = (int) ((float) PS(gc_divisor) * php_combined_lcg(TSRMLS_C)); if (nrand < PS(gc_probability)) { PS(mod)->s_gc(&PS(mod_data), PS(gc_maxlifetime), &nrdels TSRMLS_CC); -#if 0 - if (nrdels != -1) +#ifdef SESSION_DEBUG + if (nrdels != -1) { php_error_docref(NULL TSRMLS_CC, E_NOTICE, "purged %d expired session objects", nrdels); + } #endif } } } +/* }}} */ -static int php_session_destroy(TSRMLS_D) +static void php_session_flush(TSRMLS_D) /* {{{ */ { - int retval = SUCCESS; - - if (PS(session_status) != php_session_active) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Trying to destroy uninitialized session"); - return FAILURE; + if (PS(session_status) == php_session_active) { + PS(session_status) = php_session_none; + zend_try { + php_session_save_current_state(TSRMLS_C); + } zend_end_try(); } +} +/* }}} */ - if (PS(mod)->s_destroy(&PS(mod_data), PS(id) TSRMLS_CC) == FAILURE) { - retval = FAILURE; - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Session object destruction failed"); +PHPAPI void session_adapt_url(const char *url, size_t urllen, char **new, size_t *newlen TSRMLS_DC) /* {{{ */ +{ + if (PS(apply_trans_sid) && (PS(session_status) == php_session_active)) { + *new = php_url_scanner_adapt_single_url(url, urllen, PS(session_name), PS(id), newlen TSRMLS_CC); } - - php_rshutdown_session_globals(TSRMLS_C); - php_rinit_session_globals(TSRMLS_C); - - return retval; } +/* }}} */ +/* ******************************** + * Userspace exported functions * + ******************************** */ /* {{{ proto void session_set_cookie_params(int lifetime [, string path [, string domain [, bool secure[, bool httponly]]]]) Set session cookie parameters */ -PHP_FUNCTION(session_set_cookie_params) +static PHP_FUNCTION(session_set_cookie_params) { zval **lifetime, **path, **domain, **secure, **httponly; @@ -1385,6 +1458,7 @@ WRONG_PARAM_COUNT; convert_to_string_ex(lifetime); + zend_alter_ini_entry("session.cookie_lifetime", sizeof("session.cookie_lifetime"), Z_STRVAL_PP(lifetime), Z_STRLEN_PP(lifetime), PHP_INI_USER, PHP_INI_STAGE_RUNTIME); if (ZEND_NUM_ARGS() > 1) { @@ -1408,8 +1482,8 @@ /* }}} */ /* {{{ proto array session_get_cookie_params(void) - Return the session cookie parameters */ -PHP_FUNCTION(session_get_cookie_params) + Return the session cookie parameters */ +static PHP_FUNCTION(session_get_cookie_params) { if (ZEND_NUM_ARGS() != 0) { WRONG_PARAM_COUNT; @@ -1427,7 +1501,7 @@ /* {{{ proto string session_name([string newname]) Return the current session name. If newname is given, the session name is replaced with newname */ -PHP_FUNCTION(session_name) +static PHP_FUNCTION(session_name) { zval **p_name; int ac = ZEND_NUM_ARGS(); @@ -1449,7 +1523,7 @@ /* {{{ proto string session_module_name([string newname]) Return the current module name used for accessing session data. If newname is given, the module name is replaced with newname */ -PHP_FUNCTION(session_module_name) +static PHP_FUNCTION(session_module_name) { zval **p_name; int ac = ZEND_NUM_ARGS(); @@ -1468,8 +1542,7 @@ if (ac == 1) { convert_to_string_ex(p_name); if (!_php_find_ps_module(Z_STRVAL_PP(p_name) TSRMLS_CC)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot find named PHP session module (%s)", - Z_STRVAL_PP(p_name)); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot find named PHP session module (%s)", Z_STRVAL_PP(p_name)); zval_dtor(return_value); RETURN_FALSE; } @@ -1485,19 +1558,20 @@ /* {{{ 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) +static PHP_FUNCTION(session_set_save_handler) { zval **args[6]; int i; ps_user *mdata; char *name; + if (PS(session_status) != php_session_none) { + RETURN_FALSE; + } + if (ZEND_NUM_ARGS() != 6 || zend_get_parameters_array_ex(6, args) == FAILURE) WRONG_PARAM_COUNT; - if (PS(session_status) != php_session_none) - RETURN_FALSE; - for (i = 0; i < 6; i++) { if (!zend_is_callable(*args[i], 0, &name)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Argument %d is not a valid callback", i+1); @@ -1533,7 +1607,7 @@ /* {{{ 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) +static PHP_FUNCTION(session_save_path) { zval **p_name; int ac = ZEND_NUM_ARGS(); @@ -1555,7 +1629,7 @@ /* {{{ proto string session_id([string newid]) Return the current session id. If newid is given, the session id is replaced with newid */ -PHP_FUNCTION(session_id) +static PHP_FUNCTION(session_id) { zval **p_name; int ac = ZEND_NUM_ARGS(); @@ -1572,7 +1646,9 @@ if (ac == 1) { convert_to_string_ex(p_name); - if (PS(id)) efree(PS(id)); + if (PS(id)) { + efree(PS(id)); + } PS(id) = estrndup(Z_STRVAL_PP(p_name), Z_STRLEN_PP(p_name)); } @@ -1582,7 +1658,7 @@ /* {{{ proto bool session_regenerate_id([bool delete_old_session]) Update the current session id with a newly generated one. If delete_old_session is set to true, remove the old session. */ -PHP_FUNCTION(session_regenerate_id) +static PHP_FUNCTION(session_regenerate_id) { zend_bool del_ses = 0; @@ -1618,7 +1694,7 @@ /* {{{ proto string session_cache_limiter([string new_cache_limiter]) Return the current cache limiter. If new_cache_limited is given, the current cache_limiter is replaced with new_cache_limiter */ -PHP_FUNCTION(session_cache_limiter) +static PHP_FUNCTION(session_cache_limiter) { zval **p_cache_limiter; int ac = ZEND_NUM_ARGS(); @@ -1640,7 +1716,7 @@ /* {{{ proto int session_cache_expire([int new_cache_expire]) Return the current cache expire. If new_cache_expire is given, the current cache_expire is replaced with new_cache_expire */ -PHP_FUNCTION(session_cache_expire) +static PHP_FUNCTION(session_cache_expire) { zval **p_cache_expire; int ac = ZEND_NUM_ARGS(); @@ -1690,93 +1766,9 @@ } /* }}} */ -/* {{{ proto bool session_register(mixed var_names [, mixed ...]) - Adds varname(s) to the list of variables which are freezed at the session end */ -PHP_FUNCTION(session_register) -{ - zval ***args; - int argc = ZEND_NUM_ARGS(); - int i; - - if (argc <= 0) - RETURN_FALSE - else - args = (zval ***)safe_emalloc(argc, sizeof(zval **), 0); - - if (zend_get_parameters_array_ex(argc, args) == FAILURE) { - efree(args); - WRONG_PARAM_COUNT; - } - - if (PS(session_status) == php_session_none || PS(session_status) == php_session_disabled) { - php_session_start(TSRMLS_C); - } - - if (PS(session_status) == php_session_disabled) { - efree(args); - RETURN_FALSE; - } - - for (i = 0; i < argc; i++) { - if (Z_TYPE_PP(args[i]) == IS_ARRAY) - SEPARATE_ZVAL(args[i]); - php_register_var(args[i] TSRMLS_CC); - } - - efree(args); - - RETURN_TRUE; -} -/* }}} */ - -/* {{{ proto bool session_unregister(string varname) - Removes varname from the list of variables which are freezed at the session end */ -PHP_FUNCTION(session_unregister) -{ - zval **p_name; - int ac = ZEND_NUM_ARGS(); - - if (ac != 1 || zend_get_parameters_ex(ac, &p_name) == FAILURE) - WRONG_PARAM_COUNT; - - convert_to_string_ex(p_name); - - PS_DEL_VARL(Z_STRVAL_PP(p_name), Z_STRLEN_PP(p_name)); - - RETURN_TRUE; -} -/* }}} */ - -/* {{{ proto bool session_is_registered(string varname) - Checks if a variable is registered in session */ -PHP_FUNCTION(session_is_registered) -{ - zval **p_name; - zval *p_var; - int ac = ZEND_NUM_ARGS(); - - if (ac != 1 || zend_get_parameters_ex(ac, &p_name) == FAILURE) - WRONG_PARAM_COUNT; - - convert_to_string_ex(p_name); - - if (PS(session_status) == php_session_none) - RETURN_FALSE; - - IF_SESSION_VARS() { - if (zend_hash_find(Z_ARRVAL_P(PS(http_session_vars)), - Z_STRVAL_PP(p_name), Z_STRLEN_PP(p_name)+1, - (void **)&p_var) == SUCCESS) { - RETURN_TRUE; - } - } - RETURN_FALSE; -} -/* }}} */ - /* {{{ proto string session_encode(void) Serializes the current setup and returns the serialized representation */ -PHP_FUNCTION(session_encode) +static PHP_FUNCTION(session_encode) { int len; char *enc; @@ -1796,18 +1788,18 @@ /* {{{ proto bool session_decode(string data) Deserializes data and reinitializes the variables */ -PHP_FUNCTION(session_decode) +static PHP_FUNCTION(session_decode) { zval **str; - if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &str) == FAILURE) { - WRONG_PARAM_COUNT; - } - if (PS(session_status) == php_session_none) { RETURN_FALSE; } + if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &str) == FAILURE) { + WRONG_PARAM_COUNT; + } + convert_to_string_ex(str); php_session_decode(Z_STRVAL_PP(str), Z_STRLEN_PP(str) TSRMLS_CC); @@ -1818,37 +1810,37 @@ /* {{{ proto bool session_start(void) Begin session - reinitializes freezed variables, registers browsers etc */ -PHP_FUNCTION(session_start) +static PHP_FUNCTION(session_start) { /* skipping check for non-zero args for performance reasons here ?*/ php_session_start(TSRMLS_C); + + if (PS(session_status) != php_session_active) { + RETURN_FALSE; + } RETURN_TRUE; } /* }}} */ /* {{{ proto bool session_destroy(void) Destroy the current session and all data associated with it */ -PHP_FUNCTION(session_destroy) +static PHP_FUNCTION(session_destroy) { if (ZEND_NUM_ARGS() != 0) { WRONG_PARAM_COUNT; } - if (php_session_destroy(TSRMLS_C) == SUCCESS) { - RETURN_TRUE; - } else { - RETURN_FALSE; - } + RETURN_BOOL(php_session_destroy(TSRMLS_C) == SUCCESS); } /* }}} */ - /* {{{ proto void session_unset(void) Unset all registered variables */ -PHP_FUNCTION(session_unset) +static PHP_FUNCTION(session_unset) { - if (PS(session_status) == php_session_none) + if (PS(session_status) == php_session_none) { RETURN_FALSE; + } IF_SESSION_VARS() { HashTable *ht = Z_ARRVAL_P(PS(http_session_vars)); @@ -1861,7 +1853,7 @@ zend_hash_internal_pointer_reset_ex(ht, &pos); - while (zend_hash_get_current_key_ex(ht, &str, &str_len, &num_key, + while (zend_hash_get_current_key_ex(ht, &str, &str_len, &num_key, 0, &pos) == HASH_KEY_IS_STRING) { zend_delete_global_variable(str, str_len-1 TSRMLS_CC); zend_hash_move_forward_ex(ht, &pos); @@ -1874,39 +1866,131 @@ } /* }}} */ -PHPAPI void session_adapt_url(const char *url, size_t urllen, char **new, size_t *newlen TSRMLS_DC) +/* {{{ proto void session_write_close(void) + Write session data and end session */ +static PHP_FUNCTION(session_write_close) { - if (PS(apply_trans_sid) && (PS(session_status) == php_session_active)) { - *new = php_url_scanner_adapt_single_url(url, urllen, PS(session_name), PS(id), newlen TSRMLS_CC); + php_session_flush(TSRMLS_C); +} +/* }}} */ + +/* {{{ proto bool session_register(mixed var_names [, mixed ...]) + Adds varname(s) to the list of variables which are freezed at the session end */ +static PHP_FUNCTION(session_register) +{ + zval ***args; + int argc = ZEND_NUM_ARGS(); + int i; + + if (argc <= 0) + RETURN_FALSE + else + args = (zval ***)safe_emalloc(argc, sizeof(zval **), 0); + + if (zend_get_parameters_array_ex(argc, args) == FAILURE) { + efree(args); + WRONG_PARAM_COUNT; + } + + if (PS(session_status) == php_session_none || PS(session_status) == php_session_disabled) { + php_session_start(TSRMLS_C); + } + + if (PS(session_status) == php_session_disabled) { + efree(args); + RETURN_FALSE; + } + + for (i = 0; i < argc; i++) { + if (Z_TYPE_PP(args[i]) == IS_ARRAY) { + SEPARATE_ZVAL(args[i]); + } + php_register_var(args[i] TSRMLS_CC); } + + efree(args); + + RETURN_TRUE; } +/* }}} */ -static void php_rinit_session_globals(TSRMLS_D) +/* {{{ proto bool session_unregister(string varname) + Removes varname from the list of variables which are freezed at the session end */ +static PHP_FUNCTION(session_unregister) { - PS(id) = NULL; - PS(session_status) = php_session_none; - PS(mod_data) = NULL; - PS(http_session_vars) = NULL; + zval **p_name; + int ac = ZEND_NUM_ARGS(); + + if (ac != 1 || zend_get_parameters_ex(ac, &p_name) == FAILURE) { + WRONG_PARAM_COUNT; + } + convert_to_string_ex(p_name); + + PS_DEL_VARL(Z_STRVAL_PP(p_name), Z_STRLEN_PP(p_name)); + + RETURN_TRUE; } +/* }}} */ -static void php_rshutdown_session_globals(TSRMLS_D) +/* {{{ proto bool session_is_registered(string varname) + Checks if a variable is registered in session */ +static PHP_FUNCTION(session_is_registered) { - if (PS(http_session_vars)) { - zval_ptr_dtor(&PS(http_session_vars)); - PS(http_session_vars) = NULL; + zval **p_name; + zval *p_var; + int ac = ZEND_NUM_ARGS(); + + if (ac != 1 || zend_get_parameters_ex(ac, &p_name) == FAILURE) { + WRONG_PARAM_COUNT; } - if (PS(mod_data)) { - zend_try { - PS(mod)->s_close(&PS(mod_data) TSRMLS_CC); - } zend_end_try(); + + if (PS(session_status) == php_session_none) { + RETURN_FALSE; } - if (PS(id)) { - efree(PS(id)); + + convert_to_string_ex(p_name); + + IF_SESSION_VARS() { + if (zend_hash_find(Z_ARRVAL_P(PS(http_session_vars)), Z_STRVAL_PP(p_name), Z_STRLEN_PP(p_name)+1, (void **)&p_var) == SUCCESS) { + RETURN_TRUE; + } } + RETURN_FALSE; } +/* }}} */ + +/* {{{ session_functions[] + */ +static const zend_function_entry session_functions[] = { + PHP_FE(session_name, NULL) + PHP_FE(session_module_name, NULL) + PHP_FE(session_save_path, NULL) + PHP_FE(session_id, NULL) + PHP_FE(session_regenerate_id, NULL) + PHP_FE(session_decode, NULL) + PHP_FE(session_register, NULL) + PHP_FE(session_unregister, NULL) + PHP_FE(session_is_registered, NULL) + PHP_FE(session_encode, NULL) + PHP_FE(session_start, NULL) + PHP_FE(session_destroy, NULL) + PHP_FE(session_unset, NULL) + PHP_FE(session_set_save_handler, NULL) + PHP_FE(session_cache_limiter, NULL) + PHP_FE(session_cache_expire, NULL) + PHP_FE(session_set_cookie_params, NULL) + PHP_FE(session_get_cookie_params, NULL) + PHP_FE(session_write_close, NULL) + PHP_FALIAS(session_commit, session_write_close, NULL) + {NULL, NULL, NULL} +}; +/* }}} */ +/* ******************************** + * Module Setup and Destruction * + ******************************** */ -PHP_RINIT_FUNCTION(session) +static PHP_RINIT_FUNCTION(session) /* {{{ */ { php_rinit_session_globals(TSRMLS_C); @@ -1917,12 +2001,6 @@ if (value) { PS(mod) = _php_find_ps_module(value TSRMLS_CC); } - - if (!PS(mod)) { - /* current status is unusable */ - PS(session_status) = php_session_disabled; - return SUCCESS; - } } if (PS(serializer) == NULL) { @@ -1946,25 +2024,9 @@ return SUCCESS; } +/* }}} */ -static void php_session_flush(TSRMLS_D) -{ - if (PS(session_status) == php_session_active) { - PS(session_status) = php_session_none; - zend_try { - php_session_save_current_state(TSRMLS_C); - } zend_end_try(); - } -} - -/* {{{ proto void session_write_close(void) - Write session data and end session */ -PHP_FUNCTION(session_write_close) -{ - php_session_flush(TSRMLS_C); -} - -PHP_RSHUTDOWN_FUNCTION(session) +static PHP_RSHUTDOWN_FUNCTION(session) /* {{{ */ { php_session_flush(TSRMLS_C); php_rshutdown_session_globals(TSRMLS_C); @@ -1973,18 +2035,20 @@ } /* }}} */ -static PHP_GINIT_FUNCTION(ps) +static PHP_GINIT_FUNCTION(ps) /* {{{ */ { ps_globals->save_path = NULL; ps_globals->session_name = NULL; ps_globals->id = NULL; ps_globals->mod = NULL; + ps_globals->serializer = NULL; ps_globals->mod_data = NULL; ps_globals->session_status = php_session_none; ps_globals->http_session_vars = NULL; } +/* }}} */ -PHP_MINIT_FUNCTION(session) +static PHP_MINIT_FUNCTION(session) /* {{{ */ { zend_register_auto_global("_SESSION", sizeof("_SESSION")-1, NULL TSRMLS_CC); @@ -1998,8 +2062,9 @@ #endif return SUCCESS; } +/* }}} */ -PHP_MSHUTDOWN_FUNCTION(session) +static PHP_MSHUTDOWN_FUNCTION(session) /* {{{ */ { UNREGISTER_INI_ENTRIES(); @@ -2012,9 +2077,9 @@ return SUCCESS; } +/* }}} */ - -PHP_MINFO_FUNCTION(session) +static PHP_MINFO_FUNCTION(session) /* {{{ */ { ps_module **mod; ps_serializer *ser; @@ -2061,7 +2126,26 @@ DISPLAY_INI_ENTRIES(); } +/* }}} */ + +zend_module_entry session_module_entry = { + STANDARD_MODULE_HEADER, + "session", + session_functions, + PHP_MINIT(session), PHP_MSHUTDOWN(session), + PHP_RINIT(session), PHP_RSHUTDOWN(session), + PHP_MINFO(session), + NO_VERSION_YET, + PHP_MODULE_GLOBALS(ps), + PHP_GINIT(ps), + NULL, + NULL, + STANDARD_MODULE_PROPERTIES_EX +}; +#ifdef COMPILE_DL_SESSION +ZEND_GET_MODULE(session) +#endif /* * Local variables: http://cvs.php.net/viewvc.cgi/php-src/ext/session/tests/008-php4.2.3.phpt?r1=1.7.4.2&r2=1.7.4.3&diff_format=u Index: php-src/ext/session/tests/008-php4.2.3.phpt diff -u php-src/ext/session/tests/008-php4.2.3.phpt:1.7.4.2 php-src/ext/session/tests/008-php4.2.3.phpt:1.7.4.3 --- php-src/ext/session/tests/008-php4.2.3.phpt:1.7.4.2 Fri May 18 11:29:55 2007 +++ php-src/ext/session/tests/008-php4.2.3.phpt Sun Apr 26 01:20:56 2009 @@ -59,7 +59,7 @@ ?> --EXPECTF-- NULL -session_write_close(): Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively. +session_write_close(): Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively array(1) { ["c"]=> float(3.14) http://cvs.php.net/viewvc.cgi/php-src/ext/session/tests/014.phpt?r1=1.7&r2=1.7.4.1&diff_format=u Index: php-src/ext/session/tests/014.phpt diff -u php-src/ext/session/tests/014.phpt:1.7 php-src/ext/session/tests/014.phpt:1.7.4.1 --- php-src/ext/session/tests/014.phpt:1.7 Mon Jul 4 13:09:14 2005 +++ php-src/ext/session/tests/014.phpt Sun Apr 26 01:20:56 2009 @@ -35,8 +35,8 @@ --EXPECTF-- <a href="/link?PHPSESSID=abtest"> -Warning: ini_set(): A session is active. You cannot change the session module's ini settings at this time. in %s on line %d +Warning: ini_set(): A session is active. You cannot change the session module's ini settings at this time in %s on line %d <a href="/link?PHPSESSID=abtest"> -Warning: ini_set(): A session is active. You cannot change the session module's ini settings at this time. in %s on line %d +Warning: ini_set(): A session is active. You cannot change the session module's ini settings at this time in %s on line %d <a href="/link?PHPSESSID=abtest"> http://cvs.php.net/viewvc.cgi/php-src/ext/session/tests/session_decode_variation3.phpt?r1=1.2.2.2&r2=1.2.2.3&diff_format=u Index: php-src/ext/session/tests/session_decode_variation3.phpt diff -u php-src/ext/session/tests/session_decode_variation3.phpt:1.2.2.2 php-src/ext/session/tests/session_decode_variation3.phpt:1.2.2.3 --- php-src/ext/session/tests/session_decode_variation3.phpt:1.2.2.2 Tue Apr 29 08:57:12 2008 +++ php-src/ext/session/tests/session_decode_variation3.phpt Sun Apr 26 01:20:56 2009 @@ -23,8 +23,8 @@ $_SESSION["bar"] = "Blah!"; $_SESSION["guff"] = 123.456; var_dump($_SESSION); -$encoded = "A2Zvb2k6MTIzNDU2Nzg5MDs="; -var_dump(session_decode(base64_decode($encoded))); +$encoded = "foo|i:1234567890;"; +var_dump(session_decode($encoded)); var_dump($_SESSION); var_dump(session_destroy()); @@ -34,10 +34,11 @@ --EXPECTF-- *** Testing session_decode() : variation *** -Warning: session_start(): Unknown session.serialize_handler. Failed to decode session object. in %s on line %d -bool(true) -array(0) { -} +Warning: session_start(): Cannot find serialization handler 'blah' - session startup failed in %s on line %d +bool(false) + +Notice: Undefined variable: _SESSION in %s on line %d +NULL array(3) { ["foo"]=> int(1234567890) @@ -47,7 +48,7 @@ float(123.456) } -Warning: session_decode(): Unknown session.serialize_handler. Failed to decode session object. in %s on line %d +Warning: session_decode(): Unknown session.serialize_handler. Failed to decode session object in %s on line %d bool(true) array(3) { ["foo"]=> @@ -57,6 +58,7 @@ ["guff"]=> float(123.456) } -bool(true) -Done +Warning: session_destroy(): Trying to destroy uninitialized session in %s on line %d +bool(false) +Done http://cvs.php.net/viewvc.cgi/php-src/ext/session/tests/session_encode_error2.phpt?r1=1.1.2.1&r2=1.1.2.2&diff_format=u Index: php-src/ext/session/tests/session_encode_error2.phpt diff -u php-src/ext/session/tests/session_encode_error2.phpt:1.1.2.1 php-src/ext/session/tests/session_encode_error2.phpt:1.1.2.2 --- php-src/ext/session/tests/session_encode_error2.phpt:1.1.2.1 Tue Apr 22 16:02:43 2008 +++ php-src/ext/session/tests/session_encode_error2.phpt Sun Apr 26 01:20:56 2009 @@ -100,63 +100,63 @@ -- Iteration 1 -- bool(true) -Notice: session_encode(): Skipping numeric key 0. in %s on line %d +Notice: session_encode(): Skipping numeric key 0 in %s on line %d bool(false) bool(true) -- Iteration 2 -- bool(true) -Notice: session_encode(): Skipping numeric key 1. in %s on line %d +Notice: session_encode(): Skipping numeric key 1 in %s on line %d bool(false) bool(true) -- Iteration 3 -- bool(true) -Notice: session_encode(): Skipping numeric key 12345. in %s on line %d +Notice: session_encode(): Skipping numeric key 12345 in %s on line %d bool(false) bool(true) -- Iteration 4 -- bool(true) -Notice: session_encode(): Skipping numeric key -2345. in %s on line %d +Notice: session_encode(): Skipping numeric key -2345 in %s on line %d bool(false) bool(true) -- Iteration 5 -- bool(true) -Notice: session_encode(): Skipping numeric key 10. in %s on line %d +Notice: session_encode(): Skipping numeric key 10 in %s on line %d bool(false) bool(true) -- Iteration 6 -- bool(true) -Notice: session_encode(): Skipping numeric key -10. in %s on line %d +Notice: session_encode(): Skipping numeric key -10 in %s on line %d bool(false) bool(true) -- Iteration 7 -- bool(true) -Notice: session_encode(): Skipping numeric key %s. in %s on line %d +Notice: session_encode(): Skipping numeric key %s in %s on line %d bool(false) bool(true) -- Iteration 8 -- bool(true) -Notice: session_encode(): Skipping numeric key 0. in %s on line %d +Notice: session_encode(): Skipping numeric key 0 in %s on line %d bool(false) bool(true) -- Iteration 9 -- bool(true) -Notice: session_encode(): Skipping numeric key 0. in %s on line %d +Notice: session_encode(): Skipping numeric key 0 in %s on line %d bool(false) bool(true) @@ -173,28 +173,28 @@ -- Iteration 12 -- bool(true) -Notice: session_encode(): Skipping numeric key 1. in %s on line %d +Notice: session_encode(): Skipping numeric key 1 in %s on line %d bool(false) bool(true) -- Iteration 13 -- bool(true) -Notice: session_encode(): Skipping numeric key 0. in %s on line %d +Notice: session_encode(): Skipping numeric key 0 in %s on line %d bool(false) bool(true) -- Iteration 14 -- bool(true) -Notice: session_encode(): Skipping numeric key 1. in %s on line %d +Notice: session_encode(): Skipping numeric key 1 in %s on line %d bool(false) bool(true) -- Iteration 15 -- bool(true) -Notice: session_encode(): Skipping numeric key 0. in %s on line %d +Notice: session_encode(): Skipping numeric key 0 in %s on line %d bool(false) bool(true) @@ -245,7 +245,7 @@ Strict Standards: Resource ID#%d used as offset, casting to integer (%d) in %s on line %d -Notice: session_encode(): Skipping numeric key %d. in %s on line %d +Notice: session_encode(): Skipping numeric key %d in %s on line %d bool(false) bool(true) Done http://cvs.php.net/viewvc.cgi/php-src/ext/session/tests/session_encode_variation1.phpt?r1=1.1.2.1&r2=1.1.2.2&diff_format=u Index: php-src/ext/session/tests/session_encode_variation1.phpt diff -u php-src/ext/session/tests/session_encode_variation1.phpt:1.1.2.1 php-src/ext/session/tests/session_encode_variation1.phpt:1.1.2.2 --- php-src/ext/session/tests/session_encode_variation1.phpt:1.1.2.1 Tue Apr 22 16:02:44 2008 +++ php-src/ext/session/tests/session_encode_variation1.phpt Sun Apr 26 01:20:56 2009 @@ -31,7 +31,7 @@ --EXPECTF-- *** Testing session_encode() : variation *** -Warning: session_encode(): Cannot encode non-existent session. in %s on line %d +Warning: session_encode(): Cannot encode non-existent session in %s on line %d bool(false) bool(true) bool(false) @@ -41,7 +41,7 @@ bool(false) bool(true) -Warning: session_encode(): Cannot encode non-existent session. in %s on line %d +Warning: session_encode(): Cannot encode non-existent session in %s on line %d bool(false) Done http://cvs.php.net/viewvc.cgi/php-src/ext/session/tests/session_encode_variation2.phpt?r1=1.1.2.1&r2=1.1.2.2&diff_format=u Index: php-src/ext/session/tests/session_encode_variation2.phpt diff -u php-src/ext/session/tests/session_encode_variation2.phpt:1.1.2.1 php-src/ext/session/tests/session_encode_variation2.phpt:1.1.2.2 --- php-src/ext/session/tests/session_encode_variation2.phpt:1.1.2.1 Tue Apr 22 16:02:43 2008 +++ php-src/ext/session/tests/session_encode_variation2.phpt Sun Apr 26 01:20:56 2009 @@ -29,7 +29,7 @@ bool(false) bool(true) -Warning: session_encode(): Cannot encode non-existent session. in %s on line %d +Warning: session_encode(): Cannot encode non-existent session in %s on line %d bool(false) Done http://cvs.php.net/viewvc.cgi/php-src/ext/session/tests/session_encode_variation6.phpt?r1=1.1.2.1&r2=1.1.2.2&diff_format=u Index: php-src/ext/session/tests/session_encode_variation6.phpt diff -u php-src/ext/session/tests/session_encode_variation6.phpt:1.1.2.1 php-src/ext/session/tests/session_encode_variation6.phpt:1.1.2.2 --- php-src/ext/session/tests/session_encode_variation6.phpt:1.1.2.1 Tue Apr 22 16:02:43 2008 +++ php-src/ext/session/tests/session_encode_variation6.phpt Sun Apr 26 01:20:56 2009 @@ -35,17 +35,17 @@ *** Testing session_encode() : variation *** bool(true) -Notice: session_encode(): Skipping numeric key 0. in %s on line %d +Notice: session_encode(): Skipping numeric key 0 in %s on line %d bool(false) bool(true) bool(true) -Notice: session_encode(): Skipping numeric key 1234567890. in %s on line %d +Notice: session_encode(): Skipping numeric key 1234567890 in %s on line %d bool(false) bool(true) bool(true) -Notice: session_encode(): Skipping numeric key -1234567890. in %s on line %d +Notice: session_encode(): Skipping numeric key -1234567890 in %s on line %d bool(false) bool(true) Done http://cvs.php.net/viewvc.cgi/php-src/ext/session/tests/session_encode_variation8.phpt?r1=1.2.2.2&r2=1.2.2.3&diff_format=u Index: php-src/ext/session/tests/session_encode_variation8.phpt diff -u php-src/ext/session/tests/session_encode_variation8.phpt:1.2.2.2 php-src/ext/session/tests/session_encode_variation8.phpt:1.2.2.3 --- php-src/ext/session/tests/session_encode_variation8.phpt:1.2.2.2 Tue Apr 29 08:57:12 2008 +++ php-src/ext/session/tests/session_encode_variation8.phpt Sun Apr 26 01:20:56 2009 @@ -29,11 +29,12 @@ --EXPECTF-- *** Testing session_encode() : variation *** -Warning: session_start(): Unknown session.serialize_handler. Failed to decode session object. in %s on line %d -bool(true) +Warning: session_start(): Cannot find serialization handler 'blah' - session startup failed in %s on line %d +bool(false) -Warning: session_encode(): Unknown session.serialize_handler. Failed to encode session object. in %s on line %d +Warning: session_encode(): Cannot encode non-existent session in %s on line %d string(0) "" -bool(true) -Done +Warning: session_destroy(): Trying to destroy uninitialized session in %s on line %d +bool(false) +Done
« previous php.cvs (#57266) next »