mod_user_class.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Arpad Ray <arpad@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include "php.h"
  19. #include "php_session.h"
  20. #define PS_SANITY_CHECK \
  21. if (PS(session_status) != php_session_active) { \
  22. php_error_docref(NULL, E_WARNING, "Session is not active"); \
  23. RETURN_FALSE; \
  24. } \
  25. if (PS(default_mod) == NULL) { \
  26. php_error_docref(NULL, E_CORE_ERROR, "Cannot call default session handler"); \
  27. RETURN_FALSE; \
  28. }
  29. #define PS_SANITY_CHECK_IS_OPEN \
  30. PS_SANITY_CHECK; \
  31. if (!PS(mod_user_is_open)) { \
  32. php_error_docref(NULL, E_WARNING, "Parent session handler is not open"); \
  33. RETURN_FALSE; \
  34. }
  35. /* {{{ proto bool SessionHandler::open(string save_path, string session_name)
  36. Wraps the old open handler */
  37. PHP_METHOD(SessionHandler, open)
  38. {
  39. char *save_path = NULL, *session_name = NULL;
  40. size_t save_path_len, session_name_len;
  41. int ret;
  42. PS_SANITY_CHECK;
  43. if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &save_path, &save_path_len, &session_name, &session_name_len) == FAILURE) {
  44. return;
  45. }
  46. PS(mod_user_is_open) = 1;
  47. zend_try {
  48. ret = PS(default_mod)->s_open(&PS(mod_data), save_path, session_name);
  49. } zend_catch {
  50. PS(session_status) = php_session_none;
  51. zend_bailout();
  52. } zend_end_try();
  53. RETVAL_BOOL(SUCCESS == ret);
  54. }
  55. /* }}} */
  56. /* {{{ proto bool SessionHandler::close()
  57. Wraps the old close handler */
  58. PHP_METHOD(SessionHandler, close)
  59. {
  60. int ret;
  61. PS_SANITY_CHECK_IS_OPEN;
  62. // don't return on failure, since not closing the default handler
  63. // could result in memory leaks or other nasties
  64. zend_parse_parameters_none();
  65. PS(mod_user_is_open) = 0;
  66. zend_try {
  67. ret = PS(default_mod)->s_close(&PS(mod_data));
  68. } zend_catch {
  69. PS(session_status) = php_session_none;
  70. zend_bailout();
  71. } zend_end_try();
  72. RETVAL_BOOL(SUCCESS == ret);
  73. }
  74. /* }}} */
  75. /* {{{ proto bool SessionHandler::read(string id)
  76. Wraps the old read handler */
  77. PHP_METHOD(SessionHandler, read)
  78. {
  79. zend_string *val;
  80. zend_string *key;
  81. PS_SANITY_CHECK_IS_OPEN;
  82. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &key) == FAILURE) {
  83. return;
  84. }
  85. if (PS(default_mod)->s_read(&PS(mod_data), key, &val, PS(gc_maxlifetime)) == FAILURE) {
  86. RETURN_FALSE;
  87. }
  88. RETURN_STR(val);
  89. }
  90. /* }}} */
  91. /* {{{ proto bool SessionHandler::write(string id, string data)
  92. Wraps the old write handler */
  93. PHP_METHOD(SessionHandler, write)
  94. {
  95. zend_string *key, *val;
  96. PS_SANITY_CHECK_IS_OPEN;
  97. if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS", &key, &val) == FAILURE) {
  98. return;
  99. }
  100. RETURN_BOOL(SUCCESS == PS(default_mod)->s_write(&PS(mod_data), key, val, PS(gc_maxlifetime)));
  101. }
  102. /* }}} */
  103. /* {{{ proto bool SessionHandler::destroy(string id)
  104. Wraps the old destroy handler */
  105. PHP_METHOD(SessionHandler, destroy)
  106. {
  107. zend_string *key;
  108. PS_SANITY_CHECK_IS_OPEN;
  109. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &key) == FAILURE) {
  110. return;
  111. }
  112. RETURN_BOOL(SUCCESS == PS(default_mod)->s_destroy(&PS(mod_data), key));
  113. }
  114. /* }}} */
  115. /* {{{ proto bool SessionHandler::gc(int maxlifetime)
  116. Wraps the old gc handler */
  117. PHP_METHOD(SessionHandler, gc)
  118. {
  119. zend_long maxlifetime;
  120. zend_long nrdels = -1;
  121. PS_SANITY_CHECK_IS_OPEN;
  122. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &maxlifetime) == FAILURE) {
  123. return;
  124. }
  125. if (PS(default_mod)->s_gc(&PS(mod_data), maxlifetime, &nrdels) == FAILURE) {
  126. RETURN_FALSE;
  127. }
  128. RETURN_LONG(nrdels);
  129. }
  130. /* }}} */
  131. /* {{{ proto char SessionHandler::create_sid()
  132. Wraps the old create_sid handler */
  133. PHP_METHOD(SessionHandler, create_sid)
  134. {
  135. zend_string *id;
  136. PS_SANITY_CHECK;
  137. if (zend_parse_parameters_none() == FAILURE) {
  138. return;
  139. }
  140. id = PS(default_mod)->s_create_sid(&PS(mod_data));
  141. RETURN_STR(id);
  142. }
  143. /* }}} */
  144. /* {{{ proto char SessionUpdateTimestampHandler::validateId(string id)
  145. Simply return TRUE */
  146. PHP_METHOD(SessionHandler, validateId)
  147. {
  148. zend_string *key;
  149. PS_SANITY_CHECK_IS_OPEN;
  150. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &key) == FAILURE) {
  151. return;
  152. }
  153. /* Legacy save handler may not support validate_sid API. Return TRUE. */
  154. RETURN_TRUE;
  155. }
  156. /* }}} */
  157. /* {{{ proto bool SessionUpdateTimestampHandler::updateTimestamp(string id, string data)
  158. Simply call update_timestamp */
  159. PHP_METHOD(SessionHandler, updateTimestamp)
  160. {
  161. zend_string *key, *val;
  162. PS_SANITY_CHECK_IS_OPEN;
  163. if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS", &key, &val) == FAILURE) {
  164. return;
  165. }
  166. /* Legacy save handler may not support update_timestamp API. Just write. */
  167. RETVAL_BOOL(SUCCESS == PS(default_mod)->s_write(&PS(mod_data), key, val, PS(gc_maxlifetime)));
  168. }
  169. /* }}} */