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