1999-10-28 02:30:41 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| PHP version 4.0 |
|
|
|
|
+----------------------------------------------------------------------+
|
2000-01-01 09:32:05 +08:00
|
|
|
| Copyright (c) 1997, 1998, 1999, 2000 The PHP Group |
|
1999-10-28 02:30:41 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| This source file is subject to version 2.0 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_0.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: Andi Gutmans <andi@zend.com> |
|
|
|
|
| Zeev Suraski <zeev@zend.com> |
|
|
|
|
| Thies C. Arntzen <thies@digicol.de> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "php.h"
|
|
|
|
#include "ext/standard/head.h"
|
|
|
|
#include "ext/session/php_session.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);
|
1999-12-05 22:16:37 +08:00
|
|
|
static void php_ob_destroy(void);
|
1999-10-28 02:30:41 +08:00
|
|
|
static void php_ob_append(const char *text, uint text_length);
|
1999-12-05 22:16:37 +08:00
|
|
|
#if 0
|
1999-10-28 02:30:41 +08:00
|
|
|
static void php_ob_prepend(const char *text, uint text_length);
|
1999-12-05 22:16:37 +08:00
|
|
|
#endif
|
|
|
|
static inline void php_ob_send(void);
|
1999-10-28 02:30:41 +08:00
|
|
|
|
1999-12-05 22:16:37 +08:00
|
|
|
void php_start_ob_buffering(void);
|
1999-10-28 02:30:41 +08:00
|
|
|
void php_end_ob_buffering(int send_buffer);
|
|
|
|
int php_ob_get_buffer(pval *p);
|
|
|
|
|
|
|
|
/* HEAD support */
|
|
|
|
void set_header_request(int value);
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int (*php_body_write)(const char *str, uint str_length); /* string output */
|
|
|
|
int (*php_header_write)(const char *str, uint str_length); /* unbuffer string output */
|
|
|
|
char *ob_buffer;
|
|
|
|
uint ob_size;
|
|
|
|
uint ob_block_size;
|
|
|
|
uint ob_text_length;
|
|
|
|
} php_output_globals;
|
|
|
|
|
|
|
|
#ifdef ZTS
|
1999-11-27 02:34:27 +08:00
|
|
|
#define OLS_D php_output_globals *output_globals
|
1999-11-29 04:38:44 +08:00
|
|
|
#define OLS_C output_globals
|
1999-11-24 06:10:44 +08:00
|
|
|
#define OG(v) (output_globals->v)
|
|
|
|
#define OLS_FETCH() php_output_globals *output_globals = ts_resource(output_globals_id)
|
1999-10-28 02:30:41 +08:00
|
|
|
int output_globals_id;
|
|
|
|
#else
|
1999-12-05 22:16:37 +08:00
|
|
|
#define OLS_D void
|
1999-11-29 04:38:44 +08:00
|
|
|
#define OLS_C
|
1999-11-24 06:10:44 +08:00
|
|
|
#define OG(v) (output_globals.v)
|
|
|
|
#define OLS_FETCH()
|
1999-10-28 02:30:41 +08:00
|
|
|
php_output_globals output_globals;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
PHP_FUNCTION(ob_start);
|
|
|
|
PHP_FUNCTION(ob_end_flush);
|
|
|
|
PHP_FUNCTION(ob_end_clean);
|
|
|
|
PHP_FUNCTION(ob_get_contents);
|
|
|
|
|
|
|
|
|
1999-11-27 02:34:27 +08:00
|
|
|
static void php_output_init_globals(OLS_D)
|
1999-10-28 02:30:41 +08:00
|
|
|
{
|
1999-11-24 06:10:44 +08:00
|
|
|
OG(php_body_write) = NULL;
|
|
|
|
OG(php_header_write) = NULL;
|
|
|
|
OG(ob_buffer) = NULL;
|
|
|
|
OG(ob_size) = 0;
|
|
|
|
OG(ob_block_size) = 0;
|
|
|
|
OG(ob_text_length) = 0;
|
1999-10-28 02:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-23 11:10:46 +08:00
|
|
|
PHP_GINIT_FUNCTION(output)
|
1999-10-28 02:30:41 +08:00
|
|
|
{
|
|
|
|
#ifdef ZTS
|
|
|
|
output_globals_id = ts_allocate_id(sizeof(php_output_globals), NULL, NULL);
|
1999-11-27 02:34:27 +08:00
|
|
|
#else
|
1999-11-29 04:38:44 +08:00
|
|
|
php_output_init_globals(OLS_C);
|
1999-10-28 02:30:41 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_function_entry php_output_functions[] = {
|
|
|
|
PHP_FE(ob_start, NULL)
|
|
|
|
PHP_FE(ob_end_flush, NULL)
|
|
|
|
PHP_FE(ob_end_clean, NULL)
|
|
|
|
PHP_FE(ob_get_contents, NULL)
|
|
|
|
{NULL, NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
PHP_RINIT_FUNCTION(output);
|
|
|
|
PHP_RSHUTDOWN_FUNCTION(output);
|
|
|
|
|
1999-12-18 04:55:31 +08:00
|
|
|
zend_module_entry output_module_entry = {
|
1999-10-28 02:30:41 +08:00
|
|
|
"PHP_output",
|
|
|
|
php_output_functions,
|
1999-11-23 11:10:46 +08:00
|
|
|
NULL, /* extension-wide startup function */
|
|
|
|
NULL, /* extension-wide shutdown function */
|
|
|
|
PHP_RINIT(output), /* per-request startup function */
|
|
|
|
PHP_RSHUTDOWN(output), /* per-request shutdown function */
|
|
|
|
NULL, /* information function */
|
|
|
|
PHP_GINIT(output), /* global startup function */
|
|
|
|
NULL, /* global shutdown function */
|
|
|
|
STANDARD_MODULE_PROPERTIES_EX
|
1999-10-28 02:30:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
PHP_RINIT_FUNCTION(output)
|
|
|
|
{
|
|
|
|
php_output_startup();
|
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
PHP_RSHUTDOWN_FUNCTION(output)
|
|
|
|
{
|
|
|
|
/* XXX needs filling in */
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Start output layer */
|
|
|
|
PHPAPI void php_output_startup()
|
|
|
|
{
|
1999-11-24 06:10:44 +08:00
|
|
|
OLS_FETCH();
|
1999-10-28 02:30:41 +08:00
|
|
|
|
1999-11-24 06:10:44 +08:00
|
|
|
OG(ob_buffer) = NULL;
|
|
|
|
OG(php_body_write) = php_ub_body_write;
|
|
|
|
OG(php_header_write) = sapi_module.ub_write;
|
1999-10-28 02:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PHPAPI int php_body_write(const char *str, uint str_length)
|
|
|
|
{
|
1999-11-24 06:10:44 +08:00
|
|
|
OLS_FETCH();
|
|
|
|
return OG(php_body_write)(str, str_length);
|
1999-10-28 02:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PHPAPI int php_header_write(const char *str, uint str_length)
|
|
|
|
{
|
1999-11-24 06:10:44 +08:00
|
|
|
OLS_FETCH();
|
|
|
|
return OG(php_header_write)(str, str_length);
|
1999-10-28 02:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Start output buffering */
|
|
|
|
PHPAPI void php_start_ob_buffering()
|
|
|
|
{
|
1999-11-24 06:10:44 +08:00
|
|
|
OLS_FETCH();
|
1999-10-28 02:30:41 +08:00
|
|
|
|
|
|
|
php_ob_init(4096, 1024);
|
1999-11-24 06:10:44 +08:00
|
|
|
OG(php_body_write) = php_b_body_write;
|
1999-10-28 02:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* End output buffering */
|
|
|
|
PHPAPI void php_end_ob_buffering(int send_buffer)
|
|
|
|
{
|
|
|
|
SLS_FETCH();
|
1999-11-24 06:10:44 +08:00
|
|
|
OLS_FETCH();
|
1999-10-28 02:30:41 +08:00
|
|
|
|
1999-11-24 06:10:44 +08:00
|
|
|
if (!OG(ob_buffer)) {
|
1999-10-28 02:30:41 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (SG(headers_sent) && !SG(request_info).headers_only) {
|
1999-11-24 06:10:44 +08:00
|
|
|
OG(php_body_write) = php_ub_body_write_no_header;
|
1999-10-28 02:30:41 +08:00
|
|
|
} else {
|
1999-11-24 06:10:44 +08:00
|
|
|
OG(php_body_write) = php_ub_body_write;
|
1999-10-28 02:30:41 +08:00
|
|
|
}
|
|
|
|
if (send_buffer) {
|
|
|
|
php_ob_send();
|
|
|
|
}
|
|
|
|
php_ob_destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Output buffering - implementation
|
|
|
|
*/
|
|
|
|
|
1999-12-05 22:16:37 +08:00
|
|
|
static inline void php_ob_allocate(void)
|
1999-10-28 02:30:41 +08:00
|
|
|
{
|
1999-11-24 06:10:44 +08:00
|
|
|
OLS_FETCH();
|
1999-10-28 02:30:41 +08:00
|
|
|
|
1999-11-24 06:10:44 +08:00
|
|
|
if (OG(ob_size)<OG(ob_text_length)) {
|
|
|
|
while (OG(ob_size) <= OG(ob_text_length))
|
|
|
|
OG(ob_size)+=OG(ob_block_size);
|
1999-10-28 02:30:41 +08:00
|
|
|
|
1999-11-24 06:10:44 +08:00
|
|
|
OG(ob_buffer) = (char *) erealloc(OG(ob_buffer), OG(ob_size)+1);
|
1999-10-28 02:30:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void php_ob_init(uint initial_size, uint block_size)
|
|
|
|
{
|
1999-11-24 06:10:44 +08:00
|
|
|
OLS_FETCH();
|
1999-10-28 02:30:41 +08:00
|
|
|
|
1999-11-24 06:10:44 +08:00
|
|
|
if (OG(ob_buffer)) {
|
1999-10-28 02:30:41 +08:00
|
|
|
return;
|
|
|
|
}
|
1999-11-24 06:10:44 +08:00
|
|
|
OG(ob_block_size) = block_size;
|
|
|
|
OG(ob_size) = initial_size;
|
|
|
|
OG(ob_buffer) = (char *) emalloc(initial_size+1);
|
|
|
|
OG(ob_text_length) = 0;
|
1999-10-28 02:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void php_ob_destroy()
|
|
|
|
{
|
1999-11-24 06:10:44 +08:00
|
|
|
OLS_FETCH();
|
1999-10-28 02:30:41 +08:00
|
|
|
|
1999-11-24 06:10:44 +08:00
|
|
|
if (OG(ob_buffer)) {
|
|
|
|
efree(OG(ob_buffer));
|
|
|
|
OG(ob_buffer) = NULL;
|
1999-10-28 02:30:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void php_ob_append(const char *text, uint text_length)
|
|
|
|
{
|
|
|
|
char *target;
|
|
|
|
int original_ob_text_length;
|
1999-11-24 06:10:44 +08:00
|
|
|
OLS_FETCH();
|
1999-10-28 02:30:41 +08:00
|
|
|
|
1999-11-24 06:10:44 +08:00
|
|
|
original_ob_text_length=OG(ob_text_length);
|
1999-10-28 02:30:41 +08:00
|
|
|
|
1999-11-24 06:10:44 +08:00
|
|
|
OG(ob_text_length) += text_length;
|
1999-10-28 02:30:41 +08:00
|
|
|
php_ob_allocate();
|
1999-11-24 06:10:44 +08:00
|
|
|
target = OG(ob_buffer)+original_ob_text_length;
|
1999-10-28 02:30:41 +08:00
|
|
|
memcpy(target, text, text_length);
|
|
|
|
target[text_length]=0;
|
|
|
|
}
|
|
|
|
|
1999-12-05 22:16:37 +08:00
|
|
|
#if 0
|
1999-10-28 02:30:41 +08:00
|
|
|
static void php_ob_prepend(const char *text, uint text_length)
|
|
|
|
{
|
|
|
|
char *p, *start;
|
1999-11-24 06:10:44 +08:00
|
|
|
OLS_FETCH();
|
1999-10-28 02:30:41 +08:00
|
|
|
|
1999-11-24 06:10:44 +08:00
|
|
|
OG(ob_text_length) += text_length;
|
1999-10-28 02:30:41 +08:00
|
|
|
php_ob_allocate();
|
|
|
|
|
1999-11-24 06:10:44 +08:00
|
|
|
/* 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);
|
1999-10-28 02:30:41 +08:00
|
|
|
|
|
|
|
while (--p>=start) {
|
|
|
|
p[text_length] = *p;
|
|
|
|
}
|
1999-11-24 06:10:44 +08:00
|
|
|
memcpy(OG(ob_buffer), text, text_length);
|
|
|
|
OG(ob_buffer)[OG(ob_text_length)]=0;
|
1999-10-28 02:30:41 +08:00
|
|
|
}
|
1999-12-05 22:16:37 +08:00
|
|
|
#endif
|
1999-10-28 02:30:41 +08:00
|
|
|
|
|
|
|
static inline void php_ob_send()
|
|
|
|
{
|
1999-11-24 06:10:44 +08:00
|
|
|
OLS_FETCH();
|
1999-10-28 02:30:41 +08:00
|
|
|
|
|
|
|
/* header_write is a simple, unbuffered output function */
|
1999-11-24 06:10:44 +08:00
|
|
|
OG(php_body_write)(OG(ob_buffer), OG(ob_text_length));
|
1999-10-28 02:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Return the current output buffer */
|
|
|
|
int php_ob_get_buffer(pval *p)
|
|
|
|
{
|
1999-11-24 06:10:44 +08:00
|
|
|
OLS_FETCH();
|
1999-10-28 02:30:41 +08:00
|
|
|
|
1999-11-24 06:10:44 +08:00
|
|
|
if (!OG(ob_buffer)) {
|
1999-10-28 02:30:41 +08:00
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
p->type = IS_STRING;
|
1999-11-24 06:10:44 +08:00
|
|
|
p->value.str.val = estrndup(OG(ob_buffer), OG(ob_text_length));
|
|
|
|
p->value.str.len = OG(ob_text_length);
|
1999-10-28 02:30:41 +08:00
|
|
|
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;
|
|
|
|
uint new_length=0;
|
|
|
|
int result;
|
1999-11-24 06:10:44 +08:00
|
|
|
OLS_FETCH();
|
1999-10-28 02:30:41 +08:00
|
|
|
|
|
|
|
session_adapt_uris(str, str_length, &newstr, &new_length);
|
|
|
|
|
|
|
|
if (newstr) {
|
|
|
|
str = newstr;
|
|
|
|
str_length = new_length;
|
|
|
|
}
|
|
|
|
|
1999-11-24 06:10:44 +08:00
|
|
|
result = OG(php_header_write)(str, str_length);
|
1999-10-28 02:30:41 +08:00
|
|
|
|
|
|
|
if (newstr) {
|
|
|
|
free(newstr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int php_ub_body_write(const char *str, uint str_length)
|
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
SLS_FETCH();
|
1999-11-24 06:10:44 +08:00
|
|
|
OLS_FETCH();
|
1999-10-28 02:30:41 +08:00
|
|
|
|
|
|
|
if (SG(request_info).headers_only) {
|
|
|
|
zend_bailout();
|
|
|
|
}
|
1999-12-18 03:51:39 +08:00
|
|
|
if (php_header()) {
|
1999-11-24 06:10:44 +08:00
|
|
|
OG(php_body_write) = php_ub_body_write_no_header;
|
1999-10-28 02:30:41 +08:00
|
|
|
result = php_ub_body_write_no_header(str, str_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* HEAD support
|
|
|
|
*/
|
|
|
|
|
|
|
|
void set_header_request(int value)
|
|
|
|
{
|
|
|
|
/* deprecated */
|
|
|
|
}
|
|
|
|
|
|
|
|
PHP_FUNCTION(ob_start)
|
|
|
|
{
|
|
|
|
php_start_ob_buffering();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PHP_FUNCTION(ob_end_flush)
|
|
|
|
{
|
|
|
|
php_end_ob_buffering(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PHP_FUNCTION(ob_end_clean)
|
|
|
|
{
|
|
|
|
php_end_ob_buffering(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PHP_FUNCTION(ob_get_contents)
|
|
|
|
{
|
|
|
|
if (php_ob_get_buffer(return_value)==FAILURE) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
|
|
|
*/
|