1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 #ifndef PHP_OUTPUT_H
22 #define PHP_OUTPUT_H
23
24 #define PHP_OUTPUT_NEWAPI 1
25
26
27 #define PHP_OUTPUT_HANDLER_WRITE 0x00
28 #define PHP_OUTPUT_HANDLER_START 0x01
29 #define PHP_OUTPUT_HANDLER_CLEAN 0x02
30 #define PHP_OUTPUT_HANDLER_FLUSH 0x04
31 #define PHP_OUTPUT_HANDLER_FINAL 0x08
32 #define PHP_OUTPUT_HANDLER_CONT PHP_OUTPUT_HANDLER_WRITE
33 #define PHP_OUTPUT_HANDLER_END PHP_OUTPUT_HANDLER_FINAL
34
35
36 #define PHP_OUTPUT_HANDLER_INTERNAL 0x0000
37 #define PHP_OUTPUT_HANDLER_USER 0x0001
38
39
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
46 #define PHP_OUTPUT_HANDLER_STARTED 0x1000
47 #define PHP_OUTPUT_HANDLER_DISABLED 0x2000
48 #define PHP_OUTPUT_HANDLER_PROCESSED 0x4000
49
50
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
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
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
69 #define PHP_OUTPUT_ACTIVE 0x10
70 #define PHP_OUTPUT_LOCKED 0x20
71
72 #define PHP_OUTPUT_ACTIVATED 0x100000
73
74
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
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 uint free:1;
98 uint _res: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 #ifdef ZTS
106 void ***tsrm_ls;
107 #endif
108 } php_output_context;
109
110 #define PHP_OUTPUT_TSRMLS(ctx) TSRMLS_FETCH_FROM_CTX((ctx)->tsrm_ls)
111
112
113 typedef void (*php_output_handler_func_t)(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC);
114
115 typedef int (*php_output_handler_context_func_t)(void **handler_context, php_output_context *output_context);
116
117 typedef void (*php_output_handler_context_dtor_t)(void *opaq TSRMLS_DC);
118
119 typedef int (*php_output_handler_conflict_check_t)(const char *handler_name, size_t handler_name_len TSRMLS_DC);
120
121 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);
122
123 typedef struct _php_output_handler_user_func_t {
124 zend_fcall_info fci;
125 zend_fcall_info_cache fcc;
126 zval *zoh;
127 } php_output_handler_user_func_t;
128
129 typedef struct _php_output_handler {
130 char *name;
131 size_t name_len;
132 int flags;
133 int level;
134 size_t size;
135 php_output_buffer buffer;
136
137 void *opaq;
138 void (*dtor)(void *opaq TSRMLS_DC);
139
140 union {
141 php_output_handler_user_func_t *user;
142 php_output_handler_context_func_t internal;
143 } func;
144 } php_output_handler;
145
146 ZEND_BEGIN_MODULE_GLOBALS(output)
147 int flags;
148 zend_stack handlers;
149 php_output_handler *active;
150 php_output_handler *running;
151 const char *output_start_filename;
152 int output_start_lineno;
153 ZEND_END_MODULE_GLOBALS(output)
154
155 PHPAPI ZEND_EXTERN_MODULE_GLOBALS(output);
156
157
158 #ifdef ZTS
159 # define OG(v) TSRMG(output_globals_id, zend_output_globals *, v)
160 #else
161 # define OG(v) (output_globals.v)
162 #endif
163
164
165 #define PHPWRITE(str, str_len) php_output_write((str), (str_len) TSRMLS_CC)
166 #define PHPWRITE_H(str, str_len) php_output_write_unbuffered((str), (str_len) TSRMLS_CC)
167
168 #define PUTC(c) (php_output_write(&(c), 1 TSRMLS_CC), (c))
169 #define PUTC_H(c) (php_output_write_unbuffered(&(c), 1 TSRMLS_CC), (c))
170
171 #define PUTS(str) do { \
172 const char *__str = (str); \
173 php_output_write(__str, strlen(__str) TSRMLS_CC); \
174 } while (0)
175 #define PUTS_H(str) do { \
176 const char *__str = (str); \
177 php_output_write_unbuffered(__str, strlen(__str) TSRMLS_CC); \
178 } while (0)
179
180
181 BEGIN_EXTERN_C()
182
183 extern const char php_output_default_handler_name[sizeof("default output handler")];
184 extern const char php_output_devnull_handler_name[sizeof("null output handler")];
185
186 #define php_output_tearup() \
187 php_output_startup(); \
188 php_output_activate(TSRMLS_C)
189 #define php_output_teardown() \
190 php_output_end_all(TSRMLS_C); \
191 php_output_deactivate(TSRMLS_C); \
192 php_output_shutdown()
193
194
195 PHPAPI void php_output_startup(void);
196
197 PHPAPI void php_output_shutdown(void);
198
199 PHPAPI void php_output_register_constants(TSRMLS_D);
200
201
202 PHPAPI int php_output_activate(TSRMLS_D);
203
204 PHPAPI void php_output_deactivate(TSRMLS_D);
205
206 PHPAPI void php_output_set_status(int status TSRMLS_DC);
207 PHPAPI int php_output_get_status(TSRMLS_D);
208 PHPAPI void php_output_set_implicit_flush(int flush TSRMLS_DC);
209 PHPAPI const char *php_output_get_start_filename(TSRMLS_D);
210 PHPAPI int php_output_get_start_lineno(TSRMLS_D);
211
212 PHPAPI int php_output_write_unbuffered(const char *str, size_t len TSRMLS_DC);
213 PHPAPI int php_output_write(const char *str, size_t len TSRMLS_DC);
214
215 PHPAPI int php_output_flush(TSRMLS_D);
216 PHPAPI void php_output_flush_all(TSRMLS_D);
217 PHPAPI int php_output_clean(TSRMLS_D);
218 PHPAPI void php_output_clean_all(TSRMLS_D);
219 PHPAPI int php_output_end(TSRMLS_D);
220 PHPAPI void php_output_end_all(TSRMLS_D);
221 PHPAPI int php_output_discard(TSRMLS_D);
222 PHPAPI void php_output_discard_all(TSRMLS_D);
223
224 PHPAPI int php_output_get_contents(zval *p TSRMLS_DC);
225 PHPAPI int php_output_get_length(zval *p TSRMLS_DC);
226 PHPAPI int php_output_get_level(TSRMLS_D);
227 PHPAPI php_output_handler* php_output_get_active_handler(TSRMLS_D);
228
229 PHPAPI int php_output_start_default(TSRMLS_D);
230 PHPAPI int php_output_start_devnull(TSRMLS_D);
231
232 PHPAPI int php_output_start_user(zval *output_handler, size_t chunk_size, int flags TSRMLS_DC);
233 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);
234
235 PHPAPI php_output_handler *php_output_handler_create_user(zval *handler, size_t chunk_size, int flags TSRMLS_DC);
236 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);
237
238 PHPAPI void php_output_handler_set_context(php_output_handler *handler, void *opaq, void (*dtor)(void* TSRMLS_DC) TSRMLS_DC);
239 PHPAPI int php_output_handler_start(php_output_handler *handler TSRMLS_DC);
240 PHPAPI int php_output_handler_started(const char *name, size_t name_len TSRMLS_DC);
241 PHPAPI int php_output_handler_hook(php_output_handler_hook_t type, void *arg TSRMLS_DC);
242 PHPAPI void php_output_handler_dtor(php_output_handler *handler TSRMLS_DC);
243 PHPAPI void php_output_handler_free(php_output_handler **handler TSRMLS_DC);
244
245 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);
246 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);
247 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);
248
249 PHPAPI php_output_handler_alias_ctor_t *php_output_handler_alias(const char *handler_name, size_t handler_name_len TSRMLS_DC);
250 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);
251
252 END_EXTERN_C()
253
254
255 PHP_FUNCTION(ob_start);
256 PHP_FUNCTION(ob_flush);
257 PHP_FUNCTION(ob_clean);
258 PHP_FUNCTION(ob_end_flush);
259 PHP_FUNCTION(ob_end_clean);
260 PHP_FUNCTION(ob_get_flush);
261 PHP_FUNCTION(ob_get_clean);
262 PHP_FUNCTION(ob_get_contents);
263 PHP_FUNCTION(ob_get_length);
264 PHP_FUNCTION(ob_get_level);
265 PHP_FUNCTION(ob_get_status);
266 PHP_FUNCTION(ob_implicit_flush);
267 PHP_FUNCTION(ob_list_handlers);
268
269 PHP_FUNCTION(output_add_rewrite_var);
270 PHP_FUNCTION(output_reset_rewrite_vars);
271
272 #endif
273
274
275
276
277
278
279
280
281