php_session.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 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. /* To check php_session_valid_key()/php_session_reset_id() */
  27. #define PHP_SESSION_STRICT 1
  28. #define PS_OPEN_ARGS void **mod_data, const char *save_path, const char *session_name TSRMLS_DC
  29. #define PS_CLOSE_ARGS void **mod_data TSRMLS_DC
  30. #define PS_READ_ARGS void **mod_data, const char *key, char **val, int *vallen TSRMLS_DC
  31. #define PS_WRITE_ARGS void **mod_data, const char *key, const char *val, const int vallen TSRMLS_DC
  32. #define PS_DESTROY_ARGS void **mod_data, const char *key TSRMLS_DC
  33. #define PS_GC_ARGS void **mod_data, int maxlifetime, int *nrdels TSRMLS_DC
  34. #define PS_CREATE_SID_ARGS void **mod_data, int *newlen TSRMLS_DC
  35. /* default create id function */
  36. PHPAPI char *php_session_create_id(PS_CREATE_SID_ARGS);
  37. typedef struct ps_module_struct {
  38. const char *s_name;
  39. int (*s_open)(PS_OPEN_ARGS);
  40. int (*s_close)(PS_CLOSE_ARGS);
  41. int (*s_read)(PS_READ_ARGS);
  42. int (*s_write)(PS_WRITE_ARGS);
  43. int (*s_destroy)(PS_DESTROY_ARGS);
  44. int (*s_gc)(PS_GC_ARGS);
  45. char *(*s_create_sid)(PS_CREATE_SID_ARGS);
  46. } ps_module;
  47. #define PS_GET_MOD_DATA() *mod_data
  48. #define PS_SET_MOD_DATA(a) *mod_data = (a)
  49. #define PS_OPEN_FUNC(x) int ps_open_##x(PS_OPEN_ARGS)
  50. #define PS_CLOSE_FUNC(x) int ps_close_##x(PS_CLOSE_ARGS)
  51. #define PS_READ_FUNC(x) int ps_read_##x(PS_READ_ARGS)
  52. #define PS_WRITE_FUNC(x) int ps_write_##x(PS_WRITE_ARGS)
  53. #define PS_DESTROY_FUNC(x) int ps_delete_##x(PS_DESTROY_ARGS)
  54. #define PS_GC_FUNC(x) int ps_gc_##x(PS_GC_ARGS)
  55. #define PS_CREATE_SID_FUNC(x) char *ps_create_sid_##x(PS_CREATE_SID_ARGS)
  56. #define PS_FUNCS(x) \
  57. PS_OPEN_FUNC(x); \
  58. PS_CLOSE_FUNC(x); \
  59. PS_READ_FUNC(x); \
  60. PS_WRITE_FUNC(x); \
  61. PS_DESTROY_FUNC(x); \
  62. PS_GC_FUNC(x); \
  63. PS_CREATE_SID_FUNC(x)
  64. #define PS_MOD(x) \
  65. #x, ps_open_##x, ps_close_##x, ps_read_##x, ps_write_##x, \
  66. ps_delete_##x, ps_gc_##x, php_session_create_id
  67. /* SID creation enabled module handler definitions */
  68. #define PS_FUNCS_SID(x) \
  69. PS_OPEN_FUNC(x); \
  70. PS_CLOSE_FUNC(x); \
  71. PS_READ_FUNC(x); \
  72. PS_WRITE_FUNC(x); \
  73. PS_DESTROY_FUNC(x); \
  74. PS_GC_FUNC(x); \
  75. PS_CREATE_SID_FUNC(x)
  76. #define PS_MOD_SID(x) \
  77. #x, ps_open_##x, ps_close_##x, ps_read_##x, ps_write_##x, \
  78. ps_delete_##x, ps_gc_##x, ps_create_sid_##x
  79. typedef enum {
  80. php_session_disabled,
  81. php_session_none,
  82. php_session_active
  83. } php_session_status;
  84. typedef struct _php_session_rfc1867_progress {
  85. size_t sname_len;
  86. zval sid;
  87. smart_str key;
  88. long update_step;
  89. long next_update;
  90. double next_update_time;
  91. zend_bool cancel_upload;
  92. zend_bool apply_trans_sid;
  93. size_t content_length;
  94. zval *data; /* the array exported to session data */
  95. zval *post_bytes_processed; /* data["bytes_processed"] */
  96. zval *files; /* data["files"] array */
  97. zval *current_file; /* array of currently uploading file */
  98. zval *current_file_bytes_processed;
  99. } php_session_rfc1867_progress;
  100. typedef struct _php_ps_globals {
  101. char *save_path;
  102. char *session_name;
  103. char *id;
  104. char *extern_referer_chk;
  105. char *entropy_file;
  106. char *cache_limiter;
  107. long entropy_length;
  108. long cookie_lifetime;
  109. char *cookie_path;
  110. char *cookie_domain;
  111. zend_bool cookie_secure;
  112. zend_bool cookie_httponly;
  113. ps_module *mod;
  114. ps_module *default_mod;
  115. void *mod_data;
  116. php_session_status session_status;
  117. long gc_probability;
  118. long gc_divisor;
  119. long gc_maxlifetime;
  120. int module_number;
  121. long cache_expire;
  122. union {
  123. zval *names[7];
  124. struct {
  125. zval *ps_open;
  126. zval *ps_close;
  127. zval *ps_read;
  128. zval *ps_write;
  129. zval *ps_destroy;
  130. zval *ps_gc;
  131. zval *ps_create_sid;
  132. } name;
  133. } mod_user_names;
  134. int mod_user_implemented;
  135. int mod_user_is_open;
  136. const struct ps_serializer_struct *serializer;
  137. zval *http_session_vars;
  138. zend_bool auto_start;
  139. zend_bool use_cookies;
  140. zend_bool use_only_cookies;
  141. zend_bool use_trans_sid; /* contains the INI value of whether to use trans-sid */
  142. zend_bool apply_trans_sid; /* whether or not to enable trans-sid for the current request */
  143. long hash_func;
  144. #if defined(HAVE_HASH_EXT) && !defined(COMPILE_DL_HASH)
  145. php_hash_ops *hash_ops;
  146. #endif
  147. long hash_bits_per_character;
  148. int send_cookie;
  149. int define_sid;
  150. zend_bool invalid_session_id; /* allows the driver to report about an invalid session id and request id regeneration */
  151. php_session_rfc1867_progress *rfc1867_progress;
  152. zend_bool rfc1867_enabled; /* session.upload_progress.enabled */
  153. zend_bool rfc1867_cleanup; /* session.upload_progress.cleanup */
  154. smart_str rfc1867_prefix; /* session.upload_progress.prefix */
  155. smart_str rfc1867_name; /* session.upload_progress.name */
  156. long rfc1867_freq; /* session.upload_progress.freq */
  157. double rfc1867_min_freq; /* session.upload_progress.min_freq */
  158. zend_bool use_strict_mode; /* whether or not PHP accepts unknown session ids */
  159. unsigned char session_data_hash[16]; /* binary MD5 hash length */
  160. } php_ps_globals;
  161. typedef php_ps_globals zend_ps_globals;
  162. extern zend_module_entry session_module_entry;
  163. #define phpext_session_ptr &session_module_entry
  164. #ifdef ZTS
  165. #define PS(v) TSRMG(ps_globals_id, php_ps_globals *, v)
  166. #else
  167. #define PS(v) (ps_globals.v)
  168. #endif
  169. #define PS_SERIALIZER_ENCODE_ARGS char **newstr, int *newlen TSRMLS_DC
  170. #define PS_SERIALIZER_DECODE_ARGS const char *val, int vallen TSRMLS_DC
  171. typedef struct ps_serializer_struct {
  172. const char *name;
  173. int (*encode)(PS_SERIALIZER_ENCODE_ARGS);
  174. int (*decode)(PS_SERIALIZER_DECODE_ARGS);
  175. } ps_serializer;
  176. #define PS_SERIALIZER_ENCODE_NAME(x) ps_srlzr_encode_##x
  177. #define PS_SERIALIZER_DECODE_NAME(x) ps_srlzr_decode_##x
  178. #define PS_SERIALIZER_ENCODE_FUNC(x) \
  179. int PS_SERIALIZER_ENCODE_NAME(x)(PS_SERIALIZER_ENCODE_ARGS)
  180. #define PS_SERIALIZER_DECODE_FUNC(x) \
  181. int PS_SERIALIZER_DECODE_NAME(x)(PS_SERIALIZER_DECODE_ARGS)
  182. #define PS_SERIALIZER_FUNCS(x) \
  183. PS_SERIALIZER_ENCODE_FUNC(x); \
  184. PS_SERIALIZER_DECODE_FUNC(x)
  185. #define PS_SERIALIZER_ENTRY(x) \
  186. { #x, PS_SERIALIZER_ENCODE_NAME(x), PS_SERIALIZER_DECODE_NAME(x) }
  187. PHPAPI void session_adapt_url(const char *, size_t, char **, size_t * TSRMLS_DC);
  188. PHPAPI void php_add_session_var(char *name, size_t namelen TSRMLS_DC);
  189. PHPAPI void php_set_session_var(char *name, size_t namelen, zval *state_val, php_unserialize_data_t *var_hash TSRMLS_DC);
  190. PHPAPI int php_get_session_var(char *name, size_t namelen, zval ***state_var TSRMLS_DC);
  191. PHPAPI int php_session_register_module(ps_module *);
  192. PHPAPI int php_session_register_serializer(const char *name,
  193. int (*encode)(PS_SERIALIZER_ENCODE_ARGS),
  194. int (*decode)(PS_SERIALIZER_DECODE_ARGS));
  195. PHPAPI void php_session_set_id(char *id TSRMLS_DC);
  196. PHPAPI void php_session_start(TSRMLS_D);
  197. PHPAPI ps_module *_php_find_ps_module(char *name TSRMLS_DC);
  198. PHPAPI const ps_serializer *_php_find_ps_serializer(char *name TSRMLS_DC);
  199. PHPAPI int php_session_valid_key(const char *key);
  200. PHPAPI void php_session_reset_id(TSRMLS_D);
  201. #define PS_ADD_VARL(name,namelen) do { \
  202. php_add_session_var(name, namelen TSRMLS_CC); \
  203. } while (0)
  204. #define PS_ADD_VAR(name) PS_ADD_VARL(name, strlen(name))
  205. #define PS_DEL_VARL(name,namelen) do { \
  206. if (PS(http_session_vars)) { \
  207. zend_hash_del(Z_ARRVAL_P(PS(http_session_vars)), name, namelen+1); \
  208. } \
  209. } while (0)
  210. #define PS_ENCODE_VARS \
  211. char *key; \
  212. uint key_length; \
  213. ulong num_key; \
  214. zval **struc;
  215. #define PS_ENCODE_LOOP(code) do { \
  216. HashTable *_ht = Z_ARRVAL_P(PS(http_session_vars)); \
  217. int key_type; \
  218. \
  219. for (zend_hash_internal_pointer_reset(_ht); \
  220. (key_type = zend_hash_get_current_key_ex(_ht, &key, &key_length, &num_key, 0, NULL)) != HASH_KEY_NON_EXISTENT; \
  221. zend_hash_move_forward(_ht)) { \
  222. if (key_type == HASH_KEY_IS_LONG) { \
  223. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Skipping numeric key %ld", num_key); \
  224. continue; \
  225. } \
  226. key_length--; \
  227. if (php_get_session_var(key, key_length, &struc TSRMLS_CC) == SUCCESS) { \
  228. code; \
  229. } \
  230. } \
  231. } while(0)
  232. PHPAPI ZEND_EXTERN_MODULE_GLOBALS(ps)
  233. void php_session_auto_start(void *data);
  234. #define PS_CLASS_NAME "SessionHandler"
  235. extern zend_class_entry *php_session_class_entry;
  236. #define PS_IFACE_NAME "SessionHandlerInterface"
  237. extern zend_class_entry *php_session_iface_entry;
  238. #define PS_SID_IFACE_NAME "SessionIdInterface"
  239. extern zend_class_entry *php_session_id_iface_entry;
  240. extern PHP_METHOD(SessionHandler, open);
  241. extern PHP_METHOD(SessionHandler, close);
  242. extern PHP_METHOD(SessionHandler, read);
  243. extern PHP_METHOD(SessionHandler, write);
  244. extern PHP_METHOD(SessionHandler, destroy);
  245. extern PHP_METHOD(SessionHandler, gc);
  246. extern PHP_METHOD(SessionHandler, create_sid);
  247. #endif