1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 5 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2007 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,v 1.167.2.3.2.2 2007/01/29 11:21:31 dmitry Exp $ */
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 0 : {
52 0 : fwrite(str, 1, str_len, stderr);
53 0 : 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 221 : {
60 221 : OG(php_body_write) = php_default_output_func;
61 221 : OG(php_header_write) = php_default_output_func;
62 221 : OG(implicit_flush) = 0;
63 221 : OG(output_start_filename) = NULL;
64 221 : OG(output_start_lineno) = 0;
65 221 : }
66 : /* }}} */
67 :
68 :
69 : /* {{{ php_output_startup
70 : Start output layer */
71 : PHPAPI void php_output_startup(void)
72 221 : {
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 221 : php_output_init_globals(&output_globals TSRMLS_CC);
77 : #endif
78 221 : }
79 : /* }}} */
80 :
81 :
82 : /* {{{ php_output_activate
83 : Initilize output global for activation */
84 : PHPAPI void php_output_activate(TSRMLS_D)
85 220 : {
86 220 : OG(php_body_write) = php_ub_body_write;
87 220 : OG(php_header_write) = sapi_module.ub_write;
88 220 : OG(ob_nesting_level) = 0;
89 220 : OG(ob_lock) = 0;
90 220 : OG(disable_output) = 0;
91 220 : OG(output_start_filename) = NULL;
92 220 : OG(output_start_lineno) = 0;
93 220 : }
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 0 : {
101 0 : OG(disable_output) = !status;
102 0 : }
103 : /* }}} */
104 :
105 : /* {{{ php_output_register_constants */
106 : void php_output_register_constants(TSRMLS_D)
107 220 : {
108 220 : REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_START", PHP_OUTPUT_HANDLER_START, CONST_CS | CONST_PERSISTENT);
109 220 : REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_CONT", PHP_OUTPUT_HANDLER_CONT, CONST_CS | CONST_PERSISTENT);
110 220 : REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_END", PHP_OUTPUT_HANDLER_END, CONST_CS | CONST_PERSISTENT);
111 220 : }
112 : /* }}} */
113 :
114 :
115 : /* {{{ php_body_wirte
116 : * Write body part */
117 : PHPAPI int php_body_write(const char *str, uint str_length TSRMLS_DC)
118 3918 : {
119 3918 : return OG(php_body_write)(str, str_length TSRMLS_CC);
120 : }
121 : /* }}} */
122 :
123 : /* {{{ php_header_wirte
124 : * Write HTTP header */
125 : PHPAPI int php_header_write(const char *str, uint str_length TSRMLS_DC)
126 784 : {
127 784 : if (OG(disable_output)) {
128 0 : return 0;
129 : } else {
130 784 : 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 10 : {
139 : uint initial_size, block_size;
140 :
141 10 : if (OG(ob_lock)) {
142 0 : if (SG(headers_sent) && !SG(request_info).headers_only) {
143 0 : OG(php_body_write) = php_ub_body_write_no_header;
144 : } else {
145 0 : OG(php_body_write) = php_ub_body_write;
146 : }
147 0 : OG(ob_nesting_level) = 0;
148 0 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_ERROR, "Cannot use output buffering in output buffering display handlers");
149 0 : return FAILURE;
150 : }
151 10 : if (chunk_size > 0) {
152 0 : if (chunk_size==1) {
153 0 : chunk_size = 4096;
154 : }
155 0 : initial_size = (chunk_size*3/2);
156 0 : block_size = chunk_size/2;
157 : } else {
158 10 : initial_size = 40*1024;
159 10 : block_size = 10*1024;
160 : }
161 10 : 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 8 : {
169 : zval *output_handler;
170 : int result;
171 :
172 8 : ALLOC_INIT_ZVAL(output_handler);
173 8 : Z_STRLEN_P(output_handler) = strlen(output_handler_name); /* this can be optimized */
174 8 : Z_STRVAL_P(output_handler) = estrndup(output_handler_name, Z_STRLEN_P(output_handler));
175 8 : Z_TYPE_P(output_handler) = IS_STRING;
176 8 : result = php_start_ob_buffer(output_handler, chunk_size, erase TSRMLS_CC);
177 8 : zval_dtor(output_handler);
178 8 : FREE_ZVAL(output_handler);
179 8 : 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 10 : {
187 10 : char *final_buffer=NULL;
188 10 : unsigned int final_buffer_length=0;
189 10 : zval *alternate_buffer=NULL;
190 : char *to_be_destroyed_buffer, *to_be_destroyed_handler_name;
191 10 : char *to_be_destroyed_handled_output[2] = { 0, 0 };
192 : int status;
193 10 : php_ob_buffer *prev_ob_buffer_p=NULL;
194 : php_ob_buffer orig_ob_buffer;
195 :
196 10 : if (OG(ob_nesting_level)==0) {
197 0 : return;
198 : }
199 10 : status = 0;
200 10 : if (!OG(active_ob_buffer).status & PHP_OUTPUT_HANDLER_START) {
201 : /* our first call */
202 10 : status |= PHP_OUTPUT_HANDLER_START;
203 : }
204 10 : if (just_flush) {
205 0 : status |= PHP_OUTPUT_HANDLER_CONT;
206 : } else {
207 10 : 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 10 : if (OG(active_ob_buffer).internal_output_handler) {
220 0 : final_buffer = OG(active_ob_buffer).internal_output_handler_buffer;
221 0 : final_buffer_length = OG(active_ob_buffer).internal_output_handler_buffer_size;
222 0 : 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 10 : } else if (OG(active_ob_buffer).output_handler) {
224 : zval **params[2];
225 : zval *orig_buffer;
226 : zval *z_status;
227 :
228 10 : ALLOC_INIT_ZVAL(orig_buffer);
229 10 : ZVAL_STRINGL(orig_buffer, OG(active_ob_buffer).buffer, OG(active_ob_buffer).text_length, 1);
230 10 : orig_buffer->refcount=2; /* don't let call_user_function() destroy our buffer */
231 10 : orig_buffer->is_ref=1;
232 :
233 10 : ALLOC_INIT_ZVAL(z_status);
234 10 : ZVAL_LONG(z_status, status);
235 :
236 10 : params[0] = &orig_buffer;
237 10 : params[1] = &z_status;
238 10 : OG(ob_lock) = 1;
239 :
240 10 : if (call_user_function_ex(CG(function_table), NULL, OG(active_ob_buffer).output_handler, &alternate_buffer, 2, params, 1, NULL TSRMLS_CC)==SUCCESS) {
241 10 : if (alternate_buffer && !(Z_TYPE_P(alternate_buffer)==IS_BOOL && Z_BVAL_P(alternate_buffer)==0)) {
242 10 : convert_to_string_ex(&alternate_buffer);
243 10 : final_buffer = Z_STRVAL_P(alternate_buffer);
244 10 : final_buffer_length = Z_STRLEN_P(alternate_buffer);
245 : }
246 : }
247 10 : OG(ob_lock) = 0;
248 10 : if (!just_flush) {
249 10 : zval_ptr_dtor(&OG(active_ob_buffer).output_handler);
250 : }
251 10 : orig_buffer->refcount -=2;
252 10 : if (orig_buffer->refcount <= 0) { /* free the zval */
253 10 : zval_dtor(orig_buffer);
254 10 : FREE_ZVAL(orig_buffer);
255 : }
256 10 : zval_ptr_dtor(&z_status);
257 : }
258 :
259 10 : if (!final_buffer) {
260 0 : final_buffer = OG(active_ob_buffer).buffer;
261 0 : final_buffer_length = OG(active_ob_buffer).text_length;
262 : }
263 :
264 10 : if (OG(ob_nesting_level)==1) { /* end buffering */
265 10 : if (SG(headers_sent) && !SG(request_info).headers_only) {
266 0 : OG(php_body_write) = php_ub_body_write_no_header;
267 : } else {
268 10 : OG(php_body_write) = php_ub_body_write;
269 : }
270 : }
271 :
272 10 : to_be_destroyed_buffer = OG(active_ob_buffer).buffer;
273 10 : to_be_destroyed_handler_name = OG(active_ob_buffer).handler_name;
274 10 : if (OG(active_ob_buffer).internal_output_handler
275 : && (final_buffer != OG(active_ob_buffer).internal_output_handler_buffer)
276 : && (final_buffer != OG(active_ob_buffer).buffer)) {
277 0 : to_be_destroyed_handled_output[0] = final_buffer;
278 : }
279 :
280 10 : if (!just_flush) {
281 10 : if (OG(active_ob_buffer).internal_output_handler) {
282 0 : to_be_destroyed_handled_output[1] = OG(active_ob_buffer).internal_output_handler_buffer;
283 : }
284 : }
285 10 : if (OG(ob_nesting_level)>1) { /* restore previous buffer */
286 0 : zend_stack_top(&OG(ob_buffers), (void **) &prev_ob_buffer_p);
287 0 : orig_ob_buffer = OG(active_ob_buffer);
288 0 : OG(active_ob_buffer) = *prev_ob_buffer_p;
289 0 : zend_stack_del_top(&OG(ob_buffers));
290 0 : if (!just_flush && OG(ob_nesting_level)==2) { /* destroy the stack */
291 0 : zend_stack_destroy(&OG(ob_buffers));
292 : }
293 : }
294 10 : OG(ob_nesting_level)--;
295 :
296 10 : if (send_buffer) {
297 10 : if (just_flush) { /* if flush is called prior to proper end, ensure presence of NUL */
298 0 : final_buffer[final_buffer_length] = '\0';
299 : }
300 10 : OG(php_body_write)(final_buffer, final_buffer_length TSRMLS_CC);
301 : }
302 :
303 10 : if (just_flush) { /* we restored the previous ob, return to the current */
304 0 : if (prev_ob_buffer_p) {
305 0 : zend_stack_push(&OG(ob_buffers), &OG(active_ob_buffer), sizeof(php_ob_buffer));
306 0 : OG(active_ob_buffer) = orig_ob_buffer;
307 : }
308 0 : OG(ob_nesting_level)++;
309 : }
310 :
311 10 : if (alternate_buffer) {
312 10 : zval_ptr_dtor(&alternate_buffer);
313 : }
314 :
315 10 : if (status & PHP_OUTPUT_HANDLER_END) {
316 10 : efree(to_be_destroyed_handler_name);
317 : }
318 10 : if (!just_flush) {
319 10 : efree(to_be_destroyed_buffer);
320 : } else {
321 0 : OG(active_ob_buffer).text_length = 0;
322 0 : OG(active_ob_buffer).status |= PHP_OUTPUT_HANDLER_START;
323 0 : OG(php_body_write) = php_b_body_write;
324 : }
325 10 : if (to_be_destroyed_handled_output[0]) {
326 0 : efree(to_be_destroyed_handled_output[0]);
327 : }
328 10 : if (to_be_destroyed_handled_output[1]) {
329 0 : efree(to_be_destroyed_handled_output[1]);
330 : }
331 : }
332 : /* }}} */
333 :
334 : /* {{{ php_end_ob_buffers
335 : * End output buffering (all buffers) */
336 : PHPAPI void php_end_ob_buffers(zend_bool send_buffer TSRMLS_DC)
337 231 : {
338 472 : while (OG(ob_nesting_level)!=0) {
339 10 : php_end_ob_buffer(send_buffer, 0 TSRMLS_CC);
340 : }
341 231 : }
342 : /* }}} */
343 :
344 : /* {{{ php_start_implicit_flush
345 : */
346 : PHPAPI void php_start_implicit_flush(TSRMLS_D)
347 1 : {
348 1 : OG(implicit_flush)=1;
349 1 : }
350 : /* }}} */
351 :
352 : /* {{{ php_end_implicit_flush
353 : */
354 : PHPAPI void php_end_implicit_flush(TSRMLS_D)
355 0 : {
356 0 : OG(implicit_flush)=0;
357 0 : }
358 : /* }}} */
359 :
360 : /* {{{ php_ob_set_internal_handler
361 : */
362 : 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)
363 0 : {
364 0 : if (OG(ob_nesting_level)==0 || OG(active_ob_buffer).internal_output_handler || strcmp(OG(active_ob_buffer).handler_name, OB_DEFAULT_HANDLER_NAME)) {
365 0 : php_start_ob_buffer(NULL, buffer_size, erase TSRMLS_CC);
366 : }
367 :
368 0 : OG(active_ob_buffer).internal_output_handler = internal_output_handler;
369 0 : OG(active_ob_buffer).internal_output_handler_buffer = (char *) emalloc(buffer_size);
370 0 : OG(active_ob_buffer).internal_output_handler_buffer_size = buffer_size;
371 0 : if (OG(active_ob_buffer).handler_name) {
372 0 : efree(OG(active_ob_buffer).handler_name);
373 : }
374 0 : OG(active_ob_buffer).handler_name = estrdup(handler_name);
375 0 : OG(active_ob_buffer).erase = erase;
376 0 : }
377 : /* }}} */
378 :
379 : /*
380 : * Output buffering - implementation
381 : */
382 :
383 : /* {{{ php_ob_allocate
384 : */
385 : static inline void php_ob_allocate(uint text_length TSRMLS_DC)
386 11 : {
387 11 : uint new_len = OG(active_ob_buffer).text_length + text_length;
388 :
389 11 : if (OG(active_ob_buffer).size < new_len) {
390 0 : uint buf_size = OG(active_ob_buffer).size;
391 0 : while (buf_size <= new_len) {
392 0 : buf_size += OG(active_ob_buffer).block_size;
393 : }
394 :
395 0 : OG(active_ob_buffer).buffer = (char *) erealloc(OG(active_ob_buffer).buffer, buf_size+1);
396 0 : OG(active_ob_buffer).size = buf_size;
397 : }
398 11 : OG(active_ob_buffer).text_length = new_len;
399 11 : }
400 : /* }}} */
401 :
402 : /* {{{ php_ob_init_conflict
403 : * Returns 1 if handler_set is already used and generates error message
404 : */
405 : PHPAPI int php_ob_init_conflict(char *handler_new, char *handler_set TSRMLS_DC)
406 0 : {
407 0 : if (php_ob_handler_used(handler_set TSRMLS_CC)) {
408 0 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_WARNING, "output handler '%s' conflicts with '%s'", handler_new, handler_set);
409 0 : return 1;
410 : }
411 0 : return 0;
412 : }
413 : /* }}} */
414 :
415 : /* {{{ php_ob_init_named
416 : */
417 : 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)
418 10 : {
419 : php_ob_buffer tmp_buf;
420 :
421 10 : if (output_handler && !zend_is_callable(output_handler, 0, NULL)) {
422 0 : return FAILURE;
423 : }
424 :
425 10 : tmp_buf.block_size = block_size;
426 10 : tmp_buf.size = initial_size;
427 10 : tmp_buf.buffer = (char *) emalloc(initial_size+1);
428 10 : tmp_buf.text_length = 0;
429 10 : tmp_buf.output_handler = output_handler;
430 10 : tmp_buf.chunk_size = chunk_size;
431 10 : tmp_buf.status = 0;
432 10 : tmp_buf.internal_output_handler = NULL;
433 10 : tmp_buf.handler_name = estrdup(handler_name&&handler_name[0]?handler_name:OB_DEFAULT_HANDLER_NAME);
434 10 : tmp_buf.erase = erase;
435 :
436 10 : if (OG(ob_nesting_level)>0) {
437 : #if HAVE_ZLIB && !defined(COMPILE_DL_ZLIB)
438 : if (!strncmp(handler_name, "ob_gzhandler", sizeof("ob_gzhandler")) && php_ob_gzhandler_check(TSRMLS_C)) {
439 : return FAILURE;
440 : }
441 : #endif
442 0 : if (OG(ob_nesting_level)==1) { /* initialize stack */
443 0 : zend_stack_init(&OG(ob_buffers));
444 : }
445 0 : zend_stack_push(&OG(ob_buffers), &OG(active_ob_buffer), sizeof(php_ob_buffer));
446 : }
447 10 : OG(ob_nesting_level)++;
448 10 : OG(active_ob_buffer) = tmp_buf;
449 10 : OG(php_body_write) = php_b_body_write;
450 10 : return SUCCESS;
451 : }
452 : /* }}} */
453 :
454 : /* {{{ php_ob_handler_from_string
455 : * Create zval output handler from string
456 : */
457 : static zval* php_ob_handler_from_string(const char *handler_name, int len TSRMLS_DC)
458 10 : {
459 : zval *output_handler;
460 :
461 10 : ALLOC_INIT_ZVAL(output_handler);
462 10 : Z_STRLEN_P(output_handler) = len;
463 10 : Z_STRVAL_P(output_handler) = estrndup(handler_name, len);
464 10 : Z_TYPE_P(output_handler) = IS_STRING;
465 10 : return output_handler;
466 : }
467 : /* }}} */
468 :
469 : /* {{{ php_ob_init
470 : */
471 : static int php_ob_init(uint initial_size, uint block_size, zval *output_handler, uint chunk_size, zend_bool erase TSRMLS_DC)
472 10 : {
473 10 : int result = FAILURE, handler_len, len;
474 : char *handler_name, *next_handler_name;
475 : HashPosition pos;
476 : zval **tmp;
477 : zval *handler_zval;
478 :
479 20 : if (output_handler && output_handler->type == IS_STRING) {
480 10 : handler_name = Z_STRVAL_P(output_handler);
481 10 : handler_len = Z_STRLEN_P(output_handler);
482 :
483 10 : result = SUCCESS;
484 10 : if (handler_len && handler_name[0] != '\0') {
485 20 : while ((next_handler_name=strchr(handler_name, ',')) != NULL) {
486 0 : len = next_handler_name-handler_name;
487 0 : next_handler_name = estrndup(handler_name, len);
488 0 : handler_zval = php_ob_handler_from_string(next_handler_name, len TSRMLS_CC);
489 0 : result = php_ob_init_named(initial_size, block_size, next_handler_name, handler_zval, chunk_size, erase TSRMLS_CC);
490 0 : if (result != SUCCESS) {
491 0 : zval_dtor(handler_zval);
492 0 : FREE_ZVAL(handler_zval);
493 : }
494 0 : handler_name += len+1;
495 0 : handler_len -= len+1;
496 0 : efree(next_handler_name);
497 : }
498 : }
499 10 : if (result == SUCCESS) {
500 10 : handler_zval = php_ob_handler_from_string(handler_name, handler_len TSRMLS_CC);
501 10 : result = php_ob_init_named(initial_size, block_size, handler_name, handler_zval, chunk_size, erase TSRMLS_CC);
502 10 : if (result != SUCCESS) {
503 0 : zval_dtor(handler_zval);
504 0 : FREE_ZVAL(handler_zval);
505 : }
506 : }
507 0 : } else if (output_handler && output_handler->type == IS_ARRAY) {
508 : /* do we have array(object,method) */
509 0 : if (zend_is_callable(output_handler, 0, &handler_name)) {
510 0 : SEPARATE_ZVAL(&output_handler);
511 0 : output_handler->refcount++;
512 0 : result = php_ob_init_named(initial_size, block_size, handler_name, output_handler, chunk_size, erase TSRMLS_CC);
513 0 : efree(handler_name);
514 : } else {
515 0 : efree(handler_name);
516 : /* init all array elements recursively */
517 0 : zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(output_handler), &pos);
518 0 : while (zend_hash_get_current_data_ex(Z_ARRVAL_P(output_handler), (void **)&tmp, &pos) == SUCCESS) {
519 0 : result = php_ob_init(initial_size, block_size, *tmp, chunk_size, erase TSRMLS_CC);
520 0 : if (result == FAILURE) {
521 0 : break;
522 : }
523 0 : zend_hash_move_forward_ex(Z_ARRVAL_P(output_handler), &pos);
524 : }
525 : }
526 0 : } else if (output_handler && output_handler->type == IS_OBJECT) {
527 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);
528 0 : result = FAILURE;
529 : } else {
530 0 : result = php_ob_init_named(initial_size, block_size, OB_DEFAULT_HANDLER_NAME, NULL, chunk_size, erase TSRMLS_CC);
531 : }
532 10 : return result;
533 : }
534 : /* }}} */
535 :
536 : /* {{{ php_ob_list_each
537 : */
538 : static int php_ob_list_each(php_ob_buffer *ob_buffer, zval *ob_handler_array)
539 0 : {
540 0 : add_next_index_string(ob_handler_array, ob_buffer->handler_name, 1);
541 0 : return 0;
542 : }
543 : /* }}} */
544 :
545 : /* {{{ proto false|array ob_list_handlers()
546 : * List all output_buffers in an array
547 : */
548 : PHP_FUNCTION(ob_list_handlers)
549 0 : {
550 0 : if (ZEND_NUM_ARGS()!=0) {
551 0 : ZEND_WRONG_PARAM_COUNT();
552 : RETURN_FALSE;
553 : }
554 :
555 0 : array_init(return_value);
556 0 : if (OG(ob_nesting_level)) {
557 0 : if (OG(ob_nesting_level)>1) {
558 0 : zend_stack_apply_with_argument(&OG(ob_buffers), ZEND_STACK_APPLY_BOTTOMUP, (int (*)(void *element, void *)) php_ob_list_each, return_value);
559 : }
560 0 : php_ob_list_each(&OG(active_ob_buffer), return_value);
561 : }
562 : }
563 : /* }}} */
564 :
565 : /* {{{ php_ob_used_each
566 : * Sets handler_name to NULL is found
567 : */
568 : static int php_ob_handler_used_each(php_ob_buffer *ob_buffer, char **handler_name)
569 0 : {
570 0 : if (!strcmp(ob_buffer->handler_name, *handler_name)) {
571 0 : *handler_name = NULL;
572 0 : return 1;
573 : }
574 0 : return 0;
575 : }
576 : /* }}} */
577 :
578 : /* {{{ php_ob_used
579 : * returns 1 if given handler_name is used as output_handler
580 : */
581 : PHPAPI int php_ob_handler_used(char *handler_name TSRMLS_DC)
582 114 : {
583 114 : char *tmp = handler_name;
584 :
585 114 : if (OG(ob_nesting_level)) {
586 18 : if (!strcmp(OG(active_ob_buffer).handler_name, handler_name)) {
587 0 : return 1;
588 : }
589 18 : if (OG(ob_nesting_level)>1) {
590 0 : zend_stack_apply_with_argument(&OG(ob_buffers), ZEND_STACK_APPLY_BOTTOMUP, (int (*)(void *element, void *)) php_ob_handler_used_each, &tmp);
591 : }
592 : }
593 114 : return tmp ? 0 : 1;
594 : }
595 : /* }}} */
596 :
597 : /* {{{ php_ob_append
598 : */
599 : static inline void php_ob_append(const char *text, uint text_length TSRMLS_DC)
600 11 : {
601 : char *target;
602 : int original_ob_text_length;
603 :
604 11 : original_ob_text_length=OG(active_ob_buffer).text_length;
605 :
606 11 : php_ob_allocate(text_length TSRMLS_CC);
607 11 : target = OG(active_ob_buffer).buffer+original_ob_text_length;
608 11 : memcpy(target, text, text_length);
609 11 : target[text_length]=0;
610 :
611 : /* If implicit_flush is On or chunked buffering, send contents to next buffer and return. */
612 11 : if (OG(active_ob_buffer).chunk_size
613 : && OG(active_ob_buffer).text_length >= OG(active_ob_buffer).chunk_size) {
614 :
615 0 : php_end_ob_buffer(1, 1 TSRMLS_CC);
616 0 : return;
617 : }
618 : }
619 : /* }}} */
620 :
621 : #if 0
622 : static inline void php_ob_prepend(const char *text, uint text_length)
623 : {
624 : char *p, *start;
625 : TSRMLS_FETCH();
626 :
627 : php_ob_allocate(text_length TSRMLS_CC);
628 :
629 : /* php_ob_allocate() may change OG(ob_buffer), so we can't initialize p&start earlier */
630 : p = OG(ob_buffer)+OG(ob_text_length);
631 : start = OG(ob_buffer);
632 :
633 : while (--p>=start) {
634 : p[text_length] = *p;
635 : }
636 : memcpy(OG(ob_buffer), text, text_length);
637 : OG(ob_buffer)[OG(active_ob_buffer).text_length]=0;
638 : }
639 : #endif
640 :
641 :
642 : /* {{{ php_ob_get_buffer
643 : * Return the current output buffer */
644 : PHPAPI int php_ob_get_buffer(zval *p TSRMLS_DC)
645 0 : {
646 0 : if (OG(ob_nesting_level)==0) {
647 0 : return FAILURE;
648 : }
649 0 : ZVAL_STRINGL(p, OG(active_ob_buffer).buffer, OG(active_ob_buffer).text_length, 1);
650 0 : return SUCCESS;
651 : }
652 : /* }}} */
653 :
654 : /* {{{ php_ob_get_length
655 : * Return the size of the current output buffer */
656 : PHPAPI int php_ob_get_length(zval *p TSRMLS_DC)
657 0 : {
658 0 : if (OG(ob_nesting_level) == 0) {
659 0 : return FAILURE;
660 : }
661 0 : ZVAL_LONG(p, OG(active_ob_buffer).text_length);
662 0 : return SUCCESS;
663 : }
664 : /* }}} */
665 :
666 : /*
667 : * Wrapper functions - implementation
668 : */
669 :
670 :
671 : /* buffered output function */
672 : static int php_b_body_write(const char *str, uint str_length TSRMLS_DC)
673 11 : {
674 11 : php_ob_append(str, str_length TSRMLS_CC);
675 11 : return str_length;
676 : }
677 :
678 : /* {{{ php_ub_body_write_no_header
679 : */
680 : PHPAPI int php_ub_body_write_no_header(const char *str, uint str_length TSRMLS_DC)
681 3917 : {
682 : int result;
683 :
684 3917 : if (OG(disable_output)) {
685 0 : return 0;
686 : }
687 :
688 3917 : result = OG(php_header_write)(str, str_length TSRMLS_CC);
689 :
690 3917 : if (OG(implicit_flush)) {
691 224 : sapi_flush(TSRMLS_C);
692 : }
693 :
694 3917 : return result;
695 : }
696 : /* }}} */
697 :
698 : /* {{{ php_ub_body_write
699 : */
700 : PHPAPI int php_ub_body_write(const char *str, uint str_length TSRMLS_DC)
701 107 : {
702 107 : int result = 0;
703 :
704 107 : if (SG(request_info).headers_only) {
705 0 : if(SG(headers_sent)) {
706 0 : return 0;
707 : }
708 0 : php_header(TSRMLS_C);
709 0 : zend_bailout();
710 : }
711 107 : if (php_header(TSRMLS_C)) {
712 107 : if (zend_is_compiling(TSRMLS_C)) {
713 0 : OG(output_start_filename) = zend_get_compiled_filename(TSRMLS_C);
714 0 : OG(output_start_lineno) = zend_get_compiled_lineno(TSRMLS_C);
715 107 : } else if (zend_is_executing(TSRMLS_C)) {
716 96 : OG(output_start_filename) = zend_get_executed_filename(TSRMLS_C);
717 96 : OG(output_start_lineno) = zend_get_executed_lineno(TSRMLS_C);
718 : }
719 :
720 107 : OG(php_body_write) = php_ub_body_write_no_header;
721 107 : result = php_ub_body_write_no_header(str, str_length TSRMLS_CC);
722 : }
723 :
724 107 : return result;
725 : }
726 : /* }}} */
727 :
728 : /*
729 : * HEAD support
730 : */
731 :
732 : /* {{{ proto bool ob_start([ string|array user_function [, int chunk_size [, bool erase]]])
733 : Turn on Output Buffering (specifying an optional output handler). */
734 : PHP_FUNCTION(ob_start)
735 2 : {
736 2 : zval *output_handler=NULL;
737 2 : long chunk_size=0;
738 2 : zend_bool erase=1;
739 2 : int argc = ZEND_NUM_ARGS();
740 :
741 2 : if (zend_parse_parameters(argc TSRMLS_CC, "|zlb", &output_handler, &chunk_size, &erase) == FAILURE) {
742 0 : RETURN_FALSE;
743 : }
744 :
745 2 : if (chunk_size < 0)
746 0 : chunk_size = 0;
747 :
748 2 : if (php_start_ob_buffer(output_handler, chunk_size, erase TSRMLS_CC)==FAILURE) {
749 0 : RETURN_FALSE;
750 : }
751 2 : RETURN_TRUE;
752 : }
753 : /* }}} */
754 :
755 : /* {{{ proto bool ob_flush(void)
756 : Flush (send) contents of the output buffer. The last buffer content is sent to next buffer */
757 : PHP_FUNCTION(ob_flush)
758 0 : {
759 0 : if (ZEND_NUM_ARGS() != 0) {
760 0 : ZEND_WRONG_PARAM_COUNT();
761 : }
762 :
763 0 : if (!OG(ob_nesting_level)) {
764 0 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to flush buffer. No buffer to flush.");
765 0 : RETURN_FALSE;
766 : }
767 :
768 0 : php_end_ob_buffer(1, 1 TSRMLS_CC);
769 0 : RETURN_TRUE;
770 : }
771 : /* }}} */
772 :
773 :
774 : /* {{{ proto bool ob_clean(void)
775 : Clean (delete) the current output buffer */
776 : PHP_FUNCTION(ob_clean)
777 0 : {
778 0 : if (ZEND_NUM_ARGS() != 0) {
779 0 : ZEND_WRONG_PARAM_COUNT();
780 : }
781 :
782 0 : if (!OG(ob_nesting_level)) {
783 0 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer. No buffer to delete.");
784 0 : RETURN_FALSE;
785 : }
786 :
787 0 : if (!OG(active_ob_buffer).status && !OG(active_ob_buffer).erase) {
788 0 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer %s.", OG(active_ob_buffer).handler_name);
789 0 : RETURN_FALSE;
790 : }
791 :
792 0 : php_end_ob_buffer(0, 1 TSRMLS_CC);
793 0 : RETURN_TRUE;
794 : }
795 : /* }}} */
796 :
797 : /* {{{ proto bool ob_end_flush(void)
798 : Flush (send) the output buffer, and delete current output buffer */
799 : PHP_FUNCTION(ob_end_flush)
800 0 : {
801 0 : if (ZEND_NUM_ARGS() != 0) {
802 0 : ZEND_WRONG_PARAM_COUNT();
803 : }
804 :
805 0 : if (!OG(ob_nesting_level)) {
806 0 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete and flush buffer. No buffer to delete or flush.");
807 0 : RETURN_FALSE;
808 : }
809 0 : if (OG(ob_nesting_level) && !OG(active_ob_buffer).status && !OG(active_ob_buffer).erase) {
810 0 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer %s.", OG(active_ob_buffer).handler_name);
811 0 : RETURN_FALSE;
812 : }
813 :
814 0 : php_end_ob_buffer(1, 0 TSRMLS_CC);
815 0 : RETURN_TRUE;
816 : }
817 : /* }}} */
818 :
819 : /* {{{ proto bool ob_end_clean(void)
820 : Clean the output buffer, and delete current output buffer */
821 : PHP_FUNCTION(ob_end_clean)
822 1 : {
823 1 : if (ZEND_NUM_ARGS() != 0) {
824 0 : ZEND_WRONG_PARAM_COUNT();
825 : }
826 :
827 1 : if (!OG(ob_nesting_level)) {
828 1 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer. No buffer to delete.");
829 1 : RETURN_FALSE;
830 : }
831 0 : if (OG(ob_nesting_level) && !OG(active_ob_buffer).status && !OG(active_ob_buffer).erase) {
832 0 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer %s.", OG(active_ob_buffer).handler_name);
833 0 : RETURN_FALSE;
834 : }
835 :
836 0 : php_end_ob_buffer(0, 0 TSRMLS_CC);
837 0 : RETURN_TRUE;
838 : }
839 : /* }}} */
840 :
841 : /* {{{ proto bool ob_get_flush(void)
842 : Get current buffer contents, flush (send) the output buffer, and delete current output buffer */
843 : PHP_FUNCTION(ob_get_flush)
844 0 : {
845 0 : if (ZEND_NUM_ARGS() != 0) {
846 0 : ZEND_WRONG_PARAM_COUNT();
847 : }
848 :
849 : /* get contents */
850 0 : if (php_ob_get_buffer(return_value TSRMLS_CC)==FAILURE) {
851 0 : RETURN_FALSE;
852 : }
853 : /* error checks */
854 0 : if (!OG(ob_nesting_level)) {
855 0 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete and flush buffer. No buffer to delete or flush.");
856 0 : RETURN_FALSE;
857 : }
858 0 : if (OG(ob_nesting_level) && !OG(active_ob_buffer).status && !OG(active_ob_buffer).erase) {
859 0 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer %s.", OG(active_ob_buffer).handler_name);
860 0 : RETURN_FALSE;
861 : }
862 : /* flush */
863 0 : php_end_ob_buffer(1, 0 TSRMLS_CC);
864 : }
865 : /* }}} */
866 :
867 : /* {{{ proto bool ob_get_clean(void)
868 : Get current buffer contents and delete current output buffer */
869 : PHP_FUNCTION(ob_get_clean)
870 0 : {
871 0 : if (ZEND_NUM_ARGS() != 0)
872 0 : ZEND_WRONG_PARAM_COUNT();
873 :
874 : /* get contents */
875 0 : if (php_ob_get_buffer(return_value TSRMLS_CC)==FAILURE) {
876 0 : RETURN_FALSE;
877 : }
878 : /* error checks */
879 0 : if (!OG(ob_nesting_level)) {
880 0 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer. No buffer to delete.");
881 0 : RETURN_FALSE;
882 : }
883 0 : if (OG(ob_nesting_level) && !OG(active_ob_buffer).status && !OG(active_ob_buffer).erase) {
884 0 : php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer %s.", OG(active_ob_buffer).handler_name);
885 0 : RETURN_FALSE;
886 : }
887 : /* delete buffer */
888 0 : 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 0 : {
896 0 : if (ZEND_NUM_ARGS() != 0) {
897 0 : ZEND_WRONG_PARAM_COUNT();
898 : }
899 :
900 0 : if (php_ob_get_buffer(return_value TSRMLS_CC)==FAILURE) {
901 0 : 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 1 : {
910 1 : if (ZEND_NUM_ARGS() != 0) {
911 0 : ZEND_WRONG_PARAM_COUNT();
912 : }
913 :
914 1 : 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 0 : {
922 0 : if (ZEND_NUM_ARGS() != 0) {
923 0 : ZEND_WRONG_PARAM_COUNT();
924 : }
925 :
926 0 : if (php_ob_get_length(return_value TSRMLS_CC)==FAILURE) {
927 0 : 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 0 : {
935 : zval *elem;
936 :
937 0 : MAKE_STD_ZVAL(elem);
938 0 : array_init(elem);
939 :
940 0 : add_assoc_long(elem, "chunk_size", ob_buffer->chunk_size);
941 0 : if (!ob_buffer->chunk_size) {
942 0 : add_assoc_long(elem, "size", ob_buffer->size);
943 0 : add_assoc_long(elem, "block_size", ob_buffer->block_size);
944 : }
945 0 : 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 0 : add_assoc_long(elem, "type", PHP_OUTPUT_HANDLER_USER);
951 : }
952 0 : add_assoc_long(elem, "status", ob_buffer->status);
953 0 : add_assoc_string(elem, "name", ob_buffer->handler_name, 1);
954 0 : add_assoc_bool(elem, "del", ob_buffer->erase);
955 0 : add_next_index_zval(result, elem);
956 :
957 0 : 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 0 : {
966 0 : int argc = ZEND_NUM_ARGS();
967 0 : zend_bool full_status = 0;
968 :
969 0 : if (zend_parse_parameters(argc TSRMLS_CC, "|b", &full_status) == FAILURE )
970 0 : RETURN_FALSE;
971 :
972 0 : array_init(return_value);
973 :
974 0 : if (full_status) {
975 0 : if (OG(ob_nesting_level)>1) {
976 0 : zend_stack_apply_with_argument(&OG(ob_buffers), ZEND_STACK_APPLY_BOTTOMUP, (int (*)(void *elem, void *))php_ob_buffer_status, return_value);
977 : }
978 0 : if (OG(ob_nesting_level)>0 && php_ob_buffer_status(&OG(active_ob_buffer), return_value)==FAILURE) {
979 0 : RETURN_FALSE;
980 : }
981 0 : } else if (OG(ob_nesting_level)>0) {
982 0 : add_assoc_long(return_value, "level", OG(ob_nesting_level));
983 0 : if (OG(active_ob_buffer).internal_output_handler) {
984 0 : add_assoc_long(return_value, "type", PHP_OUTPUT_HANDLER_INTERNAL);
985 : } else {
986 0 : add_assoc_long(return_value, "type", PHP_OUTPUT_HANDLER_USER);
987 : }
988 0 : add_assoc_long(return_value, "status", OG(active_ob_buffer).status);
989 0 : add_assoc_string(return_value, "name", OG(active_ob_buffer).handler_name, 1);
990 0 : 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 0 : {
1000 : zval **zv_flag;
1001 : int flag;
1002 :
1003 0 : switch(ZEND_NUM_ARGS()) {
1004 : case 0:
1005 0 : flag = 1;
1006 0 : break;
1007 : case 1:
1008 0 : if (zend_get_parameters_ex(1, &zv_flag)==FAILURE) {
1009 0 : RETURN_FALSE;
1010 : }
1011 0 : convert_to_long_ex(zv_flag);
1012 0 : flag = Z_LVAL_PP(zv_flag);
1013 0 : break;
1014 : default:
1015 0 : ZEND_WRONG_PARAM_COUNT();
1016 : break;
1017 : }
1018 0 : if (flag) {
1019 0 : php_start_implicit_flush(TSRMLS_C);
1020 : } else {
1021 0 : 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 0 : {
1031 0 : 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 0 : {
1040 0 : 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 0 : {
1049 0 : if (php_url_scanner_reset_vars(TSRMLS_C) == SUCCESS) {
1050 0 : 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 0 : {
1062 : char *name, *value;
1063 : int name_len, value_len;
1064 :
1065 0 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &name, &name_len, &value, &value_len) == FAILURE) {
1066 0 : RETURN_FALSE;
1067 : }
1068 :
1069 0 : if (php_url_scanner_add_var(name, name_len, value, value_len, 1 TSRMLS_CC) == SUCCESS) {
1070 0 : 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 : */
|