mod_user.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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: Sascha Schumann <sascha@schumann.cx> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. #include "php.h"
  20. #include "php_session.h"
  21. #include "mod_user.h"
  22. ps_module ps_mod_user = {
  23. PS_MOD_SID(user)
  24. };
  25. #define SESS_ZVAL_LONG(val, a) \
  26. { \
  27. MAKE_STD_ZVAL(a); \
  28. ZVAL_LONG(a, val); \
  29. }
  30. #define SESS_ZVAL_STRING(vl, a) \
  31. { \
  32. char *__vl = vl; \
  33. SESS_ZVAL_STRINGN(__vl, strlen(__vl), a); \
  34. }
  35. #define SESS_ZVAL_STRINGN(vl, ln, a) \
  36. { \
  37. MAKE_STD_ZVAL(a); \
  38. ZVAL_STRINGL(a, vl, ln, 1); \
  39. }
  40. static zval *ps_call_handler(zval *func, int argc, zval **argv TSRMLS_DC)
  41. {
  42. int i;
  43. zval *retval = NULL;
  44. MAKE_STD_ZVAL(retval);
  45. if (call_user_function(EG(function_table), NULL, func, retval, argc, argv TSRMLS_CC) == FAILURE) {
  46. zval_ptr_dtor(&retval);
  47. retval = NULL;
  48. }
  49. for (i = 0; i < argc; i++) {
  50. zval_ptr_dtor(&argv[i]);
  51. }
  52. return retval;
  53. }
  54. #define STDVARS \
  55. zval *retval = NULL; \
  56. int ret = FAILURE
  57. #define PSF(a) PS(mod_user_names).name.ps_##a
  58. #define FINISH \
  59. if (retval) { \
  60. convert_to_long(retval); \
  61. ret = Z_LVAL_P(retval); \
  62. zval_ptr_dtor(&retval); \
  63. } \
  64. return ret
  65. PS_OPEN_FUNC(user)
  66. {
  67. zval *args[2];
  68. STDVARS;
  69. if (PSF(open) == NULL) {
  70. php_error_docref(NULL TSRMLS_CC, E_WARNING,
  71. "user session functions not defined");
  72. return FAILURE;
  73. }
  74. SESS_ZVAL_STRING((char*)save_path, args[0]);
  75. SESS_ZVAL_STRING((char*)session_name, args[1]);
  76. retval = ps_call_handler(PSF(open), 2, args TSRMLS_CC);
  77. PS(mod_user_implemented) = 1;
  78. FINISH;
  79. }
  80. PS_CLOSE_FUNC(user)
  81. {
  82. zend_bool bailout = 0;
  83. STDVARS;
  84. if (!PS(mod_user_implemented)) {
  85. /* already closed */
  86. return SUCCESS;
  87. }
  88. zend_try {
  89. retval = ps_call_handler(PSF(close), 0, NULL TSRMLS_CC);
  90. } zend_catch {
  91. bailout = 1;
  92. } zend_end_try();
  93. PS(mod_user_implemented) = 0;
  94. if (bailout) {
  95. if (retval) {
  96. zval_ptr_dtor(&retval);
  97. }
  98. zend_bailout();
  99. }
  100. FINISH;
  101. }
  102. PS_READ_FUNC(user)
  103. {
  104. zval *args[1];
  105. STDVARS;
  106. SESS_ZVAL_STRING((char*)key, args[0]);
  107. retval = ps_call_handler(PSF(read), 1, args TSRMLS_CC);
  108. if (retval) {
  109. if (Z_TYPE_P(retval) == IS_STRING) {
  110. *val = estrndup(Z_STRVAL_P(retval), Z_STRLEN_P(retval));
  111. *vallen = Z_STRLEN_P(retval);
  112. ret = SUCCESS;
  113. }
  114. zval_ptr_dtor(&retval);
  115. }
  116. return ret;
  117. }
  118. PS_WRITE_FUNC(user)
  119. {
  120. zval *args[2];
  121. STDVARS;
  122. SESS_ZVAL_STRING((char*)key, args[0]);
  123. SESS_ZVAL_STRINGN((char*)val, vallen, args[1]);
  124. retval = ps_call_handler(PSF(write), 2, args TSRMLS_CC);
  125. FINISH;
  126. }
  127. PS_DESTROY_FUNC(user)
  128. {
  129. zval *args[1];
  130. STDVARS;
  131. SESS_ZVAL_STRING((char*)key, args[0]);
  132. retval = ps_call_handler(PSF(destroy), 1, args TSRMLS_CC);
  133. FINISH;
  134. }
  135. PS_GC_FUNC(user)
  136. {
  137. zval *args[1];
  138. STDVARS;
  139. SESS_ZVAL_LONG(maxlifetime, args[0]);
  140. retval = ps_call_handler(PSF(gc), 1, args TSRMLS_CC);
  141. FINISH;
  142. }
  143. PS_CREATE_SID_FUNC(user)
  144. {
  145. /* maintain backwards compatibility */
  146. if (PSF(create_sid) != NULL) {
  147. char *id = NULL;
  148. zval *retval = NULL;
  149. retval = ps_call_handler(PSF(create_sid), 0, NULL TSRMLS_CC);
  150. if (retval) {
  151. if (Z_TYPE_P(retval) == IS_STRING) {
  152. id = estrndup(Z_STRVAL_P(retval), Z_STRLEN_P(retval));
  153. }
  154. zval_ptr_dtor(&retval);
  155. }
  156. else {
  157. php_error_docref(NULL TSRMLS_CC, E_ERROR, "No session id returned by function");
  158. return NULL;
  159. }
  160. if (!id) {
  161. php_error_docref(NULL TSRMLS_CC, E_ERROR, "Session id must be a string");
  162. return NULL;
  163. }
  164. return id;
  165. }
  166. /* function as defined by PS_MOD */
  167. return php_session_create_id(mod_data, newlen TSRMLS_CC);
  168. }
  169. /*
  170. * Local variables:
  171. * tab-width: 4
  172. * c-basic-offset: 4
  173. * End:
  174. * vim600: sw=4 ts=4 fdm=marker
  175. * vim<600: sw=4 ts=4
  176. */