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 | http://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 | Authors: Zeev Suraski <zeev@php.net> |
14 | Thies C. Arntzen <thies@thieso.net> |
15 | Marcus Boerger <helly@php.net> |
16 | New API: Michael Wallner <mike@php.net> |
17 +----------------------------------------------------------------------+
18 */
19
20 #ifndef PHP_OUTPUT_DEBUG
21 # define PHP_OUTPUT_DEBUG 0
22 #endif
23 #ifndef PHP_OUTPUT_NOINLINE
24 # define PHP_OUTPUT_NOINLINE 0
25 #endif
26
27 #include "php.h"
28 #include "ext/standard/head.h"
29 #include "ext/standard/url_scanner_ex.h"
30 #include "SAPI.h"
31 #include "zend_stack.h"
32 #include "php_output.h"
33
34 PHPAPI ZEND_DECLARE_MODULE_GLOBALS(output)
35
36 const char php_output_default_handler_name[sizeof("default output handler")] = "default output handler";
37 const char php_output_devnull_handler_name[sizeof("null output handler")] = "null output handler";
38
39 #if PHP_OUTPUT_NOINLINE || PHP_OUTPUT_DEBUG
40 # undef inline
41 # define inline
42 #endif
43
44 /* {{{ aliases, conflict and reverse conflict hash tables */
45 static HashTable php_output_handler_aliases;
46 static HashTable php_output_handler_conflicts;
47 static HashTable php_output_handler_reverse_conflicts;
48 /* }}} */
49
50 /* {{{ forward declarations */
51 static inline int php_output_lock_error(int op);
52 static inline void php_output_op(int op, const char *str, size_t len);
53
54 static inline php_output_handler *php_output_handler_init(zend_string *name, size_t chunk_size, int flags);
55 static inline php_output_handler_status_t php_output_handler_op(php_output_handler *handler, php_output_context *context);
56 static inline int php_output_handler_append(php_output_handler *handler, const php_output_buffer *buf);
57 static inline zval *php_output_handler_status(php_output_handler *handler, zval *entry);
58
59 static inline void php_output_context_init(php_output_context *context, int op);
60 static inline void php_output_context_reset(php_output_context *context);
61 static inline void php_output_context_swap(php_output_context *context);
62 static inline void php_output_context_dtor(php_output_context *context);
63
64 static int php_output_stack_pop(int flags);
65
66 static int php_output_stack_apply_op(void *h, void *c);
67 static int php_output_stack_apply_clean(void *h, void *c);
68 static int php_output_stack_apply_list(void *h, void *z);
69 static int php_output_stack_apply_status(void *h, void *z);
70
71 static int php_output_handler_compat_func(void **handler_context, php_output_context *output_context);
72 static int php_output_handler_default_func(void **handler_context, php_output_context *output_context);
73 static int php_output_handler_devnull_func(void **handler_context, php_output_context *output_context);
74 /* }}} */
75
76 /* {{{ static void php_output_init_globals(zend_output_globals *G)
77 * Initialize the module globals on MINIT */
php_output_init_globals(zend_output_globals * G)78 static inline void php_output_init_globals(zend_output_globals *G)
79 {
80 memset(G, 0, sizeof(*G));
81 }
82 /* }}} */
83
84 /* {{{ stderr/stdout writer if not PHP_OUTPUT_ACTIVATED */
php_output_stdout(const char * str,size_t str_len)85 static size_t php_output_stdout(const char *str, size_t str_len)
86 {
87 fwrite(str, 1, str_len, stdout);
88 return str_len;
89 }
php_output_stderr(const char * str,size_t str_len)90 static size_t php_output_stderr(const char *str, size_t str_len)
91 {
92 fwrite(str, 1, str_len, stderr);
93 /* See http://support.microsoft.com/kb/190351 */
94 #ifdef PHP_WIN32
95 fflush(stderr);
96 #endif
97 return str_len;
98 }
99 static size_t (*php_output_direct)(const char *str, size_t str_len) = php_output_stderr;
100 /* }}} */
101
102 /* {{{ void php_output_header(void) */
php_output_header(void)103 static void php_output_header(void)
104 {
105 if (!SG(headers_sent)) {
106 if (!OG(output_start_filename_str)) {
107 if (zend_is_compiling()) {
108 OG(output_start_filename_str) = zend_get_compiled_filename();
109 OG(output_start_lineno) = zend_get_compiled_lineno();
110 } else if (zend_is_executing()) {
111 OG(output_start_filename_str) = zend_get_executed_filename_ex();
112 OG(output_start_lineno) = zend_get_executed_lineno();
113 }
114 if (OG(output_start_filename_str)) {
115 zend_string_addref(OG(output_start_filename_str));
116 }
117 #if PHP_OUTPUT_DEBUG
118 fprintf(stderr, "!!! output started at: %s (%d)\n",
119 ZSTR_VAL(OG(output_start_filename_str)), OG(output_start_lineno));
120 #endif
121 }
122 if (!php_header()) {
123 OG(flags) |= PHP_OUTPUT_DISABLED;
124 }
125 }
126 }
127 /* }}} */
128
reverse_conflict_dtor(zval * zv)129 static void reverse_conflict_dtor(zval *zv)
130 {
131 HashTable *ht = Z_PTR_P(zv);
132 zend_hash_destroy(ht);
133 }
134
135 /* {{{ void php_output_startup(void)
136 * Set up module globals and initialize the conflict and reverse conflict hash tables */
php_output_startup(void)137 PHPAPI void php_output_startup(void)
138 {
139 ZEND_INIT_MODULE_GLOBALS(output, php_output_init_globals, NULL);
140 zend_hash_init(&php_output_handler_aliases, 8, NULL, NULL, 1);
141 zend_hash_init(&php_output_handler_conflicts, 8, NULL, NULL, 1);
142 zend_hash_init(&php_output_handler_reverse_conflicts, 8, NULL, reverse_conflict_dtor, 1);
143 php_output_direct = php_output_stdout;
144 }
145 /* }}} */
146
147 /* {{{ void php_output_shutdown(void)
148 * Destroy module globals and the conflict and reverse conflict hash tables */
php_output_shutdown(void)149 PHPAPI void php_output_shutdown(void)
150 {
151 php_output_direct = php_output_stderr;
152 zend_hash_destroy(&php_output_handler_aliases);
153 zend_hash_destroy(&php_output_handler_conflicts);
154 zend_hash_destroy(&php_output_handler_reverse_conflicts);
155 }
156 /* }}} */
157
158 /* {{{ SUCCESS|FAILURE php_output_activate(void)
159 * Reset output globals and setup the output handler stack */
php_output_activate(void)160 PHPAPI int php_output_activate(void)
161 {
162 #ifdef ZTS
163 memset(TSRMG_BULK_STATIC(output_globals_id, zend_output_globals*), 0, sizeof(zend_output_globals));
164 #else
165 memset(&output_globals, 0, sizeof(zend_output_globals));
166 #endif
167
168 zend_stack_init(&OG(handlers), sizeof(php_output_handler *));
169 OG(flags) |= PHP_OUTPUT_ACTIVATED;
170
171 return SUCCESS;
172 }
173 /* }}} */
174
175 /* {{{ void php_output_deactivate(void)
176 * Destroy the output handler stack */
php_output_deactivate(void)177 PHPAPI void php_output_deactivate(void)
178 {
179 php_output_handler **handler = NULL;
180
181 if ((OG(flags) & PHP_OUTPUT_ACTIVATED)) {
182 php_output_header();
183
184 OG(flags) ^= PHP_OUTPUT_ACTIVATED;
185 OG(active) = NULL;
186 OG(running) = NULL;
187
188 /* release all output handlers */
189 if (OG(handlers).elements) {
190 while ((handler = zend_stack_top(&OG(handlers)))) {
191 php_output_handler_free(handler);
192 zend_stack_del_top(&OG(handlers));
193 }
194 }
195 zend_stack_destroy(&OG(handlers));
196 }
197
198 if (OG(output_start_filename_str)) {
199 zend_string_release(OG(output_start_filename_str));
200 OG(output_start_filename_str) = NULL;
201 }
202 }
203 /* }}} */
204
205 /* {{{ void php_output_register_constants() */
php_output_register_constants(void)206 PHPAPI void php_output_register_constants(void)
207 {
208 REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_START", PHP_OUTPUT_HANDLER_START, CONST_CS | CONST_PERSISTENT);
209 REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_WRITE", PHP_OUTPUT_HANDLER_WRITE, CONST_CS | CONST_PERSISTENT);
210 REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_FLUSH", PHP_OUTPUT_HANDLER_FLUSH, CONST_CS | CONST_PERSISTENT);
211 REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_CLEAN", PHP_OUTPUT_HANDLER_CLEAN, CONST_CS | CONST_PERSISTENT);
212 REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_FINAL", PHP_OUTPUT_HANDLER_FINAL, CONST_CS | CONST_PERSISTENT);
213 REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_CONT", PHP_OUTPUT_HANDLER_WRITE, CONST_CS | CONST_PERSISTENT);
214 REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_END", PHP_OUTPUT_HANDLER_FINAL, CONST_CS | CONST_PERSISTENT);
215
216 REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_CLEANABLE", PHP_OUTPUT_HANDLER_CLEANABLE, CONST_CS | CONST_PERSISTENT);
217 REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_FLUSHABLE", PHP_OUTPUT_HANDLER_FLUSHABLE, CONST_CS | CONST_PERSISTENT);
218 REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_REMOVABLE", PHP_OUTPUT_HANDLER_REMOVABLE, CONST_CS | CONST_PERSISTENT);
219 REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_STDFLAGS", PHP_OUTPUT_HANDLER_STDFLAGS, CONST_CS | CONST_PERSISTENT);
220 REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_STARTED", PHP_OUTPUT_HANDLER_STARTED, CONST_CS | CONST_PERSISTENT);
221 REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_DISABLED", PHP_OUTPUT_HANDLER_DISABLED, CONST_CS | CONST_PERSISTENT);
222 }
223 /* }}} */
224
225 /* {{{ void php_output_set_status(int status)
226 * Used by SAPIs to disable output */
php_output_set_status(int status)227 PHPAPI void php_output_set_status(int status)
228 {
229 OG(flags) = (OG(flags) & ~0xf) | (status & 0xf);
230 }
231 /* }}} */
232
233 /* {{{ int php_output_get_status()
234 * Get output control status */
php_output_get_status(void)235 PHPAPI int php_output_get_status(void)
236 {
237 return (
238 OG(flags)
239 | (OG(active) ? PHP_OUTPUT_ACTIVE : 0)
240 | (OG(running)? PHP_OUTPUT_LOCKED : 0)
241 ) & 0xff;
242 }
243 /* }}} */
244
245 /* {{{ int php_output_write_unbuffered(const char *str, size_t len)
246 * Unbuffered write */
php_output_write_unbuffered(const char * str,size_t len)247 PHPAPI size_t php_output_write_unbuffered(const char *str, size_t len)
248 {
249 if (OG(flags) & PHP_OUTPUT_ACTIVATED) {
250 return sapi_module.ub_write(str, len);
251 }
252 return php_output_direct(str, len);
253 }
254 /* }}} */
255
256 /* {{{ int php_output_write(const char *str, size_t len)
257 * Buffered write */
php_output_write(const char * str,size_t len)258 PHPAPI size_t php_output_write(const char *str, size_t len)
259 {
260 if (OG(flags) & PHP_OUTPUT_ACTIVATED) {
261 php_output_op(PHP_OUTPUT_HANDLER_WRITE, str, len);
262 return len;
263 }
264 if (OG(flags) & PHP_OUTPUT_DISABLED) {
265 return 0;
266 }
267 return php_output_direct(str, len);
268 }
269 /* }}} */
270
271 /* {{{ SUCCESS|FAILURE php_output_flush(void)
272 * Flush the most recent output handlers buffer */
php_output_flush(void)273 PHPAPI int php_output_flush(void)
274 {
275 php_output_context context;
276
277 if (OG(active) && (OG(active)->flags & PHP_OUTPUT_HANDLER_FLUSHABLE)) {
278 php_output_context_init(&context, PHP_OUTPUT_HANDLER_FLUSH);
279 php_output_handler_op(OG(active), &context);
280 if (context.out.data && context.out.used) {
281 zend_stack_del_top(&OG(handlers));
282 php_output_write(context.out.data, context.out.used);
283 zend_stack_push(&OG(handlers), &OG(active));
284 }
285 php_output_context_dtor(&context);
286 return SUCCESS;
287 }
288 return FAILURE;
289 }
290 /* }}} */
291
292 /* {{{ void php_output_flush_all()
293 * Flush all output buffers subsequently */
php_output_flush_all(void)294 PHPAPI void php_output_flush_all(void)
295 {
296 if (OG(active)) {
297 php_output_op(PHP_OUTPUT_HANDLER_FLUSH, NULL, 0);
298 }
299 }
300 /* }}} */
301
302 /* {{{ SUCCESS|FAILURE php_output_clean(void)
303 * Cleans the most recent output handlers buffer if the handler is cleanable */
php_output_clean(void)304 PHPAPI int php_output_clean(void)
305 {
306 php_output_context context;
307
308 if (OG(active) && (OG(active)->flags & PHP_OUTPUT_HANDLER_CLEANABLE)) {
309 php_output_context_init(&context, PHP_OUTPUT_HANDLER_CLEAN);
310 php_output_handler_op(OG(active), &context);
311 php_output_context_dtor(&context);
312 return SUCCESS;
313 }
314 return FAILURE;
315 }
316 /* }}} */
317
318 /* {{{ void php_output_clean_all(void)
319 * Cleans all output handler buffers, without regard whether the handler is cleanable */
php_output_clean_all(void)320 PHPAPI void php_output_clean_all(void)
321 {
322 php_output_context context;
323
324 if (OG(active)) {
325 php_output_context_init(&context, PHP_OUTPUT_HANDLER_CLEAN);
326 zend_stack_apply_with_argument(&OG(handlers), ZEND_STACK_APPLY_TOPDOWN, php_output_stack_apply_clean, &context);
327 }
328 }
329
330 /* {{{ SUCCESS|FAILURE php_output_end(void)
331 * Finalizes the most recent output handler at pops it off the stack if the handler is removable */
php_output_end(void)332 PHPAPI int php_output_end(void)
333 {
334 if (php_output_stack_pop(PHP_OUTPUT_POP_TRY)) {
335 return SUCCESS;
336 }
337 return FAILURE;
338 }
339 /* }}} */
340
341 /* {{{ void php_output_end_all(void)
342 * Finalizes all output handlers and ends output buffering without regard whether a handler is removable */
php_output_end_all(void)343 PHPAPI void php_output_end_all(void)
344 {
345 while (OG(active) && php_output_stack_pop(PHP_OUTPUT_POP_FORCE));
346 }
347 /* }}} */
348
349 /* {{{ SUCCESS|FAILURE php_output_discard(void)
350 * Discards the most recent output handlers buffer and pops it off the stack if the handler is removable */
php_output_discard(void)351 PHPAPI int php_output_discard(void)
352 {
353 if (php_output_stack_pop(PHP_OUTPUT_POP_DISCARD|PHP_OUTPUT_POP_TRY)) {
354 return SUCCESS;
355 }
356 return FAILURE;
357 }
358 /* }}} */
359
360 /* {{{ void php_output_discard_all(void)
361 * Discard all output handlers and buffers without regard whether a handler is removable */
php_output_discard_all(void)362 PHPAPI void php_output_discard_all(void)
363 {
364 while (OG(active)) {
365 php_output_stack_pop(PHP_OUTPUT_POP_DISCARD|PHP_OUTPUT_POP_FORCE);
366 }
367 }
368 /* }}} */
369
370 /* {{{ int php_output_get_level(void)
371 * Get output buffering level, ie. how many output handlers the stack contains */
php_output_get_level(void)372 PHPAPI int php_output_get_level(void)
373 {
374 return OG(active) ? zend_stack_count(&OG(handlers)) : 0;
375 }
376 /* }}} */
377
378 /* {{{ SUCCESS|FAILURE php_output_get_contents(zval *z)
379 * Get the contents of the active output handlers buffer */
php_output_get_contents(zval * p)380 PHPAPI int php_output_get_contents(zval *p)
381 {
382 if (OG(active)) {
383 ZVAL_STRINGL(p, OG(active)->buffer.data, OG(active)->buffer.used);
384 return SUCCESS;
385 } else {
386 ZVAL_NULL(p);
387 return FAILURE;
388 }
389 }
390
391 /* {{{ SUCCESS|FAILURE php_output_get_length(zval *z)
392 * Get the length of the active output handlers buffer */
php_output_get_length(zval * p)393 PHPAPI int php_output_get_length(zval *p)
394 {
395 if (OG(active)) {
396 ZVAL_LONG(p, OG(active)->buffer.used);
397 return SUCCESS;
398 } else {
399 ZVAL_NULL(p);
400 return FAILURE;
401 }
402 }
403 /* }}} */
404
405 /* {{{ php_output_handler* php_output_get_active_handler(void)
406 * Get active output handler */
php_output_get_active_handler(void)407 PHPAPI php_output_handler* php_output_get_active_handler(void)
408 {
409 return OG(active);
410 }
411 /* }}} */
412
413 /* {{{ SUCCESS|FAILURE php_output_handler_start_default(void)
414 * Start a "default output handler" */
php_output_start_default(void)415 PHPAPI int php_output_start_default(void)
416 {
417 php_output_handler *handler;
418
419 handler = php_output_handler_create_internal(ZEND_STRL(php_output_default_handler_name), php_output_handler_default_func, 0, PHP_OUTPUT_HANDLER_STDFLAGS);
420 if (SUCCESS == php_output_handler_start(handler)) {
421 return SUCCESS;
422 }
423 php_output_handler_free(&handler);
424 return FAILURE;
425 }
426 /* }}} */
427
428 /* {{{ SUCCESS|FAILURE php_output_handler_start_devnull(void)
429 * Start a "null output handler" */
php_output_start_devnull(void)430 PHPAPI int php_output_start_devnull(void)
431 {
432 php_output_handler *handler;
433
434 handler = php_output_handler_create_internal(ZEND_STRL(php_output_devnull_handler_name), php_output_handler_devnull_func, PHP_OUTPUT_HANDLER_DEFAULT_SIZE, 0);
435 if (SUCCESS == php_output_handler_start(handler)) {
436 return SUCCESS;
437 }
438 php_output_handler_free(&handler);
439 return FAILURE;
440 }
441 /* }}} */
442
443 /* {{{ SUCCESS|FAILURE php_output_start_user(zval *handler, size_t chunk_size, int flags)
444 * Start a user level output handler */
php_output_start_user(zval * output_handler,size_t chunk_size,int flags)445 PHPAPI int php_output_start_user(zval *output_handler, size_t chunk_size, int flags)
446 {
447 php_output_handler *handler;
448
449 if (output_handler) {
450 handler = php_output_handler_create_user(output_handler, chunk_size, flags);
451 } else {
452 handler = php_output_handler_create_internal(ZEND_STRL(php_output_default_handler_name), php_output_handler_default_func, chunk_size, flags);
453 }
454 if (SUCCESS == php_output_handler_start(handler)) {
455 return SUCCESS;
456 }
457 php_output_handler_free(&handler);
458 return FAILURE;
459 }
460 /* }}} */
461
462 /* {{{ SUCCESS|FAILURE php_output_start_internal(zval *name, php_output_handler_func_t handler, size_t chunk_size, int flags)
463 * Start an internal output handler that does not have to maintain a non-global state */
php_output_start_internal(const char * name,size_t name_len,php_output_handler_func_t output_handler,size_t chunk_size,int flags)464 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)
465 {
466 php_output_handler *handler;
467
468 handler = php_output_handler_create_internal(name, name_len, php_output_handler_compat_func, chunk_size, flags);
469 php_output_handler_set_context(handler, output_handler, NULL);
470 if (SUCCESS == php_output_handler_start(handler)) {
471 return SUCCESS;
472 }
473 php_output_handler_free(&handler);
474 return FAILURE;
475 }
476 /* }}} */
477
478 /* {{{ php_output_handler *php_output_handler_create_user(zval *handler, size_t chunk_size, int flags)
479 * Create a user level output handler */
php_output_handler_create_user(zval * output_handler,size_t chunk_size,int flags)480 PHPAPI php_output_handler *php_output_handler_create_user(zval *output_handler, size_t chunk_size, int flags)
481 {
482 zend_string *handler_name = NULL;
483 char *error = NULL;
484 php_output_handler *handler = NULL;
485 php_output_handler_alias_ctor_t alias = NULL;
486 php_output_handler_user_func_t *user = NULL;
487
488 switch (Z_TYPE_P(output_handler)) {
489 case IS_NULL:
490 handler = php_output_handler_create_internal(ZEND_STRL(php_output_default_handler_name), php_output_handler_default_func, chunk_size, flags);
491 break;
492 case IS_STRING:
493 if (Z_STRLEN_P(output_handler) && (alias = php_output_handler_alias(Z_STRVAL_P(output_handler), Z_STRLEN_P(output_handler)))) {
494 handler = alias(Z_STRVAL_P(output_handler), Z_STRLEN_P(output_handler), chunk_size, flags);
495 break;
496 }
497 default:
498 user = ecalloc(1, sizeof(php_output_handler_user_func_t));
499 if (SUCCESS == zend_fcall_info_init(output_handler, 0, &user->fci, &user->fcc, &handler_name, &error)) {
500 handler = php_output_handler_init(handler_name, chunk_size, (flags & ~0xf) | PHP_OUTPUT_HANDLER_USER);
501 ZVAL_COPY(&user->zoh, output_handler);
502 handler->func.user = user;
503 } else {
504 efree(user);
505 }
506 if (error) {
507 php_error_docref("ref.outcontrol", E_WARNING, "%s", error);
508 efree(error);
509 }
510 if (handler_name) {
511 zend_string_release_ex(handler_name, 0);
512 }
513 }
514
515 return handler;
516 }
517 /* }}} */
518
519 /* {{{ php_output_handler *php_output_handler_create_internal(zval *name, php_output_handler_context_func_t handler, size_t chunk_size, int flags)
520 * Create an internal output handler that can maintain a non-global state */
php_output_handler_create_internal(const char * name,size_t name_len,php_output_handler_context_func_t output_handler,size_t chunk_size,int flags)521 PHPAPI php_output_handler *php_output_handler_create_internal(const char *name, size_t name_len, php_output_handler_context_func_t output_handler, size_t chunk_size, int flags)
522 {
523 php_output_handler *handler;
524 zend_string *str = zend_string_init(name, name_len, 0);
525
526 handler = php_output_handler_init(str, chunk_size, (flags & ~0xf) | PHP_OUTPUT_HANDLER_INTERNAL);
527 handler->func.internal = output_handler;
528 zend_string_release_ex(str, 0);
529
530 return handler;
531 }
532 /* }}} */
533
534 /* {{{ void php_output_handler_set_context(php_output_handler *handler, void *opaq, void (*dtor)(void*))
535 * Set the context/state of an output handler. Calls the dtor of the previous context if there is one */
php_output_handler_set_context(php_output_handler * handler,void * opaq,void (* dtor)(void *))536 PHPAPI void php_output_handler_set_context(php_output_handler *handler, void *opaq, void (*dtor)(void*))
537 {
538 if (handler->dtor && handler->opaq) {
539 handler->dtor(handler->opaq);
540 }
541 handler->dtor = dtor;
542 handler->opaq = opaq;
543 }
544 /* }}} */
545
546 /* {{{ SUCCESS|FAILURE php_output_handler_start(php_output_handler *handler)
547 * Starts the set up output handler and pushes it on top of the stack. Checks for any conflicts regarding the output handler to start */
php_output_handler_start(php_output_handler * handler)548 PHPAPI int php_output_handler_start(php_output_handler *handler)
549 {
550 HashTable *rconflicts;
551 php_output_handler_conflict_check_t conflict;
552
553 if (php_output_lock_error(PHP_OUTPUT_HANDLER_START) || !handler) {
554 return FAILURE;
555 }
556 if (NULL != (conflict = zend_hash_find_ptr(&php_output_handler_conflicts, handler->name))) {
557 if (SUCCESS != conflict(ZSTR_VAL(handler->name), ZSTR_LEN(handler->name))) {
558 return FAILURE;
559 }
560 }
561 if (NULL != (rconflicts = zend_hash_find_ptr(&php_output_handler_reverse_conflicts, handler->name))) {
562 ZEND_HASH_FOREACH_PTR(rconflicts, conflict) {
563 if (SUCCESS != conflict(ZSTR_VAL(handler->name), ZSTR_LEN(handler->name))) {
564 return FAILURE;
565 }
566 } ZEND_HASH_FOREACH_END();
567 }
568 /* zend_stack_push returns stack level */
569 handler->level = zend_stack_push(&OG(handlers), &handler);
570 OG(active) = handler;
571 return SUCCESS;
572 }
573 /* }}} */
574
575 /* {{{ int php_output_handler_started(zval *name)
576 * Check whether a certain output handler is in use */
php_output_handler_started(const char * name,size_t name_len)577 PHPAPI int php_output_handler_started(const char *name, size_t name_len)
578 {
579 php_output_handler **handlers;
580 int i, count = php_output_get_level();
581
582 if (count) {
583 handlers = (php_output_handler **) zend_stack_base(&OG(handlers));
584
585 for (i = 0; i < count; ++i) {
586 if (name_len == ZSTR_LEN(handlers[i]->name) && !memcmp(ZSTR_VAL(handlers[i]->name), name, name_len)) {
587 return 1;
588 }
589 }
590 }
591
592 return 0;
593 }
594 /* }}} */
595
596 /* {{{ int php_output_handler_conflict(zval *handler_new, zval *handler_old)
597 * Check whether a certain handler is in use and issue a warning that the new handler would conflict with the already used one */
php_output_handler_conflict(const char * handler_new,size_t handler_new_len,const char * handler_set,size_t handler_set_len)598 PHPAPI int php_output_handler_conflict(const char *handler_new, size_t handler_new_len, const char *handler_set, size_t handler_set_len)
599 {
600 if (php_output_handler_started(handler_set, handler_set_len)) {
601 if (handler_new_len != handler_set_len || memcmp(handler_new, handler_set, handler_set_len)) {
602 php_error_docref("ref.outcontrol", E_WARNING, "Output handler '%s' conflicts with '%s'", handler_new, handler_set);
603 } else {
604 php_error_docref("ref.outcontrol", E_WARNING, "Output handler '%s' cannot be used twice", handler_new);
605 }
606 return 1;
607 }
608 return 0;
609 }
610 /* }}} */
611
612 /* {{{ SUCCESS|FAILURE php_output_handler_conflict_register(zval *name, php_output_handler_conflict_check_t check_func)
613 * Register a conflict checking function on MINIT */
php_output_handler_conflict_register(const char * name,size_t name_len,php_output_handler_conflict_check_t check_func)614 PHPAPI int php_output_handler_conflict_register(const char *name, size_t name_len, php_output_handler_conflict_check_t check_func)
615 {
616 zend_string *str;
617
618 if (!EG(current_module)) {
619 zend_error(E_ERROR, "Cannot register an output handler conflict outside of MINIT");
620 return FAILURE;
621 }
622 str = zend_string_init_interned(name, name_len, 1);
623 zend_hash_update_ptr(&php_output_handler_conflicts, str, check_func);
624 zend_string_release_ex(str, 1);
625 return SUCCESS;
626 }
627 /* }}} */
628
629 /* {{{ SUCCESS|FAILURE php_output_handler_reverse_conflict_register(zval *name, php_output_handler_conflict_check_t check_func)
630 * Register a reverse conflict checking function on MINIT */
php_output_handler_reverse_conflict_register(const char * name,size_t name_len,php_output_handler_conflict_check_t check_func)631 PHPAPI int php_output_handler_reverse_conflict_register(const char *name, size_t name_len, php_output_handler_conflict_check_t check_func)
632 {
633 HashTable rev, *rev_ptr = NULL;
634
635 if (!EG(current_module)) {
636 zend_error(E_ERROR, "Cannot register a reverse output handler conflict outside of MINIT");
637 return FAILURE;
638 }
639
640 if (NULL != (rev_ptr = zend_hash_str_find_ptr(&php_output_handler_reverse_conflicts, name, name_len))) {
641 return zend_hash_next_index_insert_ptr(rev_ptr, check_func) ? SUCCESS : FAILURE;
642 } else {
643 zend_string *str;
644
645 zend_hash_init(&rev, 8, NULL, NULL, 1);
646 if (NULL == zend_hash_next_index_insert_ptr(&rev, check_func)) {
647 zend_hash_destroy(&rev);
648 return FAILURE;
649 }
650 str = zend_string_init_interned(name, name_len, 1);
651 zend_hash_update_mem(&php_output_handler_reverse_conflicts, str, &rev, sizeof(HashTable));
652 zend_string_release_ex(str, 1);
653 return SUCCESS;
654 }
655 }
656 /* }}} */
657
658 /* {{{ php_output_handler_alias_ctor_t php_output_handler_alias(zval *name)
659 * Get an internal output handler for a user handler if it exists */
php_output_handler_alias(const char * name,size_t name_len)660 PHPAPI php_output_handler_alias_ctor_t php_output_handler_alias(const char *name, size_t name_len)
661 {
662 return zend_hash_str_find_ptr(&php_output_handler_aliases, name, name_len);
663 }
664 /* }}} */
665
666 /* {{{ SUCCESS|FAILURE php_output_handler_alias_register(zval *name, php_output_handler_alias_ctor_t func)
667 * Registers an internal output handler as alias for a user handler */
php_output_handler_alias_register(const char * name,size_t name_len,php_output_handler_alias_ctor_t func)668 PHPAPI int php_output_handler_alias_register(const char *name, size_t name_len, php_output_handler_alias_ctor_t func)
669 {
670 zend_string *str;
671
672 if (!EG(current_module)) {
673 zend_error(E_ERROR, "Cannot register an output handler alias outside of MINIT");
674 return FAILURE;
675 }
676 str = zend_string_init_interned(name, name_len, 1);
677 zend_hash_update_ptr(&php_output_handler_aliases, str, func);
678 zend_string_release_ex(str, 1);
679 return SUCCESS;
680 }
681 /* }}} */
682
683 /* {{{ SUCCESS|FAILURE php_output_handler_hook(php_output_handler_hook_t type, void *arg)
684 * Output handler hook for output handler functions to check/modify the current handlers abilities */
php_output_handler_hook(php_output_handler_hook_t type,void * arg)685 PHPAPI int php_output_handler_hook(php_output_handler_hook_t type, void *arg)
686 {
687 if (OG(running)) {
688 switch (type) {
689 case PHP_OUTPUT_HANDLER_HOOK_GET_OPAQ:
690 *(void ***) arg = &OG(running)->opaq;
691 return SUCCESS;
692 case PHP_OUTPUT_HANDLER_HOOK_GET_FLAGS:
693 *(int *) arg = OG(running)->flags;
694 return SUCCESS;
695 case PHP_OUTPUT_HANDLER_HOOK_GET_LEVEL:
696 *(int *) arg = OG(running)->level;
697 return SUCCESS;
698 case PHP_OUTPUT_HANDLER_HOOK_IMMUTABLE:
699 OG(running)->flags &= ~(PHP_OUTPUT_HANDLER_REMOVABLE|PHP_OUTPUT_HANDLER_CLEANABLE);
700 return SUCCESS;
701 case PHP_OUTPUT_HANDLER_HOOK_DISABLE:
702 OG(running)->flags |= PHP_OUTPUT_HANDLER_DISABLED;
703 return SUCCESS;
704 default:
705 break;
706 }
707 }
708 return FAILURE;
709 }
710 /* }}} */
711
712 /* {{{ void php_output_handler_dtor(php_output_handler *handler)
713 * Destroy an output handler */
php_output_handler_dtor(php_output_handler * handler)714 PHPAPI void php_output_handler_dtor(php_output_handler *handler)
715 {
716 if (handler->name) {
717 zend_string_release_ex(handler->name, 0);
718 }
719 if (handler->buffer.data) {
720 efree(handler->buffer.data);
721 }
722 if (handler->flags & PHP_OUTPUT_HANDLER_USER) {
723 zval_ptr_dtor(&handler->func.user->zoh);
724 efree(handler->func.user);
725 }
726 if (handler->dtor && handler->opaq) {
727 handler->dtor(handler->opaq);
728 }
729 memset(handler, 0, sizeof(*handler));
730 }
731 /* }}} */
732
733 /* {{{ void php_output_handler_free(php_output_handler **handler)
734 * Destroy and free an output handler */
php_output_handler_free(php_output_handler ** h)735 PHPAPI void php_output_handler_free(php_output_handler **h)
736 {
737 if (*h) {
738 php_output_handler_dtor(*h);
739 efree(*h);
740 *h = NULL;
741 }
742 }
743 /* }}} */
744
745 /* void php_output_set_implicit_flush(int enabled)
746 * Enable or disable implicit flush */
php_output_set_implicit_flush(int flush)747 PHPAPI void php_output_set_implicit_flush(int flush)
748 {
749 if (flush) {
750 OG(flags) |= PHP_OUTPUT_IMPLICITFLUSH;
751 } else {
752 OG(flags) &= ~PHP_OUTPUT_IMPLICITFLUSH;
753 }
754 }
755 /* }}} */
756
757 /* {{{ char *php_output_get_start_filename(void)
758 * Get the file name where output has started */
php_output_get_start_filename(void)759 PHPAPI const char *php_output_get_start_filename(void)
760 {
761 return OG(output_start_filename_str) ? ZSTR_VAL(OG(output_start_filename_str)) : NULL;
762 }
763 /* }}} */
764
765 /* {{{ int php_output_get_start_lineno(void)
766 * Get the line number where output has started */
php_output_get_start_lineno(void)767 PHPAPI int php_output_get_start_lineno(void)
768 {
769 return OG(output_start_lineno);
770 }
771 /* }}} */
772
773 /* {{{ static int php_output_lock_error(int op)
774 * Checks whether an unallowed operation is attempted from within the output handler and issues a fatal error */
php_output_lock_error(int op)775 static inline int php_output_lock_error(int op)
776 {
777 /* if there's no ob active, ob has been stopped */
778 if (op && OG(active) && OG(running)) {
779 /* fatal error */
780 php_output_deactivate();
781 php_error_docref("ref.outcontrol", E_ERROR, "Cannot use output buffering in output buffering display handlers");
782 return 1;
783 }
784 return 0;
785 }
786 /* }}} */
787
788 /* {{{ static php_output_context *php_output_context_init(php_output_context *context, int op)
789 * Initialize a new output context */
php_output_context_init(php_output_context * context,int op)790 static inline void php_output_context_init(php_output_context *context, int op)
791 {
792 memset(context, 0, sizeof(php_output_context));
793 context->op = op;
794 }
795 /* }}} */
796
797 /* {{{ static void php_output_context_reset(php_output_context *context)
798 * Reset an output context */
php_output_context_reset(php_output_context * context)799 static inline void php_output_context_reset(php_output_context *context)
800 {
801 int op = context->op;
802 php_output_context_dtor(context);
803 memset(context, 0, sizeof(php_output_context));
804 context->op = op;
805 }
806 /* }}} */
807
808 /* {{{ static void php_output_context_feed(php_output_context *context, char *, size_t, size_t)
809 * Feed output contexts input buffer */
php_output_context_feed(php_output_context * context,char * data,size_t size,size_t used,zend_bool free)810 static inline void php_output_context_feed(php_output_context *context, char *data, size_t size, size_t used, zend_bool free)
811 {
812 if (context->in.free && context->in.data) {
813 efree(context->in.data);
814 }
815 context->in.data = data;
816 context->in.used = used;
817 context->in.free = free;
818 context->in.size = size;
819 }
820 /* }}} */
821
822 /* {{{ static void php_output_context_swap(php_output_context *context)
823 * Swap output contexts buffers */
php_output_context_swap(php_output_context * context)824 static inline void php_output_context_swap(php_output_context *context)
825 {
826 if (context->in.free && context->in.data) {
827 efree(context->in.data);
828 }
829 context->in.data = context->out.data;
830 context->in.used = context->out.used;
831 context->in.free = context->out.free;
832 context->in.size = context->out.size;
833 context->out.data = NULL;
834 context->out.used = 0;
835 context->out.free = 0;
836 context->out.size = 0;
837 }
838 /* }}} */
839
840 /* {{{ static void php_output_context_pass(php_output_context *context)
841 * Pass input to output buffer */
php_output_context_pass(php_output_context * context)842 static inline void php_output_context_pass(php_output_context *context)
843 {
844 context->out.data = context->in.data;
845 context->out.used = context->in.used;
846 context->out.size = context->in.size;
847 context->out.free = context->in.free;
848 context->in.data = NULL;
849 context->in.used = 0;
850 context->in.free = 0;
851 context->in.size = 0;
852 }
853 /* }}} */
854
855 /* {{{ static void php_output_context_dtor(php_output_context *context)
856 * Destroy the contents of an output context */
php_output_context_dtor(php_output_context * context)857 static inline void php_output_context_dtor(php_output_context *context)
858 {
859 if (context->in.free && context->in.data) {
860 efree(context->in.data);
861 context->in.data = NULL;
862 }
863 if (context->out.free && context->out.data) {
864 efree(context->out.data);
865 context->out.data = NULL;
866 }
867 }
868 /* }}} */
869
870 /* {{{ static php_output_handler *php_output_handler_init(zval *name, size_t chunk_size, int flags)
871 * Allocates and initializes a php_output_handler structure */
php_output_handler_init(zend_string * name,size_t chunk_size,int flags)872 static inline php_output_handler *php_output_handler_init(zend_string *name, size_t chunk_size, int flags)
873 {
874 php_output_handler *handler;
875
876 handler = ecalloc(1, sizeof(php_output_handler));
877 handler->name = zend_string_copy(name);
878 handler->size = chunk_size;
879 handler->flags = flags;
880 handler->buffer.size = PHP_OUTPUT_HANDLER_INITBUF_SIZE(chunk_size);
881 handler->buffer.data = emalloc(handler->buffer.size);
882
883 return handler;
884 }
885 /* }}} */
886
887 /* {{{ static int php_output_handler_appen(php_output_handler *handler, const php_output_buffer *buf)
888 * Appends input to the output handlers buffer and indicates whether the buffer does not have to be processed by the output handler */
php_output_handler_append(php_output_handler * handler,const php_output_buffer * buf)889 static inline int php_output_handler_append(php_output_handler *handler, const php_output_buffer *buf)
890 {
891 if (buf->used) {
892 OG(flags) |= PHP_OUTPUT_WRITTEN;
893 /* store it away */
894 if ((handler->buffer.size - handler->buffer.used) <= buf->used) {
895 size_t grow_int = PHP_OUTPUT_HANDLER_INITBUF_SIZE(handler->size);
896 size_t grow_buf = PHP_OUTPUT_HANDLER_INITBUF_SIZE(buf->used - (handler->buffer.size - handler->buffer.used));
897 size_t grow_max = MAX(grow_int, grow_buf);
898
899 handler->buffer.data = safe_erealloc(handler->buffer.data, 1, handler->buffer.size, grow_max);
900 handler->buffer.size += grow_max;
901 }
902 memcpy(handler->buffer.data + handler->buffer.used, buf->data, buf->used);
903 handler->buffer.used += buf->used;
904
905 /* chunked buffering */
906 if (handler->size && (handler->buffer.used >= handler->size)) {
907 /* store away errors and/or any intermediate output */
908 return OG(running) ? 1 : 0;
909 }
910 }
911 return 1;
912 }
913 /* }}} */
914
915 /* {{{ static php_output_handler_status_t php_output_handler_op(php_output_handler *handler, php_output_context *context)
916 * Output handler operation dispatcher, applying context op to the php_output_handler handler */
php_output_handler_op(php_output_handler * handler,php_output_context * context)917 static inline php_output_handler_status_t php_output_handler_op(php_output_handler *handler, php_output_context *context)
918 {
919 php_output_handler_status_t status;
920 int original_op = context->op;
921
922 #if PHP_OUTPUT_DEBUG
923 fprintf(stderr, ">>> op(%d, "
924 "handler=%p, "
925 "name=%s, "
926 "flags=%d, "
927 "buffer.data=%s, "
928 "buffer.used=%zu, "
929 "buffer.size=%zu, "
930 "in.data=%s, "
931 "in.used=%zu)\n",
932 context->op,
933 handler,
934 handler->name,
935 handler->flags,
936 handler->buffer.used?handler->buffer.data:"",
937 handler->buffer.used,
938 handler->buffer.size,
939 context->in.used?context->in.data:"",
940 context->in.used
941 );
942 #endif
943
944 if (php_output_lock_error(context->op)) {
945 /* fatal error */
946 return PHP_OUTPUT_HANDLER_FAILURE;
947 }
948
949 /* storable? */
950 if (php_output_handler_append(handler, &context->in) && !context->op) {
951 context->op = original_op;
952 return PHP_OUTPUT_HANDLER_NO_DATA;
953 } else {
954 /* need to start? */
955 if (!(handler->flags & PHP_OUTPUT_HANDLER_STARTED)) {
956 context->op |= PHP_OUTPUT_HANDLER_START;
957 }
958
959 OG(running) = handler;
960 if (handler->flags & PHP_OUTPUT_HANDLER_USER) {
961 zval retval, ob_data, ob_mode;
962
963 ZVAL_STRINGL(&ob_data, handler->buffer.data, handler->buffer.used);
964 ZVAL_LONG(&ob_mode, (zend_long) context->op);
965 zend_fcall_info_argn(&handler->func.user->fci, 2, &ob_data, &ob_mode);
966 zval_ptr_dtor(&ob_data);
967
968 #define PHP_OUTPUT_USER_SUCCESS(retval) ((Z_TYPE(retval) != IS_UNDEF) && !(Z_TYPE(retval) == IS_FALSE))
969 if (SUCCESS == zend_fcall_info_call(&handler->func.user->fci, &handler->func.user->fcc, &retval, NULL) && PHP_OUTPUT_USER_SUCCESS(retval)) {
970 /* user handler may have returned TRUE */
971 status = PHP_OUTPUT_HANDLER_NO_DATA;
972 if (Z_TYPE(retval) != IS_FALSE && Z_TYPE(retval) != IS_TRUE) {
973 convert_to_string_ex(&retval);
974 if (Z_STRLEN(retval)) {
975 context->out.data = estrndup(Z_STRVAL(retval), Z_STRLEN(retval));
976 context->out.used = Z_STRLEN(retval);
977 context->out.free = 1;
978 status = PHP_OUTPUT_HANDLER_SUCCESS;
979 }
980 }
981 } else {
982 /* call failed, pass internal buffer along */
983 status = PHP_OUTPUT_HANDLER_FAILURE;
984 }
985
986 zend_fcall_info_argn(&handler->func.user->fci, 0);
987 zval_ptr_dtor(&retval);
988
989 } else {
990
991 php_output_context_feed(context, handler->buffer.data, handler->buffer.size, handler->buffer.used, 0);
992
993 if (SUCCESS == handler->func.internal(&handler->opaq, context)) {
994 if (context->out.used) {
995 status = PHP_OUTPUT_HANDLER_SUCCESS;
996 } else {
997 status = PHP_OUTPUT_HANDLER_NO_DATA;
998 }
999 } else {
1000 status = PHP_OUTPUT_HANDLER_FAILURE;
1001 }
1002 }
1003 handler->flags |= PHP_OUTPUT_HANDLER_STARTED;
1004 OG(running) = NULL;
1005 }
1006
1007 switch (status) {
1008 case PHP_OUTPUT_HANDLER_FAILURE:
1009 /* disable this handler */
1010 handler->flags |= PHP_OUTPUT_HANDLER_DISABLED;
1011 /* discard any output */
1012 if (context->out.data && context->out.free) {
1013 efree(context->out.data);
1014 }
1015 /* returns handlers buffer */
1016 context->out.data = handler->buffer.data;
1017 context->out.used = handler->buffer.used;
1018 context->out.free = 1;
1019 handler->buffer.data = NULL;
1020 handler->buffer.used = 0;
1021 handler->buffer.size = 0;
1022 break;
1023 case PHP_OUTPUT_HANDLER_NO_DATA:
1024 /* handler ate all */
1025 php_output_context_reset(context);
1026 /* no break */
1027 case PHP_OUTPUT_HANDLER_SUCCESS:
1028 /* no more buffered data */
1029 handler->buffer.used = 0;
1030 handler->flags |= PHP_OUTPUT_HANDLER_PROCESSED;
1031 break;
1032 }
1033
1034 context->op = original_op;
1035 return status;
1036 }
1037 /* }}} */
1038
1039
1040 /* {{{ static void php_output_op(int op, const char *str, size_t len)
1041 * Output op dispatcher, passes input and output handlers output through the output handler stack until it gets written to the SAPI */
php_output_op(int op,const char * str,size_t len)1042 static inline void php_output_op(int op, const char *str, size_t len)
1043 {
1044 php_output_context context;
1045 php_output_handler **active;
1046 int obh_cnt;
1047
1048 if (php_output_lock_error(op)) {
1049 return;
1050 }
1051
1052 php_output_context_init(&context, op);
1053
1054 /*
1055 * broken up for better performance:
1056 * - apply op to the one active handler; note that OG(active) might be popped off the stack on a flush
1057 * - or apply op to the handler stack
1058 */
1059 if (OG(active) && (obh_cnt = zend_stack_count(&OG(handlers)))) {
1060 context.in.data = (char *) str;
1061 context.in.used = len;
1062
1063 if (obh_cnt > 1) {
1064 zend_stack_apply_with_argument(&OG(handlers), ZEND_STACK_APPLY_TOPDOWN, php_output_stack_apply_op, &context);
1065 } else if ((active = zend_stack_top(&OG(handlers))) && (!((*active)->flags & PHP_OUTPUT_HANDLER_DISABLED))) {
1066 php_output_handler_op(*active, &context);
1067 } else {
1068 php_output_context_pass(&context);
1069 }
1070 } else {
1071 context.out.data = (char *) str;
1072 context.out.used = len;
1073 }
1074
1075 if (context.out.data && context.out.used) {
1076 php_output_header();
1077
1078 if (!(OG(flags) & PHP_OUTPUT_DISABLED)) {
1079 #if PHP_OUTPUT_DEBUG
1080 fprintf(stderr, "::: sapi_write('%s', %zu)\n", context.out.data, context.out.used);
1081 #endif
1082 sapi_module.ub_write(context.out.data, context.out.used);
1083
1084 if (OG(flags) & PHP_OUTPUT_IMPLICITFLUSH) {
1085 sapi_flush();
1086 }
1087
1088 OG(flags) |= PHP_OUTPUT_SENT;
1089 }
1090 }
1091 php_output_context_dtor(&context);
1092 }
1093 /* }}} */
1094
1095 /* {{{ static int php_output_stack_apply_op(void *h, void *c)
1096 * Operation callback for the stack apply function */
php_output_stack_apply_op(void * h,void * c)1097 static int php_output_stack_apply_op(void *h, void *c)
1098 {
1099 int was_disabled;
1100 php_output_handler_status_t status;
1101 php_output_handler *handler = *(php_output_handler **) h;
1102 php_output_context *context = (php_output_context *) c;
1103
1104 if ((was_disabled = (handler->flags & PHP_OUTPUT_HANDLER_DISABLED))) {
1105 status = PHP_OUTPUT_HANDLER_FAILURE;
1106 } else {
1107 status = php_output_handler_op(handler, context);
1108 }
1109
1110 /*
1111 * handler ate all => break
1112 * handler returned data or failed resp. is disabled => continue
1113 */
1114 switch (status) {
1115 case PHP_OUTPUT_HANDLER_NO_DATA:
1116 return 1;
1117
1118 case PHP_OUTPUT_HANDLER_SUCCESS:
1119 /* swap contexts buffers, unless this is the last handler in the stack */
1120 if (handler->level) {
1121 php_output_context_swap(context);
1122 }
1123 return 0;
1124
1125 case PHP_OUTPUT_HANDLER_FAILURE:
1126 default:
1127 if (was_disabled) {
1128 /* pass input along, if it's the last handler in the stack */
1129 if (!handler->level) {
1130 php_output_context_pass(context);
1131 }
1132 } else {
1133 /* swap buffers, unless this is the last handler */
1134 if (handler->level) {
1135 php_output_context_swap(context);
1136 }
1137 }
1138 return 0;
1139 }
1140 }
1141 /* }}} */
1142
1143 /* {{{ static int php_output_stack_apply_clean(void *h, void *c)
1144 * Clean callback for the stack apply function */
php_output_stack_apply_clean(void * h,void * c)1145 static int php_output_stack_apply_clean(void *h, void *c)
1146 {
1147 php_output_handler *handler = *(php_output_handler **) h;
1148 php_output_context *context = (php_output_context *) c;
1149
1150 handler->buffer.used = 0;
1151 php_output_handler_op(handler, context);
1152 php_output_context_reset(context);
1153 return 0;
1154 }
1155 /* }}} */
1156
1157 /* {{{ static int php_output_stack_apply_list(void *h, void *z)
1158 * List callback for the stack apply function */
php_output_stack_apply_list(void * h,void * z)1159 static int php_output_stack_apply_list(void *h, void *z)
1160 {
1161 php_output_handler *handler = *(php_output_handler **) h;
1162 zval *array = (zval *) z;
1163
1164 add_next_index_str(array, zend_string_copy(handler->name));
1165 return 0;
1166 }
1167 /* }}} */
1168
1169 /* {{{ static int php_output_stack_apply_status(void *h, void *z)
1170 * Status callback for the stack apply function */
php_output_stack_apply_status(void * h,void * z)1171 static int php_output_stack_apply_status(void *h, void *z)
1172 {
1173 php_output_handler *handler = *(php_output_handler **) h;
1174 zval arr, *array = (zval *) z;
1175
1176 add_next_index_zval(array, php_output_handler_status(handler, &arr));
1177
1178 return 0;
1179 }
1180
1181 /* {{{ static zval *php_output_handler_status(php_output_handler *handler, zval *entry)
1182 * Returns an array with the status of the output handler */
php_output_handler_status(php_output_handler * handler,zval * entry)1183 static inline zval *php_output_handler_status(php_output_handler *handler, zval *entry)
1184 {
1185 ZEND_ASSERT(entry != NULL);
1186
1187 array_init(entry);
1188 add_assoc_str(entry, "name", zend_string_copy(handler->name));
1189 add_assoc_long(entry, "type", (zend_long) (handler->flags & 0xf));
1190 add_assoc_long(entry, "flags", (zend_long) handler->flags);
1191 add_assoc_long(entry, "level", (zend_long) handler->level);
1192 add_assoc_long(entry, "chunk_size", (zend_long) handler->size);
1193 add_assoc_long(entry, "buffer_size", (zend_long) handler->buffer.size);
1194 add_assoc_long(entry, "buffer_used", (zend_long) handler->buffer.used);
1195
1196 return entry;
1197 }
1198 /* }}} */
1199
1200 /* {{{ static int php_output_stack_pop(int flags)
1201 * Pops an output handler off the stack */
php_output_stack_pop(int flags)1202 static int php_output_stack_pop(int flags)
1203 {
1204 php_output_context context;
1205 php_output_handler **current, *orphan = OG(active);
1206
1207 if (!orphan) {
1208 if (!(flags & PHP_OUTPUT_POP_SILENT)) {
1209 php_error_docref("ref.outcontrol", E_NOTICE, "Failed to %s buffer. No buffer to %s", (flags&PHP_OUTPUT_POP_DISCARD)?"discard":"send", (flags&PHP_OUTPUT_POP_DISCARD)?"discard":"send");
1210 }
1211 return 0;
1212 } else if (!(flags & PHP_OUTPUT_POP_FORCE) && !(orphan->flags & PHP_OUTPUT_HANDLER_REMOVABLE)) {
1213 if (!(flags & PHP_OUTPUT_POP_SILENT)) {
1214 php_error_docref("ref.outcontrol", E_NOTICE, "Failed to %s buffer of %s (%d)", (flags&PHP_OUTPUT_POP_DISCARD)?"discard":"send", ZSTR_VAL(orphan->name), orphan->level);
1215 }
1216 return 0;
1217 } else {
1218 php_output_context_init(&context, PHP_OUTPUT_HANDLER_FINAL);
1219
1220 /* don't run the output handler if it's disabled */
1221 if (!(orphan->flags & PHP_OUTPUT_HANDLER_DISABLED)) {
1222 /* didn't it start yet? */
1223 if (!(orphan->flags & PHP_OUTPUT_HANDLER_STARTED)) {
1224 context.op |= PHP_OUTPUT_HANDLER_START;
1225 }
1226 /* signal that we're cleaning up */
1227 if (flags & PHP_OUTPUT_POP_DISCARD) {
1228 context.op |= PHP_OUTPUT_HANDLER_CLEAN;
1229 }
1230 php_output_handler_op(orphan, &context);
1231 }
1232
1233 /* pop it off the stack */
1234 zend_stack_del_top(&OG(handlers));
1235 if ((current = zend_stack_top(&OG(handlers)))) {
1236 OG(active) = *current;
1237 } else {
1238 OG(active) = NULL;
1239 }
1240
1241 /* pass output along */
1242 if (context.out.data && context.out.used && !(flags & PHP_OUTPUT_POP_DISCARD)) {
1243 php_output_write(context.out.data, context.out.used);
1244 }
1245
1246 /* destroy the handler (after write!) */
1247 php_output_handler_free(&orphan);
1248 php_output_context_dtor(&context);
1249
1250 return 1;
1251 }
1252 }
1253 /* }}} */
1254
1255 /* {{{ static SUCCESS|FAILURE php_output_handler_compat_func(void *ctx, php_output_context *)
1256 * php_output_handler_context_func_t for php_output_handler_func_t output handlers */
php_output_handler_compat_func(void ** handler_context,php_output_context * output_context)1257 static int php_output_handler_compat_func(void **handler_context, php_output_context *output_context)
1258 {
1259 php_output_handler_func_t func = *(php_output_handler_func_t *) handler_context;
1260
1261 if (func) {
1262 char *out_str = NULL;
1263 size_t out_len = 0;
1264
1265 func(output_context->in.data, output_context->in.used, &out_str, &out_len, output_context->op);
1266
1267 if (out_str) {
1268 output_context->out.data = out_str;
1269 output_context->out.used = out_len;
1270 output_context->out.free = 1;
1271 } else {
1272 php_output_context_pass(output_context);
1273 }
1274
1275 return SUCCESS;
1276 }
1277 return FAILURE;
1278 }
1279 /* }}} */
1280
1281 /* {{{ static SUCCESS|FAILURE php_output_handler_default_func(void *ctx, php_output_context *)
1282 * Default output handler */
php_output_handler_default_func(void ** handler_context,php_output_context * output_context)1283 static int php_output_handler_default_func(void **handler_context, php_output_context *output_context)
1284 {
1285 php_output_context_pass(output_context);
1286 return SUCCESS;
1287 }
1288 /* }}} */
1289
1290 /* {{{ static SUCCESS|FAILURE php_output_handler_devnull_func(void *ctx, php_output_context *)
1291 * Null output handler */
php_output_handler_devnull_func(void ** handler_context,php_output_context * output_context)1292 static int php_output_handler_devnull_func(void **handler_context, php_output_context *output_context)
1293 {
1294 return SUCCESS;
1295 }
1296 /* }}} */
1297
1298 /*
1299 * USERLAND (nearly 1:1 of old output.c)
1300 */
1301
1302 /* {{{ Turn on Output Buffering (specifying an optional output handler). */
PHP_FUNCTION(ob_start)1303 PHP_FUNCTION(ob_start)
1304 {
1305 zval *output_handler = NULL;
1306 zend_long chunk_size = 0;
1307 zend_long flags = PHP_OUTPUT_HANDLER_STDFLAGS;
1308
1309 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|zll", &output_handler, &chunk_size, &flags) == FAILURE) {
1310 RETURN_THROWS();
1311 }
1312
1313 if (chunk_size < 0) {
1314 chunk_size = 0;
1315 }
1316
1317 if (php_output_start_user(output_handler, chunk_size, flags) == FAILURE) {
1318 php_error_docref("ref.outcontrol", E_NOTICE, "Failed to create buffer");
1319 RETURN_FALSE;
1320 }
1321 RETURN_TRUE;
1322 }
1323 /* }}} */
1324
1325 /* {{{ Flush (send) contents of the output buffer. The last buffer content is sent to next buffer */
PHP_FUNCTION(ob_flush)1326 PHP_FUNCTION(ob_flush)
1327 {
1328 if (zend_parse_parameters_none() == FAILURE) {
1329 RETURN_THROWS();
1330 }
1331
1332 if (!OG(active)) {
1333 php_error_docref("ref.outcontrol", E_NOTICE, "Failed to flush buffer. No buffer to flush");
1334 RETURN_FALSE;
1335 }
1336
1337 if (SUCCESS != php_output_flush()) {
1338 php_error_docref("ref.outcontrol", E_NOTICE, "Failed to flush buffer of %s (%d)", ZSTR_VAL(OG(active)->name), OG(active)->level);
1339 RETURN_FALSE;
1340 }
1341 RETURN_TRUE;
1342 }
1343 /* }}} */
1344
1345 /* {{{ Clean (delete) the current output buffer */
PHP_FUNCTION(ob_clean)1346 PHP_FUNCTION(ob_clean)
1347 {
1348 if (zend_parse_parameters_none() == FAILURE) {
1349 RETURN_THROWS();
1350 }
1351
1352 if (!OG(active)) {
1353 php_error_docref("ref.outcontrol", E_NOTICE, "Failed to delete buffer. No buffer to delete");
1354 RETURN_FALSE;
1355 }
1356
1357 if (SUCCESS != php_output_clean()) {
1358 php_error_docref("ref.outcontrol", E_NOTICE, "Failed to delete buffer of %s (%d)", ZSTR_VAL(OG(active)->name), OG(active)->level);
1359 RETURN_FALSE;
1360 }
1361 RETURN_TRUE;
1362 }
1363 /* }}} */
1364
1365 /* {{{ Flush (send) the output buffer, and delete current output buffer */
PHP_FUNCTION(ob_end_flush)1366 PHP_FUNCTION(ob_end_flush)
1367 {
1368 if (zend_parse_parameters_none() == FAILURE) {
1369 RETURN_THROWS();
1370 }
1371
1372 if (!OG(active)) {
1373 php_error_docref("ref.outcontrol", E_NOTICE, "Failed to delete and flush buffer. No buffer to delete or flush");
1374 RETURN_FALSE;
1375 }
1376
1377 RETURN_BOOL(SUCCESS == php_output_end());
1378 }
1379 /* }}} */
1380
1381 /* {{{ Clean the output buffer, and delete current output buffer */
PHP_FUNCTION(ob_end_clean)1382 PHP_FUNCTION(ob_end_clean)
1383 {
1384 if (zend_parse_parameters_none() == FAILURE) {
1385 RETURN_THROWS();
1386 }
1387
1388 if (!OG(active)) {
1389 php_error_docref("ref.outcontrol", E_NOTICE, "Failed to delete buffer. No buffer to delete");
1390 RETURN_FALSE;
1391 }
1392
1393 RETURN_BOOL(SUCCESS == php_output_discard());
1394 }
1395 /* }}} */
1396
1397 /* {{{ Get current buffer contents, flush (send) the output buffer, and delete current output buffer */
PHP_FUNCTION(ob_get_flush)1398 PHP_FUNCTION(ob_get_flush)
1399 {
1400 if (zend_parse_parameters_none() == FAILURE) {
1401 RETURN_THROWS();
1402 }
1403
1404 if (php_output_get_contents(return_value) == FAILURE) {
1405 php_error_docref("ref.outcontrol", E_NOTICE, "Failed to delete and flush buffer. No buffer to delete or flush");
1406 RETURN_FALSE;
1407 }
1408
1409 if (SUCCESS != php_output_end()) {
1410 php_error_docref("ref.outcontrol", E_NOTICE, "Failed to delete buffer of %s (%d)", ZSTR_VAL(OG(active)->name), OG(active)->level);
1411 }
1412 }
1413 /* }}} */
1414
1415 /* {{{ Get current buffer contents and delete current output buffer */
PHP_FUNCTION(ob_get_clean)1416 PHP_FUNCTION(ob_get_clean)
1417 {
1418 if (zend_parse_parameters_none() == FAILURE) {
1419 RETURN_THROWS();
1420 }
1421
1422 if(!OG(active)) {
1423 RETURN_FALSE;
1424 }
1425
1426 if (php_output_get_contents(return_value) == FAILURE) {
1427 php_error_docref("ref.outcontrol", E_NOTICE, "Failed to delete buffer. No buffer to delete");
1428 RETURN_FALSE;
1429 }
1430
1431 if (SUCCESS != php_output_discard()) {
1432 php_error_docref("ref.outcontrol", E_NOTICE, "Failed to delete buffer of %s (%d)", ZSTR_VAL(OG(active)->name), OG(active)->level);
1433 }
1434 }
1435 /* }}} */
1436
1437 /* {{{ Return the contents of the output buffer */
PHP_FUNCTION(ob_get_contents)1438 PHP_FUNCTION(ob_get_contents)
1439 {
1440 if (zend_parse_parameters_none() == FAILURE) {
1441 RETURN_THROWS();
1442 }
1443
1444 if (php_output_get_contents(return_value) == FAILURE) {
1445 RETURN_FALSE;
1446 }
1447 }
1448 /* }}} */
1449
1450 /* {{{ Return the nesting level of the output buffer */
PHP_FUNCTION(ob_get_level)1451 PHP_FUNCTION(ob_get_level)
1452 {
1453 if (zend_parse_parameters_none() == FAILURE) {
1454 RETURN_THROWS();
1455 }
1456
1457 RETURN_LONG(php_output_get_level());
1458 }
1459 /* }}} */
1460
1461 /* {{{ Return the length of the output buffer */
PHP_FUNCTION(ob_get_length)1462 PHP_FUNCTION(ob_get_length)
1463 {
1464 if (zend_parse_parameters_none() == FAILURE) {
1465 RETURN_THROWS();
1466 }
1467
1468 if (php_output_get_length(return_value) == FAILURE) {
1469 RETURN_FALSE;
1470 }
1471 }
1472 /* }}} */
1473
1474 /* {{{ List all output_buffers in an array */
PHP_FUNCTION(ob_list_handlers)1475 PHP_FUNCTION(ob_list_handlers)
1476 {
1477 if (zend_parse_parameters_none() == FAILURE) {
1478 RETURN_THROWS();
1479 }
1480
1481 array_init(return_value);
1482
1483 if (!OG(active)) {
1484 return;
1485 }
1486
1487 zend_stack_apply_with_argument(&OG(handlers), ZEND_STACK_APPLY_BOTTOMUP, php_output_stack_apply_list, return_value);
1488 }
1489 /* }}} */
1490
1491 /* {{{ Return the status of the active or all output buffers */
PHP_FUNCTION(ob_get_status)1492 PHP_FUNCTION(ob_get_status)
1493 {
1494 zend_bool full_status = 0;
1495
1496 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &full_status) == FAILURE) {
1497 RETURN_THROWS();
1498 }
1499
1500 if (!OG(active)) {
1501 array_init(return_value);
1502 return;
1503 }
1504
1505 if (full_status) {
1506 array_init(return_value);
1507 zend_stack_apply_with_argument(&OG(handlers), ZEND_STACK_APPLY_BOTTOMUP, php_output_stack_apply_status, return_value);
1508 } else {
1509 php_output_handler_status(OG(active), return_value);
1510 }
1511 }
1512 /* }}} */
1513
1514 /* {{{ Turn implicit flush on/off and is equivalent to calling flush() after every output call */
PHP_FUNCTION(ob_implicit_flush)1515 PHP_FUNCTION(ob_implicit_flush)
1516 {
1517 zend_long flag = 1;
1518
1519 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &flag) == FAILURE) {
1520 RETURN_THROWS();
1521 }
1522
1523 php_output_set_implicit_flush((int) flag);
1524 }
1525 /* }}} */
1526
1527 /* {{{ Reset(clear) URL rewriter values */
PHP_FUNCTION(output_reset_rewrite_vars)1528 PHP_FUNCTION(output_reset_rewrite_vars)
1529 {
1530 if (zend_parse_parameters_none() == FAILURE) {
1531 RETURN_THROWS();
1532 }
1533
1534 if (php_url_scanner_reset_vars() == SUCCESS) {
1535 RETURN_TRUE;
1536 } else {
1537 RETURN_FALSE;
1538 }
1539 }
1540 /* }}} */
1541
1542 /* {{{ Add URL rewriter values */
PHP_FUNCTION(output_add_rewrite_var)1543 PHP_FUNCTION(output_add_rewrite_var)
1544 {
1545 char *name, *value;
1546 size_t name_len, value_len;
1547
1548 if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &name, &name_len, &value, &value_len) == FAILURE) {
1549 RETURN_THROWS();
1550 }
1551
1552 if (php_url_scanner_add_var(name, name_len, value, value_len, 1) == SUCCESS) {
1553 RETURN_TRUE;
1554 } else {
1555 RETURN_FALSE;
1556 }
1557 }
1558 /* }}} */
1559