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 */
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 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
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 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
PHP_INI_MH(OnUpdateRfc1867Freq)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
ZEND_INI_MH(OnUpdateSmartStr)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) */
PHP_INI_END()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
PS_SERIALIZER_DECODE_FUNC(php_serialize)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
PS_SERIALIZER_ENCODE_FUNC(php_binary)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 int skip = 0;
928
929 PHP_VAR_UNSERIALIZE_INIT(var_hash);
930
931 for (p = val; p < endptr; ) {
932 zval **tmp;
933 skip = 0;
934 namelen = ((unsigned char)(*p)) & (~PS_BIN_UNDEF);
935
936 if (namelen < 0 || namelen > PS_BIN_MAX || (p + namelen) >= endptr) {
937 PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
938 return FAILURE;
939 }
940
941 has_value = *p & PS_BIN_UNDEF ? 0 : 1;
942
943 name = estrndup(p + 1, namelen);
944
945 p += namelen + 1;
946
947 if (zend_hash_find(&EG(symbol_table), name, namelen + 1, (void **) &tmp) == SUCCESS) {
948 if ((Z_TYPE_PP(tmp) == IS_ARRAY && Z_ARRVAL_PP(tmp) == &EG(symbol_table)) || *tmp == PS(http_session_vars)) {
949 skip = 1;
950 }
951 }
952
953 if (has_value) {
954 ALLOC_INIT_ZVAL(current);
955 if (php_var_unserialize(¤t, (const unsigned char **) &p, (const unsigned char *) endptr, &var_hash TSRMLS_CC)) {
956 if (!skip) {
957 php_set_session_var(name, namelen, current, &var_hash TSRMLS_CC);
958 }
959 } else {
960 PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
961 return FAILURE;
962 }
963 var_push_dtor_no_addref(&var_hash, ¤t);
964 }
965 if (!skip) {
966 PS_ADD_VARL(name, namelen);
967 }
968 efree(name);
969 }
970
971 PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
972
973 return SUCCESS;
974 }
975 /* }}} */
976
977 #define PS_DELIMITER '|'
978 #define PS_UNDEF_MARKER '!'
979
980 PS_SERIALIZER_ENCODE_FUNC(php) /* {{{ */
981 {
982 smart_str buf = {0};
983 php_serialize_data_t var_hash;
984 PS_ENCODE_VARS;
985
986 PHP_VAR_SERIALIZE_INIT(var_hash);
987
988 PS_ENCODE_LOOP(
989 smart_str_appendl(&buf, key, key_length);
990 if (memchr(key, PS_DELIMITER, key_length) || memchr(key, PS_UNDEF_MARKER, key_length)) {
991 PHP_VAR_SERIALIZE_DESTROY(var_hash);
992 smart_str_free(&buf);
993 return FAILURE;
994 }
995 smart_str_appendc(&buf, PS_DELIMITER);
996
997 php_var_serialize(&buf, struc, &var_hash TSRMLS_CC);
998 } else {
999 smart_str_appendc(&buf, PS_UNDEF_MARKER);
1000 smart_str_appendl(&buf, key, key_length);
1001 smart_str_appendc(&buf, PS_DELIMITER);
1002 );
1003
1004 if (newlen) {
1005 *newlen = buf.len;
1006 }
1007 smart_str_0(&buf);
1008 *newstr = buf.c;
1009
1010 PHP_VAR_SERIALIZE_DESTROY(var_hash);
1011 return SUCCESS;
1012 }
1013 /* }}} */
1014
1015 PS_SERIALIZER_DECODE_FUNC(php) /* {{{ */
1016 {
1017 const char *p, *q;
1018 char *name;
1019 const char *endptr = val + vallen;
1020 zval *current;
1021 int namelen;
1022 int has_value;
1023 php_unserialize_data_t var_hash;
1024 int skip = 0;
1025
1026 PHP_VAR_UNSERIALIZE_INIT(var_hash);
1027
1028 p = val;
1029
1030 while (p < endptr) {
1031 zval **tmp;
1032 q = p;
1033 skip = 0;
1034 while (*q != PS_DELIMITER) {
1035 if (++q >= endptr) goto break_outer_loop;
1036 }
1037 if (p[0] == PS_UNDEF_MARKER) {
1038 p++;
1039 has_value = 0;
1040 } else {
1041 has_value = 1;
1042 }
1043
1044 namelen = q - p;
1045 name = estrndup(p, namelen);
1046 q++;
1047
1048 if (zend_hash_find(&EG(symbol_table), name, namelen + 1, (void **) &tmp) == SUCCESS) {
1049 if ((Z_TYPE_PP(tmp) == IS_ARRAY && Z_ARRVAL_PP(tmp) == &EG(symbol_table)) || *tmp == PS(http_session_vars)) {
1050 skip = 1;
1051 }
1052 }
1053
1054 if (has_value) {
1055 ALLOC_INIT_ZVAL(current);
1056 if (php_var_unserialize(¤t, (const unsigned char **) &q, (const unsigned char *) endptr, &var_hash TSRMLS_CC)) {
1057 if (!skip) {
1058 php_set_session_var(name, namelen, current, &var_hash TSRMLS_CC);
1059 }
1060 } else {
1061 var_push_dtor_no_addref(&var_hash, ¤t);
1062 efree(name);
1063 PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
1064 return FAILURE;
1065 }
1066 var_push_dtor_no_addref(&var_hash, ¤t);
1067 }
1068 if (!skip) {
1069 PS_ADD_VARL(name, namelen);
1070 }
1071 skip:
1072 efree(name);
1073
1074 p = q;
1075 }
1076 break_outer_loop:
1077
1078 PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
1079
1080 return SUCCESS;
1081 }
1082 /* }}} */
1083
1084 #define MAX_SERIALIZERS 32
1085 #define PREDEFINED_SERIALIZERS 3
1086
1087 static ps_serializer ps_serializers[MAX_SERIALIZERS + 1] = {
1088 PS_SERIALIZER_ENTRY(php_serialize),
1089 PS_SERIALIZER_ENTRY(php),
1090 PS_SERIALIZER_ENTRY(php_binary)
1091 };
1092
1093 PHPAPI int php_session_register_serializer(const char *name, int (*encode)(PS_SERIALIZER_ENCODE_ARGS), int (*decode)(PS_SERIALIZER_DECODE_ARGS)) /* {{{ */
1094 {
1095 int ret = -1;
1096 int i;
1097
1098 for (i = 0; i < MAX_SERIALIZERS; i++) {
1099 if (ps_serializers[i].name == NULL) {
1100 ps_serializers[i].name = name;
1101 ps_serializers[i].encode = encode;
1102 ps_serializers[i].decode = decode;
1103 ps_serializers[i + 1].name = NULL;
1104 ret = 0;
1105 break;
1106 }
1107 }
1108 return ret;
1109 }
1110 /* }}} */
1111
1112 /* *******************
1113 * Storage Modules *
1114 ******************* */
1115
1116 #define MAX_MODULES 10
1117 #define PREDEFINED_MODULES 2
1118
1119 static ps_module *ps_modules[MAX_MODULES + 1] = {
1120 ps_files_ptr,
1121 ps_user_ptr
1122 };
1123
1124 PHPAPI int php_session_register_module(ps_module *ptr) /* {{{ */
1125 {
1126 int ret = -1;
1127 int i;
1128
1129 for (i = 0; i < MAX_MODULES; i++) {
1130 if (!ps_modules[i]) {
1131 ps_modules[i] = ptr;
1132 ret = 0;
1133 break;
1134 }
1135 }
1136 return ret;
1137 }
1138 /* }}} */
1139
1140 /* ******************
1141 * Cache Limiters *
1142 ****************** */
1143
1144 typedef struct {
1145 char *name;
1146 void (*func)(TSRMLS_D);
1147 } php_session_cache_limiter_t;
1148
1149 #define CACHE_LIMITER(name) _php_cache_limiter_##name
1150 #define CACHE_LIMITER_FUNC(name) static void CACHE_LIMITER(name)(TSRMLS_D)
1151 #define CACHE_LIMITER_ENTRY(name) { #name, CACHE_LIMITER(name) },
1152 #define ADD_HEADER(a) sapi_add_header(a, strlen(a), 1);
1153 #define MAX_STR 512
1154
1155 static char *month_names[] = {
1156 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1157 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
1158 };
1159
1160 static char *week_days[] = {
1161 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
1162 };
1163
1164 static inline void strcpy_gmt(char *ubuf, time_t *when) /* {{{ */
1165 {
1166 char buf[MAX_STR];
1167 struct tm tm, *res;
1168 int n;
1169
1170 res = php_gmtime_r(when, &tm);
1171
1172 if (!res) {
1173 ubuf[0] = '\0';
1174 return;
1175 }
1176
1177 n = slprintf(buf, sizeof(buf), "%s, %02d %s %d %02d:%02d:%02d GMT", /* SAFE */
1178 week_days[tm.tm_wday], tm.tm_mday,
1179 month_names[tm.tm_mon], tm.tm_year + 1900,
1180 tm.tm_hour, tm.tm_min,
1181 tm.tm_sec);
1182 memcpy(ubuf, buf, n);
1183 ubuf[n] = '\0';
1184 }
1185 /* }}} */
1186
1187 static inline void last_modified(TSRMLS_D) /* {{{ */
1188 {
1189 const char *path;
1190 struct stat sb;
1191 char buf[MAX_STR + 1];
1192
1193 path = SG(request_info).path_translated;
1194 if (path) {
1195 if (VCWD_STAT(path, &sb) == -1) {
1196 return;
1197 }
1198
1199 #define LAST_MODIFIED "Last-Modified: "
1200 memcpy(buf, LAST_MODIFIED, sizeof(LAST_MODIFIED) - 1);
1201 strcpy_gmt(buf + sizeof(LAST_MODIFIED) - 1, &sb.st_mtime);
1202 ADD_HEADER(buf);
1203 }
1204 }
1205 /* }}} */
1206
1207 #define EXPIRES "Expires: "
1208 CACHE_LIMITER_FUNC(public) /* {{{ */
1209 {
1210 char buf[MAX_STR + 1];
1211 struct timeval tv;
1212 time_t now;
1213
1214 gettimeofday(&tv, NULL);
1215 now = tv.tv_sec + PS(cache_expire) * 60;
1216 memcpy(buf, EXPIRES, sizeof(EXPIRES) - 1);
1217 strcpy_gmt(buf + sizeof(EXPIRES) - 1, &now);
1218 ADD_HEADER(buf);
1219
1220 snprintf(buf, sizeof(buf) , "Cache-Control: public, max-age=%ld", PS(cache_expire) * 60); /* SAFE */
1221 ADD_HEADER(buf);
1222
1223 last_modified(TSRMLS_C);
1224 }
1225 /* }}} */
1226
1227 CACHE_LIMITER_FUNC(private_no_expire) /* {{{ */
1228 {
1229 char buf[MAX_STR + 1];
1230
1231 snprintf(buf, sizeof(buf), "Cache-Control: private, max-age=%ld, pre-check=%ld", PS(cache_expire) * 60, PS(cache_expire) * 60); /* SAFE */
1232 ADD_HEADER(buf);
1233
1234 last_modified(TSRMLS_C);
1235 }
1236 /* }}} */
1237
1238 CACHE_LIMITER_FUNC(private) /* {{{ */
1239 {
1240 ADD_HEADER("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
1241 CACHE_LIMITER(private_no_expire)(TSRMLS_C);
1242 }
1243 /* }}} */
1244
1245 CACHE_LIMITER_FUNC(nocache) /* {{{ */
1246 {
1247 ADD_HEADER("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
1248
1249 /* For HTTP/1.1 conforming clients and the rest (MSIE 5) */
1250 ADD_HEADER("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
1251
1252 /* For HTTP/1.0 conforming clients */
1253 ADD_HEADER("Pragma: no-cache");
1254 }
1255 /* }}} */
1256
1257 static php_session_cache_limiter_t php_session_cache_limiters[] = {
1258 CACHE_LIMITER_ENTRY(public)
1259 CACHE_LIMITER_ENTRY(private)
1260 CACHE_LIMITER_ENTRY(private_no_expire)
1261 CACHE_LIMITER_ENTRY(nocache)
1262 {0}
1263 };
1264
1265 static int php_session_cache_limiter(TSRMLS_D) /* {{{ */
1266 {
1267 php_session_cache_limiter_t *lim;
1268
1269 if (PS(cache_limiter)[0] == '\0') return 0;
1270
1271 if (SG(headers_sent)) {
1272 const char *output_start_filename = php_output_get_start_filename(TSRMLS_C);
1273 int output_start_lineno = php_output_get_start_lineno(TSRMLS_C);
1274
1275 if (output_start_filename) {
1276 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);
1277 } else {
1278 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot send session cache limiter - headers already sent");
1279 }
1280 return -2;
1281 }
1282
1283 for (lim = php_session_cache_limiters; lim->name; lim++) {
1284 if (!strcasecmp(lim->name, PS(cache_limiter))) {
1285 lim->func(TSRMLS_C);
1286 return 0;
1287 }
1288 }
1289
1290 return -1;
1291 }
1292 /* }}} */
1293
1294 /* *********************
1295 * Cookie Management *
1296 ********************* */
1297
1298 #define COOKIE_SET_COOKIE "Set-Cookie: "
1299 #define COOKIE_EXPIRES "; expires="
1300 #define COOKIE_MAX_AGE "; Max-Age="
1301 #define COOKIE_PATH "; path="
1302 #define COOKIE_DOMAIN "; domain="
1303 #define COOKIE_SECURE "; secure"
1304 #define COOKIE_HTTPONLY "; HttpOnly"
1305
1306 /*
1307 * Remove already sent session ID cookie.
1308 * It must be directly removed from SG(sapi_header) because sapi_add_header_ex()
1309 * removes all of matching cookie. i.e. It deletes all of Set-Cookie headers.
1310 */
1311 static void php_session_remove_cookie(TSRMLS_D) {
1312 sapi_header_struct *header;
1313 zend_llist *l = &SG(sapi_headers).headers;
1314 zend_llist_element *next;
1315 zend_llist_element *current;
1316 char *session_cookie, *e_session_name;
1317 int session_cookie_len, len = sizeof("Set-Cookie")-1;
1318
1319 e_session_name = php_url_encode(PS(session_name), strlen(PS(session_name)), NULL);
1320 spprintf(&session_cookie, 0, "Set-Cookie: %s=", e_session_name);
1321 efree(e_session_name);
1322
1323 session_cookie_len = strlen(session_cookie);
1324 current = l->head;
1325 while (current) {
1326 header = (sapi_header_struct *)(current->data);
1327 next = current->next;
1328 if (header->header_len > len && header->header[len] == ':'
1329 && !strncmp(header->header, session_cookie, session_cookie_len)) {
1330 if (current->prev) {
1331 current->prev->next = next;
1332 } else {
1333 l->head = next;
1334 }
1335 if (next) {
1336 next->prev = current->prev;
1337 } else {
1338 l->tail = current->prev;
1339 }
1340 sapi_free_header(header);
1341 efree(current);
1342 --l->count;
1343 }
1344 current = next;
1345 }
1346 efree(session_cookie);
1347 }
1348
1349 static void php_session_send_cookie(TSRMLS_D) /* {{{ */
1350 {
1351 smart_str ncookie = {0};
1352 char *date_fmt = NULL;
1353 char *e_session_name, *e_id;
1354
1355 if (SG(headers_sent)) {
1356 const char *output_start_filename = php_output_get_start_filename(TSRMLS_C);
1357 int output_start_lineno = php_output_get_start_lineno(TSRMLS_C);
1358
1359 if (output_start_filename) {
1360 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);
1361 } else {
1362 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot send session cookie - headers already sent");
1363 }
1364 return;
1365 }
1366
1367 /* URL encode session_name and id because they might be user supplied */
1368 e_session_name = php_url_encode(PS(session_name), strlen(PS(session_name)), NULL);
1369 e_id = php_url_encode(PS(id), strlen(PS(id)), NULL);
1370
1371 smart_str_appends(&ncookie, COOKIE_SET_COOKIE);
1372 smart_str_appends(&ncookie, e_session_name);
1373 smart_str_appendc(&ncookie, '=');
1374 smart_str_appends(&ncookie, e_id);
1375
1376 efree(e_session_name);
1377 efree(e_id);
1378
1379 if (PS(cookie_lifetime) > 0) {
1380 struct timeval tv;
1381 time_t t;
1382
1383 gettimeofday(&tv, NULL);
1384 t = tv.tv_sec + PS(cookie_lifetime);
1385
1386 if (t > 0) {
1387 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);
1388 smart_str_appends(&ncookie, COOKIE_EXPIRES);
1389 smart_str_appends(&ncookie, date_fmt);
1390 efree(date_fmt);
1391
1392 smart_str_appends(&ncookie, COOKIE_MAX_AGE);
1393 smart_str_append_long(&ncookie, PS(cookie_lifetime));
1394 }
1395 }
1396
1397 if (PS(cookie_path)[0]) {
1398 smart_str_appends(&ncookie, COOKIE_PATH);
1399 smart_str_appends(&ncookie, PS(cookie_path));
1400 }
1401
1402 if (PS(cookie_domain)[0]) {
1403 smart_str_appends(&ncookie, COOKIE_DOMAIN);
1404 smart_str_appends(&ncookie, PS(cookie_domain));
1405 }
1406
1407 if (PS(cookie_secure)) {
1408 smart_str_appends(&ncookie, COOKIE_SECURE);
1409 }
1410
1411 if (PS(cookie_httponly)) {
1412 smart_str_appends(&ncookie, COOKIE_HTTPONLY);
1413 }
1414
1415 smart_str_0(&ncookie);
1416
1417 php_session_remove_cookie(TSRMLS_C); /* remove already sent session ID cookie */
1418 sapi_add_header_ex(ncookie.c, ncookie.len, 0, 0 TSRMLS_CC);
1419 }
1420 /* }}} */
1421
1422 PHPAPI ps_module *_php_find_ps_module(char *name TSRMLS_DC) /* {{{ */
1423 {
1424 ps_module *ret = NULL;
1425 ps_module **mod;
1426 int i;
1427
1428 for (i = 0, mod = ps_modules; i < MAX_MODULES; i++, mod++) {
1429 if (*mod && !strcasecmp(name, (*mod)->s_name)) {
1430 ret = *mod;
1431 break;
1432 }
1433 }
1434 return ret;
1435 }
1436 /* }}} */
1437
1438 PHPAPI const ps_serializer *_php_find_ps_serializer(char *name TSRMLS_DC) /* {{{ */
1439 {
1440 const ps_serializer *ret = NULL;
1441 const ps_serializer *mod;
1442
1443 for (mod = ps_serializers; mod->name; mod++) {
1444 if (!strcasecmp(name, mod->name)) {
1445 ret = mod;
1446 break;
1447 }
1448 }
1449 return ret;
1450 }
1451 /* }}} */
1452
1453 static void ppid2sid(zval **ppid TSRMLS_DC) {
1454 if (Z_TYPE_PP(ppid) != IS_STRING) {
1455 PS(id) = NULL;
1456 PS(send_cookie) = 1;
1457 } else {
1458 convert_to_string((*ppid));
1459 PS(id) = estrndup(Z_STRVAL_PP(ppid), Z_STRLEN_PP(ppid));
1460 PS(send_cookie) = 0;
1461 }
1462 }
1463
1464 PHPAPI void php_session_reset_id(TSRMLS_D) /* {{{ */
1465 {
1466 int module_number = PS(module_number);
1467
1468 if (!PS(id)) {
1469 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot set session ID - session ID is not initialized");
1470 return;
1471 }
1472
1473 if (PS(use_cookies) && PS(send_cookie)) {
1474 php_session_send_cookie(TSRMLS_C);
1475 PS(send_cookie) = 0;
1476 }
1477
1478 /* if the SID constant exists, destroy it. */
1479 zend_hash_del(EG(zend_constants), "sid", sizeof("sid"));
1480
1481 if (PS(define_sid)) {
1482 smart_str var = {0};
1483
1484 smart_str_appends(&var, PS(session_name));
1485 smart_str_appendc(&var, '=');
1486 smart_str_appends(&var, PS(id));
1487 smart_str_0(&var);
1488 REGISTER_STRINGL_CONSTANT("SID", var.c, var.len, 0);
1489 } else {
1490 REGISTER_STRINGL_CONSTANT("SID", STR_EMPTY_ALLOC(), 0, 0);
1491 }
1492
1493 if (PS(apply_trans_sid)) {
1494 php_url_scanner_reset_vars(TSRMLS_C);
1495 php_url_scanner_add_var(PS(session_name), strlen(PS(session_name)), PS(id), strlen(PS(id)), 1 TSRMLS_CC);
1496 }
1497 }
1498 /* }}} */
1499
1500 PHPAPI void php_session_start(TSRMLS_D) /* {{{ */
1501 {
1502 zval **ppid;
1503 zval **data;
1504 char *p, *value;
1505 int nrand;
1506 int lensess;
1507
1508 if (PS(use_only_cookies)) {
1509 PS(apply_trans_sid) = 0;
1510 } else {
1511 PS(apply_trans_sid) = PS(use_trans_sid);
1512 }
1513
1514 switch (PS(session_status)) {
1515 case php_session_active:
1516 php_error(E_NOTICE, "A session had already been started - ignoring session_start()");
1517 return;
1518 break;
1519
1520 case php_session_disabled:
1521 value = zend_ini_string("session.save_handler", sizeof("session.save_handler"), 0);
1522 if (!PS(mod) && value) {
1523 PS(mod) = _php_find_ps_module(value TSRMLS_CC);
1524 if (!PS(mod)) {
1525 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot find save handler '%s' - session startup failed", value);
1526 return;
1527 }
1528 }
1529 value = zend_ini_string("session.serialize_handler", sizeof("session.serialize_handler"), 0);
1530 if (!PS(serializer) && value) {
1531 PS(serializer) = _php_find_ps_serializer(value TSRMLS_CC);
1532 if (!PS(serializer)) {
1533 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot find serialization handler '%s' - session startup failed", value);
1534 return;
1535 }
1536 }
1537 PS(session_status) = php_session_none;
1538 /* fallthrough */
1539
1540 default:
1541 case php_session_none:
1542 PS(define_sid) = 1;
1543 PS(send_cookie) = 1;
1544 }
1545
1546 lensess = strlen(PS(session_name));
1547
1548 /* Cookies are preferred, because initially
1549 * cookie and get variables will be available. */
1550
1551 if (!PS(id)) {
1552 if (PS(use_cookies) && zend_hash_find(&EG(symbol_table), "_COOKIE", sizeof("_COOKIE"), (void **) &data) == SUCCESS &&
1553 Z_TYPE_PP(data) == IS_ARRAY &&
1554 zend_hash_find(Z_ARRVAL_PP(data), PS(session_name), lensess + 1, (void **) &ppid) == SUCCESS
1555 ) {
1556 ppid2sid(ppid TSRMLS_CC);
1557 PS(apply_trans_sid) = 0;
1558 PS(define_sid) = 0;
1559 }
1560
1561 if (!PS(use_only_cookies) && !PS(id) &&
1562 zend_hash_find(&EG(symbol_table), "_GET", sizeof("_GET"), (void **) &data) == SUCCESS &&
1563 Z_TYPE_PP(data) == IS_ARRAY &&
1564 zend_hash_find(Z_ARRVAL_PP(data), PS(session_name), lensess + 1, (void **) &ppid) == SUCCESS
1565 ) {
1566 ppid2sid(ppid TSRMLS_CC);
1567 }
1568
1569 if (!PS(use_only_cookies) && !PS(id) &&
1570 zend_hash_find(&EG(symbol_table), "_POST", sizeof("_POST"), (void **) &data) == SUCCESS &&
1571 Z_TYPE_PP(data) == IS_ARRAY &&
1572 zend_hash_find(Z_ARRVAL_PP(data), PS(session_name), lensess + 1, (void **) &ppid) == SUCCESS
1573 ) {
1574 ppid2sid(ppid TSRMLS_CC);
1575 }
1576 }
1577
1578 /* Check the REQUEST_URI symbol for a string of the form
1579 * '<session-name>=<session-id>' to allow URLs of the form
1580 * http://yoursite/<session-name>=<session-id>/script.php */
1581
1582 if (!PS(use_only_cookies) && !PS(id) && PG(http_globals)[TRACK_VARS_SERVER] &&
1583 zend_hash_find(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]), "REQUEST_URI", sizeof("REQUEST_URI"), (void **) &data) == SUCCESS &&
1584 Z_TYPE_PP(data) == IS_STRING &&
1585 (p = strstr(Z_STRVAL_PP(data), PS(session_name))) &&
1586 p[lensess] == '='
1587 ) {
1588 char *q;
1589
1590 p += lensess + 1;
1591 if ((q = strpbrk(p, "/?\\"))) {
1592 PS(id) = estrndup(p, q - p);
1593 PS(send_cookie) = 0;
1594 }
1595 }
1596
1597 /* Check whether the current request was referred to by
1598 * an external site which invalidates the previously found id. */
1599
1600 if (PS(id) &&
1601 PS(extern_referer_chk)[0] != '\0' &&
1602 PG(http_globals)[TRACK_VARS_SERVER] &&
1603 zend_hash_find(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]), "HTTP_REFERER", sizeof("HTTP_REFERER"), (void **) &data) == SUCCESS &&
1604 Z_TYPE_PP(data) == IS_STRING &&
1605 Z_STRLEN_PP(data) != 0 &&
1606 strstr(Z_STRVAL_PP(data), PS(extern_referer_chk)) == NULL
1607 ) {
1608 efree(PS(id));
1609 PS(id) = NULL;
1610 PS(send_cookie) = 1;
1611 if (PS(use_trans_sid) && !PS(use_only_cookies)) {
1612 PS(apply_trans_sid) = 1;
1613 }
1614 }
1615
1616 /* Finally check session id for dangarous characters
1617 * Security note: session id may be embedded in HTML pages.*/
1618 if (PS(id) && strpbrk(PS(id), "\r\n\t <>'\"\\")) {
1619 efree(PS(id));
1620 PS(id) = NULL;
1621 }
1622
1623 php_session_initialize(TSRMLS_C);
1624 php_session_cache_limiter(TSRMLS_C);
1625
1626 if ((PS(mod_data) || PS(mod_user_implemented)) && PS(gc_probability) > 0) {
1627 int nrdels = -1;
1628
1629 nrand = (int) ((float) PS(gc_divisor) * php_combined_lcg(TSRMLS_C));
1630 if (nrand < PS(gc_probability)) {
1631 PS(mod)->s_gc(&PS(mod_data), PS(gc_maxlifetime), &nrdels TSRMLS_CC);
1632 #ifdef SESSION_DEBUG
1633 if (nrdels != -1) {
1634 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "purged %d expired session objects", nrdels);
1635 }
1636 #endif
1637 }
1638 }
1639 }
1640 /* }}} */
1641
1642 static void php_session_flush(TSRMLS_D) /* {{{ */
1643 {
1644 if (PS(session_status) == php_session_active) {
1645 PS(session_status) = php_session_none;
1646 php_session_save_current_state(TSRMLS_C);
1647 }
1648 }
1649 /* }}} */
1650
1651 static void php_session_abort(TSRMLS_D) /* {{{ */
1652 {
1653 if (PS(session_status) == php_session_active) {
1654 PS(session_status) = php_session_none;
1655 if (PS(mod_data) || PS(mod_user_implemented)) {
1656 PS(mod)->s_close(&PS(mod_data) TSRMLS_CC);
1657 }
1658 }
1659 }
1660 /* }}} */
1661
1662 static void php_session_reset(TSRMLS_D) /* {{{ */
1663 {
1664 if (PS(session_status) == php_session_active) {
1665 php_session_initialize(TSRMLS_C);
1666 }
1667 }
1668 /* }}} */
1669
1670
1671 PHPAPI void session_adapt_url(const char *url, size_t urllen, char **new, size_t *newlen TSRMLS_DC) /* {{{ */
1672 {
1673 if (PS(apply_trans_sid) && (PS(session_status) == php_session_active)) {
1674 *new = php_url_scanner_adapt_single_url(url, urllen, PS(session_name), PS(id), newlen TSRMLS_CC);
1675 }
1676 }
1677 /* }}} */
1678
1679 /* ********************************
1680 * Userspace exported functions *
1681 ******************************** */
1682
1683 /* {{{ proto void session_set_cookie_params(int lifetime [, string path [, string domain [, bool secure[, bool httponly]]]])
1684 Set session cookie parameters */
1685 static PHP_FUNCTION(session_set_cookie_params)
1686 {
1687 zval **lifetime = NULL;
1688 char *path = NULL, *domain = NULL;
1689 int path_len, domain_len, argc = ZEND_NUM_ARGS();
1690 zend_bool secure = 0, httponly = 0;
1691
1692 if (!PS(use_cookies) ||
1693 zend_parse_parameters(argc TSRMLS_CC, "Z|ssbb", &lifetime, &path, &path_len, &domain, &domain_len, &secure, &httponly) == FAILURE) {
1694 return;
1695 }
1696
1697 convert_to_string_ex(lifetime);
1698
1699 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);
1700
1701 if (path) {
1702 zend_alter_ini_entry("session.cookie_path", sizeof("session.cookie_path"), path, path_len, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
1703 }
1704 if (domain) {
1705 zend_alter_ini_entry("session.cookie_domain", sizeof("session.cookie_domain"), domain, domain_len, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
1706 }
1707
1708 if (argc > 3) {
1709 zend_alter_ini_entry("session.cookie_secure", sizeof("session.cookie_secure"), secure ? "1" : "0", 1, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
1710 }
1711 if (argc > 4) {
1712 zend_alter_ini_entry("session.cookie_httponly", sizeof("session.cookie_httponly"), httponly ? "1" : "0", 1, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
1713 }
1714 }
1715 /* }}} */
1716
1717 /* {{{ proto array session_get_cookie_params(void)
1718 Return the session cookie parameters */
1719 static PHP_FUNCTION(session_get_cookie_params)
1720 {
1721 if (zend_parse_parameters_none() == FAILURE) {
1722 return;
1723 }
1724
1725 array_init(return_value);
1726
1727 add_assoc_long(return_value, "lifetime", PS(cookie_lifetime));
1728 add_assoc_string(return_value, "path", PS(cookie_path), 1);
1729 add_assoc_string(return_value, "domain", PS(cookie_domain), 1);
1730 add_assoc_bool(return_value, "secure", PS(cookie_secure));
1731 add_assoc_bool(return_value, "httponly", PS(cookie_httponly));
1732 }
1733 /* }}} */
1734
1735 /* {{{ proto string session_name([string newname])
1736 Return the current session name. If newname is given, the session name is replaced with newname */
1737 static PHP_FUNCTION(session_name)
1738 {
1739 char *name = NULL;
1740 int name_len;
1741
1742 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &name, &name_len) == FAILURE) {
1743 return;
1744 }
1745
1746 RETVAL_STRING(PS(session_name), 1);
1747
1748 if (name) {
1749 zend_alter_ini_entry("session.name", sizeof("session.name"), name, name_len, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
1750 }
1751 }
1752 /* }}} */
1753
1754 /* {{{ proto string session_module_name([string newname])
1755 Return the current module name used for accessing session data. If newname is given, the module name is replaced with newname */
1756 static PHP_FUNCTION(session_module_name)
1757 {
1758 char *name = NULL;
1759 int name_len;
1760
1761 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &name, &name_len) == FAILURE) {
1762 return;
1763 }
1764
1765 /* Set return_value to current module name */
1766 if (PS(mod) && PS(mod)->s_name) {
1767 RETVAL_STRING(safe_estrdup(PS(mod)->s_name), 0);
1768 } else {
1769 RETVAL_EMPTY_STRING();
1770 }
1771
1772 if (name) {
1773 if (!_php_find_ps_module(name TSRMLS_CC)) {
1774 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot find named PHP session module (%s)", name);
1775
1776 zval_dtor(return_value);
1777 RETURN_FALSE;
1778 }
1779 if (PS(mod_data) || PS(mod_user_implemented)) {
1780 PS(mod)->s_close(&PS(mod_data) TSRMLS_CC);
1781 }
1782 PS(mod_data) = NULL;
1783
1784 zend_alter_ini_entry("session.save_handler", sizeof("session.save_handler"), name, name_len, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
1785 }
1786 }
1787 /* }}} */
1788
1789 /* {{{ proto void session_set_save_handler(string open, string close, string read, string write, string destroy, string gc, string create_sid)
1790 Sets user-level functions */
1791 static PHP_FUNCTION(session_set_save_handler)
1792 {
1793 zval ***args = NULL;
1794 int i, num_args, argc = ZEND_NUM_ARGS();
1795 char *name;
1796
1797 if (PS(session_status) != php_session_none) {
1798 RETURN_FALSE;
1799 }
1800
1801 if (argc > 0 && argc <= 2) {
1802 zval *obj = NULL, *callback = NULL;
1803 zend_uint func_name_len;
1804 char *func_name;
1805 HashPosition pos;
1806 zend_function *default_mptr, *current_mptr;
1807 ulong func_index;
1808 php_shutdown_function_entry shutdown_function_entry;
1809 zend_bool register_shutdown = 1;
1810
1811 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|b", &obj, php_session_iface_entry, ®ister_shutdown) == FAILURE) {
1812 RETURN_FALSE;
1813 }
1814
1815 /* Find implemented methods - SessionHandlerInterface */
1816 zend_hash_internal_pointer_reset_ex(&php_session_iface_entry->function_table, &pos);
1817 i = 0;
1818 while (zend_hash_get_current_data_ex(&php_session_iface_entry->function_table, (void **) &default_mptr, &pos) == SUCCESS) {
1819 zend_hash_get_current_key_ex(&php_session_iface_entry->function_table, &func_name, &func_name_len, &func_index, 0, &pos);
1820
1821 if (zend_hash_find(&Z_OBJCE_P(obj)->function_table, func_name, func_name_len, (void **)¤t_mptr) == SUCCESS) {
1822 if (PS(mod_user_names).names[i] != NULL) {
1823 zval_ptr_dtor(&PS(mod_user_names).names[i]);
1824 }
1825
1826 MAKE_STD_ZVAL(callback);
1827 array_init_size(callback, 2);
1828 Z_ADDREF_P(obj);
1829 add_next_index_zval(callback, obj);
1830 add_next_index_stringl(callback, func_name, func_name_len - 1, 1);
1831 PS(mod_user_names).names[i] = callback;
1832 } else {
1833 php_error_docref(NULL TSRMLS_CC, E_ERROR, "Session handler's function table is corrupt");
1834 RETURN_FALSE;
1835 }
1836
1837 zend_hash_move_forward_ex(&php_session_iface_entry->function_table, &pos);
1838 ++i;
1839 }
1840
1841 /* Find implemented methods - SessionIdInterface (optional) */
1842 zend_hash_internal_pointer_reset_ex(&php_session_id_iface_entry->function_table, &pos);
1843 while (zend_hash_get_current_data_ex(&php_session_id_iface_entry->function_table, (void **) &default_mptr, &pos) == SUCCESS) {
1844 zend_hash_get_current_key_ex(&php_session_id_iface_entry->function_table, &func_name, &func_name_len, &func_index, 0, &pos);
1845
1846 if (zend_hash_find(&Z_OBJCE_P(obj)->function_table, func_name, func_name_len, (void **)¤t_mptr) == SUCCESS) {
1847 if (PS(mod_user_names).names[i] != NULL) {
1848 zval_ptr_dtor(&PS(mod_user_names).names[i]);
1849 }
1850
1851 MAKE_STD_ZVAL(callback);
1852 array_init_size(callback, 2);
1853 Z_ADDREF_P(obj);
1854 add_next_index_zval(callback, obj);
1855 add_next_index_stringl(callback, func_name, func_name_len - 1, 1);
1856 PS(mod_user_names).names[i] = callback;
1857 }
1858
1859 zend_hash_move_forward_ex(&php_session_id_iface_entry->function_table, &pos);
1860 ++i;
1861 }
1862
1863 if (register_shutdown) {
1864 /* create shutdown function */
1865 shutdown_function_entry.arg_count = 1;
1866 shutdown_function_entry.arguments = (zval **) safe_emalloc(sizeof(zval *), 1, 0);
1867
1868 MAKE_STD_ZVAL(callback);
1869 ZVAL_STRING(callback, "session_register_shutdown", 1);
1870 shutdown_function_entry.arguments[0] = callback;
1871
1872 /* add shutdown function, removing the old one if it exists */
1873 if (!register_user_shutdown_function("session_shutdown", sizeof("session_shutdown"), &shutdown_function_entry TSRMLS_CC)) {
1874 zval_ptr_dtor(&callback);
1875 efree(shutdown_function_entry.arguments);
1876 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to register session shutdown function");
1877 RETURN_FALSE;
1878 }
1879 } else {
1880 /* remove shutdown function */
1881 remove_user_shutdown_function("session_shutdown", sizeof("session_shutdown") TSRMLS_CC);
1882 }
1883
1884 if (PS(mod) && PS(session_status) == php_session_none && PS(mod) != &ps_mod_user) {
1885 zend_alter_ini_entry("session.save_handler", sizeof("session.save_handler"), "user", sizeof("user")-1, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
1886 }
1887
1888 RETURN_TRUE;
1889 }
1890
1891 if (argc != 6 && argc != 7) {
1892 WRONG_PARAM_COUNT;
1893 }
1894
1895 if (zend_parse_parameters(argc TSRMLS_CC, "+", &args, &num_args) == FAILURE) {
1896 return;
1897 }
1898
1899 /* remove shutdown function */
1900 remove_user_shutdown_function("session_shutdown", sizeof("session_shutdown") TSRMLS_CC);
1901
1902 /* at this point argc can only be 6 or 7 */
1903 for (i = 0; i < argc; i++) {
1904 if (!zend_is_callable(*args[i], 0, &name TSRMLS_CC)) {
1905 efree(args);
1906 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Argument %d is not a valid callback", i+1);
1907 efree(name);
1908 RETURN_FALSE;
1909 }
1910 efree(name);
1911 }
1912
1913 if (PS(mod) && PS(mod) != &ps_mod_user) {
1914 zend_alter_ini_entry("session.save_handler", sizeof("session.save_handler"), "user", sizeof("user")-1, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
1915 }
1916
1917 for (i = 0; i < argc; i++) {
1918 if (PS(mod_user_names).names[i] != NULL) {
1919 zval_ptr_dtor(&PS(mod_user_names).names[i]);
1920 }
1921 Z_ADDREF_PP(args[i]);
1922 PS(mod_user_names).names[i] = *args[i];
1923 }
1924
1925 efree(args);
1926 RETURN_TRUE;
1927 }
1928 /* }}} */
1929
1930 /* {{{ proto string session_save_path([string newname])
1931 Return the current save path passed to module_name. If newname is given, the save path is replaced with newname */
1932 static PHP_FUNCTION(session_save_path)
1933 {
1934 char *name = NULL;
1935 int name_len;
1936
1937 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &name, &name_len) == FAILURE) {
1938 return;
1939 }
1940
1941 RETVAL_STRING(PS(save_path), 1);
1942
1943 if (name) {
1944 if (memchr(name, '\0', name_len) != NULL) {
1945 php_error_docref(NULL TSRMLS_CC, E_WARNING, "The save_path cannot contain NULL characters");
1946 zval_dtor(return_value);
1947 RETURN_FALSE;
1948 }
1949 zend_alter_ini_entry("session.save_path", sizeof("session.save_path"), name, name_len, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
1950 }
1951 }
1952 /* }}} */
1953
1954 /* {{{ proto string session_id([string newid])
1955 Return the current session id. If newid is given, the session id is replaced with newid */
1956 static PHP_FUNCTION(session_id)
1957 {
1958 char *name = NULL;
1959 int name_len, argc = ZEND_NUM_ARGS();
1960
1961 if (zend_parse_parameters(argc TSRMLS_CC, "|s", &name, &name_len) == FAILURE) {
1962 return;
1963 }
1964
1965 if (PS(id)) {
1966 RETVAL_STRING(PS(id), 1);
1967 } else {
1968 RETVAL_EMPTY_STRING();
1969 }
1970
1971 if (name) {
1972 if (PS(id)) {
1973 efree(PS(id));
1974 }
1975 PS(id) = estrndup(name, name_len);
1976 }
1977 }
1978 /* }}} */
1979
1980 /* {{{ proto bool session_regenerate_id([bool delete_old_session])
1981 Update the current session id with a newly generated one. If delete_old_session is set to true, remove the old session. */
1982 static PHP_FUNCTION(session_regenerate_id)
1983 {
1984 zend_bool del_ses = 0;
1985
1986 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &del_ses) == FAILURE) {
1987 return;
1988 }
1989
1990 if (SG(headers_sent) && PS(use_cookies)) {
1991 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot regenerate session id - headers already sent");
1992 RETURN_FALSE;
1993 }
1994
1995 if (PS(session_status) == php_session_active) {
1996 if (PS(id)) {
1997 if (del_ses && PS(mod)->s_destroy(&PS(mod_data), PS(id) TSRMLS_CC) == FAILURE) {
1998 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Session object destruction failed");
1999 RETURN_FALSE;
2000 }
2001 efree(PS(id));
2002 }
2003
2004 PS(id) = PS(mod)->s_create_sid(&PS(mod_data), NULL TSRMLS_CC);
2005 if (PS(id)) {
2006 PS(send_cookie) = 1;
2007 php_session_reset_id(TSRMLS_C);
2008 RETURN_TRUE;
2009 } else {
2010 PS(id) = STR_EMPTY_ALLOC();
2011 }
2012 }
2013 RETURN_FALSE;
2014 }
2015 /* }}} */
2016
2017 /* {{{ proto string session_cache_limiter([string new_cache_limiter])
2018 Return the current cache limiter. If new_cache_limited is given, the current cache_limiter is replaced with new_cache_limiter */
2019 static PHP_FUNCTION(session_cache_limiter)
2020 {
2021 char *limiter = NULL;
2022 int limiter_len;
2023
2024 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &limiter, &limiter_len) == FAILURE) {
2025 return;
2026 }
2027
2028 RETVAL_STRING(PS(cache_limiter), 1);
2029
2030 if (limiter) {
2031 zend_alter_ini_entry("session.cache_limiter", sizeof("session.cache_limiter"), limiter, limiter_len, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
2032 }
2033 }
2034 /* }}} */
2035
2036 /* {{{ proto int session_cache_expire([int new_cache_expire])
2037 Return the current cache expire. If new_cache_expire is given, the current cache_expire is replaced with new_cache_expire */
2038 static PHP_FUNCTION(session_cache_expire)
2039 {
2040 zval **expires = NULL;
2041 int argc = ZEND_NUM_ARGS();
2042
2043 if (zend_parse_parameters(argc TSRMLS_CC, "|Z", &expires) == FAILURE) {
2044 return;
2045 }
2046
2047 RETVAL_LONG(PS(cache_expire));
2048
2049 if (argc == 1) {
2050 convert_to_string_ex(expires);
2051 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);
2052 }
2053 }
2054 /* }}} */
2055
2056 /* {{{ proto string session_encode(void)
2057 Serializes the current setup and returns the serialized representation */
2058 static PHP_FUNCTION(session_encode)
2059 {
2060 int len;
2061 char *enc;
2062
2063 if (zend_parse_parameters_none() == FAILURE) {
2064 return;
2065 }
2066
2067 enc = php_session_encode(&len TSRMLS_CC);
2068 if (enc == NULL) {
2069 RETURN_FALSE;
2070 }
2071
2072 RETVAL_STRINGL(enc, len, 0);
2073 }
2074 /* }}} */
2075
2076 /* {{{ proto bool session_decode(string data)
2077 Deserializes data and reinitializes the variables */
2078 static PHP_FUNCTION(session_decode)
2079 {
2080 char *str;
2081 int str_len;
2082
2083 if (PS(session_status) == php_session_none) {
2084 RETURN_FALSE;
2085 }
2086
2087 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
2088 return;
2089 }
2090
2091 RETVAL_BOOL(php_session_decode(str, str_len TSRMLS_CC) == SUCCESS);
2092 }
2093 /* }}} */
2094
2095 /* {{{ proto bool session_start(void)
2096 Begin session - reinitializes freezed variables, registers browsers etc */
2097 static PHP_FUNCTION(session_start)
2098 {
2099 /* skipping check for non-zero args for performance reasons here ?*/
2100 if (PS(id) && !strlen(PS(id))) {
2101 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot start session with empty session ID");
2102 RETURN_FALSE;
2103 }
2104
2105 php_session_start(TSRMLS_C);
2106
2107 if (PS(session_status) != php_session_active) {
2108 RETURN_FALSE;
2109 }
2110 RETURN_TRUE;
2111 }
2112 /* }}} */
2113
2114 /* {{{ proto bool session_destroy(void)
2115 Destroy the current session and all data associated with it */
2116 static PHP_FUNCTION(session_destroy)
2117 {
2118 if (zend_parse_parameters_none() == FAILURE) {
2119 return;
2120 }
2121
2122 RETURN_BOOL(php_session_destroy(TSRMLS_C) == SUCCESS);
2123 }
2124 /* }}} */
2125
2126 /* {{{ proto void session_unset(void)
2127 Unset all registered variables */
2128 static PHP_FUNCTION(session_unset)
2129 {
2130 if (PS(session_status) == php_session_none) {
2131 RETURN_FALSE;
2132 }
2133
2134 IF_SESSION_VARS() {
2135 HashTable *ht_sess_var;
2136
2137 SEPARATE_ZVAL_IF_NOT_REF(&PS(http_session_vars));
2138 ht_sess_var = Z_ARRVAL_P(PS(http_session_vars));
2139
2140 /* Clean $_SESSION. */
2141 zend_hash_clean(ht_sess_var);
2142 }
2143 }
2144 /* }}} */
2145
2146 /* {{{ proto void session_write_close(void)
2147 Write session data and end session */
2148 static PHP_FUNCTION(session_write_close)
2149 {
2150 php_session_flush(TSRMLS_C);
2151 }
2152 /* }}} */
2153
2154 /* {{{ proto void session_abort(void)
2155 Abort session and end session. Session data will not be written */
2156 static PHP_FUNCTION(session_abort)
2157 {
2158 php_session_abort(TSRMLS_C);
2159 }
2160 /* }}} */
2161
2162 /* {{{ proto void session_reset(void)
2163 Reset session data from saved session data */
2164 static PHP_FUNCTION(session_reset)
2165 {
2166 php_session_reset(TSRMLS_C);
2167 }
2168 /* }}} */
2169
2170 /* {{{ proto int session_status(void)
2171 Returns the current session status */
2172 static PHP_FUNCTION(session_status)
2173 {
2174 if (zend_parse_parameters_none() == FAILURE) {
2175 return;
2176 }
2177
2178 RETURN_LONG(PS(session_status));
2179 }
2180 /* }}} */
2181
2182 /* {{{ proto void session_register_shutdown(void)
2183 Registers session_write_close() as a shutdown function */
2184 static PHP_FUNCTION(session_register_shutdown)
2185 {
2186 php_shutdown_function_entry shutdown_function_entry;
2187 zval *callback;
2188
2189 /* This function is registered itself as a shutdown function by
2190 * session_set_save_handler($obj). The reason we now register another
2191 * shutdown function is in case the user registered their own shutdown
2192 * function after calling session_set_save_handler(), which expects
2193 * the session still to be available.
2194 */
2195
2196 shutdown_function_entry.arg_count = 1;
2197 shutdown_function_entry.arguments = (zval **) safe_emalloc(sizeof(zval *), 1, 0);
2198
2199 MAKE_STD_ZVAL(callback);
2200 ZVAL_STRING(callback, "session_write_close", 1);
2201 shutdown_function_entry.arguments[0] = callback;
2202
2203 if (!append_user_shutdown_function(shutdown_function_entry TSRMLS_CC)) {
2204 zval_ptr_dtor(&callback);
2205 efree(shutdown_function_entry.arguments);
2206
2207 /* Unable to register shutdown function, presumably because of lack
2208 * of memory, so flush the session now. It would be done in rshutdown
2209 * anyway but the handler will have had it's dtor called by then.
2210 * If the user does have a later shutdown function which needs the
2211 * session then tough luck.
2212 */
2213 php_session_flush(TSRMLS_C);
2214 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to register session flush function");
2215 }
2216 }
2217 /* }}} */
2218
2219 /* {{{ arginfo */
2220 ZEND_BEGIN_ARG_INFO_EX(arginfo_session_name, 0, 0, 0)
2221 ZEND_ARG_INFO(0, name)
2222 ZEND_END_ARG_INFO()
2223
2224 ZEND_BEGIN_ARG_INFO_EX(arginfo_session_module_name, 0, 0, 0)
2225 ZEND_ARG_INFO(0, module)
2226 ZEND_END_ARG_INFO()
2227
2228 ZEND_BEGIN_ARG_INFO_EX(arginfo_session_save_path, 0, 0, 0)
2229 ZEND_ARG_INFO(0, path)
2230 ZEND_END_ARG_INFO()
2231
2232 ZEND_BEGIN_ARG_INFO_EX(arginfo_session_id, 0, 0, 0)
2233 ZEND_ARG_INFO(0, id)
2234 ZEND_END_ARG_INFO()
2235
2236 ZEND_BEGIN_ARG_INFO_EX(arginfo_session_regenerate_id, 0, 0, 0)
2237 ZEND_ARG_INFO(0, delete_old_session)
2238 ZEND_END_ARG_INFO()
2239
2240 ZEND_BEGIN_ARG_INFO_EX(arginfo_session_decode, 0, 0, 1)
2241 ZEND_ARG_INFO(0, data)
2242 ZEND_END_ARG_INFO()
2243
2244 ZEND_BEGIN_ARG_INFO(arginfo_session_void, 0)
2245 ZEND_END_ARG_INFO()
2246
2247 ZEND_BEGIN_ARG_INFO_EX(arginfo_session_set_save_handler, 0, 0, 1)
2248 ZEND_ARG_INFO(0, open)
2249 ZEND_ARG_INFO(0, close)
2250 ZEND_ARG_INFO(0, read)
2251 ZEND_ARG_INFO(0, write)
2252 ZEND_ARG_INFO(0, destroy)
2253 ZEND_ARG_INFO(0, gc)
2254 ZEND_ARG_INFO(0, create_sid)
2255 ZEND_END_ARG_INFO()
2256
2257 ZEND_BEGIN_ARG_INFO_EX(arginfo_session_cache_limiter, 0, 0, 0)
2258 ZEND_ARG_INFO(0, cache_limiter)
2259 ZEND_END_ARG_INFO()
2260
2261 ZEND_BEGIN_ARG_INFO_EX(arginfo_session_cache_expire, 0, 0, 0)
2262 ZEND_ARG_INFO(0, new_cache_expire)
2263 ZEND_END_ARG_INFO()
2264
2265 ZEND_BEGIN_ARG_INFO_EX(arginfo_session_set_cookie_params, 0, 0, 1)
2266 ZEND_ARG_INFO(0, lifetime)
2267 ZEND_ARG_INFO(0, path)
2268 ZEND_ARG_INFO(0, domain)
2269 ZEND_ARG_INFO(0, secure)
2270 ZEND_ARG_INFO(0, httponly)
2271 ZEND_END_ARG_INFO()
2272
2273 ZEND_BEGIN_ARG_INFO(arginfo_session_class_open, 0)
2274 ZEND_ARG_INFO(0, save_path)
2275 ZEND_ARG_INFO(0, session_name)
2276 ZEND_END_ARG_INFO()
2277
2278 ZEND_BEGIN_ARG_INFO(arginfo_session_class_close, 0)
2279 ZEND_END_ARG_INFO()
2280
2281 ZEND_BEGIN_ARG_INFO(arginfo_session_class_read, 0)
2282 ZEND_ARG_INFO(0, key)
2283 ZEND_END_ARG_INFO()
2284
2285 ZEND_BEGIN_ARG_INFO(arginfo_session_class_write, 0)
2286 ZEND_ARG_INFO(0, key)
2287 ZEND_ARG_INFO(0, val)
2288 ZEND_END_ARG_INFO()
2289
2290 ZEND_BEGIN_ARG_INFO(arginfo_session_class_destroy, 0)
2291 ZEND_ARG_INFO(0, key)
2292 ZEND_END_ARG_INFO()
2293
2294 ZEND_BEGIN_ARG_INFO(arginfo_session_class_gc, 0)
2295 ZEND_ARG_INFO(0, maxlifetime)
2296 ZEND_END_ARG_INFO()
2297
2298 ZEND_BEGIN_ARG_INFO(arginfo_session_class_create_sid, 0)
2299 ZEND_END_ARG_INFO()
2300 /* }}} */
2301
2302 /* {{{ session_functions[]
2303 */
2304 static const zend_function_entry session_functions[] = {
2305 PHP_FE(session_name, arginfo_session_name)
2306 PHP_FE(session_module_name, arginfo_session_module_name)
2307 PHP_FE(session_save_path, arginfo_session_save_path)
2308 PHP_FE(session_id, arginfo_session_id)
2309 PHP_FE(session_regenerate_id, arginfo_session_regenerate_id)
2310 PHP_FE(session_decode, arginfo_session_decode)
2311 PHP_FE(session_encode, arginfo_session_void)
2312 PHP_FE(session_start, arginfo_session_void)
2313 PHP_FE(session_destroy, arginfo_session_void)
2314 PHP_FE(session_unset, arginfo_session_void)
2315 PHP_FE(session_set_save_handler, arginfo_session_set_save_handler)
2316 PHP_FE(session_cache_limiter, arginfo_session_cache_limiter)
2317 PHP_FE(session_cache_expire, arginfo_session_cache_expire)
2318 PHP_FE(session_set_cookie_params, arginfo_session_set_cookie_params)
2319 PHP_FE(session_get_cookie_params, arginfo_session_void)
2320 PHP_FE(session_write_close, arginfo_session_void)
2321 PHP_FE(session_abort, arginfo_session_void)
2322 PHP_FE(session_reset, arginfo_session_void)
2323 PHP_FE(session_status, arginfo_session_void)
2324 PHP_FE(session_register_shutdown, arginfo_session_void)
2325 PHP_FALIAS(session_commit, session_write_close, arginfo_session_void)
2326 PHP_FE_END
2327 };
2328 /* }}} */
2329
2330 /* {{{ SessionHandlerInterface functions[]
2331 */
2332 static const zend_function_entry php_session_iface_functions[] = {
2333 PHP_ABSTRACT_ME(SessionHandlerInterface, open, arginfo_session_class_open)
2334 PHP_ABSTRACT_ME(SessionHandlerInterface, close, arginfo_session_class_close)
2335 PHP_ABSTRACT_ME(SessionHandlerInterface, read, arginfo_session_class_read)
2336 PHP_ABSTRACT_ME(SessionHandlerInterface, write, arginfo_session_class_write)
2337 PHP_ABSTRACT_ME(SessionHandlerInterface, destroy, arginfo_session_class_destroy)
2338 PHP_ABSTRACT_ME(SessionHandlerInterface, gc, arginfo_session_class_gc)
2339 { NULL, NULL, NULL }
2340 };
2341 /* }}} */
2342
2343 /* {{{ SessionIdInterface functions[]
2344 */
2345 static const zend_function_entry php_session_id_iface_functions[] = {
2346 PHP_ABSTRACT_ME(SessionIdInterface, create_sid, arginfo_session_class_create_sid)
2347 { NULL, NULL, NULL }
2348 };
2349 /* }}} */
2350
2351 /* {{{ SessionHandler functions[]
2352 */
2353 static const zend_function_entry php_session_class_functions[] = {
2354 PHP_ME(SessionHandler, open, arginfo_session_class_open, ZEND_ACC_PUBLIC)
2355 PHP_ME(SessionHandler, close, arginfo_session_class_close, ZEND_ACC_PUBLIC)
2356 PHP_ME(SessionHandler, read, arginfo_session_class_read, ZEND_ACC_PUBLIC)
2357 PHP_ME(SessionHandler, write, arginfo_session_class_write, ZEND_ACC_PUBLIC)
2358 PHP_ME(SessionHandler, destroy, arginfo_session_class_destroy, ZEND_ACC_PUBLIC)
2359 PHP_ME(SessionHandler, gc, arginfo_session_class_gc, ZEND_ACC_PUBLIC)
2360 PHP_ME(SessionHandler, create_sid, arginfo_session_class_create_sid, ZEND_ACC_PUBLIC)
2361 { NULL, NULL, NULL }
2362 };
2363 /* }}} */
2364
2365 /* ********************************
2366 * Module Setup and Destruction *
2367 ******************************** */
2368
2369 static int php_rinit_session(zend_bool auto_start TSRMLS_DC) /* {{{ */
2370 {
2371 php_rinit_session_globals(TSRMLS_C);
2372
2373 if (PS(mod) == NULL) {
2374 char *value;
2375
2376 value = zend_ini_string("session.save_handler", sizeof("session.save_handler"), 0);
2377 if (value) {
2378 PS(mod) = _php_find_ps_module(value TSRMLS_CC);
2379 }
2380 }
2381
2382 if (PS(serializer) == NULL) {
2383 char *value;
2384
2385 value = zend_ini_string("session.serialize_handler", sizeof("session.serialize_handler"), 0);
2386 if (value) {
2387 PS(serializer) = _php_find_ps_serializer(value TSRMLS_CC);
2388 }
2389 }
2390
2391 if (PS(mod) == NULL || PS(serializer) == NULL) {
2392 /* current status is unusable */
2393 PS(session_status) = php_session_disabled;
2394 return SUCCESS;
2395 }
2396
2397 if (auto_start) {
2398 php_session_start(TSRMLS_C);
2399 }
2400
2401 return SUCCESS;
2402 } /* }}} */
2403
2404 static PHP_RINIT_FUNCTION(session) /* {{{ */
2405 {
2406 return php_rinit_session(PS(auto_start) TSRMLS_CC);
2407 }
2408 /* }}} */
2409
2410 static PHP_RSHUTDOWN_FUNCTION(session) /* {{{ */
2411 {
2412 int i;
2413
2414 zend_try {
2415 php_session_flush(TSRMLS_C);
2416 } zend_end_try();
2417 php_rshutdown_session_globals(TSRMLS_C);
2418
2419 /* this should NOT be done in php_rshutdown_session_globals() */
2420 for (i = 0; i < 7; i++) {
2421 if (PS(mod_user_names).names[i] != NULL) {
2422 zval_ptr_dtor(&PS(mod_user_names).names[i]);
2423 PS(mod_user_names).names[i] = NULL;
2424 }
2425 }
2426
2427 return SUCCESS;
2428 }
2429 /* }}} */
2430
2431 static PHP_GINIT_FUNCTION(ps) /* {{{ */
2432 {
2433 int i;
2434
2435 ps_globals->save_path = NULL;
2436 ps_globals->session_name = NULL;
2437 ps_globals->id = NULL;
2438 ps_globals->mod = NULL;
2439 ps_globals->serializer = NULL;
2440 ps_globals->mod_data = NULL;
2441 ps_globals->session_status = php_session_none;
2442 ps_globals->default_mod = NULL;
2443 ps_globals->mod_user_implemented = 0;
2444 ps_globals->mod_user_is_open = 0;
2445 for (i = 0; i < 7; i++) {
2446 ps_globals->mod_user_names.names[i] = NULL;
2447 }
2448 ps_globals->http_session_vars = NULL;
2449 }
2450 /* }}} */
2451
2452 static PHP_MINIT_FUNCTION(session) /* {{{ */
2453 {
2454 zend_class_entry ce;
2455
2456 zend_register_auto_global("_SESSION", sizeof("_SESSION")-1, 0, NULL TSRMLS_CC);
2457
2458 PS(module_number) = module_number; /* if we really need this var we need to init it in zts mode as well! */
2459
2460 PS(session_status) = php_session_none;
2461 REGISTER_INI_ENTRIES();
2462
2463 #ifdef HAVE_LIBMM
2464 PHP_MINIT(ps_mm) (INIT_FUNC_ARGS_PASSTHRU);
2465 #endif
2466 php_session_rfc1867_orig_callback = php_rfc1867_callback;
2467 php_rfc1867_callback = php_session_rfc1867_callback;
2468
2469 /* Register interfaces */
2470 INIT_CLASS_ENTRY(ce, PS_IFACE_NAME, php_session_iface_functions);
2471 php_session_iface_entry = zend_register_internal_class(&ce TSRMLS_CC);
2472 php_session_iface_entry->ce_flags |= ZEND_ACC_INTERFACE;
2473
2474 INIT_CLASS_ENTRY(ce, PS_SID_IFACE_NAME, php_session_id_iface_functions);
2475 php_session_id_iface_entry = zend_register_internal_class(&ce TSRMLS_CC);
2476 php_session_id_iface_entry->ce_flags |= ZEND_ACC_INTERFACE;
2477
2478 /* Register base class */
2479 INIT_CLASS_ENTRY(ce, PS_CLASS_NAME, php_session_class_functions);
2480 php_session_class_entry = zend_register_internal_class(&ce TSRMLS_CC);
2481 zend_class_implements(php_session_class_entry TSRMLS_CC, 1, php_session_iface_entry);
2482 zend_class_implements(php_session_class_entry TSRMLS_CC, 1, php_session_id_iface_entry);
2483
2484 REGISTER_LONG_CONSTANT("PHP_SESSION_DISABLED", php_session_disabled, CONST_CS | CONST_PERSISTENT);
2485 REGISTER_LONG_CONSTANT("PHP_SESSION_NONE", php_session_none, CONST_CS | CONST_PERSISTENT);
2486 REGISTER_LONG_CONSTANT("PHP_SESSION_ACTIVE", php_session_active, CONST_CS | CONST_PERSISTENT);
2487
2488 return SUCCESS;
2489 }
2490 /* }}} */
2491
2492 static PHP_MSHUTDOWN_FUNCTION(session) /* {{{ */
2493 {
2494 UNREGISTER_INI_ENTRIES();
2495
2496 #ifdef HAVE_LIBMM
2497 PHP_MSHUTDOWN(ps_mm) (SHUTDOWN_FUNC_ARGS_PASSTHRU);
2498 #endif
2499
2500 /* reset rfc1867 callbacks */
2501 php_session_rfc1867_orig_callback = NULL;
2502 if (php_rfc1867_callback == php_session_rfc1867_callback) {
2503 php_rfc1867_callback = NULL;
2504 }
2505
2506 ps_serializers[PREDEFINED_SERIALIZERS].name = NULL;
2507 memset(&ps_modules[PREDEFINED_MODULES], 0, (MAX_MODULES-PREDEFINED_MODULES)*sizeof(ps_module *));
2508
2509 return SUCCESS;
2510 }
2511 /* }}} */
2512
2513 static PHP_MINFO_FUNCTION(session) /* {{{ */
2514 {
2515 ps_module **mod;
2516 ps_serializer *ser;
2517 smart_str save_handlers = {0};
2518 smart_str ser_handlers = {0};
2519 int i;
2520
2521 /* Get save handlers */
2522 for (i = 0, mod = ps_modules; i < MAX_MODULES; i++, mod++) {
2523 if (*mod && (*mod)->s_name) {
2524 smart_str_appends(&save_handlers, (*mod)->s_name);
2525 smart_str_appendc(&save_handlers, ' ');
2526 }
2527 }
2528
2529 /* Get serializer handlers */
2530 for (i = 0, ser = ps_serializers; i < MAX_SERIALIZERS; i++, ser++) {
2531 if (ser && ser->name) {
2532 smart_str_appends(&ser_handlers, ser->name);
2533 smart_str_appendc(&ser_handlers, ' ');
2534 }
2535 }
2536
2537 php_info_print_table_start();
2538 php_info_print_table_row(2, "Session Support", "enabled" );
2539
2540 if (save_handlers.c) {
2541 smart_str_0(&save_handlers);
2542 php_info_print_table_row(2, "Registered save handlers", save_handlers.c);
2543 smart_str_free(&save_handlers);
2544 } else {
2545 php_info_print_table_row(2, "Registered save handlers", "none");
2546 }
2547
2548 if (ser_handlers.c) {
2549 smart_str_0(&ser_handlers);
2550 php_info_print_table_row(2, "Registered serializer handlers", ser_handlers.c);
2551 smart_str_free(&ser_handlers);
2552 } else {
2553 php_info_print_table_row(2, "Registered serializer handlers", "none");
2554 }
2555
2556 php_info_print_table_end();
2557
2558 DISPLAY_INI_ENTRIES();
2559 }
2560 /* }}} */
2561
2562 static const zend_module_dep session_deps[] = { /* {{{ */
2563 ZEND_MOD_OPTIONAL("hash")
2564 ZEND_MOD_REQUIRED("spl")
2565 ZEND_MOD_END
2566 };
2567 /* }}} */
2568
2569 /* ************************
2570 * Upload hook handling *
2571 ************************ */
2572
2573 static zend_bool early_find_sid_in(zval *dest, int where, php_session_rfc1867_progress *progress TSRMLS_DC) /* {{{ */
2574 {
2575 zval **ppid;
2576
2577 if (!PG(http_globals)[where]) {
2578 return 0;
2579 }
2580
2581 if (zend_hash_find(Z_ARRVAL_P(PG(http_globals)[where]), PS(session_name), progress->sname_len+1, (void **)&ppid) == SUCCESS
2582 && Z_TYPE_PP(ppid) == IS_STRING) {
2583 zval_dtor(dest);
2584 ZVAL_ZVAL(dest, *ppid, 1, 0);
2585 return 1;
2586 }
2587
2588 return 0;
2589 } /* }}} */
2590
2591 static void php_session_rfc1867_early_find_sid(php_session_rfc1867_progress *progress TSRMLS_DC) /* {{{ */
2592 {
2593
2594 if (PS(use_cookies)) {
2595 sapi_module.treat_data(PARSE_COOKIE, NULL, NULL TSRMLS_CC);
2596 if (early_find_sid_in(&progress->sid, TRACK_VARS_COOKIE, progress TSRMLS_CC)) {
2597 progress->apply_trans_sid = 0;
2598 return;
2599 }
2600 }
2601 if (PS(use_only_cookies)) {
2602 return;
2603 }
2604 sapi_module.treat_data(PARSE_GET, NULL, NULL TSRMLS_CC);
2605 early_find_sid_in(&progress->sid, TRACK_VARS_GET, progress TSRMLS_CC);
2606 } /* }}} */
2607
2608 static zend_bool php_check_cancel_upload(php_session_rfc1867_progress *progress TSRMLS_DC) /* {{{ */
2609 {
2610 zval **progress_ary, **cancel_upload;
2611
2612 if (zend_symtable_find(Z_ARRVAL_P(PS(http_session_vars)), progress->key.c, progress->key.len+1, (void**)&progress_ary) != SUCCESS) {
2613 return 0;
2614 }
2615 if (Z_TYPE_PP(progress_ary) != IS_ARRAY) {
2616 return 0;
2617 }
2618 if (zend_hash_find(Z_ARRVAL_PP(progress_ary), "cancel_upload", sizeof("cancel_upload"), (void**)&cancel_upload) != SUCCESS) {
2619 return 0;
2620 }
2621 return Z_TYPE_PP(cancel_upload) == IS_BOOL && Z_LVAL_PP(cancel_upload);
2622 } /* }}} */
2623
2624 static void php_session_rfc1867_update(php_session_rfc1867_progress *progress, int force_update TSRMLS_DC) /* {{{ */
2625 {
2626 if (!force_update) {
2627 if (Z_LVAL_P(progress->post_bytes_processed) < progress->next_update) {
2628 return;
2629 }
2630 #ifdef HAVE_GETTIMEOFDAY
2631 if (PS(rfc1867_min_freq) > 0.0) {
2632 struct timeval tv = {0};
2633 double dtv;
2634 gettimeofday(&tv, NULL);
2635 dtv = (double) tv.tv_sec + tv.tv_usec / 1000000.0;
2636 if (dtv < progress->next_update_time) {
2637 return;
2638 }
2639 progress->next_update_time = dtv + PS(rfc1867_min_freq);
2640 }
2641 #endif
2642 progress->next_update = Z_LVAL_P(progress->post_bytes_processed) + progress->update_step;
2643 }
2644
2645 php_session_initialize(TSRMLS_C);
2646 PS(session_status) = php_session_active;
2647 IF_SESSION_VARS() {
2648 progress->cancel_upload |= php_check_cancel_upload(progress TSRMLS_CC);
2649 ZEND_SET_SYMBOL_WITH_LENGTH(Z_ARRVAL_P(PS(http_session_vars)), progress->key.c, progress->key.len+1, progress->data, 2, 0);
2650 }
2651 php_session_flush(TSRMLS_C);
2652 } /* }}} */
2653
2654 static void php_session_rfc1867_cleanup(php_session_rfc1867_progress *progress TSRMLS_DC) /* {{{ */
2655 {
2656 php_session_initialize(TSRMLS_C);
2657 PS(session_status) = php_session_active;
2658 IF_SESSION_VARS() {
2659 zend_hash_del(Z_ARRVAL_P(PS(http_session_vars)), progress->key.c, progress->key.len+1);
2660 }
2661 php_session_flush(TSRMLS_C);
2662 } /* }}} */
2663
2664 static int php_session_rfc1867_callback(unsigned int event, void *event_data, void **extra TSRMLS_DC) /* {{{ */
2665 {
2666 php_session_rfc1867_progress *progress;
2667 int retval = SUCCESS;
2668
2669 if (php_session_rfc1867_orig_callback) {
2670 retval = php_session_rfc1867_orig_callback(event, event_data, extra TSRMLS_CC);
2671 }
2672 if (!PS(rfc1867_enabled)) {
2673 return retval;
2674 }
2675
2676 progress = PS(rfc1867_progress);
2677
2678 switch(event) {
2679 case MULTIPART_EVENT_START: {
2680 multipart_event_start *data = (multipart_event_start *) event_data;
2681 progress = ecalloc(1, sizeof(php_session_rfc1867_progress));
2682 progress->content_length = data->content_length;
2683 progress->sname_len = strlen(PS(session_name));
2684 PS(rfc1867_progress) = progress;
2685 }
2686 break;
2687 case MULTIPART_EVENT_FORMDATA: {
2688 multipart_event_formdata *data = (multipart_event_formdata *) event_data;
2689 size_t value_len;
2690
2691 if (Z_TYPE(progress->sid) && progress->key.c) {
2692 break;
2693 }
2694
2695 /* orig callback may have modified *data->newlength */
2696 if (data->newlength) {
2697 value_len = *data->newlength;
2698 } else {
2699 value_len = data->length;
2700 }
2701
2702 if (data->name && data->value && value_len) {
2703 size_t name_len = strlen(data->name);
2704
2705 if (name_len == progress->sname_len && memcmp(data->name, PS(session_name), name_len) == 0) {
2706 zval_dtor(&progress->sid);
2707 ZVAL_STRINGL(&progress->sid, (*data->value), value_len, 1);
2708
2709 } else if (name_len == PS(rfc1867_name).len && memcmp(data->name, PS(rfc1867_name).c, name_len) == 0) {
2710 smart_str_free(&progress->key);
2711 smart_str_appendl(&progress->key, PS(rfc1867_prefix).c, PS(rfc1867_prefix).len);
2712 smart_str_appendl(&progress->key, *data->value, value_len);
2713 smart_str_0(&progress->key);
2714
2715 progress->apply_trans_sid = PS(use_trans_sid);
2716 php_session_rfc1867_early_find_sid(progress TSRMLS_CC);
2717 }
2718 }
2719 }
2720 break;
2721 case MULTIPART_EVENT_FILE_START: {
2722 multipart_event_file_start *data = (multipart_event_file_start *) event_data;
2723
2724 /* Do nothing when $_POST["PHP_SESSION_UPLOAD_PROGRESS"] is not set
2725 * or when we have no session id */
2726 if (!Z_TYPE(progress->sid) || !progress->key.c) {
2727 break;
2728 }
2729
2730 /* First FILE_START event, initializing data */
2731 if (!progress->data) {
2732
2733 if (PS(rfc1867_freq) >= 0) {
2734 progress->update_step = PS(rfc1867_freq);
2735 } else if (PS(rfc1867_freq) < 0) { /* % of total size */
2736 progress->update_step = progress->content_length * -PS(rfc1867_freq) / 100;
2737 }
2738 progress->next_update = 0;
2739 progress->next_update_time = 0.0;
2740
2741 ALLOC_INIT_ZVAL(progress->data);
2742 array_init(progress->data);
2743
2744 ALLOC_INIT_ZVAL(progress->post_bytes_processed);
2745 ZVAL_LONG(progress->post_bytes_processed, data->post_bytes_processed);
2746
2747 ALLOC_INIT_ZVAL(progress->files);
2748 array_init(progress->files);
2749
2750 add_assoc_long_ex(progress->data, "start_time", sizeof("start_time"), (long)sapi_get_request_time(TSRMLS_C));
2751 add_assoc_long_ex(progress->data, "content_length", sizeof("content_length"), progress->content_length);
2752 add_assoc_zval_ex(progress->data, "bytes_processed", sizeof("bytes_processed"), progress->post_bytes_processed);
2753 add_assoc_bool_ex(progress->data, "done", sizeof("done"), 0);
2754 add_assoc_zval_ex(progress->data, "files", sizeof("files"), progress->files);
2755
2756 php_rinit_session(0 TSRMLS_CC);
2757 PS(id) = estrndup(Z_STRVAL(progress->sid), Z_STRLEN(progress->sid));
2758 PS(apply_trans_sid) = progress->apply_trans_sid;
2759 PS(send_cookie) = 0;
2760 }
2761
2762 ALLOC_INIT_ZVAL(progress->current_file);
2763 array_init(progress->current_file);
2764
2765 ALLOC_INIT_ZVAL(progress->current_file_bytes_processed);
2766 ZVAL_LONG(progress->current_file_bytes_processed, 0);
2767
2768 /* Each uploaded file has its own array. Trying to make it close to $_FILES entries. */
2769 add_assoc_string_ex(progress->current_file, "field_name", sizeof("field_name"), data->name, 1);
2770 add_assoc_string_ex(progress->current_file, "name", sizeof("name"), *data->filename, 1);
2771 add_assoc_null_ex(progress->current_file, "tmp_name", sizeof("tmp_name"));
2772 add_assoc_long_ex(progress->current_file, "error", sizeof("error"), 0);
2773
2774 add_assoc_bool_ex(progress->current_file, "done", sizeof("done"), 0);
2775 add_assoc_long_ex(progress->current_file, "start_time", sizeof("start_time"), (long)time(NULL));
2776 add_assoc_zval_ex(progress->current_file, "bytes_processed", sizeof("bytes_processed"), progress->current_file_bytes_processed);
2777
2778 add_next_index_zval(progress->files, progress->current_file);
2779
2780 Z_LVAL_P(progress->post_bytes_processed) = data->post_bytes_processed;
2781
2782 php_session_rfc1867_update(progress, 0 TSRMLS_CC);
2783 }
2784 break;
2785 case MULTIPART_EVENT_FILE_DATA: {
2786 multipart_event_file_data *data = (multipart_event_file_data *) event_data;
2787
2788 if (!Z_TYPE(progress->sid) || !progress->key.c) {
2789 break;
2790 }
2791
2792 Z_LVAL_P(progress->current_file_bytes_processed) = data->offset + data->length;
2793 Z_LVAL_P(progress->post_bytes_processed) = data->post_bytes_processed;
2794
2795 php_session_rfc1867_update(progress, 0 TSRMLS_CC);
2796 }
2797 break;
2798 case MULTIPART_EVENT_FILE_END: {
2799 multipart_event_file_end *data = (multipart_event_file_end *) event_data;
2800
2801 if (!Z_TYPE(progress->sid) || !progress->key.c) {
2802 break;
2803 }
2804
2805 if (data->temp_filename) {
2806 add_assoc_string_ex(progress->current_file, "tmp_name", sizeof("tmp_name"), data->temp_filename, 1);
2807 }
2808 add_assoc_long_ex(progress->current_file, "error", sizeof("error"), data->cancel_upload);
2809 add_assoc_bool_ex(progress->current_file, "done", sizeof("done"), 1);
2810
2811 Z_LVAL_P(progress->post_bytes_processed) = data->post_bytes_processed;
2812
2813 php_session_rfc1867_update(progress, 0 TSRMLS_CC);
2814 }
2815 break;
2816 case MULTIPART_EVENT_END: {
2817 multipart_event_end *data = (multipart_event_end *) event_data;
2818
2819 if (Z_TYPE(progress->sid) && progress->key.c) {
2820 if (PS(rfc1867_cleanup)) {
2821 php_session_rfc1867_cleanup(progress TSRMLS_CC);
2822 } else {
2823 add_assoc_bool_ex(progress->data, "done", sizeof("done"), 1);
2824 Z_LVAL_P(progress->post_bytes_processed) = data->post_bytes_processed;
2825 php_session_rfc1867_update(progress, 1 TSRMLS_CC);
2826 }
2827 php_rshutdown_session_globals(TSRMLS_C);
2828 }
2829
2830 if (progress->data) {
2831 zval_ptr_dtor(&progress->data);
2832 }
2833 zval_dtor(&progress->sid);
2834 smart_str_free(&progress->key);
2835 efree(progress);
2836 progress = NULL;
2837 PS(rfc1867_progress) = NULL;
2838 }
2839 break;
2840 }
2841
2842 if (progress && progress->cancel_upload) {
2843 return FAILURE;
2844 }
2845 return retval;
2846
2847 } /* }}} */
2848
2849 zend_module_entry session_module_entry = {
2850 STANDARD_MODULE_HEADER_EX,
2851 NULL,
2852 session_deps,
2853 "session",
2854 session_functions,
2855 PHP_MINIT(session), PHP_MSHUTDOWN(session),
2856 PHP_RINIT(session), PHP_RSHUTDOWN(session),
2857 PHP_MINFO(session),
2858 NO_VERSION_YET,
2859 PHP_MODULE_GLOBALS(ps),
2860 PHP_GINIT(ps),
2861 NULL,
2862 NULL,
2863 STANDARD_MODULE_PROPERTIES_EX
2864 };
2865
2866 #ifdef COMPILE_DL_SESSION
2867 ZEND_GET_MODULE(session)
2868 #endif
2869
2870 /*
2871 * Local variables:
2872 * tab-width: 4
2873 * c-basic-offset: 4
2874 * End:
2875 * vim600: noet sw=4 ts=4 fdm=marker
2876 * vim<600: sw=4 ts=4
2877 */
2878