You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

245 lines
8.0 KiB

24 years ago
25 years ago
25 years ago
17 years ago
17 years ago
17 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2009 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Sascha Schumann <sascha@schumann.cx> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. #ifndef PHP_SESSION_H
  20. #define PHP_SESSION_H
  21. #include "ext/standard/php_var.h"
  22. #if defined(HAVE_HASH_EXT) && !defined(COMPILE_DL_HASH)
  23. # include "ext/hash/php_hash.h"
  24. #endif
  25. #define PHP_SESSION_API 20020330
  26. #define PS_OPEN_ARGS void **mod_data, const char *save_path, const char *session_name TSRMLS_DC
  27. #define PS_CLOSE_ARGS void **mod_data TSRMLS_DC
  28. #define PS_READ_ARGS void **mod_data, const char *key, char **val, int *vallen TSRMLS_DC
  29. #define PS_WRITE_ARGS void **mod_data, const char *key, const char *val, const int vallen TSRMLS_DC
  30. #define PS_DESTROY_ARGS void **mod_data, const char *key TSRMLS_DC
  31. #define PS_GC_ARGS void **mod_data, int maxlifetime, int *nrdels TSRMLS_DC
  32. #define PS_CREATE_SID_ARGS void **mod_data, int *newlen TSRMLS_DC
  33. /* default create id function */
  34. PHPAPI char *php_session_create_id(PS_CREATE_SID_ARGS);
  35. typedef struct ps_module_struct {
  36. const char *s_name;
  37. int (*s_open)(PS_OPEN_ARGS);
  38. int (*s_close)(PS_CLOSE_ARGS);
  39. int (*s_read)(PS_READ_ARGS);
  40. int (*s_write)(PS_WRITE_ARGS);
  41. int (*s_destroy)(PS_DESTROY_ARGS);
  42. int (*s_gc)(PS_GC_ARGS);
  43. char *(*s_create_sid)(PS_CREATE_SID_ARGS);
  44. } ps_module;
  45. #define PS_GET_MOD_DATA() *mod_data
  46. #define PS_SET_MOD_DATA(a) *mod_data = (a)
  47. #define PS_OPEN_FUNC(x) int ps_open_##x(PS_OPEN_ARGS)
  48. #define PS_CLOSE_FUNC(x) int ps_close_##x(PS_CLOSE_ARGS)
  49. #define PS_READ_FUNC(x) int ps_read_##x(PS_READ_ARGS)
  50. #define PS_WRITE_FUNC(x) int ps_write_##x(PS_WRITE_ARGS)
  51. #define PS_DESTROY_FUNC(x) int ps_delete_##x(PS_DESTROY_ARGS)
  52. #define PS_GC_FUNC(x) int ps_gc_##x(PS_GC_ARGS)
  53. #define PS_CREATE_SID_FUNC(x) char *ps_create_sid_##x(PS_CREATE_SID_ARGS)
  54. #define PS_FUNCS(x) \
  55. PS_OPEN_FUNC(x); \
  56. PS_CLOSE_FUNC(x); \
  57. PS_READ_FUNC(x); \
  58. PS_WRITE_FUNC(x); \
  59. PS_DESTROY_FUNC(x); \
  60. PS_GC_FUNC(x); \
  61. PS_CREATE_SID_FUNC(x)
  62. #define PS_MOD(x) \
  63. #x, ps_open_##x, ps_close_##x, ps_read_##x, ps_write_##x, \
  64. ps_delete_##x, ps_gc_##x, php_session_create_id
  65. /* SID enabled module handler definitions */
  66. #define PS_FUNCS_SID(x) \
  67. PS_OPEN_FUNC(x); \
  68. PS_CLOSE_FUNC(x); \
  69. PS_READ_FUNC(x); \
  70. PS_WRITE_FUNC(x); \
  71. PS_DESTROY_FUNC(x); \
  72. PS_GC_FUNC(x); \
  73. PS_CREATE_SID_FUNC(x)
  74. #define PS_MOD_SID(x) \
  75. #x, ps_open_##x, ps_close_##x, ps_read_##x, ps_write_##x, \
  76. ps_delete_##x, ps_gc_##x, ps_create_sid_##x
  77. typedef enum {
  78. php_session_disabled,
  79. php_session_none,
  80. php_session_active
  81. } php_session_status;
  82. typedef struct _php_ps_globals {
  83. char *save_path;
  84. char *session_name;
  85. char *id;
  86. char *extern_referer_chk;
  87. char *entropy_file;
  88. char *cache_limiter;
  89. long entropy_length;
  90. long cookie_lifetime;
  91. char *cookie_path;
  92. char *cookie_domain;
  93. zend_bool cookie_secure;
  94. zend_bool cookie_httponly;
  95. ps_module *mod;
  96. void *mod_data;
  97. php_session_status session_status;
  98. long gc_probability;
  99. long gc_divisor;
  100. long gc_maxlifetime;
  101. int module_number;
  102. long cache_expire;
  103. union {
  104. zval *names[6];
  105. struct {
  106. zval *ps_open;
  107. zval *ps_close;
  108. zval *ps_read;
  109. zval *ps_write;
  110. zval *ps_destroy;
  111. zval *ps_gc;
  112. } name;
  113. } mod_user_names;
  114. zend_bool bug_compat; /* Whether to behave like PHP 4.2 and earlier */
  115. zend_bool bug_compat_warn; /* Whether to warn about it */
  116. const struct ps_serializer_struct *serializer;
  117. zval *http_session_vars;
  118. zend_bool auto_start;
  119. zend_bool use_cookies;
  120. zend_bool use_only_cookies;
  121. zend_bool use_trans_sid; /* contains the INI value of whether to use trans-sid */
  122. zend_bool apply_trans_sid; /* whether or not to enable trans-sid for the current request */
  123. long hash_func;
  124. #if defined(HAVE_HASH_EXT) && !defined(COMPILE_DL_HASH)
  125. php_hash_ops *hash_ops;
  126. #endif
  127. long hash_bits_per_character;
  128. int send_cookie;
  129. int define_sid;
  130. zend_bool invalid_session_id; /* allows the driver to report about an invalid session id and request id regeneration */
  131. } php_ps_globals;
  132. typedef php_ps_globals zend_ps_globals;
  133. extern zend_module_entry session_module_entry;
  134. #define phpext_session_ptr &session_module_entry
  135. #ifdef ZTS
  136. #define PS(v) TSRMG(ps_globals_id, php_ps_globals *, v)
  137. #else
  138. #define PS(v) (ps_globals.v)
  139. #endif
  140. #define PS_SERIALIZER_ENCODE_ARGS char **newstr, int *newlen TSRMLS_DC
  141. #define PS_SERIALIZER_DECODE_ARGS const char *val, int vallen TSRMLS_DC
  142. typedef struct ps_serializer_struct {
  143. const char *name;
  144. int (*encode)(PS_SERIALIZER_ENCODE_ARGS);
  145. int (*decode)(PS_SERIALIZER_DECODE_ARGS);
  146. } ps_serializer;
  147. #define PS_SERIALIZER_ENCODE_NAME(x) ps_srlzr_encode_##x
  148. #define PS_SERIALIZER_DECODE_NAME(x) ps_srlzr_decode_##x
  149. #define PS_SERIALIZER_ENCODE_FUNC(x) \
  150. int PS_SERIALIZER_ENCODE_NAME(x)(PS_SERIALIZER_ENCODE_ARGS)
  151. #define PS_SERIALIZER_DECODE_FUNC(x) \
  152. int PS_SERIALIZER_DECODE_NAME(x)(PS_SERIALIZER_DECODE_ARGS)
  153. #define PS_SERIALIZER_FUNCS(x) \
  154. PS_SERIALIZER_ENCODE_FUNC(x); \
  155. PS_SERIALIZER_DECODE_FUNC(x)
  156. #define PS_SERIALIZER_ENTRY(x) \
  157. { #x, PS_SERIALIZER_ENCODE_NAME(x), PS_SERIALIZER_DECODE_NAME(x) }
  158. PHPAPI void session_adapt_url(const char *, size_t, char **, size_t * TSRMLS_DC);
  159. PHPAPI void php_add_session_var(char *name, size_t namelen TSRMLS_DC);
  160. PHPAPI void php_set_session_var(char *name, size_t namelen, zval *state_val, php_unserialize_data_t *var_hash TSRMLS_DC);
  161. PHPAPI int php_get_session_var(char *name, size_t namelen, zval ***state_var TSRMLS_DC);
  162. PHPAPI int php_session_register_module(ps_module *);
  163. PHPAPI int php_session_register_serializer(const char *name,
  164. int (*encode)(PS_SERIALIZER_ENCODE_ARGS),
  165. int (*decode)(PS_SERIALIZER_DECODE_ARGS));
  166. PHPAPI void php_session_set_id(char *id TSRMLS_DC);
  167. PHPAPI void php_session_start(TSRMLS_D);
  168. PHPAPI ps_module *_php_find_ps_module(char *name TSRMLS_DC);
  169. PHPAPI const ps_serializer *_php_find_ps_serializer(char *name TSRMLS_DC);
  170. #define PS_ADD_VARL(name,namelen) do { \
  171. php_add_session_var(name, namelen TSRMLS_CC); \
  172. } while (0)
  173. #define PS_ADD_VAR(name) PS_ADD_VARL(name, strlen(name))
  174. #define PS_DEL_VARL(name,namelen) do { \
  175. if (PS(http_session_vars)) { \
  176. zend_hash_del(Z_ARRVAL_P(PS(http_session_vars)), name, namelen+1); \
  177. } \
  178. } while (0)
  179. #define PS_ENCODE_VARS \
  180. char *key; \
  181. uint key_length; \
  182. ulong num_key; \
  183. zval **struc;
  184. #define PS_ENCODE_LOOP(code) do { \
  185. HashTable *_ht = Z_ARRVAL_P(PS(http_session_vars)); \
  186. int key_type; \
  187. \
  188. for (zend_hash_internal_pointer_reset(_ht); \
  189. (key_type = zend_hash_get_current_key_ex(_ht, &key, &key_length, &num_key, 0, NULL)) != HASH_KEY_NON_EXISTANT; \
  190. zend_hash_move_forward(_ht)) { \
  191. if (key_type == HASH_KEY_IS_LONG) { \
  192. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Skipping numeric key %ld", num_key); \
  193. continue; \
  194. } \
  195. key_length--; \
  196. if (php_get_session_var(key, key_length, &struc TSRMLS_CC) == SUCCESS) { \
  197. code; \
  198. } \
  199. } \
  200. } while(0)
  201. PHPAPI ZEND_EXTERN_MODULE_GLOBALS(ps)
  202. void php_session_auto_start(void *data);
  203. void php_session_shutdown(void *data);
  204. #endif