mod_user_class.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Arpad Ray <arpad@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. #include "php.h"
  20. #include "php_session.h"
  21. #define PS_SANITY_CHECK \
  22. if (PS(default_mod) == NULL) { \
  23. php_error_docref(NULL TSRMLS_CC, E_CORE_ERROR, "Cannot call default session handler"); \
  24. RETURN_FALSE; \
  25. }
  26. #define PS_SANITY_CHECK_IS_OPEN \
  27. PS_SANITY_CHECK; \
  28. if (!PS(mod_user_is_open)) { \
  29. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Parent session handler is not open"); \
  30. RETURN_FALSE; \
  31. }
  32. /* {{{ proto bool SessionHandler::open(string save_path, string session_name)
  33. Wraps the old open handler */
  34. PHP_METHOD(SessionHandler, open)
  35. {
  36. char *save_path = NULL, *session_name = NULL;
  37. int save_path_len, session_name_len;
  38. PS_SANITY_CHECK;
  39. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &save_path, &save_path_len, &session_name, &session_name_len) == FAILURE) {
  40. return;
  41. }
  42. PS(mod_user_is_open) = 1;
  43. RETVAL_BOOL(SUCCESS == PS(default_mod)->s_open(&PS(mod_data), save_path, session_name TSRMLS_CC));
  44. }
  45. /* }}} */
  46. /* {{{ proto bool SessionHandler::close()
  47. Wraps the old close handler */
  48. PHP_METHOD(SessionHandler, close)
  49. {
  50. PS_SANITY_CHECK_IS_OPEN;
  51. // don't return on failure, since not closing the default handler
  52. // could result in memory leaks or other nasties
  53. zend_parse_parameters_none();
  54. PS(mod_user_is_open) = 0;
  55. RETVAL_BOOL(SUCCESS == PS(default_mod)->s_close(&PS(mod_data) TSRMLS_CC));
  56. }
  57. /* }}} */
  58. /* {{{ proto bool SessionHandler::read(string id)
  59. Wraps the old read handler */
  60. PHP_METHOD(SessionHandler, read)
  61. {
  62. char *key, *val;
  63. int key_len, val_len;
  64. PS_SANITY_CHECK_IS_OPEN;
  65. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &key, &key_len) == FAILURE) {
  66. return;
  67. }
  68. if (PS(default_mod)->s_read(&PS(mod_data), key, &val, &val_len TSRMLS_CC) == FAILURE) {
  69. RETVAL_FALSE;
  70. return;
  71. }
  72. RETVAL_STRINGL(val, val_len, 1);
  73. str_efree(val);
  74. return;
  75. }
  76. /* }}} */
  77. /* {{{ proto bool SessionHandler::write(string id, string data)
  78. Wraps the old write handler */
  79. PHP_METHOD(SessionHandler, write)
  80. {
  81. char *key, *val;
  82. int key_len, val_len;
  83. PS_SANITY_CHECK_IS_OPEN;
  84. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &key, &key_len, &val, &val_len) == FAILURE) {
  85. return;
  86. }
  87. RETVAL_BOOL(SUCCESS == PS(default_mod)->s_write(&PS(mod_data), key, val, val_len TSRMLS_CC));
  88. }
  89. /* }}} */
  90. /* {{{ proto bool SessionHandler::destroy(string id)
  91. Wraps the old destroy handler */
  92. PHP_METHOD(SessionHandler, destroy)
  93. {
  94. char *key;
  95. int key_len;
  96. PS_SANITY_CHECK_IS_OPEN;
  97. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &key, &key_len) == FAILURE) {
  98. return;
  99. }
  100. RETVAL_BOOL(SUCCESS == PS(default_mod)->s_destroy(&PS(mod_data), key TSRMLS_CC));
  101. }
  102. /* }}} */
  103. /* {{{ proto bool SessionHandler::gc(int maxlifetime)
  104. Wraps the old gc handler */
  105. PHP_METHOD(SessionHandler, gc)
  106. {
  107. long maxlifetime;
  108. int nrdels;
  109. PS_SANITY_CHECK_IS_OPEN;
  110. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &maxlifetime) == FAILURE) {
  111. return;
  112. }
  113. RETVAL_BOOL(SUCCESS == PS(default_mod)->s_gc(&PS(mod_data), maxlifetime, &nrdels TSRMLS_CC));
  114. }
  115. /* }}} */
  116. /* {{{ proto char SessionHandler::create_sid()
  117. Wraps the old create_sid handler */
  118. PHP_METHOD(SessionHandler, create_sid)
  119. {
  120. char *id;
  121. PS_SANITY_CHECK;
  122. if (zend_parse_parameters_none() == FAILURE) {
  123. return;
  124. }
  125. id = PS(default_mod)->s_create_sid(&PS(mod_data), NULL TSRMLS_CC);
  126. RETURN_STRING(id, 0);
  127. }
  128. /* }}} */