1/*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
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
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 */
51typedef 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 */
75typedef 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
93typedef struct _php_output_buffer {
94	char *data;
95	size_t size;
96	size_t used;
97	uint free:1;
98	uint _reserved:31;
99} php_output_buffer;
100
101typedef 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 */
108typedef 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 */
110typedef int (*php_output_handler_context_func_t)(void **handler_context, php_output_context *output_context);
111/* output handler context dtor */
112typedef void (*php_output_handler_context_dtor_t)(void *opaq);
113/* conflict check callback */
114typedef int (*php_output_handler_conflict_check_t)(const char *handler_name, size_t handler_name_len);
115/* ctor for aliases */
116typedef 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
118typedef 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
124typedef 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
140ZEND_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;
147ZEND_END_MODULE_GLOBALS(output)
148
149PHPAPI 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), (c))
163#define PUTC_H(c)					(php_output_write_unbuffered((const char *) &(c), 1), (c))
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
175BEGIN_EXTERN_C()
176
177extern const char php_output_default_handler_name[sizeof("default output handler")];
178extern 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 */
189PHPAPI void php_output_startup(void);
190/* MSHUTDOWN */
191PHPAPI void php_output_shutdown(void);
192
193PHPAPI void php_output_register_constants(void);
194
195/* RINIT */
196PHPAPI int php_output_activate(void);
197/* RSHUTDOWN */
198PHPAPI void php_output_deactivate(void);
199
200PHPAPI void php_output_set_status(int status);
201PHPAPI int php_output_get_status(void);
202PHPAPI void php_output_set_implicit_flush(int flush);
203PHPAPI const char *php_output_get_start_filename(void);
204PHPAPI int php_output_get_start_lineno(void);
205
206PHPAPI size_t php_output_write_unbuffered(const char *str, size_t len);
207PHPAPI size_t php_output_write(const char *str, size_t len);
208
209PHPAPI int php_output_flush(void);
210PHPAPI void php_output_flush_all(void);
211PHPAPI int php_output_clean(void);
212PHPAPI void php_output_clean_all(void);
213PHPAPI int php_output_end(void);
214PHPAPI void php_output_end_all(void);
215PHPAPI int php_output_discard(void);
216PHPAPI void php_output_discard_all(void);
217
218PHPAPI int php_output_get_contents(zval *p);
219PHPAPI int php_output_get_length(zval *p);
220PHPAPI int php_output_get_level(void);
221PHPAPI php_output_handler* php_output_get_active_handler(void);
222
223PHPAPI int php_output_start_default(void);
224PHPAPI int php_output_start_devnull(void);
225
226PHPAPI int php_output_start_user(zval *output_handler, size_t chunk_size, int flags);
227PHPAPI 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
229PHPAPI php_output_handler *php_output_handler_create_user(zval *handler, size_t chunk_size, int flags);
230PHPAPI 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
232PHPAPI void php_output_handler_set_context(php_output_handler *handler, void *opaq, void (*dtor)(void*));
233PHPAPI int php_output_handler_start(php_output_handler *handler);
234PHPAPI int php_output_handler_started(const char *name, size_t name_len);
235PHPAPI int php_output_handler_hook(php_output_handler_hook_t type, void *arg);
236PHPAPI void php_output_handler_dtor(php_output_handler *handler);
237PHPAPI void php_output_handler_free(php_output_handler **handler);
238
239PHPAPI int php_output_handler_conflict(const char *handler_new, size_t handler_new_len, const char *handler_set, size_t handler_set_len);
240PHPAPI int php_output_handler_conflict_register(const char *handler_name, size_t handler_name_len, php_output_handler_conflict_check_t check_func);
241PHPAPI 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
243PHPAPI php_output_handler_alias_ctor_t php_output_handler_alias(const char *handler_name, size_t handler_name_len);
244PHPAPI int php_output_handler_alias_register(const char *handler_name, size_t handler_name_len, php_output_handler_alias_ctor_t func);
245
246END_EXTERN_C()
247
248
249PHP_FUNCTION(ob_start);
250PHP_FUNCTION(ob_flush);
251PHP_FUNCTION(ob_clean);
252PHP_FUNCTION(ob_end_flush);
253PHP_FUNCTION(ob_end_clean);
254PHP_FUNCTION(ob_get_flush);
255PHP_FUNCTION(ob_get_clean);
256PHP_FUNCTION(ob_get_contents);
257PHP_FUNCTION(ob_get_length);
258PHP_FUNCTION(ob_get_level);
259PHP_FUNCTION(ob_get_status);
260PHP_FUNCTION(ob_implicit_flush);
261PHP_FUNCTION(ob_list_handlers);
262
263PHP_FUNCTION(output_add_rewrite_var);
264PHP_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