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