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