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