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