1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 5 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2015 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 */
php_rinit_session_globals(TSRMLS_D)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 */
php_rshutdown_session_globals(TSRMLS_D)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
php_session_destroy(TSRMLS_D)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
php_add_session_var(char * name,size_t namelen TSRMLS_DC)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
php_set_session_var(char * name,size_t namelen,zval * state_val,php_unserialize_data_t * var_hash TSRMLS_DC)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
php_get_session_var(char * name,size_t namelen,zval *** state_var TSRMLS_DC)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
php_session_track_init(TSRMLS_D)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
php_session_encode(int * newlen TSRMLS_DC)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
php_session_decode(const char * val,int vallen TSRMLS_DC)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 */
bin_to_readable(char * in,size_t inlen,char * out,char nbits)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
php_session_create_id(PS_CREATE_SID_ARGS)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 */
php_session_valid_key(const char * key)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
php_session_initialize(TSRMLS_D)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 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
php_session_save_current_state(TSRMLS_D)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
PHP_INI_MH(OnUpdateSaveHandler)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
PHP_INI_MH(OnUpdateSerializer)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
PHP_INI_MH(OnUpdateTransSid)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
PHP_INI_MH(OnUpdateSaveDir)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
PHP_INI_MH(OnUpdateName)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
PHP_INI_MH(OnUpdateHashFunc)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 return FAILURE;
742 }
743 /* }}} */
744
PHP_INI_MH(OnUpdateRfc1867Freq)745 static PHP_INI_MH(OnUpdateRfc1867Freq) /* {{{ */
746 {
747 int tmp;
748 tmp = zend_atoi(new_value, new_value_length);
749 if(tmp < 0) {
750 php_error_docref(NULL TSRMLS_CC, E_WARNING, "session.upload_progress.freq must be greater than or equal to zero");
751 return FAILURE;
752 }
753 if(new_value_length > 0 && new_value[new_value_length-1] == '%') {
754 if(tmp > 100) {
755 php_error_docref(NULL TSRMLS_CC, E_WARNING, "session.upload_progress.freq cannot be over 100%%");
756 return FAILURE;
757 }
758 PS(rfc1867_freq) = -tmp;
759 } else {
760 PS(rfc1867_freq) = tmp;
761 }
762 return SUCCESS;
763 } /* }}} */
764
ZEND_INI_MH(OnUpdateSmartStr)765 static ZEND_INI_MH(OnUpdateSmartStr) /* {{{ */
766 {
767 smart_str *p;
768 #ifndef ZTS
769 char *base = (char *) mh_arg2;
770 #else
771 char *base;
772
773 base = (char *) ts_resource(*((int *) mh_arg2));
774 #endif
775
776 p = (smart_str *) (base+(size_t) mh_arg1);
777
778 smart_str_sets(p, new_value);
779
780 return SUCCESS;
781 }
782 /* }}} */
783
784 /* {{{ PHP_INI
785 */
786 PHP_INI_BEGIN()
787 STD_PHP_INI_ENTRY("session.save_path", "", PHP_INI_ALL, OnUpdateSaveDir,save_path, php_ps_globals, ps_globals)
788 STD_PHP_INI_ENTRY("session.name", "PHPSESSID", PHP_INI_ALL, OnUpdateName, session_name, php_ps_globals, ps_globals)
789 PHP_INI_ENTRY("session.save_handler", "files", PHP_INI_ALL, OnUpdateSaveHandler)
790 STD_PHP_INI_BOOLEAN("session.auto_start", "0", PHP_INI_PERDIR, OnUpdateBool, auto_start, php_ps_globals, ps_globals)
791 STD_PHP_INI_ENTRY("session.gc_probability", "1", PHP_INI_ALL, OnUpdateLong, gc_probability, php_ps_globals, ps_globals)
792 STD_PHP_INI_ENTRY("session.gc_divisor", "100", PHP_INI_ALL, OnUpdateLong, gc_divisor, php_ps_globals, ps_globals)
793 STD_PHP_INI_ENTRY("session.gc_maxlifetime", "1440", PHP_INI_ALL, OnUpdateLong, gc_maxlifetime, php_ps_globals, ps_globals)
794 PHP_INI_ENTRY("session.serialize_handler", "php", PHP_INI_ALL, OnUpdateSerializer)
795 STD_PHP_INI_ENTRY("session.cookie_lifetime", "0", PHP_INI_ALL, OnUpdateLong, cookie_lifetime, php_ps_globals, ps_globals)
796 STD_PHP_INI_ENTRY("session.cookie_path", "/", PHP_INI_ALL, OnUpdateString, cookie_path, php_ps_globals, ps_globals)
797 STD_PHP_INI_ENTRY("session.cookie_domain", "", PHP_INI_ALL, OnUpdateString, cookie_domain, php_ps_globals, ps_globals)
798 STD_PHP_INI_BOOLEAN("session.cookie_secure", "", PHP_INI_ALL, OnUpdateBool, cookie_secure, php_ps_globals, ps_globals)
799 STD_PHP_INI_BOOLEAN("session.cookie_httponly", "", PHP_INI_ALL, OnUpdateBool, cookie_httponly, php_ps_globals, ps_globals)
800 STD_PHP_INI_BOOLEAN("session.use_cookies", "1", PHP_INI_ALL, OnUpdateBool, use_cookies, php_ps_globals, ps_globals)
801 STD_PHP_INI_BOOLEAN("session.use_only_cookies", "1", PHP_INI_ALL, OnUpdateBool, use_only_cookies, php_ps_globals, ps_globals)
802 STD_PHP_INI_BOOLEAN("session.use_strict_mode", "0", PHP_INI_ALL, OnUpdateBool, use_strict_mode, php_ps_globals, ps_globals)
803 STD_PHP_INI_ENTRY("session.referer_check", "", PHP_INI_ALL, OnUpdateString, extern_referer_chk, php_ps_globals, ps_globals)
804 #if HAVE_DEV_URANDOM
805 STD_PHP_INI_ENTRY("session.entropy_file", "/dev/urandom", PHP_INI_ALL, OnUpdateString, entropy_file, php_ps_globals, ps_globals)
806 STD_PHP_INI_ENTRY("session.entropy_length", "32", PHP_INI_ALL, OnUpdateLong, entropy_length, php_ps_globals, ps_globals)
807 #elif HAVE_DEV_ARANDOM
808 STD_PHP_INI_ENTRY("session.entropy_file", "/dev/arandom", PHP_INI_ALL, OnUpdateString, entropy_file, php_ps_globals, ps_globals)
809 STD_PHP_INI_ENTRY("session.entropy_length", "32", PHP_INI_ALL, OnUpdateLong, entropy_length, php_ps_globals, ps_globals)
810 #else
811 STD_PHP_INI_ENTRY("session.entropy_file", "", PHP_INI_ALL, OnUpdateString, entropy_file, php_ps_globals, ps_globals)
812 STD_PHP_INI_ENTRY("session.entropy_length", "0", PHP_INI_ALL, OnUpdateLong, entropy_length, php_ps_globals, ps_globals)
813 #endif
814 STD_PHP_INI_ENTRY("session.cache_limiter", "nocache", PHP_INI_ALL, OnUpdateString, cache_limiter, php_ps_globals, ps_globals)
815 STD_PHP_INI_ENTRY("session.cache_expire", "180", PHP_INI_ALL, OnUpdateLong, cache_expire, php_ps_globals, ps_globals)
816 PHP_INI_ENTRY("session.use_trans_sid", "0", PHP_INI_ALL, OnUpdateTransSid)
817 PHP_INI_ENTRY("session.hash_function", "0", PHP_INI_ALL, OnUpdateHashFunc)
818 STD_PHP_INI_ENTRY("session.hash_bits_per_character", "4", PHP_INI_ALL, OnUpdateLong, hash_bits_per_character, php_ps_globals, ps_globals)
819
820 /* Upload progress */
821 STD_PHP_INI_BOOLEAN("session.upload_progress.enabled",
822 "1", ZEND_INI_PERDIR, OnUpdateBool, rfc1867_enabled, php_ps_globals, ps_globals)
823 STD_PHP_INI_BOOLEAN("session.upload_progress.cleanup",
824 "1", ZEND_INI_PERDIR, OnUpdateBool, rfc1867_cleanup, php_ps_globals, ps_globals)
825 STD_PHP_INI_ENTRY("session.upload_progress.prefix",
826 "upload_progress_", ZEND_INI_PERDIR, OnUpdateSmartStr, rfc1867_prefix, php_ps_globals, ps_globals)
827 STD_PHP_INI_ENTRY("session.upload_progress.name",
828 "PHP_SESSION_UPLOAD_PROGRESS", ZEND_INI_PERDIR, OnUpdateSmartStr, rfc1867_name, php_ps_globals, ps_globals)
829 STD_PHP_INI_ENTRY("session.upload_progress.freq", "1%", ZEND_INI_PERDIR, OnUpdateRfc1867Freq, rfc1867_freq, php_ps_globals, ps_globals)
830 STD_PHP_INI_ENTRY("session.upload_progress.min_freq",
831 "1", ZEND_INI_PERDIR, OnUpdateReal, rfc1867_min_freq,php_ps_globals, ps_globals)
832
833 /* Commented out until future discussion */
834 /* PHP_INI_ENTRY("session.encode_sources", "globals,track", PHP_INI_ALL, NULL) */
PHP_INI_END()835 PHP_INI_END()
836 /* }}} */
837
838 /* ***************
839 * Serializers *
840 *************** */
841 PS_SERIALIZER_ENCODE_FUNC(php_serialize) /* {{{ */
842 {
843 smart_str buf = {0};
844 php_serialize_data_t var_hash;
845
846 PHP_VAR_SERIALIZE_INIT(var_hash);
847 php_var_serialize(&buf, &PS(http_session_vars), &var_hash TSRMLS_CC);
848 PHP_VAR_SERIALIZE_DESTROY(var_hash);
849 if (newlen) {
850 *newlen = buf.len;
851 }
852 smart_str_0(&buf);
853 *newstr = buf.c;
854 return SUCCESS;
855 }
856 /* }}} */
857
PS_SERIALIZER_DECODE_FUNC(php_serialize)858 PS_SERIALIZER_DECODE_FUNC(php_serialize) /* {{{ */
859 {
860 const char *endptr = val + vallen;
861 zval *session_vars;
862 php_unserialize_data_t var_hash;
863
864 PHP_VAR_UNSERIALIZE_INIT(var_hash);
865 ALLOC_INIT_ZVAL(session_vars);
866 if (php_var_unserialize(&session_vars, &val, endptr, &var_hash TSRMLS_CC)) {
867 var_push_dtor(&var_hash, &session_vars);
868 }
869
870 PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
871 if (PS(http_session_vars)) {
872 zval_ptr_dtor(&PS(http_session_vars));
873 }
874 if (Z_TYPE_P(session_vars) == IS_NULL) {
875 array_init(session_vars);
876 }
877 PS(http_session_vars) = session_vars;
878 ZEND_SET_GLOBAL_VAR_WITH_LENGTH("_SESSION", sizeof("_SESSION"), PS(http_session_vars), Z_REFCOUNT_P(PS(http_session_vars)) + 1, 1);
879 return SUCCESS;
880 }
881 /* }}} */
882
883 #define PS_BIN_NR_OF_BITS 8
884 #define PS_BIN_UNDEF (1<<(PS_BIN_NR_OF_BITS-1))
885 #define PS_BIN_MAX (PS_BIN_UNDEF-1)
886
PS_SERIALIZER_ENCODE_FUNC(php_binary)887 PS_SERIALIZER_ENCODE_FUNC(php_binary) /* {{{ */
888 {
889 smart_str buf = {0};
890 php_serialize_data_t var_hash;
891 PS_ENCODE_VARS;
892
893 PHP_VAR_SERIALIZE_INIT(var_hash);
894
895 PS_ENCODE_LOOP(
896 if (key_length > PS_BIN_MAX) continue;
897 smart_str_appendc(&buf, (unsigned char) key_length);
898 smart_str_appendl(&buf, key, key_length);
899 php_var_serialize(&buf, struc, &var_hash TSRMLS_CC);
900 } else {
901 if (key_length > PS_BIN_MAX) continue;
902 smart_str_appendc(&buf, (unsigned char) (key_length & PS_BIN_UNDEF));
903 smart_str_appendl(&buf, key, key_length);
904 );
905
906 if (newlen) {
907 *newlen = buf.len;
908 }
909 smart_str_0(&buf);
910 *newstr = buf.c;
911 PHP_VAR_SERIALIZE_DESTROY(var_hash);
912
913 return SUCCESS;
914 }
915 /* }}} */
916
917 PS_SERIALIZER_DECODE_FUNC(php_binary) /* {{{ */
918 {
919 const char *p;
920 char *name;
921 const char *endptr = val + vallen;
922 zval *current;
923 int namelen;
924 int has_value;
925 php_unserialize_data_t var_hash;
926
927 PHP_VAR_UNSERIALIZE_INIT(var_hash);
928
929 for (p = val; p < endptr; ) {
930 zval **tmp;
931 namelen = ((unsigned char)(*p)) & (~PS_BIN_UNDEF);
932
933 if (namelen < 0 || namelen > PS_BIN_MAX || (p + namelen) >= endptr) {
934 PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
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(¤t, (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, ¤t);
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(¤t, (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, ¤t);
1052 efree(name);
1053 PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
1054 return FAILURE;
1055 }
1056 var_push_dtor_no_addref(&var_hash, ¤t);
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 PHPAPI void session_adapt_url(const char *url, size_t urllen, char **new, size_t *newlen TSRMLS_DC) /* {{{ */
1640 {
1641 if (PS(apply_trans_sid) && (PS(session_status) == php_session_active)) {
1642 *new = php_url_scanner_adapt_single_url(url, urllen, PS(session_name), PS(id), newlen TSRMLS_CC);
1643 }
1644 }
1645 /* }}} */
1646
1647 /* ********************************
1648 * Userspace exported functions *
1649 ******************************** */
1650
1651 /* {{{ proto void session_set_cookie_params(int lifetime [, string path [, string domain [, bool secure[, bool httponly]]]])
1652 Set session cookie parameters */
1653 static PHP_FUNCTION(session_set_cookie_params)
1654 {
1655 zval **lifetime = NULL;
1656 char *path = NULL, *domain = NULL;
1657 int path_len, domain_len, argc = ZEND_NUM_ARGS();
1658 zend_bool secure = 0, httponly = 0;
1659
1660 if (!PS(use_cookies) ||
1661 zend_parse_parameters(argc TSRMLS_CC, "Z|ssbb", &lifetime, &path, &path_len, &domain, &domain_len, &secure, &httponly) == FAILURE) {
1662 return;
1663 }
1664
1665 convert_to_string_ex(lifetime);
1666
1667 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);
1668
1669 if (path) {
1670 zend_alter_ini_entry("session.cookie_path", sizeof("session.cookie_path"), path, path_len, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
1671 }
1672 if (domain) {
1673 zend_alter_ini_entry("session.cookie_domain", sizeof("session.cookie_domain"), domain, domain_len, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
1674 }
1675
1676 if (argc > 3) {
1677 zend_alter_ini_entry("session.cookie_secure", sizeof("session.cookie_secure"), secure ? "1" : "0", 1, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
1678 }
1679 if (argc > 4) {
1680 zend_alter_ini_entry("session.cookie_httponly", sizeof("session.cookie_httponly"), httponly ? "1" : "0", 1, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
1681 }
1682 }
1683 /* }}} */
1684
1685 /* {{{ proto array session_get_cookie_params(void)
1686 Return the session cookie parameters */
1687 static PHP_FUNCTION(session_get_cookie_params)
1688 {
1689 if (zend_parse_parameters_none() == FAILURE) {
1690 return;
1691 }
1692
1693 array_init(return_value);
1694
1695 add_assoc_long(return_value, "lifetime", PS(cookie_lifetime));
1696 add_assoc_string(return_value, "path", PS(cookie_path), 1);
1697 add_assoc_string(return_value, "domain", PS(cookie_domain), 1);
1698 add_assoc_bool(return_value, "secure", PS(cookie_secure));
1699 add_assoc_bool(return_value, "httponly", PS(cookie_httponly));
1700 }
1701 /* }}} */
1702
1703 /* {{{ proto string session_name([string newname])
1704 Return the current session name. If newname is given, the session name is replaced with newname */
1705 static PHP_FUNCTION(session_name)
1706 {
1707 char *name = NULL;
1708 int name_len;
1709
1710 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &name, &name_len) == FAILURE) {
1711 return;
1712 }
1713
1714 RETVAL_STRING(PS(session_name), 1);
1715
1716 if (name) {
1717 zend_alter_ini_entry("session.name", sizeof("session.name"), name, name_len, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
1718 }
1719 }
1720 /* }}} */
1721
1722 /* {{{ proto string session_module_name([string newname])
1723 Return the current module name used for accessing session data. If newname is given, the module name is replaced with newname */
1724 static PHP_FUNCTION(session_module_name)
1725 {
1726 char *name = NULL;
1727 int name_len;
1728
1729 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &name, &name_len) == FAILURE) {
1730 return;
1731 }
1732
1733 /* Set return_value to current module name */
1734 if (PS(mod) && PS(mod)->s_name) {
1735 RETVAL_STRING(safe_estrdup(PS(mod)->s_name), 0);
1736 } else {
1737 RETVAL_EMPTY_STRING();
1738 }
1739
1740 if (name) {
1741 if (!_php_find_ps_module(name TSRMLS_CC)) {
1742 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot find named PHP session module (%s)", name);
1743
1744 zval_dtor(return_value);
1745 RETURN_FALSE;
1746 }
1747 if (PS(mod_data) || PS(mod_user_implemented)) {
1748 PS(mod)->s_close(&PS(mod_data) TSRMLS_CC);
1749 }
1750 PS(mod_data) = NULL;
1751
1752 zend_alter_ini_entry("session.save_handler", sizeof("session.save_handler"), name, name_len, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
1753 }
1754 }
1755 /* }}} */
1756
1757 /* {{{ proto void session_set_save_handler(string open, string close, string read, string write, string destroy, string gc, string create_sid)
1758 Sets user-level functions */
1759 static PHP_FUNCTION(session_set_save_handler)
1760 {
1761 zval ***args = NULL;
1762 int i, num_args, argc = ZEND_NUM_ARGS();
1763 char *name;
1764
1765 if (PS(session_status) != php_session_none) {
1766 RETURN_FALSE;
1767 }
1768
1769 if (argc > 0 && argc <= 2) {
1770 zval *obj = NULL, *callback = NULL;
1771 zend_uint func_name_len;
1772 char *func_name;
1773 HashPosition pos;
1774 zend_function *default_mptr, *current_mptr;
1775 ulong func_index;
1776 php_shutdown_function_entry shutdown_function_entry;
1777 zend_bool register_shutdown = 1;
1778
1779 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|b", &obj, php_session_iface_entry, ®ister_shutdown) == FAILURE) {
1780 RETURN_FALSE;
1781 }
1782
1783 /* Find implemented methods - SessionHandlerInterface */
1784 zend_hash_internal_pointer_reset_ex(&php_session_iface_entry->function_table, &pos);
1785 i = 0;
1786 while (zend_hash_get_current_data_ex(&php_session_iface_entry->function_table, (void **) &default_mptr, &pos) == SUCCESS) {
1787 zend_hash_get_current_key_ex(&php_session_iface_entry->function_table, &func_name, &func_name_len, &func_index, 0, &pos);
1788
1789 if (zend_hash_find(&Z_OBJCE_P(obj)->function_table, func_name, func_name_len, (void **)¤t_mptr) == SUCCESS) {
1790 if (PS(mod_user_names).names[i] != NULL) {
1791 zval_ptr_dtor(&PS(mod_user_names).names[i]);
1792 }
1793
1794 MAKE_STD_ZVAL(callback);
1795 array_init_size(callback, 2);
1796 Z_ADDREF_P(obj);
1797 add_next_index_zval(callback, obj);
1798 add_next_index_stringl(callback, func_name, func_name_len - 1, 1);
1799 PS(mod_user_names).names[i] = callback;
1800 } else {
1801 php_error_docref(NULL TSRMLS_CC, E_ERROR, "Session handler's function table is corrupt");
1802 RETURN_FALSE;
1803 }
1804
1805 zend_hash_move_forward_ex(&php_session_iface_entry->function_table, &pos);
1806 ++i;
1807 }
1808
1809 /* Find implemented methods - SessionIdInterface (optional) */
1810 zend_hash_internal_pointer_reset_ex(&php_session_id_iface_entry->function_table, &pos);
1811 while (zend_hash_get_current_data_ex(&php_session_id_iface_entry->function_table, (void **) &default_mptr, &pos) == SUCCESS) {
1812 zend_hash_get_current_key_ex(&php_session_id_iface_entry->function_table, &func_name, &func_name_len, &func_index, 0, &pos);
1813
1814 if (zend_hash_find(&Z_OBJCE_P(obj)->function_table, func_name, func_name_len, (void **)¤t_mptr) == SUCCESS) {
1815 if (PS(mod_user_names).names[i] != NULL) {
1816 zval_ptr_dtor(&PS(mod_user_names).names[i]);
1817 }
1818
1819 MAKE_STD_ZVAL(callback);
1820 array_init_size(callback, 2);
1821 Z_ADDREF_P(obj);
1822 add_next_index_zval(callback, obj);
1823 add_next_index_stringl(callback, func_name, func_name_len - 1, 1);
1824 PS(mod_user_names).names[i] = callback;
1825 }
1826
1827 zend_hash_move_forward_ex(&php_session_id_iface_entry->function_table, &pos);
1828 ++i;
1829 }
1830
1831 if (register_shutdown) {
1832 /* create shutdown function */
1833 shutdown_function_entry.arg_count = 1;
1834 shutdown_function_entry.arguments = (zval **) safe_emalloc(sizeof(zval *), 1, 0);
1835
1836 MAKE_STD_ZVAL(callback);
1837 ZVAL_STRING(callback, "session_register_shutdown", 1);
1838 shutdown_function_entry.arguments[0] = callback;
1839
1840 /* add shutdown function, removing the old one if it exists */
1841 if (!register_user_shutdown_function("session_shutdown", sizeof("session_shutdown"), &shutdown_function_entry TSRMLS_CC)) {
1842 zval_ptr_dtor(&callback);
1843 efree(shutdown_function_entry.arguments);
1844 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to register session shutdown function");
1845 RETURN_FALSE;
1846 }
1847 } else {
1848 /* remove shutdown function */
1849 remove_user_shutdown_function("session_shutdown", sizeof("session_shutdown") TSRMLS_CC);
1850 }
1851
1852 if (PS(mod) && PS(session_status) == php_session_none && PS(mod) != &ps_mod_user) {
1853 zend_alter_ini_entry("session.save_handler", sizeof("session.save_handler"), "user", sizeof("user")-1, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
1854 }
1855
1856 RETURN_TRUE;
1857 }
1858
1859 if (argc != 6 && argc != 7) {
1860 WRONG_PARAM_COUNT;
1861 }
1862
1863 if (zend_parse_parameters(argc TSRMLS_CC, "+", &args, &num_args) == FAILURE) {
1864 return;
1865 }
1866
1867 /* remove shutdown function */
1868 remove_user_shutdown_function("session_shutdown", sizeof("session_shutdown") TSRMLS_CC);
1869
1870 /* at this point argc can only be 6 or 7 */
1871 for (i = 0; i < argc; i++) {
1872 if (!zend_is_callable(*args[i], 0, &name TSRMLS_CC)) {
1873 efree(args);
1874 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Argument %d is not a valid callback", i+1);
1875 efree(name);
1876 RETURN_FALSE;
1877 }
1878 efree(name);
1879 }
1880
1881 if (PS(mod) && PS(mod) != &ps_mod_user) {
1882 zend_alter_ini_entry("session.save_handler", sizeof("session.save_handler"), "user", sizeof("user")-1, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
1883 }
1884
1885 for (i = 0; i < argc; i++) {
1886 if (PS(mod_user_names).names[i] != NULL) {
1887 zval_ptr_dtor(&PS(mod_user_names).names[i]);
1888 }
1889 Z_ADDREF_PP(args[i]);
1890 PS(mod_user_names).names[i] = *args[i];
1891 }
1892
1893 efree(args);
1894 RETURN_TRUE;
1895 }
1896 /* }}} */
1897
1898 /* {{{ proto string session_save_path([string newname])
1899 Return the current save path passed to module_name. If newname is given, the save path is replaced with newname */
1900 static PHP_FUNCTION(session_save_path)
1901 {
1902 char *name = NULL;
1903 int name_len;
1904
1905 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &name, &name_len) == FAILURE) {
1906 return;
1907 }
1908
1909 RETVAL_STRING(PS(save_path), 1);
1910
1911 if (name) {
1912 if (memchr(name, '\0', name_len) != NULL) {
1913 php_error_docref(NULL TSRMLS_CC, E_WARNING, "The save_path cannot contain NULL characters");
1914 zval_dtor(return_value);
1915 RETURN_FALSE;
1916 }
1917 zend_alter_ini_entry("session.save_path", sizeof("session.save_path"), name, name_len, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
1918 }
1919 }
1920 /* }}} */
1921
1922 /* {{{ proto string session_id([string newid])
1923 Return the current session id. If newid is given, the session id is replaced with newid */
1924 static PHP_FUNCTION(session_id)
1925 {
1926 char *name = NULL;
1927 int name_len, argc = ZEND_NUM_ARGS();
1928
1929 if (zend_parse_parameters(argc TSRMLS_CC, "|s", &name, &name_len) == FAILURE) {
1930 return;
1931 }
1932
1933 if (PS(id)) {
1934 RETVAL_STRING(PS(id), 1);
1935 } else {
1936 RETVAL_EMPTY_STRING();
1937 }
1938
1939 if (name) {
1940 if (PS(id)) {
1941 efree(PS(id));
1942 }
1943 PS(id) = estrndup(name, name_len);
1944 }
1945 }
1946 /* }}} */
1947
1948 /* {{{ proto bool session_regenerate_id([bool delete_old_session])
1949 Update the current session id with a newly generated one. If delete_old_session is set to true, remove the old session. */
1950 static PHP_FUNCTION(session_regenerate_id)
1951 {
1952 zend_bool del_ses = 0;
1953
1954 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &del_ses) == FAILURE) {
1955 return;
1956 }
1957
1958 if (SG(headers_sent) && PS(use_cookies)) {
1959 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot regenerate session id - headers already sent");
1960 RETURN_FALSE;
1961 }
1962
1963 if (PS(session_status) == php_session_active) {
1964 if (PS(id)) {
1965 if (del_ses && PS(mod)->s_destroy(&PS(mod_data), PS(id) TSRMLS_CC) == FAILURE) {
1966 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Session object destruction failed");
1967 RETURN_FALSE;
1968 }
1969 efree(PS(id));
1970 PS(id) = NULL;
1971 }
1972
1973 PS(id) = PS(mod)->s_create_sid(&PS(mod_data), NULL TSRMLS_CC);
1974 if (PS(id)) {
1975 PS(send_cookie) = 1;
1976 php_session_reset_id(TSRMLS_C);
1977 RETURN_TRUE;
1978 } else {
1979 PS(id) = STR_EMPTY_ALLOC();
1980 }
1981 }
1982 RETURN_FALSE;
1983 }
1984 /* }}} */
1985
1986 /* {{{ proto string session_cache_limiter([string new_cache_limiter])
1987 Return the current cache limiter. If new_cache_limited is given, the current cache_limiter is replaced with new_cache_limiter */
1988 static PHP_FUNCTION(session_cache_limiter)
1989 {
1990 char *limiter = NULL;
1991 int limiter_len;
1992
1993 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &limiter, &limiter_len) == FAILURE) {
1994 return;
1995 }
1996
1997 RETVAL_STRING(PS(cache_limiter), 1);
1998
1999 if (limiter) {
2000 zend_alter_ini_entry("session.cache_limiter", sizeof("session.cache_limiter"), limiter, limiter_len, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
2001 }
2002 }
2003 /* }}} */
2004
2005 /* {{{ proto int session_cache_expire([int new_cache_expire])
2006 Return the current cache expire. If new_cache_expire is given, the current cache_expire is replaced with new_cache_expire */
2007 static PHP_FUNCTION(session_cache_expire)
2008 {
2009 zval **expires = NULL;
2010 int argc = ZEND_NUM_ARGS();
2011
2012 if (zend_parse_parameters(argc TSRMLS_CC, "|Z", &expires) == FAILURE) {
2013 return;
2014 }
2015
2016 RETVAL_LONG(PS(cache_expire));
2017
2018 if (argc == 1) {
2019 convert_to_string_ex(expires);
2020 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);
2021 }
2022 }
2023 /* }}} */
2024
2025 /* {{{ proto string session_encode(void)
2026 Serializes the current setup and returns the serialized representation */
2027 static PHP_FUNCTION(session_encode)
2028 {
2029 int len;
2030 char *enc;
2031
2032 if (zend_parse_parameters_none() == FAILURE) {
2033 return;
2034 }
2035
2036 enc = php_session_encode(&len TSRMLS_CC);
2037 if (enc == NULL) {
2038 RETURN_FALSE;
2039 }
2040
2041 RETVAL_STRINGL(enc, len, 0);
2042 }
2043 /* }}} */
2044
2045 /* {{{ proto bool session_decode(string data)
2046 Deserializes data and reinitializes the variables */
2047 static PHP_FUNCTION(session_decode)
2048 {
2049 char *str;
2050 int str_len;
2051
2052 if (PS(session_status) == php_session_none) {
2053 RETURN_FALSE;
2054 }
2055
2056 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
2057 return;
2058 }
2059
2060 RETVAL_BOOL(php_session_decode(str, str_len TSRMLS_CC) == SUCCESS);
2061 }
2062 /* }}} */
2063
2064 /* {{{ proto bool session_start(void)
2065 Begin session - reinitializes freezed variables, registers browsers etc */
2066 static PHP_FUNCTION(session_start)
2067 {
2068 /* skipping check for non-zero args for performance reasons here ?*/
2069 if (PS(id) && !strlen(PS(id))) {
2070 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot start session with empty session ID");
2071 RETURN_FALSE;
2072 }
2073
2074 php_session_start(TSRMLS_C);
2075
2076 if (PS(session_status) != php_session_active) {
2077 RETURN_FALSE;
2078 }
2079 RETURN_TRUE;
2080 }
2081 /* }}} */
2082
2083 /* {{{ proto bool session_destroy(void)
2084 Destroy the current session and all data associated with it */
2085 static PHP_FUNCTION(session_destroy)
2086 {
2087 if (zend_parse_parameters_none() == FAILURE) {
2088 return;
2089 }
2090
2091 RETURN_BOOL(php_session_destroy(TSRMLS_C) == SUCCESS);
2092 }
2093 /* }}} */
2094
2095 /* {{{ proto void session_unset(void)
2096 Unset all registered variables */
2097 static PHP_FUNCTION(session_unset)
2098 {
2099 if (PS(session_status) == php_session_none) {
2100 RETURN_FALSE;
2101 }
2102
2103 IF_SESSION_VARS() {
2104 HashTable *ht_sess_var;
2105
2106 SEPARATE_ZVAL_IF_NOT_REF(&PS(http_session_vars));
2107 ht_sess_var = Z_ARRVAL_P(PS(http_session_vars));
2108
2109 /* Clean $_SESSION. */
2110 zend_hash_clean(ht_sess_var);
2111 }
2112 }
2113 /* }}} */
2114
2115 /* {{{ proto void session_write_close(void)
2116 Write session data and end session */
2117 static PHP_FUNCTION(session_write_close)
2118 {
2119 php_session_flush(TSRMLS_C);
2120 }
2121 /* }}} */
2122
2123 /* {{{ proto int session_status(void)
2124 Returns the current session status */
2125 static PHP_FUNCTION(session_status)
2126 {
2127 if (zend_parse_parameters_none() == FAILURE) {
2128 return;
2129 }
2130
2131 RETURN_LONG(PS(session_status));
2132 }
2133 /* }}} */
2134
2135 /* {{{ proto void session_register_shutdown(void)
2136 Registers session_write_close() as a shutdown function */
2137 static PHP_FUNCTION(session_register_shutdown)
2138 {
2139 php_shutdown_function_entry shutdown_function_entry;
2140 zval *callback;
2141
2142 /* This function is registered itself as a shutdown function by
2143 * session_set_save_handler($obj). The reason we now register another
2144 * shutdown function is in case the user registered their own shutdown
2145 * function after calling session_set_save_handler(), which expects
2146 * the session still to be available.
2147 */
2148
2149 shutdown_function_entry.arg_count = 1;
2150 shutdown_function_entry.arguments = (zval **) safe_emalloc(sizeof(zval *), 1, 0);
2151
2152 MAKE_STD_ZVAL(callback);
2153 ZVAL_STRING(callback, "session_write_close", 1);
2154 shutdown_function_entry.arguments[0] = callback;
2155
2156 if (!append_user_shutdown_function(shutdown_function_entry TSRMLS_CC)) {
2157 zval_ptr_dtor(&callback);
2158 efree(shutdown_function_entry.arguments);
2159
2160 /* Unable to register shutdown function, presumably because of lack
2161 * of memory, so flush the session now. It would be done in rshutdown
2162 * anyway but the handler will have had it's dtor called by then.
2163 * If the user does have a later shutdown function which needs the
2164 * session then tough luck.
2165 */
2166 php_session_flush(TSRMLS_C);
2167 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to register session flush function");
2168 }
2169 }
2170 /* }}} */
2171
2172 /* {{{ arginfo */
2173 ZEND_BEGIN_ARG_INFO_EX(arginfo_session_name, 0, 0, 0)
2174 ZEND_ARG_INFO(0, name)
2175 ZEND_END_ARG_INFO()
2176
2177 ZEND_BEGIN_ARG_INFO_EX(arginfo_session_module_name, 0, 0, 0)
2178 ZEND_ARG_INFO(0, module)
2179 ZEND_END_ARG_INFO()
2180
2181 ZEND_BEGIN_ARG_INFO_EX(arginfo_session_save_path, 0, 0, 0)
2182 ZEND_ARG_INFO(0, path)
2183 ZEND_END_ARG_INFO()
2184
2185 ZEND_BEGIN_ARG_INFO_EX(arginfo_session_id, 0, 0, 0)
2186 ZEND_ARG_INFO(0, id)
2187 ZEND_END_ARG_INFO()
2188
2189 ZEND_BEGIN_ARG_INFO_EX(arginfo_session_regenerate_id, 0, 0, 0)
2190 ZEND_ARG_INFO(0, delete_old_session)
2191 ZEND_END_ARG_INFO()
2192
2193 ZEND_BEGIN_ARG_INFO_EX(arginfo_session_decode, 0, 0, 1)
2194 ZEND_ARG_INFO(0, data)
2195 ZEND_END_ARG_INFO()
2196
2197 ZEND_BEGIN_ARG_INFO(arginfo_session_void, 0)
2198 ZEND_END_ARG_INFO()
2199
2200 ZEND_BEGIN_ARG_INFO_EX(arginfo_session_set_save_handler, 0, 0, 1)
2201 ZEND_ARG_INFO(0, open)
2202 ZEND_ARG_INFO(0, close)
2203 ZEND_ARG_INFO(0, read)
2204 ZEND_ARG_INFO(0, write)
2205 ZEND_ARG_INFO(0, destroy)
2206 ZEND_ARG_INFO(0, gc)
2207 ZEND_ARG_INFO(0, create_sid)
2208 ZEND_END_ARG_INFO()
2209
2210 ZEND_BEGIN_ARG_INFO_EX(arginfo_session_cache_limiter, 0, 0, 0)
2211 ZEND_ARG_INFO(0, cache_limiter)
2212 ZEND_END_ARG_INFO()
2213
2214 ZEND_BEGIN_ARG_INFO_EX(arginfo_session_cache_expire, 0, 0, 0)
2215 ZEND_ARG_INFO(0, new_cache_expire)
2216 ZEND_END_ARG_INFO()
2217
2218 ZEND_BEGIN_ARG_INFO_EX(arginfo_session_set_cookie_params, 0, 0, 1)
2219 ZEND_ARG_INFO(0, lifetime)
2220 ZEND_ARG_INFO(0, path)
2221 ZEND_ARG_INFO(0, domain)
2222 ZEND_ARG_INFO(0, secure)
2223 ZEND_ARG_INFO(0, httponly)
2224 ZEND_END_ARG_INFO()
2225
2226 ZEND_BEGIN_ARG_INFO(arginfo_session_class_open, 0)
2227 ZEND_ARG_INFO(0, save_path)
2228 ZEND_ARG_INFO(0, session_name)
2229 ZEND_END_ARG_INFO()
2230
2231 ZEND_BEGIN_ARG_INFO(arginfo_session_class_close, 0)
2232 ZEND_END_ARG_INFO()
2233
2234 ZEND_BEGIN_ARG_INFO(arginfo_session_class_read, 0)
2235 ZEND_ARG_INFO(0, key)
2236 ZEND_END_ARG_INFO()
2237
2238 ZEND_BEGIN_ARG_INFO(arginfo_session_class_write, 0)
2239 ZEND_ARG_INFO(0, key)
2240 ZEND_ARG_INFO(0, val)
2241 ZEND_END_ARG_INFO()
2242
2243 ZEND_BEGIN_ARG_INFO(arginfo_session_class_destroy, 0)
2244 ZEND_ARG_INFO(0, key)
2245 ZEND_END_ARG_INFO()
2246
2247 ZEND_BEGIN_ARG_INFO(arginfo_session_class_gc, 0)
2248 ZEND_ARG_INFO(0, maxlifetime)
2249 ZEND_END_ARG_INFO()
2250
2251 ZEND_BEGIN_ARG_INFO(arginfo_session_class_create_sid, 0)
2252 ZEND_END_ARG_INFO()
2253 /* }}} */
2254
2255 /* {{{ session_functions[]
2256 */
2257 static const zend_function_entry session_functions[] = {
2258 PHP_FE(session_name, arginfo_session_name)
2259 PHP_FE(session_module_name, arginfo_session_module_name)
2260 PHP_FE(session_save_path, arginfo_session_save_path)
2261 PHP_FE(session_id, arginfo_session_id)
2262 PHP_FE(session_regenerate_id, arginfo_session_regenerate_id)
2263 PHP_FE(session_decode, arginfo_session_decode)
2264 PHP_FE(session_encode, arginfo_session_void)
2265 PHP_FE(session_start, arginfo_session_void)
2266 PHP_FE(session_destroy, arginfo_session_void)
2267 PHP_FE(session_unset, arginfo_session_void)
2268 PHP_FE(session_set_save_handler, arginfo_session_set_save_handler)
2269 PHP_FE(session_cache_limiter, arginfo_session_cache_limiter)
2270 PHP_FE(session_cache_expire, arginfo_session_cache_expire)
2271 PHP_FE(session_set_cookie_params, arginfo_session_set_cookie_params)
2272 PHP_FE(session_get_cookie_params, arginfo_session_void)
2273 PHP_FE(session_write_close, arginfo_session_void)
2274 PHP_FE(session_status, arginfo_session_void)
2275 PHP_FE(session_register_shutdown, arginfo_session_void)
2276 PHP_FALIAS(session_commit, session_write_close, arginfo_session_void)
2277 PHP_FE_END
2278 };
2279 /* }}} */
2280
2281 /* {{{ SessionHandlerInterface functions[]
2282 */
2283 static const zend_function_entry php_session_iface_functions[] = {
2284 PHP_ABSTRACT_ME(SessionHandlerInterface, open, arginfo_session_class_open)
2285 PHP_ABSTRACT_ME(SessionHandlerInterface, close, arginfo_session_class_close)
2286 PHP_ABSTRACT_ME(SessionHandlerInterface, read, arginfo_session_class_read)
2287 PHP_ABSTRACT_ME(SessionHandlerInterface, write, arginfo_session_class_write)
2288 PHP_ABSTRACT_ME(SessionHandlerInterface, destroy, arginfo_session_class_destroy)
2289 PHP_ABSTRACT_ME(SessionHandlerInterface, gc, arginfo_session_class_gc)
2290 { NULL, NULL, NULL }
2291 };
2292 /* }}} */
2293
2294 /* {{{ SessionIdInterface functions[]
2295 */
2296 static const zend_function_entry php_session_id_iface_functions[] = {
2297 PHP_ABSTRACT_ME(SessionIdInterface, create_sid, arginfo_session_class_create_sid)
2298 { NULL, NULL, NULL }
2299 };
2300 /* }}} */
2301
2302 /* {{{ SessionHandler functions[]
2303 */
2304 static const zend_function_entry php_session_class_functions[] = {
2305 PHP_ME(SessionHandler, open, arginfo_session_class_open, ZEND_ACC_PUBLIC)
2306 PHP_ME(SessionHandler, close, arginfo_session_class_close, ZEND_ACC_PUBLIC)
2307 PHP_ME(SessionHandler, read, arginfo_session_class_read, ZEND_ACC_PUBLIC)
2308 PHP_ME(SessionHandler, write, arginfo_session_class_write, ZEND_ACC_PUBLIC)
2309 PHP_ME(SessionHandler, destroy, arginfo_session_class_destroy, ZEND_ACC_PUBLIC)
2310 PHP_ME(SessionHandler, gc, arginfo_session_class_gc, ZEND_ACC_PUBLIC)
2311 PHP_ME(SessionHandler, create_sid, arginfo_session_class_create_sid, ZEND_ACC_PUBLIC)
2312 { NULL, NULL, NULL }
2313 };
2314 /* }}} */
2315
2316 /* ********************************
2317 * Module Setup and Destruction *
2318 ******************************** */
2319
2320 static int php_rinit_session(zend_bool auto_start TSRMLS_DC) /* {{{ */
2321 {
2322 php_rinit_session_globals(TSRMLS_C);
2323
2324 if (PS(mod) == NULL) {
2325 char *value;
2326
2327 value = zend_ini_string("session.save_handler", sizeof("session.save_handler"), 0);
2328 if (value) {
2329 PS(mod) = _php_find_ps_module(value TSRMLS_CC);
2330 }
2331 }
2332
2333 if (PS(serializer) == NULL) {
2334 char *value;
2335
2336 value = zend_ini_string("session.serialize_handler", sizeof("session.serialize_handler"), 0);
2337 if (value) {
2338 PS(serializer) = _php_find_ps_serializer(value TSRMLS_CC);
2339 }
2340 }
2341
2342 if (PS(mod) == NULL || PS(serializer) == NULL) {
2343 /* current status is unusable */
2344 PS(session_status) = php_session_disabled;
2345 return SUCCESS;
2346 }
2347
2348 if (auto_start) {
2349 php_session_start(TSRMLS_C);
2350 }
2351
2352 return SUCCESS;
2353 } /* }}} */
2354
2355 static PHP_RINIT_FUNCTION(session) /* {{{ */
2356 {
2357 return php_rinit_session(PS(auto_start) TSRMLS_CC);
2358 }
2359 /* }}} */
2360
2361 static PHP_RSHUTDOWN_FUNCTION(session) /* {{{ */
2362 {
2363 int i;
2364
2365 zend_try {
2366 php_session_flush(TSRMLS_C);
2367 } zend_end_try();
2368 php_rshutdown_session_globals(TSRMLS_C);
2369
2370 /* this should NOT be done in php_rshutdown_session_globals() */
2371 for (i = 0; i < 7; i++) {
2372 if (PS(mod_user_names).names[i] != NULL) {
2373 zval_ptr_dtor(&PS(mod_user_names).names[i]);
2374 PS(mod_user_names).names[i] = NULL;
2375 }
2376 }
2377
2378 return SUCCESS;
2379 }
2380 /* }}} */
2381
2382 static PHP_GINIT_FUNCTION(ps) /* {{{ */
2383 {
2384 int i;
2385
2386 ps_globals->save_path = NULL;
2387 ps_globals->session_name = NULL;
2388 ps_globals->id = NULL;
2389 ps_globals->mod = NULL;
2390 ps_globals->serializer = NULL;
2391 ps_globals->mod_data = NULL;
2392 ps_globals->session_status = php_session_none;
2393 ps_globals->default_mod = NULL;
2394 ps_globals->mod_user_implemented = 0;
2395 ps_globals->mod_user_is_open = 0;
2396 for (i = 0; i < 7; i++) {
2397 ps_globals->mod_user_names.names[i] = NULL;
2398 }
2399 ps_globals->http_session_vars = NULL;
2400 }
2401 /* }}} */
2402
2403 static PHP_MINIT_FUNCTION(session) /* {{{ */
2404 {
2405 zend_class_entry ce;
2406
2407 zend_register_auto_global("_SESSION", sizeof("_SESSION")-1, 0, NULL TSRMLS_CC);
2408
2409 PS(module_number) = module_number; /* if we really need this var we need to init it in zts mode as well! */
2410
2411 PS(session_status) = php_session_none;
2412 REGISTER_INI_ENTRIES();
2413
2414 #ifdef HAVE_LIBMM
2415 PHP_MINIT(ps_mm) (INIT_FUNC_ARGS_PASSTHRU);
2416 #endif
2417 php_session_rfc1867_orig_callback = php_rfc1867_callback;
2418 php_rfc1867_callback = php_session_rfc1867_callback;
2419
2420 /* Register interfaces */
2421 INIT_CLASS_ENTRY(ce, PS_IFACE_NAME, php_session_iface_functions);
2422 php_session_iface_entry = zend_register_internal_class(&ce TSRMLS_CC);
2423 php_session_iface_entry->ce_flags |= ZEND_ACC_INTERFACE;
2424
2425 INIT_CLASS_ENTRY(ce, PS_SID_IFACE_NAME, php_session_id_iface_functions);
2426 php_session_id_iface_entry = zend_register_internal_class(&ce TSRMLS_CC);
2427 php_session_id_iface_entry->ce_flags |= ZEND_ACC_INTERFACE;
2428
2429 /* Register base class */
2430 INIT_CLASS_ENTRY(ce, PS_CLASS_NAME, php_session_class_functions);
2431 php_session_class_entry = zend_register_internal_class(&ce TSRMLS_CC);
2432 zend_class_implements(php_session_class_entry TSRMLS_CC, 1, php_session_iface_entry);
2433 zend_class_implements(php_session_class_entry TSRMLS_CC, 1, php_session_id_iface_entry);
2434
2435 REGISTER_LONG_CONSTANT("PHP_SESSION_DISABLED", php_session_disabled, CONST_CS | CONST_PERSISTENT);
2436 REGISTER_LONG_CONSTANT("PHP_SESSION_NONE", php_session_none, CONST_CS | CONST_PERSISTENT);
2437 REGISTER_LONG_CONSTANT("PHP_SESSION_ACTIVE", php_session_active, CONST_CS | CONST_PERSISTENT);
2438
2439 return SUCCESS;
2440 }
2441 /* }}} */
2442
2443 static PHP_MSHUTDOWN_FUNCTION(session) /* {{{ */
2444 {
2445 UNREGISTER_INI_ENTRIES();
2446
2447 #ifdef HAVE_LIBMM
2448 PHP_MSHUTDOWN(ps_mm) (SHUTDOWN_FUNC_ARGS_PASSTHRU);
2449 #endif
2450
2451 /* reset rfc1867 callbacks */
2452 php_session_rfc1867_orig_callback = NULL;
2453 if (php_rfc1867_callback == php_session_rfc1867_callback) {
2454 php_rfc1867_callback = NULL;
2455 }
2456
2457 ps_serializers[PREDEFINED_SERIALIZERS].name = NULL;
2458 memset(&ps_modules[PREDEFINED_MODULES], 0, (MAX_MODULES-PREDEFINED_MODULES)*sizeof(ps_module *));
2459
2460 return SUCCESS;
2461 }
2462 /* }}} */
2463
2464 static PHP_MINFO_FUNCTION(session) /* {{{ */
2465 {
2466 ps_module **mod;
2467 ps_serializer *ser;
2468 smart_str save_handlers = {0};
2469 smart_str ser_handlers = {0};
2470 int i;
2471
2472 /* Get save handlers */
2473 for (i = 0, mod = ps_modules; i < MAX_MODULES; i++, mod++) {
2474 if (*mod && (*mod)->s_name) {
2475 smart_str_appends(&save_handlers, (*mod)->s_name);
2476 smart_str_appendc(&save_handlers, ' ');
2477 }
2478 }
2479
2480 /* Get serializer handlers */
2481 for (i = 0, ser = ps_serializers; i < MAX_SERIALIZERS; i++, ser++) {
2482 if (ser && ser->name) {
2483 smart_str_appends(&ser_handlers, ser->name);
2484 smart_str_appendc(&ser_handlers, ' ');
2485 }
2486 }
2487
2488 php_info_print_table_start();
2489 php_info_print_table_row(2, "Session Support", "enabled" );
2490
2491 if (save_handlers.c) {
2492 smart_str_0(&save_handlers);
2493 php_info_print_table_row(2, "Registered save handlers", save_handlers.c);
2494 smart_str_free(&save_handlers);
2495 } else {
2496 php_info_print_table_row(2, "Registered save handlers", "none");
2497 }
2498
2499 if (ser_handlers.c) {
2500 smart_str_0(&ser_handlers);
2501 php_info_print_table_row(2, "Registered serializer handlers", ser_handlers.c);
2502 smart_str_free(&ser_handlers);
2503 } else {
2504 php_info_print_table_row(2, "Registered serializer handlers", "none");
2505 }
2506
2507 php_info_print_table_end();
2508
2509 DISPLAY_INI_ENTRIES();
2510 }
2511 /* }}} */
2512
2513 static const zend_module_dep session_deps[] = { /* {{{ */
2514 ZEND_MOD_OPTIONAL("hash")
2515 ZEND_MOD_REQUIRED("spl")
2516 ZEND_MOD_END
2517 };
2518 /* }}} */
2519
2520 /* ************************
2521 * Upload hook handling *
2522 ************************ */
2523
2524 static zend_bool early_find_sid_in(zval *dest, int where, php_session_rfc1867_progress *progress TSRMLS_DC) /* {{{ */
2525 {
2526 zval **ppid;
2527
2528 if (!PG(http_globals)[where]) {
2529 return 0;
2530 }
2531
2532 if (zend_hash_find(Z_ARRVAL_P(PG(http_globals)[where]), PS(session_name), progress->sname_len+1, (void **)&ppid) == SUCCESS
2533 && Z_TYPE_PP(ppid) == IS_STRING) {
2534 zval_dtor(dest);
2535 ZVAL_ZVAL(dest, *ppid, 1, 0);
2536 return 1;
2537 }
2538
2539 return 0;
2540 } /* }}} */
2541
2542 static void php_session_rfc1867_early_find_sid(php_session_rfc1867_progress *progress TSRMLS_DC) /* {{{ */
2543 {
2544
2545 if (PS(use_cookies)) {
2546 sapi_module.treat_data(PARSE_COOKIE, NULL, NULL TSRMLS_CC);
2547 if (early_find_sid_in(&progress->sid, TRACK_VARS_COOKIE, progress TSRMLS_CC)) {
2548 progress->apply_trans_sid = 0;
2549 return;
2550 }
2551 }
2552 if (PS(use_only_cookies)) {
2553 return;
2554 }
2555 sapi_module.treat_data(PARSE_GET, NULL, NULL TSRMLS_CC);
2556 early_find_sid_in(&progress->sid, TRACK_VARS_GET, progress TSRMLS_CC);
2557 } /* }}} */
2558
2559 static zend_bool php_check_cancel_upload(php_session_rfc1867_progress *progress TSRMLS_DC) /* {{{ */
2560 {
2561 zval **progress_ary, **cancel_upload;
2562
2563 if (zend_symtable_find(Z_ARRVAL_P(PS(http_session_vars)), progress->key.c, progress->key.len+1, (void**)&progress_ary) != SUCCESS) {
2564 return 0;
2565 }
2566 if (Z_TYPE_PP(progress_ary) != IS_ARRAY) {
2567 return 0;
2568 }
2569 if (zend_hash_find(Z_ARRVAL_PP(progress_ary), "cancel_upload", sizeof("cancel_upload"), (void**)&cancel_upload) != SUCCESS) {
2570 return 0;
2571 }
2572 return Z_TYPE_PP(cancel_upload) == IS_BOOL && Z_LVAL_PP(cancel_upload);
2573 } /* }}} */
2574
2575 static void php_session_rfc1867_update(php_session_rfc1867_progress *progress, int force_update TSRMLS_DC) /* {{{ */
2576 {
2577 if (!force_update) {
2578 if (Z_LVAL_P(progress->post_bytes_processed) < progress->next_update) {
2579 return;
2580 }
2581 #ifdef HAVE_GETTIMEOFDAY
2582 if (PS(rfc1867_min_freq) > 0.0) {
2583 struct timeval tv = {0};
2584 double dtv;
2585 gettimeofday(&tv, NULL);
2586 dtv = (double) tv.tv_sec + tv.tv_usec / 1000000.0;
2587 if (dtv < progress->next_update_time) {
2588 return;
2589 }
2590 progress->next_update_time = dtv + PS(rfc1867_min_freq);
2591 }
2592 #endif
2593 progress->next_update = Z_LVAL_P(progress->post_bytes_processed) + progress->update_step;
2594 }
2595
2596 php_session_initialize(TSRMLS_C);
2597 PS(session_status) = php_session_active;
2598 IF_SESSION_VARS() {
2599 progress->cancel_upload |= php_check_cancel_upload(progress TSRMLS_CC);
2600 ZEND_SET_SYMBOL_WITH_LENGTH(Z_ARRVAL_P(PS(http_session_vars)), progress->key.c, progress->key.len+1, progress->data, 2, 0);
2601 }
2602 php_session_flush(TSRMLS_C);
2603 } /* }}} */
2604
2605 static void php_session_rfc1867_cleanup(php_session_rfc1867_progress *progress TSRMLS_DC) /* {{{ */
2606 {
2607 php_session_initialize(TSRMLS_C);
2608 PS(session_status) = php_session_active;
2609 IF_SESSION_VARS() {
2610 zend_hash_del(Z_ARRVAL_P(PS(http_session_vars)), progress->key.c, progress->key.len+1);
2611 }
2612 php_session_flush(TSRMLS_C);
2613 } /* }}} */
2614
2615 static int php_session_rfc1867_callback(unsigned int event, void *event_data, void **extra TSRMLS_DC) /* {{{ */
2616 {
2617 php_session_rfc1867_progress *progress;
2618 int retval = SUCCESS;
2619
2620 if (php_session_rfc1867_orig_callback) {
2621 retval = php_session_rfc1867_orig_callback(event, event_data, extra TSRMLS_CC);
2622 }
2623 if (!PS(rfc1867_enabled)) {
2624 return retval;
2625 }
2626
2627 progress = PS(rfc1867_progress);
2628
2629 switch(event) {
2630 case MULTIPART_EVENT_START: {
2631 multipart_event_start *data = (multipart_event_start *) event_data;
2632 progress = ecalloc(1, sizeof(php_session_rfc1867_progress));
2633 progress->content_length = data->content_length;
2634 progress->sname_len = strlen(PS(session_name));
2635 PS(rfc1867_progress) = progress;
2636 }
2637 break;
2638 case MULTIPART_EVENT_FORMDATA: {
2639 multipart_event_formdata *data = (multipart_event_formdata *) event_data;
2640 size_t value_len;
2641
2642 if (Z_TYPE(progress->sid) && progress->key.c) {
2643 break;
2644 }
2645
2646 /* orig callback may have modified *data->newlength */
2647 if (data->newlength) {
2648 value_len = *data->newlength;
2649 } else {
2650 value_len = data->length;
2651 }
2652
2653 if (data->name && data->value && value_len) {
2654 size_t name_len = strlen(data->name);
2655
2656 if (name_len == progress->sname_len && memcmp(data->name, PS(session_name), name_len) == 0) {
2657 zval_dtor(&progress->sid);
2658 ZVAL_STRINGL(&progress->sid, (*data->value), value_len, 1);
2659
2660 } else if (name_len == PS(rfc1867_name).len && memcmp(data->name, PS(rfc1867_name).c, name_len) == 0) {
2661 smart_str_free(&progress->key);
2662 smart_str_appendl(&progress->key, PS(rfc1867_prefix).c, PS(rfc1867_prefix).len);
2663 smart_str_appendl(&progress->key, *data->value, value_len);
2664 smart_str_0(&progress->key);
2665
2666 progress->apply_trans_sid = PS(use_trans_sid);
2667 php_session_rfc1867_early_find_sid(progress TSRMLS_CC);
2668 }
2669 }
2670 }
2671 break;
2672 case MULTIPART_EVENT_FILE_START: {
2673 multipart_event_file_start *data = (multipart_event_file_start *) event_data;
2674
2675 /* Do nothing when $_POST["PHP_SESSION_UPLOAD_PROGRESS"] is not set
2676 * or when we have no session id */
2677 if (!Z_TYPE(progress->sid) || !progress->key.c) {
2678 break;
2679 }
2680
2681 /* First FILE_START event, initializing data */
2682 if (!progress->data) {
2683
2684 if (PS(rfc1867_freq) >= 0) {
2685 progress->update_step = PS(rfc1867_freq);
2686 } else if (PS(rfc1867_freq) < 0) { /* % of total size */
2687 progress->update_step = progress->content_length * -PS(rfc1867_freq) / 100;
2688 }
2689 progress->next_update = 0;
2690 progress->next_update_time = 0.0;
2691
2692 ALLOC_INIT_ZVAL(progress->data);
2693 array_init(progress->data);
2694
2695 ALLOC_INIT_ZVAL(progress->post_bytes_processed);
2696 ZVAL_LONG(progress->post_bytes_processed, data->post_bytes_processed);
2697
2698 ALLOC_INIT_ZVAL(progress->files);
2699 array_init(progress->files);
2700
2701 add_assoc_long_ex(progress->data, "start_time", sizeof("start_time"), (long)sapi_get_request_time(TSRMLS_C));
2702 add_assoc_long_ex(progress->data, "content_length", sizeof("content_length"), progress->content_length);
2703 add_assoc_zval_ex(progress->data, "bytes_processed", sizeof("bytes_processed"), progress->post_bytes_processed);
2704 add_assoc_bool_ex(progress->data, "done", sizeof("done"), 0);
2705 add_assoc_zval_ex(progress->data, "files", sizeof("files"), progress->files);
2706
2707 php_rinit_session(0 TSRMLS_CC);
2708 PS(id) = estrndup(Z_STRVAL(progress->sid), Z_STRLEN(progress->sid));
2709 PS(apply_trans_sid) = progress->apply_trans_sid;
2710 PS(send_cookie) = 0;
2711 }
2712
2713 ALLOC_INIT_ZVAL(progress->current_file);
2714 array_init(progress->current_file);
2715
2716 ALLOC_INIT_ZVAL(progress->current_file_bytes_processed);
2717 ZVAL_LONG(progress->current_file_bytes_processed, 0);
2718
2719 /* Each uploaded file has its own array. Trying to make it close to $_FILES entries. */
2720 add_assoc_string_ex(progress->current_file, "field_name", sizeof("field_name"), data->name, 1);
2721 add_assoc_string_ex(progress->current_file, "name", sizeof("name"), *data->filename, 1);
2722 add_assoc_null_ex(progress->current_file, "tmp_name", sizeof("tmp_name"));
2723 add_assoc_long_ex(progress->current_file, "error", sizeof("error"), 0);
2724
2725 add_assoc_bool_ex(progress->current_file, "done", sizeof("done"), 0);
2726 add_assoc_long_ex(progress->current_file, "start_time", sizeof("start_time"), (long)time(NULL));
2727 add_assoc_zval_ex(progress->current_file, "bytes_processed", sizeof("bytes_processed"), progress->current_file_bytes_processed);
2728
2729 add_next_index_zval(progress->files, progress->current_file);
2730
2731 Z_LVAL_P(progress->post_bytes_processed) = data->post_bytes_processed;
2732
2733 php_session_rfc1867_update(progress, 0 TSRMLS_CC);
2734 }
2735 break;
2736 case MULTIPART_EVENT_FILE_DATA: {
2737 multipart_event_file_data *data = (multipart_event_file_data *) event_data;
2738
2739 if (!Z_TYPE(progress->sid) || !progress->key.c) {
2740 break;
2741 }
2742
2743 Z_LVAL_P(progress->current_file_bytes_processed) = data->offset + data->length;
2744 Z_LVAL_P(progress->post_bytes_processed) = data->post_bytes_processed;
2745
2746 php_session_rfc1867_update(progress, 0 TSRMLS_CC);
2747 }
2748 break;
2749 case MULTIPART_EVENT_FILE_END: {
2750 multipart_event_file_end *data = (multipart_event_file_end *) event_data;
2751
2752 if (!Z_TYPE(progress->sid) || !progress->key.c) {
2753 break;
2754 }
2755
2756 if (data->temp_filename) {
2757 add_assoc_string_ex(progress->current_file, "tmp_name", sizeof("tmp_name"), data->temp_filename, 1);
2758 }
2759 add_assoc_long_ex(progress->current_file, "error", sizeof("error"), data->cancel_upload);
2760 add_assoc_bool_ex(progress->current_file, "done", sizeof("done"), 1);
2761
2762 Z_LVAL_P(progress->post_bytes_processed) = data->post_bytes_processed;
2763
2764 php_session_rfc1867_update(progress, 0 TSRMLS_CC);
2765 }
2766 break;
2767 case MULTIPART_EVENT_END: {
2768 multipart_event_end *data = (multipart_event_end *) event_data;
2769
2770 if (Z_TYPE(progress->sid) && progress->key.c) {
2771 if (PS(rfc1867_cleanup)) {
2772 php_session_rfc1867_cleanup(progress TSRMLS_CC);
2773 } else {
2774 add_assoc_bool_ex(progress->data, "done", sizeof("done"), 1);
2775 Z_LVAL_P(progress->post_bytes_processed) = data->post_bytes_processed;
2776 php_session_rfc1867_update(progress, 1 TSRMLS_CC);
2777 }
2778 php_rshutdown_session_globals(TSRMLS_C);
2779 }
2780
2781 if (progress->data) {
2782 zval_ptr_dtor(&progress->data);
2783 }
2784 zval_dtor(&progress->sid);
2785 smart_str_free(&progress->key);
2786 efree(progress);
2787 progress = NULL;
2788 PS(rfc1867_progress) = NULL;
2789 }
2790 break;
2791 }
2792
2793 if (progress && progress->cancel_upload) {
2794 return FAILURE;
2795 }
2796 return retval;
2797
2798 } /* }}} */
2799
2800 zend_module_entry session_module_entry = {
2801 STANDARD_MODULE_HEADER_EX,
2802 NULL,
2803 session_deps,
2804 "session",
2805 session_functions,
2806 PHP_MINIT(session), PHP_MSHUTDOWN(session),
2807 PHP_RINIT(session), PHP_RSHUTDOWN(session),
2808 PHP_MINFO(session),
2809 NO_VERSION_YET,
2810 PHP_MODULE_GLOBALS(ps),
2811 PHP_GINIT(ps),
2812 NULL,
2813 NULL,
2814 STANDARD_MODULE_PROPERTIES_EX
2815 };
2816
2817 #ifdef COMPILE_DL_SESSION
2818 ZEND_GET_MODULE(session)
2819 #endif
2820
2821 /*
2822 * Local variables:
2823 * tab-width: 4
2824 * c-basic-offset: 4
2825 * End:
2826 * vim600: noet sw=4 ts=4 fdm=marker
2827 * vim<600: sw=4 ts=4
2828 */
2829