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