1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
|
/*
+----------------------------------------------------------------------+
| PHP version 4.0 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2001 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 2.02 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| http://www.php.net/license/2_02.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Zeev Suraski <zeev@zend.com> |
| Thies C. Arntzen <thies@thieso.net> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#include "php.h"
#include "ext/standard/head.h"
#include "ext/session/php_session.h"
#include "basic_functions.h"
#include "SAPI.h"
/* output functions */
static int php_ub_body_write(const char *str, uint str_length);
static int php_ub_body_write_no_header(const char *str, uint str_length);
static int php_b_body_write(const char *str, uint str_length);
static void php_ob_init(uint initial_size, uint block_size, zval *output_handler, uint chunk_size);
static void php_ob_append(const char *text, uint text_length);
#if 0
static void php_ob_prepend(const char *text, uint text_length);
#endif
#ifdef ZTS
int output_globals_id;
#else
php_output_globals output_globals;
#endif
static int php_default_output_func(const char *str, uint str_len)
{
fwrite(str, 1, str_len, stderr);
return str_len;
}
static void php_output_init_globals(OLS_D)
{
OG(php_body_write) = php_default_output_func;
OG(php_header_write) = php_default_output_func;
OG(implicit_flush) = 0;
OG(output_start_filename) = NULL;
OG(output_start_lineno) = 0;
}
/* Start output layer */
PHPAPI void php_output_startup()
{
#ifdef ZTS
output_globals_id = ts_allocate_id(sizeof(php_output_globals), (ts_allocate_ctor) php_output_init_globals, NULL);
#else
php_output_init_globals(OLS_C);
#endif
{
OLS_FETCH();
OG(php_body_write) = php_ub_body_write;
OG(php_header_write) = sapi_module.ub_write;
OG(nesting_level) = 0;
OG(lock) = 0;
}
}
void php_output_register_constants()
{
ELS_FETCH();
REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_START", PHP_OUTPUT_HANDLER_START, CONST_CS | CONST_PERSISTENT);
REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_CONT", PHP_OUTPUT_HANDLER_CONT, CONST_CS | CONST_PERSISTENT);
REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_END", PHP_OUTPUT_HANDLER_END, CONST_CS | CONST_PERSISTENT);
}
PHPAPI int php_body_write(const char *str, uint str_length)
{
OLS_FETCH();
return OG(php_body_write)(str, str_length);
}
PHPAPI int php_header_write(const char *str, uint str_length)
{
OLS_FETCH();
return OG(php_header_write)(str, str_length);
}
/* Start output buffering */
PHPAPI int php_start_ob_buffer(zval *output_handler, uint chunk_size)
{
OLS_FETCH();
if (OG(lock)) {
return FAILURE;
}
if (chunk_size) {
php_ob_init((chunk_size*3/2), chunk_size/2, output_handler, chunk_size);
} else {
php_ob_init(40*1024, 10*1024, output_handler, chunk_size);
}
OG(php_body_write) = php_b_body_write;
return SUCCESS;
}
/* End output buffering (one level) */
PHPAPI void php_end_ob_buffer(zend_bool send_buffer, zend_bool just_flush)
{
char *final_buffer=NULL;
int final_buffer_length=0;
zval *alternate_buffer=NULL;
char *to_be_destroyed_buffer;
char *to_be_destroyed_handled_output[2] = { 0, 0 };
int status;
SLS_FETCH();
OLS_FETCH();
if (OG(nesting_level)==0) {
return;
}
status = 0;
if (!OG(active_ob_buffer).status & PHP_OUTPUT_HANDLER_START) {
/* our first call */
status |= PHP_OUTPUT_HANDLER_START;
}
if (just_flush) {
status |= PHP_OUTPUT_HANDLER_CONT;
} else {
status |= PHP_OUTPUT_HANDLER_END;
}
if (OG(active_ob_buffer).internal_output_handler) {
final_buffer = OG(active_ob_buffer).internal_output_handler_buffer;
final_buffer_length = OG(active_ob_buffer).internal_output_handler_buffer_size;
OG(active_ob_buffer).internal_output_handler(OG(active_ob_buffer).buffer, OG(active_ob_buffer).text_length, &final_buffer, &final_buffer_length, status);
} else if (OG(active_ob_buffer).output_handler) {
zval **params[2];
zval *orig_buffer;
zval *z_status;
CLS_FETCH();
ALLOC_INIT_ZVAL(orig_buffer);
orig_buffer->value.str.val = OG(active_ob_buffer).buffer;
orig_buffer->value.str.len = OG(active_ob_buffer).text_length;
orig_buffer->type = IS_STRING;
orig_buffer->refcount=2; /* don't let call_user_function() destroy our buffer */
orig_buffer->is_ref=1;
ALLOC_INIT_ZVAL(z_status);
Z_TYPE_P(z_status) = IS_LONG;
Z_LVAL_P(z_status) = status;
params[0] = &orig_buffer;
params[1] = &z_status;
OG(lock) = 1;
if (call_user_function_ex(CG(function_table), NULL, OG(active_ob_buffer).output_handler, &alternate_buffer, 2, params, 1, NULL)==SUCCESS) {
convert_to_string_ex(&alternate_buffer);
final_buffer = alternate_buffer->value.str.val;
final_buffer_length = alternate_buffer->value.str.len;
}
OG(lock) = 0;
zval_ptr_dtor(&OG(active_ob_buffer).output_handler);
if (orig_buffer->refcount==2) { /* free the zval */
FREE_ZVAL(orig_buffer);
} else {
orig_buffer->refcount-=2;
}
zval_ptr_dtor(&z_status);
}
if (!final_buffer) {
final_buffer = OG(active_ob_buffer).buffer;
final_buffer_length = OG(active_ob_buffer).text_length;
}
if (OG(nesting_level)==1) { /* end buffering */
if (SG(headers_sent) && !SG(request_info).headers_only) {
OG(php_body_write) = php_ub_body_write_no_header;
} else {
OG(php_body_write) = php_ub_body_write;
}
}
to_be_destroyed_buffer = OG(active_ob_buffer).buffer;
if (OG(active_ob_buffer).internal_output_handler
&& (final_buffer != OG(active_ob_buffer).internal_output_handler_buffer)) {
to_be_destroyed_handled_output[0] = final_buffer;
}
if (!just_flush) {
if (OG(active_ob_buffer).internal_output_handler) {
to_be_destroyed_handled_output[1] = OG(active_ob_buffer).internal_output_handler_buffer;
}
if (OG(nesting_level)>1) { /* restore previous buffer */
php_ob_buffer *ob_buffer_p;
zend_stack_top(&OG(ob_buffers), (void **) &ob_buffer_p);
OG(active_ob_buffer) = *ob_buffer_p;
zend_stack_del_top(&OG(ob_buffers));
if (OG(nesting_level)==2) { /* destroy the stack */
zend_stack_destroy(&OG(ob_buffers));
}
}
OG(nesting_level)--;
}
if (send_buffer) {
OG(php_body_write)(final_buffer, final_buffer_length);
}
if (alternate_buffer) {
zval_ptr_dtor(&alternate_buffer);
}
if (!just_flush) {
efree(to_be_destroyed_buffer);
} else {
OG(active_ob_buffer).text_length = 0;
OG(active_ob_buffer).status |= PHP_OUTPUT_HANDLER_START;
OG(php_body_write) = php_b_body_write;
}
if (to_be_destroyed_handled_output[0]) {
efree(to_be_destroyed_handled_output[0]);
}
if (to_be_destroyed_handled_output[1]) {
efree(to_be_destroyed_handled_output[1]);
}
}
/* End output buffering (all buffers) */
PHPAPI void php_end_ob_buffers(zend_bool send_buffer)
{
OLS_FETCH();
while (OG(nesting_level)!=0) {
php_end_ob_buffer(send_buffer, 0);
}
}
PHPAPI void php_start_implicit_flush()
{
OLS_FETCH();
php_end_ob_buffer(1, 0); /* Switch out of output buffering if we're in it */
OG(implicit_flush)=1;
}
PHPAPI void php_end_implicit_flush()
{
OLS_FETCH();
OG(implicit_flush)=0;
}
PHPAPI void php_ob_set_internal_handler(php_output_handler_func_t internal_output_handler, uint buffer_size)
{
OLS_FETCH();
if (OG(nesting_level)==0) {
return;
}
OG(active_ob_buffer).internal_output_handler = internal_output_handler;
OG(active_ob_buffer).internal_output_handler_buffer = (char *) emalloc(buffer_size);
OG(active_ob_buffer).internal_output_handler_buffer_size = buffer_size;
}
/*
* Output buffering - implementation
*/
static inline void php_ob_allocate(void)
{
OLS_FETCH();
if (OG(active_ob_buffer).size<OG(active_ob_buffer).text_length) {
while (OG(active_ob_buffer).size <= OG(active_ob_buffer).text_length) {
OG(active_ob_buffer).size+=OG(active_ob_buffer).block_size;
}
OG(active_ob_buffer).buffer = (char *) erealloc(OG(active_ob_buffer).buffer, OG(active_ob_buffer).size+1);
}
}
static void php_ob_init(uint initial_size, uint block_size, zval *output_handler, uint chunk_size)
{
OLS_FETCH();
if (OG(nesting_level)>0) {
if (OG(nesting_level)==1) { /* initialize stack */
zend_stack_init(&OG(ob_buffers));
}
zend_stack_push(&OG(ob_buffers), &OG(active_ob_buffer), sizeof(php_ob_buffer));
}
OG(nesting_level)++;
OG(active_ob_buffer).block_size = block_size;
OG(active_ob_buffer).size = initial_size;
OG(active_ob_buffer).buffer = (char *) emalloc(initial_size+1);
OG(active_ob_buffer).text_length = 0;
OG(active_ob_buffer).output_handler = output_handler;
OG(active_ob_buffer).chunk_size = chunk_size;
OG(active_ob_buffer).status = 0;
OG(active_ob_buffer).internal_output_handler = NULL;
}
static void php_ob_append(const char *text, uint text_length)
{
char *target;
int original_ob_text_length;
OLS_FETCH();
original_ob_text_length=OG(active_ob_buffer).text_length;
OG(active_ob_buffer).text_length = OG(active_ob_buffer).text_length + text_length;
php_ob_allocate();
target = OG(active_ob_buffer).buffer+original_ob_text_length;
memcpy(target, text, text_length);
target[text_length]=0;
if (OG(active_ob_buffer).chunk_size
&& OG(active_ob_buffer).text_length >= OG(active_ob_buffer).chunk_size) {
zval *output_handler = OG(active_ob_buffer).output_handler;
if (output_handler) {
output_handler->refcount++;
}
php_end_ob_buffer(1, 1);
return;
}
}
#if 0
static void php_ob_prepend(const char *text, uint text_length)
{
char *p, *start;
OLS_FETCH();
OG(active_ob_buffer).text_length += text_length;
php_ob_allocate();
/* php_ob_allocate() may change OG(ob_buffer), so we can't initialize p&start earlier */
p = OG(ob_buffer)+OG(ob_text_length);
start = OG(ob_buffer);
while (--p>=start) {
p[text_length] = *p;
}
memcpy(OG(ob_buffer), text, text_length);
OG(ob_buffer)[OG(active_ob_buffer).text_length]=0;
}
#endif
/* Return the current output buffer */
int php_ob_get_buffer(pval *p)
{
OLS_FETCH();
if (OG(nesting_level)==0) {
return FAILURE;
}
p->type = IS_STRING;
p->value.str.val = estrndup(OG(active_ob_buffer).buffer, OG(active_ob_buffer).text_length);
p->value.str.len = OG(active_ob_buffer).text_length;
return SUCCESS;
}
/* Return the size of the current output buffer */
int php_ob_get_length(pval *p)
{
OLS_FETCH();
if (OG(nesting_level) == 0) {
return FAILURE;
}
p->type = IS_LONG;
p->value.lval = OG(active_ob_buffer).text_length;
return SUCCESS;
}
/*
* Wrapper functions - implementation
*/
/* buffered output function */
static int php_b_body_write(const char *str, uint str_length)
{
php_ob_append(str, str_length);
return str_length;
}
static int php_ub_body_write_no_header(const char *str, uint str_length)
{
char *newstr = NULL;
size_t new_length=0;
int result;
OLS_FETCH();
BLS_FETCH();
if (BG(use_trans_sid)) {
session_adapt_uris(str, str_length, &newstr, &new_length);
}
if (newstr) {
str = newstr;
str_length = new_length;
}
result = OG(php_header_write)(str, str_length);
if (OG(implicit_flush)) {
sapi_flush();
}
return result;
}
static int php_ub_body_write(const char *str, uint str_length)
{
int result = 0;
SLS_FETCH();
OLS_FETCH();
if (SG(request_info).headers_only) {
php_header();
zend_bailout();
}
if (php_header()) {
if (zend_is_compiling()) {
CLS_FETCH();
OG(output_start_filename) = zend_get_compiled_filename(CLS_C);
OG(output_start_lineno) = zend_get_compiled_lineno(CLS_C);
} else if (zend_is_executing()) {
ELS_FETCH();
OG(output_start_filename) = zend_get_executed_filename(ELS_C);
OG(output_start_lineno) = zend_get_executed_lineno(ELS_C);
}
OG(php_body_write) = php_ub_body_write_no_header;
result = php_ub_body_write_no_header(str, str_length);
}
return result;
}
/*
* HEAD support
*/
/* {{{ proto void ob_start([ string user_function [, int chunk_size]])
Turn on Output Buffering (specifying an optional output handler). */
PHP_FUNCTION(ob_start)
{
zval *output_handler;
uint chunk_size=0;
switch (ZEND_NUM_ARGS()) {
case 0:
output_handler = NULL;
break;
case 1: {
zval **output_handler_p;
if (zend_get_parameters_ex(1, &output_handler_p)==FAILURE) {
RETURN_FALSE;
}
SEPARATE_ZVAL(output_handler_p);
output_handler = *output_handler_p;
output_handler->refcount++;
}
break;
case 2: {
zval **output_handler_p, **chunk_size_p;
if (zend_get_parameters_ex(2, &output_handler_p, &chunk_size_p)==FAILURE) {
RETURN_FALSE;
}
SEPARATE_ZVAL(output_handler_p);
output_handler = *output_handler_p;
output_handler->refcount++;
convert_to_long_ex(chunk_size_p);
chunk_size = (uint) Z_LVAL_PP(chunk_size_p);
}
break;
default:
ZEND_WRONG_PARAM_COUNT();
break;
}
if (php_start_ob_buffer(output_handler, chunk_size)==FAILURE) {
php_error(E_WARNING, "Cannot use output buffering in output buffering display handlers");
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto void ob_end_flush(void)
Flush (send) the output buffer, and turn off output buffering */
PHP_FUNCTION(ob_end_flush)
{
php_end_ob_buffer(1, 0);
}
/* }}} */
/* {{{ proto void ob_end_clean(void)
Clean (erase) the output buffer, and turn off output buffering */
PHP_FUNCTION(ob_end_clean)
{
php_end_ob_buffer(0, 0);
}
/* }}} */
/* {{{ proto string ob_get_contents(void)
Return the contents of the output buffer */
PHP_FUNCTION(ob_get_contents)
{
if (php_ob_get_buffer(return_value)==FAILURE) {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto string ob_get_length(void)
Return the length of the output buffer */
PHP_FUNCTION(ob_get_length)
{
if (php_ob_get_length(return_value)==FAILURE) {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto void ob_implicit_flush([int flag])
Turn implicit flush on/off and is equivalent to calling flush() after every output call */
PHP_FUNCTION(ob_implicit_flush)
{
zval **zv_flag;
int flag;
switch(ZEND_NUM_ARGS()) {
case 0:
flag = 1;
break;
case 1:
if (zend_get_parameters_ex(1, &zv_flag)==FAILURE) {
RETURN_FALSE;
}
convert_to_long_ex(zv_flag);
flag = (*zv_flag)->value.lval;
break;
default:
WRONG_PARAM_COUNT;
break;
}
if (flag) {
php_start_implicit_flush();
} else {
php_end_implicit_flush();
}
}
/* }}} */
PHPAPI char *php_get_output_start_filename()
{
OLS_FETCH();
return OG(output_start_filename);
}
PHPAPI int php_get_output_start_lineno()
{
OLS_FETCH();
return OG(output_start_lineno);
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
*/
|