php_output.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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: Michael Wallner <mike@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. #ifndef PHP_OUTPUT_H
  20. #define PHP_OUTPUT_H
  21. #define PHP_OUTPUT_NEWAPI 1
  22. /* handler ops */
  23. #define PHP_OUTPUT_HANDLER_WRITE 0x00 /* standard passthru */
  24. #define PHP_OUTPUT_HANDLER_START 0x01 /* start */
  25. #define PHP_OUTPUT_HANDLER_CLEAN 0x02 /* restart */
  26. #define PHP_OUTPUT_HANDLER_FLUSH 0x04 /* pass along as much as possible */
  27. #define PHP_OUTPUT_HANDLER_FINAL 0x08 /* finalize */
  28. #define PHP_OUTPUT_HANDLER_CONT PHP_OUTPUT_HANDLER_WRITE
  29. #define PHP_OUTPUT_HANDLER_END PHP_OUTPUT_HANDLER_FINAL
  30. /* handler types */
  31. #define PHP_OUTPUT_HANDLER_INTERNAL 0x0000
  32. #define PHP_OUTPUT_HANDLER_USER 0x0001
  33. /* handler ability flags */
  34. #define PHP_OUTPUT_HANDLER_CLEANABLE 0x0010
  35. #define PHP_OUTPUT_HANDLER_FLUSHABLE 0x0020
  36. #define PHP_OUTPUT_HANDLER_REMOVABLE 0x0040
  37. #define PHP_OUTPUT_HANDLER_STDFLAGS 0x0070
  38. /* handler status flags */
  39. #define PHP_OUTPUT_HANDLER_STARTED 0x1000
  40. #define PHP_OUTPUT_HANDLER_DISABLED 0x2000
  41. #define PHP_OUTPUT_HANDLER_PROCESSED 0x4000
  42. /* handler op return values */
  43. typedef enum _php_output_handler_status_t {
  44. PHP_OUTPUT_HANDLER_FAILURE,
  45. PHP_OUTPUT_HANDLER_SUCCESS,
  46. PHP_OUTPUT_HANDLER_NO_DATA
  47. } php_output_handler_status_t;
  48. /* php_output_stack_pop() flags */
  49. #define PHP_OUTPUT_POP_TRY 0x000
  50. #define PHP_OUTPUT_POP_FORCE 0x001
  51. #define PHP_OUTPUT_POP_DISCARD 0x010
  52. #define PHP_OUTPUT_POP_SILENT 0x100
  53. /* real global flags */
  54. #define PHP_OUTPUT_IMPLICITFLUSH 0x01
  55. #define PHP_OUTPUT_DISABLED 0x02
  56. #define PHP_OUTPUT_WRITTEN 0x04
  57. #define PHP_OUTPUT_SENT 0x08
  58. /* supplementary flags for php_output_get_status() */
  59. #define PHP_OUTPUT_ACTIVE 0x10
  60. #define PHP_OUTPUT_LOCKED 0x20
  61. /* output layer is ready to use */
  62. #define PHP_OUTPUT_ACTIVATED 0x100000
  63. /* handler hooks */
  64. typedef enum _php_output_handler_hook_t {
  65. PHP_OUTPUT_HANDLER_HOOK_GET_OPAQ,
  66. PHP_OUTPUT_HANDLER_HOOK_GET_FLAGS,
  67. PHP_OUTPUT_HANDLER_HOOK_GET_LEVEL,
  68. PHP_OUTPUT_HANDLER_HOOK_IMMUTABLE,
  69. PHP_OUTPUT_HANDLER_HOOK_DISABLE,
  70. /* unused */
  71. PHP_OUTPUT_HANDLER_HOOK_LAST
  72. } php_output_handler_hook_t;
  73. #define PHP_OUTPUT_HANDLER_INITBUF_SIZE(s) \
  74. ( ((s) > 1) ? \
  75. (s) + PHP_OUTPUT_HANDLER_ALIGNTO_SIZE - ((s) % (PHP_OUTPUT_HANDLER_ALIGNTO_SIZE)) : \
  76. PHP_OUTPUT_HANDLER_DEFAULT_SIZE \
  77. )
  78. #define PHP_OUTPUT_HANDLER_ALIGNTO_SIZE 0x1000
  79. #define PHP_OUTPUT_HANDLER_DEFAULT_SIZE 0x4000
  80. typedef struct _php_output_buffer {
  81. char *data;
  82. size_t size;
  83. size_t used;
  84. uint free:1;
  85. uint _res:31;
  86. } php_output_buffer;
  87. typedef struct _php_output_context {
  88. int op;
  89. php_output_buffer in;
  90. php_output_buffer out;
  91. #ifdef ZTS
  92. void ***tsrm_ls;
  93. #endif
  94. } php_output_context;
  95. #define PHP_OUTPUT_TSRMLS(ctx) TSRMLS_FETCH_FROM_CTX((ctx)->tsrm_ls)
  96. /* old-style, stateless callback */
  97. typedef void (*php_output_handler_func_t)(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC);
  98. /* new-style, opaque context callback */
  99. typedef int (*php_output_handler_context_func_t)(void **handler_context, php_output_context *output_context);
  100. /* output handler context dtor */
  101. typedef void (*php_output_handler_context_dtor_t)(void *opaq TSRMLS_DC);
  102. /* conflict check callback */
  103. typedef int (*php_output_handler_conflict_check_t)(const char *handler_name, size_t handler_name_len TSRMLS_DC);
  104. /* ctor for aliases */
  105. typedef struct _php_output_handler *(*php_output_handler_alias_ctor_t)(const char *handler_name, size_t handler_name_len, size_t chunk_size, int flags TSRMLS_DC);
  106. typedef struct _php_output_handler_user_func_t {
  107. zend_fcall_info fci;
  108. zend_fcall_info_cache fcc;
  109. zval *zoh;
  110. } php_output_handler_user_func_t;
  111. typedef struct _php_output_handler {
  112. char *name;
  113. size_t name_len;
  114. int flags;
  115. int level;
  116. size_t size;
  117. php_output_buffer buffer;
  118. void *opaq;
  119. void (*dtor)(void *opaq TSRMLS_DC);
  120. union {
  121. php_output_handler_user_func_t *user;
  122. php_output_handler_context_func_t internal;
  123. } func;
  124. } php_output_handler;
  125. ZEND_BEGIN_MODULE_GLOBALS(output)
  126. int flags;
  127. zend_stack handlers;
  128. php_output_handler *active;
  129. php_output_handler *running;
  130. const char *output_start_filename;
  131. int output_start_lineno;
  132. ZEND_END_MODULE_GLOBALS(output)
  133. PHPAPI ZEND_EXTERN_MODULE_GLOBALS(output);
  134. /* there should not be a need to use OG() from outside of output.c */
  135. #ifdef ZTS
  136. # define OG(v) TSRMG(output_globals_id, zend_output_globals *, v)
  137. #else
  138. # define OG(v) (output_globals.v)
  139. #endif
  140. /* convenience macros */
  141. #define PHPWRITE(str, str_len) php_output_write((str), (str_len) TSRMLS_CC)
  142. #define PHPWRITE_H(str, str_len) php_output_write_unbuffered((str), (str_len) TSRMLS_CC)
  143. #define PUTC(c) (php_output_write(&(c), 1 TSRMLS_CC), (c))
  144. #define PUTC_H(c) (php_output_write_unbuffered(&(c), 1 TSRMLS_CC), (c))
  145. #define PUTS(str) do { \
  146. const char *__str = (str); \
  147. php_output_write(__str, strlen(__str) TSRMLS_CC); \
  148. } while (0)
  149. #define PUTS_H(str) do { \
  150. const char *__str = (str); \
  151. php_output_write_unbuffered(__str, strlen(__str) TSRMLS_CC); \
  152. } while (0)
  153. BEGIN_EXTERN_C()
  154. extern const char php_output_default_handler_name[sizeof("default output handler")];
  155. extern const char php_output_devnull_handler_name[sizeof("null output handler")];
  156. #define php_output_tearup() \
  157. php_output_startup(); \
  158. php_output_activate(TSRMLS_C)
  159. #define php_output_teardown() \
  160. php_output_end_all(TSRMLS_C); \
  161. php_output_deactivate(TSRMLS_C); \
  162. php_output_shutdown()
  163. /* MINIT */
  164. PHPAPI void php_output_startup(void);
  165. /* MSHUTDOWN */
  166. PHPAPI void php_output_shutdown(void);
  167. PHPAPI void php_output_register_constants(TSRMLS_D);
  168. /* RINIT */
  169. PHPAPI int php_output_activate(TSRMLS_D);
  170. /* RSHUTDOWN */
  171. PHPAPI void php_output_deactivate(TSRMLS_D);
  172. PHPAPI void php_output_set_status(int status TSRMLS_DC);
  173. PHPAPI int php_output_get_status(TSRMLS_D);
  174. PHPAPI void php_output_set_implicit_flush(int flush TSRMLS_DC);
  175. PHPAPI const char *php_output_get_start_filename(TSRMLS_D);
  176. PHPAPI int php_output_get_start_lineno(TSRMLS_D);
  177. PHPAPI int php_output_write_unbuffered(const char *str, size_t len TSRMLS_DC);
  178. PHPAPI int php_output_write(const char *str, size_t len TSRMLS_DC);
  179. PHPAPI int php_output_flush(TSRMLS_D);
  180. PHPAPI void php_output_flush_all(TSRMLS_D);
  181. PHPAPI int php_output_clean(TSRMLS_D);
  182. PHPAPI void php_output_clean_all(TSRMLS_D);
  183. PHPAPI int php_output_end(TSRMLS_D);
  184. PHPAPI void php_output_end_all(TSRMLS_D);
  185. PHPAPI int php_output_discard(TSRMLS_D);
  186. PHPAPI void php_output_discard_all(TSRMLS_D);
  187. PHPAPI int php_output_get_contents(zval *p TSRMLS_DC);
  188. PHPAPI int php_output_get_length(zval *p TSRMLS_DC);
  189. PHPAPI int php_output_get_level(TSRMLS_D);
  190. PHPAPI php_output_handler* php_output_get_active_handler(TSRMLS_D);
  191. PHPAPI int php_output_start_default(TSRMLS_D);
  192. PHPAPI int php_output_start_devnull(TSRMLS_D);
  193. PHPAPI int php_output_start_user(zval *output_handler, size_t chunk_size, int flags TSRMLS_DC);
  194. PHPAPI int php_output_start_internal(const char *name, size_t name_len, php_output_handler_func_t output_handler, size_t chunk_size, int flags TSRMLS_DC);
  195. PHPAPI php_output_handler *php_output_handler_create_user(zval *handler, size_t chunk_size, int flags TSRMLS_DC);
  196. PHPAPI php_output_handler *php_output_handler_create_internal(const char *name, size_t name_len, php_output_handler_context_func_t handler, size_t chunk_size, int flags TSRMLS_DC);
  197. PHPAPI void php_output_handler_set_context(php_output_handler *handler, void *opaq, void (*dtor)(void* TSRMLS_DC) TSRMLS_DC);
  198. PHPAPI int php_output_handler_start(php_output_handler *handler TSRMLS_DC);
  199. PHPAPI int php_output_handler_started(const char *name, size_t name_len TSRMLS_DC);
  200. PHPAPI int php_output_handler_hook(php_output_handler_hook_t type, void *arg TSRMLS_DC);
  201. PHPAPI void php_output_handler_dtor(php_output_handler *handler TSRMLS_DC);
  202. PHPAPI void php_output_handler_free(php_output_handler **handler TSRMLS_DC);
  203. PHPAPI int php_output_handler_conflict(const char *handler_new, size_t handler_new_len, const char *handler_set, size_t handler_set_len TSRMLS_DC);
  204. PHPAPI int php_output_handler_conflict_register(const char *handler_name, size_t handler_name_len, php_output_handler_conflict_check_t check_func TSRMLS_DC);
  205. PHPAPI int php_output_handler_reverse_conflict_register(const char *handler_name, size_t handler_name_len, php_output_handler_conflict_check_t check_func TSRMLS_DC);
  206. PHPAPI php_output_handler_alias_ctor_t *php_output_handler_alias(const char *handler_name, size_t handler_name_len TSRMLS_DC);
  207. PHPAPI int php_output_handler_alias_register(const char *handler_name, size_t handler_name_len, php_output_handler_alias_ctor_t func TSRMLS_DC);
  208. END_EXTERN_C()
  209. PHP_FUNCTION(ob_start);
  210. PHP_FUNCTION(ob_flush);
  211. PHP_FUNCTION(ob_clean);
  212. PHP_FUNCTION(ob_end_flush);
  213. PHP_FUNCTION(ob_end_clean);
  214. PHP_FUNCTION(ob_get_flush);
  215. PHP_FUNCTION(ob_get_clean);
  216. PHP_FUNCTION(ob_get_contents);
  217. PHP_FUNCTION(ob_get_length);
  218. PHP_FUNCTION(ob_get_level);
  219. PHP_FUNCTION(ob_get_status);
  220. PHP_FUNCTION(ob_implicit_flush);
  221. PHP_FUNCTION(ob_list_handlers);
  222. PHP_FUNCTION(output_add_rewrite_var);
  223. PHP_FUNCTION(output_reset_rewrite_vars);
  224. #endif
  225. /*
  226. * Local variables:
  227. * tab-width: 4
  228. * c-basic-offset: 4
  229. * End:
  230. * vim600: sw=4 ts=4 fdm=marker
  231. * vim<600: sw=4 ts=4
  232. */