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 14 : {
52 14 : fwrite(str, 1, str_len, stderr);
53 14 : 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 13565 : {
60 13565 : OG(php_body_write) = php_default_output_func;
61 13565 : OG(php_header_write) = php_default_output_func;
62 13565 : OG(implicit_flush) = 0;
63 13565 : OG(output_start_filename) = NULL;
64 13565 : OG(output_start_lineno) = 0;
65 13565 : }
66 : /* }}} */
67 :
68 :
69 : /* {{{ php_output_startup
70 : Start output layer */
71 : PHPAPI void php_output_startup(void)
72 13565 : {
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 13565 : php_output_init_globals(&output_globals TSRMLS_CC);
77 : #endif
78 13565 : }
79 : /* }}} */
80 :
81 :
82 : /* {{{ php_output_activate
83 : Initilize output global for activation */
84 : PHPAPI void php_output_activate(TSRMLS_D)
85 13551 : {
86 13551 : OG(php_body_write) = php_ub_body_write;
87 13551 : OG(php_header_write) = sapi_module.ub_write;
88 13551 : OG(ob_nesting_level) = 0;
89 13551 : OG(ob_lock) = 0;
90 13551 : OG(disable_output) = 0;
91 13551 : OG(output_start_filename) = NULL;
92 13551 : OG(output_start_lineno) = 0;
93 13551 : }
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 13565 : {
108 13565 : REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_START", PHP_OUTPUT_HANDLER_START, CONST_CS | CONST_PERSISTENT);
109 13565 : REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_CONT", PHP_OUTPUT_HANDLER_CONT, CONST_CS | CONST_PERSISTENT);
110 13565 : REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_END", PHP_OUTPUT_HANDLER_END, CONST_CS | CONST_PERSISTENT);
111 13565 : }
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 798375 : {
119 798375 : 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 351 : {
127 351 : if (OG(disable_output)) {
128 0 : return 0;
129 : } else {
130 351 : 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 4887 : {
139 : uint initial_size, block_size;
140 :
141 4887 : 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 4885 : if (chunk_size > 0) {
152 32 : if (chunk_size==1) {
153 3 : chunk_size = 4096;
154 : }
155 32 : initial_size = (chunk_size*3/2);
156 32 : block_size = chunk_size/2;
157 : } else {
158 4853 : initial_size = 40*1024;
159 4853 : block_size = 10*1024;
160 : }
161 4885 : 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 4970 : {
187 4970 : char *final_buffer=NULL;
188 4970 : unsigned int final_buffer_length=0;
189 4970 : zval *alternate_buffer=NULL;
190 : char *to_be_destroyed_buffer, *to_be_destroyed_handler_name;
191 4970 : char *to_be_destroyed_handled_output[2] = { 0, 0 };
192 : int status;
193 4970 : php_ob_buffer *prev_ob_buffer_p=NULL;
194 : php_ob_buffer orig_ob_buffer;
195 :
196 4970 : if (OG(ob_nesting_level)==0) {
197 0 : return;
198 : }
199 4970 : status = 0;
200 4970 : if (!OG(active_ob_buffer).status & PHP_OUTPUT_HANDLER_START) {
201 : /* our first call */
202 4884 : status |= PHP_OUTPUT_HANDLER_START;
203 : }
204 4970 : if (just_flush) {
205 86 : status |= PHP_OUTPUT_HANDLER_CONT;
206 : } else {
207 4884 : 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 4970 : 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 4952 : } else if (OG(active_ob_buffer).output_handler) {
224 : zval **params[2];
225 : zval *orig_buffer;
226 : zval *z_status;
227 :
228 104 : ALLOC_INIT_ZVAL(orig_buffer);
229 104 : ZVAL_STRINGL(orig_buffer, OG(active_ob_buffer).buffer, OG(active_ob_buffer).text_length, 1);
230 :
231 104 : ALLOC_INIT_ZVAL(z_status);
232 104 : ZVAL_LONG(z_status, status);
233 :
234 104 : params[0] = &orig_buffer;
235 104 : params[1] = &z_status;
236 104 : OG(ob_lock) = 1;
237 :
238 104 : 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 102 : if (alternate_buffer && !(Z_TYPE_P(alternate_buffer)==IS_BOOL && Z_BVAL_P(alternate_buffer)==0)) {
240 100 : convert_to_string_ex(&alternate_buffer);
241 100 : final_buffer = Z_STRVAL_P(alternate_buffer);
242 100 : final_buffer_length = Z_STRLEN_P(alternate_buffer);
243 : }
244 : }
245 102 : OG(ob_lock) = 0;
246 102 : if (!just_flush) {
247 72 : zval_ptr_dtor(&OG(active_ob_buffer).output_handler);
248 : }
249 102 : zval_ptr_dtor(&orig_buffer);
250 102 : zval_ptr_dtor(&z_status);
251 : }
252 :
253 4968 : if (!final_buffer) {
254 4851 : final_buffer = OG(active_ob_buffer).buffer;
255 4851 : final_buffer_length = OG(active_ob_buffer).text_length;
256 : }
257 :
258 4968 : if (OG(ob_nesting_level)==1) { /* end buffering */
259 9154 : if (SG(headers_sent) && !SG(request_info).headers_only) {
260 4359 : OG(php_body_write) = php_ub_body_write_no_header;
261 : } else {
262 436 : OG(php_body_write) = php_ub_body_write;
263 : }
264 : }
265 :
266 4968 : to_be_destroyed_buffer = OG(active_ob_buffer).buffer;
267 4968 : to_be_destroyed_handler_name = OG(active_ob_buffer).handler_name;
268 4968 : 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 4968 : if (!just_flush) {
275 4882 : 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 4968 : if (OG(ob_nesting_level)>1) { /* restore previous buffer */
280 173 : zend_stack_top(&OG(ob_buffers), (void **) &prev_ob_buffer_p);
281 173 : orig_ob_buffer = OG(active_ob_buffer);
282 173 : OG(active_ob_buffer) = *prev_ob_buffer_p;
283 173 : zend_stack_del_top(&OG(ob_buffers));
284 173 : if (!just_flush && OG(ob_nesting_level)==2) { /* destroy the stack */
285 150 : zend_stack_destroy(&OG(ob_buffers));
286 : }
287 : }
288 4968 : OG(ob_nesting_level)--;
289 :
290 4968 : if (send_buffer) {
291 360 : if (just_flush) { /* if flush is called prior to proper end, ensure presence of NUL */
292 79 : final_buffer[final_buffer_length] = '\0';
293 : }
294 360 : OG(php_body_write)(final_buffer, final_buffer_length TSRMLS_CC);
295 : }
296 :
297 4968 : if (just_flush) { /* we restored the previous ob, return to the current */
298 86 : if (prev_ob_buffer_p) {
299 13 : zend_stack_push(&OG(ob_buffers), &OG(active_ob_buffer), sizeof(php_ob_buffer));
300 13 : OG(active_ob_buffer) = orig_ob_buffer;
301 : }
302 86 : OG(ob_nesting_level)++;
303 : }
304 :
305 4968 : if (alternate_buffer) {
306 101 : zval_ptr_dtor(&alternate_buffer);
307 : }
308 :
309 4968 : if (status & PHP_OUTPUT_HANDLER_END) {
310 4882 : efree(to_be_destroyed_handler_name);
311 : }
312 4968 : if (!just_flush) {
313 4882 : efree(to_be_destroyed_buffer);
314 : } else {
315 86 : OG(active_ob_buffer).text_length = 0;
316 86 : OG(active_ob_buffer).status |= PHP_OUTPUT_HANDLER_START;
317 86 : OG(php_body_write) = php_b_body_write;
318 : }
319 4968 : if (to_be_destroyed_handled_output[0]) {
320 15 : efree(to_be_destroyed_handled_output[0]);
321 : }
322 4968 : 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 13611 : {
332 27315 : while (OG(ob_nesting_level)!=0) {
333 95 : php_end_ob_buffer(send_buffer, 0 TSRMLS_CC);
334 : }
335 13609 : }
336 : /* }}} */
337 :
338 : /* {{{ php_start_implicit_flush
339 : */
340 : PHPAPI void php_start_implicit_flush(TSRMLS_D)
341 13462 : {
342 13462 : OG(implicit_flush)=1;
343 13462 : }
344 : /* }}} */
345 :
346 : /* {{{ php_end_implicit_flush
347 : */
348 : PHPAPI void php_end_implicit_flush(TSRMLS_D)
349 15 : {
350 15 : OG(implicit_flush)=0;
351 15 : }
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 48479 : {
381 48479 : uint new_len = OG(active_ob_buffer).text_length + text_length;
382 :
383 48479 : if (OG(active_ob_buffer).size < new_len) {
384 20 : uint buf_size = OG(active_ob_buffer).size;
385 62 : while (buf_size <= new_len) {
386 22 : buf_size += OG(active_ob_buffer).block_size;
387 : }
388 :
389 20 : OG(active_ob_buffer).buffer = (char *) erealloc(OG(active_ob_buffer).buffer, buf_size+1);
390 20 : OG(active_ob_buffer).size = buf_size;
391 : }
392 48479 : OG(active_ob_buffer).text_length = new_len;
393 48479 : }
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 4892 : {
413 : php_ob_buffer tmp_buf;
414 :
415 4892 : if (output_handler && !zend_is_callable(output_handler, 0, NULL)) {
416 7 : return FAILURE;
417 : }
418 :
419 4885 : tmp_buf.block_size = block_size;
420 4885 : tmp_buf.size = initial_size;
421 4885 : tmp_buf.buffer = (char *) emalloc(initial_size+1);
422 4885 : tmp_buf.text_length = 0;
423 4885 : tmp_buf.output_handler = output_handler;
424 4885 : tmp_buf.chunk_size = chunk_size;
425 4885 : tmp_buf.status = 0;
426 4885 : tmp_buf.internal_output_handler = NULL;
427 4885 : tmp_buf.internal_output_handler_buffer = NULL;
428 4885 : tmp_buf.internal_output_handler_buffer_size = 0;
429 4885 : tmp_buf.handler_name = estrdup(handler_name&&handler_name[0]?handler_name:OB_DEFAULT_HANDLER_NAME);
430 4885 : tmp_buf.erase = erase;
431 :
432 4885 : if (OG(ob_nesting_level)>0) {
433 : #if HAVE_ZLIB && !defined(COMPILE_DL_ZLIB)
434 161 : if (!strncmp(handler_name, "ob_gzhandler", sizeof("ob_gzhandler")) && php_ob_gzhandler_check(TSRMLS_C)) {
435 1 : return FAILURE;
436 : }
437 : #endif
438 160 : if (OG(ob_nesting_level)==1) { /* initialize stack */
439 150 : zend_stack_init(&OG(ob_buffers));
440 : }
441 160 : zend_stack_push(&OG(ob_buffers), &OG(active_ob_buffer), sizeof(php_ob_buffer));
442 : }
443 4884 : OG(ob_nesting_level)++;
444 4884 : OG(active_ob_buffer) = tmp_buf;
445 4884 : OG(php_body_write) = php_b_body_write;
446 4884 : 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 77 : {
455 : zval *output_handler;
456 :
457 77 : ALLOC_INIT_ZVAL(output_handler);
458 77 : Z_STRLEN_P(output_handler) = len;
459 77 : Z_STRVAL_P(output_handler) = estrndup(handler_name, len);
460 77 : Z_TYPE_P(output_handler) = IS_STRING;
461 77 : 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 4905 : {
469 4905 : 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 4982 : if (output_handler && output_handler->type == IS_STRING) {
476 77 : handler_name = Z_STRVAL_P(output_handler);
477 77 : handler_len = Z_STRLEN_P(output_handler);
478 :
479 77 : result = SUCCESS;
480 77 : if (handler_len && handler_name[0] != '\0') {
481 152 : 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 77 : if (result == SUCCESS) {
496 77 : handler_zval = php_ob_handler_from_string(handler_name, handler_len TSRMLS_CC);
497 77 : result = php_ob_init_named(initial_size, block_size, handler_name, handler_zval, chunk_size, erase TSRMLS_CC);
498 77 : if (result != SUCCESS) {
499 8 : zval_dtor(handler_zval);
500 8 : FREE_ZVAL(handler_zval);
501 : }
502 : }
503 4842 : } 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)) {
506 5 : SEPARATE_ZVAL(&output_handler);
507 5 : output_handler->refcount++;
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 4812 : } else if (output_handler && output_handler->type == IS_OBJECT) {
523 2 : 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);
524 0 : result = FAILURE;
525 : } else {
526 4810 : result = php_ob_init_named(initial_size, block_size, OB_DEFAULT_HANDLER_NAME, NULL, chunk_size, erase TSRMLS_CC);
527 : }
528 4901 : return result;
529 : }
530 : /* }}} */
531 :
532 : /* {{{ php_ob_list_each
533 : */
534 : static int php_ob_list_each(php_ob_buffer *ob_buffer, zval *ob_handler_array)
535 24 : {
536 24 : add_next_index_string(ob_handler_array, ob_buffer->handler_name, 1);
537 24 : return 0;
538 : }
539 : /* }}} */
540 :
541 : /* {{{ proto false|array ob_list_handlers()
542 : * List all output_buffers in an array
543 : */
544 : PHP_FUNCTION(ob_list_handlers)
545 15 : {
546 15 : if (ZEND_NUM_ARGS()!=0) {
547 0 : ZEND_WRONG_PARAM_COUNT();
548 : RETURN_FALSE;
549 : }
550 :
551 15 : array_init(return_value);
552 15 : if (OG(ob_nesting_level)) {
553 12 : if (OG(ob_nesting_level)>1) {
554 5 : zend_stack_apply_with_argument(&OG(ob_buffers), ZEND_STACK_APPLY_BOTTOMUP, (int (*)(void *element, void *)) php_ob_list_each, return_value);
555 : }
556 12 : php_ob_list_each(&OG(active_ob_buffer), return_value);
557 : }
558 : }
559 : /* }}} */
560 :
561 : /* {{{ php_ob_used_each
562 : * Sets handler_name to NULL is found
563 : */
564 : static int php_ob_handler_used_each(php_ob_buffer *ob_buffer, char **handler_name)
565 0 : {
566 0 : if (!strcmp(ob_buffer->handler_name, *handler_name)) {
567 0 : *handler_name = NULL;
568 0 : return 1;
569 : }
570 0 : return 0;
571 : }
572 : /* }}} */
573 :
574 : /* {{{ php_ob_used
575 : * returns 1 if given handler_name is used as output_handler
576 : */
577 : PHPAPI int php_ob_handler_used(char *handler_name TSRMLS_DC)
578 4 : {
579 4 : char *tmp = handler_name;
580 :
581 4 : if (OG(ob_nesting_level)) {
582 4 : if (!strcmp(OG(active_ob_buffer).handler_name, handler_name)) {
583 1 : return 1;
584 : }
585 3 : if (OG(ob_nesting_level)>1) {
586 0 : zend_stack_apply_with_argument(&OG(ob_buffers), ZEND_STACK_APPLY_BOTTOMUP, (int (*)(void *element, void *)) php_ob_handler_used_each, &tmp);
587 : }
588 : }
589 3 : return tmp ? 0 : 1;
590 : }
591 : /* }}} */
592 :
593 : /* {{{ php_ob_append
594 : */
595 : static inline void php_ob_append(const char *text, uint text_length TSRMLS_DC)
596 48479 : {
597 : char *target;
598 : int original_ob_text_length;
599 :
600 48479 : original_ob_text_length=OG(active_ob_buffer).text_length;
601 :
602 48479 : php_ob_allocate(text_length TSRMLS_CC);
603 48479 : target = OG(active_ob_buffer).buffer+original_ob_text_length;
604 48479 : memcpy(target, text, text_length);
605 48479 : target[text_length]=0;
606 :
607 : /* If implicit_flush is On or chunked buffering, send contents to next buffer and return. */
608 48479 : if (OG(active_ob_buffer).chunk_size
609 : && OG(active_ob_buffer).text_length >= OG(active_ob_buffer).chunk_size) {
610 :
611 60 : php_end_ob_buffer(1, 1 TSRMLS_CC);
612 60 : return;
613 : }
614 : }
615 : /* }}} */
616 :
617 : #if 0
618 : static inline void php_ob_prepend(const char *text, uint text_length)
619 : {
620 : char *p, *start;
621 : TSRMLS_FETCH();
622 :
623 : php_ob_allocate(text_length TSRMLS_CC);
624 :
625 : /* php_ob_allocate() may change OG(ob_buffer), so we can't initialize p&start earlier */
626 : p = OG(ob_buffer)+OG(ob_text_length);
627 : start = OG(ob_buffer);
628 :
629 : while (--p>=start) {
630 : p[text_length] = *p;
631 : }
632 : memcpy(OG(ob_buffer), text, text_length);
633 : OG(ob_buffer)[OG(active_ob_buffer).text_length]=0;
634 : }
635 : #endif
636 :
637 :
638 : /* {{{ php_ob_get_buffer
639 : * Return the current output buffer */
640 : PHPAPI int php_ob_get_buffer(zval *p TSRMLS_DC)
641 4075 : {
642 4075 : if (OG(ob_nesting_level)==0) {
643 6 : return FAILURE;
644 : }
645 4069 : ZVAL_STRINGL(p, OG(active_ob_buffer).buffer, OG(active_ob_buffer).text_length, 1);
646 4069 : return SUCCESS;
647 : }
648 : /* }}} */
649 :
650 : /* {{{ php_ob_get_length
651 : * Return the size of the current output buffer */
652 : PHPAPI int php_ob_get_length(zval *p TSRMLS_DC)
653 15 : {
654 15 : if (OG(ob_nesting_level) == 0) {
655 2 : return FAILURE;
656 : }
657 13 : ZVAL_LONG(p, OG(active_ob_buffer).text_length);
658 13 : return SUCCESS;
659 : }
660 : /* }}} */
661 :
662 : /*
663 : * Wrapper functions - implementation
664 : */
665 :
666 :
667 : /* buffered output function */
668 : static int php_b_body_write(const char *str, uint str_length TSRMLS_DC)
669 48479 : {
670 48479 : php_ob_append(str, str_length TSRMLS_CC);
671 48479 : return str_length;
672 : }
673 :
674 : /* {{{ php_ub_body_write_no_header
675 : */
676 : PHPAPI int php_ub_body_write_no_header(const char *str, uint str_length TSRMLS_DC)
677 750242 : {
678 : int result;
679 :
680 750242 : if (OG(disable_output)) {
681 0 : return 0;
682 : }
683 :
684 750242 : result = OG(php_header_write)(str, str_length TSRMLS_CC);
685 :
686 750239 : if (OG(implicit_flush)) {
687 747850 : sapi_flush(TSRMLS_C);
688 : }
689 :
690 750239 : return result;
691 : }
692 : /* }}} */
693 :
694 : /* {{{ php_ub_body_write
695 : */
696 : PHPAPI int php_ub_body_write(const char *str, uint str_length TSRMLS_DC)
697 9153 : {
698 9153 : int result = 0;
699 :
700 9153 : if (SG(request_info).headers_only) {
701 0 : if(SG(headers_sent)) {
702 0 : return 0;
703 : }
704 0 : php_header(TSRMLS_C);
705 0 : zend_bailout();
706 : }
707 9153 : if (php_header(TSRMLS_C)) {
708 9153 : if (zend_is_compiling(TSRMLS_C)) {
709 162 : OG(output_start_filename) = zend_get_compiled_filename(TSRMLS_C);
710 162 : OG(output_start_lineno) = zend_get_compiled_lineno(TSRMLS_C);
711 8991 : } else if (zend_is_executing(TSRMLS_C)) {
712 8862 : OG(output_start_filename) = zend_get_executed_filename(TSRMLS_C);
713 8862 : OG(output_start_lineno) = zend_get_executed_lineno(TSRMLS_C);
714 : }
715 :
716 9153 : OG(php_body_write) = php_ub_body_write_no_header;
717 9153 : result = php_ub_body_write_no_header(str, str_length TSRMLS_CC);
718 : }
719 :
720 9151 : return result;
721 : }
722 : /* }}} */
723 :
724 : /*
725 : * HEAD support
726 : */
727 :
728 : /* {{{ proto bool ob_start([ string|array user_function [, int chunk_size [, bool erase]]])
729 : Turn on Output Buffering (specifying an optional output handler). */
730 : PHP_FUNCTION(ob_start)
731 3892 : {
732 3892 : zval *output_handler=NULL;
733 3892 : long chunk_size=0;
734 3892 : zend_bool erase=1;
735 3892 : int argc = ZEND_NUM_ARGS();
736 :
737 3892 : if (zend_parse_parameters(argc TSRMLS_CC, "|zlb", &output_handler, &chunk_size, &erase) == FAILURE) {
738 2 : RETURN_FALSE;
739 : }
740 :
741 3890 : if (chunk_size < 0)
742 2 : chunk_size = 0;
743 :
744 3890 : if (php_start_ob_buffer(output_handler, chunk_size, erase TSRMLS_CC)==FAILURE) {
745 8 : RETURN_FALSE;
746 : }
747 3879 : RETURN_TRUE;
748 : }
749 : /* }}} */
750 :
751 : /* {{{ proto bool ob_flush(void)
752 : Flush (send) contents of the output buffer. The last buffer content is sent to next buffer */
753 : PHP_FUNCTION(ob_flush)
754 21 : {
755 21 : if (ZEND_NUM_ARGS() != 0) {
756 1 : ZEND_WRONG_PARAM_COUNT();
757 : }
758 :
759 20 : if (!OG(ob_nesting_level)) {
760 1 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to flush buffer. No buffer to flush.");
761 1 : RETURN_FALSE;
762 : }
763 :
764 19 : php_end_ob_buffer(1, 1 TSRMLS_CC);
765 19 : RETURN_TRUE;
766 : }
767 : /* }}} */
768 :
769 :
770 : /* {{{ proto bool ob_clean(void)
771 : Clean (delete) the current output buffer */
772 : PHP_FUNCTION(ob_clean)
773 10 : {
774 10 : if (ZEND_NUM_ARGS() != 0) {
775 1 : ZEND_WRONG_PARAM_COUNT();
776 : }
777 :
778 9 : if (!OG(ob_nesting_level)) {
779 1 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer. No buffer to delete.");
780 1 : RETURN_FALSE;
781 : }
782 :
783 8 : if (!OG(active_ob_buffer).status && !OG(active_ob_buffer).erase) {
784 1 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer %s.", OG(active_ob_buffer).handler_name);
785 1 : RETURN_FALSE;
786 : }
787 :
788 7 : php_end_ob_buffer(0, 1 TSRMLS_CC);
789 7 : RETURN_TRUE;
790 : }
791 : /* }}} */
792 :
793 : /* {{{ proto bool ob_end_flush(void)
794 : Flush (send) the output buffer, and delete current output buffer */
795 : PHP_FUNCTION(ob_end_flush)
796 181 : {
797 181 : if (ZEND_NUM_ARGS() != 0) {
798 1 : ZEND_WRONG_PARAM_COUNT();
799 : }
800 :
801 180 : if (!OG(ob_nesting_level)) {
802 3 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete and flush buffer. No buffer to delete or flush.");
803 3 : RETURN_FALSE;
804 : }
805 177 : if (OG(ob_nesting_level) && !OG(active_ob_buffer).status && !OG(active_ob_buffer).erase) {
806 1 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer %s.", OG(active_ob_buffer).handler_name);
807 1 : RETURN_FALSE;
808 : }
809 :
810 176 : php_end_ob_buffer(1, 0 TSRMLS_CC);
811 176 : RETURN_TRUE;
812 : }
813 : /* }}} */
814 :
815 : /* {{{ proto bool ob_end_clean(void)
816 : Clean the output buffer, and delete current output buffer */
817 : PHP_FUNCTION(ob_end_clean)
818 190 : {
819 190 : if (ZEND_NUM_ARGS() != 0) {
820 1 : ZEND_WRONG_PARAM_COUNT();
821 : }
822 :
823 189 : if (!OG(ob_nesting_level)) {
824 3 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer. No buffer to delete.");
825 3 : RETURN_FALSE;
826 : }
827 186 : if (OG(ob_nesting_level) && !OG(active_ob_buffer).status && !OG(active_ob_buffer).erase) {
828 1 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer %s.", OG(active_ob_buffer).handler_name);
829 1 : RETURN_FALSE;
830 : }
831 :
832 185 : php_end_ob_buffer(0, 0 TSRMLS_CC);
833 185 : RETURN_TRUE;
834 : }
835 : /* }}} */
836 :
837 : /* {{{ proto bool ob_get_flush(void)
838 : Get current buffer contents, flush (send) the output buffer, and delete current output buffer */
839 : PHP_FUNCTION(ob_get_flush)
840 2 : {
841 2 : if (ZEND_NUM_ARGS() != 0) {
842 0 : ZEND_WRONG_PARAM_COUNT();
843 : }
844 :
845 : /* get contents */
846 2 : if (php_ob_get_buffer(return_value TSRMLS_CC)==FAILURE) {
847 0 : RETURN_FALSE;
848 : }
849 : /* error checks */
850 2 : if (!OG(ob_nesting_level)) {
851 0 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete and flush buffer. No buffer to delete or flush.");
852 0 : zval_dtor(return_value);
853 0 : RETURN_FALSE;
854 : }
855 2 : if (OG(ob_nesting_level) && !OG(active_ob_buffer).status && !OG(active_ob_buffer).erase) {
856 1 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer %s.", OG(active_ob_buffer).handler_name);
857 1 : zval_dtor(return_value);
858 1 : RETURN_FALSE;
859 : }
860 : /* flush */
861 1 : php_end_ob_buffer(1, 0 TSRMLS_CC);
862 : }
863 : /* }}} */
864 :
865 : /* {{{ proto bool ob_get_clean(void)
866 : Get current buffer contents and delete current output buffer */
867 : PHP_FUNCTION(ob_get_clean)
868 3481 : {
869 3481 : if (ZEND_NUM_ARGS() != 0)
870 1 : ZEND_WRONG_PARAM_COUNT();
871 :
872 : /* get contents */
873 3480 : if (php_ob_get_buffer(return_value TSRMLS_CC)==FAILURE) {
874 1 : RETURN_FALSE;
875 : }
876 : /* error checks */
877 3479 : if (!OG(ob_nesting_level)) {
878 0 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer. No buffer to delete.");
879 0 : zval_dtor(return_value);
880 0 : RETURN_FALSE;
881 : }
882 3479 : if (OG(ob_nesting_level) && !OG(active_ob_buffer).status && !OG(active_ob_buffer).erase) {
883 1 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer %s.", OG(active_ob_buffer).handler_name);
884 1 : zval_dtor(return_value);
885 1 : RETURN_FALSE;
886 : }
887 : /* delete buffer */
888 3478 : php_end_ob_buffer(0, 0 TSRMLS_CC);
889 : }
890 : /* }}} */
891 :
892 : /* {{{ proto string ob_get_contents(void)
893 : Return the contents of the output buffer */
894 : PHP_FUNCTION(ob_get_contents)
895 99 : {
896 99 : if (ZEND_NUM_ARGS() != 0) {
897 2 : ZEND_WRONG_PARAM_COUNT();
898 : }
899 :
900 97 : if (php_ob_get_buffer(return_value TSRMLS_CC)==FAILURE) {
901 5 : RETURN_FALSE;
902 : }
903 : }
904 : /* }}} */
905 :
906 : /* {{{ proto int ob_get_level(void)
907 : Return the nesting level of the output buffer */
908 : PHP_FUNCTION(ob_get_level)
909 36 : {
910 36 : if (ZEND_NUM_ARGS() != 0) {
911 1 : ZEND_WRONG_PARAM_COUNT();
912 : }
913 :
914 35 : RETURN_LONG (OG(ob_nesting_level));
915 : }
916 : /* }}} */
917 :
918 : /* {{{ proto int ob_get_length(void)
919 : Return the length of the output buffer */
920 : PHP_FUNCTION(ob_get_length)
921 7 : {
922 7 : if (ZEND_NUM_ARGS() != 0) {
923 1 : ZEND_WRONG_PARAM_COUNT();
924 : }
925 :
926 6 : if (php_ob_get_length(return_value TSRMLS_CC)==FAILURE) {
927 2 : RETURN_FALSE;
928 : }
929 : }
930 : /* }}} */
931 :
932 : /* {{{ int php_ob_buffer_status(php_ob_buffer *ob_buffer, zval *result) */
933 : static int php_ob_buffer_status(php_ob_buffer *ob_buffer, zval *result)
934 6 : {
935 : zval *elem;
936 :
937 6 : MAKE_STD_ZVAL(elem);
938 6 : array_init(elem);
939 :
940 6 : add_assoc_long(elem, "chunk_size", ob_buffer->chunk_size);
941 6 : if (!ob_buffer->chunk_size) {
942 6 : add_assoc_long(elem, "size", ob_buffer->size);
943 6 : add_assoc_long(elem, "block_size", ob_buffer->block_size);
944 : }
945 6 : if (ob_buffer->internal_output_handler) {
946 0 : add_assoc_long(elem, "type", PHP_OUTPUT_HANDLER_INTERNAL);
947 0 : add_assoc_long(elem, "buffer_size", ob_buffer->internal_output_handler_buffer_size);
948 : }
949 : else {
950 6 : add_assoc_long(elem, "type", PHP_OUTPUT_HANDLER_USER);
951 : }
952 6 : add_assoc_long(elem, "status", ob_buffer->status);
953 6 : add_assoc_string(elem, "name", ob_buffer->handler_name, 1);
954 6 : add_assoc_bool(elem, "del", ob_buffer->erase);
955 6 : add_next_index_zval(result, elem);
956 :
957 6 : return SUCCESS;
958 : }
959 : /* }}} */
960 :
961 :
962 : /* {{{ proto false|array ob_get_status([bool full_status])
963 : Return the status of the active or all output buffers */
964 : PHP_FUNCTION(ob_get_status)
965 3 : {
966 3 : int argc = ZEND_NUM_ARGS();
967 3 : zend_bool full_status = 0;
968 :
969 3 : if (zend_parse_parameters(argc TSRMLS_CC, "|b", &full_status) == FAILURE )
970 0 : RETURN_FALSE;
971 :
972 3 : array_init(return_value);
973 :
974 3 : if (full_status) {
975 2 : if (OG(ob_nesting_level)>1) {
976 1 : zend_stack_apply_with_argument(&OG(ob_buffers), ZEND_STACK_APPLY_BOTTOMUP, (int (*)(void *elem, void *))php_ob_buffer_status, return_value);
977 : }
978 2 : if (OG(ob_nesting_level)>0 && php_ob_buffer_status(&OG(active_ob_buffer), return_value)==FAILURE) {
979 0 : RETURN_FALSE;
980 : }
981 1 : } else if (OG(ob_nesting_level)>0) {
982 1 : add_assoc_long(return_value, "level", OG(ob_nesting_level));
983 1 : if (OG(active_ob_buffer).internal_output_handler) {
984 0 : add_assoc_long(return_value, "type", PHP_OUTPUT_HANDLER_INTERNAL);
985 : } else {
986 1 : add_assoc_long(return_value, "type", PHP_OUTPUT_HANDLER_USER);
987 : }
988 1 : add_assoc_long(return_value, "status", OG(active_ob_buffer).status);
989 1 : add_assoc_string(return_value, "name", OG(active_ob_buffer).handler_name, 1);
990 1 : add_assoc_bool(return_value, "del", OG(active_ob_buffer).erase);
991 : }
992 : }
993 : /* }}} */
994 :
995 :
996 : /* {{{ proto void ob_implicit_flush([int flag])
997 : Turn implicit flush on/off and is equivalent to calling flush() after every output call */
998 : PHP_FUNCTION(ob_implicit_flush)
999 30 : {
1000 : zval **zv_flag;
1001 : int flag;
1002 :
1003 30 : switch(ZEND_NUM_ARGS()) {
1004 : case 0:
1005 1 : flag = 1;
1006 1 : break;
1007 : case 1:
1008 28 : if (zend_get_parameters_ex(1, &zv_flag)==FAILURE) {
1009 0 : RETURN_FALSE;
1010 : }
1011 28 : convert_to_long_ex(zv_flag);
1012 28 : flag = Z_LVAL_PP(zv_flag);
1013 28 : break;
1014 : default:
1015 1 : ZEND_WRONG_PARAM_COUNT();
1016 : break;
1017 : }
1018 29 : if (flag) {
1019 14 : php_start_implicit_flush(TSRMLS_C);
1020 : } else {
1021 15 : 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 557 : {
1031 557 : 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 557 : {
1040 557 : 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_FALSE;
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 : */
|