1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 5 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 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@zend.com> |
16 : | Thies C. Arntzen <thies@thieso.net> |
17 : | Marcus Boerger <helly@php.net> |
18 : +----------------------------------------------------------------------+
19 : */
20 :
21 : /* $Id: output.c 288518 2009-09-21 11:22:13Z dmitry $ */
22 :
23 : #include "php.h"
24 : #include "ext/standard/head.h"
25 : #include "ext/standard/basic_functions.h"
26 : #include "ext/standard/url_scanner_ex.h"
27 : #if HAVE_ZLIB && !defined(COMPILE_DL_ZLIB)
28 : #include "ext/zlib/php_zlib.h"
29 : #endif
30 : #include "SAPI.h"
31 :
32 : #define OB_DEFAULT_HANDLER_NAME "default output handler"
33 :
34 : /* output functions */
35 : static int php_b_body_write(const char *str, uint str_length TSRMLS_DC);
36 :
37 : static int php_ob_init(uint initial_size, uint block_size, zval *output_handler, uint chunk_size, zend_bool erase TSRMLS_DC);
38 : static void php_ob_append(const char *text, uint text_length TSRMLS_DC);
39 : #if 0
40 : static void php_ob_prepend(const char *text, uint text_length);
41 : #endif
42 :
43 : #ifdef ZTS
44 : int output_globals_id;
45 : #else
46 : php_output_globals output_globals;
47 : #endif
48 :
49 : /* {{{ php_default_output_func */
50 : PHPAPI int php_default_output_func(const char *str, uint str_len TSRMLS_DC)
51 79 : {
52 79 : fwrite(str, 1, str_len, stderr);
53 79 : return str_len;
54 : }
55 : /* }}} */
56 :
57 : /* {{{ php_output_init_globals */
58 : static void php_output_init_globals(php_output_globals *output_globals_p TSRMLS_DC)
59 17633 : {
60 17633 : OG(php_body_write) = php_default_output_func;
61 17633 : OG(php_header_write) = php_default_output_func;
62 17633 : OG(implicit_flush) = 0;
63 17633 : OG(output_start_filename) = NULL;
64 17633 : OG(output_start_lineno) = 0;
65 17633 : }
66 : /* }}} */
67 :
68 :
69 : /* {{{ php_output_startup
70 : Start output layer */
71 : PHPAPI void php_output_startup(void)
72 17633 : {
73 : #ifdef ZTS
74 : ts_allocate_id(&output_globals_id, sizeof(php_output_globals), (ts_allocate_ctor) php_output_init_globals, NULL);
75 : #else
76 17633 : php_output_init_globals(&output_globals TSRMLS_CC);
77 : #endif
78 17633 : }
79 : /* }}} */
80 :
81 :
82 : /* {{{ php_output_activate
83 : Initilize output global for activation */
84 : PHPAPI void php_output_activate(TSRMLS_D)
85 17619 : {
86 17619 : OG(php_body_write) = php_ub_body_write;
87 17619 : OG(php_header_write) = sapi_module.ub_write;
88 17619 : OG(ob_nesting_level) = 0;
89 17619 : OG(ob_lock) = 0;
90 17619 : OG(disable_output) = 0;
91 17619 : OG(output_start_filename) = NULL;
92 17619 : OG(output_start_lineno) = 0;
93 17619 : }
94 : /* }}} */
95 :
96 :
97 : /* {{{ php_output_set_status
98 : Toggle output status. Do NOT use in application code, only in SAPIs where appropriate. */
99 : PHPAPI void php_output_set_status(zend_bool status TSRMLS_DC)
100 3 : {
101 3 : OG(disable_output) = !status;
102 3 : }
103 : /* }}} */
104 :
105 : /* {{{ php_output_register_constants */
106 : void php_output_register_constants(TSRMLS_D)
107 17633 : {
108 17633 : REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_START", PHP_OUTPUT_HANDLER_START, CONST_CS | CONST_PERSISTENT);
109 17633 : REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_CONT", PHP_OUTPUT_HANDLER_CONT, CONST_CS | CONST_PERSISTENT);
110 17633 : REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_END", PHP_OUTPUT_HANDLER_END, CONST_CS | CONST_PERSISTENT);
111 17633 : }
112 : /* }}} */
113 :
114 :
115 : /* {{{ php_body_write
116 : * Write body part */
117 : PHPAPI int php_body_write(const char *str, uint str_length TSRMLS_DC)
118 1077184 : {
119 1077184 : return OG(php_body_write)(str, str_length TSRMLS_CC);
120 : }
121 : /* }}} */
122 :
123 : /* {{{ php_header_write
124 : * Write HTTP header */
125 : PHPAPI int php_header_write(const char *str, uint str_length TSRMLS_DC)
126 1041 : {
127 1041 : if (OG(disable_output)) {
128 0 : return 0;
129 : } else {
130 1041 : return OG(php_header_write)(str, str_length TSRMLS_CC);
131 : }
132 : }
133 : /* }}} */
134 :
135 : /* {{{ php_start_ob_buffer
136 : * Start output buffering */
137 : PHPAPI int php_start_ob_buffer(zval *output_handler, uint chunk_size, zend_bool erase TSRMLS_DC)
138 5956 : {
139 : uint initial_size, block_size;
140 :
141 5956 : if (OG(ob_lock)) {
142 2 : if (SG(headers_sent) && !SG(request_info).headers_only) {
143 0 : OG(php_body_write) = php_ub_body_write_no_header;
144 : } else {
145 2 : OG(php_body_write) = php_ub_body_write;
146 : }
147 2 : OG(ob_nesting_level) = 0;
148 2 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_ERROR, "Cannot use output buffering in output buffering display handlers");
149 0 : return FAILURE;
150 : }
151 5954 : if (chunk_size > 0) {
152 67 : if (chunk_size==1) {
153 3 : chunk_size = 4096;
154 : }
155 67 : initial_size = (chunk_size*3/2);
156 67 : block_size = chunk_size/2;
157 : } else {
158 5887 : initial_size = 40*1024;
159 5887 : block_size = 10*1024;
160 : }
161 5954 : return php_ob_init(initial_size, block_size, output_handler, chunk_size, erase TSRMLS_CC);
162 : }
163 : /* }}} */
164 :
165 : /* {{{ php_start_ob_buffer_named
166 : * Start output buffering */
167 : PHPAPI int php_start_ob_buffer_named(const char *output_handler_name, uint chunk_size, zend_bool erase TSRMLS_DC)
168 6 : {
169 : zval *output_handler;
170 : int result;
171 :
172 6 : ALLOC_INIT_ZVAL(output_handler);
173 6 : Z_STRLEN_P(output_handler) = strlen(output_handler_name); /* this can be optimized */
174 6 : Z_STRVAL_P(output_handler) = estrndup(output_handler_name, Z_STRLEN_P(output_handler));
175 6 : Z_TYPE_P(output_handler) = IS_STRING;
176 6 : result = php_start_ob_buffer(output_handler, chunk_size, erase TSRMLS_CC);
177 6 : zval_dtor(output_handler);
178 6 : FREE_ZVAL(output_handler);
179 6 : return result;
180 : }
181 : /* }}} */
182 :
183 : /* {{{ php_end_ob_buffer
184 : * End output buffering (one level) */
185 : PHPAPI void php_end_ob_buffer(zend_bool send_buffer, zend_bool just_flush TSRMLS_DC)
186 6295 : {
187 6295 : char *final_buffer=NULL;
188 6295 : unsigned int final_buffer_length=0;
189 6295 : zval *alternate_buffer=NULL;
190 : char *to_be_destroyed_buffer, *to_be_destroyed_handler_name;
191 6295 : char *to_be_destroyed_handled_output[2] = { 0, 0 };
192 : int status;
193 6295 : php_ob_buffer *prev_ob_buffer_p=NULL;
194 : php_ob_buffer orig_ob_buffer;
195 :
196 6295 : if (OG(ob_nesting_level)==0) {
197 0 : return;
198 : }
199 6295 : status = 0;
200 6295 : if (!OG(active_ob_buffer).status & PHP_OUTPUT_HANDLER_START) {
201 : /* our first call */
202 5954 : status |= PHP_OUTPUT_HANDLER_START;
203 : }
204 6295 : if (just_flush) {
205 341 : status |= PHP_OUTPUT_HANDLER_CONT;
206 : } else {
207 5954 : status |= PHP_OUTPUT_HANDLER_END;
208 : }
209 :
210 : #if 0
211 : {
212 : FILE *fp;
213 : fp = fopen("/tmp/ob_log", "a");
214 : fprintf(fp, "NestLevel: %d ObStatus: %d HandlerName: %s\n", OG(ob_nesting_level), status, OG(active_ob_buffer).handler_name);
215 : fclose(fp);
216 : }
217 : #endif
218 :
219 6295 : if (OG(active_ob_buffer).internal_output_handler) {
220 18 : final_buffer = OG(active_ob_buffer).internal_output_handler_buffer;
221 18 : final_buffer_length = OG(active_ob_buffer).internal_output_handler_buffer_size;
222 18 : OG(active_ob_buffer).internal_output_handler(OG(active_ob_buffer).buffer, OG(active_ob_buffer).text_length, &final_buffer, &final_buffer_length, status TSRMLS_CC);
223 6277 : } else if (OG(active_ob_buffer).output_handler) {
224 : zval **params[2];
225 : zval *orig_buffer;
226 : zval *z_status;
227 :
228 121 : ALLOC_INIT_ZVAL(orig_buffer);
229 121 : ZVAL_STRINGL(orig_buffer, OG(active_ob_buffer).buffer, OG(active_ob_buffer).text_length, 1);
230 :
231 121 : ALLOC_INIT_ZVAL(z_status);
232 121 : ZVAL_LONG(z_status, status);
233 :
234 121 : params[0] = &orig_buffer;
235 121 : params[1] = &z_status;
236 121 : OG(ob_lock) = 1;
237 :
238 121 : if (call_user_function_ex(CG(function_table), NULL, OG(active_ob_buffer).output_handler, &alternate_buffer, 2, params, 1, NULL TSRMLS_CC)==SUCCESS) {
239 119 : if (alternate_buffer && !(Z_TYPE_P(alternate_buffer)==IS_BOOL && Z_BVAL_P(alternate_buffer)==0)) {
240 117 : convert_to_string_ex(&alternate_buffer);
241 117 : final_buffer = Z_STRVAL_P(alternate_buffer);
242 117 : final_buffer_length = Z_STRLEN_P(alternate_buffer);
243 : }
244 : }
245 119 : OG(ob_lock) = 0;
246 119 : if (!just_flush) {
247 90 : zval_ptr_dtor(&OG(active_ob_buffer).output_handler);
248 : }
249 119 : zval_ptr_dtor(&orig_buffer);
250 119 : zval_ptr_dtor(&z_status);
251 : }
252 :
253 6293 : if (!final_buffer) {
254 6159 : final_buffer = OG(active_ob_buffer).buffer;
255 6159 : final_buffer_length = OG(active_ob_buffer).text_length;
256 : }
257 :
258 6293 : if (OG(ob_nesting_level)==1) { /* end buffering */
259 10445 : if (SG(headers_sent) && !SG(request_info).headers_only) {
260 4615 : OG(php_body_write) = php_ub_body_write_no_header;
261 : } else {
262 1215 : OG(php_body_write) = php_ub_body_write;
263 : }
264 : }
265 :
266 6293 : to_be_destroyed_buffer = OG(active_ob_buffer).buffer;
267 6293 : to_be_destroyed_handler_name = OG(active_ob_buffer).handler_name;
268 6293 : if (OG(active_ob_buffer).internal_output_handler
269 : && (final_buffer != OG(active_ob_buffer).internal_output_handler_buffer)
270 : && (final_buffer != OG(active_ob_buffer).buffer)) {
271 15 : to_be_destroyed_handled_output[0] = final_buffer;
272 : }
273 :
274 6293 : if (!just_flush) {
275 5952 : if (OG(active_ob_buffer).internal_output_handler) {
276 13 : to_be_destroyed_handled_output[1] = OG(active_ob_buffer).internal_output_handler_buffer;
277 : }
278 : }
279 6293 : if (OG(ob_nesting_level)>1) { /* restore previous buffer */
280 463 : zend_stack_top(&OG(ob_buffers), (void **) &prev_ob_buffer_p);
281 463 : orig_ob_buffer = OG(active_ob_buffer);
282 463 : OG(active_ob_buffer) = *prev_ob_buffer_p;
283 463 : zend_stack_del_top(&OG(ob_buffers));
284 463 : if (!just_flush && OG(ob_nesting_level)==2) { /* destroy the stack */
285 201 : zend_stack_destroy(&OG(ob_buffers));
286 : }
287 : }
288 6293 : OG(ob_nesting_level)--;
289 :
290 6293 : if (send_buffer) {
291 676 : if (just_flush) { /* if flush is called prior to proper end, ensure presence of NUL */
292 334 : final_buffer[final_buffer_length] = '\0';
293 : }
294 676 : OG(php_body_write)(final_buffer, final_buffer_length TSRMLS_CC);
295 : }
296 :
297 6293 : if (just_flush) { /* we restored the previous ob, return to the current */
298 341 : if (prev_ob_buffer_p) {
299 252 : zend_stack_push(&OG(ob_buffers), &OG(active_ob_buffer), sizeof(php_ob_buffer));
300 252 : OG(active_ob_buffer) = orig_ob_buffer;
301 : }
302 341 : OG(ob_nesting_level)++;
303 : }
304 :
305 6293 : if (alternate_buffer) {
306 118 : zval_ptr_dtor(&alternate_buffer);
307 : }
308 :
309 6293 : if (status & PHP_OUTPUT_HANDLER_END) {
310 5952 : efree(to_be_destroyed_handler_name);
311 : }
312 6293 : if (!just_flush) {
313 5952 : efree(to_be_destroyed_buffer);
314 : } else {
315 341 : OG(active_ob_buffer).text_length = 0;
316 341 : OG(active_ob_buffer).status |= PHP_OUTPUT_HANDLER_START;
317 341 : OG(php_body_write) = php_b_body_write;
318 : }
319 6293 : if (to_be_destroyed_handled_output[0]) {
320 15 : efree(to_be_destroyed_handled_output[0]);
321 : }
322 6293 : if (to_be_destroyed_handled_output[1]) {
323 13 : efree(to_be_destroyed_handled_output[1]);
324 : }
325 : }
326 : /* }}} */
327 :
328 : /* {{{ php_end_ob_buffers
329 : * End output buffering (all buffers) */
330 : PHPAPI void php_end_ob_buffers(zend_bool send_buffer TSRMLS_DC)
331 17682 : {
332 35460 : while (OG(ob_nesting_level)!=0) {
333 98 : php_end_ob_buffer(send_buffer, 0 TSRMLS_CC);
334 : }
335 17680 : }
336 : /* }}} */
337 :
338 : /* {{{ php_start_implicit_flush
339 : */
340 : PHPAPI void php_start_implicit_flush(TSRMLS_D)
341 17305 : {
342 17305 : OG(implicit_flush)=1;
343 17305 : }
344 : /* }}} */
345 :
346 : /* {{{ php_end_implicit_flush
347 : */
348 : PHPAPI void php_end_implicit_flush(TSRMLS_D)
349 8 : {
350 8 : OG(implicit_flush)=0;
351 8 : }
352 : /* }}} */
353 :
354 : /* {{{ php_ob_set_internal_handler
355 : */
356 : PHPAPI void php_ob_set_internal_handler(php_output_handler_func_t internal_output_handler, uint buffer_size, char *handler_name, zend_bool erase TSRMLS_DC)
357 13 : {
358 13 : if (OG(ob_nesting_level)==0 || OG(active_ob_buffer).internal_output_handler || strcmp(OG(active_ob_buffer).handler_name, OB_DEFAULT_HANDLER_NAME)) {
359 13 : php_start_ob_buffer(NULL, buffer_size, erase TSRMLS_CC);
360 : }
361 :
362 13 : OG(active_ob_buffer).internal_output_handler = internal_output_handler;
363 13 : OG(active_ob_buffer).internal_output_handler_buffer = (char *) emalloc(buffer_size);
364 13 : OG(active_ob_buffer).internal_output_handler_buffer_size = buffer_size;
365 13 : if (OG(active_ob_buffer).handler_name) {
366 13 : efree(OG(active_ob_buffer).handler_name);
367 : }
368 13 : OG(active_ob_buffer).handler_name = estrdup(handler_name);
369 13 : OG(active_ob_buffer).erase = erase;
370 13 : }
371 : /* }}} */
372 :
373 : /*
374 : * Output buffering - implementation
375 : */
376 :
377 : /* {{{ php_ob_allocate
378 : */
379 : static inline void php_ob_allocate(uint text_length TSRMLS_DC)
380 208897 : {
381 208897 : uint new_len = OG(active_ob_buffer).text_length + text_length;
382 :
383 208897 : if (OG(active_ob_buffer).size < new_len) {
384 447 : uint buf_size = OG(active_ob_buffer).size;
385 1343 : while (buf_size <= new_len) {
386 449 : buf_size += OG(active_ob_buffer).block_size;
387 : }
388 :
389 447 : OG(active_ob_buffer).buffer = (char *) erealloc(OG(active_ob_buffer).buffer, buf_size+1);
390 447 : OG(active_ob_buffer).size = buf_size;
391 : }
392 208897 : OG(active_ob_buffer).text_length = new_len;
393 208897 : }
394 : /* }}} */
395 :
396 : /* {{{ php_ob_init_conflict
397 : * Returns 1 if handler_set is already used and generates error message
398 : */
399 : PHPAPI int php_ob_init_conflict(char *handler_new, char *handler_set TSRMLS_DC)
400 1 : {
401 1 : if (php_ob_handler_used(handler_set TSRMLS_CC)) {
402 1 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_WARNING, "output handler '%s' conflicts with '%s'", handler_new, handler_set);
403 1 : return 1;
404 : }
405 0 : return 0;
406 : }
407 : /* }}} */
408 :
409 : /* {{{ php_ob_init_named
410 : */
411 : static int php_ob_init_named(uint initial_size, uint block_size, char *handler_name, zval *output_handler, uint chunk_size, zend_bool erase TSRMLS_DC)
412 5961 : {
413 : php_ob_buffer tmp_buf;
414 :
415 5961 : if (output_handler && !zend_is_callable(output_handler, 0, NULL TSRMLS_CC)) {
416 6 : return FAILURE;
417 : }
418 :
419 5955 : tmp_buf.block_size = block_size;
420 5955 : tmp_buf.size = initial_size;
421 5955 : tmp_buf.buffer = (char *) emalloc(initial_size+1);
422 5955 : tmp_buf.text_length = 0;
423 5955 : tmp_buf.output_handler = output_handler;
424 5955 : tmp_buf.chunk_size = chunk_size;
425 5955 : tmp_buf.status = 0;
426 5955 : tmp_buf.internal_output_handler = NULL;
427 5955 : tmp_buf.internal_output_handler_buffer = NULL;
428 5955 : tmp_buf.internal_output_handler_buffer_size = 0;
429 5955 : tmp_buf.handler_name = estrdup(handler_name&&handler_name[0]?handler_name:OB_DEFAULT_HANDLER_NAME);
430 5955 : tmp_buf.erase = erase;
431 :
432 5955 : if (OG(ob_nesting_level)>0) {
433 : #if HAVE_ZLIB && !defined(COMPILE_DL_ZLIB)
434 212 : if (!strncmp(handler_name, "ob_gzhandler", sizeof("ob_gzhandler")) && php_ob_gzhandler_check(TSRMLS_C)) {
435 1 : return FAILURE;
436 : }
437 : #endif
438 211 : if (OG(ob_nesting_level)==1) { /* initialize stack */
439 201 : zend_stack_init(&OG(ob_buffers));
440 : }
441 211 : zend_stack_push(&OG(ob_buffers), &OG(active_ob_buffer), sizeof(php_ob_buffer));
442 : }
443 5954 : OG(ob_nesting_level)++;
444 5954 : OG(active_ob_buffer) = tmp_buf;
445 5954 : OG(php_body_write) = php_b_body_write;
446 5954 : return SUCCESS;
447 : }
448 : /* }}} */
449 :
450 : /* {{{ php_ob_handler_from_string
451 : * Create zval output handler from string
452 : */
453 : static zval* php_ob_handler_from_string(const char *handler_name, int len TSRMLS_DC)
454 92 : {
455 : zval *output_handler;
456 :
457 92 : ALLOC_INIT_ZVAL(output_handler);
458 92 : Z_STRLEN_P(output_handler) = len;
459 92 : Z_STRVAL_P(output_handler) = estrndup(handler_name, len);
460 92 : Z_TYPE_P(output_handler) = IS_STRING;
461 92 : return output_handler;
462 : }
463 : /* }}} */
464 :
465 : /* {{{ php_ob_init
466 : */
467 : static int php_ob_init(uint initial_size, uint block_size, zval *output_handler, uint chunk_size, zend_bool erase TSRMLS_DC)
468 5974 : {
469 5974 : int result = FAILURE, handler_len, len;
470 : char *handler_name, *next_handler_name;
471 : HashPosition pos;
472 : zval **tmp;
473 : zval *handler_zval;
474 :
475 6066 : if (output_handler && output_handler->type == IS_STRING) {
476 92 : handler_name = Z_STRVAL_P(output_handler);
477 92 : handler_len = Z_STRLEN_P(output_handler);
478 :
479 92 : result = SUCCESS;
480 92 : if (handler_len && handler_name[0] != '\0') {
481 182 : while ((next_handler_name=strchr(handler_name, ',')) != NULL) {
482 0 : len = next_handler_name-handler_name;
483 0 : next_handler_name = estrndup(handler_name, len);
484 0 : handler_zval = php_ob_handler_from_string(next_handler_name, len TSRMLS_CC);
485 0 : result = php_ob_init_named(initial_size, block_size, next_handler_name, handler_zval, chunk_size, erase TSRMLS_CC);
486 0 : if (result != SUCCESS) {
487 0 : zval_dtor(handler_zval);
488 0 : FREE_ZVAL(handler_zval);
489 : }
490 0 : handler_name += len+1;
491 0 : handler_len -= len+1;
492 0 : efree(next_handler_name);
493 : }
494 : }
495 92 : if (result == SUCCESS) {
496 92 : handler_zval = php_ob_handler_from_string(handler_name, handler_len TSRMLS_CC);
497 92 : result = php_ob_init_named(initial_size, block_size, handler_name, handler_zval, chunk_size, erase TSRMLS_CC);
498 92 : if (result != SUCCESS) {
499 7 : zval_dtor(handler_zval);
500 7 : FREE_ZVAL(handler_zval);
501 : }
502 : }
503 5896 : } else if (output_handler && output_handler->type == IS_ARRAY) {
504 : /* do we have array(object,method) */
505 16 : if (zend_is_callable(output_handler, 0, &handler_name TSRMLS_CC)) {
506 5 : SEPARATE_ZVAL(&output_handler);
507 5 : Z_ADDREF_P(output_handler);
508 5 : result = php_ob_init_named(initial_size, block_size, handler_name, output_handler, chunk_size, erase TSRMLS_CC);
509 5 : efree(handler_name);
510 : } else {
511 11 : efree(handler_name);
512 : /* init all array elements recursively */
513 11 : zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(output_handler), &pos);
514 36 : while (zend_hash_get_current_data_ex(Z_ARRVAL_P(output_handler), (void **)&tmp, &pos) == SUCCESS) {
515 20 : result = php_ob_init(initial_size, block_size, *tmp, chunk_size, erase TSRMLS_CC);
516 18 : if (result == FAILURE) {
517 4 : break;
518 : }
519 14 : zend_hash_move_forward_ex(Z_ARRVAL_P(output_handler), &pos);
520 : }
521 : }
522 5868 : } else if (output_handler && output_handler->type == IS_OBJECT) {
523 : /* do we have callable object */
524 4 : if (zend_is_callable(output_handler, 0, &handler_name TSRMLS_CC)) {
525 2 : SEPARATE_ZVAL(&output_handler);
526 2 : Z_ADDREF_P(output_handler);
527 2 : result = php_ob_init_named(initial_size, block_size, handler_name, output_handler, chunk_size, erase TSRMLS_CC);
528 2 : efree(handler_name);
529 : } else {
530 0 : efree(handler_name);
531 0 : php_error_docref(NULL TSRMLS_CC, E_ERROR, "No method name given: use ob_start(array($object,'method')) to specify instance $object and the name of a method of class %s to use as output handler", Z_OBJCE_P(output_handler)->name);
532 0 : result = FAILURE;
533 : }
534 : } else {
535 5862 : result = php_ob_init_named(initial_size, block_size, OB_DEFAULT_HANDLER_NAME, NULL, chunk_size, erase TSRMLS_CC);
536 : }
537 5970 : return result;
538 : }
539 : /* }}} */
540 :
541 : /* {{{ php_ob_list_each
542 : */
543 : static int php_ob_list_each(php_ob_buffer *ob_buffer, zval *ob_handler_array)
544 25 : {
545 25 : add_next_index_string(ob_handler_array, ob_buffer->handler_name, 1);
546 25 : return 0;
547 : }
548 : /* }}} */
549 :
550 : /* {{{ proto false|array ob_list_handlers()
551 : * List all output_buffers in an array
552 : */
553 : PHP_FUNCTION(ob_list_handlers)
554 16 : {
555 16 : if (zend_parse_parameters_none() == FAILURE) {
556 0 : return;
557 : }
558 :
559 16 : array_init(return_value);
560 16 : if (OG(ob_nesting_level)) {
561 13 : if (OG(ob_nesting_level)>1) {
562 5 : zend_stack_apply_with_argument(&OG(ob_buffers), ZEND_STACK_APPLY_BOTTOMUP, (int (*)(void *element, void *)) php_ob_list_each, return_value);
563 : }
564 13 : php_ob_list_each(&OG(active_ob_buffer), return_value);
565 : }
566 : }
567 : /* }}} */
568 :
569 : /* {{{ php_ob_used_each
570 : * Sets handler_name to NULL is found
571 : */
572 : static int php_ob_handler_used_each(php_ob_buffer *ob_buffer, char **handler_name)
573 0 : {
574 0 : if (!strcmp(ob_buffer->handler_name, *handler_name)) {
575 0 : *handler_name = NULL;
576 0 : return 1;
577 : }
578 0 : return 0;
579 : }
580 : /* }}} */
581 :
582 : /* {{{ php_ob_used
583 : * returns 1 if given handler_name is used as output_handler
584 : */
585 : PHPAPI int php_ob_handler_used(char *handler_name TSRMLS_DC)
586 4 : {
587 4 : char *tmp = handler_name;
588 :
589 4 : if (OG(ob_nesting_level)) {
590 4 : if (!strcmp(OG(active_ob_buffer).handler_name, handler_name)) {
591 1 : return 1;
592 : }
593 3 : if (OG(ob_nesting_level)>1) {
594 0 : zend_stack_apply_with_argument(&OG(ob_buffers), ZEND_STACK_APPLY_BOTTOMUP, (int (*)(void *element, void *)) php_ob_handler_used_each, &tmp);
595 : }
596 : }
597 3 : return tmp ? 0 : 1;
598 : }
599 : /* }}} */
600 :
601 : /* {{{ php_ob_append
602 : */
603 : static inline void php_ob_append(const char *text, uint text_length TSRMLS_DC)
604 208897 : {
605 : char *target;
606 : int original_ob_text_length;
607 :
608 208897 : original_ob_text_length=OG(active_ob_buffer).text_length;
609 :
610 208897 : php_ob_allocate(text_length TSRMLS_CC);
611 208897 : target = OG(active_ob_buffer).buffer+original_ob_text_length;
612 208897 : memcpy(target, text, text_length);
613 208897 : target[text_length]=0;
614 :
615 : /* If implicit_flush is On or chunked buffering, send contents to next buffer and return. */
616 208897 : if (OG(active_ob_buffer).chunk_size
617 : && OG(active_ob_buffer).text_length >= OG(active_ob_buffer).chunk_size) {
618 :
619 316 : php_end_ob_buffer(1, 1 TSRMLS_CC);
620 316 : return;
621 : }
622 : }
623 : /* }}} */
624 :
625 : #if 0
626 : static inline void php_ob_prepend(const char *text, uint text_length)
627 : {
628 : char *p, *start;
629 : TSRMLS_FETCH();
630 :
631 : php_ob_allocate(text_length TSRMLS_CC);
632 :
633 : /* php_ob_allocate() may change OG(ob_buffer), so we can't initialize p&start earlier */
634 : p = OG(ob_buffer)+OG(ob_text_length);
635 : start = OG(ob_buffer);
636 :
637 : while (--p>=start) {
638 : p[text_length] = *p;
639 : }
640 : memcpy(OG(ob_buffer), text, text_length);
641 : OG(ob_buffer)[OG(active_ob_buffer).text_length]=0;
642 : }
643 : #endif
644 :
645 :
646 : /* {{{ php_ob_get_buffer
647 : * Return the current output buffer */
648 : PHPAPI int php_ob_get_buffer(zval *p TSRMLS_DC)
649 5081 : {
650 5081 : if (OG(ob_nesting_level)==0) {
651 7 : return FAILURE;
652 : }
653 5074 : ZVAL_STRINGL(p, OG(active_ob_buffer).buffer, OG(active_ob_buffer).text_length, 1);
654 5074 : return SUCCESS;
655 : }
656 : /* }}} */
657 :
658 : /* {{{ php_ob_get_length
659 : * Return the size of the current output buffer */
660 : PHPAPI int php_ob_get_length(zval *p TSRMLS_DC)
661 22 : {
662 22 : if (OG(ob_nesting_level) == 0) {
663 3 : return FAILURE;
664 : }
665 19 : ZVAL_LONG(p, OG(active_ob_buffer).text_length);
666 19 : return SUCCESS;
667 : }
668 : /* }}} */
669 :
670 : /*
671 : * Wrapper functions - implementation
672 : */
673 :
674 :
675 : /* buffered output function */
676 : static int php_b_body_write(const char *str, uint str_length TSRMLS_DC)
677 208897 : {
678 208897 : php_ob_append(str, str_length TSRMLS_CC);
679 208897 : return str_length;
680 : }
681 :
682 : /* {{{ php_ub_body_write_no_header
683 : */
684 : PHPAPI int php_ub_body_write_no_header(const char *str, uint str_length TSRMLS_DC)
685 868884 : {
686 : int result;
687 :
688 868884 : if (OG(disable_output)) {
689 0 : return 0;
690 : }
691 :
692 868884 : result = OG(php_header_write)(str, str_length TSRMLS_CC);
693 :
694 868881 : if (OG(implicit_flush)) {
695 865131 : sapi_flush(TSRMLS_C);
696 : }
697 :
698 868881 : return result;
699 : }
700 : /* }}} */
701 :
702 : /* {{{ php_ub_body_write
703 : */
704 : PHPAPI int php_ub_body_write(const char *str, uint str_length TSRMLS_DC)
705 11516 : {
706 11516 : int result = 0;
707 :
708 11516 : if (SG(request_info).headers_only) {
709 0 : if(SG(headers_sent)) {
710 0 : return 0;
711 : }
712 0 : php_header(TSRMLS_C);
713 0 : zend_bailout();
714 : }
715 11516 : if (php_header(TSRMLS_C)) {
716 11516 : if (zend_is_compiling(TSRMLS_C)) {
717 205 : OG(output_start_filename) = zend_get_compiled_filename(TSRMLS_C);
718 205 : OG(output_start_lineno) = zend_get_compiled_lineno(TSRMLS_C);
719 11311 : } else if (zend_is_executing(TSRMLS_C)) {
720 11140 : OG(output_start_filename) = zend_get_executed_filename(TSRMLS_C);
721 11140 : OG(output_start_lineno) = zend_get_executed_lineno(TSRMLS_C);
722 : }
723 :
724 11516 : OG(php_body_write) = php_ub_body_write_no_header;
725 11516 : result = php_ub_body_write_no_header(str, str_length TSRMLS_CC);
726 : }
727 :
728 11513 : return result;
729 : }
730 : /* }}} */
731 :
732 : /*
733 : * HEAD support
734 : */
735 :
736 : /* {{{ proto bool ob_start([ string|array user_function [, int chunk_size [, bool erase]]])
737 : Turn on Output Buffering (specifying an optional output handler). */
738 : PHP_FUNCTION(ob_start)
739 3992 : {
740 3992 : zval *output_handler=NULL;
741 3992 : long chunk_size=0;
742 3992 : zend_bool erase=1;
743 :
744 3992 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|zlb", &output_handler, &chunk_size, &erase) == FAILURE) {
745 2 : return;
746 : }
747 :
748 3990 : if (chunk_size < 0)
749 2 : chunk_size = 0;
750 :
751 3990 : if (php_start_ob_buffer(output_handler, chunk_size, erase TSRMLS_CC)==FAILURE) {
752 7 : RETURN_FALSE;
753 : }
754 3980 : RETURN_TRUE;
755 : }
756 : /* }}} */
757 :
758 : /* {{{ proto bool ob_flush(void)
759 : Flush (send) contents of the output buffer. The last buffer content is sent to next buffer */
760 : PHP_FUNCTION(ob_flush)
761 22 : {
762 22 : if (zend_parse_parameters_none() == FAILURE) {
763 1 : return;
764 : }
765 :
766 21 : if (!OG(ob_nesting_level)) {
767 1 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to flush buffer. No buffer to flush.");
768 1 : RETURN_FALSE;
769 : }
770 :
771 20 : if (!OG(active_ob_buffer).status && !OG(active_ob_buffer).erase) {
772 2 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to flush buffer %s.", OG(active_ob_buffer).handler_name);
773 2 : RETURN_FALSE;
774 : }
775 :
776 18 : php_end_ob_buffer(1, 1 TSRMLS_CC);
777 18 : RETURN_TRUE;
778 : }
779 : /* }}} */
780 :
781 :
782 : /* {{{ proto bool ob_clean(void)
783 : Clean (delete) the current output buffer */
784 : PHP_FUNCTION(ob_clean)
785 12 : {
786 12 : if (zend_parse_parameters_none() == FAILURE) {
787 1 : return;
788 : }
789 :
790 11 : if (!OG(ob_nesting_level)) {
791 3 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer. No buffer to delete.");
792 3 : RETURN_FALSE;
793 : }
794 :
795 8 : if (!OG(active_ob_buffer).status && !OG(active_ob_buffer).erase) {
796 1 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer %s.", OG(active_ob_buffer).handler_name);
797 1 : RETURN_FALSE;
798 : }
799 :
800 7 : php_end_ob_buffer(0, 1 TSRMLS_CC);
801 7 : RETURN_TRUE;
802 : }
803 : /* }}} */
804 :
805 : /* {{{ proto bool ob_end_flush(void)
806 : Flush (send) the output buffer, and delete current output buffer */
807 : PHP_FUNCTION(ob_end_flush)
808 202 : {
809 202 : if (zend_parse_parameters_none() == FAILURE) {
810 1 : return;
811 : }
812 :
813 201 : if (!OG(ob_nesting_level)) {
814 3 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete and flush buffer. No buffer to delete or flush.");
815 3 : RETURN_FALSE;
816 : }
817 198 : if (OG(ob_nesting_level) && !OG(active_ob_buffer).status && !OG(active_ob_buffer).erase) {
818 1 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer %s.", OG(active_ob_buffer).handler_name);
819 1 : RETURN_FALSE;
820 : }
821 :
822 197 : php_end_ob_buffer(1, 0 TSRMLS_CC);
823 197 : RETURN_TRUE;
824 : }
825 : /* }}} */
826 :
827 : /* {{{ proto bool ob_end_clean(void)
828 : Clean the output buffer, and delete current output buffer */
829 : PHP_FUNCTION(ob_end_clean)
830 235 : {
831 235 : if (zend_parse_parameters_none() == FAILURE) {
832 1 : return;
833 : }
834 :
835 234 : if (!OG(ob_nesting_level)) {
836 3 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer. No buffer to delete.");
837 3 : RETURN_FALSE;
838 : }
839 231 : if (OG(ob_nesting_level) && !OG(active_ob_buffer).status && !OG(active_ob_buffer).erase) {
840 1 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer %s.", OG(active_ob_buffer).handler_name);
841 1 : RETURN_FALSE;
842 : }
843 :
844 230 : php_end_ob_buffer(0, 0 TSRMLS_CC);
845 230 : RETURN_TRUE;
846 : }
847 : /* }}} */
848 :
849 : /* {{{ proto bool ob_get_flush(void)
850 : Get current buffer contents, flush (send) the output buffer, and delete current output buffer */
851 : PHP_FUNCTION(ob_get_flush)
852 6 : {
853 6 : if (zend_parse_parameters_none() == FAILURE) {
854 1 : return;
855 : }
856 :
857 : /* get contents */
858 5 : if (php_ob_get_buffer(return_value TSRMLS_CC)==FAILURE) {
859 1 : RETURN_FALSE;
860 : }
861 : /* error checks */
862 4 : if (!OG(ob_nesting_level)) {
863 0 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete and flush buffer. No buffer to delete or flush.");
864 0 : zval_dtor(return_value);
865 0 : RETURN_FALSE;
866 : }
867 4 : if (OG(ob_nesting_level) && !OG(active_ob_buffer).status && !OG(active_ob_buffer).erase) {
868 1 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer %s.", OG(active_ob_buffer).handler_name);
869 1 : zval_dtor(return_value);
870 1 : RETURN_FALSE;
871 : }
872 : /* flush */
873 3 : php_end_ob_buffer(1, 0 TSRMLS_CC);
874 : }
875 : /* }}} */
876 :
877 : /* {{{ proto bool ob_get_clean(void)
878 : Get current buffer contents and delete current output buffer */
879 : PHP_FUNCTION(ob_get_clean)
880 3511 : {
881 3511 : if (zend_parse_parameters_none() == FAILURE) {
882 1 : return;
883 : }
884 : /* get contents */
885 3510 : if (php_ob_get_buffer(return_value TSRMLS_CC)==FAILURE) {
886 1 : RETURN_FALSE;
887 : }
888 : /* error checks */
889 3509 : if (!OG(ob_nesting_level)) {
890 0 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer. No buffer to delete.");
891 0 : zval_dtor(return_value);
892 0 : RETURN_FALSE;
893 : }
894 3509 : if (OG(ob_nesting_level) && !OG(active_ob_buffer).status && !OG(active_ob_buffer).erase) {
895 1 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer %s.", OG(active_ob_buffer).handler_name);
896 1 : zval_dtor(return_value);
897 1 : RETURN_FALSE;
898 : }
899 : /* delete buffer */
900 3508 : php_end_ob_buffer(0, 0 TSRMLS_CC);
901 : }
902 : /* }}} */
903 :
904 : /* {{{ proto string ob_get_contents(void)
905 : Return the contents of the output buffer */
906 : PHP_FUNCTION(ob_get_contents)
907 140 : {
908 140 : if (zend_parse_parameters_none() == FAILURE) {
909 2 : return;
910 : }
911 :
912 138 : if (php_ob_get_buffer(return_value TSRMLS_CC)==FAILURE) {
913 5 : RETURN_FALSE;
914 : }
915 : }
916 : /* }}} */
917 :
918 : /* {{{ proto int ob_get_level(void)
919 : Return the nesting level of the output buffer */
920 : PHP_FUNCTION(ob_get_level)
921 37 : {
922 37 : if (zend_parse_parameters_none() == FAILURE) {
923 1 : return;
924 : }
925 :
926 36 : RETURN_LONG (OG(ob_nesting_level));
927 : }
928 : /* }}} */
929 :
930 : /* {{{ proto int ob_get_length(void)
931 : Return the length of the output buffer */
932 : PHP_FUNCTION(ob_get_length)
933 15 : {
934 15 : if (zend_parse_parameters_none() == FAILURE) {
935 2 : return;
936 : }
937 :
938 13 : if (php_ob_get_length(return_value TSRMLS_CC)==FAILURE) {
939 3 : RETURN_FALSE;
940 : }
941 : }
942 : /* }}} */
943 :
944 : /* {{{ int php_ob_buffer_status(php_ob_buffer *ob_buffer, zval *result) */
945 : static int php_ob_buffer_status(php_ob_buffer *ob_buffer, zval *result)
946 6 : {
947 : zval *elem;
948 :
949 6 : MAKE_STD_ZVAL(elem);
950 6 : array_init(elem);
951 :
952 6 : add_assoc_long(elem, "chunk_size", ob_buffer->chunk_size);
953 6 : if (!ob_buffer->chunk_size) {
954 6 : add_assoc_long(elem, "size", ob_buffer->size);
955 6 : add_assoc_long(elem, "block_size", ob_buffer->block_size);
956 : }
957 6 : if (ob_buffer->internal_output_handler) {
958 0 : add_assoc_long(elem, "type", PHP_OUTPUT_HANDLER_INTERNAL);
959 0 : add_assoc_long(elem, "buffer_size", ob_buffer->internal_output_handler_buffer_size);
960 : }
961 : else {
962 6 : add_assoc_long(elem, "type", PHP_OUTPUT_HANDLER_USER);
963 : }
964 6 : add_assoc_long(elem, "status", ob_buffer->status);
965 6 : add_assoc_string(elem, "name", ob_buffer->handler_name, 1);
966 6 : add_assoc_bool(elem, "del", ob_buffer->erase);
967 6 : add_next_index_zval(result, elem);
968 :
969 6 : return SUCCESS;
970 : }
971 : /* }}} */
972 :
973 :
974 : /* {{{ proto false|array ob_get_status([bool full_status])
975 : Return the status of the active or all output buffers */
976 : PHP_FUNCTION(ob_get_status)
977 3 : {
978 3 : zend_bool full_status = 0;
979 :
980 3 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &full_status) == FAILURE ) {
981 0 : return;
982 : }
983 :
984 3 : array_init(return_value);
985 :
986 3 : if (full_status) {
987 2 : if (OG(ob_nesting_level)>1) {
988 1 : zend_stack_apply_with_argument(&OG(ob_buffers), ZEND_STACK_APPLY_BOTTOMUP, (int (*)(void *elem, void *))php_ob_buffer_status, return_value);
989 : }
990 2 : if (OG(ob_nesting_level)>0 && php_ob_buffer_status(&OG(active_ob_buffer), return_value)==FAILURE) {
991 0 : RETURN_FALSE;
992 : }
993 1 : } else if (OG(ob_nesting_level)>0) {
994 1 : add_assoc_long(return_value, "level", OG(ob_nesting_level));
995 1 : if (OG(active_ob_buffer).internal_output_handler) {
996 0 : add_assoc_long(return_value, "type", PHP_OUTPUT_HANDLER_INTERNAL);
997 : } else {
998 1 : add_assoc_long(return_value, "type", PHP_OUTPUT_HANDLER_USER);
999 : }
1000 1 : add_assoc_long(return_value, "status", OG(active_ob_buffer).status);
1001 1 : add_assoc_string(return_value, "name", OG(active_ob_buffer).handler_name, 1);
1002 1 : add_assoc_bool(return_value, "del", OG(active_ob_buffer).erase);
1003 : }
1004 : }
1005 : /* }}} */
1006 :
1007 :
1008 : /* {{{ proto void ob_implicit_flush([int flag])
1009 : Turn implicit flush on/off and is equivalent to calling flush() after every output call */
1010 : PHP_FUNCTION(ob_implicit_flush)
1011 30 : {
1012 30 : long flag = 1;
1013 :
1014 30 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &flag) == FAILURE) {
1015 13 : return;
1016 : }
1017 :
1018 17 : if (flag) {
1019 9 : php_start_implicit_flush(TSRMLS_C);
1020 : } else {
1021 8 : php_end_implicit_flush(TSRMLS_C);
1022 : }
1023 : }
1024 : /* }}} */
1025 :
1026 :
1027 : /* {{{ char *php_get_output_start_filename(TSRMLS_D)
1028 : Return filename start output something */
1029 : PHPAPI char *php_get_output_start_filename(TSRMLS_D)
1030 558 : {
1031 558 : return OG(output_start_filename);
1032 : }
1033 : /* }}} */
1034 :
1035 :
1036 : /* {{{ char *php_get_output_start_lineno(TSRMLS_D)
1037 : Return line number start output something */
1038 : PHPAPI int php_get_output_start_lineno(TSRMLS_D)
1039 558 : {
1040 558 : return OG(output_start_lineno);
1041 : }
1042 : /* }}} */
1043 :
1044 :
1045 : /* {{{ proto bool output_reset_rewrite_vars(void)
1046 : Reset(clear) URL rewriter values */
1047 : PHP_FUNCTION(output_reset_rewrite_vars)
1048 1 : {
1049 1 : if (php_url_scanner_reset_vars(TSRMLS_C) == SUCCESS) {
1050 1 : RETURN_TRUE;
1051 : } else {
1052 0 : RETURN_FALSE;
1053 : }
1054 : }
1055 : /* }}} */
1056 :
1057 :
1058 : /* {{{ proto bool output_add_rewrite_var(string name, string value)
1059 : Add URL rewriter values */
1060 : PHP_FUNCTION(output_add_rewrite_var)
1061 3 : {
1062 : char *name, *value;
1063 : int name_len, value_len;
1064 :
1065 3 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &name, &name_len, &value, &value_len) == FAILURE) {
1066 0 : return;
1067 : }
1068 :
1069 3 : if (php_url_scanner_add_var(name, name_len, value, value_len, 1 TSRMLS_CC) == SUCCESS) {
1070 3 : RETURN_TRUE;
1071 : } else {
1072 0 : RETURN_FALSE;
1073 : }
1074 : }
1075 : /* }}} */
1076 :
1077 : /*
1078 : * Local variables:
1079 : * tab-width: 4
1080 : * c-basic-offset: 4
1081 : * End:
1082 : * vim600: sw=4 ts=4 fdm=marker
1083 : * vim<600: sw=4 ts=4
1084 : */
|