root/ext/session/session.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. php_rinit_session_globals
  2. php_rshutdown_session_globals
  3. php_session_destroy
  4. php_add_session_var
  5. php_set_session_var
  6. php_get_session_var
  7. php_session_track_init
  8. php_session_encode
  9. php_session_decode
  10. bin_to_readable
  11. php_session_create_id
  12. php_session_valid_key
  13. php_session_initialize
  14. php_session_save_current_state
  15. PHP_INI_MH
  16. PHP_INI_MH
  17. PHP_INI_MH
  18. PHP_INI_MH
  19. PHP_INI_MH
  20. PHP_INI_MH
  21. PHP_INI_MH
  22. ZEND_INI_MH
  23. PHP_INI_BEGIN
  24. PS_SERIALIZER_DECODE_FUNC
  25. PS_SERIALIZER_ENCODE_FUNC
  26. PS_SERIALIZER_DECODE_FUNC
  27. PS_SERIALIZER_ENCODE_FUNC
  28. PS_SERIALIZER_DECODE_FUNC
  29. php_session_register_serializer
  30. php_session_register_module
  31. strcpy_gmt
  32. last_modified
  33. CACHE_LIMITER_FUNC
  34. CACHE_LIMITER_FUNC
  35. CACHE_LIMITER_FUNC
  36. CACHE_LIMITER_FUNC
  37. php_session_cache_limiter
  38. php_session_remove_cookie
  39. php_session_send_cookie
  40. _php_find_ps_module
  41. _php_find_ps_serializer
  42. ppid2sid
  43. php_session_reset_id
  44. php_session_start
  45. php_session_flush
  46. php_session_abort
  47. php_session_reset
  48. session_adapt_url
  49. PHP_FUNCTION
  50. PHP_FUNCTION
  51. PHP_FUNCTION
  52. PHP_FUNCTION
  53. PHP_FUNCTION
  54. PHP_FUNCTION
  55. PHP_FUNCTION
  56. PHP_FUNCTION
  57. PHP_FUNCTION
  58. PHP_FUNCTION
  59. PHP_FUNCTION
  60. PHP_FUNCTION
  61. PHP_FUNCTION
  62. PHP_FUNCTION
  63. PHP_FUNCTION
  64. PHP_FUNCTION
  65. PHP_FUNCTION
  66. PHP_FUNCTION
  67. PHP_FUNCTION
  68. PHP_FUNCTION
  69. php_rinit_session
  70. PHP_RINIT_FUNCTION
  71. PHP_RSHUTDOWN_FUNCTION
  72. PHP_GINIT_FUNCTION
  73. PHP_MINIT_FUNCTION
  74. PHP_MSHUTDOWN_FUNCTION
  75. PHP_MINFO_FUNCTION
  76. early_find_sid_in
  77. php_session_rfc1867_early_find_sid
  78. php_check_cancel_upload
  79. php_session_rfc1867_update
  80. php_session_rfc1867_cleanup
  81. php_session_rfc1867_callback

   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    | Authors: Sascha Schumann <sascha@schumann.cx>                        |
  16    |          Andrei Zmievski <andrei@php.net>                            |
  17    +----------------------------------------------------------------------+
  18  */
  19 
  20 /* $Id$ */
  21 
  22 #ifdef HAVE_CONFIG_H
  23 #include "config.h"
  24 #endif
  25 
  26 #include "php.h"
  27 
  28 #ifdef PHP_WIN32
  29 # include "win32/winutil.h"
  30 # include "win32/time.h"
  31 #else
  32 # include <sys/time.h>
  33 #endif
  34 
  35 #include <sys/stat.h>
  36 #include <fcntl.h>
  37 
  38 #include "php_ini.h"
  39 #include "SAPI.h"
  40 #include "rfc1867.h"
  41 #include "php_variables.h"
  42 #include "php_session.h"
  43 #include "ext/standard/md5.h"
  44 #include "ext/standard/sha1.h"
  45 #include "ext/standard/php_var.h"
  46 #include "ext/date/php_date.h"
  47 #include "ext/standard/php_lcg.h"
  48 #include "ext/standard/url_scanner_ex.h"
  49 #include "ext/standard/php_rand.h" /* for RAND_MAX */
  50 #include "ext/standard/info.h"
  51 #include "ext/standard/php_smart_str.h"
  52 #include "ext/standard/url.h"
  53 #include "ext/standard/basic_functions.h"
  54 
  55 #include "mod_files.h"
  56 #include "mod_user.h"
  57 
  58 #ifdef HAVE_LIBMM
  59 #include "mod_mm.h"
  60 #endif
  61 
  62 PHPAPI ZEND_DECLARE_MODULE_GLOBALS(ps)
  63 
  64 static int php_session_rfc1867_callback(unsigned int event, void *event_data, void **extra TSRMLS_DC);
  65 static int (*php_session_rfc1867_orig_callback)(unsigned int event, void *event_data, void **extra TSRMLS_DC);
  66 
  67 /* SessionHandler class */
  68 zend_class_entry *php_session_class_entry;
  69 
  70 /* SessionHandlerInterface */
  71 zend_class_entry *php_session_iface_entry;
  72 
  73 /* SessionIdInterface */
  74 zend_class_entry *php_session_id_iface_entry;
  75 
  76 /* ***********
  77    * Helpers *
  78    *********** */
  79 
  80 #define IF_SESSION_VARS() \
  81         if (PS(http_session_vars) && PS(http_session_vars)->type == IS_ARRAY)
  82 
  83 #define SESSION_CHECK_ACTIVE_STATE      \
  84         if (PS(session_status) == php_session_active) { \
  85                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "A session is active. You cannot change the session module's ini settings at this time");   \
  86                 return FAILURE; \
  87         }
  88 
  89 static void php_session_send_cookie(TSRMLS_D);
  90 
  91 /* Dispatched by RINIT and by php_session_destroy */
  92 static inline void php_rinit_session_globals(TSRMLS_D) /* {{{ */
  93 {
  94         PS(id) = NULL;
  95         PS(session_status) = php_session_none;
  96         PS(mod_data) = NULL;
  97         PS(mod_user_is_open) = 0;
  98         /* Do NOT init PS(mod_user_names) here! */
  99         PS(http_session_vars) = NULL;
 100 }
 101 /* }}} */
 102 
 103 /* Dispatched by RSHUTDOWN and by php_session_destroy */
 104 static inline void php_rshutdown_session_globals(TSRMLS_D) /* {{{ */
 105 {
 106         if (PS(http_session_vars)) {
 107                 zval_ptr_dtor(&PS(http_session_vars));
 108                 PS(http_session_vars) = NULL;
 109         }
 110         /* Do NOT destroy PS(mod_user_names) here! */
 111         if (PS(mod_data) || PS(mod_user_implemented)) {
 112                 zend_try {
 113                         PS(mod)->s_close(&PS(mod_data) TSRMLS_CC);
 114                 } zend_end_try();
 115         }
 116         if (PS(id)) {
 117                 efree(PS(id));
 118                 PS(id) = NULL;
 119         }
 120 }
 121 /* }}} */
 122 
 123 static int php_session_destroy(TSRMLS_D) /* {{{ */
 124 {
 125         int retval = SUCCESS;
 126 
 127         if (PS(session_status) != php_session_active) {
 128                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Trying to destroy uninitialized session");
 129                 return FAILURE;
 130         }
 131 
 132         if (PS(id) && PS(mod)->s_destroy(&PS(mod_data), PS(id) TSRMLS_CC) == FAILURE) {
 133                 retval = FAILURE;
 134                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Session object destruction failed");
 135         }
 136 
 137         php_rshutdown_session_globals(TSRMLS_C);
 138         php_rinit_session_globals(TSRMLS_C);
 139 
 140         return retval;
 141 }
 142 /* }}} */
 143 
 144 PHPAPI void php_add_session_var(char *name, size_t namelen TSRMLS_DC) /* {{{ */
 145 {
 146         zval **sym_track = NULL;
 147 
 148         IF_SESSION_VARS() {
 149                 zend_hash_find(Z_ARRVAL_P(PS(http_session_vars)), name, namelen + 1, (void *) &sym_track);
 150         } else {
 151                 return;
 152         }
 153 
 154         if (sym_track == NULL) {
 155                 zval *empty_var;
 156 
 157                 ALLOC_INIT_ZVAL(empty_var);
 158                 ZEND_SET_SYMBOL_WITH_LENGTH(Z_ARRVAL_P(PS(http_session_vars)), name, namelen+1, empty_var, 1, 0);
 159         }
 160 }
 161 /* }}} */
 162 
 163 PHPAPI void php_set_session_var(char *name, size_t namelen, zval *state_val, php_unserialize_data_t *var_hash TSRMLS_DC) /* {{{ */
 164 {
 165         IF_SESSION_VARS() {
 166                 zend_set_hash_symbol(state_val, name, namelen, PZVAL_IS_REF(state_val), 1, Z_ARRVAL_P(PS(http_session_vars)));
 167         }
 168 }
 169 /* }}} */
 170 
 171 PHPAPI int php_get_session_var(char *name, size_t namelen, zval ***state_var TSRMLS_DC) /* {{{ */
 172 {
 173         int ret = FAILURE;
 174 
 175         IF_SESSION_VARS() {
 176                 ret = zend_hash_find(Z_ARRVAL_P(PS(http_session_vars)), name, namelen + 1, (void **) state_var);
 177         }
 178         return ret;
 179 }
 180 /* }}} */
 181 
 182 static void php_session_track_init(TSRMLS_D) /* {{{ */
 183 {
 184         zval *session_vars = NULL;
 185 
 186         /* Unconditionally destroy existing array -- possible dirty data */
 187         zend_delete_global_variable("_SESSION", sizeof("_SESSION")-1 TSRMLS_CC);
 188 
 189         if (PS(http_session_vars)) {
 190                 zval_ptr_dtor(&PS(http_session_vars));
 191         }
 192 
 193         MAKE_STD_ZVAL(session_vars);
 194         array_init(session_vars);
 195         PS(http_session_vars) = session_vars;
 196 
 197         ZEND_SET_GLOBAL_VAR_WITH_LENGTH("_SESSION", sizeof("_SESSION"), PS(http_session_vars), 2, 1);
 198 }
 199 /* }}} */
 200 
 201 static char *php_session_encode(int *newlen TSRMLS_DC) /* {{{ */
 202 {
 203         char *ret = NULL;
 204 
 205         IF_SESSION_VARS() {
 206                 if (!PS(serializer)) {
 207                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown session.serialize_handler. Failed to encode session object");
 208                         ret = NULL;
 209                 } else if (PS(serializer)->encode(&ret, newlen TSRMLS_CC) == FAILURE) {
 210                         ret = NULL;
 211                 }
 212         } else {
 213                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot encode non-existent session");
 214         }
 215         return ret;
 216 }
 217 /* }}} */
 218 
 219 static int php_session_decode(const char *val, int vallen TSRMLS_DC) /* {{{ */
 220 {
 221         if (!PS(serializer)) {
 222                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown session.serialize_handler. Failed to decode session object");
 223                 return FAILURE;
 224         }
 225         if (PS(serializer)->decode(val, vallen TSRMLS_CC) == FAILURE) {
 226                 php_session_destroy(TSRMLS_C);
 227                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to decode session object. Session has been destroyed");
 228                 return FAILURE;
 229         }
 230         return SUCCESS;
 231 }
 232 /* }}} */
 233 
 234 /*
 235  * Note that we cannot use the BASE64 alphabet here, because
 236  * it contains "/" and "+": both are unacceptable for simple inclusion
 237  * into URLs.
 238  */
 239 
 240 static char hexconvtab[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,-";
 241 
 242 enum {
 243         PS_HASH_FUNC_MD5,
 244         PS_HASH_FUNC_SHA1,
 245         PS_HASH_FUNC_OTHER
 246 };
 247 
 248 /* returns a pointer to the byte after the last valid character in out */
 249 static char *bin_to_readable(char *in, size_t inlen, char *out, char nbits) /* {{{ */
 250 {
 251         unsigned char *p, *q;
 252         unsigned short w;
 253         int mask;
 254         int have;
 255 
 256         p = (unsigned char *) in;
 257         q = (unsigned char *)in + inlen;
 258 
 259         w = 0;
 260         have = 0;
 261         mask = (1 << nbits) - 1;
 262 
 263         while (1) {
 264                 if (have < nbits) {
 265                         if (p < q) {
 266                                 w |= *p++ << have;
 267                                 have += 8;
 268                         } else {
 269                                 /* consumed everything? */
 270                                 if (have == 0) break;
 271                                 /* No? We need a final round */
 272                                 have = nbits;
 273                         }
 274                 }
 275 
 276                 /* consume nbits */
 277                 *out++ = hexconvtab[w & mask];
 278                 w >>= nbits;
 279                 have -= nbits;
 280         }
 281 
 282         *out = '\0';
 283         return out;
 284 }
 285 /* }}} */
 286 
 287 PHPAPI char *php_session_create_id(PS_CREATE_SID_ARGS) /* {{{ */
 288 {
 289         PHP_MD5_CTX md5_context;
 290         PHP_SHA1_CTX sha1_context;
 291 #if defined(HAVE_HASH_EXT) && !defined(COMPILE_DL_HASH)
 292         void *hash_context = NULL;
 293 #endif
 294         unsigned char *digest;
 295         int digest_len;
 296         int j;
 297         char *buf, *outid;
 298         struct timeval tv;
 299         zval **array;
 300         zval **token;
 301         char *remote_addr = NULL;
 302 
 303         gettimeofday(&tv, NULL);
 304 
 305         if (zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void **) &array) == SUCCESS &&
 306                 Z_TYPE_PP(array) == IS_ARRAY &&
 307                 zend_hash_find(Z_ARRVAL_PP(array), "REMOTE_ADDR", sizeof("REMOTE_ADDR"), (void **) &token) == SUCCESS &&
 308                 Z_TYPE_PP(token) == IS_STRING
 309         ) {
 310                 remote_addr = Z_STRVAL_PP(token);
 311         }
 312 
 313         /* maximum 15+19+19+10 bytes */
 314         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);
 315 
 316         switch (PS(hash_func)) {
 317                 case PS_HASH_FUNC_MD5:
 318                         PHP_MD5Init(&md5_context);
 319                         PHP_MD5Update(&md5_context, (unsigned char *) buf, strlen(buf));
 320                         digest_len = 16;
 321                         break;
 322                 case PS_HASH_FUNC_SHA1:
 323                         PHP_SHA1Init(&sha1_context);
 324                         PHP_SHA1Update(&sha1_context, (unsigned char *) buf, strlen(buf));
 325                         digest_len = 20;
 326                         break;
 327 #if defined(HAVE_HASH_EXT) && !defined(COMPILE_DL_HASH)
 328                 case PS_HASH_FUNC_OTHER:
 329                         if (!PS(hash_ops)) {
 330                                 php_error_docref(NULL TSRMLS_CC, E_ERROR, "Invalid session hash function");
 331                                 efree(buf);
 332                                 return NULL;
 333                         }
 334 
 335                         hash_context = emalloc(PS(hash_ops)->context_size);
 336                         PS(hash_ops)->hash_init(hash_context);
 337                         PS(hash_ops)->hash_update(hash_context, (unsigned char *) buf, strlen(buf));
 338                         digest_len = PS(hash_ops)->digest_size;
 339                         break;
 340 #endif /* HAVE_HASH_EXT */
 341                 default:
 342                         php_error_docref(NULL TSRMLS_CC, E_ERROR, "Invalid session hash function");
 343                         efree(buf);
 344                         return NULL;
 345         }
 346         efree(buf);
 347 
 348         if (PS(entropy_length) > 0) {
 349 #ifdef PHP_WIN32
 350                 unsigned char rbuf[2048];
 351                 size_t toread = PS(entropy_length);
 352 
 353                 if (php_win32_get_random_bytes(rbuf, MIN(toread, sizeof(rbuf))) == SUCCESS){
 354 
 355                         switch (PS(hash_func)) {
 356                                 case PS_HASH_FUNC_MD5:
 357                                         PHP_MD5Update(&md5_context, rbuf, toread);
 358                                         break;
 359                                 case PS_HASH_FUNC_SHA1:
 360                                         PHP_SHA1Update(&sha1_context, rbuf, toread);
 361                                         break;
 362 # if defined(HAVE_HASH_EXT) && !defined(COMPILE_DL_HASH)
 363                                 case PS_HASH_FUNC_OTHER:
 364                                         PS(hash_ops)->hash_update(hash_context, rbuf, toread);
 365                                         break;
 366 # endif /* HAVE_HASH_EXT */
 367                         }
 368                 }
 369 #else
 370                 int fd;
 371 
 372                 fd = VCWD_OPEN(PS(entropy_file), O_RDONLY);
 373                 if (fd >= 0) {
 374                         unsigned char rbuf[2048];
 375                         int n;
 376                         int to_read = PS(entropy_length);
 377 
 378                         while (to_read > 0) {
 379                                 n = read(fd, rbuf, MIN(to_read, sizeof(rbuf)));
 380                                 if (n <= 0) break;
 381 
 382                                 switch (PS(hash_func)) {
 383                                         case PS_HASH_FUNC_MD5:
 384                                                 PHP_MD5Update(&md5_context, rbuf, n);
 385                                                 break;
 386                                         case PS_HASH_FUNC_SHA1:
 387                                                 PHP_SHA1Update(&sha1_context, rbuf, n);
 388                                                 break;
 389 #if defined(HAVE_HASH_EXT) && !defined(COMPILE_DL_HASH)
 390                                         case PS_HASH_FUNC_OTHER:
 391                                                 PS(hash_ops)->hash_update(hash_context, rbuf, n);
 392                                                 break;
 393 #endif /* HAVE_HASH_EXT */
 394                                 }
 395                                 to_read -= n;
 396                         }
 397                         close(fd);
 398                 }
 399 #endif
 400         }
 401 
 402         digest = emalloc(digest_len + 1);
 403         switch (PS(hash_func)) {
 404                 case PS_HASH_FUNC_MD5:
 405                         PHP_MD5Final(digest, &md5_context);
 406                         break;
 407                 case PS_HASH_FUNC_SHA1:
 408                         PHP_SHA1Final(digest, &sha1_context);
 409                         break;
 410 #if defined(HAVE_HASH_EXT) && !defined(COMPILE_DL_HASH)
 411                 case PS_HASH_FUNC_OTHER:
 412                         PS(hash_ops)->hash_final(digest, hash_context);
 413                         efree(hash_context);
 414                         break;
 415 #endif /* HAVE_HASH_EXT */
 416         }
 417 
 418         if (PS(hash_bits_per_character) < 4
 419                         || PS(hash_bits_per_character) > 6) {
 420                 PS(hash_bits_per_character) = 4;
 421 
 422                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "The ini setting hash_bits_per_character is out of range (should be 4, 5, or 6) - using 4 for now");
 423         }
 424 
 425         outid = emalloc((size_t)((digest_len + 2) * ((8.0f / PS(hash_bits_per_character)) + 0.5)));
 426         j = (int) (bin_to_readable((char *)digest, digest_len, outid, (char)PS(hash_bits_per_character)) - outid);
 427         efree(digest);
 428 
 429         if (newlen) {
 430                 *newlen = j;
 431         }
 432 
 433         return outid;
 434 }
 435 /* }}} */
 436 
 437 /* Default session id char validation function allowed by ps_modules.
 438  * If you change the logic here, please also update the error message in
 439  * ps_modules appropriately */
 440 PHPAPI int php_session_valid_key(const char *key) /* {{{ */
 441 {
 442         size_t len;
 443         const char *p;
 444         char c;
 445         int ret = SUCCESS;
 446 
 447         for (p = key; (c = *p); p++) {
 448                 /* valid characters are a..z,A..Z,0..9 */
 449                 if (!((c >= 'a' && c <= 'z')
 450                                 || (c >= 'A' && c <= 'Z')
 451                                 || (c >= '0' && c <= '9')
 452                                 || c == ','
 453                                 || c == '-')) {
 454                         ret = FAILURE;
 455                         break;
 456                 }
 457         }
 458 
 459         len = p - key;
 460 
 461         /* Somewhat arbitrary length limit here, but should be way more than
 462            anyone needs and avoids file-level warnings later on if we exceed MAX_PATH */
 463         if (len == 0 || len > 128) {
 464                 ret = FAILURE;
 465         }
 466 
 467         return ret;
 468 }
 469 /* }}} */
 470 
 471 static void php_session_initialize(TSRMLS_D) /* {{{ */
 472 {
 473         char *val = NULL;
 474         int vallen;
 475 
 476         if (!PS(mod)) {
 477                 php_error_docref(NULL TSRMLS_CC, E_ERROR, "No storage module chosen - failed to initialize session");
 478                 return;
 479         }
 480 
 481         /* Open session handler first */
 482         if (PS(mod)->s_open(&PS(mod_data), PS(save_path), PS(session_name) TSRMLS_CC) == FAILURE) {
 483                 php_error_docref(NULL TSRMLS_CC, E_ERROR, "Failed to initialize storage module: %s (path: %s)", PS(mod)->s_name, PS(save_path));
 484                 return;
 485         }
 486 
 487         /* If there is no ID, use session module to create one */
 488         if (!PS(id)) {
 489                 PS(id) = PS(mod)->s_create_sid(&PS(mod_data), NULL TSRMLS_CC);
 490                 if (!PS(id)) {
 491                         php_error_docref(NULL TSRMLS_CC, E_ERROR, "Failed to create session ID: %s (path: %s)", PS(mod)->s_name, PS(save_path));
 492                         return;
 493                 }
 494                 if (PS(use_cookies)) {
 495                         PS(send_cookie) = 1;
 496                 }
 497         }
 498 
 499         /* Set session ID for compatibility for older/3rd party save handlers */
 500         if (!PS(use_strict_mode)) {
 501                 php_session_reset_id(TSRMLS_C);
 502                 PS(session_status) = php_session_active;
 503         }
 504 
 505         /* Read data */
 506         php_session_track_init(TSRMLS_C);
 507         if (PS(mod)->s_read(&PS(mod_data), PS(id), &val, &vallen TSRMLS_CC) == FAILURE) {
 508                 /* Some broken save handler implementation returns FAILURE for non-existent session ID */
 509                 /* It's better to raise error for this, but disabled error for better compatibility */
 510                 /*
 511                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Failed to read session data: %s (path: %s)", PS(mod)->s_name, PS(save_path));
 512                 */
 513         }
 514         /* Set session ID if session read didn't activated session */
 515         if (PS(use_strict_mode) && PS(session_status) != php_session_active) {
 516                 php_session_reset_id(TSRMLS_C);
 517                 PS(session_status) = php_session_active;
 518         }
 519         if (val) {
 520                 php_session_decode(val, vallen TSRMLS_CC);
 521                 str_efree(val);
 522         }
 523 
 524         if (!PS(use_cookies) && PS(send_cookie)) {
 525                 if (PS(use_trans_sid) && !PS(use_only_cookies)) {
 526                         PS(apply_trans_sid) = 1;
 527                 }
 528                 PS(send_cookie) = 0;
 529         }
 530 }
 531 /* }}} */
 532 
 533 static void php_session_save_current_state(TSRMLS_D) /* {{{ */
 534 {
 535         int ret = FAILURE;
 536 
 537         IF_SESSION_VARS() {
 538                 if (PS(mod_data) || PS(mod_user_implemented)) {
 539                         char *val;
 540                         int vallen;
 541 
 542                         val = php_session_encode(&vallen TSRMLS_CC);
 543                         if (val) {
 544                                 ret = PS(mod)->s_write(&PS(mod_data), PS(id), val, vallen TSRMLS_CC);
 545                                 efree(val);
 546                         } else {
 547                                 ret = PS(mod)->s_write(&PS(mod_data), PS(id), "", 0 TSRMLS_CC);
 548                         }
 549                 }
 550 
 551                 if (ret == FAILURE) {
 552                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to write session data (%s). Please "
 553                                         "verify that the current setting of session.save_path "
 554                                         "is correct (%s)",
 555                                         PS(mod)->s_name,
 556                                         PS(save_path));
 557                 }
 558         }
 559 
 560         if (PS(mod_data) || PS(mod_user_implemented)) {
 561                 PS(mod)->s_close(&PS(mod_data) TSRMLS_CC);
 562         }
 563 }
 564 /* }}} */
 565 
 566 /* *************************
 567    * INI Settings/Handlers *
 568    ************************* */
 569 
 570 static PHP_INI_MH(OnUpdateSaveHandler) /* {{{ */
 571 {
 572         ps_module *tmp;
 573         SESSION_CHECK_ACTIVE_STATE;
 574 
 575         tmp = _php_find_ps_module(new_value TSRMLS_CC);
 576 
 577         if (PG(modules_activated) && !tmp) {
 578                 int err_type;
 579 
 580                 if (stage == ZEND_INI_STAGE_RUNTIME) {
 581                         err_type = E_WARNING;
 582                 } else {
 583                         err_type = E_ERROR;
 584                 }
 585 
 586                 /* Do not output error when restoring ini options. */
 587                 if (stage != ZEND_INI_STAGE_DEACTIVATE) {
 588                         php_error_docref(NULL TSRMLS_CC, err_type, "Cannot find save handler '%s'", new_value);
 589                 }
 590                 return FAILURE;
 591         }
 592 
 593         PS(default_mod) = PS(mod);
 594         PS(mod) = tmp;
 595 
 596         return SUCCESS;
 597 }
 598 /* }}} */
 599 
 600 static PHP_INI_MH(OnUpdateSerializer) /* {{{ */
 601 {
 602         const ps_serializer *tmp;
 603         SESSION_CHECK_ACTIVE_STATE;
 604 
 605         tmp = _php_find_ps_serializer(new_value TSRMLS_CC);
 606 
 607         if (PG(modules_activated) && !tmp) {
 608                 int err_type;
 609 
 610                 if (stage == ZEND_INI_STAGE_RUNTIME) {
 611                         err_type = E_WARNING;
 612                 } else {
 613                         err_type = E_ERROR;
 614                 }
 615 
 616                 /* Do not output error when restoring ini options. */
 617                 if (stage != ZEND_INI_STAGE_DEACTIVATE) {
 618                         php_error_docref(NULL TSRMLS_CC, err_type, "Cannot find serialization handler '%s'", new_value);
 619                 }
 620                 return FAILURE;
 621         }
 622         PS(serializer) = tmp;
 623 
 624         return SUCCESS;
 625 }
 626 /* }}} */
 627 
 628 static PHP_INI_MH(OnUpdateTransSid) /* {{{ */
 629 {
 630         SESSION_CHECK_ACTIVE_STATE;
 631 
 632         if (!strncasecmp(new_value, "on", sizeof("on"))) {
 633                 PS(use_trans_sid) = (zend_bool) 1;
 634         } else {
 635                 PS(use_trans_sid) = (zend_bool) atoi(new_value);
 636         }
 637 
 638         return SUCCESS;
 639 }
 640 /* }}} */
 641 
 642 static PHP_INI_MH(OnUpdateSaveDir) /* {{{ */
 643 {
 644         /* Only do the safemode/open_basedir check at runtime */
 645         if (stage == PHP_INI_STAGE_RUNTIME || stage == PHP_INI_STAGE_HTACCESS) {
 646                 char *p;
 647 
 648                 if (memchr(new_value, '\0', new_value_length) != NULL) {
 649                         return FAILURE;
 650                 }
 651 
 652                 /* we do not use zend_memrchr() since path can contain ; itself */
 653                 if ((p = strchr(new_value, ';'))) {
 654                         char *p2;
 655                         p++;
 656                         if ((p2 = strchr(p, ';'))) {
 657                                 p = p2 + 1;
 658                         }
 659                 } else {
 660                         p = new_value;
 661                 }
 662 
 663                 if (PG(open_basedir) && *p && php_check_open_basedir(p TSRMLS_CC)) {
 664                         return FAILURE;
 665                 }
 666         }
 667 
 668         OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
 669         return SUCCESS;
 670 }
 671 /* }}} */
 672 
 673 static PHP_INI_MH(OnUpdateName) /* {{{ */
 674 {
 675         /* Numeric session.name won't work at all */
 676         if ((!new_value_length || is_numeric_string(new_value, new_value_length, NULL, NULL, 0))) {
 677                 int err_type;
 678 
 679                 if (stage == ZEND_INI_STAGE_RUNTIME || stage == ZEND_INI_STAGE_ACTIVATE || stage == ZEND_INI_STAGE_STARTUP) {
 680                         err_type = E_WARNING;
 681                 } else {
 682                         err_type = E_ERROR;
 683                 }
 684 
 685                 /* Do not output error when restoring ini options. */
 686                 if (stage != ZEND_INI_STAGE_DEACTIVATE) {
 687                         php_error_docref(NULL TSRMLS_CC, err_type, "session.name cannot be a numeric or empty '%s'", new_value);
 688                 }
 689                 return FAILURE;
 690         }
 691 
 692         OnUpdateStringUnempty(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
 693         return SUCCESS;
 694 }
 695 /* }}} */
 696 
 697 static PHP_INI_MH(OnUpdateHashFunc) /* {{{ */
 698 {
 699         long val;
 700         char *endptr = NULL;
 701 
 702 #if defined(HAVE_HASH_EXT) && !defined(COMPILE_DL_HASH)
 703         PS(hash_ops) = NULL;
 704 #endif
 705 
 706         val = strtol(new_value, &endptr, 10);
 707         if (endptr && (*endptr == '\0')) {
 708                 /* Numeric value */
 709                 PS(hash_func) = val ? 1 : 0;
 710 
 711                 return SUCCESS;
 712         }
 713 
 714         if (new_value_length == (sizeof("md5") - 1) &&
 715                 strncasecmp(new_value, "md5", sizeof("md5") - 1) == 0) {
 716                 PS(hash_func) = PS_HASH_FUNC_MD5;
 717 
 718                 return SUCCESS;
 719         }
 720 
 721         if (new_value_length == (sizeof("sha1") - 1) &&
 722                 strncasecmp(new_value, "sha1", sizeof("sha1") - 1) == 0) {
 723                 PS(hash_func) = PS_HASH_FUNC_SHA1;
 724 
 725                 return SUCCESS;
 726         }
 727 
 728 #if defined(HAVE_HASH_EXT) && !defined(COMPILE_DL_HASH) /* {{{ */
 729 {
 730         php_hash_ops *ops = (php_hash_ops*)php_hash_fetch_ops(new_value, new_value_length);
 731 
 732         if (ops) {
 733                 PS(hash_func) = PS_HASH_FUNC_OTHER;
 734                 PS(hash_ops) = ops;
 735 
 736                 return SUCCESS;
 737         }
 738 }
 739 #endif /* HAVE_HASH_EXT }}} */
 740 
 741         php_error_docref(NULL TSRMLS_CC, E_WARNING, "session.configuration 'session.hash_function' must be existing hash function. %s does not exist.", new_value);
 742         return FAILURE;
 743 }
 744 /* }}} */
 745 
 746 static PHP_INI_MH(OnUpdateRfc1867Freq) /* {{{ */
 747 {
 748         int tmp;
 749         tmp = zend_atoi(new_value, new_value_length);
 750         if(tmp < 0) {
 751                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "session.upload_progress.freq must be greater than or equal to zero");
 752                 return FAILURE;
 753         }
 754         if(new_value_length > 0 && new_value[new_value_length-1] == '%') {
 755                 if(tmp > 100) {
 756                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "session.upload_progress.freq cannot be over 100%%");
 757                         return FAILURE;
 758                 }
 759                 PS(rfc1867_freq) = -tmp;
 760         } else {
 761                 PS(rfc1867_freq) = tmp;
 762         }
 763         return SUCCESS;
 764 } /* }}} */
 765 
 766 static ZEND_INI_MH(OnUpdateSmartStr) /* {{{ */
 767 {
 768         smart_str *p;
 769 #ifndef ZTS
 770         char *base = (char *) mh_arg2;
 771 #else
 772         char *base;
 773 
 774         base = (char *) ts_resource(*((int *) mh_arg2));
 775 #endif
 776 
 777         p = (smart_str *) (base+(size_t) mh_arg1);
 778 
 779         smart_str_sets(p, new_value);
 780 
 781         return SUCCESS;
 782 }
 783 /* }}} */
 784 
 785 /* {{{ PHP_INI
 786  */
 787 PHP_INI_BEGIN()
 788         STD_PHP_INI_ENTRY("session.save_path",          "",          PHP_INI_ALL, OnUpdateSaveDir,save_path,          php_ps_globals,    ps_globals)
 789         STD_PHP_INI_ENTRY("session.name",               "PHPSESSID", PHP_INI_ALL, OnUpdateName, session_name,       php_ps_globals,    ps_globals)
 790         PHP_INI_ENTRY("session.save_handler",           "files",     PHP_INI_ALL, OnUpdateSaveHandler)
 791         STD_PHP_INI_BOOLEAN("session.auto_start",       "0",         PHP_INI_PERDIR, OnUpdateBool,   auto_start,         php_ps_globals,    ps_globals)
 792         STD_PHP_INI_ENTRY("session.gc_probability",     "1",         PHP_INI_ALL, OnUpdateLong,   gc_probability,     php_ps_globals,    ps_globals)
 793         STD_PHP_INI_ENTRY("session.gc_divisor",         "100",       PHP_INI_ALL, OnUpdateLong,   gc_divisor,         php_ps_globals,    ps_globals)
 794         STD_PHP_INI_ENTRY("session.gc_maxlifetime",     "1440",      PHP_INI_ALL, OnUpdateLong,   gc_maxlifetime,     php_ps_globals,    ps_globals)
 795         PHP_INI_ENTRY("session.serialize_handler",      "php",       PHP_INI_ALL, OnUpdateSerializer)
 796         STD_PHP_INI_ENTRY("session.cookie_lifetime",    "0",         PHP_INI_ALL, OnUpdateLong,   cookie_lifetime,    php_ps_globals,    ps_globals)
 797         STD_PHP_INI_ENTRY("session.cookie_path",        "/",         PHP_INI_ALL, OnUpdateString, cookie_path,        php_ps_globals,    ps_globals)
 798         STD_PHP_INI_ENTRY("session.cookie_domain",      "",          PHP_INI_ALL, OnUpdateString, cookie_domain,      php_ps_globals,    ps_globals)
 799         STD_PHP_INI_BOOLEAN("session.cookie_secure",    "",          PHP_INI_ALL, OnUpdateBool,   cookie_secure,      php_ps_globals,    ps_globals)
 800         STD_PHP_INI_BOOLEAN("session.cookie_httponly",  "",          PHP_INI_ALL, OnUpdateBool,   cookie_httponly,    php_ps_globals,    ps_globals)
 801         STD_PHP_INI_BOOLEAN("session.use_cookies",      "1",         PHP_INI_ALL, OnUpdateBool,   use_cookies,        php_ps_globals,    ps_globals)
 802         STD_PHP_INI_BOOLEAN("session.use_only_cookies", "1",         PHP_INI_ALL, OnUpdateBool,   use_only_cookies,   php_ps_globals,    ps_globals)
 803         STD_PHP_INI_BOOLEAN("session.use_strict_mode",  "0",         PHP_INI_ALL, OnUpdateBool,   use_strict_mode,    php_ps_globals,    ps_globals)
 804         STD_PHP_INI_ENTRY("session.referer_check",      "",          PHP_INI_ALL, OnUpdateString, extern_referer_chk, php_ps_globals,    ps_globals)
 805 #if HAVE_DEV_URANDOM
 806         STD_PHP_INI_ENTRY("session.entropy_file",       "/dev/urandom",          PHP_INI_ALL, OnUpdateString, entropy_file,       php_ps_globals,    ps_globals)
 807         STD_PHP_INI_ENTRY("session.entropy_length",     "32",         PHP_INI_ALL, OnUpdateLong,   entropy_length,     php_ps_globals,    ps_globals)
 808 #elif HAVE_DEV_ARANDOM
 809         STD_PHP_INI_ENTRY("session.entropy_file",       "/dev/arandom",          PHP_INI_ALL, OnUpdateString, entropy_file,       php_ps_globals,    ps_globals)
 810         STD_PHP_INI_ENTRY("session.entropy_length",     "32",         PHP_INI_ALL, OnUpdateLong,   entropy_length,     php_ps_globals,    ps_globals)
 811 #else
 812         STD_PHP_INI_ENTRY("session.entropy_file",       "",          PHP_INI_ALL, OnUpdateString, entropy_file,       php_ps_globals,    ps_globals)
 813         STD_PHP_INI_ENTRY("session.entropy_length",     "0",         PHP_INI_ALL, OnUpdateLong,   entropy_length,     php_ps_globals,    ps_globals)
 814 #endif
 815         STD_PHP_INI_ENTRY("session.cache_limiter",      "nocache",   PHP_INI_ALL, OnUpdateString, cache_limiter,      php_ps_globals,    ps_globals)
 816         STD_PHP_INI_ENTRY("session.cache_expire",       "180",       PHP_INI_ALL, OnUpdateLong,   cache_expire,       php_ps_globals,    ps_globals)
 817         PHP_INI_ENTRY("session.use_trans_sid",          "0",         PHP_INI_ALL, OnUpdateTransSid)
 818         PHP_INI_ENTRY("session.hash_function",          "0",         PHP_INI_ALL, OnUpdateHashFunc)
 819         STD_PHP_INI_ENTRY("session.hash_bits_per_character", "4",    PHP_INI_ALL, OnUpdateLong,   hash_bits_per_character, php_ps_globals, ps_globals)
 820 
 821         /* Upload progress */
 822         STD_PHP_INI_BOOLEAN("session.upload_progress.enabled",
 823                                                         "1",     ZEND_INI_PERDIR, OnUpdateBool,        rfc1867_enabled, php_ps_globals, ps_globals)
 824         STD_PHP_INI_BOOLEAN("session.upload_progress.cleanup",
 825                                                         "1",     ZEND_INI_PERDIR, OnUpdateBool,        rfc1867_cleanup, php_ps_globals, ps_globals)
 826         STD_PHP_INI_ENTRY("session.upload_progress.prefix",
 827                                              "upload_progress_", ZEND_INI_PERDIR, OnUpdateSmartStr,      rfc1867_prefix,  php_ps_globals, ps_globals)
 828         STD_PHP_INI_ENTRY("session.upload_progress.name",
 829                                   "PHP_SESSION_UPLOAD_PROGRESS", ZEND_INI_PERDIR, OnUpdateSmartStr,      rfc1867_name,    php_ps_globals, ps_globals)
 830         STD_PHP_INI_ENTRY("session.upload_progress.freq",  "1%", ZEND_INI_PERDIR, OnUpdateRfc1867Freq, rfc1867_freq,    php_ps_globals, ps_globals)
 831         STD_PHP_INI_ENTRY("session.upload_progress.min_freq",
 832                                                            "1",  ZEND_INI_PERDIR, OnUpdateReal,        rfc1867_min_freq,php_ps_globals, ps_globals)
 833 
 834         /* Commented out until future discussion */
 835         /* PHP_INI_ENTRY("session.encode_sources", "globals,track", PHP_INI_ALL, NULL) */
 836 PHP_INI_END()
 837 /* }}} */
 838 
 839 /* ***************
 840    * Serializers *
 841    *************** */
 842 PS_SERIALIZER_ENCODE_FUNC(php_serialize) /* {{{ */
 843 {
 844         smart_str buf = {0};
 845         php_serialize_data_t var_hash;
 846 
 847         PHP_VAR_SERIALIZE_INIT(var_hash);
 848         php_var_serialize(&buf, &PS(http_session_vars), &var_hash TSRMLS_CC);
 849         PHP_VAR_SERIALIZE_DESTROY(var_hash);
 850         if (newlen) {
 851                 *newlen = buf.len;
 852         }
 853         smart_str_0(&buf);
 854         *newstr = buf.c;
 855         return SUCCESS;
 856 }
 857 /* }}} */
 858 
 859 PS_SERIALIZER_DECODE_FUNC(php_serialize) /* {{{ */
 860 {
 861         const char *endptr = val + vallen;
 862         zval *session_vars;
 863         php_unserialize_data_t var_hash;
 864 
 865         PHP_VAR_UNSERIALIZE_INIT(var_hash);
 866         ALLOC_INIT_ZVAL(session_vars);
 867         if (php_var_unserialize(&session_vars, &val, endptr, &var_hash TSRMLS_CC)) {
 868                 var_push_dtor(&var_hash, &session_vars);
 869         }
 870         
 871         PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
 872         if (PS(http_session_vars)) {
 873                 zval_ptr_dtor(&PS(http_session_vars));
 874         }
 875         if (Z_TYPE_P(session_vars) == IS_NULL) {
 876                 array_init(session_vars);
 877         }
 878         PS(http_session_vars) = session_vars;
 879         ZEND_SET_GLOBAL_VAR_WITH_LENGTH("_SESSION", sizeof("_SESSION"), PS(http_session_vars), Z_REFCOUNT_P(PS(http_session_vars)) + 1, 1);
 880         return SUCCESS;
 881 }
 882 /* }}} */
 883 
 884 #define PS_BIN_NR_OF_BITS 8
 885 #define PS_BIN_UNDEF (1<<(PS_BIN_NR_OF_BITS-1))
 886 #define PS_BIN_MAX (PS_BIN_UNDEF-1)
 887 
 888 PS_SERIALIZER_ENCODE_FUNC(php_binary) /* {{{ */
 889 {
 890         smart_str buf = {0};
 891         php_serialize_data_t var_hash;
 892         PS_ENCODE_VARS;
 893 
 894         PHP_VAR_SERIALIZE_INIT(var_hash);
 895 
 896         PS_ENCODE_LOOP(
 897                         if (key_length > PS_BIN_MAX) continue;
 898                         smart_str_appendc(&buf, (unsigned char) key_length);
 899                         smart_str_appendl(&buf, key, key_length);
 900                         php_var_serialize(&buf, struc, &var_hash TSRMLS_CC);
 901                 } else {
 902                         if (key_length > PS_BIN_MAX) continue;
 903                         smart_str_appendc(&buf, (unsigned char) (key_length & PS_BIN_UNDEF));
 904                         smart_str_appendl(&buf, key, key_length);
 905         );
 906 
 907         if (newlen) {
 908                 *newlen = buf.len;
 909         }
 910         smart_str_0(&buf);
 911         *newstr = buf.c;
 912         PHP_VAR_SERIALIZE_DESTROY(var_hash);
 913 
 914         return SUCCESS;
 915 }
 916 /* }}} */
 917 
 918 PS_SERIALIZER_DECODE_FUNC(php_binary) /* {{{ */
 919 {
 920         const char *p;
 921         char *name;
 922         const char *endptr = val + vallen;
 923         zval *current;
 924         int namelen;
 925         int has_value;
 926         php_unserialize_data_t var_hash;
 927 
 928         PHP_VAR_UNSERIALIZE_INIT(var_hash);
 929 
 930         for (p = val; p < endptr; ) {
 931                 zval **tmp;
 932                 namelen = ((unsigned char)(*p)) & (~PS_BIN_UNDEF);
 933 
 934                 if (namelen < 0 || namelen > PS_BIN_MAX || (p + namelen) >= endptr) {
 935                         return FAILURE;
 936                 }
 937 
 938                 has_value = *p & PS_BIN_UNDEF ? 0 : 1;
 939 
 940                 name = estrndup(p + 1, namelen);
 941 
 942                 p += namelen + 1;
 943 
 944                 if (zend_hash_find(&EG(symbol_table), name, namelen + 1, (void **) &tmp) == SUCCESS) {
 945                         if ((Z_TYPE_PP(tmp) == IS_ARRAY && Z_ARRVAL_PP(tmp) == &EG(symbol_table)) || *tmp == PS(http_session_vars)) {
 946                                 efree(name);
 947                                 continue;
 948                         }
 949                 }
 950 
 951                 if (has_value) {
 952                         ALLOC_INIT_ZVAL(current);
 953                         if (php_var_unserialize(&current, (const unsigned char **) &p, (const unsigned char *) endptr, &var_hash TSRMLS_CC)) {
 954                                 php_set_session_var(name, namelen, current, &var_hash  TSRMLS_CC);
 955                         } else {
 956                                 PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
 957                                 return FAILURE;
 958                         }
 959                         var_push_dtor_no_addref(&var_hash, &current);
 960                 }
 961                 PS_ADD_VARL(name, namelen);
 962                 efree(name);
 963         }
 964 
 965         PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
 966 
 967         return SUCCESS;
 968 }
 969 /* }}} */
 970 
 971 #define PS_DELIMITER '|'
 972 #define PS_UNDEF_MARKER '!'
 973 
 974 PS_SERIALIZER_ENCODE_FUNC(php) /* {{{ */
 975 {
 976         smart_str buf = {0};
 977         php_serialize_data_t var_hash;
 978         PS_ENCODE_VARS;
 979 
 980         PHP_VAR_SERIALIZE_INIT(var_hash);
 981 
 982         PS_ENCODE_LOOP(
 983                         smart_str_appendl(&buf, key, key_length);
 984                         if (memchr(key, PS_DELIMITER, key_length) || memchr(key, PS_UNDEF_MARKER, key_length)) {
 985                                 PHP_VAR_SERIALIZE_DESTROY(var_hash);
 986                                 smart_str_free(&buf);
 987                                 return FAILURE;
 988                         }
 989                         smart_str_appendc(&buf, PS_DELIMITER);
 990 
 991                         php_var_serialize(&buf, struc, &var_hash TSRMLS_CC);
 992                 } else {
 993                         smart_str_appendc(&buf, PS_UNDEF_MARKER);
 994                         smart_str_appendl(&buf, key, key_length);
 995                         smart_str_appendc(&buf, PS_DELIMITER);
 996         );
 997 
 998         if (newlen) {
 999                 *newlen = buf.len;
1000         }
1001         smart_str_0(&buf);
1002         *newstr = buf.c;
1003 
1004         PHP_VAR_SERIALIZE_DESTROY(var_hash);
1005         return SUCCESS;
1006 }
1007 /* }}} */
1008 
1009 PS_SERIALIZER_DECODE_FUNC(php) /* {{{ */
1010 {
1011         const char *p, *q;
1012         char *name;
1013         const char *endptr = val + vallen;
1014         zval *current;
1015         int namelen;
1016         int has_value;
1017         php_unserialize_data_t var_hash;
1018 
1019         PHP_VAR_UNSERIALIZE_INIT(var_hash);
1020 
1021         p = val;
1022 
1023         while (p < endptr) {
1024                 zval **tmp;
1025                 q = p;
1026                 while (*q != PS_DELIMITER) {
1027                         if (++q >= endptr) goto break_outer_loop;
1028                 }
1029                 if (p[0] == PS_UNDEF_MARKER) {
1030                         p++;
1031                         has_value = 0;
1032                 } else {
1033                         has_value = 1;
1034                 }
1035 
1036                 namelen = q - p;
1037                 name = estrndup(p, namelen);
1038                 q++;
1039 
1040                 if (zend_hash_find(&EG(symbol_table), name, namelen + 1, (void **) &tmp) == SUCCESS) {
1041                         if ((Z_TYPE_PP(tmp) == IS_ARRAY && Z_ARRVAL_PP(tmp) == &EG(symbol_table)) || *tmp == PS(http_session_vars)) {
1042                                 goto skip;
1043                         }
1044                 }
1045 
1046                 if (has_value) {
1047                         ALLOC_INIT_ZVAL(current);
1048                         if (php_var_unserialize(&current, (const unsigned char **) &q, (const unsigned char *) endptr, &var_hash TSRMLS_CC)) {
1049                                 php_set_session_var(name, namelen, current, &var_hash  TSRMLS_CC);
1050                         } else {
1051                                 var_push_dtor_no_addref(&var_hash, &current);
1052                                 efree(name);
1053                                 PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
1054                                 return FAILURE;
1055                         }
1056                         var_push_dtor_no_addref(&var_hash, &current);
1057                 }
1058                 PS_ADD_VARL(name, namelen);
1059 skip:
1060                 efree(name);
1061 
1062                 p = q;
1063         }
1064 break_outer_loop:
1065 
1066         PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
1067 
1068         return SUCCESS;
1069 }
1070 /* }}} */
1071 
1072 #define MAX_SERIALIZERS 32
1073 #define PREDEFINED_SERIALIZERS 3
1074 
1075 static ps_serializer ps_serializers[MAX_SERIALIZERS + 1] = {
1076         PS_SERIALIZER_ENTRY(php_serialize),
1077         PS_SERIALIZER_ENTRY(php),
1078         PS_SERIALIZER_ENTRY(php_binary)
1079 };
1080 
1081 PHPAPI int php_session_register_serializer(const char *name, int (*encode)(PS_SERIALIZER_ENCODE_ARGS), int (*decode)(PS_SERIALIZER_DECODE_ARGS)) /* {{{ */
1082 {
1083         int ret = -1;
1084         int i;
1085 
1086         for (i = 0; i < MAX_SERIALIZERS; i++) {
1087                 if (ps_serializers[i].name == NULL) {
1088                         ps_serializers[i].name = name;
1089                         ps_serializers[i].encode = encode;
1090                         ps_serializers[i].decode = decode;
1091                         ps_serializers[i + 1].name = NULL;
1092                         ret = 0;
1093                         break;
1094                 }
1095         }
1096         return ret;
1097 }
1098 /* }}} */
1099 
1100 /* *******************
1101    * Storage Modules *
1102    ******************* */
1103 
1104 #define MAX_MODULES 10
1105 #define PREDEFINED_MODULES 2
1106 
1107 static ps_module *ps_modules[MAX_MODULES + 1] = {
1108         ps_files_ptr,
1109         ps_user_ptr
1110 };
1111 
1112 PHPAPI int php_session_register_module(ps_module *ptr) /* {{{ */
1113 {
1114         int ret = -1;
1115         int i;
1116 
1117         for (i = 0; i < MAX_MODULES; i++) {
1118                 if (!ps_modules[i]) {
1119                         ps_modules[i] = ptr;
1120                         ret = 0;
1121                         break;
1122                 }
1123         }
1124         return ret;
1125 }
1126 /* }}} */
1127 
1128 /* ******************
1129    * Cache Limiters *
1130    ****************** */
1131 
1132 typedef struct {
1133         char *name;
1134         void (*func)(TSRMLS_D);
1135 } php_session_cache_limiter_t;
1136 
1137 #define CACHE_LIMITER(name) _php_cache_limiter_##name
1138 #define CACHE_LIMITER_FUNC(name) static void CACHE_LIMITER(name)(TSRMLS_D)
1139 #define CACHE_LIMITER_ENTRY(name) { #name, CACHE_LIMITER(name) },
1140 #define ADD_HEADER(a) sapi_add_header(a, strlen(a), 1);
1141 #define MAX_STR 512
1142 
1143 static char *month_names[] = {
1144         "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1145         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
1146 };
1147 
1148 static char *week_days[] = {
1149         "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
1150 };
1151 
1152 static inline void strcpy_gmt(char *ubuf, time_t *when) /* {{{ */
1153 {
1154         char buf[MAX_STR];
1155         struct tm tm, *res;
1156         int n;
1157 
1158         res = php_gmtime_r(when, &tm);
1159 
1160         if (!res) {
1161                 ubuf[0] = '\0';
1162                 return;
1163         }
1164 
1165         n = slprintf(buf, sizeof(buf), "%s, %02d %s %d %02d:%02d:%02d GMT", /* SAFE */
1166                                 week_days[tm.tm_wday], tm.tm_mday,
1167                                 month_names[tm.tm_mon], tm.tm_year + 1900,
1168                                 tm.tm_hour, tm.tm_min,
1169                                 tm.tm_sec);
1170         memcpy(ubuf, buf, n);
1171         ubuf[n] = '\0';
1172 }
1173 /* }}} */
1174 
1175 static inline void last_modified(TSRMLS_D) /* {{{ */
1176 {
1177         const char *path;
1178         struct stat sb;
1179         char buf[MAX_STR + 1];
1180 
1181         path = SG(request_info).path_translated;
1182         if (path) {
1183                 if (VCWD_STAT(path, &sb) == -1) {
1184                         return;
1185                 }
1186 
1187 #define LAST_MODIFIED "Last-Modified: "
1188                 memcpy(buf, LAST_MODIFIED, sizeof(LAST_MODIFIED) - 1);
1189                 strcpy_gmt(buf + sizeof(LAST_MODIFIED) - 1, &sb.st_mtime);
1190                 ADD_HEADER(buf);
1191         }
1192 }
1193 /* }}} */
1194 
1195 #define EXPIRES "Expires: "
1196 CACHE_LIMITER_FUNC(public) /* {{{ */
1197 {
1198         char buf[MAX_STR + 1];
1199         struct timeval tv;
1200         time_t now;
1201 
1202         gettimeofday(&tv, NULL);
1203         now = tv.tv_sec + PS(cache_expire) * 60;
1204         memcpy(buf, EXPIRES, sizeof(EXPIRES) - 1);
1205         strcpy_gmt(buf + sizeof(EXPIRES) - 1, &now);
1206         ADD_HEADER(buf);
1207 
1208         snprintf(buf, sizeof(buf) , "Cache-Control: public, max-age=%ld", PS(cache_expire) * 60); /* SAFE */
1209         ADD_HEADER(buf);
1210 
1211         last_modified(TSRMLS_C);
1212 }
1213 /* }}} */
1214 
1215 CACHE_LIMITER_FUNC(private_no_expire) /* {{{ */
1216 {
1217         char buf[MAX_STR + 1];
1218 
1219         snprintf(buf, sizeof(buf), "Cache-Control: private, max-age=%ld, pre-check=%ld", PS(cache_expire) * 60, PS(cache_expire) * 60); /* SAFE */
1220         ADD_HEADER(buf);
1221 
1222         last_modified(TSRMLS_C);
1223 }
1224 /* }}} */
1225 
1226 CACHE_LIMITER_FUNC(private) /* {{{ */
1227 {
1228         ADD_HEADER("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
1229         CACHE_LIMITER(private_no_expire)(TSRMLS_C);
1230 }
1231 /* }}} */
1232 
1233 CACHE_LIMITER_FUNC(nocache) /* {{{ */
1234 {
1235         ADD_HEADER("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
1236 
1237         /* For HTTP/1.1 conforming clients and the rest (MSIE 5) */
1238         ADD_HEADER("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
1239 
1240         /* For HTTP/1.0 conforming clients */
1241         ADD_HEADER("Pragma: no-cache");
1242 }
1243 /* }}} */
1244 
1245 static php_session_cache_limiter_t php_session_cache_limiters[] = {
1246         CACHE_LIMITER_ENTRY(public)
1247         CACHE_LIMITER_ENTRY(private)
1248         CACHE_LIMITER_ENTRY(private_no_expire)
1249         CACHE_LIMITER_ENTRY(nocache)
1250         {0}
1251 };
1252 
1253 static int php_session_cache_limiter(TSRMLS_D) /* {{{ */
1254 {
1255         php_session_cache_limiter_t *lim;
1256 
1257         if (PS(cache_limiter)[0] == '\0') return 0;
1258 
1259         if (SG(headers_sent)) {
1260                 const char *output_start_filename = php_output_get_start_filename(TSRMLS_C);
1261                 int output_start_lineno = php_output_get_start_lineno(TSRMLS_C);
1262 
1263                 if (output_start_filename) {
1264                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot send session cache limiter - headers already sent (output started at %s:%d)", output_start_filename, output_start_lineno);
1265                 } else {
1266                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot send session cache limiter - headers already sent");
1267                 }
1268                 return -2;
1269         }
1270 
1271         for (lim = php_session_cache_limiters; lim->name; lim++) {
1272                 if (!strcasecmp(lim->name, PS(cache_limiter))) {
1273                         lim->func(TSRMLS_C);
1274                         return 0;
1275                 }
1276         }
1277 
1278         return -1;
1279 }
1280 /* }}} */
1281 
1282 /* *********************
1283    * Cookie Management *
1284    ********************* */
1285 
1286 #define COOKIE_SET_COOKIE "Set-Cookie: "
1287 #define COOKIE_EXPIRES  "; expires="
1288 #define COOKIE_MAX_AGE  "; Max-Age="
1289 #define COOKIE_PATH             "; path="
1290 #define COOKIE_DOMAIN   "; domain="
1291 #define COOKIE_SECURE   "; secure"
1292 #define COOKIE_HTTPONLY "; HttpOnly"
1293 
1294 /*
1295  * Remove already sent session ID cookie.
1296  * It must be directly removed from SG(sapi_header) because sapi_add_header_ex()
1297  * removes all of matching cookie. i.e. It deletes all of Set-Cookie headers.
1298  */
1299 static void php_session_remove_cookie(TSRMLS_D) {
1300         sapi_header_struct *header;
1301         zend_llist *l = &SG(sapi_headers).headers;
1302         zend_llist_element *next;
1303         zend_llist_element *current;
1304         char *session_cookie, *e_session_name;
1305         int session_cookie_len, len = sizeof("Set-Cookie")-1;
1306 
1307         e_session_name = php_url_encode(PS(session_name), strlen(PS(session_name)), NULL);
1308         spprintf(&session_cookie, 0, "Set-Cookie: %s=", e_session_name);
1309         efree(e_session_name);
1310 
1311         session_cookie_len = strlen(session_cookie);
1312         current = l->head;
1313         while (current) {
1314                 header = (sapi_header_struct *)(current->data);
1315                 next = current->next;
1316                 if (header->header_len > len && header->header[len] == ':'
1317                         && !strncmp(header->header, session_cookie, session_cookie_len)) {
1318                         if (current->prev) {
1319                                 current->prev->next = next;
1320                         } else {
1321                                 l->head = next;
1322                         }
1323                         if (next) {
1324                                 next->prev = current->prev;
1325                         } else {
1326                                 l->tail = current->prev;
1327                         }
1328                         sapi_free_header(header);
1329                         efree(current);
1330                         --l->count;
1331                 }
1332                 current = next;
1333         }
1334         efree(session_cookie);
1335 }
1336 
1337 static void php_session_send_cookie(TSRMLS_D) /* {{{ */
1338 {
1339         smart_str ncookie = {0};
1340         char *date_fmt = NULL;
1341         char *e_session_name, *e_id;
1342 
1343         if (SG(headers_sent)) {
1344                 const char *output_start_filename = php_output_get_start_filename(TSRMLS_C);
1345                 int output_start_lineno = php_output_get_start_lineno(TSRMLS_C);
1346 
1347                 if (output_start_filename) {
1348                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot send session cookie - headers already sent by (output started at %s:%d)", output_start_filename, output_start_lineno);
1349                 } else {
1350                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot send session cookie - headers already sent");
1351                 }
1352                 return;
1353         }
1354 
1355         /* URL encode session_name and id because they might be user supplied */
1356         e_session_name = php_url_encode(PS(session_name), strlen(PS(session_name)), NULL);
1357         e_id = php_url_encode(PS(id), strlen(PS(id)), NULL);
1358 
1359         smart_str_appends(&ncookie, COOKIE_SET_COOKIE);
1360         smart_str_appends(&ncookie, e_session_name);
1361         smart_str_appendc(&ncookie, '=');
1362         smart_str_appends(&ncookie, e_id);
1363 
1364         efree(e_session_name);
1365         efree(e_id);
1366 
1367         if (PS(cookie_lifetime) > 0) {
1368                 struct timeval tv;
1369                 time_t t;
1370 
1371                 gettimeofday(&tv, NULL);
1372                 t = tv.tv_sec + PS(cookie_lifetime);
1373 
1374                 if (t > 0) {
1375                         date_fmt = php_format_date("D, d-M-Y H:i:s T", sizeof("D, d-M-Y H:i:s T")-1, t, 0 TSRMLS_CC);
1376                         smart_str_appends(&ncookie, COOKIE_EXPIRES);
1377                         smart_str_appends(&ncookie, date_fmt);
1378                         efree(date_fmt);
1379 
1380                         smart_str_appends(&ncookie, COOKIE_MAX_AGE);
1381                         smart_str_append_long(&ncookie, PS(cookie_lifetime));
1382                 }
1383         }
1384 
1385         if (PS(cookie_path)[0]) {
1386                 smart_str_appends(&ncookie, COOKIE_PATH);
1387                 smart_str_appends(&ncookie, PS(cookie_path));
1388         }
1389 
1390         if (PS(cookie_domain)[0]) {
1391                 smart_str_appends(&ncookie, COOKIE_DOMAIN);
1392                 smart_str_appends(&ncookie, PS(cookie_domain));
1393         }
1394 
1395         if (PS(cookie_secure)) {
1396                 smart_str_appends(&ncookie, COOKIE_SECURE);
1397         }
1398 
1399         if (PS(cookie_httponly)) {
1400                 smart_str_appends(&ncookie, COOKIE_HTTPONLY);
1401         }
1402 
1403         smart_str_0(&ncookie);
1404 
1405         php_session_remove_cookie(TSRMLS_C); /* remove already sent session ID cookie */
1406         sapi_add_header_ex(ncookie.c, ncookie.len, 0, 0 TSRMLS_CC);
1407 }
1408 /* }}} */
1409 
1410 PHPAPI ps_module *_php_find_ps_module(char *name TSRMLS_DC) /* {{{ */
1411 {
1412         ps_module *ret = NULL;
1413         ps_module **mod;
1414         int i;
1415 
1416         for (i = 0, mod = ps_modules; i < MAX_MODULES; i++, mod++) {
1417                 if (*mod && !strcasecmp(name, (*mod)->s_name)) {
1418                         ret = *mod;
1419                         break;
1420                 }
1421         }
1422         return ret;
1423 }
1424 /* }}} */
1425 
1426 PHPAPI const ps_serializer *_php_find_ps_serializer(char *name TSRMLS_DC) /* {{{ */
1427 {
1428         const ps_serializer *ret = NULL;
1429         const ps_serializer *mod;
1430 
1431         for (mod = ps_serializers; mod->name; mod++) {
1432                 if (!strcasecmp(name, mod->name)) {
1433                         ret = mod;
1434                         break;
1435                 }
1436         }
1437         return ret;
1438 }
1439 /* }}} */
1440 
1441 static void ppid2sid(zval **ppid TSRMLS_DC) {
1442         if (Z_TYPE_PP(ppid) != IS_STRING) {
1443                 PS(id) = NULL;
1444                 PS(send_cookie) = 1;
1445         } else {
1446                 convert_to_string((*ppid));
1447                 PS(id) = estrndup(Z_STRVAL_PP(ppid), Z_STRLEN_PP(ppid));
1448                 PS(send_cookie) = 0;
1449         }
1450 }
1451 
1452 PHPAPI void php_session_reset_id(TSRMLS_D) /* {{{ */
1453 {
1454         int module_number = PS(module_number);
1455 
1456         if (!PS(id)) {
1457                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot set session ID - session ID is not initialized");
1458                 return;
1459         }
1460 
1461         if (PS(use_cookies) && PS(send_cookie)) {
1462                 php_session_send_cookie(TSRMLS_C);
1463                 PS(send_cookie) = 0;
1464         }
1465 
1466         /* if the SID constant exists, destroy it. */
1467         zend_hash_del(EG(zend_constants), "sid", sizeof("sid"));
1468 
1469         if (PS(define_sid)) {
1470                 smart_str var = {0};
1471 
1472                 smart_str_appends(&var, PS(session_name));
1473                 smart_str_appendc(&var, '=');
1474                 smart_str_appends(&var, PS(id));
1475                 smart_str_0(&var);
1476                 REGISTER_STRINGL_CONSTANT("SID", var.c, var.len, 0);
1477         } else {
1478                 REGISTER_STRINGL_CONSTANT("SID", STR_EMPTY_ALLOC(), 0, 0);
1479         }
1480 
1481         if (PS(apply_trans_sid)) {
1482                 php_url_scanner_reset_vars(TSRMLS_C);
1483                 php_url_scanner_add_var(PS(session_name), strlen(PS(session_name)), PS(id), strlen(PS(id)), 1 TSRMLS_CC);
1484         }
1485 }
1486 /* }}} */
1487 
1488 PHPAPI void php_session_start(TSRMLS_D) /* {{{ */
1489 {
1490         zval **ppid;
1491         zval **data;
1492         char *p, *value;
1493         int nrand;
1494         int lensess;
1495 
1496         if (PS(use_only_cookies)) {
1497                 PS(apply_trans_sid) = 0;
1498         } else {
1499                 PS(apply_trans_sid) = PS(use_trans_sid);
1500         }
1501 
1502         switch (PS(session_status)) {
1503                 case php_session_active:
1504                         php_error(E_NOTICE, "A session had already been started - ignoring session_start()");
1505                         return;
1506                         break;
1507 
1508                 case php_session_disabled:
1509                         value = zend_ini_string("session.save_handler", sizeof("session.save_handler"), 0);
1510                         if (!PS(mod) && value) {
1511                                 PS(mod) = _php_find_ps_module(value TSRMLS_CC);
1512                                 if (!PS(mod)) {
1513                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot find save handler '%s' - session startup failed", value);
1514                                         return;
1515                                 }
1516                         }
1517                         value = zend_ini_string("session.serialize_handler", sizeof("session.serialize_handler"), 0);
1518                         if (!PS(serializer) && value) {
1519                                 PS(serializer) = _php_find_ps_serializer(value TSRMLS_CC);
1520                                 if (!PS(serializer)) {
1521                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot find serialization handler '%s' - session startup failed", value);
1522                                         return;
1523                                 }
1524                         }
1525                         PS(session_status) = php_session_none;
1526                         /* fallthrough */
1527 
1528                 default:
1529                 case php_session_none:
1530                         PS(define_sid) = 1;
1531                         PS(send_cookie) = 1;
1532         }
1533 
1534         lensess = strlen(PS(session_name));
1535 
1536         /* Cookies are preferred, because initially
1537          * cookie and get variables will be available. */
1538 
1539         if (!PS(id)) {
1540                 if (PS(use_cookies) && zend_hash_find(&EG(symbol_table), "_COOKIE", sizeof("_COOKIE"), (void **) &data) == SUCCESS &&
1541                                 Z_TYPE_PP(data) == IS_ARRAY &&
1542                                 zend_hash_find(Z_ARRVAL_PP(data), PS(session_name), lensess + 1, (void **) &ppid) == SUCCESS
1543                 ) {
1544                         ppid2sid(ppid TSRMLS_CC);
1545                         PS(apply_trans_sid) = 0;
1546                         PS(define_sid) = 0;
1547                 }
1548 
1549                 if (!PS(use_only_cookies) && !PS(id) &&
1550                                 zend_hash_find(&EG(symbol_table), "_GET", sizeof("_GET"), (void **) &data) == SUCCESS &&
1551                                 Z_TYPE_PP(data) == IS_ARRAY &&
1552                                 zend_hash_find(Z_ARRVAL_PP(data), PS(session_name), lensess + 1, (void **) &ppid) == SUCCESS
1553                 ) {
1554                         ppid2sid(ppid TSRMLS_CC);
1555                 }
1556 
1557                 if (!PS(use_only_cookies) && !PS(id) &&
1558                                 zend_hash_find(&EG(symbol_table), "_POST", sizeof("_POST"), (void **) &data) == SUCCESS &&
1559                                 Z_TYPE_PP(data) == IS_ARRAY &&
1560                                 zend_hash_find(Z_ARRVAL_PP(data), PS(session_name), lensess + 1, (void **) &ppid) == SUCCESS
1561                 ) {
1562                         ppid2sid(ppid TSRMLS_CC);
1563                 }
1564         }
1565 
1566         /* Check the REQUEST_URI symbol for a string of the form
1567          * '<session-name>=<session-id>' to allow URLs of the form
1568          * http://yoursite/<session-name>=<session-id>/script.php */
1569 
1570         if (!PS(use_only_cookies) && !PS(id) && PG(http_globals)[TRACK_VARS_SERVER] &&
1571                         zend_hash_find(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]), "REQUEST_URI", sizeof("REQUEST_URI"), (void **) &data) == SUCCESS &&
1572                         Z_TYPE_PP(data) == IS_STRING &&
1573                         (p = strstr(Z_STRVAL_PP(data), PS(session_name))) &&
1574                         p[lensess] == '='
1575         ) {
1576                 char *q;
1577 
1578                 p += lensess + 1;
1579                 if ((q = strpbrk(p, "/?\\"))) {
1580                         PS(id) = estrndup(p, q - p);
1581                         PS(send_cookie) = 0;
1582                 }
1583         }
1584 
1585         /* Check whether the current request was referred to by
1586          * an external site which invalidates the previously found id. */
1587 
1588         if (PS(id) &&
1589                         PS(extern_referer_chk)[0] != '\0' &&
1590                         PG(http_globals)[TRACK_VARS_SERVER] &&
1591                         zend_hash_find(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]), "HTTP_REFERER", sizeof("HTTP_REFERER"), (void **) &data) == SUCCESS &&
1592                         Z_TYPE_PP(data) == IS_STRING &&
1593                         Z_STRLEN_PP(data) != 0 &&
1594                         strstr(Z_STRVAL_PP(data), PS(extern_referer_chk)) == NULL
1595         ) {
1596                 efree(PS(id));
1597                 PS(id) = NULL;
1598                 PS(send_cookie) = 1;
1599                 if (PS(use_trans_sid) && !PS(use_only_cookies)) {
1600                         PS(apply_trans_sid) = 1;
1601                 }
1602         }
1603 
1604         /* Finally check session id for dangarous characters
1605          * Security note: session id may be embedded in HTML pages.*/
1606         if (PS(id) && strpbrk(PS(id), "\r\n\t <>'\"\\")) {
1607                 efree(PS(id));
1608                 PS(id) = NULL;
1609         }
1610 
1611         php_session_initialize(TSRMLS_C);
1612         php_session_cache_limiter(TSRMLS_C);
1613 
1614         if ((PS(mod_data) || PS(mod_user_implemented)) && PS(gc_probability) > 0) {
1615                 int nrdels = -1;
1616 
1617                 nrand = (int) ((float) PS(gc_divisor) * php_combined_lcg(TSRMLS_C));
1618                 if (nrand < PS(gc_probability)) {
1619                         PS(mod)->s_gc(&PS(mod_data), PS(gc_maxlifetime), &nrdels TSRMLS_CC);
1620 #ifdef SESSION_DEBUG
1621                         if (nrdels != -1) {
1622                                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "purged %d expired session objects", nrdels);
1623                         }
1624 #endif
1625                 }
1626         }
1627 }
1628 /* }}} */
1629 
1630 static void php_session_flush(TSRMLS_D) /* {{{ */
1631 {
1632         if (PS(session_status) == php_session_active) {
1633                 PS(session_status) = php_session_none;
1634                 php_session_save_current_state(TSRMLS_C);
1635         }
1636 }
1637 /* }}} */
1638 
1639 static void php_session_abort(TSRMLS_D) /* {{{ */
1640 {
1641         if (PS(session_status) == php_session_active) {
1642                 PS(session_status) = php_session_none;
1643                 if (PS(mod_data) || PS(mod_user_implemented)) {
1644                         PS(mod)->s_close(&PS(mod_data) TSRMLS_CC);
1645                 }
1646         }
1647 }
1648 /* }}} */
1649 
1650 static void php_session_reset(TSRMLS_D) /* {{{ */
1651 {
1652         if (PS(session_status) == php_session_active) {
1653                 php_session_initialize(TSRMLS_C);
1654         }
1655 }
1656 /* }}} */
1657 
1658 
1659 PHPAPI void session_adapt_url(const char *url, size_t urllen, char **new, size_t *newlen TSRMLS_DC) /* {{{ */
1660 {
1661         if (PS(apply_trans_sid) && (PS(session_status) == php_session_active)) {
1662                 *new = php_url_scanner_adapt_single_url(url, urllen, PS(session_name), PS(id), newlen TSRMLS_CC);
1663         }
1664 }
1665 /* }}} */
1666 
1667 /* ********************************
1668    * Userspace exported functions *
1669    ******************************** */
1670 
1671 /* {{{ proto void session_set_cookie_params(int lifetime [, string path [, string domain [, bool secure[, bool httponly]]]])
1672    Set session cookie parameters */
1673 static PHP_FUNCTION(session_set_cookie_params)
1674 {
1675         zval **lifetime = NULL;
1676         char *path = NULL, *domain = NULL;
1677         int path_len, domain_len, argc = ZEND_NUM_ARGS();
1678         zend_bool secure = 0, httponly = 0;
1679 
1680         if (!PS(use_cookies) ||
1681                 zend_parse_parameters(argc TSRMLS_CC, "Z|ssbb", &lifetime, &path, &path_len, &domain, &domain_len, &secure, &httponly) == FAILURE) {
1682                 return;
1683         }
1684 
1685         convert_to_string_ex(lifetime);
1686 
1687         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);
1688 
1689         if (path) {
1690                 zend_alter_ini_entry("session.cookie_path", sizeof("session.cookie_path"), path, path_len, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
1691         }
1692         if (domain) {
1693                 zend_alter_ini_entry("session.cookie_domain", sizeof("session.cookie_domain"), domain, domain_len, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
1694         }
1695 
1696         if (argc > 3) {
1697                 zend_alter_ini_entry("session.cookie_secure", sizeof("session.cookie_secure"), secure ? "1" : "0", 1, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
1698         }
1699         if (argc > 4) {
1700                 zend_alter_ini_entry("session.cookie_httponly", sizeof("session.cookie_httponly"), httponly ? "1" : "0", 1, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
1701         }
1702 }
1703 /* }}} */
1704 
1705 /* {{{ proto array session_get_cookie_params(void)
1706    Return the session cookie parameters */
1707 static PHP_FUNCTION(session_get_cookie_params)
1708 {
1709         if (zend_parse_parameters_none() == FAILURE) {
1710                 return;
1711         }
1712 
1713         array_init(return_value);
1714 
1715         add_assoc_long(return_value, "lifetime", PS(cookie_lifetime));
1716         add_assoc_string(return_value, "path", PS(cookie_path), 1);
1717         add_assoc_string(return_value, "domain", PS(cookie_domain), 1);
1718         add_assoc_bool(return_value, "secure", PS(cookie_secure));
1719         add_assoc_bool(return_value, "httponly", PS(cookie_httponly));
1720 }
1721 /* }}} */
1722 
1723 /* {{{ proto string session_name([string newname])
1724    Return the current session name. If newname is given, the session name is replaced with newname */
1725 static PHP_FUNCTION(session_name)
1726 {
1727         char *name = NULL;
1728         int name_len;
1729 
1730         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &name, &name_len) == FAILURE) {
1731                 return;
1732         }
1733 
1734         RETVAL_STRING(PS(session_name), 1);
1735 
1736         if (name) {
1737                 zend_alter_ini_entry("session.name", sizeof("session.name"), name, name_len, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
1738         }
1739 }
1740 /* }}} */
1741 
1742 /* {{{ proto string session_module_name([string newname])
1743    Return the current module name used for accessing session data. If newname is given, the module name is replaced with newname */
1744 static PHP_FUNCTION(session_module_name)
1745 {
1746         char *name = NULL;
1747         int name_len;
1748 
1749         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &name, &name_len) == FAILURE) {
1750                 return;
1751         }
1752 
1753         /* Set return_value to current module name */
1754         if (PS(mod) && PS(mod)->s_name) {
1755                 RETVAL_STRING(safe_estrdup(PS(mod)->s_name), 0);
1756         } else {
1757                 RETVAL_EMPTY_STRING();
1758         }
1759 
1760         if (name) {
1761                 if (!_php_find_ps_module(name TSRMLS_CC)) {
1762                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot find named PHP session module (%s)", name);
1763 
1764                         zval_dtor(return_value);
1765                         RETURN_FALSE;
1766                 }
1767                 if (PS(mod_data) || PS(mod_user_implemented)) {
1768                         PS(mod)->s_close(&PS(mod_data) TSRMLS_CC);
1769                 }
1770                 PS(mod_data) = NULL;
1771 
1772                 zend_alter_ini_entry("session.save_handler", sizeof("session.save_handler"), name, name_len, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
1773         }
1774 }
1775 /* }}} */
1776 
1777 /* {{{ proto void session_set_save_handler(string open, string close, string read, string write, string destroy, string gc, string create_sid)
1778    Sets user-level functions */
1779 static PHP_FUNCTION(session_set_save_handler)
1780 {
1781         zval ***args = NULL;
1782         int i, num_args, argc = ZEND_NUM_ARGS();
1783         char *name;
1784 
1785         if (PS(session_status) != php_session_none) {
1786                 RETURN_FALSE;
1787         }
1788 
1789         if (argc > 0 && argc <= 2) {
1790                 zval *obj = NULL, *callback = NULL;
1791                 zend_uint func_name_len;
1792                 char *func_name;
1793                 HashPosition pos;
1794                 zend_function *default_mptr, *current_mptr;
1795                 ulong func_index;
1796                 php_shutdown_function_entry shutdown_function_entry;
1797                 zend_bool register_shutdown = 1;
1798 
1799                 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|b", &obj, php_session_iface_entry, &register_shutdown) == FAILURE) {
1800                         RETURN_FALSE;
1801                 }
1802 
1803                 /* Find implemented methods - SessionHandlerInterface */
1804                 zend_hash_internal_pointer_reset_ex(&php_session_iface_entry->function_table, &pos);
1805                 i = 0;
1806                 while (zend_hash_get_current_data_ex(&php_session_iface_entry->function_table, (void **) &default_mptr, &pos) == SUCCESS) {
1807                         zend_hash_get_current_key_ex(&php_session_iface_entry->function_table, &func_name, &func_name_len, &func_index, 0, &pos);
1808 
1809                         if (zend_hash_find(&Z_OBJCE_P(obj)->function_table, func_name, func_name_len, (void **)&current_mptr) == SUCCESS) {
1810                                 if (PS(mod_user_names).names[i] != NULL) {
1811                                         zval_ptr_dtor(&PS(mod_user_names).names[i]);
1812                                 }
1813 
1814                                 MAKE_STD_ZVAL(callback);
1815                                 array_init_size(callback, 2);
1816                                 Z_ADDREF_P(obj);
1817                                 add_next_index_zval(callback, obj);
1818                                 add_next_index_stringl(callback, func_name, func_name_len - 1, 1);
1819                                 PS(mod_user_names).names[i] = callback;
1820                         } else {
1821                                 php_error_docref(NULL TSRMLS_CC, E_ERROR, "Session handler's function table is corrupt");
1822                                 RETURN_FALSE;
1823                         }
1824 
1825                         zend_hash_move_forward_ex(&php_session_iface_entry->function_table, &pos);
1826                         ++i;
1827                 }
1828 
1829                 /* Find implemented methods - SessionIdInterface (optional) */
1830                 zend_hash_internal_pointer_reset_ex(&php_session_id_iface_entry->function_table, &pos);
1831                 while (zend_hash_get_current_data_ex(&php_session_id_iface_entry->function_table, (void **) &default_mptr, &pos) == SUCCESS) {
1832                         zend_hash_get_current_key_ex(&php_session_id_iface_entry->function_table, &func_name, &func_name_len, &func_index, 0, &pos);
1833 
1834                         if (zend_hash_find(&Z_OBJCE_P(obj)->function_table, func_name, func_name_len, (void **)&current_mptr) == SUCCESS) {
1835                                 if (PS(mod_user_names).names[i] != NULL) {
1836                                         zval_ptr_dtor(&PS(mod_user_names).names[i]);
1837                                 }
1838 
1839                                 MAKE_STD_ZVAL(callback);
1840                                 array_init_size(callback, 2);
1841                                 Z_ADDREF_P(obj);
1842                                 add_next_index_zval(callback, obj);
1843                                 add_next_index_stringl(callback, func_name, func_name_len - 1, 1);
1844                                 PS(mod_user_names).names[i] = callback;
1845                         }
1846 
1847                         zend_hash_move_forward_ex(&php_session_id_iface_entry->function_table, &pos);
1848                         ++i;
1849                 }
1850 
1851                 if (register_shutdown) {
1852                         /* create shutdown function */
1853                         shutdown_function_entry.arg_count = 1;
1854                         shutdown_function_entry.arguments = (zval **) safe_emalloc(sizeof(zval *), 1, 0);
1855 
1856                         MAKE_STD_ZVAL(callback);
1857                         ZVAL_STRING(callback, "session_register_shutdown", 1);
1858                         shutdown_function_entry.arguments[0] = callback;
1859 
1860                         /* add shutdown function, removing the old one if it exists */
1861                         if (!register_user_shutdown_function("session_shutdown", sizeof("session_shutdown"), &shutdown_function_entry TSRMLS_CC)) {
1862                                 zval_ptr_dtor(&callback);
1863                                 efree(shutdown_function_entry.arguments);
1864                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to register session shutdown function");
1865                                 RETURN_FALSE;
1866                         }
1867                 } else {
1868                         /* remove shutdown function */
1869                         remove_user_shutdown_function("session_shutdown", sizeof("session_shutdown") TSRMLS_CC);
1870                 }
1871 
1872                 if (PS(mod) && PS(session_status) == php_session_none && PS(mod) != &ps_mod_user) {
1873                         zend_alter_ini_entry("session.save_handler", sizeof("session.save_handler"), "user", sizeof("user")-1, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
1874                 }
1875 
1876                 RETURN_TRUE;
1877         }
1878 
1879         if (argc != 6 && argc != 7) {
1880                 WRONG_PARAM_COUNT;
1881         }
1882 
1883         if (zend_parse_parameters(argc TSRMLS_CC, "+", &args, &num_args) == FAILURE) {
1884                 return;
1885         }
1886 
1887         /* remove shutdown function */
1888         remove_user_shutdown_function("session_shutdown", sizeof("session_shutdown") TSRMLS_CC);
1889 
1890         /* at this point argc can only be 6 or 7 */
1891         for (i = 0; i < argc; i++) {
1892                 if (!zend_is_callable(*args[i], 0, &name TSRMLS_CC)) {
1893                         efree(args);
1894                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Argument %d is not a valid callback", i+1);
1895                         efree(name);
1896                         RETURN_FALSE;
1897                 }
1898                 efree(name);
1899         }
1900 
1901         if (PS(mod) && PS(mod) != &ps_mod_user) {
1902                 zend_alter_ini_entry("session.save_handler", sizeof("session.save_handler"), "user", sizeof("user")-1, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
1903         }
1904 
1905         for (i = 0; i < argc; i++) {
1906                 if (PS(mod_user_names).names[i] != NULL) {
1907                         zval_ptr_dtor(&PS(mod_user_names).names[i]);
1908                 }
1909                 Z_ADDREF_PP(args[i]);
1910                 PS(mod_user_names).names[i] = *args[i];
1911         }
1912 
1913         efree(args);
1914         RETURN_TRUE;
1915 }
1916 /* }}} */
1917 
1918 /* {{{ proto string session_save_path([string newname])
1919    Return the current save path passed to module_name. If newname is given, the save path is replaced with newname */
1920 static PHP_FUNCTION(session_save_path)
1921 {
1922         char *name = NULL;
1923         int name_len;
1924 
1925         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &name, &name_len) == FAILURE) {
1926                 return;
1927         }
1928 
1929         RETVAL_STRING(PS(save_path), 1);
1930 
1931         if (name) {
1932                 if (memchr(name, '\0', name_len) != NULL) {
1933                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "The save_path cannot contain NULL characters");
1934                         zval_dtor(return_value);
1935                         RETURN_FALSE;
1936                 }
1937                 zend_alter_ini_entry("session.save_path", sizeof("session.save_path"), name, name_len, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
1938         }
1939 }
1940 /* }}} */
1941 
1942 /* {{{ proto string session_id([string newid])
1943    Return the current session id. If newid is given, the session id is replaced with newid */
1944 static PHP_FUNCTION(session_id)
1945 {
1946         char *name = NULL;
1947         int name_len, argc = ZEND_NUM_ARGS();
1948 
1949         if (zend_parse_parameters(argc TSRMLS_CC, "|s", &name, &name_len) == FAILURE) {
1950                 return;
1951         }
1952 
1953         if (PS(id)) {
1954                 RETVAL_STRING(PS(id), 1);
1955         } else {
1956                 RETVAL_EMPTY_STRING();
1957         }
1958 
1959         if (name) {
1960                 if (PS(id)) {
1961                         efree(PS(id));
1962                 }
1963                 PS(id) = estrndup(name, name_len);
1964         }
1965 }
1966 /* }}} */
1967 
1968 /* {{{ proto bool session_regenerate_id([bool delete_old_session])
1969    Update the current session id with a newly generated one. If delete_old_session is set to true, remove the old session. */
1970 static PHP_FUNCTION(session_regenerate_id)
1971 {
1972         zend_bool del_ses = 0;
1973 
1974         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &del_ses) == FAILURE) {
1975                 return;
1976         }
1977 
1978         if (SG(headers_sent) && PS(use_cookies)) {
1979                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot regenerate session id - headers already sent");
1980                 RETURN_FALSE;
1981         }
1982 
1983         if (PS(session_status) == php_session_active) {
1984                 if (PS(id)) {
1985                         if (del_ses && PS(mod)->s_destroy(&PS(mod_data), PS(id) TSRMLS_CC) == FAILURE) {
1986                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Session object destruction failed");
1987                                 RETURN_FALSE;
1988                         }
1989                         efree(PS(id));
1990                 }
1991 
1992                 PS(id) = PS(mod)->s_create_sid(&PS(mod_data), NULL TSRMLS_CC);
1993                 if (PS(id)) {
1994                         PS(send_cookie) = 1;
1995                         php_session_reset_id(TSRMLS_C);
1996                         RETURN_TRUE;
1997                 } else {
1998                         PS(id) = STR_EMPTY_ALLOC();
1999                 }
2000         }
2001         RETURN_FALSE;
2002 }
2003 /* }}} */
2004 
2005 /* {{{ proto string session_cache_limiter([string new_cache_limiter])
2006    Return the current cache limiter. If new_cache_limited is given, the current cache_limiter is replaced with new_cache_limiter */
2007 static PHP_FUNCTION(session_cache_limiter)
2008 {
2009         char *limiter = NULL;
2010         int limiter_len;
2011 
2012         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &limiter, &limiter_len) == FAILURE) {
2013                 return;
2014         }
2015 
2016         RETVAL_STRING(PS(cache_limiter), 1);
2017 
2018         if (limiter) {
2019                 zend_alter_ini_entry("session.cache_limiter", sizeof("session.cache_limiter"), limiter, limiter_len, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
2020         }
2021 }
2022 /* }}} */
2023 
2024 /* {{{ proto int session_cache_expire([int new_cache_expire])
2025    Return the current cache expire. If new_cache_expire is given, the current cache_expire is replaced with new_cache_expire */
2026 static PHP_FUNCTION(session_cache_expire)
2027 {
2028         zval **expires = NULL;
2029         int argc = ZEND_NUM_ARGS();
2030 
2031         if (zend_parse_parameters(argc TSRMLS_CC, "|Z", &expires) == FAILURE) {
2032                 return;
2033         }
2034 
2035         RETVAL_LONG(PS(cache_expire));
2036 
2037         if (argc == 1) {
2038                 convert_to_string_ex(expires);
2039                 zend_alter_ini_entry("session.cache_expire", sizeof("session.cache_expire"), Z_STRVAL_PP(expires), Z_STRLEN_PP(expires), ZEND_INI_USER, ZEND_INI_STAGE_RUNTIME);
2040         }
2041 }
2042 /* }}} */
2043 
2044 /* {{{ proto string session_encode(void)
2045    Serializes the current setup and returns the serialized representation */
2046 static PHP_FUNCTION(session_encode)
2047 {
2048         int len;
2049         char *enc;
2050 
2051         if (zend_parse_parameters_none() == FAILURE) {
2052                 return;
2053         }
2054 
2055         enc = php_session_encode(&len TSRMLS_CC);
2056         if (enc == NULL) {
2057                 RETURN_FALSE;
2058         }
2059 
2060         RETVAL_STRINGL(enc, len, 0);
2061 }
2062 /* }}} */
2063 
2064 /* {{{ proto bool session_decode(string data)
2065    Deserializes data and reinitializes the variables */
2066 static PHP_FUNCTION(session_decode)
2067 {
2068         char *str;
2069         int str_len;
2070 
2071         if (PS(session_status) == php_session_none) {
2072                 RETURN_FALSE;
2073         }
2074 
2075         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
2076                 return;
2077         }
2078 
2079         RETVAL_BOOL(php_session_decode(str, str_len TSRMLS_CC) == SUCCESS);
2080 }
2081 /* }}} */
2082 
2083 /* {{{ proto bool session_start(void)
2084    Begin session - reinitializes freezed variables, registers browsers etc */
2085 static PHP_FUNCTION(session_start)
2086 {
2087         /* skipping check for non-zero args for performance reasons here ?*/
2088         if (PS(id) && !strlen(PS(id))) {
2089                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot start session with empty session ID");
2090                 RETURN_FALSE;
2091         }
2092 
2093         php_session_start(TSRMLS_C);
2094 
2095         if (PS(session_status) != php_session_active) {
2096                 RETURN_FALSE;
2097         }
2098         RETURN_TRUE;
2099 }
2100 /* }}} */
2101 
2102 /* {{{ proto bool session_destroy(void)
2103    Destroy the current session and all data associated with it */
2104 static PHP_FUNCTION(session_destroy)
2105 {
2106         if (zend_parse_parameters_none() == FAILURE) {
2107                 return;
2108         }
2109 
2110         RETURN_BOOL(php_session_destroy(TSRMLS_C) == SUCCESS);
2111 }
2112 /* }}} */
2113 
2114 /* {{{ proto void session_unset(void)
2115    Unset all registered variables */
2116 static PHP_FUNCTION(session_unset)
2117 {
2118         if (PS(session_status) == php_session_none) {
2119                 RETURN_FALSE;
2120         }
2121 
2122         IF_SESSION_VARS() {
2123                 HashTable *ht_sess_var;
2124 
2125                 SEPARATE_ZVAL_IF_NOT_REF(&PS(http_session_vars));
2126                 ht_sess_var = Z_ARRVAL_P(PS(http_session_vars));
2127 
2128                 /* Clean $_SESSION. */
2129                 zend_hash_clean(ht_sess_var);
2130         }
2131 }
2132 /* }}} */
2133 
2134 /* {{{ proto void session_write_close(void)
2135    Write session data and end session */
2136 static PHP_FUNCTION(session_write_close)
2137 {
2138         php_session_flush(TSRMLS_C);
2139 }
2140 /* }}} */
2141 
2142 /* {{{ proto void session_abort(void)
2143    Abort session and end session. Session data will not be written */
2144 static PHP_FUNCTION(session_abort)
2145 {
2146         php_session_abort(TSRMLS_C);
2147 }
2148 /* }}} */
2149 
2150 /* {{{ proto void session_reset(void)
2151    Reset session data from saved session data */
2152 static PHP_FUNCTION(session_reset)
2153 {
2154         php_session_reset(TSRMLS_C);
2155 }
2156 /* }}} */
2157 
2158 /* {{{ proto int session_status(void)
2159    Returns the current session status */
2160 static PHP_FUNCTION(session_status)
2161 {
2162         if (zend_parse_parameters_none() == FAILURE) {
2163                 return;
2164         }
2165 
2166         RETURN_LONG(PS(session_status));
2167 }
2168 /* }}} */
2169 
2170 /* {{{ proto void session_register_shutdown(void)
2171    Registers session_write_close() as a shutdown function */
2172 static PHP_FUNCTION(session_register_shutdown)
2173 {
2174         php_shutdown_function_entry shutdown_function_entry;
2175         zval *callback;
2176 
2177         /* This function is registered itself as a shutdown function by
2178          * session_set_save_handler($obj). The reason we now register another
2179          * shutdown function is in case the user registered their own shutdown
2180          * function after calling session_set_save_handler(), which expects
2181          * the session still to be available.
2182          */
2183 
2184         shutdown_function_entry.arg_count = 1;
2185         shutdown_function_entry.arguments = (zval **) safe_emalloc(sizeof(zval *), 1, 0);
2186 
2187         MAKE_STD_ZVAL(callback);
2188         ZVAL_STRING(callback, "session_write_close", 1);
2189         shutdown_function_entry.arguments[0] = callback;
2190 
2191         if (!append_user_shutdown_function(shutdown_function_entry TSRMLS_CC)) {
2192                 zval_ptr_dtor(&callback);
2193                 efree(shutdown_function_entry.arguments);
2194 
2195                 /* Unable to register shutdown function, presumably because of lack
2196                  * of memory, so flush the session now. It would be done in rshutdown
2197                  * anyway but the handler will have had it's dtor called by then.
2198                  * If the user does have a later shutdown function which needs the
2199                  * session then tough luck.
2200                  */
2201                 php_session_flush(TSRMLS_C);
2202                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to register session flush function");
2203         }
2204 }
2205 /* }}} */
2206 
2207 /* {{{ arginfo */
2208 ZEND_BEGIN_ARG_INFO_EX(arginfo_session_name, 0, 0, 0)
2209         ZEND_ARG_INFO(0, name)
2210 ZEND_END_ARG_INFO()
2211 
2212 ZEND_BEGIN_ARG_INFO_EX(arginfo_session_module_name, 0, 0, 0)
2213         ZEND_ARG_INFO(0, module)
2214 ZEND_END_ARG_INFO()
2215 
2216 ZEND_BEGIN_ARG_INFO_EX(arginfo_session_save_path, 0, 0, 0)
2217         ZEND_ARG_INFO(0, path)
2218 ZEND_END_ARG_INFO()
2219 
2220 ZEND_BEGIN_ARG_INFO_EX(arginfo_session_id, 0, 0, 0)
2221         ZEND_ARG_INFO(0, id)
2222 ZEND_END_ARG_INFO()
2223 
2224 ZEND_BEGIN_ARG_INFO_EX(arginfo_session_regenerate_id, 0, 0, 0)
2225         ZEND_ARG_INFO(0, delete_old_session)
2226 ZEND_END_ARG_INFO()
2227 
2228 ZEND_BEGIN_ARG_INFO_EX(arginfo_session_decode, 0, 0, 1)
2229         ZEND_ARG_INFO(0, data)
2230 ZEND_END_ARG_INFO()
2231 
2232 ZEND_BEGIN_ARG_INFO(arginfo_session_void, 0)
2233 ZEND_END_ARG_INFO()
2234 
2235 ZEND_BEGIN_ARG_INFO_EX(arginfo_session_set_save_handler, 0, 0, 1)
2236         ZEND_ARG_INFO(0, open)
2237         ZEND_ARG_INFO(0, close)
2238         ZEND_ARG_INFO(0, read)
2239         ZEND_ARG_INFO(0, write)
2240         ZEND_ARG_INFO(0, destroy)
2241         ZEND_ARG_INFO(0, gc)
2242         ZEND_ARG_INFO(0, create_sid)
2243 ZEND_END_ARG_INFO()
2244 
2245 ZEND_BEGIN_ARG_INFO_EX(arginfo_session_cache_limiter, 0, 0, 0)
2246         ZEND_ARG_INFO(0, cache_limiter)
2247 ZEND_END_ARG_INFO()
2248 
2249 ZEND_BEGIN_ARG_INFO_EX(arginfo_session_cache_expire, 0, 0, 0)
2250         ZEND_ARG_INFO(0, new_cache_expire)
2251 ZEND_END_ARG_INFO()
2252 
2253 ZEND_BEGIN_ARG_INFO_EX(arginfo_session_set_cookie_params, 0, 0, 1)
2254         ZEND_ARG_INFO(0, lifetime)
2255         ZEND_ARG_INFO(0, path)
2256         ZEND_ARG_INFO(0, domain)
2257         ZEND_ARG_INFO(0, secure)
2258         ZEND_ARG_INFO(0, httponly)
2259 ZEND_END_ARG_INFO()
2260 
2261 ZEND_BEGIN_ARG_INFO(arginfo_session_class_open, 0)
2262         ZEND_ARG_INFO(0, save_path)
2263         ZEND_ARG_INFO(0, session_name)
2264 ZEND_END_ARG_INFO()
2265 
2266 ZEND_BEGIN_ARG_INFO(arginfo_session_class_close, 0)
2267 ZEND_END_ARG_INFO()
2268 
2269 ZEND_BEGIN_ARG_INFO(arginfo_session_class_read, 0)
2270         ZEND_ARG_INFO(0, key)
2271 ZEND_END_ARG_INFO()
2272 
2273 ZEND_BEGIN_ARG_INFO(arginfo_session_class_write, 0)
2274         ZEND_ARG_INFO(0, key)
2275         ZEND_ARG_INFO(0, val)
2276 ZEND_END_ARG_INFO()
2277 
2278 ZEND_BEGIN_ARG_INFO(arginfo_session_class_destroy, 0)
2279         ZEND_ARG_INFO(0, key)
2280 ZEND_END_ARG_INFO()
2281 
2282 ZEND_BEGIN_ARG_INFO(arginfo_session_class_gc, 0)
2283         ZEND_ARG_INFO(0, maxlifetime)
2284 ZEND_END_ARG_INFO()
2285 
2286 ZEND_BEGIN_ARG_INFO(arginfo_session_class_create_sid, 0)
2287 ZEND_END_ARG_INFO()
2288 /* }}} */
2289 
2290 /* {{{ session_functions[]
2291  */
2292 static const zend_function_entry session_functions[] = {
2293         PHP_FE(session_name,              arginfo_session_name)
2294         PHP_FE(session_module_name,       arginfo_session_module_name)
2295         PHP_FE(session_save_path,         arginfo_session_save_path)
2296         PHP_FE(session_id,                arginfo_session_id)
2297         PHP_FE(session_regenerate_id,     arginfo_session_regenerate_id)
2298         PHP_FE(session_decode,            arginfo_session_decode)
2299         PHP_FE(session_encode,            arginfo_session_void)
2300         PHP_FE(session_start,             arginfo_session_void)
2301         PHP_FE(session_destroy,           arginfo_session_void)
2302         PHP_FE(session_unset,             arginfo_session_void)
2303         PHP_FE(session_set_save_handler,  arginfo_session_set_save_handler)
2304         PHP_FE(session_cache_limiter,     arginfo_session_cache_limiter)
2305         PHP_FE(session_cache_expire,      arginfo_session_cache_expire)
2306         PHP_FE(session_set_cookie_params, arginfo_session_set_cookie_params)
2307         PHP_FE(session_get_cookie_params, arginfo_session_void)
2308         PHP_FE(session_write_close,       arginfo_session_void)
2309         PHP_FE(session_abort,             arginfo_session_void)
2310         PHP_FE(session_reset,             arginfo_session_void)
2311         PHP_FE(session_status,            arginfo_session_void)
2312         PHP_FE(session_register_shutdown, arginfo_session_void)
2313         PHP_FALIAS(session_commit, session_write_close, arginfo_session_void)
2314         PHP_FE_END
2315 };
2316 /* }}} */
2317 
2318 /* {{{ SessionHandlerInterface functions[]
2319 */
2320 static const zend_function_entry php_session_iface_functions[] = {
2321         PHP_ABSTRACT_ME(SessionHandlerInterface, open, arginfo_session_class_open)
2322         PHP_ABSTRACT_ME(SessionHandlerInterface, close, arginfo_session_class_close)
2323         PHP_ABSTRACT_ME(SessionHandlerInterface, read, arginfo_session_class_read)
2324         PHP_ABSTRACT_ME(SessionHandlerInterface, write, arginfo_session_class_write)
2325         PHP_ABSTRACT_ME(SessionHandlerInterface, destroy, arginfo_session_class_destroy)
2326         PHP_ABSTRACT_ME(SessionHandlerInterface, gc, arginfo_session_class_gc)
2327         { NULL, NULL, NULL }
2328 };
2329 /* }}} */
2330 
2331 /* {{{ SessionIdInterface functions[]
2332 */
2333 static const zend_function_entry php_session_id_iface_functions[] = {
2334         PHP_ABSTRACT_ME(SessionIdInterface, create_sid, arginfo_session_class_create_sid)
2335         { NULL, NULL, NULL }
2336 };
2337 /* }}} */
2338 
2339 /* {{{ SessionHandler functions[]
2340  */
2341 static const zend_function_entry php_session_class_functions[] = {
2342         PHP_ME(SessionHandler, open, arginfo_session_class_open, ZEND_ACC_PUBLIC)
2343         PHP_ME(SessionHandler, close, arginfo_session_class_close, ZEND_ACC_PUBLIC)
2344         PHP_ME(SessionHandler, read, arginfo_session_class_read, ZEND_ACC_PUBLIC)
2345         PHP_ME(SessionHandler, write, arginfo_session_class_write, ZEND_ACC_PUBLIC)
2346         PHP_ME(SessionHandler, destroy, arginfo_session_class_destroy, ZEND_ACC_PUBLIC)
2347         PHP_ME(SessionHandler, gc, arginfo_session_class_gc, ZEND_ACC_PUBLIC)
2348         PHP_ME(SessionHandler, create_sid, arginfo_session_class_create_sid, ZEND_ACC_PUBLIC)
2349         { NULL, NULL, NULL }
2350 };
2351 /* }}} */
2352 
2353 /* ********************************
2354    * Module Setup and Destruction *
2355    ******************************** */
2356 
2357 static int php_rinit_session(zend_bool auto_start TSRMLS_DC) /* {{{ */
2358 {
2359         php_rinit_session_globals(TSRMLS_C);
2360 
2361         if (PS(mod) == NULL) {
2362                 char *value;
2363 
2364                 value = zend_ini_string("session.save_handler", sizeof("session.save_handler"), 0);
2365                 if (value) {
2366                         PS(mod) = _php_find_ps_module(value TSRMLS_CC);
2367                 }
2368         }
2369 
2370         if (PS(serializer) == NULL) {
2371                 char *value;
2372 
2373                 value = zend_ini_string("session.serialize_handler", sizeof("session.serialize_handler"), 0);
2374                 if (value) {
2375                         PS(serializer) = _php_find_ps_serializer(value TSRMLS_CC);
2376                 }
2377         }
2378 
2379         if (PS(mod) == NULL || PS(serializer) == NULL) {
2380                 /* current status is unusable */
2381                 PS(session_status) = php_session_disabled;
2382                 return SUCCESS;
2383         }
2384 
2385         if (auto_start) {
2386                 php_session_start(TSRMLS_C);
2387         }
2388 
2389         return SUCCESS;
2390 } /* }}} */
2391 
2392 static PHP_RINIT_FUNCTION(session) /* {{{ */
2393 {
2394         return php_rinit_session(PS(auto_start) TSRMLS_CC);
2395 }
2396 /* }}} */
2397 
2398 static PHP_RSHUTDOWN_FUNCTION(session) /* {{{ */
2399 {
2400         int i;
2401 
2402         zend_try {
2403                 php_session_flush(TSRMLS_C);
2404         } zend_end_try();
2405         php_rshutdown_session_globals(TSRMLS_C);
2406 
2407         /* this should NOT be done in php_rshutdown_session_globals() */
2408         for (i = 0; i < 7; i++) {
2409                 if (PS(mod_user_names).names[i] != NULL) {
2410                         zval_ptr_dtor(&PS(mod_user_names).names[i]);
2411                         PS(mod_user_names).names[i] = NULL;
2412                 }
2413         }
2414 
2415         return SUCCESS;
2416 }
2417 /* }}} */
2418 
2419 static PHP_GINIT_FUNCTION(ps) /* {{{ */
2420 {
2421         int i;
2422 
2423         ps_globals->save_path = NULL;
2424         ps_globals->session_name = NULL;
2425         ps_globals->id = NULL;
2426         ps_globals->mod = NULL;
2427         ps_globals->serializer = NULL;
2428         ps_globals->mod_data = NULL;
2429         ps_globals->session_status = php_session_none;
2430         ps_globals->default_mod = NULL;
2431         ps_globals->mod_user_implemented = 0;
2432         ps_globals->mod_user_is_open = 0;
2433         for (i = 0; i < 7; i++) {
2434                 ps_globals->mod_user_names.names[i] = NULL;
2435         }
2436         ps_globals->http_session_vars = NULL;
2437 }
2438 /* }}} */
2439 
2440 static PHP_MINIT_FUNCTION(session) /* {{{ */
2441 {
2442         zend_class_entry ce;
2443 
2444         zend_register_auto_global("_SESSION", sizeof("_SESSION")-1, 0, NULL TSRMLS_CC);
2445 
2446         PS(module_number) = module_number; /* if we really need this var we need to init it in zts mode as well! */
2447 
2448         PS(session_status) = php_session_none;
2449         REGISTER_INI_ENTRIES();
2450 
2451 #ifdef HAVE_LIBMM
2452         PHP_MINIT(ps_mm) (INIT_FUNC_ARGS_PASSTHRU);
2453 #endif
2454         php_session_rfc1867_orig_callback = php_rfc1867_callback;
2455         php_rfc1867_callback = php_session_rfc1867_callback;
2456 
2457         /* Register interfaces */
2458         INIT_CLASS_ENTRY(ce, PS_IFACE_NAME, php_session_iface_functions);
2459         php_session_iface_entry = zend_register_internal_class(&ce TSRMLS_CC);
2460         php_session_iface_entry->ce_flags |= ZEND_ACC_INTERFACE;
2461 
2462         INIT_CLASS_ENTRY(ce, PS_SID_IFACE_NAME, php_session_id_iface_functions);
2463         php_session_id_iface_entry = zend_register_internal_class(&ce TSRMLS_CC);
2464         php_session_id_iface_entry->ce_flags |= ZEND_ACC_INTERFACE;
2465 
2466         /* Register base class */
2467         INIT_CLASS_ENTRY(ce, PS_CLASS_NAME, php_session_class_functions);
2468         php_session_class_entry = zend_register_internal_class(&ce TSRMLS_CC);
2469         zend_class_implements(php_session_class_entry TSRMLS_CC, 1, php_session_iface_entry);
2470         zend_class_implements(php_session_class_entry TSRMLS_CC, 1, php_session_id_iface_entry);
2471 
2472         REGISTER_LONG_CONSTANT("PHP_SESSION_DISABLED", php_session_disabled, CONST_CS | CONST_PERSISTENT);
2473         REGISTER_LONG_CONSTANT("PHP_SESSION_NONE", php_session_none, CONST_CS | CONST_PERSISTENT);
2474         REGISTER_LONG_CONSTANT("PHP_SESSION_ACTIVE", php_session_active, CONST_CS | CONST_PERSISTENT);
2475 
2476         return SUCCESS;
2477 }
2478 /* }}} */
2479 
2480 static PHP_MSHUTDOWN_FUNCTION(session) /* {{{ */
2481 {
2482         UNREGISTER_INI_ENTRIES();
2483 
2484 #ifdef HAVE_LIBMM
2485         PHP_MSHUTDOWN(ps_mm) (SHUTDOWN_FUNC_ARGS_PASSTHRU);
2486 #endif
2487 
2488         /* reset rfc1867 callbacks */
2489         php_session_rfc1867_orig_callback = NULL;
2490         if (php_rfc1867_callback == php_session_rfc1867_callback) {
2491                 php_rfc1867_callback = NULL;
2492         }
2493 
2494         ps_serializers[PREDEFINED_SERIALIZERS].name = NULL;
2495         memset(&ps_modules[PREDEFINED_MODULES], 0, (MAX_MODULES-PREDEFINED_MODULES)*sizeof(ps_module *));
2496 
2497         return SUCCESS;
2498 }
2499 /* }}} */
2500 
2501 static PHP_MINFO_FUNCTION(session) /* {{{ */
2502 {
2503         ps_module **mod;
2504         ps_serializer *ser;
2505         smart_str save_handlers = {0};
2506         smart_str ser_handlers = {0};
2507         int i;
2508 
2509         /* Get save handlers */
2510         for (i = 0, mod = ps_modules; i < MAX_MODULES; i++, mod++) {
2511                 if (*mod && (*mod)->s_name) {
2512                         smart_str_appends(&save_handlers, (*mod)->s_name);
2513                         smart_str_appendc(&save_handlers, ' ');
2514                 }
2515         }
2516 
2517         /* Get serializer handlers */
2518         for (i = 0, ser = ps_serializers; i < MAX_SERIALIZERS; i++, ser++) {
2519                 if (ser && ser->name) {
2520                         smart_str_appends(&ser_handlers, ser->name);
2521                         smart_str_appendc(&ser_handlers, ' ');
2522                 }
2523         }
2524 
2525         php_info_print_table_start();
2526         php_info_print_table_row(2, "Session Support", "enabled" );
2527 
2528         if (save_handlers.c) {
2529                 smart_str_0(&save_handlers);
2530                 php_info_print_table_row(2, "Registered save handlers", save_handlers.c);
2531                 smart_str_free(&save_handlers);
2532         } else {
2533                 php_info_print_table_row(2, "Registered save handlers", "none");
2534         }
2535 
2536         if (ser_handlers.c) {
2537                 smart_str_0(&ser_handlers);
2538                 php_info_print_table_row(2, "Registered serializer handlers", ser_handlers.c);
2539                 smart_str_free(&ser_handlers);
2540         } else {
2541                 php_info_print_table_row(2, "Registered serializer handlers", "none");
2542         }
2543 
2544         php_info_print_table_end();
2545 
2546         DISPLAY_INI_ENTRIES();
2547 }
2548 /* }}} */
2549 
2550 static const zend_module_dep session_deps[] = { /* {{{ */
2551         ZEND_MOD_OPTIONAL("hash")
2552         ZEND_MOD_REQUIRED("spl")
2553         ZEND_MOD_END
2554 };
2555 /* }}} */
2556 
2557 /* ************************
2558    * Upload hook handling *
2559    ************************ */
2560 
2561 static zend_bool early_find_sid_in(zval *dest, int where, php_session_rfc1867_progress *progress TSRMLS_DC) /* {{{ */
2562 {
2563         zval **ppid;
2564 
2565         if (!PG(http_globals)[where]) {
2566                 return 0;
2567         }
2568 
2569         if (zend_hash_find(Z_ARRVAL_P(PG(http_globals)[where]), PS(session_name), progress->sname_len+1, (void **)&ppid) == SUCCESS
2570                         && Z_TYPE_PP(ppid) == IS_STRING) {
2571                 zval_dtor(dest);
2572                 ZVAL_ZVAL(dest, *ppid, 1, 0);
2573                 return 1;
2574         }
2575 
2576         return 0;
2577 } /* }}} */
2578 
2579 static void php_session_rfc1867_early_find_sid(php_session_rfc1867_progress *progress TSRMLS_DC) /* {{{ */
2580 {
2581 
2582         if (PS(use_cookies)) {
2583                 sapi_module.treat_data(PARSE_COOKIE, NULL, NULL TSRMLS_CC);
2584                 if (early_find_sid_in(&progress->sid, TRACK_VARS_COOKIE, progress TSRMLS_CC)) {
2585                         progress->apply_trans_sid = 0;
2586                         return;
2587                 }
2588         }
2589         if (PS(use_only_cookies)) {
2590                 return;
2591         }
2592         sapi_module.treat_data(PARSE_GET, NULL, NULL TSRMLS_CC);
2593         early_find_sid_in(&progress->sid, TRACK_VARS_GET, progress TSRMLS_CC);
2594 } /* }}} */
2595 
2596 static zend_bool php_check_cancel_upload(php_session_rfc1867_progress *progress TSRMLS_DC) /* {{{ */
2597 {
2598         zval **progress_ary, **cancel_upload;
2599 
2600         if (zend_symtable_find(Z_ARRVAL_P(PS(http_session_vars)), progress->key.c, progress->key.len+1, (void**)&progress_ary) != SUCCESS) {
2601                 return 0;
2602         }
2603         if (Z_TYPE_PP(progress_ary) != IS_ARRAY) {
2604                 return 0;
2605         }
2606         if (zend_hash_find(Z_ARRVAL_PP(progress_ary), "cancel_upload", sizeof("cancel_upload"), (void**)&cancel_upload) != SUCCESS) {
2607                 return 0;
2608         }
2609         return Z_TYPE_PP(cancel_upload) == IS_BOOL && Z_LVAL_PP(cancel_upload);
2610 } /* }}} */
2611 
2612 static void php_session_rfc1867_update(php_session_rfc1867_progress *progress, int force_update TSRMLS_DC) /* {{{ */
2613 {
2614         if (!force_update) {
2615                 if (Z_LVAL_P(progress->post_bytes_processed) < progress->next_update) {
2616                         return;
2617                 }
2618 #ifdef HAVE_GETTIMEOFDAY
2619                 if (PS(rfc1867_min_freq) > 0.0) {
2620                         struct timeval tv = {0};
2621                         double dtv;
2622                         gettimeofday(&tv, NULL);
2623                         dtv = (double) tv.tv_sec + tv.tv_usec / 1000000.0;
2624                         if (dtv < progress->next_update_time) {
2625                                 return;
2626                         }
2627                         progress->next_update_time = dtv + PS(rfc1867_min_freq);
2628                 }
2629 #endif
2630                 progress->next_update = Z_LVAL_P(progress->post_bytes_processed) + progress->update_step;
2631         }
2632 
2633         php_session_initialize(TSRMLS_C);
2634         PS(session_status) = php_session_active;
2635         IF_SESSION_VARS() {
2636                 progress->cancel_upload |= php_check_cancel_upload(progress TSRMLS_CC);
2637                 ZEND_SET_SYMBOL_WITH_LENGTH(Z_ARRVAL_P(PS(http_session_vars)), progress->key.c, progress->key.len+1, progress->data, 2, 0);
2638         }
2639         php_session_flush(TSRMLS_C);
2640 } /* }}} */
2641 
2642 static void php_session_rfc1867_cleanup(php_session_rfc1867_progress *progress TSRMLS_DC) /* {{{ */
2643 {
2644         php_session_initialize(TSRMLS_C);
2645         PS(session_status) = php_session_active;
2646         IF_SESSION_VARS() {
2647                 zend_hash_del(Z_ARRVAL_P(PS(http_session_vars)), progress->key.c, progress->key.len+1);
2648         }
2649         php_session_flush(TSRMLS_C);
2650 } /* }}} */
2651 
2652 static int php_session_rfc1867_callback(unsigned int event, void *event_data, void **extra TSRMLS_DC) /* {{{ */
2653 {
2654         php_session_rfc1867_progress *progress;
2655         int retval = SUCCESS;
2656 
2657         if (php_session_rfc1867_orig_callback) {
2658                 retval = php_session_rfc1867_orig_callback(event, event_data, extra TSRMLS_CC);
2659         }
2660         if (!PS(rfc1867_enabled)) {
2661                 return retval;
2662         }
2663 
2664         progress = PS(rfc1867_progress);
2665 
2666         switch(event) {
2667                 case MULTIPART_EVENT_START: {
2668                         multipart_event_start *data = (multipart_event_start *) event_data;
2669                         progress = ecalloc(1, sizeof(php_session_rfc1867_progress));
2670                         progress->content_length = data->content_length;
2671                         progress->sname_len  = strlen(PS(session_name));
2672                         PS(rfc1867_progress) = progress;
2673                 }
2674                 break;
2675                 case MULTIPART_EVENT_FORMDATA: {
2676                         multipart_event_formdata *data = (multipart_event_formdata *) event_data;
2677                         size_t value_len;
2678 
2679                         if (Z_TYPE(progress->sid) && progress->key.c) {
2680                                 break;
2681                         }
2682 
2683                         /* orig callback may have modified *data->newlength */
2684                         if (data->newlength) {
2685                                 value_len = *data->newlength;
2686                         } else {
2687                                 value_len = data->length;
2688                         }
2689 
2690                         if (data->name && data->value && value_len) {
2691                                 size_t name_len = strlen(data->name);
2692 
2693                                 if (name_len == progress->sname_len && memcmp(data->name, PS(session_name), name_len) == 0) {
2694                                         zval_dtor(&progress->sid);
2695                                         ZVAL_STRINGL(&progress->sid, (*data->value), value_len, 1);
2696 
2697                                 } else if (name_len == PS(rfc1867_name).len && memcmp(data->name, PS(rfc1867_name).c, name_len) == 0) {
2698                                         smart_str_free(&progress->key);
2699                                         smart_str_appendl(&progress->key, PS(rfc1867_prefix).c, PS(rfc1867_prefix).len);
2700                                         smart_str_appendl(&progress->key, *data->value, value_len);
2701                                         smart_str_0(&progress->key);
2702 
2703                                         progress->apply_trans_sid = PS(use_trans_sid);
2704                                         php_session_rfc1867_early_find_sid(progress TSRMLS_CC);
2705                                 }
2706                         }
2707                 }
2708                 break;
2709                 case MULTIPART_EVENT_FILE_START: {
2710                         multipart_event_file_start *data = (multipart_event_file_start *) event_data;
2711 
2712                         /* Do nothing when $_POST["PHP_SESSION_UPLOAD_PROGRESS"] is not set
2713                          * or when we have no session id */
2714                         if (!Z_TYPE(progress->sid) || !progress->key.c) {
2715                                 break;
2716                         }
2717 
2718                         /* First FILE_START event, initializing data */
2719                         if (!progress->data) {
2720 
2721                                 if (PS(rfc1867_freq) >= 0) {
2722                                         progress->update_step = PS(rfc1867_freq);
2723                                 } else if (PS(rfc1867_freq) < 0) { /* % of total size */
2724                                         progress->update_step = progress->content_length * -PS(rfc1867_freq) / 100;
2725                                 }
2726                                 progress->next_update = 0;
2727                                 progress->next_update_time = 0.0;
2728 
2729                                 ALLOC_INIT_ZVAL(progress->data);
2730                                 array_init(progress->data);
2731 
2732                                 ALLOC_INIT_ZVAL(progress->post_bytes_processed);
2733                                 ZVAL_LONG(progress->post_bytes_processed, data->post_bytes_processed);
2734 
2735                                 ALLOC_INIT_ZVAL(progress->files);
2736                                 array_init(progress->files);
2737 
2738                                 add_assoc_long_ex(progress->data, "start_time",      sizeof("start_time"),      (long)sapi_get_request_time(TSRMLS_C));
2739                                 add_assoc_long_ex(progress->data, "content_length",  sizeof("content_length"),  progress->content_length);
2740                                 add_assoc_zval_ex(progress->data, "bytes_processed", sizeof("bytes_processed"), progress->post_bytes_processed);
2741                                 add_assoc_bool_ex(progress->data, "done",            sizeof("done"),            0);
2742                                 add_assoc_zval_ex(progress->data, "files",           sizeof("files"),           progress->files);
2743 
2744                                 php_rinit_session(0 TSRMLS_CC);
2745                                 PS(id) = estrndup(Z_STRVAL(progress->sid), Z_STRLEN(progress->sid));
2746                                 PS(apply_trans_sid) = progress->apply_trans_sid;
2747                                 PS(send_cookie) = 0;
2748                         }
2749 
2750                         ALLOC_INIT_ZVAL(progress->current_file);
2751                         array_init(progress->current_file);
2752 
2753                         ALLOC_INIT_ZVAL(progress->current_file_bytes_processed);
2754                         ZVAL_LONG(progress->current_file_bytes_processed, 0);
2755 
2756                         /* Each uploaded file has its own array. Trying to make it close to $_FILES entries. */
2757                         add_assoc_string_ex(progress->current_file, "field_name",    sizeof("field_name"),      data->name, 1);
2758                         add_assoc_string_ex(progress->current_file, "name",          sizeof("name"),            *data->filename, 1);
2759                         add_assoc_null_ex(progress->current_file, "tmp_name",        sizeof("tmp_name"));
2760                         add_assoc_long_ex(progress->current_file, "error",           sizeof("error"),           0);
2761 
2762                         add_assoc_bool_ex(progress->current_file, "done",            sizeof("done"),            0);
2763                         add_assoc_long_ex(progress->current_file, "start_time",      sizeof("start_time"),      (long)time(NULL));
2764                         add_assoc_zval_ex(progress->current_file, "bytes_processed", sizeof("bytes_processed"), progress->current_file_bytes_processed);
2765 
2766                         add_next_index_zval(progress->files, progress->current_file);
2767 
2768                         Z_LVAL_P(progress->post_bytes_processed) = data->post_bytes_processed;
2769 
2770                         php_session_rfc1867_update(progress, 0 TSRMLS_CC);
2771                 }
2772                 break;
2773                 case MULTIPART_EVENT_FILE_DATA: {
2774                         multipart_event_file_data *data = (multipart_event_file_data *) event_data;
2775 
2776                         if (!Z_TYPE(progress->sid) || !progress->key.c) {
2777                                 break;
2778                         }
2779 
2780                         Z_LVAL_P(progress->current_file_bytes_processed) = data->offset + data->length;
2781                         Z_LVAL_P(progress->post_bytes_processed) = data->post_bytes_processed;
2782 
2783                         php_session_rfc1867_update(progress, 0 TSRMLS_CC);
2784                 }
2785                 break;
2786                 case MULTIPART_EVENT_FILE_END: {
2787                         multipart_event_file_end *data = (multipart_event_file_end *) event_data;
2788 
2789                         if (!Z_TYPE(progress->sid) || !progress->key.c) {
2790                                 break;
2791                         }
2792 
2793                         if (data->temp_filename) {
2794                                 add_assoc_string_ex(progress->current_file, "tmp_name",  sizeof("tmp_name"), data->temp_filename, 1);
2795                         }
2796                         add_assoc_long_ex(progress->current_file, "error", sizeof("error"), data->cancel_upload);
2797                         add_assoc_bool_ex(progress->current_file, "done",  sizeof("done"),  1);
2798 
2799                         Z_LVAL_P(progress->post_bytes_processed) = data->post_bytes_processed;
2800 
2801                         php_session_rfc1867_update(progress, 0 TSRMLS_CC);
2802                 }
2803                 break;
2804                 case MULTIPART_EVENT_END: {
2805                         multipart_event_end *data = (multipart_event_end *) event_data;
2806 
2807                         if (Z_TYPE(progress->sid) && progress->key.c) {
2808                                 if (PS(rfc1867_cleanup)) {
2809                                         php_session_rfc1867_cleanup(progress TSRMLS_CC);
2810                                 } else {
2811                                         add_assoc_bool_ex(progress->data, "done", sizeof("done"), 1);
2812                                         Z_LVAL_P(progress->post_bytes_processed) = data->post_bytes_processed;
2813                                         php_session_rfc1867_update(progress, 1 TSRMLS_CC);
2814                                 }
2815                                 php_rshutdown_session_globals(TSRMLS_C);
2816                         }
2817 
2818                         if (progress->data) {
2819                                 zval_ptr_dtor(&progress->data);
2820                         }
2821                         zval_dtor(&progress->sid);
2822                         smart_str_free(&progress->key);
2823                         efree(progress);
2824                         progress = NULL;
2825                         PS(rfc1867_progress) = NULL;
2826                 }
2827                 break;
2828         }
2829 
2830         if (progress && progress->cancel_upload) {
2831                 return FAILURE;
2832         }
2833         return retval;
2834 
2835 } /* }}} */
2836 
2837 zend_module_entry session_module_entry = {
2838         STANDARD_MODULE_HEADER_EX,
2839         NULL,
2840         session_deps,
2841         "session",
2842         session_functions,
2843         PHP_MINIT(session), PHP_MSHUTDOWN(session),
2844         PHP_RINIT(session), PHP_RSHUTDOWN(session),
2845         PHP_MINFO(session),
2846         NO_VERSION_YET,
2847         PHP_MODULE_GLOBALS(ps),
2848         PHP_GINIT(ps),
2849         NULL,
2850         NULL,
2851         STANDARD_MODULE_PROPERTIES_EX
2852 };
2853 
2854 #ifdef COMPILE_DL_SESSION
2855 ZEND_GET_MODULE(session)
2856 #endif
2857 
2858 /*
2859  * Local variables:
2860  * tab-width: 4
2861  * c-basic-offset: 4
2862  * End:
2863  * vim600: noet sw=4 ts=4 fdm=marker
2864  * vim<600: sw=4 ts=4
2865  */

/* [<][>][^][v][top][bottom][index][help] */