php_output.h 9.3 KB

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