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