PageRenderTime 36ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/ext/session/session.c

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