mod_user_class.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. | Author: Arpad Ray <arpad@php.net> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #include "php.h"
  17. #include "php_session.h"
  18. #define PS_SANITY_CHECK \
  19. if (PS(session_status) != php_session_active) { \
  20. zend_throw_error(NULL, "Session is not active"); \
  21. RETURN_THROWS(); \
  22. } \
  23. if (PS(default_mod) == NULL) { \
  24. zend_throw_error(NULL, "Cannot call default session handler"); \
  25. RETURN_THROWS(); \
  26. }
  27. #define PS_SANITY_CHECK_IS_OPEN \
  28. PS_SANITY_CHECK; \
  29. if (!PS(mod_user_is_open)) { \
  30. php_error_docref(NULL, E_WARNING, "Parent session handler is not open"); \
  31. RETURN_FALSE; \
  32. }
  33. /* {{{ Wraps the old open handler */
  34. PHP_METHOD(SessionHandler, open)
  35. {
  36. char *save_path = NULL, *session_name = NULL;
  37. size_t save_path_len, session_name_len;
  38. int ret;
  39. if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &save_path, &save_path_len, &session_name, &session_name_len) == FAILURE) {
  40. RETURN_THROWS();
  41. }
  42. PS_SANITY_CHECK;
  43. PS(mod_user_is_open) = 1;
  44. zend_try {
  45. ret = PS(default_mod)->s_open(&PS(mod_data), save_path, session_name);
  46. } zend_catch {
  47. PS(session_status) = php_session_none;
  48. zend_bailout();
  49. } zend_end_try();
  50. RETVAL_BOOL(SUCCESS == ret);
  51. }
  52. /* }}} */
  53. /* {{{ Wraps the old close handler */
  54. PHP_METHOD(SessionHandler, close)
  55. {
  56. int ret;
  57. // don't return on failure, since not closing the default handler
  58. // could result in memory leaks or other nasties
  59. zend_parse_parameters_none();
  60. PS_SANITY_CHECK_IS_OPEN;
  61. PS(mod_user_is_open) = 0;
  62. zend_try {
  63. ret = PS(default_mod)->s_close(&PS(mod_data));
  64. } zend_catch {
  65. PS(session_status) = php_session_none;
  66. zend_bailout();
  67. } zend_end_try();
  68. RETVAL_BOOL(SUCCESS == ret);
  69. }
  70. /* }}} */
  71. /* {{{ Wraps the old read handler */
  72. PHP_METHOD(SessionHandler, read)
  73. {
  74. zend_string *val;
  75. zend_string *key;
  76. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &key) == FAILURE) {
  77. RETURN_THROWS();
  78. }
  79. PS_SANITY_CHECK_IS_OPEN;
  80. if (PS(default_mod)->s_read(&PS(mod_data), key, &val, PS(gc_maxlifetime)) == FAILURE) {
  81. RETURN_FALSE;
  82. }
  83. RETURN_STR(val);
  84. }
  85. /* }}} */
  86. /* {{{ Wraps the old write handler */
  87. PHP_METHOD(SessionHandler, write)
  88. {
  89. zend_string *key, *val;
  90. if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS", &key, &val) == FAILURE) {
  91. RETURN_THROWS();
  92. }
  93. PS_SANITY_CHECK_IS_OPEN;
  94. RETURN_BOOL(SUCCESS == PS(default_mod)->s_write(&PS(mod_data), key, val, PS(gc_maxlifetime)));
  95. }
  96. /* }}} */
  97. /* {{{ Wraps the old destroy handler */
  98. PHP_METHOD(SessionHandler, destroy)
  99. {
  100. zend_string *key;
  101. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &key) == FAILURE) {
  102. RETURN_THROWS();
  103. }
  104. PS_SANITY_CHECK_IS_OPEN;
  105. RETURN_BOOL(SUCCESS == PS(default_mod)->s_destroy(&PS(mod_data), key));
  106. }
  107. /* }}} */
  108. /* {{{ Wraps the old gc handler */
  109. PHP_METHOD(SessionHandler, gc)
  110. {
  111. zend_long maxlifetime;
  112. zend_long nrdels = -1;
  113. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &maxlifetime) == FAILURE) {
  114. RETURN_THROWS();
  115. }
  116. PS_SANITY_CHECK_IS_OPEN;
  117. if (PS(default_mod)->s_gc(&PS(mod_data), maxlifetime, &nrdels) == FAILURE) {
  118. RETURN_FALSE;
  119. }
  120. RETURN_LONG(nrdels);
  121. }
  122. /* }}} */
  123. /* {{{ Wraps the old create_sid handler */
  124. PHP_METHOD(SessionHandler, create_sid)
  125. {
  126. zend_string *id;
  127. if (zend_parse_parameters_none() == FAILURE) {
  128. RETURN_THROWS();
  129. }
  130. PS_SANITY_CHECK;
  131. id = PS(default_mod)->s_create_sid(&PS(mod_data));
  132. RETURN_STR(id);
  133. }
  134. /* }}} */