? ext/pdo_sqlite/sqlite/src/config.h ? ext/pdo_sqlite/sqlite/src/sqlite3.h Index: README.NEW-OUTPUT-API =================================================================== RCS file: README.NEW-OUTPUT-API diff -N README.NEW-OUTPUT-API --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ README.NEW-OUTPUT-API 18 Sep 2008 18:38:16 -0000 @@ -0,0 +1,142 @@ +$Id: README.NEW-OUTPUT-API,v 1.3 2006/08/30 07:39:09 mike Exp $ + + +API adjustment to the old output control code: + + Everything now resides beneath the php_output namespace, + and there's an API call for every output handler op. + + Checking output control layers status: + // Using OG() + php_output_get_status(TSRMLS_C); + + Starting the default output handler: + // php_start_ob_buffer(NULL, 0, 1 TSRMLS_CC); + php_output_start_default(TSRMLS_C); + + Starting an user handler by zval: + // php_start_ob_buffer(zhandler, chunk_size, erase TSRMLS_CC); + php_output_start_user(zhandler, chunk_size, flags TSRMLS_CC); + + Starting an internal handler whithout context: + // php_ob_set_internal_handler(my_php_output_handler_func_t, buffer_size, "output handler name", erase TSRMLS_CC); + php_output_start_internal(handler_name_zval, my_php_output_handler_func_t, chunk_size, flags TSRMLS_CC); + + Starting an internal handler with context: + // not possible with old API + php_output_handler *h; + h = php_output_handler_create_internal(handler_name_zval, my_php_output_handler_context_func_t, chunk_size, flags TSRMLS_CC); + php_output_handler_set_context(h, my_context, my_context_dtor); + php_output_handler_start(h TSRMLS_CC); + + Testing whether a certain output handler has already been started: + // php_ob_handler_used("output handler name" TSRMLS_CC); + php_output_handler_started(handler_name_zval TSRMLS_CC); + + Flushing one output buffer: + // php_ob_end_buffer(1, 1 TSRMLS_CC); + php_output_flush(TSRMLS_C); + + Flushing all output buffers: + // not possible with old API + php_output_flush_all(TSRMLS_C); + + Cleaning one output buffer: + // php_ob_end_buffer(0, 1 TSRMLS_CC); + php_output_clean(TSRMLS_C); + + Cleaning all output buffers: + // not possible with old API + php_output_clean_all(TSRMLS_C); + + Discarding one output buffer: + // php_ob_end_buffer(0, 0 TSRMLS_CC); + php_output_discard(TSRMLS_C); + + Discarding all output buffers: + // php_ob_end_buffers(0 TSRMLS_CC); + php_output_discard_all(TSRMLS_C); + + Stopping (and dropping) one output buffer: + // php_ob_end_buffer(1, 0 TSRMLS_CC) + php_output_end(TSRMLS_C); + + Stopping (and dropping) all output buffers: + // php_ob_end_buffers(1, 0 TSRMLS_CC); + php_output_end_all(TSRMLS_C); + + Retrieving output buffers contents: + // php_ob_get_buffer(zstring TSRMLS_CC); + php_output_get_contents(zstring TSRMLS_CC); + + Retrieving output buffers length: + // php_ob_get_length(zlength TSRMLS_CC); + php_output_get_length(zlength TSRMLS_CC); + + Retrieving output buffering level: + // OG(nesting_level); + php_output_get_level(TSRMLS_C); + + Issue a warning because of an output handler conflict: + // php_ob_init_conflict("to be started handler name", "to be tested if already started handler name" TSRMLS_CC); + php_output_handler_conflict(new_handler_name_zval, set_handler_name_zval TSRMLS_CC); + + Registering a conflict checking function, which will be checked prior starting the handler: + // not possible with old API, unless hardcoding into output.c + php_output_handler_conflict_register(handler_name_zval, my_php_output_handler_conflict_check_t TSRMLS_CC); + + Registering a reverse conflict checking function, which will be checked prior starting the specified foreign handler: + // not possible with old API + php_output_handler_reverse_conflict_register(foreign_handler_name_zval, my_php_output_handler_conflict_check_t TSRMLS_CC); + + Facilitating a context from within an output handler callable with ob_start(): + // not possible with old API + php_output_handler_hook(PHP_OUTPUT_HANDLER_HOOK_GET_OPAQ, (void *) &custom_ctx_ptr_ptr TSRMLS_CC); + + Disabling of the output handler by itself: + //not possible with old API + php_output_handler_hook(PHP_OUTPUT_HANDLER_HOOK_DISABLE, NULL TSRMLS_CC); + + Marking an output handler immutable by itself because of irreversibility of its operation: + // not possible with old API + php_output_handler_hook(PHP_OUTPUT_HANDLER_HOOK_IMMUTABLE, NULL TSRMLS_CC); + + Restarting the output handler because of a CLEAN operation: + // not possible with old API + if (flags & PHP_OUTPUT_HANDLER_CLEAN) { ... } + + Recognizing by the output handler itself if it gets discarded: + // not possible with old API + if ((flags & PHP_OUTPUT_HANDLER_CLEAN) && (flags & PHP_OUTPUT_HANDLER_FINAL)) { ... } + + +Output handler hooks + + The output handler can change its abilities at runtime. Eg. the gz handler can + remove the CLEANABLE and REMOVABLE bits when the first output has passed through it; + or handlers implemented in C to be used with ob_start() can contain a non-global + context: + PHP_OUTPUT_HANDLER_HOOK_GET_OPAQ + pass a void*** pointer as second arg to receive the address of a pointer + pointer to the opaque field of the output handler context + PHP_OUTPUT_HANDLER_HOOK_GET_FLAGS + pass a int* pointer as second arg to receive the flags set for the output handler + PHP_OUTPUT_HANDLER_HOOK_GET_LEVEL + pass a int* pointer as second arg to receive the level of this output handler + (starts with 0) + PHP_OUTPUT_HANDLER_HOOK_IMMUTABLE + the second arg is ignored; marks the output handler to be neither cleanable + nor removable + PHP_OUTPUT_HANDLER_HOOK_DISABLE + the second arg is ignored; marks the output handler as disabled + + +Open questions + + Should the userland API be adjusted and unified? + + Many bits of the manual (and very first implementation) do not comply + with the behaviour of the current (to be obsoleted) code, thus should + the manual or the behaviour be adjusted? + +END Index: ext/iconv/iconv.c =================================================================== RCS file: /repository/php-src/ext/iconv/iconv.c,v retrieving revision 1.124.2.8.2.20.2.7 diff -u -p -d -r1.124.2.8.2.20.2.7 iconv.c --- ext/iconv/iconv.c 21 May 2008 15:03:20 -0000 1.124.2.8.2.20.2.7 +++ ext/iconv/iconv.c 18 Sep 2008 18:38:17 -0000 @@ -121,12 +121,6 @@ ZEND_BEGIN_ARG_INFO(arginfo_iconv, 0) ZEND_END_ARG_INFO() static -ZEND_BEGIN_ARG_INFO(arginfo_ob_iconv_handler, 0) - ZEND_ARG_INFO(0, contents) - ZEND_ARG_INFO(0, status) -ZEND_END_ARG_INFO() - -static ZEND_BEGIN_ARG_INFO(arginfo_iconv_set_encoding, 0) ZEND_ARG_INFO(0, type) ZEND_ARG_INFO(0, charset) @@ -143,7 +137,6 @@ ZEND_END_ARG_INFO() */ const zend_function_entry iconv_functions[] = { PHP_RAW_NAMED_FE(iconv,php_if_iconv, arginfo_iconv) - PHP_FE(ob_iconv_handler, arginfo_ob_iconv_handler) PHP_FE(iconv_get_encoding, arginfo_iconv_get_encoding) PHP_FE(iconv_set_encoding, arginfo_iconv_set_encoding) PHP_FE(iconv_strlen, arginfo_iconv_strlen) @@ -225,6 +218,10 @@ static php_iconv_err_t _php_iconv_mime_d static php_iconv_err_t php_iconv_stream_filter_register_factory(TSRMLS_D); static php_iconv_err_t php_iconv_stream_filter_unregister_factory(TSRMLS_D); + +static int php_iconv_output_conflict(zval *handler_name TSRMLS_DC); +static php_output_handler *php_iconv_output_handler_init(zval *name, size_t chunk_size, int flags TSRMLS_DC); +static int php_iconv_output_handler(void **nothing, php_output_context *output_context); /* }}} */ /* {{{ static globals */ @@ -289,6 +286,9 @@ PHP_MINIT_FUNCTION(miconv) return FAILURE; } + PHP_OUTPUT_ALIAS_REGISTER("ob_iconv_handler", php_iconv_output_handler_init); + PHP_OUTPUT_CONFLICT_REGISTER("ob_iconv_handler", php_iconv_output_conflict); + return SUCCESS; } /* }}} */ @@ -323,6 +323,60 @@ PHP_MINFO_FUNCTION(miconv) } /* }}} */ +static int php_iconv_output_conflict(zval *handler_name TSRMLS_DC) +{ + if (php_output_get_level(TSRMLS_C)) { + PHP_OUTPUT_CONFLICT("ob_iconv_handler", return FAILURE); + PHP_OUTPUT_CONFLICT("mb_output_handler", return FAILURE); + } + return SUCCESS; +} + +static php_output_handler *php_iconv_output_handler_init(zval *handler_name, size_t chunk_size, int flags TSRMLS_DC) +{ + return php_output_handler_create_internal(handler_name, php_iconv_output_handler, chunk_size, flags TSRMLS_CC); +} + +static int php_iconv_output_handler(void **nothing, php_output_context *output_context) +{ + char *s, *content_type, *mimetype = NULL; + int output_status, mimetype_len = 0; + PHP_OUTPUT_TSRMLS(output_context); + + if (output_context->op & PHP_OUTPUT_HANDLER_START) { + output_status = php_output_get_status(TSRMLS_C); + if (output_status & PHP_OUTPUT_SENT) { + return FAILURE; + } + + if (SG(sapi_headers).mimetype && !strncasecmp(SG(sapi_headers).mimetype, "text/", 5)) { + if ((s = strchr(SG(sapi_headers).mimetype,';')) == NULL){ + mimetype = SG(sapi_headers).mimetype; + } else { + mimetype = SG(sapi_headers).mimetype; + mimetype_len = s - SG(sapi_headers).mimetype; + } + } else if (SG(sapi_headers).send_default_content_type) { + mimetype = SG(default_mimetype) ? SG(default_mimetype) : SAPI_DEFAULT_MIMETYPE; + } + + if (mimetype != NULL && !(output_context->op & PHP_OUTPUT_HANDLER_CLEAN)) { + int len = spprintf(&content_type, 0, "Content-Type: %.*s; charset=%s", mimetype_len?mimetype_len:strlen(mimetype), mimetype, ICONVG(output_encoding)); + if (content_type && SUCCESS == sapi_add_header(content_type, len, 0)) { + SG(sapi_headers).send_default_content_type = 0; + php_output_handler_hook(PHP_OUTPUT_HANDLER_HOOK_IMMUTABLE, NULL TSRMLS_CC); + } + } + } + + if (output_context->in.used) { + output_context->out.free = 1; + _php_iconv_show_error(php_iconv_string(output_context->in.data, output_context->in.used, &output_context->out.data, &output_context->out.used, ICONVG(output_encoding), ICONVG(internal_encoding)), ICONVG(output_encoding), ICONVG(internal_encoding) TSRMLS_CC); + } + + return SUCCESS; +} + /* {{{ _php_iconv_appendl() */ static php_iconv_err_t _php_iconv_appendl(smart_str *d, const char *s, size_t l, iconv_t cd) { @@ -2333,58 +2387,6 @@ PHP_NAMED_FUNCTION(php_if_iconv) } /* }}} */ -/* {{{ proto string ob_iconv_handler(string contents, int status) - Returns str in output buffer converted to the iconv.output_encoding character set */ -PHP_FUNCTION(ob_iconv_handler) -{ - char *out_buffer, *content_type, *mimetype = NULL, *s; - zval *zv_string; - size_t out_len; - int mimetype_alloced = 0; - long status; - - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zl", &zv_string, &status) == FAILURE) - return; - - convert_to_string(zv_string); - - if (SG(sapi_headers).mimetype && - strncasecmp(SG(sapi_headers).mimetype, "text/", 5) == 0) { - if ((s = strchr(SG(sapi_headers).mimetype,';')) == NULL){ - mimetype = SG(sapi_headers).mimetype; - } else { - mimetype = estrndup(SG(sapi_headers).mimetype, s-SG(sapi_headers).mimetype); - mimetype_alloced = 1; - } - } else if (SG(sapi_headers).send_default_content_type) { - mimetype =(SG(default_mimetype) ? SG(default_mimetype) : SAPI_DEFAULT_MIMETYPE); - } - if (mimetype != NULL) { - php_iconv_err_t err = php_iconv_string(Z_STRVAL_P(zv_string), - Z_STRLEN_P(zv_string), &out_buffer, &out_len, - ICONVG(output_encoding), ICONVG(internal_encoding)); - _php_iconv_show_error(err, ICONVG(output_encoding), ICONVG(internal_encoding) TSRMLS_CC); - if (out_buffer != NULL) { - int len = spprintf(&content_type, 0, "Content-Type:%s; charset=%s", mimetype, ICONVG(output_encoding)); - if (content_type && sapi_add_header(content_type, len, 0) != FAILURE) { - SG(sapi_headers).send_default_content_type = 0; - } - if (mimetype_alloced) { - efree(mimetype); - } - RETURN_STRINGL(out_buffer, out_len, 0); - } - if (mimetype_alloced) { - efree(mimetype); - } - } - - zval_dtor(return_value); - *return_value = *zv_string; - zval_copy_ctor(return_value); -} -/* }}} */ - /* {{{ proto bool iconv_set_encoding(string type, string charset) Sets internal encoding and output encoding for ob_iconv_handler() */ PHP_FUNCTION(iconv_set_encoding) Index: ext/iconv/php_iconv.h =================================================================== RCS file: /repository/php-src/ext/iconv/php_iconv.h,v retrieving revision 1.28.2.2.2.2.2.4 diff -u -p -d -r1.28.2.2.2.2.2.4 php_iconv.h --- ext/iconv/php_iconv.h 21 May 2008 15:03:20 -0000 1.28.2.2.2.2.2.4 +++ ext/iconv/php_iconv.h 18 Sep 2008 18:38:17 -0000 @@ -55,7 +55,6 @@ PHP_MSHUTDOWN_FUNCTION(miconv); PHP_MINFO_FUNCTION(miconv); PHP_NAMED_FUNCTION(php_if_iconv); -PHP_FUNCTION(ob_iconv_handler); PHP_FUNCTION(iconv_get_encoding); PHP_FUNCTION(iconv_set_encoding); PHP_FUNCTION(iconv_strlen); Index: ext/pgsql/pgsql.c =================================================================== RCS file: /repository/php-src/ext/pgsql/pgsql.c,v retrieving revision 1.331.2.13.2.24.2.14 diff -u -p -d -r1.331.2.13.2.24.2.14 pgsql.c --- ext/pgsql/pgsql.c 10 Sep 2008 01:39:35 -0000 1.331.2.13.2.24.2.14 +++ ext/pgsql/pgsql.c 18 Sep 2008 18:38:17 -0000 @@ -3376,7 +3376,7 @@ PHP_FUNCTION(pg_lo_read_all) tbytes = 0; while ((nbytes = lo_read((PGconn *)pgsql->conn, pgsql->lofd, buf, PGSQL_LO_READ_BUF_SIZE))>0) { - php_body_write(buf, nbytes TSRMLS_CC); + PHPWRITE(buf, nbytes); tbytes += nbytes; } RETURN_LONG(tbytes); Index: ext/session/session.c =================================================================== RCS file: /repository/php-src/ext/session/session.c,v retrieving revision 1.417.2.8.2.40.2.14 diff -u -p -d -r1.417.2.8.2.40.2.14 session.c --- ext/session/session.c 6 Aug 2008 05:53:31 -0000 1.417.2.8.2.40.2.14 +++ ext/session/session.c 18 Sep 2008 18:38:17 -0000 @@ -1184,8 +1184,8 @@ static int php_session_cache_limiter(TSR if (PS(cache_limiter)[0] == '\0') return 0; if (SG(headers_sent)) { - char *output_start_filename = php_get_output_start_filename(TSRMLS_C); - int output_start_lineno = php_get_output_start_lineno(TSRMLS_C); + char *output_start_filename = php_output_get_start_filename(TSRMLS_C); + int output_start_lineno = php_output_get_start_lineno(TSRMLS_C); if (output_start_filename) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot send session cache limiter - headers already sent (output started at %s:%d)", @@ -1225,8 +1225,8 @@ static void php_session_send_cookie(TSRM char *e_session_name, *e_id; if (SG(headers_sent)) { - char *output_start_filename = php_get_output_start_filename(TSRMLS_C); - int output_start_lineno = php_get_output_start_lineno(TSRMLS_C); + char *output_start_filename = php_output_get_start_filename(TSRMLS_C); + int output_start_lineno = php_output_get_start_lineno(TSRMLS_C); if (output_start_filename) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot send session cookie - headers already sent by (output started at %s:%d)", Index: ext/soap/soap.c =================================================================== RCS file: /repository/php-src/ext/soap/soap.c,v retrieving revision 1.156.2.28.2.30.2.23 diff -u -p -d -r1.156.2.28.2.30.2.23 soap.c --- ext/soap/soap.c 15 Sep 2008 18:07:16 -0000 1.156.2.28.2.30.2.23 +++ ext/soap/soap.c 18 Sep 2008 18:38:17 -0000 @@ -1680,7 +1680,7 @@ PHP_METHOD(SoapServer, handle) ALLOC_INIT_ZVAL(retval); - if (php_start_ob_buffer(NULL, 0, 0 TSRMLS_CC) != SUCCESS) { + if (php_output_start_default(TSRMLS_C) != SUCCESS) { php_error_docref(NULL TSRMLS_CC, E_ERROR,"ob_start failed"); } @@ -1767,7 +1767,7 @@ PHP_METHOD(SoapServer, handle) #ifdef ZEND_ENGINE_2 if (EG(exception)) { - php_end_ob_buffer(0, 0 TSRMLS_CC); + php_output_discard(TSRMLS_C); if (Z_TYPE_P(EG(exception)) == IS_OBJECT && instanceof_function(Z_OBJCE_P(EG(exception)), soap_fault_class_entry TSRMLS_CC)) { soap_server_fault_ex(function, EG(exception), NULL TSRMLS_CC); @@ -1821,7 +1821,7 @@ PHP_METHOD(SoapServer, handle) php_error_docref(NULL TSRMLS_CC, E_ERROR, "Error calling constructor"); } if (EG(exception)) { - php_end_ob_buffer(0, 0 TSRMLS_CC); + php_output_discard(TSRMLS_C); if (Z_TYPE_P(EG(exception)) == IS_OBJECT && instanceof_function(Z_OBJCE_P(EG(exception)), soap_fault_class_entry TSRMLS_CC)) { soap_server_fault_ex(function, EG(exception), NULL TSRMLS_CC); @@ -1853,7 +1853,7 @@ PHP_METHOD(SoapServer, handle) } #ifdef ZEND_ENGINE_2 if (EG(exception)) { - php_end_ob_buffer(0, 0 TSRMLS_CC); + php_output_discard(TSRMLS_C); if (Z_TYPE_P(EG(exception)) == IS_OBJECT && instanceof_function(Z_OBJCE_P(EG(exception)), soap_fault_class_entry TSRMLS_CC)) { soap_server_fault_ex(function, EG(exception), NULL TSRMLS_CC); @@ -1932,14 +1932,14 @@ PHP_METHOD(SoapServer, handle) Z_TYPE_PP(tmp) != IS_NULL) { headerfault = *tmp; } - php_end_ob_buffer(0, 0 TSRMLS_CC); + php_output_discard(TSRMLS_C); soap_server_fault_ex(function, &h->retval, h TSRMLS_CC); efree(fn_name); if (service->type == SOAP_CLASS && soap_obj) {zval_ptr_dtor(&soap_obj);} goto fail; #ifdef ZEND_ENGINE_2 } else if (EG(exception)) { - php_end_ob_buffer(0, 0 TSRMLS_CC); + php_output_discard(TSRMLS_C); if (Z_TYPE_P(EG(exception)) == IS_OBJECT && instanceof_function(Z_OBJCE_P(EG(exception)), soap_fault_class_entry TSRMLS_CC)) { zval *headerfault = NULL, **tmp; @@ -1989,7 +1989,7 @@ PHP_METHOD(SoapServer, handle) #ifdef ZEND_ENGINE_2 if (EG(exception)) { - php_end_ob_buffer(0, 0 TSRMLS_CC); + php_output_discard(TSRMLS_C); if (Z_TYPE_P(EG(exception)) == IS_OBJECT && instanceof_function(Z_OBJCE_P(EG(exception)), soap_fault_class_entry TSRMLS_CC)) { soap_server_fault_ex(function, EG(exception), NULL TSRMLS_CC); @@ -2011,7 +2011,7 @@ PHP_METHOD(SoapServer, handle) if (Z_TYPE_P(retval) == IS_OBJECT && instanceof_function(Z_OBJCE_P(retval), soap_fault_class_entry TSRMLS_CC)) { - php_end_ob_buffer(0, 0 TSRMLS_CC); + php_output_discard(TSRMLS_C); soap_server_fault_ex(function, retval, NULL TSRMLS_CC); goto fail; } @@ -2032,7 +2032,7 @@ PHP_METHOD(SoapServer, handle) #ifdef ZEND_ENGINE_2 if (EG(exception)) { - php_end_ob_buffer(0, 0 TSRMLS_CC); + php_output_discard(TSRMLS_C); if (Z_TYPE_P(EG(exception)) == IS_OBJECT && instanceof_function(Z_OBJCE_P(EG(exception)), soap_fault_class_entry TSRMLS_CC)) { soap_server_fault_ex(function, EG(exception), NULL TSRMLS_CC); @@ -2051,7 +2051,7 @@ PHP_METHOD(SoapServer, handle) #endif /* Flush buffer */ - php_end_ob_buffer(0, 0 TSRMLS_CC); + php_output_discard(TSRMLS_C); if (doc_return) { /* xmlDocDumpMemoryEnc(doc_return, &buf, &size, XML_CHAR_ENCODING_UTF8); */ @@ -2438,11 +2438,11 @@ static void soap_error_handler(int error } /* Get output buffer and send as fault detials */ - if (php_ob_get_length(&outbuflen TSRMLS_CC) != FAILURE && Z_LVAL(outbuflen) != 0) { + if (php_output_get_length(&outbuflen TSRMLS_CC) != FAILURE && Z_LVAL(outbuflen) != 0) { ALLOC_INIT_ZVAL(outbuf); - php_ob_get_buffer(outbuf TSRMLS_CC); + php_output_get_contents(outbuf TSRMLS_CC); } - php_end_ob_buffer(0, 0 TSRMLS_CC); + php_output_discard(TSRMLS_C); } INIT_ZVAL(fault_obj); Index: ext/standard/basic_functions.c =================================================================== RCS file: /repository/php-src/ext/standard/basic_functions.c,v retrieving revision 1.725.2.31.2.64.2.63 diff -u -p -d -r1.725.2.31.2.64.2.63 basic_functions.c --- ext/standard/basic_functions.c 31 Aug 2008 00:19:50 -0000 1.725.2.31.2.64.2.63 +++ ext/standard/basic_functions.c 18 Sep 2008 18:38:18 -0000 @@ -810,7 +810,8 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_error_log ZEND_END_ARG_INFO() static -ZEND_BEGIN_ARG_INFO_EX(arginfo_error_get_last, 0, 0, 0) +ZEND_BEGIN_ARG_INFO(arginfo_error_get_last, 0) + ZEND_ARG_INFO(0, part) ZEND_END_ARG_INFO() static @@ -5121,20 +5122,43 @@ PHPAPI int _php_error_log(int opt_err, c } /* }}} */ -/* {{{ proto array error_get_last() - Get the last occurred error as associative array. Returns NULL if there hasn't been an error yet. */ +/* {{{ proto array error_get_last([string part]) + Get the last occurred error as associative array. Returns NULL if there hasn't been an error yet. Optional parameter part defines the part of the error to return, *m*essage, *t*ype, *f*ile or *l*ineno. */ PHP_FUNCTION(error_get_last) { - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE) { + int part_len = 0, show_notice = 0; + char *part = NULL; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!", &part, &part_len) == FAILURE) { return; } if (PG(last_error_message)) { + if (part && *part) { + switch (tolower(*part)) { + case 't': + RETURN_LONG(PG(last_error_type)); + case 'm': + RETURN_STRING(PG(last_error_message), 1); + case 'f': + RETURN_STRING(PG(last_error_file)?PG(last_error_file):"-", 1); + case 'l': + RETURN_LONG(PG(last_error_lineno)); + default: + show_notice = 1; + break; + } + } + array_init(return_value); add_assoc_long_ex(return_value, "type", sizeof("type"), PG(last_error_type)); add_assoc_string_ex(return_value, "message", sizeof("message"), PG(last_error_message), 1); add_assoc_string_ex(return_value, "file", sizeof("file"), PG(last_error_file)?PG(last_error_file):"-", 1 ); add_assoc_long_ex(return_value, "line", sizeof("line"), PG(last_error_lineno)); + + if (show_notice) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "unknown last error part '%c'", *part); + } } } /* }}} */ @@ -5551,21 +5575,21 @@ PHP_FUNCTION(highlight_file) } if (i) { - php_start_ob_buffer (NULL, 0, 1 TSRMLS_CC); + php_output_start_default(TSRMLS_C); } php_get_highlight_struct(&syntax_highlighter_ini); if (highlight_file(filename, &syntax_highlighter_ini TSRMLS_CC) == FAILURE) { if (i) { - int res = php_ob_get_buffer(return_value TSRMLS_CC); + int res = php_output_get_contents(return_value TSRMLS_CC); /* flush the buffer only if there is something to flush */ if (res == SUCCESS && Z_STRLEN_P(return_value) > 0) { - php_end_ob_buffer (1, 0 TSRMLS_CC); + php_output_end(TSRMLS_C); zval_dtor(return_value); } else { - php_end_ob_buffer (0, 0 TSRMLS_CC); + php_output_discard(TSRMLS_C); if (res == SUCCESS) { zval_dtor(return_value); } @@ -5575,8 +5599,8 @@ PHP_FUNCTION(highlight_file) } if (i) { - php_ob_get_buffer (return_value TSRMLS_CC); - php_end_ob_buffer (0, 0 TSRMLS_CC); + php_output_get_contents(return_value TSRMLS_CC); + php_output_discard(TSRMLS_C); } else { RETURN_TRUE; } @@ -5606,15 +5630,15 @@ PHP_FUNCTION(php_strip_whitespace) RETURN_EMPTY_STRING(); } - php_start_ob_buffer(NULL, 0, 1 TSRMLS_CC); + php_output_start_default(TSRMLS_C); zend_strip(TSRMLS_C); zend_destroy_file_handle(&file_handle TSRMLS_CC); zend_restore_lexical_state(&original_lex_state TSRMLS_CC); - php_ob_get_buffer(return_value TSRMLS_CC); - php_end_ob_buffer(0, 0 TSRMLS_CC); + php_output_get_contents(return_value TSRMLS_CC); + php_output_discard(TSRMLS_C); } /* }}} */ @@ -5634,7 +5658,7 @@ PHP_FUNCTION(highlight_string) convert_to_string_ex(expr); if (i) { - php_start_ob_buffer (NULL, 0, 1 TSRMLS_CC); + php_output_start_default(TSRMLS_C); } EG(error_reporting) = E_ERROR; @@ -5647,7 +5671,7 @@ PHP_FUNCTION(highlight_string) efree(hicompiled_string_description); EG(error_reporting) = old_error_reporting; if (i) { - php_end_ob_buffer (1, 0 TSRMLS_CC); + php_output_end(TSRMLS_C); } RETURN_FALSE; } @@ -5656,8 +5680,8 @@ PHP_FUNCTION(highlight_string) EG(error_reporting) = old_error_reporting; if (i) { - php_ob_get_buffer (return_value TSRMLS_CC); - php_end_ob_buffer (0, 0 TSRMLS_CC); + php_output_get_contents(return_value TSRMLS_CC); + php_output_discard(TSRMLS_C); } else { RETURN_TRUE; } @@ -5913,14 +5937,14 @@ PHP_FUNCTION(print_r) } if (do_return) { - php_start_ob_buffer (NULL, 0, 1 TSRMLS_CC); + php_output_start_default(TSRMLS_C); } zend_print_zval_r(var, 0 TSRMLS_CC); if (do_return) { - php_ob_get_buffer (return_value TSRMLS_CC); - php_end_ob_buffer (0, 0 TSRMLS_CC); + php_output_get_contents(return_value TSRMLS_CC); + php_output_discard(TSRMLS_C); } else { RETURN_TRUE; } Index: ext/standard/head.c =================================================================== RCS file: /repository/php-src/ext/standard/head.c,v retrieving revision 1.84.2.1.2.7.2.4 diff -u -p -d -r1.84.2.1.2.7.2.4 head.c --- ext/standard/head.c 10 Jun 2008 08:14:52 -0000 1.84.2.1.2.7.2.4 +++ ext/standard/head.c 18 Sep 2008 18:38:18 -0000 @@ -205,8 +205,8 @@ PHP_FUNCTION(headers_sent) return; if (SG(headers_sent)) { - line = php_get_output_start_lineno(TSRMLS_C); - file = php_get_output_start_filename(TSRMLS_C); + line = php_output_get_start_lineno(TSRMLS_C); + file = php_output_get_start_filename(TSRMLS_C); } switch(ZEND_NUM_ARGS()) { Index: ext/standard/info.c =================================================================== RCS file: /repository/php-src/ext/standard/info.c,v retrieving revision 1.249.2.10.2.14.2.11 diff -u -p -d -r1.249.2.10.2.14.2.11 info.c --- ext/standard/info.c 13 Aug 2008 00:53:28 -0000 1.249.2.10.2.14.2.11 +++ ext/standard/info.c 18 Sep 2008 18:38:18 -0000 @@ -69,7 +69,7 @@ static int php_info_write_wrapper(const elem_esc = php_escape_html_entities((unsigned char *)str, str_length, &new_len, 0, ENT_QUOTES, NULL TSRMLS_CC); - written = php_body_write(elem_esc, new_len TSRMLS_CC); + written = php_output_write(elem_esc, new_len TSRMLS_CC); efree(elem_esc); @@ -1010,9 +1010,9 @@ PHP_FUNCTION(phpinfo) } /* Andale! Andale! Yee-Hah! */ - php_start_ob_buffer(NULL, 4096, 0 TSRMLS_CC); + php_output_start_default(TSRMLS_C); php_print_info(flag TSRMLS_CC); - php_end_ob_buffer(1, 0 TSRMLS_CC); + php_output_end(TSRMLS_C); RETURN_TRUE; } Index: ext/standard/url_scanner_ex.c =================================================================== RCS file: /repository/php-src/ext/standard/url_scanner_ex.c,v retrieving revision 1.95.2.4.2.3.2.5 diff -u -p -d -r1.95.2.4.2.3.2.5 url_scanner_ex.c --- ext/standard/url_scanner_ex.c 8 Apr 2008 12:17:03 -0000 1.95.2.4.2.3.2.5 +++ ext/standard/url_scanner_ex.c 18 Sep 2008 18:38:18 -0000 @@ -1,4 +1,4 @@ -/* Generated by re2c 0.13.4 on Tue Apr 8 13:43:20 2008 */ +/* Generated by re2c 0.13.5 on Tue Sep 16 20:24:28 2008 */ #line 1 "ext/standard/url_scanner_ex.re" /* +----------------------------------------------------------------------+ @@ -18,7 +18,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: url_scanner_ex.c,v 1.95.2.4.2.3.2.5 2008/04/08 12:17:03 jani Exp $ */ +/* $Id: url_scanner_ex.re,v 1.76.2.2.2.1.2.2 2008/03/12 19:34:37 felipe Exp $ */ #include "php.h" @@ -1024,11 +1024,15 @@ int php_url_scanner_add_var(char *name, char *encoded; int encoded_len; smart_str val; + zval *ob_name; if (! BG(url_adapt_state_ex).active) { + MAKE_STD_ZVAL(ob_name); + ZVAL_STRING(ob_name, "URL-Rewriter", 1); php_url_scanner_ex_activate(TSRMLS_C); - php_ob_set_internal_handler(php_url_scanner_output_handler, 0, "URL-Rewriter", 1 TSRMLS_CC); + php_output_start_internal(ob_name, php_url_scanner_output_handler, 0, PHP_OUTPUT_HANDLER_STDFLAGS TSRMLS_CC); BG(url_adapt_state_ex).active = 1; + zval_ptr_dtor(&ob_name); } Index: ext/standard/url_scanner_ex.re =================================================================== RCS file: /repository/php-src/ext/standard/url_scanner_ex.re,v retrieving revision 1.76.2.2.2.1.2.2 diff -u -p -d -r1.76.2.2.2.1.2.2 url_scanner_ex.re --- ext/standard/url_scanner_ex.re 12 Mar 2008 19:34:37 -0000 1.76.2.2.2.1.2.2 +++ ext/standard/url_scanner_ex.re 18 Sep 2008 18:38:18 -0000 @@ -462,11 +462,15 @@ int php_url_scanner_add_var(char *name, char *encoded; int encoded_len; smart_str val; + zval *ob_name; if (! BG(url_adapt_state_ex).active) { + MAKE_STD_ZVAL(ob_name); + ZVAL_STRING(ob_name, "URL-Rewriter", 1); php_url_scanner_ex_activate(TSRMLS_C); - php_ob_set_internal_handler(php_url_scanner_output_handler, 0, "URL-Rewriter", 1 TSRMLS_CC); + php_output_start_internal(ob_name, php_url_scanner_output_handler, 0, PHP_OUTPUT_HANDLER_STDFLAGS TSRMLS_CC); BG(url_adapt_state_ex).active = 1; + zval_ptr_dtor(&ob_name); } Index: ext/standard/var.c =================================================================== RCS file: /repository/php-src/ext/standard/var.c,v retrieving revision 1.203.2.7.2.18.2.12 diff -u -p -d -r1.203.2.7.2.18.2.12 var.c --- ext/standard/var.c 20 Aug 2008 21:26:04 -0000 1.203.2.7.2.18.2.12 +++ ext/standard/var.c 18 Sep 2008 18:38:18 -0000 @@ -12,7 +12,7 @@ | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ - | Authors: Jani Lehtimäki | + | Authors: Jani Lehtim�ki | | Thies C. Arntzen | | Sascha Schumann | +----------------------------------------------------------------------+ @@ -451,14 +451,14 @@ PHP_FUNCTION(var_export) } if (return_output) { - php_start_ob_buffer (NULL, 0, 1 TSRMLS_CC); + php_output_start_default(TSRMLS_C); } php_var_export(&var, 1 TSRMLS_CC); if (return_output) { - php_ob_get_buffer (return_value TSRMLS_CC); - php_end_ob_buffer (0, 0 TSRMLS_CC); + php_output_get_contents(return_value TSRMLS_CC); + php_output_discard(TSRMLS_C); } } /* }}} */ Index: ext/tidy/tidy.c =================================================================== RCS file: /repository/php-src/ext/tidy/tidy.c,v retrieving revision 1.66.2.8.2.24.2.7 diff -u -p -d -r1.66.2.8.2.24.2.7 tidy.c --- ext/tidy/tidy.c 3 Jul 2008 01:55:47 -0000 1.66.2.8.2.24.2.7 +++ ext/tidy/tidy.c 18 Sep 2008 18:38:18 -0000 @@ -1104,9 +1104,13 @@ static PHP_MINIT_FUNCTION(tidy) static PHP_RINIT_FUNCTION(tidy) { if (INI_BOOL("tidy.clean_output") == TRUE) { - if (php_start_ob_buffer_named("ob_tidyhandler", 0, 1 TSRMLS_CC) == FAILURE) { + zval *name; + MAKE_STD_ZVAL(name); + ZVAL_STRINGL(name, "ob_tidyhandler", sizeof("ob_tidyhandler")-1, 1); + if (php_output_start_user(name, 0, PHP_OUTPUT_HANDLER_STDFLAGS TSRMLS_CC) == FAILURE) { zend_error(E_NOTICE, "Failure installing Tidy output buffering."); } + zval_ptr_dtor(&name); } return SUCCESS; Index: ext/zlib/config.w32 =================================================================== RCS file: /repository/php-src/ext/zlib/config.w32,v retrieving revision 1.8.6.2 diff -u -p -d -r1.8.6.2 config.w32 --- ext/zlib/config.w32 22 Jun 2008 21:55:59 -0000 1.8.6.2 +++ ext/zlib/config.w32 18 Sep 2008 18:38:19 -0000 @@ -1,4 +1,4 @@ -// $Id: config.w32,v 1.8.6.2 2008/06/22 21:55:59 pajoye Exp $ +// $Id: config.w32,v 1.10 2008/07/02 20:52:35 pajoye Exp $ // vim:ft=javascript ARG_ENABLE("zlib", "ZLIB support", "yes"); @@ -18,5 +18,3 @@ if (PHP_ZLIB == "yes") { PHP_ZLIB = "no" } } - - Index: ext/zlib/config0.m4 =================================================================== RCS file: /repository/php-src/ext/zlib/config0.m4,v retrieving revision 1.17.2.1 diff -u -p -d -r1.17.2.1 config0.m4 --- ext/zlib/config0.m4 29 Nov 2005 18:35:26 -0000 1.17.2.1 +++ ext/zlib/config0.m4 18 Sep 2008 18:38:19 -0000 @@ -1,5 +1,5 @@ dnl -dnl $Id: config0.m4,v 1.17.2.1 2005/11/29 18:35:26 tony2001 Exp $ +dnl $Id: config0.m4,v 1.19 2006/08/30 07:42:02 mike Exp $ dnl PHP_ARG_WITH(zlib,for ZLIB support, @@ -41,10 +41,17 @@ if test "$PHP_ZLIB" != "no" || test "$PH *) ac_extra=-L$ZLIB_DIR/$PHP_LIBDIR ;; esac + AC_MSG_CHECKING([for zlib version >= 1.2.0.4]) + ZLIB_VERSION=`$EGREP "define ZLIB_VERSION" $ZLIB_DIR/include/zlib.h | $SED -e 's/[[^0-9\.]]//g'` + AC_MSG_RESULT([$ZLIB_VERSION]) + if test `echo $ZLIB_VERSION | $SED -e 's/[[^0-9]]/ /g' | $AWK '{print $1*1000000 + $2*10000 + $3*100 + $4}'` -lt 1020004; then + AC_MSG_ERROR([libz version greater or equal to 1.2.0.4 required]) + fi + PHP_CHECK_LIBRARY(z, gzgets, [ AC_DEFINE(HAVE_ZLIB,1,[ ]) ],[ - AC_MSG_ERROR(ZLIB extension requires zlib >= 1.0.9) + AC_MSG_ERROR(ZLIB extension requires gzgets in zlib) ],[ $ac_extra ]) Index: ext/zlib/php_zlib.h =================================================================== RCS file: /repository/php-src/ext/zlib/php_zlib.h,v retrieving revision 1.42.2.1.2.2.2.1 diff -u -p -d -r1.42.2.1.2.2.2.1 php_zlib.h --- ext/zlib/php_zlib.h 31 Dec 2007 07:17:17 -0000 1.42.2.1.2.2.2.1 +++ ext/zlib/php_zlib.h 18 Sep 2008 18:38:19 -0000 @@ -14,47 +14,91 @@ +----------------------------------------------------------------------+ | Authors: Rasmus Lerdorf | | Stefan Röhrich | + | Michael Wallner | +----------------------------------------------------------------------+ */ -/* $Id: php_zlib.h,v 1.42.2.1.2.2.2.1 2007/12/31 07:17:17 sebastian Exp $ */ +/* $Id: php_zlib.h,v 1.52 2007/12/31 07:12:18 sebastian Exp $ */ #ifndef PHP_ZLIB_H #define PHP_ZLIB_H #include +#define PHP_ZLIB_ENCODING_RAW -0xf +#define PHP_ZLIB_ENCODING_GZIP 0x1f +#define PHP_ZLIB_ENCODING_DEFLATE 0x0f + +#define PHP_ZLIB_ENCODING_ANY 0x2f + +#define PHP_ZLIB_OUTPUT_HANDLER_NAME "zlib output compression" +#define PHP_ZLIB_BUFFER_SIZE_GUESS(in_len) (((size_t) ((double) in_len * (double) 1.015)) + 10 + 8 + 4 + 1) + ZEND_BEGIN_MODULE_GLOBALS(zlib) /* variables for transparent gzip encoding */ int compression_coding; - z_stream stream; - uLong crc; - int ob_gzhandler_status; long output_compression; long output_compression_level; char *output_handler; -ZEND_END_MODULE_GLOBALS(zlib) +ZEND_END_MODULE_GLOBALS(zlib); + +typedef struct _php_zlib_buffer { + char *data; + char *aptr; + size_t used; + size_t free; + size_t size; +} php_zlib_buffer; + +typedef struct _php_zlib_context { + z_stream Z; + php_zlib_buffer buffer; +} php_zlib_context; + +int php_zlib_output_encoding(TSRMLS_D); +int php_zlib_encode(const char *in_buf, size_t in_len, char **out_buf, size_t *out_len, int encoding, int level TSRMLS_DC); +int php_zlib_decode(const char *in_buf, size_t in_len, char **out_buf, size_t *out_len, int encoding, size_t max_len TSRMLS_DC); +php_output_handler *php_zlib_output_handler_init(zval *handler_name, size_t chunk_size, int flags TSRMLS_DC); +int php_zlib_output_handler(void **handler_context, php_output_context *output_context); +void php_zlib_output_handler_dtor(void *opaq TSRMLS_DC); +int php_zlib_output_conflict_check(zval *handler_name TSRMLS_DC); +php_stream *php_stream_gzopen(php_stream_wrapper *wrapper, char *path, char *mode, int options, char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC); +extern php_stream_ops php_stream_gzio_ops; +extern php_stream_wrapper php_stream_gzip_wrapper; extern php_stream_filter_factory php_zlib_filter_factory; extern zend_module_entry php_zlib_module_entry; #define zlib_module_ptr &php_zlib_module_entry +#define phpext_zlib_ptr zlib_module_ptr -int php_ob_gzhandler_check(TSRMLS_D); +PHP_MINIT_FUNCTION(zlib); +PHP_MSHUTDOWN_FUNCTION(zlib); +PHP_RINIT_FUNCTION(zlib); +PHP_MINFO_FUNCTION(zlib); + +PHP_FUNCTION(gzfile); +PHP_FUNCTION(gzopen); +PHP_FUNCTION(readgzfile); + +PHP_FUNCTION(gzcompress); +PHP_FUNCTION(gzuncompress); +PHP_FUNCTION(gzdeflate); +PHP_FUNCTION(gzinflate); +PHP_FUNCTION(gzencode); +PHP_FUNCTION(gzdecode); + +PHP_FUNCTION(zlib_encode); +PHP_FUNCTION(zlib_decode); + +PHP_FUNCTION(zlib_get_coding_type); -php_stream *php_stream_gzopen(php_stream_wrapper *wrapper, char *path, char *mode, int options, char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC); -extern php_stream_wrapper php_stream_gzip_wrapper; #ifdef ZTS -#define ZLIBG(v) TSRMG(zlib_globals_id, zend_zlib_globals *, v) +# define ZLIBG(v) TSRMG(zlib_globals_id, zend_zlib_globals *, v) #else -#define ZLIBG(v) (zlib_globals.v) +# define ZLIBG(v) (zlib_globals.v) #endif -#define phpext_zlib_ptr zlib_module_ptr - -#define CODING_GZIP 1 -#define CODING_DEFLATE 2 - #endif /* PHP_ZLIB_H */ /* Index: ext/zlib/zlib.c =================================================================== RCS file: /repository/php-src/ext/zlib/zlib.c,v retrieving revision 1.183.2.6.2.5.2.5 diff -u -p -d -r1.183.2.6.2.5.2.5 zlib.c --- ext/zlib/zlib.c 2 Aug 2008 02:36:25 -0000 1.183.2.6.2.5.2.5 +++ ext/zlib/zlib.c 18 Sep 2008 18:38:19 -0000 @@ -13,210 +13,28 @@ | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Rasmus Lerdorf | - | Stefan Röhrich | + | Stefan R�hrich | | Zeev Suraski | | Jade Nicoletti | + | Michael Wallner | +----------------------------------------------------------------------+ */ -/* $Id: zlib.c,v 1.183.2.6.2.5.2.5 2008/08/02 02:36:25 lbarnaud Exp $ */ +/* $Id: zlib.c,v 1.220 2008/08/02 02:36:14 lbarnaud Exp $ */ #ifdef HAVE_CONFIG_H -#include "config.h" +# include "config.h" #endif #include "php.h" #include "SAPI.h" #include "php_ini.h" - -#include -#include -#include -#include -#include -#ifdef PHP_WIN32 -#define O_RDONLY _O_RDONLY -#include "win32/param.h" -#else -#include -/* #include */ -#endif -#include "ext/standard/head.h" -#include "safe_mode.h" -#include "ext/standard/php_standard.h" #include "ext/standard/info.h" -#include "php_zlib.h" -#include "fopen_wrappers.h" -#if HAVE_PWD_H -#ifdef PHP_WIN32 -#include "win32/pwd.h" -#else -#include -#endif -#endif -#if defined(HAVE_UNISTD_H) && defined(PHP_WIN32) -#undef HAVE_UNISTD_H -#endif - -#ifdef COMPILE_DL_ZLIB -#ifndef PUTS -#define PUTS(a) php_printf("%s",a) -#endif -#ifndef PUTC -#define PUTC(a) PUTS(a) -#endif -#ifndef PHPWRITE -#define PHPWRITE(a,n) php_write((a),(n) TSRMLS_CC) -#endif -#endif - -/* Win32 needs some more memory */ -#ifdef PHP_WIN32 -#define PHP_ZLIB_MODIFIER 100 -#else -#define PHP_ZLIB_MODIFIER 1000 -#endif - -#define OS_CODE 0x03 /* FIXME */ -#define GZIP_HEADER_LENGTH 10 -#define GZIP_FOOTER_LENGTH 8 - -/* True globals, no need for thread safety */ -static const int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */ - -static int php_enable_output_compression(int buffer_size TSRMLS_DC); - -static PHP_MINIT_FUNCTION(zlib); -static PHP_MSHUTDOWN_FUNCTION(zlib); -static PHP_RINIT_FUNCTION(zlib); -static PHP_MINFO_FUNCTION(zlib); -static PHP_FUNCTION(gzopen); -static PHP_FUNCTION(readgzfile); -static PHP_FUNCTION(gzfile); -static PHP_FUNCTION(gzcompress); -static PHP_FUNCTION(gzuncompress); -static PHP_FUNCTION(gzdeflate); -static PHP_FUNCTION(gzinflate); -static PHP_FUNCTION(gzencode); -static PHP_FUNCTION(ob_gzhandler); -static PHP_FUNCTION(zlib_get_coding_type); - -/* {{{ arginfo */ -static -ZEND_BEGIN_ARG_INFO_EX(arginfo_gzfile, 0, 0, 1) - ZEND_ARG_INFO(0, filename) - ZEND_ARG_INFO(0, use_include_path) -ZEND_END_ARG_INFO() - -static -ZEND_BEGIN_ARG_INFO_EX(arginfo_gzopen, 0, 0, 2) - ZEND_ARG_INFO(0, filename) - ZEND_ARG_INFO(0, mode) - ZEND_ARG_INFO(0, use_include_path) -ZEND_END_ARG_INFO() - -static -ZEND_BEGIN_ARG_INFO_EX(arginfo_readgzfile, 0, 0, 1) - ZEND_ARG_INFO(0, filename) - ZEND_ARG_INFO(0, use_include_path) -ZEND_END_ARG_INFO() - -static -ZEND_BEGIN_ARG_INFO_EX(arginfo_gzcompress, 0, 0, 1) - ZEND_ARG_INFO(0, data) - ZEND_ARG_INFO(0, level) -ZEND_END_ARG_INFO() - -static -ZEND_BEGIN_ARG_INFO_EX(arginfo_gzuncompress, 0, 0, 1) - ZEND_ARG_INFO(0, data) - ZEND_ARG_INFO(0, length) -ZEND_END_ARG_INFO() - -static -ZEND_BEGIN_ARG_INFO_EX(arginfo_gzdeflate, 0, 0, 1) - ZEND_ARG_INFO(0, data) - ZEND_ARG_INFO(0, level) -ZEND_END_ARG_INFO() - -static -ZEND_BEGIN_ARG_INFO_EX(arginfo_gzinflate, 0, 0, 1) - ZEND_ARG_INFO(0, data) - ZEND_ARG_INFO(0, length) -ZEND_END_ARG_INFO() - -static -ZEND_BEGIN_ARG_INFO(arginfo_zlib_get_coding_type, 0) -ZEND_END_ARG_INFO() - -static -ZEND_BEGIN_ARG_INFO_EX(arginfo_gzencode, 0, 0, 1) - ZEND_ARG_INFO(0, data) - ZEND_ARG_INFO(0, level) - ZEND_ARG_INFO(0, encoding_mode) -ZEND_END_ARG_INFO() - -static -ZEND_BEGIN_ARG_INFO_EX(arginfo_ob_gzhandler, 0, 0, 2) - ZEND_ARG_INFO(0, str) - ZEND_ARG_INFO(0, mode) -ZEND_END_ARG_INFO() -/* }}} */ - -/* {{{ php_zlib_functions[] - */ -static const zend_function_entry php_zlib_functions[] = { - PHP_FE(readgzfile, arginfo_readgzfile) - PHP_FALIAS(gzrewind, rewind, NULL) - PHP_FALIAS(gzclose, fclose, NULL) - PHP_FALIAS(gzeof, feof, NULL) - PHP_FALIAS(gzgetc, fgetc, NULL) - PHP_FALIAS(gzgets, fgets, NULL) - PHP_FALIAS(gzgetss, fgetss, NULL) - PHP_FALIAS(gzread, fread, NULL) - PHP_FE(gzopen, arginfo_gzopen) - PHP_FALIAS(gzpassthru, fpassthru, NULL) - PHP_FALIAS(gzseek, fseek, NULL) - PHP_FALIAS(gztell, ftell, NULL) - PHP_FALIAS(gzwrite, fwrite, NULL) - PHP_FALIAS(gzputs, fwrite, NULL) - PHP_FE(gzfile, arginfo_gzfile) - PHP_FE(gzcompress, arginfo_gzcompress) - PHP_FE(gzuncompress, arginfo_gzuncompress) - PHP_FE(gzdeflate, arginfo_gzdeflate) - PHP_FE(gzinflate, arginfo_gzinflate) - PHP_FE(gzencode, arginfo_gzencode) - PHP_FE(ob_gzhandler, arginfo_ob_gzhandler) - PHP_FE(zlib_get_coding_type, arginfo_zlib_get_coding_type) - {NULL, NULL, NULL} -}; -/* }}} */ - -ZEND_DECLARE_MODULE_GLOBALS(zlib) +#include "ext/standard/file.h" -/* {{{ php_zlib_module_entry - */ -zend_module_entry php_zlib_module_entry = { - STANDARD_MODULE_HEADER, - "zlib", - php_zlib_functions, - PHP_MINIT(zlib), - PHP_MSHUTDOWN(zlib), - PHP_RINIT(zlib), - NULL, - PHP_MINFO(zlib), - "1.1", - PHP_MODULE_GLOBALS(zlib), - NULL, - NULL, - NULL, - STANDARD_MODULE_PROPERTIES_EX -}; -/* }}} */ +#include "php_zlib.h" -#ifdef COMPILE_DL_ZLIB -ZEND_GET_MODULE(php_zlib) -#endif +ZEND_DECLARE_MODULE_GLOBALS(zlib); /* {{{ Memory management wrappers */ @@ -231,196 +49,428 @@ static void php_zlib_free(voidpf opaque, } /* }}} */ -/* {{{ OnUpdate_zlib_output_compression */ -static PHP_INI_MH(OnUpdate_zlib_output_compression) +/* {{{ php_zlib_output_conflict_check() */ +int php_zlib_output_conflict_check(zval *handler_name TSRMLS_DC) { - char *ini_value; - - if (new_value == NULL) { - return FAILURE; - } - - if (!strncasecmp(new_value, "off", sizeof("off"))) { - new_value = "0"; - new_value_length = sizeof("0"); - } else if (!strncasecmp(new_value, "on", sizeof("on"))) { - new_value = "1"; - new_value_length = sizeof("1"); + if (php_output_get_level(TSRMLS_C) > 0) { + PHP_OUTPUT_CONFLICT(PHP_ZLIB_OUTPUT_HANDLER_NAME, return FAILURE); + PHP_OUTPUT_CONFLICT("ob_gzhandler", return FAILURE); + PHP_OUTPUT_CONFLICT("mb_output_handler", return FAILURE); + PHP_OUTPUT_CONFLICT("URL-Rewriter", return FAILURE); } + return SUCCESS; +} +/* }}} */ - ini_value = zend_ini_string("output_handler", sizeof("output_handler"), 0); - if (ini_value != NULL && strlen(ini_value) != 0 && zend_atoi(new_value, new_value_length) != 0) { - php_error_docref("ref.outcontrol" TSRMLS_CC, E_CORE_ERROR, "Cannot use both zlib.output_compression and output_handler together!!"); - return FAILURE; +/* {{{ php_zlib_output_encoding() */ +int php_zlib_output_encoding(TSRMLS_D) +{ + zval **enc; + + if (!ZLIBG(compression_coding)) { + zend_is_auto_global(ZEND_STRL("_SERVER") TSRMLS_CC); + + if (PG(http_globals)[TRACK_VARS_SERVER] && SUCCESS == zend_hash_find(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]), "HTTP_ACCEPT_ENCODING", sizeof("HTTP_ACCEPT_ENCODING"), (void *) &enc)) { + convert_to_string(*enc); + if (strstr(Z_STRVAL_PP(enc), "gzip")) { + ZLIBG(compression_coding) = PHP_ZLIB_ENCODING_GZIP; + } else if (strstr(Z_STRVAL_PP(enc), "deflate")) { + ZLIBG(compression_coding) = PHP_ZLIB_ENCODING_DEFLATE; + } + } } + + return ZLIBG(compression_coding); +} +/* }}} */ - if (stage == PHP_INI_STAGE_RUNTIME && SG(headers_sent) && !SG(request_info).no_headers) { - php_error_docref("ref.outcontrol" TSRMLS_CC, E_WARNING, "Cannot change zlib.output_compression - headers already sent"); - return FAILURE; +/* {{{ php_zlib_output_compression_start() */ +void php_zlib_output_compression_start(TSRMLS_D) +{ + zval *zoh, *tmp; + php_output_handler *h; + + switch (ZLIBG(output_compression)) { + case 0: + break; + case 1: + ZLIBG(output_compression) = PHP_OUTPUT_HANDLER_DEFAULT_SIZE; + default: + MAKE_STD_ZVAL(tmp); + ZVAL_STRING(tmp, PHP_ZLIB_OUTPUT_HANDLER_NAME, 1); + if ( (h = php_zlib_output_handler_init(tmp, ZLIBG(output_compression), PHP_OUTPUT_HANDLER_STDFLAGS TSRMLS_CC)) && + (SUCCESS == php_output_handler_start(h TSRMLS_CC))) { + if (ZLIBG(output_handler) && *ZLIBG(output_handler)) { + MAKE_STD_ZVAL(zoh); + ZVAL_STRING(zoh, ZLIBG(output_handler), 1); + php_output_start_user(zoh, ZLIBG(output_compression), PHP_OUTPUT_HANDLER_STDFLAGS TSRMLS_CC); + zval_ptr_dtor(&zoh); + } + } + zval_ptr_dtor(&tmp); + break; } - - OnUpdateLong(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC); - - return SUCCESS; } /* }}} */ -/* {{{ OnUpdate_zlib_output_compression_level */ -static PHP_INI_MH(OnUpdate_zlib_output_compression_level) +/* {{{ php_zlib_output_handler_init() */ +php_output_handler *php_zlib_output_handler_init(zval *handler_name, size_t chunk_size, int flags TSRMLS_DC) { - OnUpdateLong(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC); - - return SUCCESS; + php_output_handler *h = NULL; + php_zlib_context *ctx; + + if (!ZLIBG(output_compression)) { + ZLIBG(output_compression) = chunk_size ? chunk_size : PHP_OUTPUT_HANDLER_DEFAULT_SIZE; + } + if ((h = php_output_handler_create_internal(handler_name, php_zlib_output_handler, chunk_size, flags TSRMLS_CC))) { + ctx = (php_zlib_context *) ecalloc(1, sizeof(php_zlib_context)); + ctx->Z.zalloc = php_zlib_alloc; + ctx->Z.zfree = php_zlib_free; + php_output_handler_set_context(h, ctx, php_zlib_output_handler_dtor TSRMLS_CC); + } + + return h; } /* }}} */ -/* {{{ OnUpdate_zlib_output_handler */ -static PHP_INI_MH(OnUpdate_zlib_output_handler) +/* {{{ php_zlib_output_handler() */ +int php_zlib_output_handler(void **handler_context, php_output_context *output_context) { - if (stage == PHP_INI_STAGE_RUNTIME && SG(headers_sent) && !SG(request_info).no_headers) { - php_error_docref("ref.outcontrol" TSRMLS_CC, E_WARNING, "Cannot change zlib.output_handler - headers already sent"); + php_zlib_context *ctx = *(php_zlib_context **) handler_context; + int flags = Z_SYNC_FLUSH; + PHP_OUTPUT_TSRMLS(output_context); + + if (!php_zlib_output_encoding(TSRMLS_C)) { + /* "Vary: Accept-Encoding" header sent along uncompressed content breaks caching in MSIE, + so let's just send it with successfully compressed content or unless the complete + buffer gets discarded, see http://bugs.php.net/40325; + + Test as follows: + +Vary: $ HTTP_ACCEPT_ENCODING=gzip ./sapi/cgi/php <<<'op != (PHP_OUTPUT_HANDLER_START|PHP_OUTPUT_HANDLER_CLEAN|PHP_OUTPUT_HANDLER_FINAL)) { + sapi_add_header_ex(ZEND_STRL("Vary: Accept-Encoding"), 1, 1 TSRMLS_CC); + } return FAILURE; } - - OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC); - + + if (output_context->op & PHP_OUTPUT_HANDLER_START) { + /* start up */ + if (Z_OK != deflateInit2(&ctx->Z, ZLIBG(output_compression_level), Z_DEFLATED, ZLIBG(compression_coding), MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY)) { + return FAILURE; + } + } + + if (output_context->op & PHP_OUTPUT_HANDLER_CLEAN) { + /* free buffers */ + deflateEnd(&ctx->Z); + + if (output_context->op & PHP_OUTPUT_HANDLER_FINAL) { + /* discard */ + return SUCCESS; + } else { + /* restart */ + if (Z_OK != deflateInit2(&ctx->Z, ZLIBG(output_compression_level), Z_DEFLATED, ZLIBG(compression_coding), MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY)) { + return FAILURE; + } + ctx->buffer.used = 0; + } + } else { + + if (output_context->in.used) { + /* append input */ + if (ctx->buffer.free < output_context->in.used) { + if (!(ctx->buffer.aptr = erealloc_recoverable(ctx->buffer.data, ctx->buffer.used + ctx->buffer.free + output_context->in.used))) { + deflateEnd(&ctx->Z); + return FAILURE; + } + ctx->buffer.data = ctx->buffer.aptr; + ctx->buffer.free += output_context->in.used; + } + memcpy(ctx->buffer.data + ctx->buffer.used, output_context->in.data, output_context->in.used); + ctx->buffer.free -= output_context->in.used; + ctx->buffer.used += output_context->in.used; + } + + output_context->out.size = PHP_ZLIB_BUFFER_SIZE_GUESS(output_context->in.used); + output_context->out.data = emalloc(output_context->out.size); + output_context->out.free = 1; + output_context->out.used = 0; + + ctx->Z.avail_in = ctx->buffer.used; + ctx->Z.next_in = (Bytef *) ctx->buffer.data; + ctx->Z.avail_out = output_context->out.size; + ctx->Z.next_out = (Bytef *) output_context->out.data; + + if (output_context->op & PHP_OUTPUT_HANDLER_FINAL) { + flags = Z_FINISH; + } else if (output_context->op & PHP_OUTPUT_HANDLER_FLUSH) { + flags = Z_FULL_FLUSH; + } + + switch (deflate(&ctx->Z, flags)) { + case Z_OK: + if (flags == Z_FINISH) { + deflateEnd(&ctx->Z); + return FAILURE; + } + case Z_STREAM_END: + if (ctx->Z.avail_in) { + memmove(ctx->buffer.data, ctx->buffer.data + ctx->buffer.used - ctx->Z.avail_in, ctx->Z.avail_in); + } + ctx->buffer.free += ctx->buffer.used - ctx->Z.avail_in; + ctx->buffer.used = ctx->Z.avail_in; + output_context->out.used = output_context->out.size - ctx->Z.avail_out; + break; + default: + deflateEnd(&ctx->Z); + return FAILURE; + } + + php_output_handler_hook(PHP_OUTPUT_HANDLER_HOOK_GET_FLAGS, &flags TSRMLS_CC); + + if (!(flags & PHP_OUTPUT_HANDLER_STARTED)) { + if (SG(headers_sent) || !ZLIBG(output_compression)) { + deflateEnd(&ctx->Z); + return FAILURE; + } + switch (ZLIBG(compression_coding)) { + case PHP_ZLIB_ENCODING_GZIP: + sapi_add_header_ex(ZEND_STRL("Content-Encoding: gzip"), 1, 1 TSRMLS_CC); + break; + case PHP_ZLIB_ENCODING_DEFLATE: + sapi_add_header_ex(ZEND_STRL("Content-Encoding: deflate"), 1, 1 TSRMLS_CC); + break; + default: + deflateEnd(&ctx->Z); + return FAILURE; + } + sapi_add_header_ex(ZEND_STRL("Vary: Accept-Encoding"), 1, 1 TSRMLS_CC); + php_output_handler_hook(PHP_OUTPUT_HANDLER_HOOK_IMMUTABLE, NULL TSRMLS_CC); + } + + if (output_context->op & PHP_OUTPUT_HANDLER_FINAL) { + deflateEnd(&ctx->Z); + } + } return SUCCESS; } /* }}} */ - -PHP_INI_BEGIN() - STD_PHP_INI_BOOLEAN("zlib.output_compression", "0", PHP_INI_ALL, OnUpdate_zlib_output_compression, output_compression, zend_zlib_globals, zlib_globals) - STD_PHP_INI_ENTRY("zlib.output_compression_level", "-1", PHP_INI_ALL, OnUpdate_zlib_output_compression_level, output_compression_level, zend_zlib_globals, zlib_globals) - STD_PHP_INI_ENTRY("zlib.output_handler", "", PHP_INI_ALL, OnUpdate_zlib_output_handler, output_handler, zend_zlib_globals, zlib_globals) -PHP_INI_END() - -/* {{{ PHP_MINIT_FUNCTION - */ -static PHP_MINIT_FUNCTION(zlib) +/* {{{ php_zlib_output_handler_dtor() */ +void php_zlib_output_handler_dtor(void *opaq TSRMLS_DC) { - php_register_url_stream_wrapper("compress.zlib", &php_stream_gzip_wrapper TSRMLS_CC); - php_stream_filter_register_factory("zlib.*", &php_zlib_filter_factory TSRMLS_CC); - - REGISTER_LONG_CONSTANT("FORCE_GZIP", CODING_GZIP, CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("FORCE_DEFLATE", CODING_DEFLATE, CONST_CS | CONST_PERSISTENT); - - REGISTER_INI_ENTRIES(); - - return SUCCESS; + php_zlib_context *ctx = (php_zlib_context *) opaq; + + if (ctx) { + if (ctx->buffer.data) { + efree(ctx->buffer.data); + } + efree(ctx); + } } /* }}} */ -/* {{{ PHP_RINIT_FUNCTION - */ -static PHP_RINIT_FUNCTION(zlib) +/* {{{ php_zlib_encode() */ +int php_zlib_encode(const char *in_buf, size_t in_len, char **out_buf, size_t *out_len, int encoding, int level TSRMLS_DC) { - uint chunk_size = ZLIBG(output_compression); + int status; + z_stream Z; + + memset(&Z, 0, sizeof(z_stream)); + Z.zalloc = php_zlib_alloc; + Z.zfree = php_zlib_free; + + if (Z_OK == (status = deflateInit2(&Z, level, Z_DEFLATED, encoding, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY))) { + *out_len = PHP_ZLIB_BUFFER_SIZE_GUESS(in_len); + *out_buf = emalloc(*out_len); + + Z.next_in = (Bytef *) in_buf; + Z.next_out = (Bytef *) *out_buf; + Z.avail_in = in_len; + Z.avail_out = *out_len; + + status = deflate(&Z, Z_FINISH); + deflateEnd(&Z); + + if (Z_STREAM_END == status) { + /* size buffer down to actual length */ + *out_buf = erealloc(*out_buf, Z.total_out + 1); + (*out_buf)[*out_len = Z.total_out] = '\0'; + return SUCCESS; + } else { + efree(*out_buf); + } + } + + *out_buf = NULL; + *out_len = 0; + + php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", zError(status)); + return FAILURE; +} +/* }}} */ - ZLIBG(ob_gzhandler_status) = 0; - ZLIBG(compression_coding) = 0; - if (chunk_size) { - if (chunk_size == 1) { - chunk_size = 4096; /* use the default size */ - ZLIBG(output_compression) = chunk_size; +/* {{{ inflate_rounds() */ +static inline int inflate_rounds(z_stream *Z, size_t max, char **buf, size_t *len) +{ + int status, round = 0; + php_zlib_buffer buffer = {NULL, NULL, 0, 0, 0}; + + *buf = NULL; + *len = 0; + + buffer.size = Z->avail_in; + + do { + if ((max && (max < buffer.used)) || !(buffer.aptr = erealloc_recoverable(buffer.data, buffer.size))) { + status = Z_MEM_ERROR; + } else { + buffer.data = buffer.aptr; + Z->avail_out = buffer.free = buffer.size - buffer.used; + Z->next_out = (Bytef *) buffer.data + buffer.used; +#if 0 + fprintf(stderr, "\n%3d: %3d PRIOR: size=%7lu,\tfree=%7lu,\tused=%7lu,\tavail_in=%7lu,\tavail_out=%7lu\n", round, status, buffer.size, buffer.free, buffer.used, Z->avail_in, Z->avail_out); +#endif + status = inflate(Z, Z_NO_FLUSH); + + buffer.used += buffer.free - Z->avail_out; + buffer.free = Z->avail_out; +#if 0 + fprintf(stderr, "%3d: %3d AFTER: size=%7lu,\tfree=%7lu,\tused=%7lu,\tavail_in=%7lu,\tavail_out=%7lu\n", round, status, buffer.size, buffer.free, buffer.used, Z->avail_in, Z->avail_out); +#endif + buffer.size += (buffer.size >> 3) + 1; } - php_enable_output_compression(chunk_size TSRMLS_CC); + } while ((Z_BUF_ERROR == status || (Z_OK == status && Z->avail_in)) && ++round < 100); + + if (status == Z_OK || status == Z_STREAM_END) { + buffer.data = erealloc(buffer.data, buffer.used + 1); + buffer.data[buffer.used] = '\0'; + *buf = buffer.data; + *len = buffer.used; + } else if (buffer.data) { + efree(buffer.data); } - return SUCCESS; + + return status; } /* }}} */ -/* {{{ PHP_MSHUTDOWN_FUNCTION - */ -static PHP_MSHUTDOWN_FUNCTION(zlib) +/* {{{ php_zlib_decode() */ +int php_zlib_decode(const char *in_buf, size_t in_len, char **out_buf, size_t *out_len, int encoding, size_t max_len TSRMLS_DC) { - php_unregister_url_stream_wrapper("zlib" TSRMLS_CC); - php_stream_filter_unregister_factory("zlib.*" TSRMLS_CC); + int status = Z_DATA_ERROR; + z_stream Z; - UNREGISTER_INI_ENTRIES(); + memset(&Z, 0, sizeof(z_stream)); + Z.zalloc = php_zlib_alloc; + Z.zfree = php_zlib_free; - return SUCCESS; + if (in_len) { +retry_raw_inflate: + status = inflateInit2(&Z, encoding); + if (Z_OK == status) { + Z.next_in = (Bytef *) in_buf; + Z.avail_in = in_len; + + switch (status = inflate_rounds(&Z, max_len, out_buf, out_len)) { + case Z_OK: + case Z_STREAM_END: + inflateEnd(&Z); + return SUCCESS; + + case Z_DATA_ERROR: + /* raw deflated data? */ + if (PHP_ZLIB_ENCODING_ANY == encoding) { + inflateEnd(&Z); + encoding = PHP_ZLIB_ENCODING_RAW; + goto retry_raw_inflate; + } + } + inflateEnd(&Z); + } + } + + *out_buf = NULL; + *out_len = 0; + + php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", zError(status)); + return FAILURE; } /* }}} */ -/* {{{ PHP_MINFO_FUNCTION - */ -static PHP_MINFO_FUNCTION(zlib) +/* {{{ proto string zlib_get_coding_type(void) U + Returns the coding type used for output compression */ +PHP_FUNCTION(zlib_get_coding_type) { - php_info_print_table_start(); - php_info_print_table_row(2, "ZLib Support", "enabled"); - php_info_print_table_row(2, "Stream Wrapper support", "compress.zlib://"); - php_info_print_table_row(2, "Stream Filter support", "zlib.inflate, zlib.deflate"); - php_info_print_table_row(2, "Compiled Version", ZLIB_VERSION); - php_info_print_table_row(2, "Linked Version", (char *) zlibVersion()); - php_info_print_table_end(); - - DISPLAY_INI_ENTRIES(); + if (zend_parse_parameters_none() == FAILURE) { + return; + } + switch (ZLIBG(compression_coding)) { + case PHP_ZLIB_ENCODING_GZIP: + RETURN_STRINGL("gzip", sizeof("gzip") - 1, 1); + case PHP_ZLIB_ENCODING_DEFLATE: + RETURN_STRINGL("deflate", sizeof("deflate") - 1, 1); + default: + RETURN_FALSE; + } } /* }}} */ -/* {{{ proto array gzfile(string filename [, int use_include_path]) - Read und uncompress entire .gz-file into an array */ -static PHP_FUNCTION(gzfile) +/* {{{ proto array gzfile(string filename [, int use_include_path]) U + Read and uncompress entire .gz-file into an array */ +PHP_FUNCTION(gzfile) { char *filename; int filename_len; - long flags = 0; - char *slashed, buf[8192]; - register int i = 0; - int use_include_path = 0; + int flags = REPORT_ERRORS; + char buf[8192] = {0}; + long use_include_path = 0; php_stream *stream; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &filename, &filename_len, &flags) == FAILURE) { + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &filename, &filename_len, &use_include_path)) { return; } - use_include_path = flags ? USE_PATH : 0; - + if (use_include_path) { + flags |= USE_PATH; + } /* using a stream here is a bit more efficient (resource wise) than php_gzopen_wrapper */ - stream = php_stream_gzopen(NULL, filename, "rb", use_include_path | ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL, NULL STREAMS_CC TSRMLS_CC); - if (stream == NULL) { + stream = php_stream_gzopen(NULL, filename, "rb", flags, NULL, NULL STREAMS_CC TSRMLS_CC); + if (!stream) { /* Error reporting is already done by stream code */ RETURN_FALSE; } - /* Initialize return array */ array_init(return_value); - - /* Now loop through the file and do the magic quotes thing if needed */ - memset(buf,0,sizeof(buf)); - - while (php_stream_gets(stream, buf, sizeof(buf) - 1) != NULL) { - if (PG(magic_quotes_runtime)) { - int len; - - slashed = php_addslashes(buf, 0, &len, 0 TSRMLS_CC); /* 0 = don't free source string */ - add_index_stringl(return_value, i++, slashed, len, 0); - } else { - add_index_string(return_value, i++, buf, 1); - } + while (php_stream_gets(stream, buf, sizeof(buf) - 1)) { + add_next_index_string(return_value, buf, 1); } php_stream_close(stream); } /* }}} */ -/* {{{ proto resource gzopen(string filename, string mode [, int use_include_path]) +/* {{{ proto resource gzopen(string filename, string mode [, int use_include_path]) U Open a .gz-file and return a .gz-file pointer */ -static PHP_FUNCTION(gzopen) +PHP_FUNCTION(gzopen) { char *filename, *mode; int filename_len, mode_len; - long flags = 0; + int flags = REPORT_ERRORS; php_stream *stream; - int use_include_path = 0; + long use_include_path = 0; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|l", &filename, &filename_len, &mode, &mode_len, &flags) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|l", &filename, &filename_len, &mode, &mode_len, &use_include_path) == FAILURE) { return; } - use_include_path = flags ? USE_PATH : 0; - - stream = php_stream_gzopen(NULL, filename, mode, use_include_path | ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL, NULL STREAMS_CC TSRMLS_CC); + if (use_include_path) { + flags |= USE_PATH; + } + stream = php_stream_gzopen(NULL, filename, mode, flags, NULL, NULL STREAMS_CC TSRMLS_CC); if (!stream) { RETURN_FALSE; @@ -429,666 +479,390 @@ static PHP_FUNCTION(gzopen) } /* }}} */ -/* - * Read a file and write the ouput to stdout - */ -/* {{{ proto int readgzfile(string filename [, int use_include_path]) +/* {{{ proto int readgzfile(string filename [, int use_include_path]) U Output a .gz-file */ -static PHP_FUNCTION(readgzfile) +PHP_FUNCTION(readgzfile) { char *filename; int filename_len; - long flags = 0; + int flags = REPORT_ERRORS; php_stream *stream; int size; - int use_include_path = 0; + long use_include_path = 0; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &filename, &filename_len, &flags) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "t|l", &filename, &filename_len, &use_include_path) == FAILURE) { return; } - use_include_path = flags ? USE_PATH : 0; - - stream = php_stream_gzopen(NULL, filename, "rb", use_include_path | ENFORCE_SAFE_MODE, NULL, NULL STREAMS_CC TSRMLS_CC); + if (use_include_path) { + flags |= USE_PATH; + } + stream = php_stream_gzopen(NULL, filename, "rb", flags, NULL, NULL STREAMS_CC TSRMLS_CC); if (!stream) { RETURN_FALSE; } size = php_stream_passthru(stream); - php_stream_close(stream); + php_stream_close(stream); RETURN_LONG(size); } /* }}} */ -/* {{{ proto string gzcompress(string data [, int level]) - Gzip-compress a string */ -static PHP_FUNCTION(gzcompress) -{ - int data_len, status; - long level = Z_DEFAULT_COMPRESSION; - unsigned long l2; - char *data, *s2; - - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &data, &data_len, &level) == FAILURE) { - return; - } - - if ((level < -1) || (level > 9)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "compression level (%ld) must be within -1..9", level); - RETURN_FALSE; - } - - l2 = data_len + (data_len / PHP_ZLIB_MODIFIER) + 15 + 1; /* room for \0 */ - s2 = (char *) emalloc(l2); - if (!s2) { - RETURN_FALSE; - } - - if (level >= 0) { - status = compress2(s2, &l2, data, data_len, level); - } else { - status = compress(s2, &l2, data, data_len); - } - - if (status == Z_OK) { - s2 = erealloc(s2, l2 + 1); - s2[l2] = '\0'; - RETURN_STRINGL(s2, l2, 0); - } else { - efree(s2); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", zError(status)); - RETURN_FALSE; - } +#define PHP_ZLIB_ENCODE_FUNC(name, default_encoding) \ +PHP_FUNCTION(name) \ +{ \ + char *in_buf, *out_buf; \ + int in_len; \ + size_t out_len; \ + long level = -1; \ + long encoding = default_encoding; \ + if (default_encoding) { \ + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S|ll", &in_buf, &in_len, &level, &encoding)) { \ + return; \ + } \ + } else { \ + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Sl|l", &in_buf, &in_len, &encoding, &level)) { \ + return; \ + } \ + } \ + if (level < -1 || level > 9) { \ + php_error_docref(NULL TSRMLS_CC, E_WARNING, "compression level (%ld) must be within -1..9", level); \ + RETURN_FALSE; \ + } \ + switch (encoding) { \ + case PHP_ZLIB_ENCODING_RAW: \ + case PHP_ZLIB_ENCODING_GZIP: \ + case PHP_ZLIB_ENCODING_DEFLATE: \ + break; \ + default: \ + php_error_docref(NULL TSRMLS_CC, E_WARNING, "encoding mode must be either ZLIB_ENCODING_RAW, ZLIB_ENCODING_GZIP or ZLIB_ENCODING_DEFLATE"); \ + RETURN_FALSE; \ + } \ + if (SUCCESS != php_zlib_encode(in_buf, in_len, &out_buf, &out_len, encoding, level TSRMLS_CC)) { \ + RETURN_FALSE; \ + } \ + RETURN_STRINGL(out_buf, out_len, 0); \ } -/* }}} */ - -/* {{{ proto string gzuncompress(string data [, int length]) - Unzip a gzip-compressed string */ -static PHP_FUNCTION(gzuncompress) -{ - int data_len, status; - unsigned int factor=1, maxfactor=16; - long limit = 0; - unsigned long plength=0, length; - char *data, *s1=NULL, *s2=NULL; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &data, &data_len, &limit) == FAILURE) { - return; - } - - if (limit < 0) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "length (%ld) must be greater or equal zero", limit); - RETURN_FALSE; - } - plength = limit; - - /* - zlib::uncompress() wants to know the output data length - if none was given as a parameter - we try from input length * 2 up to input length * 2^15 - doubling it whenever it wasn't big enough - that should be eneugh for all real life cases - */ - do { - length = plength ? plength : (unsigned long)data_len * (1 << factor++); - s2 = (char *) erealloc(s1, length); - status = uncompress(s2, &length, data, data_len); - s1 = s2; - } while ((status == Z_BUF_ERROR) && (!plength) && (factor < maxfactor)); - - if (status == Z_OK) { - s2 = erealloc(s2, length + 1); /* space for \0 */ - s2[ length ] = '\0'; - RETURN_STRINGL(s2, length, 0); - } else { - efree(s2); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", zError(status)); - RETURN_FALSE; - } +#define PHP_ZLIB_DECODE_FUNC(name, encoding) \ +PHP_FUNCTION(name) \ +{ \ + char *in_buf, *out_buf; \ + int in_len; \ + size_t out_len; \ + long max_len = 0; \ + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S|l", &in_buf, &in_len, &max_len)) { \ + return; \ + } \ + if (max_len < 0) { \ + php_error_docref(NULL TSRMLS_CC, E_WARNING, "length (%ld) must be greater or equal zero", max_len); \ + RETURN_FALSE; \ + } \ + if (SUCCESS != php_zlib_decode(in_buf, in_len, &out_buf, &out_len, encoding, max_len TSRMLS_CC)) { \ + RETURN_FALSE; \ + } \ + RETURN_STRINGL(out_buf, out_len, 0); \ } -/* }}} */ - -/* {{{ proto string gzdeflate(string data [, int level]) - Gzip-compress a string */ -static PHP_FUNCTION(gzdeflate) -{ - int data_len,status; - long level = Z_DEFAULT_COMPRESSION; - z_stream stream; - char *data, *s2; - - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &data, &data_len, &level) == FAILURE) { - return; - } - - if ((level < -1) || (level > 9)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "compression level (%ld) must be within -1..9", level); - RETURN_FALSE; - } - - stream.data_type = Z_ASCII; - stream.zalloc = php_zlib_alloc; - stream.zfree = php_zlib_free; - stream.opaque = (voidpf) Z_NULL; - - stream.next_in = (Bytef *) data; - stream.avail_in = data_len; - stream.avail_out = stream.avail_in + (stream.avail_in / PHP_ZLIB_MODIFIER) + 15 + 1; /* room for \0 */ +/* {{{ proto binary zlib_encode(binary data, int encoding[, int level = -1]) U + Compress data with the specified encoding */ +PHP_ZLIB_ENCODE_FUNC(zlib_encode, 0); +/* }}} */ - s2 = (char *) emalloc(stream.avail_out); - if (!s2) { - RETURN_FALSE; - } - - stream.next_out = s2; +/* {{{ proto binary zlib_decode(binary data[, int max_decoded_len]) U + Uncompress any raw/gzip/zlib encoded data */ +PHP_ZLIB_DECODE_FUNC(zlib_decode, PHP_ZLIB_ENCODING_ANY); +/* }}} */ - /* init with -MAX_WBITS disables the zlib internal headers */ - status = deflateInit2(&stream, level, Z_DEFLATED, -MAX_WBITS, MAX_MEM_LEVEL, 0); - if (status == Z_OK) { - status = deflate(&stream, Z_FINISH); - if (status != Z_STREAM_END) { - deflateEnd(&stream); - if (status == Z_OK) { - status = Z_BUF_ERROR; - } - } else { - status = deflateEnd(&stream); - } - } +/* NOTE: The naming of these userland functions was quite unlucky */ - if (status == Z_OK) { - s2 = erealloc(s2,stream.total_out + 1); /* resize to buffer to the "right" size */ - s2[ stream.total_out ] = '\0'; - RETURN_STRINGL(s2, stream.total_out, 0); - } else { - efree(s2); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", zError(status)); - RETURN_FALSE; - } -} +/* {{{ proto binary gzdeflate(binary data[, int level = -1[, int encoding = ZLIB_ENCODING_RAW]) U + Encode data with the raw deflate encoding */ +PHP_ZLIB_ENCODE_FUNC(gzdeflate, PHP_ZLIB_ENCODING_RAW); +/* }}} */ +/* {{{ proto binary gzencode(binary data[, int level = -1[, int encoding = ZLIB_ENCODING_GZIP]) U + Encode data with the gzip encoding */ +PHP_ZLIB_ENCODE_FUNC(gzencode, PHP_ZLIB_ENCODING_GZIP); +/* }}} */ +/* {{{ proto binary gzcompress(binary data[, int level = -1[, int encoding = ZLIB_ENCODING_DEFLATE]) U + Encode data with the zlib encoding */ +PHP_ZLIB_ENCODE_FUNC(gzcompress, PHP_ZLIB_ENCODING_DEFLATE); +/* }}} */ +/* {{{ proto binary gzinflate(binary data[, int max_decoded_len]) U + Decode raw deflate encoded data */ +PHP_ZLIB_DECODE_FUNC(gzinflate, PHP_ZLIB_ENCODING_RAW); +/* }}} */ +/* {{{ proto binary gzdecode(binary data[, int max_decoded_len]) U + Decode gzip encoded data */ +PHP_ZLIB_DECODE_FUNC(gzdecode, PHP_ZLIB_ENCODING_GZIP); +/* }}} */ +/* {{{ proto binary gzuncompress(binary data[, int max_decoded_len]) U + Decode zlib encoded data */ +PHP_ZLIB_DECODE_FUNC(gzuncompress, PHP_ZLIB_ENCODING_DEFLATE); /* }}} */ -/* {{{ proto string gzinflate(string data [, int length]) - Unzip a gzip-compressed string */ -static PHP_FUNCTION(gzinflate) -{ - int data_len, status; - unsigned int factor=1, maxfactor=16; - long limit = 0; - unsigned long plength=0, length; - char *data, *s1=NULL, *s2=NULL; - z_stream stream; +#ifdef COMPILE_DL_ZLIB +ZEND_GET_MODULE(php_zlib) +#endif - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &data, &data_len, &limit) == FAILURE) { - return; - } +/* {{{ arginfo */ +static +ZEND_BEGIN_ARG_INFO(arginfo_zlib_get_coding_type, 0) +ZEND_END_ARG_INFO() - if (!data_len) { - RETURN_FALSE; - } +static +ZEND_BEGIN_ARG_INFO_EX(arginfo_gzfile, 0, 0, 1) + ZEND_ARG_INFO(0, filename) + ZEND_ARG_INFO(0, use_include_path) +ZEND_END_ARG_INFO() - if (limit < 0) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "length (%ld) must be greater or equal zero", limit); - RETURN_FALSE; - } - plength = limit; +static +ZEND_BEGIN_ARG_INFO_EX(arginfo_gzopen, 0, 0, 2) + ZEND_ARG_INFO(0, filename) + ZEND_ARG_INFO(0, mode) + ZEND_ARG_INFO(0, use_include_path) +ZEND_END_ARG_INFO() - stream.zalloc = php_zlib_alloc; - stream.zfree = php_zlib_free; - stream.opaque = Z_NULL; - stream.avail_in = data_len + 1; /* there is room for \0 */ - stream.next_in = (Bytef *) data; - stream.total_out = 0; +static +ZEND_BEGIN_ARG_INFO_EX(arginfo_readgzfile, 0, 0, 1) + ZEND_ARG_INFO(0, filename) + ZEND_ARG_INFO(0, use_include_path) +ZEND_END_ARG_INFO() - /* init with -MAX_WBITS disables the zlib internal headers */ - status = inflateInit2(&stream, -MAX_WBITS); - if (status != Z_OK) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", zError(status)); - RETURN_FALSE; - } +static +ZEND_BEGIN_ARG_INFO_EX(arginfo_zlib_encode, 0, 0, 2) + ZEND_ARG_INFO(0, data) + ZEND_ARG_INFO(0, encoding) + ZEND_ARG_INFO(0, level) +ZEND_END_ARG_INFO() - /* - stream.avail_out wants to know the output data length - if none was given as a parameter - we try from input length * 2 up to input length * 2^15 - doubling it whenever it wasn't big enough - that should be enaugh for all real life cases - */ - do { - length = plength ? plength : (unsigned long)data_len * (1 << factor++); - s2 = (char *) erealloc(s1, length); +static +ZEND_BEGIN_ARG_INFO_EX(arginfo_zlib_decode, 0, 0, 1) + ZEND_ARG_INFO(0, data) + ZEND_ARG_INFO(0, max_decoded_len) +ZEND_END_ARG_INFO() - if (!s2) { - if (s1) { - efree(s1); - } - inflateEnd(&stream); - RETURN_FALSE; - } - s1 = s2; +static +ZEND_BEGIN_ARG_INFO_EX(arginfo_gzdeflate, 0, 0, 1) + ZEND_ARG_INFO(0, data) + ZEND_ARG_INFO(0, level) + ZEND_ARG_INFO(0, encoding) +ZEND_END_ARG_INFO() - stream.next_out = (Bytef *) &s2[stream.total_out]; - stream.avail_out = length - stream.total_out; - status = inflate(&stream, Z_NO_FLUSH); +static +ZEND_BEGIN_ARG_INFO_EX(arginfo_gzencode, 0, 0, 1) + ZEND_ARG_INFO(0, data) + ZEND_ARG_INFO(0, level) + ZEND_ARG_INFO(0, encoding) +ZEND_END_ARG_INFO() - } while ((Z_BUF_ERROR == status || (Z_OK == status && stream.avail_in)) && !plength && factor < maxfactor); +static +ZEND_BEGIN_ARG_INFO_EX(arginfo_gzcompress, 0, 0, 1) + ZEND_ARG_INFO(0, data) + ZEND_ARG_INFO(0, level) + ZEND_ARG_INFO(0, encoding) +ZEND_END_ARG_INFO() - inflateEnd(&stream); +static +ZEND_BEGIN_ARG_INFO_EX(arginfo_gzinflate, 0, 0, 1) + ZEND_ARG_INFO(0, data) + ZEND_ARG_INFO(0, max_decoded_len) +ZEND_END_ARG_INFO() - if ((plength && Z_OK == status) || factor >= maxfactor) { - status = Z_MEM_ERROR; - } +static +ZEND_BEGIN_ARG_INFO_EX(arginfo_gzdecode, 0, 0, 1) + ZEND_ARG_INFO(0, data) + ZEND_ARG_INFO(0, max_decoded_len) +ZEND_END_ARG_INFO() - if (Z_STREAM_END == status || Z_OK == status) { - s2 = erealloc(s2, stream.total_out + 1); /* room for \0 */ - s2[ stream.total_out ] = '\0'; - RETURN_STRINGL(s2, stream.total_out, 0); - } else { - efree(s2); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", zError(status)); - RETURN_FALSE; - } -} +static +ZEND_BEGIN_ARG_INFO_EX(arginfo_gzuncompress, 0, 0, 1) + ZEND_ARG_INFO(0, data) + ZEND_ARG_INFO(0, max_decoded_len) +ZEND_END_ARG_INFO() /* }}} */ -/* {{{ proto string zlib_get_coding_type(void) - Returns the coding type used for output compression */ -static PHP_FUNCTION(zlib_get_coding_type) -{ - switch (ZLIBG(compression_coding)) { - case CODING_GZIP: - RETURN_STRINGL("gzip", sizeof("gzip") - 1, 1); - - case CODING_DEFLATE: - RETURN_STRINGL("deflate", sizeof("deflate") - 1, 1); - } +/* {{{ php_zlib_functions[] */ +const zend_function_entry php_zlib_functions[] = { + PHP_FE(readgzfile, arginfo_readgzfile) + PHP_FALIAS(gzrewind, rewind, NULL) + PHP_FALIAS(gzclose, fclose, NULL) + PHP_FALIAS(gzeof, feof, NULL) + PHP_FALIAS(gzgetc, fgetc, NULL) + PHP_FALIAS(gzgets, fgets, NULL) + PHP_FALIAS(gzgetss, fgetss, NULL) + PHP_FALIAS(gzread, fread, NULL) + PHP_FE(gzopen, arginfo_gzopen) + PHP_FALIAS(gzpassthru, fpassthru, NULL) + PHP_FALIAS(gzseek, fseek, NULL) + PHP_FALIAS(gztell, ftell, NULL) + PHP_FALIAS(gzwrite, fwrite, NULL) + PHP_FALIAS(gzputs, fwrite, NULL) + PHP_FE(gzfile, NULL) + PHP_FE(gzcompress, arginfo_gzcompress) + PHP_FE(gzuncompress, arginfo_gzuncompress) + PHP_FE(gzdeflate, arginfo_gzdeflate) + PHP_FE(gzinflate, arginfo_gzinflate) + PHP_FE(gzencode, arginfo_gzencode) + PHP_FE(gzdecode, arginfo_gzdecode) + PHP_FE(zlib_encode, arginfo_zlib_encode) + PHP_FE(zlib_decode, arginfo_zlib_decode) + PHP_FE(zlib_get_coding_type, arginfo_zlib_get_coding_type) + {NULL, NULL, NULL} +}; +/* }}} */ - RETURN_FALSE; -} -/* {{{ php_do_deflate - */ -static int php_do_deflate(uint str_length, Bytef **p_buffer, uint *p_buffer_len, zend_bool do_start, zend_bool do_end TSRMLS_DC) +/* {{{ OnUpdate_zlib_output_compression */ +static PHP_INI_MH(OnUpdate_zlib_output_compression) { - Bytef *buffer; - uInt prev_outlen, outlen; - int err; - int start_offset = ((do_start && ZLIBG(compression_coding) == CODING_GZIP) ? 10 : 0); - int end_offset = (do_end ? 8 : 0); - - outlen = (uint) (str_length + (str_length / PHP_ZLIB_MODIFIER) + 12 + 1); /* leave some room for a trailing \0 */ - if ((outlen + start_offset + end_offset) > *p_buffer_len) { - buffer = (Bytef *) emalloc(outlen + start_offset + end_offset); - } else { - buffer = *p_buffer; + int status, int_value; + char *ini_value; + + if (new_value == NULL) { + return FAILURE; } - ZLIBG(stream).next_out = buffer + start_offset; - ZLIBG(stream).avail_out = outlen; - - err = deflate(&ZLIBG(stream), Z_SYNC_FLUSH); - while (err == Z_OK && !ZLIBG(stream).avail_out) { - prev_outlen = outlen; - outlen *= 3; - if ((outlen + start_offset + end_offset) > *p_buffer_len) { - buffer = erealloc(buffer, outlen + start_offset + end_offset); - } - - ZLIBG(stream).next_out = buffer + start_offset + prev_outlen; - ZLIBG(stream).avail_out = prev_outlen * 2; - - err = deflate(&ZLIBG(stream), Z_SYNC_FLUSH); + if (!strncasecmp(new_value, "off", sizeof("off"))) { + new_value = "0"; + new_value_length = sizeof("0"); + } else if (!strncasecmp(new_value, "on", sizeof("on"))) { + new_value = "1"; + new_value_length = sizeof("1"); } - - if (do_end) { - err = deflate(&ZLIBG(stream), Z_FINISH); - buffer[outlen + start_offset - ZLIBG(stream).avail_out] = '\0'; + + int_value = zend_atoi(new_value, new_value_length); + ini_value = zend_ini_string("output_handler", sizeof("output_handler"), 0); + + if (ini_value && *ini_value && int_value) { + php_error_docref("ref.outcontrol" TSRMLS_CC, E_CORE_ERROR, "Cannot use both zlib.output_compression and output_handler together!!"); + return FAILURE; } - *p_buffer = buffer; - *p_buffer_len = outlen - ZLIBG(stream).avail_out; - - return err; -} -/* }}} */ - -/* {{{ php_deflate_string - */ -static int php_deflate_string(const char *str, uint str_length, char **newstr, uint *new_length, zend_bool do_start, zend_bool do_end TSRMLS_DC) -{ - int err; - - if (do_start) { - ZLIBG(stream).zalloc = php_zlib_alloc; - ZLIBG(stream).zfree = php_zlib_free; - ZLIBG(stream).opaque = Z_NULL; - - switch (ZLIBG(compression_coding)) { - case CODING_GZIP: - /* windowBits is passed < 0 to suppress zlib header & trailer */ - if (deflateInit2(&ZLIBG(stream), ZLIBG(output_compression_level), Z_DEFLATED, -MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK) { - /* TODO: print out error */ - return FAILURE; - } - - ZLIBG(crc) = crc32(0L, Z_NULL, 0); - break; - - case CODING_DEFLATE: - if (deflateInit(&ZLIBG(stream), ZLIBG(output_compression_level)) != Z_OK) { - /* TODO: print out error */ - return FAILURE; - } - break; + if (stage == PHP_INI_STAGE_RUNTIME) { + status = php_output_get_status(TSRMLS_C); + if (status & PHP_OUTPUT_SENT) { + php_error_docref("ref.outcontrol" TSRMLS_CC, E_WARNING, "Cannot change zlib.output_compression - headers already sent"); + return FAILURE; + } else if ((status & PHP_OUTPUT_WRITTEN) && int_value) { + php_error_docref("ref.outcontrol" TSRMLS_CC, E_WARNING, "Cannot enable zlib.output_compression - there has already been output"); + return FAILURE; } } - - ZLIBG(stream).next_in = (Bytef *) str; - ZLIBG(stream).avail_in = (uInt) str_length; - - if (ZLIBG(compression_coding) == CODING_GZIP) { - ZLIBG(crc) = crc32(ZLIBG(crc), (const Bytef *) str, str_length); - } - - err = php_do_deflate(str_length, (Bytef **) newstr, new_length, do_start, do_end TSRMLS_CC); - /* TODO: error handling (err may be Z_STREAM_ERROR, Z_BUF_ERROR, ?) */ - - if (do_start && ZLIBG(compression_coding) == CODING_GZIP) { - /* Write a very simple .gz header: */ - (*newstr)[0] = gz_magic[0]; - (*newstr)[1] = gz_magic[1]; - (*newstr)[2] = Z_DEFLATED; - (*newstr)[3] = (*newstr)[4] = (*newstr)[5] = (*newstr)[6] = (*newstr)[7] = (*newstr)[8] = 0; - (*newstr)[9] = OS_CODE; - *new_length += 10; - } - if (do_end) { - if (ZLIBG(compression_coding) == CODING_GZIP) { - char *trailer = (*newstr) + (*new_length); - - /* write crc & stream.total_in in LSB order */ - trailer[0] = (char) ZLIBG(crc) & 0xFF; - trailer[1] = (char) (ZLIBG(crc) >> 8) & 0xFF; - trailer[2] = (char) (ZLIBG(crc) >> 16) & 0xFF; - trailer[3] = (char) (ZLIBG(crc) >> 24) & 0xFF; - trailer[4] = (char) ZLIBG(stream).total_in & 0xFF; - trailer[5] = (char) (ZLIBG(stream).total_in >> 8) & 0xFF; - trailer[6] = (char) (ZLIBG(stream).total_in >> 16) & 0xFF; - trailer[7] = (char) (ZLIBG(stream).total_in >> 24) & 0xFF; - trailer[8] = '\0'; - *new_length += 8; + + status = OnUpdateLong(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC); + + if (stage == PHP_INI_STAGE_RUNTIME && int_value) { + zval tmp; + + INIT_PZVAL(&tmp); + ZVAL_STRING(&tmp, PHP_ZLIB_OUTPUT_HANDLER_NAME, 1); + if (!php_output_handler_started(&tmp TSRMLS_CC)) { + php_zlib_output_compression_start(TSRMLS_C); } - deflateEnd(&ZLIBG(stream)); + zval_dtor(&tmp); } - - return SUCCESS; + + return status; } /* }}} */ -/* {{{ proto string gzencode(string data [, int level [, int encoding_mode]]) - GZ encode a string */ -static PHP_FUNCTION(gzencode) +/* {{{ OnUpdate_zlib_output_handler */ +static PHP_INI_MH(OnUpdate_zlib_output_handler) { - char *data, *s2; - int data_len; - long level = Z_DEFAULT_COMPRESSION, coding = CODING_GZIP; - int status; - z_stream stream; - - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ll", &data, &data_len, &level, &coding) == FAILURE) { - return; - } - - if ((level < -1) || (level > 9)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "compression level(%ld) must be within -1..9", level); - RETURN_FALSE; - } - - if ((coding != CODING_GZIP) && (coding != CODING_DEFLATE)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "encoding mode must be FORCE_GZIP or FORCE_DEFLATE"); - RETURN_FALSE; - } - - stream.zalloc = php_zlib_alloc; - stream.zfree = php_zlib_free; - stream.opaque = Z_NULL; - - stream.next_in = (Bytef *) data; - stream.avail_in = data_len; - - stream.avail_out = stream.avail_in + (stream.avail_in / PHP_ZLIB_MODIFIER) + 15 + 1; /* room for \0 */ - s2 = (char *) emalloc(stream.avail_out + GZIP_HEADER_LENGTH + (coding == CODING_GZIP ? GZIP_FOOTER_LENGTH : 0)); - - /* add gzip file header */ - s2[0] = gz_magic[0]; - s2[1] = gz_magic[1]; - s2[2] = Z_DEFLATED; - s2[3] = s2[4] = s2[5] = s2[6] = s2[7] = s2[8] = 0; /* time set to 0 */ - s2[9] = OS_CODE; - - stream.next_out = &(s2[GZIP_HEADER_LENGTH]); - - switch (coding) { - case CODING_GZIP: - /* windowBits is passed < 0 to suppress zlib header & trailer */ - if ((status = deflateInit2(&stream, level, Z_DEFLATED, -MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY)) != Z_OK) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", zError(status)); - RETURN_FALSE; - } - - break; - case CODING_DEFLATE: - if ((status = deflateInit(&stream, level)) != Z_OK) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", zError(status)); - RETURN_FALSE; - } - break; - } - - status = deflate(&stream, Z_FINISH); - if (status != Z_STREAM_END) { - deflateEnd(&stream); - if (status == Z_OK) { - status = Z_BUF_ERROR; - } - } else { - status = deflateEnd(&stream); + if (stage == PHP_INI_STAGE_RUNTIME && (php_output_get_status(TSRMLS_C) & PHP_OUTPUT_SENT)) { + php_error_docref("ref.outcontrol" TSRMLS_CC, E_WARNING, "Cannot change zlib.output_handler - headers already sent"); + return FAILURE; } - if (status == Z_OK) { - /* resize to buffer to the "right" size */ - s2 = erealloc(s2, stream.total_out + GZIP_HEADER_LENGTH + (coding == CODING_GZIP ? GZIP_FOOTER_LENGTH : 0) + 1); - - if (coding == CODING_GZIP) { - char *trailer = s2 + (stream.total_out + GZIP_HEADER_LENGTH); - uLong crc = crc32(0L, Z_NULL, 0); - - crc = crc32(crc, (const Bytef *) data, data_len); - - /* write crc & stream.total_in in LSB order */ - trailer[0] = (char) crc & 0xFF; - trailer[1] = (char) (crc >> 8) & 0xFF; - trailer[2] = (char) (crc >> 16) & 0xFF; - trailer[3] = (char) (crc >> 24) & 0xFF; - trailer[4] = (char) stream.total_in & 0xFF; - trailer[5] = (char) (stream.total_in >> 8) & 0xFF; - trailer[6] = (char) (stream.total_in >> 16) & 0xFF; - trailer[7] = (char) (stream.total_in >> 24) & 0xFF; - trailer[8] = '\0'; - } else { - s2[stream.total_out + GZIP_HEADER_LENGTH + (coding == CODING_GZIP ? GZIP_FOOTER_LENGTH : 0)] = '\0'; - } - RETURN_STRINGL(s2, stream.total_out + GZIP_HEADER_LENGTH + (coding == CODING_GZIP ? GZIP_FOOTER_LENGTH : 0), 0); - } else { - efree(s2); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", zError(status)); - RETURN_FALSE; - } + return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC); } /* }}} */ -/* {{{ php_ob_gzhandler_check - */ -int php_ob_gzhandler_check(TSRMLS_D) -{ - /* check for wrong usages */ - if (OG(ob_nesting_level > 0)) { - if (php_ob_handler_used("ob_gzhandler" TSRMLS_CC)) { - php_error_docref("ref.outcontrol" TSRMLS_CC, E_WARNING, "output handler 'ob_gzhandler' cannot be used twice"); - return FAILURE; - } - if (php_ob_handler_used("mb_output_handler" TSRMLS_CC)) { - php_error_docref("ref.outcontrol" TSRMLS_CC, E_WARNING, "output handler 'ob_gzhandler' cannot be used after 'mb_output_handler'"); - return FAILURE; - } - if (php_ob_handler_used("URL-Rewriter" TSRMLS_CC)) { - php_error_docref("ref.outcontrol" TSRMLS_CC, E_WARNING, "output handler 'ob_gzhandler' cannot be used after 'URL-Rewriter'"); - return FAILURE; - } - if (php_ob_init_conflict("ob_gzhandler", "zlib output compression" TSRMLS_CC)) { - return FAILURE; - } - } +/* {{{ INI */ +PHP_INI_BEGIN() + STD_PHP_INI_BOOLEAN("zlib.output_compression", "0", PHP_INI_ALL, OnUpdate_zlib_output_compression, output_compression, zend_zlib_globals, zlib_globals) + STD_PHP_INI_ENTRY("zlib.output_compression_level", "-1", PHP_INI_ALL, OnUpdateLong, output_compression_level, zend_zlib_globals, zlib_globals) + STD_PHP_INI_ENTRY("zlib.output_handler", "", PHP_INI_ALL, OnUpdate_zlib_output_handler, output_handler, zend_zlib_globals, zlib_globals) +PHP_INI_END() +/* }}} */ +/* {{{ PHP_MINIT_FUNCTION */ +PHP_MINIT_FUNCTION(zlib) +{ + php_register_url_stream_wrapper("compress.zlib", &php_stream_gzip_wrapper TSRMLS_CC); + php_stream_filter_register_factory("zlib.*", &php_zlib_filter_factory TSRMLS_CC); + + PHP_OUTPUT_ALIAS_REGISTER("ob_gzhandler", php_zlib_output_handler_init); + PHP_OUTPUT_CONFLICT_REGISTER("ob_gzhandler", php_zlib_output_conflict_check); + PHP_OUTPUT_CONFLICT_REGISTER(PHP_ZLIB_OUTPUT_HANDLER_NAME, php_zlib_output_conflict_check); + + REGISTER_LONG_CONSTANT("FORCE_GZIP", PHP_ZLIB_ENCODING_GZIP, CONST_CS|CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("FORCE_DEFLATE", PHP_ZLIB_ENCODING_DEFLATE, CONST_CS|CONST_PERSISTENT); + + REGISTER_LONG_CONSTANT("ZLIB_ENCODING_RAW", PHP_ZLIB_ENCODING_RAW, CONST_CS|CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("ZLIB_ENCODING_GZIP", PHP_ZLIB_ENCODING_GZIP, CONST_CS|CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("ZLIB_ENCODING_DEFLATE", PHP_ZLIB_ENCODING_DEFLATE, CONST_CS|CONST_PERSISTENT); + REGISTER_INI_ENTRIES(); return SUCCESS; } - /* }}} */ -/* {{{ proto string ob_gzhandler(string str, int mode) - Encode str based on accept-encoding setting - designed to be called from ob_start() */ -static PHP_FUNCTION(ob_gzhandler) +/* {{{ PHP_MSHUTDOWN_FUNCTION */ +PHP_MSHUTDOWN_FUNCTION(zlib) { - char *string; - int string_len; - long mode; - zval **a_encoding; - zend_bool return_original = 0; - zend_bool do_start, do_end; - - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &string, &string_len, &mode) == FAILURE) { - return; - } - - if(ZLIBG(ob_gzhandler_status) == -1) - RETURN_FALSE; - - zend_is_auto_global("_SERVER", sizeof("_SERVER")-1 TSRMLS_CC); - - if (!PG(http_globals)[TRACK_VARS_SERVER] - || zend_hash_find(PG(http_globals)[TRACK_VARS_SERVER]->value.ht, "HTTP_ACCEPT_ENCODING", sizeof("HTTP_ACCEPT_ENCODING"), (void **) &a_encoding) == FAILURE - ) { - ZLIBG(ob_gzhandler_status) = -1; - RETURN_FALSE; - } - - convert_to_string_ex(a_encoding); - if (php_memnstr(Z_STRVAL_PP(a_encoding), "gzip", 4, Z_STRVAL_PP(a_encoding) + Z_STRLEN_PP(a_encoding))) { - ZLIBG(compression_coding) = CODING_GZIP; - } else if (php_memnstr(Z_STRVAL_PP(a_encoding), "deflate", 7, Z_STRVAL_PP(a_encoding) + Z_STRLEN_PP(a_encoding))) { - ZLIBG(compression_coding) = CODING_DEFLATE; - } else { - ZLIBG(ob_gzhandler_status) = -1; - RETURN_FALSE; - } + php_unregister_url_stream_wrapper("zlib" TSRMLS_CC); + php_stream_filter_unregister_factory("zlib.*" TSRMLS_CC); - do_start = ((mode & PHP_OUTPUT_HANDLER_START) ? 1 : 0); - do_end = ((mode & PHP_OUTPUT_HANDLER_END) ? 1 : 0); - Z_STRVAL_P(return_value) = NULL; - Z_STRLEN_P(return_value) = 0; - - if (php_deflate_string(string, string_len, &Z_STRVAL_P(return_value), &Z_STRLEN_P(return_value), do_start, do_end TSRMLS_CC) == SUCCESS) { - Z_TYPE_P(return_value) = IS_STRING; - if (do_start) { - switch (ZLIBG(compression_coding)) { - case CODING_GZIP: - if (sapi_add_header("Content-Encoding: gzip", sizeof("Content-Encoding: gzip") - 1, 1) == FAILURE) { - return_original = 1; - } - if (sapi_add_header_ex("Vary: Accept-Encoding", sizeof("Vary: Accept-Encoding") - 1, 1, 0 TSRMLS_CC)==FAILURE) { - return_original = 1; - } - break; - case CODING_DEFLATE: - if (sapi_add_header("Content-Encoding: deflate", sizeof("Content-Encoding: deflate") - 1, 1) == FAILURE) { - return_original = 1; - } - if (sapi_add_header_ex("Vary: Accept-Encoding", sizeof("Vary: Accept-Encoding") - 1, 1, 0 TSRMLS_CC)==FAILURE) { - return_original = 1; - } - break; - default: - return_original = 1; - break; - } - } - - if (return_original) { - zval_dtor(return_value); - } - - } else { - return_original = 1; - } - - if (return_original) { - /* return the original string */ - RETURN_STRINGL(string, string_len, 1); - } + UNREGISTER_INI_ENTRIES(); + return SUCCESS; } /* }}} */ -/* {{{ php_gzip_output_handler - */ -static void php_gzip_output_handler(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC) +/* {{{ PHP_RINIT_FUNCTION */ +PHP_RINIT_FUNCTION(zlib) { - zend_bool do_start, do_end; - - if (!ZLIBG(output_compression)) { - *handled_output = NULL; - } else { - do_start = (mode & PHP_OUTPUT_HANDLER_START ? 1 : 0); - do_end = (mode & PHP_OUTPUT_HANDLER_END ? 1 : 0); - if (php_deflate_string(output, output_len, handled_output, handled_output_len, do_start, do_end TSRMLS_CC) != SUCCESS) { - zend_error(E_ERROR, "Compression failed"); - } - } + ZLIBG(compression_coding) = 0; + + php_zlib_output_compression_start(TSRMLS_C); + + return SUCCESS; } /* }}} */ -/* {{{ php_enable_output_compression - */ -static int php_enable_output_compression(int buffer_size TSRMLS_DC) +/* {{{ PHP_MINFO_FUNCTION */ +PHP_MINFO_FUNCTION(zlib) { - zval **a_encoding; - - zend_is_auto_global("_SERVER", sizeof("_SERVER")-1 TSRMLS_CC); - - if (!PG(http_globals)[TRACK_VARS_SERVER] - || zend_hash_find(PG(http_globals)[TRACK_VARS_SERVER]->value.ht, "HTTP_ACCEPT_ENCODING", sizeof("HTTP_ACCEPT_ENCODING"), (void **) &a_encoding) == FAILURE - ) { - return FAILURE; - } - - convert_to_string_ex(a_encoding); - - if (php_memnstr(Z_STRVAL_PP(a_encoding), "gzip", 4, Z_STRVAL_PP(a_encoding) + Z_STRLEN_PP(a_encoding))) { - ZLIBG(compression_coding) = CODING_GZIP; - } else if (php_memnstr(Z_STRVAL_PP(a_encoding), "deflate", 7, Z_STRVAL_PP(a_encoding) + Z_STRLEN_PP(a_encoding))) { - ZLIBG(compression_coding) = CODING_DEFLATE; - } else { - return FAILURE; - } - - php_ob_set_internal_handler(php_gzip_output_handler, (uint)buffer_size, "zlib output compression", 0 TSRMLS_CC); + php_info_print_table_start(); + php_info_print_table_header(2, "ZLib Support", "enabled"); + php_info_print_table_row(2, "Stream Wrapper", "compress.zlib://"); + php_info_print_table_row(2, "Stream Filter", "zlib.inflate, zlib.deflate"); + php_info_print_table_row(2, "Compiled Version", ZLIB_VERSION); + php_info_print_table_row(2, "Linked Version", (char *) zlibVersion()); + php_info_print_table_end(); - if (ZLIBG(output_handler) && strlen(ZLIBG(output_handler))) { - php_start_ob_buffer_named(ZLIBG(output_handler), 0, 1 TSRMLS_CC); - } - return SUCCESS; + DISPLAY_INI_ENTRIES(); } /* }}} */ +/* {{{ php_zlib_module_entry */ +zend_module_entry php_zlib_module_entry = { + STANDARD_MODULE_HEADER, + "zlib", + php_zlib_functions, + PHP_MINIT(zlib), + PHP_MSHUTDOWN(zlib), + PHP_RINIT(zlib), + NULL, + PHP_MINFO(zlib), + "2.0", + PHP_MODULE_GLOBALS(zlib), + NULL, + NULL, + NULL, + STANDARD_MODULE_PROPERTIES_EX +}; +/* }}} */ + /* * Local variables: * tab-width: 4 Index: ext/zlib/zlib_fopen_wrapper.c =================================================================== RCS file: /repository/php-src/ext/zlib/zlib_fopen_wrapper.c,v retrieving revision 1.46.2.1.2.4.2.1 diff -u -p -d -r1.46.2.1.2.4.2.1 zlib_fopen_wrapper.c --- ext/zlib/zlib_fopen_wrapper.c 31 Dec 2007 07:17:17 -0000 1.46.2.1.2.4.2.1 +++ ext/zlib/zlib_fopen_wrapper.c 18 Sep 2008 18:38:19 -0000 @@ -92,7 +92,7 @@ static int php_gziop_flush(php_stream *s return gzflush(self->gz_file, Z_SYNC_FLUSH); } -static php_stream_ops php_stream_gzio_ops = { +php_stream_ops php_stream_gzio_ops = { php_gziop_write, php_gziop_read, php_gziop_close, php_gziop_flush, "ZLIB", Index: main/SAPI.c =================================================================== RCS file: /repository/php-src/main/SAPI.c,v retrieving revision 1.202.2.7.2.15.2.4 diff -u -p -d -r1.202.2.7.2.15.2.4 SAPI.c --- main/SAPI.c 18 Mar 2008 21:42:50 -0000 1.202.2.7.2.15.2.4 +++ main/SAPI.c 18 Sep 2008 18:38:19 -0000 @@ -529,8 +529,8 @@ SAPI_API int sapi_header_op(sapi_header_ int http_response_code; if (SG(headers_sent) && !SG(request_info).no_headers) { - char *output_start_filename = php_get_output_start_filename(TSRMLS_C); - int output_start_lineno = php_get_output_start_lineno(TSRMLS_C); + char *output_start_filename = php_output_get_start_filename(TSRMLS_C); + int output_start_lineno = php_output_get_start_lineno(TSRMLS_C); if (output_start_filename) { sapi_module.sapi_error(E_WARNING, "Cannot modify header information - headers already sent by (output started at %s:%d)", Index: main/main.c =================================================================== RCS file: /repository/php-src/main/main.c,v retrieving revision 1.640.2.23.2.57.2.34 diff -u -p -d -r1.640.2.23.2.57.2.34 main.c --- main/main.c 17 Sep 2008 00:20:29 -0000 1.640.2.23.2.57.2.34 +++ main/main.c 18 Sep 2008 18:38:19 -0000 @@ -904,14 +904,7 @@ static void php_error_cb(int type, const php_log_err(log_buffer TSRMLS_CC); efree(log_buffer); } - if (PG(display_errors) - && ((module_initialized && !PG(during_request_startup)) - || (PG(display_startup_errors) - && (OG(php_body_write)==php_default_output_func || OG(php_body_write)==php_ub_body_write_no_header || OG(php_body_write)==php_ub_body_write) - ) - ) - ) { - + if (PG(display_errors) && ((module_initialized && !PG(during_request_startup)) || (PG(display_startup_errors)))) { if (PG(xmlrpc_errors)) { php_printf("faultCode%ldfaultString%s:%s in %s on line %d", PG(xmlrpc_error_number), error_type_str, buffer, error_filename, error_lineno); } else { @@ -1312,15 +1305,16 @@ int php_request_startup(TSRMLS_D) } if (PG(output_handler) && PG(output_handler)[0]) { - php_start_ob_buffer_named(PG(output_handler), 0, 1 TSRMLS_CC); + zval *oh; + + MAKE_STD_ZVAL(oh); + ZVAL_STRING(oh, PG(output_handler), 1); + php_output_start_user(oh, 0, PHP_OUTPUT_HANDLER_STDFLAGS TSRMLS_CC); + zval_ptr_dtor(&oh); } else if (PG(output_buffering)) { - if (PG(output_buffering)>1) { - php_start_ob_buffer(NULL, PG(output_buffering), 1 TSRMLS_CC); - } else { - php_start_ob_buffer(NULL, 0, 1 TSRMLS_CC); - } + php_output_start_user(NULL, PG(output_buffering) > 1 ? PG(output_buffering) : 0, PHP_OUTPUT_HANDLER_STDFLAGS TSRMLS_CC); } else if (PG(implicit_flush)) { - php_start_implicit_flush(TSRMLS_C); + php_output_set_implicit_flush(1 TSRMLS_CC); } /* We turn this off in php_execute_script() */ @@ -1356,7 +1350,6 @@ int php_request_startup(TSRMLS_D) zend_try { PG(during_request_startup) = 1; - php_output_activate(TSRMLS_C); if (PG(expose_php)) { sapi_add_header(SAPI_PHP_VERSION_HEADER, sizeof(SAPI_PHP_VERSION_HEADER)-1, 1); } @@ -1478,12 +1471,13 @@ void php_request_shutdown(void *dummy) /* 3. Flush all output buffers */ zend_try { - zend_bool send_buffer = SG(request_info).headers_only ? 0 : 1; - if (CG(unclean_shutdown) && PG(last_error_type) == E_ERROR && - !OG(active_ob_buffer).chunk_size && PG(memory_limit) < zend_memory_usage(1 TSRMLS_CC)) { - send_buffer = 0; + if (SG(request_info).headers_only || + (CG(unclean_shutdown) && PG(last_error_type) == E_ERROR && PG(memory_limit) < zend_memory_usage(1 TSRMLS_CC))) { + php_output_discard_all(TSRMLS_C); + } else { + php_output_end_all(TSRMLS_C); } - php_end_ob_buffers(send_buffer TSRMLS_CC); + php_output_deactivate(TSRMLS_C); } zend_end_try(); /* 4. Send the set HTTP headers (note: This must be done AFTER php_end_ob_buffers() !!) */ @@ -1568,12 +1562,12 @@ PHPAPI void php_com_initialize(TSRMLS_D) } /* }}} */ -/* {{{ php_body_write_wrapper +/* {{{ php_output_wrapper */ -static int php_body_write_wrapper(const char *str, uint str_length) +static int php_output_wrapper(const char *str, uint str_length) { TSRMLS_FETCH(); - return php_body_write(str, str_length TSRMLS_CC); + return php_output_write(str, str_length TSRMLS_CC); } /* }}} */ @@ -1717,7 +1711,7 @@ int php_module_startup(sapi_module_struc zuf.error_function = php_error_cb; zuf.printf_function = php_printf; - zuf.write_function = php_body_write_wrapper; + zuf.write_function = php_output_wrapper; zuf.fopen_function = php_fopen_wrapper_for_zend; zuf.message_handler = php_message_handler_for_zend; zuf.block_interruptions = sapi_module.block_interruptions; @@ -1979,6 +1973,7 @@ void php_module_shutdown(TSRMLS_D) ts_free_id(core_globals_id); #endif + php_output_shutdown(); php_shutdown_temporary_directory(); module_initialized = 0; @@ -2138,7 +2133,7 @@ PHPAPI void php_handle_aborted_connectio TSRMLS_FETCH(); PG(connection_status) = PHP_CONNECTION_ABORTED; - php_output_set_status(0 TSRMLS_CC); + php_output_set_status(PHP_OUTPUT_DISABLED TSRMLS_CC); if (!PG(ignore_user_abort)) { zend_bailout(); Index: main/output.c =================================================================== RCS file: /repository/php-src/main/output.c,v retrieving revision 1.167.2.3.2.4.2.9 diff -u -p -d -r1.167.2.3.2.4.2.9 output.c --- main/output.c 18 Aug 2008 07:46:31 -0000 1.167.2.3.2.4.2.9 +++ main/output.c 18 Sep 2008 18:38:19 -0000 @@ -1,4 +1,4 @@ -/* +/* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ @@ -15,773 +15,1285 @@ | Authors: Zeev Suraski | | Thies C. Arntzen | | Marcus Boerger | + | New API: Michael Wallner | +----------------------------------------------------------------------+ */ -/* $Id: output.c,v 1.167.2.3.2.4.2.9 2008/08/18 07:46:31 tony2001 Exp $ */ +/* $Id: output.c,v 1.214 2008/08/18 07:45:59 tony2001 Exp $ */ + +#ifndef PHP_OUTPUT_DEBUG +# define PHP_OUTPUT_DEBUG 0 +#endif +#ifndef PHP_OUTPUT_NOINLINE +# define PHP_OUTPUT_NOINLINE 0 +#endif #include "php.h" #include "ext/standard/head.h" -#include "ext/standard/basic_functions.h" #include "ext/standard/url_scanner_ex.h" -#if HAVE_ZLIB && !defined(COMPILE_DL_ZLIB) -#include "ext/zlib/php_zlib.h" -#endif #include "SAPI.h" +#include "zend_stack.h" +#include "php_output.h" -#define OB_DEFAULT_HANDLER_NAME "default output handler" - -/* output functions */ -static int php_b_body_write(const char *str, uint str_length TSRMLS_DC); +ZEND_DECLARE_MODULE_GLOBALS(output); -static int php_ob_init(uint initial_size, uint block_size, zval *output_handler, uint chunk_size, zend_bool erase TSRMLS_DC); -static void php_ob_append(const char *text, uint text_length TSRMLS_DC); -#if 0 -static void php_ob_prepend(const char *text, uint text_length); +#if PHP_OUTPUT_NOINLINE || PHP_OUTPUT_DEBUG +# undef inline +# define inline #endif -#ifdef ZTS -int output_globals_id; -#else -php_output_globals output_globals; -#endif +/* {{{ aliases, conflict and reverse conflict hash tables */ +static HashTable php_output_handler_aliases; +static HashTable php_output_handler_conflicts; +static HashTable php_output_handler_reverse_conflicts; +/* }}} */ -/* {{{ php_default_output_func */ -PHPAPI int php_default_output_func(const char *str, uint str_len TSRMLS_DC) +/* {{{ forward declarations */ +static inline int php_output_lock_error(int op TSRMLS_DC); +static inline void php_output_op(int op, const char *str, size_t len TSRMLS_DC); + +#define php_output_handler_init(n, cs, f) php_output_handler_init_ex((n), (cs), (f) TSRMLS_CC) +static inline php_output_handler *php_output_handler_init_ex(zval *name, size_t chunk_size, int flags TSRMLS_DC); +static inline php_output_handler_status_t php_output_handler_op(php_output_handler *handler, php_output_context *context); +static inline int php_output_handler_append(php_output_handler *handler, const php_output_buffer *buf TSRMLS_DC); +static inline zval *php_output_handler_status(php_output_handler *handler, zval *entry); + +static inline php_output_context *php_output_context_init(php_output_context *context, int op TSRMLS_DC); +static inline void php_output_context_reset(php_output_context *context); +static inline void php_output_context_swap(php_output_context *context); +static inline void php_output_context_dtor(php_output_context *context); + +static inline int php_output_stack_pop(int flags TSRMLS_DC); + +static int php_output_stack_apply_op(void *h, void *c); +static int php_output_stack_apply_clean(void *h, void *c); +static int php_output_stack_apply_list(void *h, void *z); +static int php_output_stack_apply_status(void *h, void *z); + +static int php_output_handler_compat_func(void **handler_context, php_output_context *output_context); +static int php_output_handler_default_func(void **handler_context, php_output_context *output_context); +static int php_output_handler_devnull_func(void **handler_context, php_output_context *output_context); +/* }}} */ + +/* {{{ static void php_output_init_globals(zend_output_globals *G) + Initialize the module globals on MINIT */ +static inline void php_output_init_globals(zend_output_globals *G) { - fwrite(str, 1, str_len, stderr); - return str_len; + memset(G, 0, sizeof(*G)); } /* }}} */ -/* {{{ php_output_init_globals */ -static void php_output_init_globals(php_output_globals *output_globals_p TSRMLS_DC) +/* {{{ void php_output_startup(void) + Set up module globals and initalize the conflict and reverse conflict hash tables */ +PHPAPI void php_output_startup(void) { - 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; + ZEND_INIT_MODULE_GLOBALS(output, php_output_init_globals, NULL); + zend_hash_init(&php_output_handler_aliases, 0, NULL, NULL, 1); + zend_hash_init(&php_output_handler_conflicts, 0, NULL, NULL, 1); + zend_hash_init(&php_output_handler_reverse_conflicts, 0, NULL, (void (*)(void *)) zend_hash_destroy, 1); } /* }}} */ +/* {{{ void php_output_shutdown(void) + Destroy module globals and the conflict and reverse conflict hash tables */ +PHPAPI void php_output_shutdown(void) +{ + zend_hash_destroy(&php_output_handler_aliases); + zend_hash_destroy(&php_output_handler_conflicts); + zend_hash_destroy(&php_output_handler_reverse_conflicts); +} +/* }}} */ -/* {{{ php_output_startup - Start output layer */ -PHPAPI void php_output_startup(void) +/* {{{ SUCCESS|FAILURE php_output_activate(TSRMLS_D) + Reset output globals and setup the output handler stack */ +PHPAPI int php_output_activate(TSRMLS_D) { #ifdef ZTS - ts_allocate_id(&output_globals_id, sizeof(php_output_globals), (ts_allocate_ctor) php_output_init_globals, NULL); -#else - php_output_init_globals(&output_globals TSRMLS_CC); + memset((*((void ***) tsrm_ls))[TSRM_UNSHUFFLE_RSRC_ID(output_globals_id)], 0, sizeof(zend_output_globals)); +#else + memset(&output_globals, 0, sizeof(zend_output_globals)); #endif + + OG(handlers) = emalloc(sizeof(zend_stack)); + if (SUCCESS != zend_stack_init(OG(handlers))) { + return FAILURE; + } + + MAKE_STD_ZVAL(OG(default_output_handler_name)); + ZVAL_STRINGL(OG(default_output_handler_name), "default output handler", sizeof("default output handler")-1, 1); + MAKE_STD_ZVAL(OG(devnull_output_handler_name)); + ZVAL_STRINGL(OG(devnull_output_handler_name), "null output handler", sizeof("null output handler")-1, 1); + + return SUCCESS; } /* }}} */ - -/* {{{ php_output_activate - Initilize output global for activation */ -PHPAPI void php_output_activate(TSRMLS_D) +/* {{{ void php_output_deactivate(TSRMLS_D) + Destroy the output handler stack */ +PHPAPI void php_output_deactivate(TSRMLS_D) { - OG(php_body_write) = php_ub_body_write; - OG(php_header_write) = sapi_module.ub_write; - OG(ob_nesting_level) = 0; - OG(ob_lock) = 0; - OG(disable_output) = 0; - OG(output_start_filename) = NULL; - OG(output_start_lineno) = 0; + php_output_handler **handler = NULL; + + OG(active) = NULL; + OG(running) = NULL; + + /* release all output handlers */ + if (OG(handlers)) { + while (SUCCESS == zend_stack_top(OG(handlers), (void *) &handler)) { + php_output_handler_free(handler TSRMLS_CC); + zend_stack_del_top(OG(handlers)); + } + zend_stack_destroy(OG(handlers)); + efree(OG(handlers)); + OG(handlers) = NULL; + } + + if (OG(default_output_handler_name)) { + zval_ptr_dtor(&OG(default_output_handler_name)); + OG(default_output_handler_name) = NULL; + } + if (OG(devnull_output_handler_name)) { + zval_ptr_dtor(&OG(devnull_output_handler_name)); + OG(devnull_output_handler_name) = NULL; + } } /* }}} */ +/* {{{ void php_output_register_constants() */ +PHPAPI void php_output_register_constants(TSRMLS_D) +{ + REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_START", PHP_OUTPUT_HANDLER_START, CONST_CS | CONST_PERSISTENT); + REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_WRITE", PHP_OUTPUT_HANDLER_WRITE, CONST_CS | CONST_PERSISTENT); + REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_FLUSH", PHP_OUTPUT_HANDLER_FLUSH, CONST_CS | CONST_PERSISTENT); + REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_CLEAN", PHP_OUTPUT_HANDLER_CLEAN, CONST_CS | CONST_PERSISTENT); + REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_FINAL", PHP_OUTPUT_HANDLER_FINAL, CONST_CS | CONST_PERSISTENT); + REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_CONT", PHP_OUTPUT_HANDLER_WRITE, CONST_CS | CONST_PERSISTENT); + REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_END", PHP_OUTPUT_HANDLER_FINAL, CONST_CS | CONST_PERSISTENT); -/* {{{ php_output_set_status - Toggle output status. Do NOT use in application code, only in SAPIs where appropriate. */ -PHPAPI void php_output_set_status(zend_bool status TSRMLS_DC) + REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_CLEANABLE", PHP_OUTPUT_HANDLER_CLEANABLE, CONST_CS | CONST_PERSISTENT); + REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_FLUSHABLE", PHP_OUTPUT_HANDLER_FLUSHABLE, CONST_CS | CONST_PERSISTENT); + REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_REMOVABLE", PHP_OUTPUT_HANDLER_REMOVABLE, CONST_CS | CONST_PERSISTENT); + REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_STDFLAGS", PHP_OUTPUT_HANDLER_STDFLAGS, CONST_CS | CONST_PERSISTENT); + REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_STARTED", PHP_OUTPUT_HANDLER_STARTED, CONST_CS | CONST_PERSISTENT); + REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_DISABLED", PHP_OUTPUT_HANDLER_DISABLED, CONST_CS | CONST_PERSISTENT); +} +/* }}} */ + +/* {{{ void php_output_set_status(int status TSRMLS_DC) + Used by SAPIs to disable output */ +PHPAPI void php_output_set_status(int status TSRMLS_DC) { - OG(disable_output) = !status; + OG(flags) = status & 0xf; } /* }}} */ -/* {{{ php_output_register_constants */ -void php_output_register_constants(TSRMLS_D) +/* {{{ int php_output_get_status(TSRMLS_C) + Get output control status */ +PHPAPI int php_output_get_status(TSRMLS_D) { - 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); + return OG(flags) + | (OG(active) ? PHP_OUTPUT_ACTIVE : 0) + | (OG(running)? PHP_OUTPUT_LOCKED : 0); } /* }}} */ +/* {{{ zval *php_output_get_default_handler_name(TSRMLS_C) */ +PHPAPI zval *php_output_get_default_handler_name(TSRMLS_D) +{ + return OG(default_output_handler_name); +} +/* }}} */ -/* {{{ php_body_write - * Write body part */ -PHPAPI int php_body_write(const char *str, uint str_length TSRMLS_DC) +/* {{{ zval *php_output_get_devnull_handler_name(TSRMLS_C) */ +PHPAPI zval *php_output_get_devnull_handler_name(TSRMLS_D) { - return OG(php_body_write)(str, str_length TSRMLS_CC); + return OG(devnull_output_handler_name); } /* }}} */ -/* {{{ php_header_write - * Write HTTP header */ -PHPAPI int php_header_write(const char *str, uint str_length TSRMLS_DC) +/* {{{ int php_output_write_unbuffered(const char *str, size_t len TSRMLS_DC) + Unbuffered write */ +PHPAPI int php_output_write_unbuffered(const char *str, size_t len TSRMLS_DC) { - if (OG(disable_output)) { + if (OG(flags) & PHP_OUTPUT_DISABLED) { return 0; - } else { - return OG(php_header_write)(str, str_length TSRMLS_CC); } + return sapi_module.ub_write(str, len TSRMLS_CC); } /* }}} */ -/* {{{ php_start_ob_buffer - * Start output buffering */ -PHPAPI int php_start_ob_buffer(zval *output_handler, uint chunk_size, zend_bool erase TSRMLS_DC) +/* {{{ int php_output_write(const char *str, size_t len TSRMLS_DC) + Buffered write */ +PHPAPI int php_output_write(const char *str, size_t len TSRMLS_DC) { - uint initial_size, block_size; - - if (OG(ob_lock)) { - 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; - } - OG(ob_nesting_level) = 0; - php_error_docref("ref.outcontrol" TSRMLS_CC, E_ERROR, "Cannot use output buffering in output buffering display handlers"); - return FAILURE; + if (OG(flags) & PHP_OUTPUT_DISABLED) { + return 0; } - if (chunk_size > 0) { - if (chunk_size==1) { - chunk_size = 4096; + php_output_op(PHP_OUTPUT_HANDLER_WRITE, str, len TSRMLS_CC); + return (int) len; +} +/* }}} */ + +/* {{{ SUCCESS|FAILURE php_output_flush(TSRMLS_D) + Flush the most recent output handlers buffer */ +PHPAPI int php_output_flush(TSRMLS_D) +{ + php_output_context context; + + if (OG(active) && (OG(active)->flags & PHP_OUTPUT_HANDLER_FLUSHABLE)) { + php_output_context_init(&context, PHP_OUTPUT_HANDLER_FLUSH TSRMLS_CC); + php_output_handler_op(OG(active), &context); + if (context.out.data && context.out.used) { + zend_stack_del_top(OG(handlers)); + php_output_write(context.out.data, context.out.used TSRMLS_CC); + zend_stack_push(OG(handlers), &OG(active), sizeof(php_output_handler *)); } - initial_size = (chunk_size*3/2); - block_size = chunk_size/2; - } else { - initial_size = 40*1024; - block_size = 10*1024; + php_output_context_dtor(&context); + return SUCCESS; } - return php_ob_init(initial_size, block_size, output_handler, chunk_size, erase TSRMLS_CC); + return FAILURE; } /* }}} */ -/* {{{ php_start_ob_buffer_named - * Start output buffering */ -PHPAPI int php_start_ob_buffer_named(const char *output_handler_name, uint chunk_size, zend_bool erase TSRMLS_DC) +/* {{{ void php_output_flush_all(TSRMLS_C) + Flush all output buffers subsequently */ +PHPAPI void php_output_flush_all(TSRMLS_D) { - zval *output_handler; - int result; - - ALLOC_INIT_ZVAL(output_handler); - Z_STRLEN_P(output_handler) = strlen(output_handler_name); /* this can be optimized */ - Z_STRVAL_P(output_handler) = estrndup(output_handler_name, Z_STRLEN_P(output_handler)); - Z_TYPE_P(output_handler) = IS_STRING; - result = php_start_ob_buffer(output_handler, chunk_size, erase TSRMLS_CC); - zval_dtor(output_handler); - FREE_ZVAL(output_handler); - return result; + if (OG(active)) { + php_output_op(PHP_OUTPUT_HANDLER_FLUSH, NULL, 0 TSRMLS_CC); + } } /* }}} */ -/* {{{ php_end_ob_buffer - * End output buffering (one level) */ -PHPAPI void php_end_ob_buffer(zend_bool send_buffer, zend_bool just_flush TSRMLS_DC) +/* {{{ SUCCESS|FAILURE php_output_clean(TSRMLS_D) + Cleans the most recent output handlers buffer if the handler is cleanable */ +PHPAPI int php_output_clean(TSRMLS_D) { - char *final_buffer=NULL; - unsigned int final_buffer_length=0; - zval *alternate_buffer=NULL; - char *to_be_destroyed_buffer, *to_be_destroyed_handler_name; - char *to_be_destroyed_handled_output[2] = { 0, 0 }; - int status; - php_ob_buffer *prev_ob_buffer_p=NULL; - php_ob_buffer orig_ob_buffer; - - if (OG(ob_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; + php_output_context context; + + if (OG(active) && (OG(active)->flags & PHP_OUTPUT_HANDLER_CLEANABLE)) { + OG(active)->buffer.used = 0; + php_output_context_init(&context, PHP_OUTPUT_HANDLER_CLEAN TSRMLS_CC); + php_output_handler_op(OG(active), &context); + php_output_context_dtor(&context); + return SUCCESS; } + return FAILURE; +} +/* }}} */ -#if 0 - { - FILE *fp; - fp = fopen("/tmp/ob_log", "a"); - fprintf(fp, "NestLevel: %d ObStatus: %d HandlerName: %s\n", OG(ob_nesting_level), status, OG(active_ob_buffer).handler_name); - fclose(fp); - } -#endif +/* {{{ void php_output_clean_all(TSRMLS_D) + Cleans all output handler buffers, without regard whether the handler is cleanable */ +PHPAPI void php_output_clean_all(TSRMLS_D) +{ + php_output_context context; - 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 TSRMLS_CC); - } else if (OG(active_ob_buffer).output_handler) { - zval **params[2]; - zval *orig_buffer; - zval *z_status; - - ALLOC_INIT_ZVAL(orig_buffer); - ZVAL_STRINGL(orig_buffer, OG(active_ob_buffer).buffer, OG(active_ob_buffer).text_length, 1); - Z_SET_REFCOUNT_P(orig_buffer, 2); /* don't let call_user_function() destroy our buffer */ - Z_SET_ISREF_P(orig_buffer); - - ALLOC_INIT_ZVAL(z_status); - ZVAL_LONG(z_status, status); - - params[0] = &orig_buffer; - params[1] = &z_status; - OG(ob_lock) = 1; - - if (call_user_function_ex(CG(function_table), NULL, OG(active_ob_buffer).output_handler, &alternate_buffer, 2, params, 1, NULL TSRMLS_CC)==SUCCESS) { - if (alternate_buffer && !(Z_TYPE_P(alternate_buffer)==IS_BOOL && Z_BVAL_P(alternate_buffer)==0)) { - convert_to_string_ex(&alternate_buffer); - final_buffer = Z_STRVAL_P(alternate_buffer); - final_buffer_length = Z_STRLEN_P(alternate_buffer); - } - } - OG(ob_lock) = 0; - if (!just_flush) { - zval_ptr_dtor(&OG(active_ob_buffer).output_handler); - } - Z_SET_REFCOUNT_P(orig_buffer, Z_REFCOUNT_P(orig_buffer) - 2); - if (Z_REFCOUNT_P(orig_buffer) <= 0) { /* free the zval */ - zval_dtor(orig_buffer); - FREE_ZVAL(orig_buffer); - } - zval_ptr_dtor(&z_status); + if (OG(active)) { + php_output_context_init(&context, PHP_OUTPUT_HANDLER_CLEAN TSRMLS_CC); + zend_stack_apply_with_argument(OG(handlers), ZEND_STACK_APPLY_TOPDOWN, php_output_stack_apply_clean, &context); } +} - if (!final_buffer) { - final_buffer = OG(active_ob_buffer).buffer; - final_buffer_length = OG(active_ob_buffer).text_length; +/* {{{ SUCCESS|FAILURE php_output_end(TSRMLS_D) + Finalizes the most recent output handler at pops it off the stack if the handler is removable */ +PHPAPI int php_output_end(TSRMLS_D) +{ + if (php_output_stack_pop(PHP_OUTPUT_POP_TRY TSRMLS_CC)) { + return SUCCESS; } + return FAILURE; +} +/* }}} */ - if (OG(ob_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; - } - } +/* {{{ void php_output_end_all(TSRMLS_D) + Finalizes all output handlers and ends output buffering without regard whether a handler is removable */ +PHPAPI void php_output_end_all(TSRMLS_D) +{ + while (OG(active) && php_output_stack_pop(PHP_OUTPUT_POP_FORCE TSRMLS_CC)); +} +/* }}} */ - to_be_destroyed_buffer = OG(active_ob_buffer).buffer; - to_be_destroyed_handler_name = OG(active_ob_buffer).handler_name; - if (OG(active_ob_buffer).internal_output_handler - && (final_buffer != OG(active_ob_buffer).internal_output_handler_buffer) - && (final_buffer != OG(active_ob_buffer).buffer)) { - to_be_destroyed_handled_output[0] = final_buffer; +/* {{{ SUCCESS|FAILURE php_output_discard(TSRMLS_D) + Discards the most recent output handlers buffer and pops it off the stack if the handler is removable */ +PHPAPI int php_output_discard(TSRMLS_D) +{ + if (php_output_stack_pop(PHP_OUTPUT_POP_DISCARD|PHP_OUTPUT_POP_TRY TSRMLS_CC)) { + return SUCCESS; } + return FAILURE; +} +/* }}} */ - 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(ob_nesting_level)>1) { /* restore previous buffer */ - zend_stack_top(&OG(ob_buffers), (void **) &prev_ob_buffer_p); - orig_ob_buffer = OG(active_ob_buffer); - OG(active_ob_buffer) = *prev_ob_buffer_p; - zend_stack_del_top(&OG(ob_buffers)); - if (!just_flush && OG(ob_nesting_level)==2) { /* destroy the stack */ - zend_stack_destroy(&OG(ob_buffers)); - } +/* {{{ void php_output_discard_all(TSRMLS_D) + Discard all output handlers and buffers without regard whether a handler is removable */ +PHPAPI void php_output_discard_all(TSRMLS_D) +{ + while (OG(active)) { + php_output_stack_pop(PHP_OUTPUT_POP_DISCARD|PHP_OUTPUT_POP_FORCE TSRMLS_CC); } - OG(ob_nesting_level)--; +} +/* }}} */ - if (send_buffer) { - if (just_flush) { /* if flush is called prior to proper end, ensure presence of NUL */ - final_buffer[final_buffer_length] = '\0'; - } - OG(php_body_write)(final_buffer, final_buffer_length TSRMLS_CC); +/* {{{ int php_output_get_level(TSRMLS_D) + Get output buffering level, ie. how many output handlers the stack contains */ +PHPAPI int php_output_get_level(TSRMLS_D) +{ + return OG(active) ? zend_stack_count(OG(handlers)) : 0; +} +/* }}} */ + +/* {{{ SUCCESS|FAILURE php_output_get_contents(zval *z TSRMLS_DC) + Get the contents of the active output handlers buffer */ +PHPAPI int php_output_get_contents(zval *p TSRMLS_DC) +{ + if (OG(active)) { + ZVAL_STRINGL(p, OG(active)->buffer.data, OG(active)->buffer.used, 1); + return SUCCESS; + } else { + ZVAL_NULL(p); + return FAILURE; } +} - if (just_flush) { /* we restored the previous ob, return to the current */ - if (prev_ob_buffer_p) { - zend_stack_push(&OG(ob_buffers), &OG(active_ob_buffer), sizeof(php_ob_buffer)); - OG(active_ob_buffer) = orig_ob_buffer; - } - OG(ob_nesting_level)++; +/* {{{ SUCCESS|FAILURE php_output_get_length(zval *z TSRMLS_DC) + Get the length of the active output handlers buffer */ +PHPAPI int php_output_get_length(zval *p TSRMLS_DC) +{ + if (OG(active)) { + ZVAL_LONG(p, OG(active)->buffer.used); + return SUCCESS; + } else { + ZVAL_NULL(p); + return FAILURE; } +} +/* }}} */ - if (alternate_buffer) { - zval_ptr_dtor(&alternate_buffer); +/* {{{ SUCCESS|FAILURE php_output_handler_start_default(TSRMLS_D) + Start a "default output handler" */ +PHPAPI int php_output_start_default(TSRMLS_D) +{ + php_output_handler *handler; + + handler = php_output_handler_create_internal(OG(default_output_handler_name), php_output_handler_default_func, 0, PHP_OUTPUT_HANDLER_STDFLAGS TSRMLS_CC); + if (SUCCESS == php_output_handler_start(handler TSRMLS_CC)) { + return SUCCESS; } + php_output_handler_free(&handler TSRMLS_CC); + return FAILURE; +} +/* }}} */ - if (status & PHP_OUTPUT_HANDLER_END) { - efree(to_be_destroyed_handler_name); +/* {{{ SUCCESS|FAILURE php_output_handler_start_devnull(TSRMLS_D) + Start a "null output handler" */ +PHPAPI int php_output_start_devnull(TSRMLS_D) +{ + php_output_handler *handler; + + handler = php_output_handler_create_internal(OG(devnull_output_handler_name), php_output_handler_devnull_func, PHP_OUTPUT_HANDLER_DEFAULT_SIZE, 0 TSRMLS_CC); + if (SUCCESS == php_output_handler_start(handler TSRMLS_CC)) { + return SUCCESS; } - if (!just_flush) { - efree(to_be_destroyed_buffer); + php_output_handler_free(&handler TSRMLS_CC); + return FAILURE; +} +/* }}} */ + +/* {{{ SUCCESS|FAILURE php_output_start_user(zval *handler, size_t chunk_size, int flags TSRMLS_DC) + Start a user level output handler */ +PHPAPI int php_output_start_user(zval *output_handler, size_t chunk_size, int flags TSRMLS_DC) +{ + php_output_handler *handler; + + if (output_handler) { + handler = php_output_handler_create_user(output_handler, chunk_size, flags TSRMLS_CC); } 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]); + handler = php_output_handler_create_internal(OG(default_output_handler_name), php_output_handler_default_func, chunk_size, flags TSRMLS_CC); } - if (to_be_destroyed_handled_output[1]) { - efree(to_be_destroyed_handled_output[1]); + if (SUCCESS == php_output_handler_start(handler TSRMLS_CC)) { + return SUCCESS; } + php_output_handler_free(&handler TSRMLS_CC); + return FAILURE; } /* }}} */ -/* {{{ php_end_ob_buffers - * End output buffering (all buffers) */ -PHPAPI void php_end_ob_buffers(zend_bool send_buffer TSRMLS_DC) +/* {{{ SUCCESS|FAILURE php_output_start_internal(zval *name, php_output_handler_func_t handler, size_t chunk_size, int flags TSRMLS_DC) + Start an internal output handler that does not have to maintain a non-global state */ +PHPAPI int php_output_start_internal(zval *name, php_output_handler_func_t output_handler, size_t chunk_size, int flags TSRMLS_DC) { - while (OG(ob_nesting_level)!=0) { - php_end_ob_buffer(send_buffer, 0 TSRMLS_CC); + php_output_handler *handler; + + handler = php_output_handler_create_internal(name, php_output_handler_compat_func, chunk_size, flags TSRMLS_CC); + php_output_handler_set_context(handler, output_handler, NULL TSRMLS_CC); + if (SUCCESS == php_output_handler_start(handler TSRMLS_CC)) { + return SUCCESS; } + php_output_handler_free(&handler TSRMLS_CC); + return FAILURE; } /* }}} */ -/* {{{ php_start_implicit_flush - */ -PHPAPI void php_start_implicit_flush(TSRMLS_D) +/* {{{ php_output_handler *php_output_handler_create_user(zval *handler, size_t chunk_size, int flags TSRMLS_DC) + Create a user level output handler */ +PHPAPI php_output_handler *php_output_handler_create_user(zval *output_handler, size_t chunk_size, int flags TSRMLS_DC) { - OG(implicit_flush)=1; + char *handler_name_str = NULL; + zval *handler_name = NULL; + php_output_handler *handler = NULL; + php_output_handler_alias_ctor_t *alias = NULL; + php_output_handler_user_func_t *user = NULL; + + switch (Z_TYPE_P(output_handler)) { + case IS_NULL: + handler = php_output_handler_create_internal(OG(default_output_handler_name), php_output_handler_default_func, chunk_size, flags TSRMLS_CC); + break; + case IS_STRING: + if (Z_STRLEN_P(output_handler) && (alias = php_output_handler_alias(output_handler TSRMLS_CC))) { + handler = (*alias)(output_handler, chunk_size, flags TSRMLS_CC); + break; + } + default: + user = ecalloc(1, sizeof(php_output_handler_user_func_t)); + if (SUCCESS == zend_fcall_info_init(output_handler, 0, &user->fci, &user->fcc, &handler_name_str, NULL TSRMLS_CC)) { + MAKE_STD_ZVAL(handler_name); + ZVAL_STRING(handler_name, handler_name_str, 0); + handler = php_output_handler_init(handler_name, chunk_size, (flags & ~0xf) | PHP_OUTPUT_HANDLER_USER); + Z_ADDREF_P(output_handler); + user->zoh = output_handler; + handler->func.user = user; + zval_ptr_dtor(&handler_name); + } else { + /* TODO(helly) log the rror? */ + efree(user); + } + } + + return handler; } /* }}} */ -/* {{{ php_end_implicit_flush - */ -PHPAPI void php_end_implicit_flush(TSRMLS_D) +/* {{{ php_output_handler *php_output_handler_create_internal(zval *name, php_output_handler_context_func_t handler, size_t chunk_size, int flags TSRMLS_DC) + Create an internal output handler that can maintain a non-global state */ +PHPAPI php_output_handler *php_output_handler_create_internal(zval *name, php_output_handler_context_func_t output_handler, size_t chunk_size, int flags TSRMLS_DC) { - OG(implicit_flush)=0; + php_output_handler *handler; + + handler = php_output_handler_init(name, chunk_size, (flags & ~0xf) | PHP_OUTPUT_HANDLER_INTERNAL); + handler->func.internal = output_handler; + + return handler; } -/* }}} */ -/* {{{ php_ob_set_internal_handler - */ -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) +/* {{{ void php_output_handler_set_context(php_output_handler *handler, void *opaq, void (*dtor)(void* TSRMLS_DC) TSRMLS_DC) + Set the context/state of an output handler. Calls the dtor of the previous context if there is one */ +PHPAPI void php_output_handler_set_context(php_output_handler *handler, void *opaq, void (*dtor)(void* TSRMLS_DC) TSRMLS_DC) { - if (OG(ob_nesting_level)==0 || OG(active_ob_buffer).internal_output_handler || strcmp(OG(active_ob_buffer).handler_name, OB_DEFAULT_HANDLER_NAME)) { - php_start_ob_buffer(NULL, buffer_size, erase TSRMLS_CC); + if (handler->dtor && handler->opaq) { + handler->dtor(handler->opaq TSRMLS_CC); } + handler->dtor = dtor; + handler->opaq = opaq; +} +/* }}} */ - 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; - if (OG(active_ob_buffer).handler_name) { - efree(OG(active_ob_buffer).handler_name); +/* {{{ SUCCESS|FAILURE php_output_handler_start(php_output_handler *handler TSRMLS_DC) + Starts the set up output handler and pushes it on top of the stack. Checks for any conflicts regarding the output handler to start */ +PHPAPI int php_output_handler_start(php_output_handler *handler TSRMLS_DC) +{ + HashPosition pos; + HashTable *rconflicts; + php_output_handler_conflict_check_t *conflict; + + if (php_output_lock_error(PHP_OUTPUT_HANDLER_START TSRMLS_CC) || !handler) { + return FAILURE; } - OG(active_ob_buffer).handler_name = estrdup(handler_name); - OG(active_ob_buffer).erase = erase; + if (SUCCESS == zend_hash_find(&php_output_handler_conflicts, Z_STRVAL_P(handler->name), Z_STRLEN_P(handler->name)+1, (void *) &conflict)) { + if (SUCCESS != (*conflict)(handler->name TSRMLS_CC)) { + return FAILURE; + } + } + if (SUCCESS == zend_hash_find(&php_output_handler_reverse_conflicts, Z_STRVAL_P(handler->name), Z_STRLEN_P(handler->name)+1, (void *) &rconflicts)) { + for ( zend_hash_internal_pointer_reset_ex(rconflicts, &pos); + zend_hash_get_current_data_ex(rconflicts, (void *) &conflict, &pos) == SUCCESS; + zend_hash_move_forward_ex(rconflicts, &pos)) { + if (SUCCESS != (*conflict)(handler->name TSRMLS_CC)) { + return FAILURE; + } + } + } + /* zend_stack_push never returns SUCCESS but FAILURE or stack level */ + if (FAILURE == (handler->level = zend_stack_push(OG(handlers), &handler, sizeof(php_output_handler *)))) { + return FAILURE; + } + OG(active) = handler; + return SUCCESS; } /* }}} */ -/* - * Output buffering - implementation - */ - -/* {{{ php_ob_allocate - */ -static inline void php_ob_allocate(uint text_length TSRMLS_DC) +/* {{{ int php_output_handler_started(zval *name TSRMLS_DC) + Check whether a certain output handler is in use */ +PHPAPI int php_output_handler_started(zval *name TSRMLS_DC) { - uint new_len = OG(active_ob_buffer).text_length + text_length; - - if (OG(active_ob_buffer).size < new_len) { - uint buf_size = OG(active_ob_buffer).size; - while (buf_size <= new_len) { - buf_size += OG(active_ob_buffer).block_size; + php_output_handler **handlers; + int i, count = php_output_get_level(TSRMLS_C); + + if (count) { + handlers = *(php_output_handler ***) zend_stack_base(OG(handlers)); + + for (i = 0; i < count; ++i) { + if (!zend_binary_zval_strcmp(handlers[i]->name, name)) { + return 1; + } } - - OG(active_ob_buffer).buffer = (char *) erealloc(OG(active_ob_buffer).buffer, buf_size+1); - OG(active_ob_buffer).size = buf_size; } - OG(active_ob_buffer).text_length = new_len; + + return 0; } /* }}} */ -/* {{{ php_ob_init_conflict - * Returns 1 if handler_set is already used and generates error message - */ -PHPAPI int php_ob_init_conflict(char *handler_new, char *handler_set TSRMLS_DC) +/* {{{ int php_output_handler_conflict(zval *handler_new, zval *handler_old TSRMLS_DC) + Check whether a certain handler is in use and issue a warning that the new handler would conflict with the already used one */ +PHPAPI int php_output_handler_conflict(zval *handler_new, zval *handler_set TSRMLS_DC) { - if (php_ob_handler_used(handler_set TSRMLS_CC)) { - php_error_docref("ref.outcontrol" TSRMLS_CC, E_WARNING, "output handler '%s' conflicts with '%s'", handler_new, handler_set); + if (php_output_handler_started(handler_set TSRMLS_CC)) { + if (zend_binary_zval_strcmp(handler_new, handler_set)) { + php_error_docref("ref.outcontrol" TSRMLS_CC, E_WARNING, "output handler '%s' conflicts with '%s'", Z_STRVAL_P(handler_new), Z_STRVAL_P(handler_set)); + } else { + php_error_docref("ref.outcontrol" TSRMLS_CC, E_WARNING, "output handler '%s' cannot be used twice", Z_STRVAL_P(handler_new)); + } return 1; } return 0; } /* }}} */ -/* {{{ php_ob_init_named - */ -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) +/* {{{ SUCCESS|FAILURE php_output_handler_conflict_register(zval *name, php_output_handler_conflict_check_t check_func TSRMLS_DC) + Register a conflict checking function on MINIT */ +PHPAPI int php_output_handler_conflict_register(zval *name, php_output_handler_conflict_check_t check_func TSRMLS_DC) { - php_ob_buffer tmp_buf; - - if (output_handler && !zend_is_callable(output_handler, 0, NULL TSRMLS_CC)) { + if (!EG(current_module)) { + zend_error(E_ERROR, "Cannot register an output handler conflict outside of MINIT"); return FAILURE; } - - tmp_buf.block_size = block_size; - tmp_buf.size = initial_size; - tmp_buf.buffer = (char *) emalloc(initial_size+1); - tmp_buf.text_length = 0; - tmp_buf.output_handler = output_handler; - tmp_buf.chunk_size = chunk_size; - tmp_buf.status = 0; - tmp_buf.internal_output_handler = NULL; - tmp_buf.internal_output_handler_buffer = NULL; - tmp_buf.internal_output_handler_buffer_size = 0; - tmp_buf.handler_name = estrdup(handler_name&&handler_name[0]?handler_name:OB_DEFAULT_HANDLER_NAME); - tmp_buf.erase = erase; + return zend_hash_update(&php_output_handler_conflicts, Z_STRVAL_P(name), Z_STRLEN_P(name)+1, &check_func, sizeof(php_output_handler_conflict_check_t *), NULL); +} +/* }}} */ - if (OG(ob_nesting_level)>0) { -#if HAVE_ZLIB && !defined(COMPILE_DL_ZLIB) - if (!strncmp(handler_name, "ob_gzhandler", sizeof("ob_gzhandler")) && php_ob_gzhandler_check(TSRMLS_C)) { +/* {{{ SUCCESS|FAILURE php_output_handler_reverse_conflict_register(zval *name, php_output_handler_conflict_check_t check_func TSRMLS_DC) + Register a reverse conflict checking function on MINIT */ +PHPAPI int php_output_handler_reverse_conflict_register(zval *name, php_output_handler_conflict_check_t check_func TSRMLS_DC) +{ + HashTable rev, *rev_ptr = NULL; + + if (!EG(current_module)) { + zend_error(E_ERROR, "Cannot register a reverse output handler conflict outside of MINIT"); + return FAILURE; + } + if (SUCCESS == zend_hash_find(&php_output_handler_reverse_conflicts, Z_STRVAL_P(name), Z_STRLEN_P(name)+1, (void *) &rev_ptr)) { + return zend_hash_next_index_insert(rev_ptr, &check_func, sizeof(php_output_handler_conflict_check_t *), NULL); + } else { + zend_hash_init(&rev, 1, NULL, NULL, 1); + if (SUCCESS != zend_hash_next_index_insert(&rev, &check_func, sizeof(php_output_handler_conflict_check_t *), NULL)) { + zend_hash_destroy(&rev); return FAILURE; } -#endif - if (OG(ob_nesting_level)==1) { /* initialize stack */ - zend_stack_init(&OG(ob_buffers)); + if (SUCCESS != zend_hash_update(&php_output_handler_reverse_conflicts, Z_STRVAL_P(name), Z_STRLEN_P(name)+1, &rev, sizeof(HashTable), NULL)) { + zend_hash_destroy(&rev); + return FAILURE; } - zend_stack_push(&OG(ob_buffers), &OG(active_ob_buffer), sizeof(php_ob_buffer)); + return SUCCESS; } - OG(ob_nesting_level)++; - OG(active_ob_buffer) = tmp_buf; - OG(php_body_write) = php_b_body_write; - return SUCCESS; } /* }}} */ -/* {{{ php_ob_handler_from_string - * Create zval output handler from string - */ -static zval* php_ob_handler_from_string(const char *handler_name, int len TSRMLS_DC) +/* {{{ php_output_handler_alias_ctor_t php_output_handler_alias(zval *name TSRMLS_DC) + Get an internal output handler for a user handler if it exists */ +PHPAPI php_output_handler_alias_ctor_t *php_output_handler_alias(zval *name TSRMLS_DC) { - zval *output_handler; - - ALLOC_INIT_ZVAL(output_handler); - Z_STRLEN_P(output_handler) = len; - Z_STRVAL_P(output_handler) = estrndup(handler_name, len); - Z_TYPE_P(output_handler) = IS_STRING; - return output_handler; + php_output_handler_alias_ctor_t *func = NULL; + + zend_hash_find(&php_output_handler_aliases, Z_STRVAL_P(name), Z_STRLEN_P(name)+1, (void *) &func); + return func; } /* }}} */ -/* {{{ php_ob_init - */ -static int php_ob_init(uint initial_size, uint block_size, zval *output_handler, uint chunk_size, zend_bool erase TSRMLS_DC) +/* {{{ SUCCESS|FAILURE php_output_handler_alias_register(zval *name, php_output_handler_alias_ctor_t func TSRMLS_DC) + Registers an internal output handler as alias for a user handler */ +PHPAPI int php_output_handler_alias_register(zval *name, php_output_handler_alias_ctor_t func TSRMLS_DC) { - int result = FAILURE, handler_len, len; - char *handler_name, *next_handler_name; - HashPosition pos; - zval **tmp; - zval *handler_zval; - - if (output_handler && output_handler->type == IS_STRING) { - handler_name = Z_STRVAL_P(output_handler); - handler_len = Z_STRLEN_P(output_handler); + if (!EG(current_module)) { + zend_error(E_ERROR, "Cannot register an output handler alias outside of MINIT"); + return FAILURE; + } + return zend_hash_update(&php_output_handler_aliases, Z_STRVAL_P(name), Z_STRLEN_P(name)+1, &func, sizeof(php_output_handler_alias_ctor_t *), NULL); +} +/* }}} */ - result = SUCCESS; - if (handler_len && handler_name[0] != '\0') { - while ((next_handler_name=strchr(handler_name, ',')) != NULL) { - len = next_handler_name-handler_name; - next_handler_name = estrndup(handler_name, len); - handler_zval = php_ob_handler_from_string(next_handler_name, len TSRMLS_CC); - result = php_ob_init_named(initial_size, block_size, next_handler_name, handler_zval, chunk_size, erase TSRMLS_CC); - if (result != SUCCESS) { - zval_dtor(handler_zval); - FREE_ZVAL(handler_zval); - } - handler_name += len+1; - handler_len -= len+1; - efree(next_handler_name); - } - } - if (result == SUCCESS) { - handler_zval = php_ob_handler_from_string(handler_name, handler_len TSRMLS_CC); - result = php_ob_init_named(initial_size, block_size, handler_name, handler_zval, chunk_size, erase TSRMLS_CC); - if (result != SUCCESS) { - zval_dtor(handler_zval); - FREE_ZVAL(handler_zval); - } - } - } else if (output_handler && output_handler->type == IS_ARRAY) { - /* do we have array(object,method) */ - if (zend_is_callable(output_handler, 0, &handler_name TSRMLS_CC)) { - SEPARATE_ZVAL(&output_handler); - Z_ADDREF_P(output_handler); - result = php_ob_init_named(initial_size, block_size, handler_name, output_handler, chunk_size, erase TSRMLS_CC); - efree(handler_name); - } else { - efree(handler_name); - /* init all array elements recursively */ - zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(output_handler), &pos); - while (zend_hash_get_current_data_ex(Z_ARRVAL_P(output_handler), (void **)&tmp, &pos) == SUCCESS) { - result = php_ob_init(initial_size, block_size, *tmp, chunk_size, erase TSRMLS_CC); - if (result == FAILURE) { - break; - } - zend_hash_move_forward_ex(Z_ARRVAL_P(output_handler), &pos); - } - } - } else if (output_handler && output_handler->type == IS_OBJECT) { - /* do we have callable object */ - if (zend_is_callable(output_handler, 0, &handler_name TSRMLS_CC)) { - SEPARATE_ZVAL(&output_handler); - Z_ADDREF_P(output_handler); - result = php_ob_init_named(initial_size, block_size, handler_name, output_handler, chunk_size, erase TSRMLS_CC); - efree(handler_name); - } else { - efree(handler_name); - 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); - result = FAILURE; +/* {{{ SUCCESS|FAILURE php_output_handler_hook(php_output_handler_hook_t type, void *arg TSMRLS_DC) + Output handler hook for output handler functions to check/modify the current handlers abilities */ +PHPAPI int php_output_handler_hook(php_output_handler_hook_t type, void *arg TSRMLS_DC) +{ + if (OG(running)) { + switch (type) { + case PHP_OUTPUT_HANDLER_HOOK_GET_OPAQ: + *(void ***) arg = &OG(running)->opaq; + return SUCCESS; + case PHP_OUTPUT_HANDLER_HOOK_GET_FLAGS: + *(int *) arg = OG(running)->flags; + return SUCCESS; + case PHP_OUTPUT_HANDLER_HOOK_GET_LEVEL: + *(int *) arg = OG(running)->level; + case PHP_OUTPUT_HANDLER_HOOK_IMMUTABLE: + OG(running)->flags &= ~(PHP_OUTPUT_HANDLER_REMOVABLE|PHP_OUTPUT_HANDLER_CLEANABLE); + return SUCCESS; + case PHP_OUTPUT_HANDLER_HOOK_DISABLE: + OG(running)->flags |= PHP_OUTPUT_HANDLER_DISABLED; + return SUCCESS; + default: + break; } - } else { - result = php_ob_init_named(initial_size, block_size, OB_DEFAULT_HANDLER_NAME, NULL, chunk_size, erase TSRMLS_CC); } - return result; + return FAILURE; } /* }}} */ -/* {{{ php_ob_list_each - */ -static int php_ob_list_each(php_ob_buffer *ob_buffer, zval *ob_handler_array) +/* {{{ void php_output_handler_dtor(php_output_handler *handler TSRMLS_DC) + Destroy an output handler */ +PHPAPI void php_output_handler_dtor(php_output_handler *handler TSRMLS_DC) { - add_next_index_string(ob_handler_array, ob_buffer->handler_name, 1); - return 0; + zval_ptr_dtor(&handler->name); + STR_FREE(handler->buffer.data); + if (handler->flags & PHP_OUTPUT_HANDLER_USER) { + zval_ptr_dtor(&handler->func.user->zoh); + efree(handler->func.user); + } + if (handler->dtor && handler->opaq) { + handler->dtor(handler->opaq TSRMLS_CC); + } + memset(handler, 0, sizeof(*handler)); } /* }}} */ -/* {{{ proto false|array ob_list_handlers() - * List all output_buffers in an array - */ -PHP_FUNCTION(ob_list_handlers) +/* {{{ void php_output_handler_free(php_output_handler **handler TSMRLS_DC) + Destroy and free an output handler */ +PHPAPI void php_output_handler_free(php_output_handler **h TSRMLS_DC) { - if (ZEND_NUM_ARGS()!=0) { - ZEND_WRONG_PARAM_COUNT(); - RETURN_FALSE; + if (*h) { + php_output_handler_dtor(*h TSRMLS_CC); + efree(*h); + *h = NULL; } +} +/* }}} */ - array_init(return_value); - if (OG(ob_nesting_level)) { - if (OG(ob_nesting_level)>1) { - zend_stack_apply_with_argument(&OG(ob_buffers), ZEND_STACK_APPLY_BOTTOMUP, (int (*)(void *element, void *)) php_ob_list_each, return_value); - } - php_ob_list_each(&OG(active_ob_buffer), return_value); +/* void php_output_set_implicit_flush(int enabled TSRMLS_DC) + Enable or disable implicit flush */ +PHPAPI void php_output_set_implicit_flush(int flush TSRMLS_DC) +{ + if (flush) { + OG(flags) |= PHP_OUTPUT_IMPLICITFLUSH; + } else { + OG(flags) &= ~PHP_OUTPUT_IMPLICITFLUSH; } } /* }}} */ -/* {{{ php_ob_used_each - * Sets handler_name to NULL is found - */ -static int php_ob_handler_used_each(php_ob_buffer *ob_buffer, char **handler_name) +/* {{{ char *php_output_get_start_filename(TSRMLS_D) + Get the file name where output has started */ +PHPAPI char *php_output_get_start_filename(TSRMLS_D) { - if (!strcmp(ob_buffer->handler_name, *handler_name)) { - *handler_name = NULL; + return OG(output_start_filename); +} +/* }}} */ + +/* {{{ int php_output_get_start_lineno(TSRMLS_D) + Get the line number where output has started */ +PHPAPI int php_output_get_start_lineno(TSRMLS_D) +{ + return OG(output_start_lineno); +} +/* }}} */ + +/* {{{ static int php_output_lock_error(int op TSRMLS_DC) + Checks whether an unallowed operation is attempted from within the output handler and issues a fatal error */ +static inline int php_output_lock_error(int op TSRMLS_DC) +{ + /* if there's no ob active, ob has been stopped */ + if (op && OG(active) && OG(running)) { + /* fatal error */ + php_output_deactivate(TSRMLS_C); + php_error_docref("ref.outcontrol" TSRMLS_CC, E_ERROR, "Cannot use output buffering in output buffering display handlers"); return 1; } return 0; } /* }}} */ -/* {{{ php_ob_used - * returns 1 if given handler_name is used as output_handler - */ -PHPAPI int php_ob_handler_used(char *handler_name TSRMLS_DC) +/* {{{ static php_output_context *php_output_context_init(php_output_context *context, int op TSRMLS_DC) + Initialize a new output context */ +static inline php_output_context *php_output_context_init(php_output_context *context, int op TSRMLS_DC) { - char *tmp = handler_name; - - if (OG(ob_nesting_level)) { - if (!strcmp(OG(active_ob_buffer).handler_name, handler_name)) { - return 1; - } - if (OG(ob_nesting_level)>1) { - zend_stack_apply_with_argument(&OG(ob_buffers), ZEND_STACK_APPLY_BOTTOMUP, (int (*)(void *element, void *)) php_ob_handler_used_each, &tmp); - } + if (!context) { + context = emalloc(sizeof(php_output_context)); } - return tmp ? 0 : 1; + + memset(context, 0, sizeof(php_output_context)); + TSRMLS_SET_CTX(context->tsrm_ls); + context->op = op; + + return context; } /* }}} */ -/* {{{ php_ob_append - */ -static inline void php_ob_append(const char *text, uint text_length TSRMLS_DC) +/* {{{ static void php_output_context_reset(php_output_context *context) + Reset an output context */ +static inline void php_output_context_reset(php_output_context *context) { - char *target; - int original_ob_text_length; - - original_ob_text_length=OG(active_ob_buffer).text_length; - - php_ob_allocate(text_length TSRMLS_CC); - target = OG(active_ob_buffer).buffer+original_ob_text_length; - memcpy(target, text, text_length); - target[text_length]=0; + int op = context->op; + php_output_context_dtor(context); + memset(context, 0, sizeof(php_output_context)); + context->op = op; +} +/* }}} */ - /* If implicit_flush is On or chunked buffering, send contents to next buffer and return. */ - if (OG(active_ob_buffer).chunk_size - && OG(active_ob_buffer).text_length >= OG(active_ob_buffer).chunk_size) { - - php_end_ob_buffer(1, 1 TSRMLS_CC); - return; +/* {{{ static void php_output_context_swap(php_output_context *context) + Swap output contexts buffers */ +static inline void php_output_context_swap(php_output_context *context) +{ + if (context->in.free && context->in.data) { + efree(context->in.data); } + context->in.data = context->out.data; + context->in.used = context->out.used; + context->in.free = context->out.free; + context->in.size = context->out.size; + context->out.data = NULL; + context->out.used = 0; + context->out.size = 0; } /* }}} */ -#if 0 -static inline void php_ob_prepend(const char *text, uint text_length) +/* {{{ static void php_output_context_pass(php_output_context *context) + Pass input to output buffer */ +static inline void php_output_context_pass(php_output_context *context) { - char *p, *start; - TSRMLS_FETCH(); - - php_ob_allocate(text_length TSRMLS_CC); - - /* 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); + context->out.data = context->in.data; + context->out.used = context->in.used; + context->out.size = context->in.size; + context->out.free = context->in.free; + context->in.data = NULL; + context->in.used = 0; + context->in.size = 0; +} +/* }}} */ - while (--p>=start) { - p[text_length] = *p; +/* {{{ static void php_output_context_dtor(php_output_context *context) + Destroy the contents of an output context */ +static inline void php_output_context_dtor(php_output_context *context) +{ + if (context->in.free && context->in.data) { + efree(context->in.data); + context->in.data = NULL; + } + if (context->out.free && context->out.data) { + efree(context->out.data); + context->out.data = NULL; } - memcpy(OG(ob_buffer), text, text_length); - OG(ob_buffer)[OG(active_ob_buffer).text_length]=0; } -#endif +/* }}} */ +/* {{{ static php_output_handler *php_output_handler_init(zval *name, size_t chunk_size, int flags) + Allocates and initializes a php_output_handler structure */ +static inline php_output_handler *php_output_handler_init_ex(zval *name, size_t chunk_size, int flags TSRMLS_DC) +{ + php_output_handler *handler; + + handler = ecalloc(1, sizeof(php_output_handler)); + Z_ADDREF_P(name); + handler->name = name; + handler->size = chunk_size; + handler->flags = flags; + handler->buffer.size = PHP_OUTPUT_HANDLER_INITBUF_SIZE(chunk_size); + handler->buffer.data = emalloc(handler->buffer.size); + + return handler; +} +/* }}} */ -/* {{{ php_ob_get_buffer - * Return the current output buffer */ -PHPAPI int php_ob_get_buffer(zval *p TSRMLS_DC) +/* {{{ static int php_output_handler_appen(php_output_handler *handler, const php_output_buffer *buf TSRMLS_DC) + Appends input to the output handlers buffer and indicates whether the buffer does not have to be processed by the output handler */ +static inline int php_output_handler_append(php_output_handler *handler, const php_output_buffer *buf TSRMLS_DC) { - if (OG(ob_nesting_level)==0) { - return FAILURE; + if (buf->used) { + OG(flags) |= PHP_OUTPUT_WRITTEN; + /* store it away */ + if ((handler->buffer.size - handler->buffer.used) <= buf->used) { + size_t grow_int = PHP_OUTPUT_HANDLER_INITBUF_SIZE(handler->size); + size_t grow_buf = PHP_OUTPUT_HANDLER_INITBUF_SIZE(buf->used - (handler->buffer.size - handler->buffer.used)); + size_t grow_max = MAX(grow_int, grow_buf); + + handler->buffer.data = erealloc(handler->buffer.data, handler->buffer.size + grow_max); + handler->buffer.size += grow_max; + } + memcpy(handler->buffer.data + handler->buffer.used, buf->data, buf->used); + handler->buffer.used += buf->used; + + /* chunked buffering */ + if (handler->size && (handler->buffer.used >= handler->size)) { + /* store away errors and/or any intermediate output */ + return OG(running) ? 1 : 0; + } } - ZVAL_STRINGL(p, OG(active_ob_buffer).buffer, OG(active_ob_buffer).text_length, 1); - return SUCCESS; + return 1; } /* }}} */ -/* {{{ php_ob_get_length - * Return the size of the current output buffer */ -PHPAPI int php_ob_get_length(zval *p TSRMLS_DC) +/* {{{ static php_output_handler_status_t php_output_handler_op(php_output_handler *handler, php_output_context *context) + Output handler operation dispatcher, applying context op to the php_output_handler handler */ +static inline php_output_handler_status_t php_output_handler_op(php_output_handler *handler, php_output_context *context) { - if (OG(ob_nesting_level) == 0) { - return FAILURE; + php_output_handler_status_t status; + int original_op = context->op; + PHP_OUTPUT_TSRMLS(context); + +#if PHP_OUTPUT_DEBUG + fprintf(stderr, ">>> op(%d, " + "handler=%p, " + "name=%s, " + "flags=%d, " + "buffer.data=%s, " + "buffer.used=%zu, " + "buffer.size=%zu, " + "in.data=%s, " + "in.used=%zu)\n", + context->op, + handler, + Z_STRVAL_P(handler->name), + handler->flags, + handler->buffer.used?handler->buffer.data:"", + handler->buffer.used, + handler->buffer.size, + context->in.used?context->in.data:"", + context->in.used + ); +#endif + + if (php_output_lock_error(context->op TSRMLS_CC)) { + /* fatal error */ + return PHP_OUTPUT_HANDLER_FAILURE; } - ZVAL_LONG(p, OG(active_ob_buffer).text_length); - return SUCCESS; + + /* storable? */ + if (php_output_handler_append(handler, &context->in TSRMLS_CC) && !context->op) { + status = PHP_OUTPUT_HANDLER_NO_DATA; + } else { + /* need to start? */ + if (!(handler->flags & PHP_OUTPUT_HANDLER_STARTED)) { + context->op |= PHP_OUTPUT_HANDLER_START; + } + + OG(running) = handler; + if (handler->flags & PHP_OUTPUT_HANDLER_USER) { + zval *retval = NULL, *ob_data, *ob_mode; + + MAKE_STD_ZVAL(ob_data); + ZVAL_STRINGL(ob_data, handler->buffer.data, handler->buffer.used, 1); + MAKE_STD_ZVAL(ob_mode); + ZVAL_LONG(ob_mode, (long) context->op); + zend_fcall_info_argn(&handler->func.user->fci TSRMLS_CC, 2, &ob_data, &ob_mode); + +#define PHP_OUTPUT_USER_SUCCESS(retval) (retval && (Z_TYPE_P(retval) != IS_NULL) && (Z_TYPE_P(retval) != IS_BOOL || Z_BVAL_P(retval))) + if (SUCCESS == zend_fcall_info_call(&handler->func.user->fci, &handler->func.user->fcc, &retval, NULL TSRMLS_CC) && PHP_OUTPUT_USER_SUCCESS(retval)) { + /* user handler may have returned TRUE */ + status = PHP_OUTPUT_HANDLER_NO_DATA; + if (Z_TYPE_P(retval) != IS_BOOL) { + convert_to_string_ex(&retval); + if (Z_STRLEN_P(retval)) { + context->out.data = estrndup(Z_STRVAL_P(retval), Z_STRLEN_P(retval)); + context->out.used = Z_STRLEN_P(retval); + context->out.free = 1; + status = PHP_OUTPUT_HANDLER_SUCCESS; + } + } + } else { + /* call failed, pass internal buffer along */ + status = PHP_OUTPUT_HANDLER_FAILURE; + } + + zend_fcall_info_argn(&handler->func.user->fci TSRMLS_CC, 0); + zval_ptr_dtor(&ob_data); + zval_ptr_dtor(&ob_mode); + if (retval) { + zval_ptr_dtor(&retval); + } + + } else { + + context->in.data = handler->buffer.data; + context->in.used = handler->buffer.used; + context->in.free = 0; + + if (SUCCESS == handler->func.internal(&handler->opaq, context)) { + if (context->out.used) { + status = PHP_OUTPUT_HANDLER_SUCCESS; + } else { + status = PHP_OUTPUT_HANDLER_NO_DATA; + } + } else { + status = PHP_OUTPUT_HANDLER_FAILURE; + } + } + handler->flags |= PHP_OUTPUT_HANDLER_STARTED; + OG(running) = NULL; + } + + switch (status) { + case PHP_OUTPUT_HANDLER_FAILURE: + /* disable this handler */ + handler->flags |= PHP_OUTPUT_HANDLER_DISABLED; + /* discard any output */ + if (context->out.data && context->out.free) { + efree(context->out.data); + } + /* returns handlers buffer */ + context->out.data = handler->buffer.data; + context->out.used = handler->buffer.used; + context->out.free = 1; + handler->buffer.data = NULL; + handler->buffer.used = 0; + handler->buffer.size = 0; + break; + case PHP_OUTPUT_HANDLER_SUCCESS: + /* no more buffered data */ + handler->buffer.used = 0; + break; + case PHP_OUTPUT_HANDLER_NO_DATA: + /* handler ate all */ + php_output_context_reset(context); + break; + } + + context->op = original_op; + return status; } /* }}} */ -/* - * Wrapper functions - implementation - */ +/* {{{ static void php_output_op(int op, const char *str, size_t len TSRMLS_DC) + Output op dispatcher, passes input and output handlers output through the output handler stack until it gets written to the SAPI */ +static inline void php_output_op(int op, const char *str, size_t len TSRMLS_DC) +{ + php_output_context context; + php_output_handler **active; + int obh_cnt; + + if (php_output_lock_error(op TSRMLS_CC)) { + return; + } + + php_output_context_init(&context, op TSRMLS_CC); + + /* + * broken up for better performance: + * - apply op to the one active handler; note that OG(active) might be popped off the stack on a flush + * - or apply op to the handler stack + */ + if (OG(active) && (obh_cnt = zend_stack_count(OG(handlers)))) { + context.in.data = (char *) str; + context.in.used = len; + + if (obh_cnt > 1) { + zend_stack_apply_with_argument(OG(handlers), ZEND_STACK_APPLY_TOPDOWN, php_output_stack_apply_op, &context); + } else if ((SUCCESS == zend_stack_top(OG(handlers), (void *) &active)) && (!((*active)->flags & PHP_OUTPUT_HANDLER_DISABLED))) { + php_output_handler_op(*active, &context); + } else { + php_output_context_pass(&context); + } + } else { + context.out.data = (char *) str; + context.out.used = len; + } + + if (context.out.data && context.out.used) { +#if PHP_OUTPUT_DEBUG + fprintf(stderr, "::: sapi_write('%s', %zu)\n", context.out.data, context.out.used); +#endif + if (!SG(headers_sent) && php_header(TSRMLS_C)) { + if (zend_is_compiling(TSRMLS_C)) { + OG(output_start_filename) = zend_get_compiled_filename(TSRMLS_C); + OG(output_start_lineno) = zend_get_compiled_lineno(TSRMLS_C); + } else if (zend_is_executing(TSRMLS_C)) { + OG(output_start_filename) = zend_get_executed_filename(TSRMLS_C); + OG(output_start_lineno) = zend_get_executed_lineno(TSRMLS_C); + } +#if PHP_OUTPUT_DEBUG + fprintf(stderr, "!!! output started at: %s (%d)\n", OG(output_start_filename), OG(output_start_lineno)); +#endif + } + sapi_module.ub_write(context.out.data, context.out.used TSRMLS_CC); + if (OG(flags) & PHP_OUTPUT_IMPLICITFLUSH) { + sapi_flush(TSRMLS_C); + } + OG(flags) |= PHP_OUTPUT_SENT; + } + php_output_context_dtor(&context); +} +/* }}} */ -/* buffered output function */ -static int php_b_body_write(const char *str, uint str_length TSRMLS_DC) +/* {{{ static int php_output_stack_apply_op(void *h, void *c) + Operation callback for the stack apply function */ +static int php_output_stack_apply_op(void *h, void *c) { - php_ob_append(str, str_length TSRMLS_CC); - return str_length; + int was_disabled; + php_output_handler_status_t status; + php_output_handler *handler = *(php_output_handler **) h; + php_output_context *context = (php_output_context *) c; + + if ((was_disabled = (handler->flags & PHP_OUTPUT_HANDLER_DISABLED))) { + status = PHP_OUTPUT_HANDLER_FAILURE; + } else { + status = php_output_handler_op(handler, context); + } + + /* + * handler ate all => break + * handler returned data or failed resp. is disabled => continue + */ + switch (status) { + case PHP_OUTPUT_HANDLER_NO_DATA: + return 1; + + case PHP_OUTPUT_HANDLER_SUCCESS: + /* swap contexts buffers, unless this is the last handler in the stack */ + if (handler->level) { + php_output_context_swap(context); + } + return 0; + + case PHP_OUTPUT_HANDLER_FAILURE: + default: + if (was_disabled) { + /* pass input along, if it's the last handler in the stack */ + if (!handler->level) { + php_output_context_pass(context); + } + } else { + /* swap buffers, unless this is the last handler */ + if (handler->level) { + php_output_context_swap(context); + } + } + return 0; + } } +/* }}} */ -/* {{{ php_ub_body_write_no_header - */ -PHPAPI int php_ub_body_write_no_header(const char *str, uint str_length TSRMLS_DC) +/* {{{ static int php_output_stack_apply_clean(void *h, void *c) + Clean callback for the stack apply function */ +static int php_output_stack_apply_clean(void *h, void *c) { - int result; + php_output_handler *handler = *(php_output_handler **) h; + php_output_context *context = (php_output_context *) c; + + handler->buffer.used = 0; + php_output_handler_op(handler, context); + php_output_context_reset(context); + return 0; +} +/* }}} */ - if (OG(disable_output)) { - return 0; - } +/* {{{ static int php_output_stack_apply_list(void *h, void *z) + List callback for the stack apply function */ +static int php_output_stack_apply_list(void *h, void *z) +{ + php_output_handler *handler = *(php_output_handler **) h; + zval *array = (zval *) z; + + Z_ADDREF_P(handler->name); + add_next_index_zval(array, handler->name); + return 0; +} +/* }}} */ - result = OG(php_header_write)(str, str_length TSRMLS_CC); +/* {{{ static int php_output_stack_apply_status(void *h, void *z) + Status callback for the stack apply function */ +static int php_output_stack_apply_status(void *h, void *z) +{ + php_output_handler *handler = *(php_output_handler **) h; + zval *array = (zval *) z; + + add_next_index_zval(array, php_output_handler_status(handler, NULL)); + + return 0; +} +/* }}} */ - if (OG(implicit_flush)) { - sapi_flush(TSRMLS_C); +/* {{{ static zval *php_output_handler_status(php_output_handler *handler, zval *entry) + Returns an array with the status of the output handler */ +static inline zval *php_output_handler_status(php_output_handler *handler, zval *entry) +{ + if (!entry) { + MAKE_STD_ZVAL(entry); + array_init(entry); } - - return result; + + Z_ADDREF_P(handler->name); + add_assoc_zval(entry, "name", handler->name); + add_assoc_long(entry, "type", (long) (handler->flags & 0xf)); + add_assoc_long(entry, "flags", (long) handler->flags); + add_assoc_long(entry, "level", (long) handler->level); + add_assoc_long(entry, "chunk_size", (long) handler->size); + add_assoc_long(entry, "buffer_size", (long) handler->buffer.size); + add_assoc_long(entry, "buffer_used", (long) handler->buffer.used); + + return entry; } /* }}} */ -/* {{{ php_ub_body_write - */ -PHPAPI int php_ub_body_write(const char *str, uint str_length TSRMLS_DC) +/* {{{ static int php_output_stack_pop(int flags TSRMLS_DC) + Pops an output handler off the stack */ +static inline int php_output_stack_pop(int flags TSRMLS_DC) { - int result = 0; - - if (SG(request_info).headers_only) { - if(SG(headers_sent)) { - return 0; + php_output_context context; + php_output_handler **current, *orphan = OG(active); + + if (!orphan) { + if (!(flags & PHP_OUTPUT_POP_SILENT)) { + php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to %s buffer. No buffer to %s", (flags&PHP_OUTPUT_POP_DISCARD)?"discard":"send", (flags&PHP_OUTPUT_POP_DISCARD)?"discard":"send"); } - php_header(TSRMLS_C); - zend_bailout(); - } - if (php_header(TSRMLS_C)) { - if (zend_is_compiling(TSRMLS_C)) { - OG(output_start_filename) = zend_get_compiled_filename(TSRMLS_C); - OG(output_start_lineno) = zend_get_compiled_lineno(TSRMLS_C); - } else if (zend_is_executing(TSRMLS_C)) { - OG(output_start_filename) = zend_get_executed_filename(TSRMLS_C); - OG(output_start_lineno) = zend_get_executed_lineno(TSRMLS_C); + return 0; + } else if (!(flags & PHP_OUTPUT_POP_FORCE) && !(orphan->flags & PHP_OUTPUT_HANDLER_REMOVABLE)) { + if (!(flags & PHP_OUTPUT_POP_SILENT)) { + php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to %s buffer of %s (%d)", (flags&PHP_OUTPUT_POP_DISCARD)?"discard":"send", Z_STRVAL_P(orphan->name), orphan->level); + } + return 0; + } else { + php_output_context_init(&context, PHP_OUTPUT_HANDLER_FINAL TSRMLS_CC); + + /* don't run the output handler if it's disabled */ + if (!(orphan->flags & PHP_OUTPUT_HANDLER_DISABLED)) { + /* didn't it start yet? */ + if (!(orphan->flags & PHP_OUTPUT_HANDLER_STARTED)) { + context.op |= PHP_OUTPUT_HANDLER_START; + } + /* signal that we're cleaning up */ + if (flags & PHP_OUTPUT_POP_DISCARD) { + context.op |= PHP_OUTPUT_HANDLER_CLEAN; + orphan->buffer.used = 0; + } + php_output_handler_op(orphan, &context); + } + + /* pop it off the stack */ + zend_stack_del_top(OG(handlers)); + if (SUCCESS == zend_stack_top(OG(handlers), (void *) ¤t)) { + OG(active) = *current; + } else { + OG(active) = NULL; + } + + /* pass output along */ + if (context.out.data && context.out.used && !(flags & PHP_OUTPUT_POP_DISCARD)) { + php_output_write(context.out.data, context.out.used TSRMLS_CC); } + + /* destroy the handler (after write!) */ + php_output_handler_free(&orphan TSRMLS_CC); + php_output_context_dtor(&context); + + return 1; + } +} +/* }}} */ - OG(php_body_write) = php_ub_body_write_no_header; - result = php_ub_body_write_no_header(str, str_length TSRMLS_CC); +/* {{{ static SUCCESS|FAILURE php_output_handler_compat_func(void *ctx, php_output_context *) + php_output_handler_context_func_t for php_output_handler_func_t output handlers */ +static int php_output_handler_compat_func(void **handler_context, php_output_context *output_context) +{ + php_output_handler_func_t func = *(php_output_handler_func_t *) handler_context; + PHP_OUTPUT_TSRMLS(output_context); + + if (func) { + func(output_context->in.data, output_context->in.used, &output_context->out.data, &output_context->out.used, output_context->op TSRMLS_CC); + output_context->out.free = 1; + return SUCCESS; } + return FAILURE; +} +/* }}} */ - return result; +/* {{{ static SUCCESS|FAILURE php_output_handler_default_func(void *ctx, php_output_context *) + Default output handler */ +static int php_output_handler_default_func(void **handler_context, php_output_context *output_context) +{ + output_context->out.data = output_context->in.data; + output_context->out.used = output_context->in.used; + output_context->out.free = output_context->in.free; + output_context->in.data = NULL; + output_context->in.used = 0; + output_context->in.free = 0; + return SUCCESS; +} +/* }}} */ + +/* {{{ static SUCCESS|FAILURE php_output_handler_devnull_func(void *ctx, php_output_context *) + Null output handler */ +static int php_output_handler_devnull_func(void **handler_context, php_output_context *output_context) +{ + return SUCCESS; } /* }}} */ /* - * HEAD support + * USERLAND (nearly 1:1 of old output.c) */ -/* {{{ proto bool ob_start([ string|array user_function [, int chunk_size [, bool erase]]]) +/* {{{ proto bool ob_start([string|array user_function [, int chunk_size [, int flags]]]) U Turn on Output Buffering (specifying an optional output handler). */ PHP_FUNCTION(ob_start) { - zval *output_handler=NULL; - long chunk_size=0; - zend_bool erase=1; - - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|zlb", &output_handler, &chunk_size, &erase) == FAILURE) { - return; + zval *output_handler = NULL; + long chunk_size = 0; + long flags = PHP_OUTPUT_HANDLER_STDFLAGS; + + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z/ll", &output_handler, &chunk_size, &flags)) { + RETURN_FALSE; } - - if (chunk_size < 0) + if (chunk_size < 0) { chunk_size = 0; - - if (php_start_ob_buffer(output_handler, chunk_size, erase TSRMLS_CC)==FAILURE) { + } + + if (SUCCESS != php_output_start_user(output_handler, chunk_size, flags TSRMLS_CC)) { + php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to create buffer"); RETURN_FALSE; } RETURN_TRUE; } /* }}} */ -/* {{{ proto bool ob_flush(void) +/* {{{ proto bool ob_flush(void) U Flush (send) contents of the output buffer. The last buffer content is sent to next buffer */ PHP_FUNCTION(ob_flush) { if (zend_parse_parameters_none() == FAILURE) { return; } - - if (!OG(ob_nesting_level)) { - php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to flush buffer. No buffer to flush."); + + if (!OG(active)) { + php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to flush buffer. No buffer to flush"); + RETURN_FALSE; + } + if (SUCCESS != php_output_flush(TSRMLS_C)) { + php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to flush buffer of %s (%d)", Z_STRVAL_P(OG(active)->name), OG(active)->level); RETURN_FALSE; } - - php_end_ob_buffer(1, 1 TSRMLS_CC); RETURN_TRUE; } /* }}} */ -/* {{{ proto bool ob_clean(void) +/* {{{ proto bool ob_clean(void) U Clean (delete) the current output buffer */ PHP_FUNCTION(ob_clean) { @@ -789,22 +1301,19 @@ PHP_FUNCTION(ob_clean) return; } - if (!OG(ob_nesting_level)) { - php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer. No buffer to delete."); + if (!OG(active)) { + php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer. No buffer to delete"); RETURN_FALSE; } - - if (!OG(active_ob_buffer).status && !OG(active_ob_buffer).erase) { - php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer %s.", OG(active_ob_buffer).handler_name); + if (SUCCESS != php_output_clean(TSRMLS_C)) { + php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer of %s (%d)", Z_STRVAL_P(OG(active)->name), OG(active)->level); RETURN_FALSE; } - - php_end_ob_buffer(0, 1 TSRMLS_CC); RETURN_TRUE; } /* }}} */ -/* {{{ proto bool ob_end_flush(void) +/* {{{ proto bool ob_end_flush(void) U Flush (send) the output buffer, and delete current output buffer */ PHP_FUNCTION(ob_end_flush) { @@ -812,234 +1321,156 @@ PHP_FUNCTION(ob_end_flush) return; } - if (!OG(ob_nesting_level)) { - php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete and flush buffer. No buffer to delete or flush."); - RETURN_FALSE; - } - if (OG(ob_nesting_level) && !OG(active_ob_buffer).status && !OG(active_ob_buffer).erase) { - php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer %s.", OG(active_ob_buffer).handler_name); + if (!OG(active)) { + php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete and flush buffer. No buffer to delete or flush"); RETURN_FALSE; } - - php_end_ob_buffer(1, 0 TSRMLS_CC); - RETURN_TRUE; + RETURN_BOOL(SUCCESS == php_output_end(TSRMLS_C)); } /* }}} */ -/* {{{ proto bool ob_end_clean(void) +/* {{{ proto bool ob_end_clean(void) U Clean the output buffer, and delete current output buffer */ PHP_FUNCTION(ob_end_clean) { if (zend_parse_parameters_none() == FAILURE) { return; } - - if (!OG(ob_nesting_level)) { - php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer. No buffer to delete."); - RETURN_FALSE; - } - if (OG(ob_nesting_level) && !OG(active_ob_buffer).status && !OG(active_ob_buffer).erase) { - php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer %s.", OG(active_ob_buffer).handler_name); + + if (!OG(active)) { + php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer. No buffer to delete"); RETURN_FALSE; } - - php_end_ob_buffer(0, 0 TSRMLS_CC); - RETURN_TRUE; + RETURN_BOOL(SUCCESS == php_output_discard(TSRMLS_C)); } /* }}} */ -/* {{{ proto bool ob_get_flush(void) +/* {{{ proto bool ob_get_flush(void) U Get current buffer contents, flush (send) the output buffer, and delete current output buffer */ PHP_FUNCTION(ob_get_flush) { if (zend_parse_parameters_none() == FAILURE) { return; } - - /* get contents */ - if (php_ob_get_buffer(return_value TSRMLS_CC)==FAILURE) { - RETURN_FALSE; - } - /* error checks */ - if (!OG(ob_nesting_level)) { - php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete and flush buffer. No buffer to delete or flush."); + + if (SUCCESS != php_output_get_contents(return_value TSRMLS_CC)) { + php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete and flush buffer. No buffer to delete or flush"); RETURN_FALSE; } - if (OG(ob_nesting_level) && !OG(active_ob_buffer).status && !OG(active_ob_buffer).erase) { - php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer %s.", OG(active_ob_buffer).handler_name); - RETURN_FALSE; + if (SUCCESS != php_output_end(TSRMLS_C)) { + php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer of %s (%d)", Z_STRVAL_P(OG(active)->name), OG(active)->level); } - /* flush */ - php_end_ob_buffer(1, 0 TSRMLS_CC); } /* }}} */ -/* {{{ proto bool ob_get_clean(void) +/* {{{ proto bool ob_get_clean(void) U Get current buffer contents and delete current output buffer */ PHP_FUNCTION(ob_get_clean) { if (zend_parse_parameters_none() == FAILURE) { return; } - /* get contents */ - if (php_ob_get_buffer(return_value TSRMLS_CC)==FAILURE) { - RETURN_FALSE; - } - /* error checks */ - if (!OG(ob_nesting_level)) { - php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer. No buffer to delete."); + + if (SUCCESS != php_output_get_contents(return_value TSRMLS_CC)) { + php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer. No buffer to delete"); RETURN_FALSE; } - if (OG(ob_nesting_level) && !OG(active_ob_buffer).status && !OG(active_ob_buffer).erase) { - php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer %s.", OG(active_ob_buffer).handler_name); - RETURN_FALSE; + if (SUCCESS != php_output_discard(TSRMLS_C)) { + php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer of %s (%d)", Z_STRVAL_P(OG(active)->name), OG(active)->level); } - /* delete buffer */ - php_end_ob_buffer(0, 0 TSRMLS_CC); } /* }}} */ -/* {{{ proto string ob_get_contents(void) +/* {{{ proto string ob_get_contents(void) U Return the contents of the output buffer */ PHP_FUNCTION(ob_get_contents) { if (zend_parse_parameters_none() == FAILURE) { return; } - - if (php_ob_get_buffer(return_value TSRMLS_CC)==FAILURE) { + if (SUCCESS != php_output_get_contents(return_value TSRMLS_CC)) { RETURN_FALSE; } } /* }}} */ -/* {{{ proto int ob_get_level(void) +/* {{{ proto int ob_get_level(void) U Return the nesting level of the output buffer */ PHP_FUNCTION(ob_get_level) { if (zend_parse_parameters_none() == FAILURE) { return; } - - RETURN_LONG (OG(ob_nesting_level)); + RETURN_LONG(php_output_get_level(TSRMLS_C)); } /* }}} */ -/* {{{ proto int ob_get_length(void) +/* {{{ proto int ob_get_length(void) U Return the length of the output buffer */ PHP_FUNCTION(ob_get_length) { if (zend_parse_parameters_none() == FAILURE) { return; } - - if (php_ob_get_length(return_value TSRMLS_CC)==FAILURE) { + if (SUCCESS != php_output_get_length(return_value TSRMLS_CC)) { RETURN_FALSE; } } /* }}} */ -/* {{{ int php_ob_buffer_status(php_ob_buffer *ob_buffer, zval *result) */ -static int php_ob_buffer_status(php_ob_buffer *ob_buffer, zval *result) +/* {{{ proto false|array ob_list_handlers() U + * List all output_buffers in an array + */ +PHP_FUNCTION(ob_list_handlers) { - zval *elem; + if (zend_parse_parameters_none() == FAILURE) { + return; + } - MAKE_STD_ZVAL(elem); - array_init(elem); + array_init(return_value); - add_assoc_long(elem, "chunk_size", ob_buffer->chunk_size); - if (!ob_buffer->chunk_size) { - add_assoc_long(elem, "size", ob_buffer->size); - add_assoc_long(elem, "block_size", ob_buffer->block_size); - } - if (ob_buffer->internal_output_handler) { - add_assoc_long(elem, "type", PHP_OUTPUT_HANDLER_INTERNAL); - add_assoc_long(elem, "buffer_size", ob_buffer->internal_output_handler_buffer_size); - } - else { - add_assoc_long(elem, "type", PHP_OUTPUT_HANDLER_USER); + if (!OG(active)) { + return; } - add_assoc_long(elem, "status", ob_buffer->status); - add_assoc_string(elem, "name", ob_buffer->handler_name, 1); - add_assoc_bool(elem, "del", ob_buffer->erase); - add_next_index_zval(result, elem); - - return SUCCESS; + + zend_stack_apply_with_argument(OG(handlers), ZEND_STACK_APPLY_BOTTOMUP, php_output_stack_apply_list, return_value); } /* }}} */ - -/* {{{ proto false|array ob_get_status([bool full_status]) +/* {{{ proto false|array ob_get_status([bool full_status]) U Return the status of the active or all output buffers */ PHP_FUNCTION(ob_get_status) { zend_bool full_status = 0; - - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &full_status) == FAILURE ) { - return; + + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &full_status)) { + RETURN_FALSE; } - + if (!OG(active)) { + RETURN_FALSE; + } + array_init(return_value); - if (full_status) { - if (OG(ob_nesting_level)>1) { - zend_stack_apply_with_argument(&OG(ob_buffers), ZEND_STACK_APPLY_BOTTOMUP, (int (*)(void *elem, void *))php_ob_buffer_status, return_value); - } - if (OG(ob_nesting_level)>0 && php_ob_buffer_status(&OG(active_ob_buffer), return_value)==FAILURE) { - RETURN_FALSE; - } - } else if (OG(ob_nesting_level)>0) { - add_assoc_long(return_value, "level", OG(ob_nesting_level)); - if (OG(active_ob_buffer).internal_output_handler) { - add_assoc_long(return_value, "type", PHP_OUTPUT_HANDLER_INTERNAL); - } else { - add_assoc_long(return_value, "type", PHP_OUTPUT_HANDLER_USER); - } - add_assoc_long(return_value, "status", OG(active_ob_buffer).status); - add_assoc_string(return_value, "name", OG(active_ob_buffer).handler_name, 1); - add_assoc_bool(return_value, "del", OG(active_ob_buffer).erase); + zend_stack_apply_with_argument(OG(handlers), ZEND_STACK_APPLY_BOTTOMUP, php_output_stack_apply_status, return_value); + } else { + php_output_handler_status(OG(active), return_value); } } /* }}} */ - -/* {{{ proto void ob_implicit_flush([int flag]) +/* {{{ proto void ob_implicit_flush([int flag]) U Turn implicit flush on/off and is equivalent to calling flush() after every output call */ PHP_FUNCTION(ob_implicit_flush) { long flag = 1; - - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &flag) == FAILURE) { - return; - } - - if (flag) { - php_start_implicit_flush(TSRMLS_C); - } else { - php_end_implicit_flush(TSRMLS_C); + + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &flag)) { + php_output_set_implicit_flush(flag TSRMLS_CC); } } /* }}} */ - -/* {{{ char *php_get_output_start_filename(TSRMLS_D) - Return filename start output something */ -PHPAPI char *php_get_output_start_filename(TSRMLS_D) -{ - return OG(output_start_filename); -} -/* }}} */ - - -/* {{{ char *php_get_output_start_lineno(TSRMLS_D) - Return line number start output something */ -PHPAPI int php_get_output_start_lineno(TSRMLS_D) -{ - return OG(output_start_lineno); -} -/* }}} */ - - /* {{{ proto bool output_reset_rewrite_vars(void) Reset(clear) URL rewriter values */ PHP_FUNCTION(output_reset_rewrite_vars) @@ -1052,7 +1483,6 @@ PHP_FUNCTION(output_reset_rewrite_vars) } /* }}} */ - /* {{{ proto bool output_add_rewrite_var(string name, string value) Add URL rewriter values */ PHP_FUNCTION(output_add_rewrite_var) @@ -1061,7 +1491,7 @@ PHP_FUNCTION(output_add_rewrite_var) int name_len, value_len; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &name, &name_len, &value, &value_len) == FAILURE) { - return; + RETURN_FALSE; } if (php_url_scanner_add_var(name, name_len, value, value_len, 1 TSRMLS_CC) == SUCCESS) { Index: main/php.h =================================================================== RCS file: /repository/php-src/main/php.h,v retrieving revision 1.221.2.4.2.8.2.10 diff -u -p -d -r1.221.2.4.2.8.2.10 php.h --- main/php.h 22 Aug 2008 12:59:46 -0000 1.221.2.4.2.8.2.10 +++ main/php.h 18 Sep 2008 18:38:19 -0000 @@ -376,20 +376,6 @@ END_EXTERN_C() /* Output support */ #include "main/php_output.h" -#define PHPWRITE(str, str_len) php_body_write((str), (str_len) TSRMLS_CC) -#define PUTS(str) do { \ - const char *__str = (str); \ - php_body_write(__str, strlen(__str) TSRMLS_CC); \ -} while (0) - -#define PUTC(c) (php_body_write(&(c), 1 TSRMLS_CC), (c)) -#define PHPWRITE_H(str, str_len) php_header_write((str), (str_len) TSRMLS_CC) -#define PUTS_H(str) do { \ - const char *__str = (str); \ - php_header_write(__str, strlen(__str) TSRMLS_CC); \ -} while (0) - -#define PUTC_H(c) (php_header_write(&(c), 1 TSRMLS_CC), (c)) #include "php_streams.h" #include "php_memory_streams.h" Index: main/php_output.h =================================================================== RCS file: /repository/php-src/main/php_output.h,v retrieving revision 1.53.2.1.2.1.2.1 diff -u -p -d -r1.53.2.1.2.1.2.1 php_output.h --- main/php_output.h 31 Dec 2007 07:17:17 -0000 1.53.2.1.2.1.2.1 +++ main/php_output.h 18 Sep 2008 18:38:19 -0000 @@ -12,44 +12,274 @@ | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ - | Author: Zeev Suraski | + | Author: Michael Wallner | +----------------------------------------------------------------------+ */ -/* $Id: php_output.h,v 1.53.2.1.2.1.2.1 2007/12/31 07:17:17 sebastian Exp $ */ +/* $Id: php_output.h,v 1.72 2007/12/31 07:12:18 sebastian Exp $ */ #ifndef PHP_OUTPUT_H #define PHP_OUTPUT_H +#define PHP_OUTPUT_NEWAPI 1 + +/* handler ops */ +#define PHP_OUTPUT_HANDLER_WRITE 0x00 /* standard passthru */ +#define PHP_OUTPUT_HANDLER_START 0x01 /* start */ +#define PHP_OUTPUT_HANDLER_CLEAN 0x02 /* restart */ +#define PHP_OUTPUT_HANDLER_FLUSH 0x04 /* pass along as much as possible */ +#define PHP_OUTPUT_HANDLER_FINAL 0x08 /* finalize */ +#define PHP_OUTPUT_HANDLER_CONT PHP_OUTPUT_HANDLER_WRITE +#define PHP_OUTPUT_HANDLER_END PHP_OUTPUT_HANDLER_FINAL + +/* handler types */ +#define PHP_OUTPUT_HANDLER_INTERNAL 0x0000 +#define PHP_OUTPUT_HANDLER_USER 0x0001 + +/* handler ability flags */ +#define PHP_OUTPUT_HANDLER_CLEANABLE 0x0010 +#define PHP_OUTPUT_HANDLER_FLUSHABLE 0x0020 +#define PHP_OUTPUT_HANDLER_REMOVABLE 0x0040 +#define PHP_OUTPUT_HANDLER_STDFLAGS 0x0070 + +/* handler status flags */ +#define PHP_OUTPUT_HANDLER_STARTED 0x1000 +#define PHP_OUTPUT_HANDLER_DISABLED 0x2000 + +/* handler op return values */ +typedef enum _php_output_handler_status_t { + PHP_OUTPUT_HANDLER_FAILURE, + PHP_OUTPUT_HANDLER_SUCCESS, + PHP_OUTPUT_HANDLER_NO_DATA, +} php_output_handler_status_t; + +/* php_output_stack_pop() flags */ +#define PHP_OUTPUT_POP_TRY 0x000 +#define PHP_OUTPUT_POP_FORCE 0x001 +#define PHP_OUTPUT_POP_DISCARD 0x010 +#define PHP_OUTPUT_POP_SILENT 0x100 + +/* real global flags */ +#define PHP_OUTPUT_IMPLICITFLUSH 0x01 +#define PHP_OUTPUT_DISABLED 0x02 +#define PHP_OUTPUT_WRITTEN 0x04 +#define PHP_OUTPUT_SENT 0x08 +/* supplementary flags for php_output_get_status() */ +#define PHP_OUTPUT_ACTIVE 0x10 +#define PHP_OUTPUT_LOCKED 0x20 + +/* handler hooks */ +typedef enum _php_output_handler_hook_t { + PHP_OUTPUT_HANDLER_HOOK_GET_OPAQ, + PHP_OUTPUT_HANDLER_HOOK_GET_FLAGS, + PHP_OUTPUT_HANDLER_HOOK_GET_LEVEL, + PHP_OUTPUT_HANDLER_HOOK_IMMUTABLE, + PHP_OUTPUT_HANDLER_HOOK_DISABLE, + /* unused */ + PHP_OUTPUT_HANDLER_HOOK_LAST, +} php_output_handler_hook_t; + +#define PHP_OUTPUT_HANDLER_INITBUF_SIZE(s) \ +( (s) ? \ + (s) + PHP_OUTPUT_HANDLER_ALIGNTO_SIZE - ((s) % (PHP_OUTPUT_HANDLER_ALIGNTO_SIZE)) : \ + PHP_OUTPUT_HANDLER_DEFAULT_SIZE \ +) +#define PHP_OUTPUT_HANDLER_ALIGNTO_SIZE 0x1000 +#define PHP_OUTPUT_HANDLER_DEFAULT_SIZE 0x4000 + +typedef struct _php_output_buffer { + char *data; + size_t size; + size_t used; + uint free:1; + uint _res:31; +} php_output_buffer; + +typedef struct _php_output_context { + int op; + php_output_buffer in; + php_output_buffer out; +#ifdef ZTS + void ***tsrm_ls; +#endif +} php_output_context; + +#define PHP_OUTPUT_TSRMLS(ctx) TSRMLS_FETCH_FROM_CTX((ctx)->tsrm_ls) + +/* old-style, stateless callback */ typedef void (*php_output_handler_func_t)(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC); +/* new-style, opaque context callback */ +typedef int (*php_output_handler_context_func_t)(void **handler_context, php_output_context *output_context); +/* output handler context dtor */ +typedef void (*php_output_handler_context_dtor_t)(void *opaq TSRMLS_DC); +/* conflict check callback */ +typedef int (*php_output_handler_conflict_check_t)(zval *handler_name TSRMLS_DC); +/* ctor for aliases */ +typedef struct _php_output_handler *(*php_output_handler_alias_ctor_t)(zval *handler_name, size_t chunk_size, int flags TSRMLS_DC); + +typedef struct _php_output_handler_user_func_t { + zend_fcall_info fci; + zend_fcall_info_cache fcc; + zval *zoh; +} php_output_handler_user_func_t; + +typedef struct _php_output_handler { + zval *name; + int flags; + int level; + size_t size; + php_output_buffer buffer; + + void *opaq; + void (*dtor)(void *opaq TSRMLS_DC); + + union { + php_output_handler_user_func_t *user; + php_output_handler_context_func_t internal; + } func; +} php_output_handler; + +ZEND_BEGIN_MODULE_GLOBALS(output) + int flags; + zend_stack *handlers; + php_output_handler *active; + php_output_handler *running; + char *output_start_filename; + int output_start_lineno; + zval *default_output_handler_name; + zval *devnull_output_handler_name; +ZEND_END_MODULE_GLOBALS(output); + +/* there should not be a need to use OG() from outside of output.c */ +#ifdef ZTS +#define OG(v) TSRMG(output_globals_id, zend_output_globals *, v) +#else +#define OG(v) (output_globals.v) +#endif + +/* convenience macros */ +#define PHPWRITE(str, str_len) php_output_write((str), (str_len) TSRMLS_CC) +#define PHPWRITE_H(str, str_len) php_output_write_unbuffered((str), (str_len) TSRMLS_CC) + +#define PUTC(c) (php_output_write(&(c), 1 TSRMLS_CC), (c)) +#define PUTC_H(c) (php_output_write_unbuffered(&(c), 1 TSRMLS_CC), (c)) + +#define PUTS(str) do { \ + const char *__str = (str); \ + php_output_write(__str, strlen(__str) TSRMLS_CC); \ +} while (0) +#define PUTS_H(str) do { \ + const char *__str = (str); \ + php_output_write_unbuffered(__str, strlen(__str) TSRMLS_CC); \ +} while (0) + BEGIN_EXTERN_C() +#define php_output_tearup() \ + php_output_startup(); \ + php_output_activate(TSRMLS_C) +#define php_output_teardown() \ + php_output_end_all(TSRMLS_C); \ + php_output_deactivate(TSRMLS_C); \ + php_output_shutdown() + +/* MINIT */ PHPAPI void php_output_startup(void); -PHPAPI void php_output_activate(TSRMLS_D); -PHPAPI void php_output_set_status(zend_bool status TSRMLS_DC); +/* MSHUTDOWN */ +PHPAPI void php_output_shutdown(void); + PHPAPI void php_output_register_constants(TSRMLS_D); -PHPAPI int php_default_output_func(const char *str, uint str_len TSRMLS_DC); -PHPAPI int php_ub_body_write(const char *str, uint str_length TSRMLS_DC); -PHPAPI int php_ub_body_write_no_header(const char *str, uint str_length TSRMLS_DC); -PHPAPI int php_body_write(const char *str, uint str_length TSRMLS_DC); -PHPAPI int php_header_write(const char *str, uint str_length TSRMLS_DC); -PHPAPI int php_start_ob_buffer(zval *output_handler, uint chunk_size, zend_bool erase TSRMLS_DC); -PHPAPI int php_start_ob_buffer_named(const char *output_handler_name, uint chunk_size, zend_bool erase TSRMLS_DC); -PHPAPI void php_end_ob_buffer(zend_bool send_buffer, zend_bool just_flush TSRMLS_DC); -PHPAPI void php_end_ob_buffers(zend_bool send_buffer TSRMLS_DC); -PHPAPI int php_ob_get_buffer(zval *p TSRMLS_DC); -PHPAPI int php_ob_get_length(zval *p TSRMLS_DC); -PHPAPI void php_start_implicit_flush(TSRMLS_D); -PHPAPI void php_end_implicit_flush(TSRMLS_D); -PHPAPI char *php_get_output_start_filename(TSRMLS_D); -PHPAPI int php_get_output_start_lineno(TSRMLS_D); -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); -PHPAPI int php_ob_handler_used(char *handler_name TSRMLS_DC); -PHPAPI int php_ob_init_conflict(char *handler_new, char *handler_set TSRMLS_DC); -PHPAPI int php_ob_get_buffer(zval *p TSRMLS_DC); -PHPAPI int php_ob_get_length(zval *p TSRMLS_DC); + +/* RINIT */ +PHPAPI int php_output_activate(TSRMLS_D); +/* RSHUTDOWN */ +PHPAPI void php_output_deactivate(TSRMLS_D); + +PHPAPI zval *php_output_get_default_handler_name(TSRMLS_D); +PHPAPI zval *php_output_get_devnull_handler_name(TSRMLS_D); + +PHPAPI void php_output_set_status(int status TSRMLS_DC); +PHPAPI int php_output_get_status(TSRMLS_D); +PHPAPI void php_output_set_implicit_flush(int flush TSRMLS_DC); +PHPAPI char *php_output_get_start_filename(TSRMLS_D); +PHPAPI int php_output_get_start_lineno(TSRMLS_D); + +PHPAPI int php_output_write_unbuffered(const char *str, size_t len TSRMLS_DC); +PHPAPI int php_output_write(const char *str, size_t len TSRMLS_DC); + +PHPAPI int php_output_flush(TSRMLS_D); +PHPAPI void php_output_flush_all(TSRMLS_D); +PHPAPI int php_output_clean(TSRMLS_D); +PHPAPI void php_output_clean_all(TSRMLS_D); +PHPAPI int php_output_end(TSRMLS_D); +PHPAPI void php_output_end_all(TSRMLS_D); +PHPAPI int php_output_discard(TSRMLS_D); +PHPAPI void php_output_discard_all(TSRMLS_D); + +PHPAPI int php_output_get_contents(zval *p TSRMLS_DC); +PHPAPI int php_output_get_length(zval *p TSRMLS_DC); +PHPAPI int php_output_get_level(TSRMLS_D); + +PHPAPI int php_output_start_default(TSRMLS_D); +PHPAPI int php_output_start_devnull(TSRMLS_D); + +PHPAPI int php_output_start_user(zval *output_handler, size_t chunk_size, int flags TSRMLS_DC); +PHPAPI int php_output_start_internal(zval *name, php_output_handler_func_t output_handler, size_t chunk_size, int flags TSRMLS_DC); + +PHPAPI php_output_handler *php_output_handler_create_user(zval *handler, size_t chunk_size, int flags TSRMLS_DC); +PHPAPI php_output_handler *php_output_handler_create_internal(zval *name, php_output_handler_context_func_t handler, size_t chunk_size, int flags TSRMLS_DC); + +PHPAPI void php_output_handler_set_context(php_output_handler *handler, void *opaq, void (*dtor)(void* TSRMLS_DC) TSRMLS_DC); +PHPAPI int php_output_handler_start(php_output_handler *handler TSRMLS_DC); +PHPAPI int php_output_handler_started(zval *name TSRMLS_DC); +PHPAPI int php_output_handler_hook(php_output_handler_hook_t type, void *arg TSRMLS_DC); +PHPAPI void php_output_handler_dtor(php_output_handler *handler TSRMLS_DC); +PHPAPI void php_output_handler_free(php_output_handler **handler TSRMLS_DC); + +PHPAPI int php_output_handler_conflict(zval *handler_new, zval *handler_set TSRMLS_DC); +PHPAPI int php_output_handler_conflict_register(zval *handler_name, php_output_handler_conflict_check_t check_func TSRMLS_DC); +PHPAPI int php_output_handler_reverse_conflict_register(zval *handler_name, php_output_handler_conflict_check_t check_func TSRMLS_DC); + +#define PHP_OUTPUT_CONFLICT_REGISTER(name, func) \ +{ \ + zval tmp_z; \ + char *tmp_s = (name); \ + INIT_PZVAL(&tmp_z); \ + ZVAL_STRING(&tmp_z, tmp_s, 1); \ + php_output_handler_conflict_register(&tmp_z, func TSRMLS_CC); \ + zval_dtor(&tmp_z); \ +} + +#define PHP_OUTPUT_CONFLICT(check_name, action) \ +{ \ + int tmp_i; \ + zval tmp_z; \ + char *tmp_s = (check_name); \ + INIT_PZVAL(&tmp_z); \ + ZVAL_STRING(&tmp_z, tmp_s, 1); \ + tmp_i = php_output_handler_conflict(handler_name, &tmp_z TSRMLS_CC); \ + zval_dtor(&tmp_z); \ + if (tmp_i) { \ + action; \ + } \ +} + +PHPAPI php_output_handler_alias_ctor_t *php_output_handler_alias(zval *handler_name TSRMLS_DC); +PHPAPI int php_output_handler_alias_register(zval *handler_name, php_output_handler_alias_ctor_t func TSRMLS_DC); + +#define PHP_OUTPUT_ALIAS_REGISTER(name, func) \ +{ \ + zval tmp_z; \ + char *tmp_s = (name); \ + INIT_PZVAL(&tmp_z); \ + ZVAL_STRING(&tmp_z, tmp_s, 1); \ + php_output_handler_alias_register(&tmp_z, func TSRMLS_CC); \ + zval_dtor(&tmp_z); \ +} + END_EXTERN_C() + PHP_FUNCTION(ob_start); PHP_FUNCTION(ob_flush); PHP_FUNCTION(ob_clean); @@ -64,51 +294,16 @@ PHP_FUNCTION(ob_get_status); PHP_FUNCTION(ob_implicit_flush); PHP_FUNCTION(ob_list_handlers); -typedef struct _php_ob_buffer { - char *buffer; - uint size; - uint text_length; - int block_size; - uint chunk_size; - int status; - zval *output_handler; - php_output_handler_func_t internal_output_handler; - char *internal_output_handler_buffer; - uint internal_output_handler_buffer_size; - char *handler_name; - zend_bool erase; -} php_ob_buffer; - -typedef struct _php_output_globals { - int (*php_body_write)(const char *str, uint str_length TSRMLS_DC); /* string output */ - int (*php_header_write)(const char *str, uint str_length TSRMLS_DC); /* unbuffer string output */ - php_ob_buffer active_ob_buffer; - unsigned char implicit_flush; - char *output_start_filename; - int output_start_lineno; - zend_stack ob_buffers; - int ob_nesting_level; - zend_bool ob_lock; - zend_bool disable_output; -} php_output_globals; - -#ifdef ZTS -#define OG(v) TSRMG(output_globals_id, php_output_globals *, v) -ZEND_API extern int output_globals_id; -#else -#define OG(v) (output_globals.v) -ZEND_API extern php_output_globals output_globals; -#endif - -#define PHP_OUTPUT_HANDLER_START (1<<0) -#define PHP_OUTPUT_HANDLER_CONT (1<<1) -#define PHP_OUTPUT_HANDLER_END (1<<2) - -#define PHP_OUTPUT_HANDLER_INTERNAL 0 -#define PHP_OUTPUT_HANDLER_USER 1 - PHP_FUNCTION(output_add_rewrite_var); PHP_FUNCTION(output_reset_rewrite_vars); +#endif -#endif /* PHP_OUTPUT_H */ +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: sw=4 ts=4 fdm=marker + * vim<600: sw=4 ts=4 + */ Index: sapi/apache/mod_php5.c =================================================================== RCS file: /repository/php-src/sapi/apache/Attic/mod_php5.c,v retrieving revision 1.19.2.7.2.13.2.7 diff -u -p -d -r1.19.2.7.2.13.2.7 mod_php5.c --- sapi/apache/mod_php5.c 28 Jun 2008 10:01:17 -0000 1.19.2.7.2.13.2.7 +++ sapi/apache/mod_php5.c 18 Sep 2008 18:38:19 -0000 @@ -317,7 +317,7 @@ static void php_apache_request_shutdown( { TSRMLS_FETCH(); - php_output_set_status(0 TSRMLS_CC); + php_output_set_status(PHP_OUTPUT_DISABLED); if (AP(in_request)) { AP(in_request) = 0; php_request_shutdown(dummy); Index: sapi/apache/php_apache.c =================================================================== RCS file: /repository/php-src/sapi/apache/php_apache.c,v retrieving revision 1.89.2.4.2.6.2.5 diff -u -p -d -r1.89.2.4.2.6.2.5 php_apache.c --- sapi/apache/php_apache.c 20 Jul 2008 14:28:41 -0000 1.89.2.4.2.6.2.5 +++ sapi/apache/php_apache.c 18 Sep 2008 18:38:19 -0000 @@ -13,7 +13,7 @@ | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Rasmus Lerdorf | - | Stig Sæther Bakken | + | Stig S�ther Bakken | | David Sklar | +----------------------------------------------------------------------+ */ @@ -379,7 +379,7 @@ PHP_FUNCTION(virtual) RETURN_FALSE; } - php_end_ob_buffers(1 TSRMLS_CC); + php_output_end_all(); php_header(TSRMLS_C); if (run_sub_req(rr)) { Index: sapi/apache2handler/php_functions.c =================================================================== RCS file: /repository/php-src/sapi/apache2handler/php_functions.c,v retrieving revision 1.18.2.6.2.5.2.5 diff -u -p -d -r1.18.2.6.2.5.2.5 php_functions.c --- sapi/apache2handler/php_functions.c 26 Aug 2008 17:34:16 -0000 1.18.2.6.2.5.2.5 +++ sapi/apache2handler/php_functions.c 18 Sep 2008 18:38:19 -0000 @@ -91,7 +91,7 @@ PHP_FUNCTION(virtual) } /* Flush everything. */ - php_end_ob_buffers(1 TSRMLS_CC); + php_output_end_all(); php_header(TSRMLS_C); /* Ensure that the ap_r* layer for the main request is flushed, to Index: sapi/apache_hooks/mod_php5.c =================================================================== RCS file: /repository/php-src/sapi/apache_hooks/mod_php5.c,v retrieving revision 1.11.2.1.2.5.2.3 diff -u -p -d -r1.11.2.1.2.5.2.3 mod_php5.c --- sapi/apache_hooks/mod_php5.c 18 Mar 2008 22:23:20 -0000 1.11.2.1.2.5.2.3 +++ sapi/apache_hooks/mod_php5.c 18 Sep 2008 18:38:19 -0000 @@ -437,7 +437,7 @@ static void php_apache_request_shutdown( { TSRMLS_FETCH(); AP(current_hook) = AP_CLEANUP; - php_output_set_status(0 TSRMLS_CC); + php_output_set_status(PHP_OUTPUT_DISABLED); SG(server_context) = NULL; /* The server context (request) is invalid by the time run_cleanups() is called */ if(SG(sapi_started)) { php_request_shutdown(dummy); Index: sapi/apache_hooks/php_apache.c =================================================================== RCS file: /repository/php-src/sapi/apache_hooks/php_apache.c,v retrieving revision 1.19.2.3.2.5.2.4 diff -u -p -d -r1.19.2.3.2.5.2.4 php_apache.c --- sapi/apache_hooks/php_apache.c 20 Jul 2008 14:28:41 -0000 1.19.2.3.2.5.2.4 +++ sapi/apache_hooks/php_apache.c 18 Sep 2008 18:38:19 -0000 @@ -13,7 +13,7 @@ | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Rasmus Lerdorf | - | Stig Sæther Bakken | + | Stig S�ther Bakken | | David Sklar | +----------------------------------------------------------------------+ */ @@ -1777,7 +1777,7 @@ PHP_FUNCTION(virtual) RETURN_FALSE; } - php_end_ob_buffers(1 TSRMLS_CC); + php_output_end_all(); php_header(TSRMLS_C); if (run_sub_req(rr)) { Index: sapi/cgi/cgi_main.c =================================================================== RCS file: /repository/php-src/sapi/cgi/cgi_main.c,v retrieving revision 1.267.2.15.2.50.2.28 diff -u -p -d -r1.267.2.15.2.50.2.28 cgi_main.c --- sapi/cgi/cgi_main.c 2 Sep 2008 13:22:15 -0000 1.267.2.15.2.50.2.28 +++ sapi/cgi/cgi_main.c 18 Sep 2008 18:38:19 -0000 @@ -1710,11 +1710,9 @@ consult the installation file that came case '?': fcgi_shutdown(); no_headers = 1; - php_output_startup(); - php_output_activate(TSRMLS_C); SG(headers_sent) = 1; php_cgi_usage(argv[0]); - php_end_ob_buffers(1 TSRMLS_CC); + php_output_end_all(TSRMLS_C); exit_status = 0; goto out; } @@ -1788,15 +1786,13 @@ consult the installation file that came if (script_file) { efree(script_file); } - php_output_startup(); - php_output_activate(TSRMLS_C); SG(headers_sent) = 1; php_printf("[PHP Modules]\n"); print_modules(TSRMLS_C); php_printf("\n[Zend Modules]\n"); print_extensions(TSRMLS_C); php_printf("\n"); - php_end_ob_buffers(1 TSRMLS_CC); + php_output_end_all(TSRMLS_C); exit_status = 0; goto out; @@ -1992,7 +1988,7 @@ consult the installation file that came if (open_file_for_scanning(&file_handle TSRMLS_CC) == SUCCESS) { zend_strip(TSRMLS_C); zend_file_handle_dtor(&file_handle TSRMLS_CC); - php_end_ob_buffers(1 TSRMLS_CC); + php_output_teardown(); } return SUCCESS; break; @@ -2007,7 +2003,7 @@ consult the installation file that came goto fastcgi_request_done; } zend_file_handle_dtor(&file_handle TSRMLS_CC); - php_end_ob_buffers(1 TSRMLS_CC); + php_output_teardown(); } return SUCCESS; } @@ -2018,6 +2014,7 @@ consult the installation file that came open_file_for_scanning(&file_handle TSRMLS_CC); zend_indent(); zend_file_handle_dtor(&file_handle TSRMLS_CC); + php_output_teardown(); return SUCCESS; break; #endif Index: sapi/cli/php_cli.c =================================================================== RCS file: /repository/php-src/sapi/cli/php_cli.c,v retrieving revision 1.129.2.13.2.22.2.15 diff -u -p -d -r1.129.2.13.2.22.2.15 php_cli.c --- sapi/cli/php_cli.c 14 Aug 2008 09:46:26 -0000 1.129.2.13.2.22.2.15 +++ sapi/cli/php_cli.c 18 Sep 2008 18:38:19 -0000 @@ -735,7 +735,7 @@ int main(int argc, char *argv[]) } request_started = 1; php_cli_usage(argv[0]); - php_end_ob_buffers(1 TSRMLS_CC); + php_output_end_all(TSRMLS_C); exit_status=0; goto out; @@ -745,7 +745,7 @@ int main(int argc, char *argv[]) } request_started = 1; php_print_info(0xFFFFFFFF TSRMLS_CC); - php_end_ob_buffers(1 TSRMLS_CC); + php_output_end_all(TSRMLS_C); exit_status=0; goto out; @@ -759,7 +759,7 @@ int main(int argc, char *argv[]) php_printf("\n[Zend Modules]\n"); print_extensions(TSRMLS_C); php_printf("\n"); - php_end_ob_buffers(1 TSRMLS_CC); + php_output_end_all(TSRMLS_C); exit_status=0; goto out; @@ -782,7 +782,7 @@ int main(int argc, char *argv[]) #endif get_zend_version() ); - php_end_ob_buffers(1 TSRMLS_CC); + php_output_end_all(TSRMLS_C); exit_status=0; goto out; Index: sapi/nsapi/nsapi.c =================================================================== RCS file: /repository/php-src/sapi/nsapi/nsapi.c,v retrieving revision 1.69.2.3.2.6.2.6 diff -u -p -d -r1.69.2.3.2.6.2.6 nsapi.c --- sapi/nsapi/nsapi.c 16 Jul 2008 11:59:15 -0000 1.69.2.3.2.6.2.6 +++ sapi/nsapi/nsapi.c 18 Sep 2008 18:38:19 -0000 @@ -359,7 +359,7 @@ PHP_FUNCTION(nsapi_virtual) php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to include uri '%s' - Sub-requests do not work with zlib.output_compression", uri); RETURN_FALSE; } else { - php_end_ob_buffers(1 TSRMLS_CC); + php_output_end_all(); php_header(TSRMLS_C); /* do the sub-request */ Index: tests/output/.cvsignore =================================================================== RCS file: tests/output/.cvsignore diff -N tests/output/.cvsignore --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ tests/output/.cvsignore 18 Sep 2008 18:38:19 -0000 @@ -0,0 +1,7 @@ +*.php +*.mem +*.log +*.diff +*.exp +*.out +*~ Index: tests/output/ob_001.phpt =================================================================== RCS file: tests/output/ob_001.phpt diff -N tests/output/ob_001.phpt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ tests/output/ob_001.phpt 18 Sep 2008 18:38:19 -0000 @@ -0,0 +1,8 @@ +--TEST-- +output buffering - nothing +--FILE-- + +--EXPECT-- +foo Index: tests/output/ob_002.phpt =================================================================== RCS file: tests/output/ob_002.phpt diff -N tests/output/ob_002.phpt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ tests/output/ob_002.phpt 18 Sep 2008 18:38:19 -0000 @@ -0,0 +1,9 @@ +--TEST-- +output buffering - ob_start +--FILE-- + +--EXPECT-- +foo Index: tests/output/ob_003.phpt =================================================================== RCS file: tests/output/ob_003.phpt diff -N tests/output/ob_003.phpt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ tests/output/ob_003.phpt 18 Sep 2008 18:38:19 -0000 @@ -0,0 +1,13 @@ +--TEST-- +output buffering - ob_flush +--FILE-- + +--EXPECT-- +foo +bar Index: tests/output/ob_004.phpt =================================================================== RCS file: tests/output/ob_004.phpt diff -N tests/output/ob_004.phpt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ tests/output/ob_004.phpt 18 Sep 2008 18:38:19 -0000 @@ -0,0 +1,11 @@ +--TEST-- +output buffering - ob_clean +--FILE-- + +--EXPECT-- +bar Index: tests/output/ob_005.phpt =================================================================== RCS file: tests/output/ob_005.phpt diff -N tests/output/ob_005.phpt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ tests/output/ob_005.phpt 18 Sep 2008 18:38:19 -0000 @@ -0,0 +1,14 @@ +--TEST-- +output buffering - ob_end_clean +--FILE-- + +--EXPECT-- +foo +baz Index: tests/output/ob_006.phpt =================================================================== RCS file: tests/output/ob_006.phpt diff -N tests/output/ob_006.phpt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ tests/output/ob_006.phpt 18 Sep 2008 18:38:19 -0000 @@ -0,0 +1,12 @@ +--TEST-- +output buffering - ob_end_flush +--FILE-- + +--EXPECT-- +foo +int(0) Index: tests/output/ob_007.phpt =================================================================== RCS file: tests/output/ob_007.phpt diff -N tests/output/ob_007.phpt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ tests/output/ob_007.phpt 18 Sep 2008 18:38:19 -0000 @@ -0,0 +1,11 @@ +--TEST-- +output buffering - ob_get_clean +--FILE-- + +--EXPECT-- +string(4) "foo +" Index: tests/output/ob_008.phpt =================================================================== RCS file: tests/output/ob_008.phpt diff -N tests/output/ob_008.phpt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ tests/output/ob_008.phpt 18 Sep 2008 18:38:19 -0000 @@ -0,0 +1,11 @@ +--TEST-- +output buffering - ob_get_contents +--FILE-- + +--EXPECT-- +foo +foo Index: tests/output/ob_009.phpt =================================================================== RCS file: tests/output/ob_009.phpt diff -N tests/output/ob_009.phpt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ tests/output/ob_009.phpt 18 Sep 2008 18:38:19 -0000 @@ -0,0 +1,12 @@ +--TEST-- +output buffering - ob_get_flush +--FILE-- + +--EXPECT-- +foo +string(4) "foo +" Index: tests/output/ob_010.phpt =================================================================== RCS file: tests/output/ob_010.phpt diff -N tests/output/ob_010.phpt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ tests/output/ob_010.phpt 18 Sep 2008 18:38:19 -0000 @@ -0,0 +1,13 @@ +--TEST-- +output buffering - fatalism +--FILE-- + +--EXPECTF-- +Fatal error: print_r(): Cannot use output buffering in output buffering display handlers in %sob_010.php on line %d Index: tests/output/ob_011.phpt =================================================================== RCS file: tests/output/ob_011.phpt diff -N tests/output/ob_011.phpt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ tests/output/ob_011.phpt 18 Sep 2008 18:38:19 -0000 @@ -0,0 +1,13 @@ +--TEST-- +output buffering - fatalism +--FILE-- + +--EXPECTF-- +Fatal error: ob_get_flush(): Cannot use output buffering in output buffering display handlers in %sob_011.php on line %d Index: tests/output/ob_012.phpt =================================================================== RCS file: tests/output/ob_012.phpt diff -N tests/output/ob_012.phpt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ tests/output/ob_012.phpt 18 Sep 2008 18:38:19 -0000 @@ -0,0 +1,22 @@ +--TEST-- +output buffering - multiple +--FILE-- + +--EXPECT-- +03412 Index: tests/output/ob_013.phpt =================================================================== RCS file: tests/output/ob_013.phpt diff -N tests/output/ob_013.phpt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ tests/output/ob_013.phpt 18 Sep 2008 18:38:19 -0000 @@ -0,0 +1,105 @@ +--TEST-- +output buffering - handlers/status +--FILE-- + +--EXPECTF-- +foo +Array +( + [0] => default output handler + [1] => a + [2] => b + [3] => c + [4] => d +) +Array +( + [name] => d + [type] => 1 + [flags] => 4209 + [level] => 4 + [chunk_size] => %d + [buffer_size] => 16384 + [buffer_used] => 96 +) +Array +( + [0] => Array + ( + [name] => default output handler + [type] => 0 + [flags] => 112 + [level] => 0 + [chunk_size] => %d + [buffer_size] => 16384 + [buffer_used] => 0 + ) + + [1] => Array + ( + [name] => a + [type] => 1 + [flags] => 113 + [level] => 1 + [chunk_size] => %d + [buffer_size] => 16384 + [buffer_used] => 0 + ) + + [2] => Array + ( + [name] => b + [type] => 1 + [flags] => 113 + [level] => 2 + [chunk_size] => %d + [buffer_size] => 16384 + [buffer_used] => 0 + ) + + [3] => Array + ( + [name] => c + [type] => 1 + [flags] => 113 + [level] => 3 + [chunk_size] => %d + [buffer_size] => 16384 + [buffer_used] => 4 + ) + + [4] => Array + ( + [name] => d + [type] => 1 + [flags] => 4209 + [level] => 4 + [chunk_size] => %d + [buffer_size] => 16384 + [buffer_used] => %d + ) + +) Index: tests/output/ob_014.phpt =================================================================== RCS file: tests/output/ob_014.phpt diff -N tests/output/ob_014.phpt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ tests/output/ob_014.phpt 18 Sep 2008 18:38:19 -0000 @@ -0,0 +1,14 @@ +--TEST-- +output buffering - failure +--FILE-- + +--EXPECTF-- +foo + +Warning: (null)() expects exactly 1 parameter, 2 given in %s on line %d Index: tests/output/ob_015.phpt =================================================================== RCS file: tests/output/ob_015.phpt diff -N tests/output/ob_015.phpt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ tests/output/ob_015.phpt 18 Sep 2008 18:38:19 -0000 @@ -0,0 +1,11 @@ +--TEST-- +output buffering - failure +--FILE-- + +--EXPECTF-- +foo + +Warning: str_rot13() expects exactly 1 parameter, 2 given in %s on line %d Index: tests/output/ob_017.phpt =================================================================== RCS file: tests/output/ob_017.phpt diff -N tests/output/ob_017.phpt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ tests/output/ob_017.phpt 18 Sep 2008 18:38:19 -0000 @@ -0,0 +1,34 @@ +--TEST-- +output buffering - stati +--FILE-- + +--EXPECT-- +yes! +yes! +Array +( + [0] => 1: yes + [1] => 4: ! + + [2] => 2: + [3] => 0: yes! + + [4] => 10: +) Index: tests/output/ob_018.phpt =================================================================== RCS file: tests/output/ob_018.phpt diff -N tests/output/ob_018.phpt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ tests/output/ob_018.phpt 18 Sep 2008 18:38:19 -0000 @@ -0,0 +1,17 @@ +--TEST-- +output buffering - error message nirvana bug #37714 +--SKIPIF-- + +--ENV-- +HTTP_ACCEPT_ENCODING=gzip,deflate +--INI-- +display_errors=1 +zlib.output_compression=1 +--FILE-- + +--EXPECTF-- +‹%a Index: tests/output/ob_019.phpt =================================================================== RCS file: tests/output/ob_019.phpt diff -N tests/output/ob_019.phpt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ tests/output/ob_019.phpt 18 Sep 2008 18:38:19 -0000 @@ -0,0 +1,51 @@ +--TEST-- +output buffering - flags +--FILE-- + +--EXPECTF-- + +==0== + +Notice: ob_flush(): failed to flush buffer of default output handler (0) in %sob_019.php on line %d + +==1== +Y:flush + +Notice: ob_clean(): failed to delete buffer of default output handler (1) in %sob_019.php on line %d + +==2== + +==2== +Y:remove-flush + +==2== Index: tests/output/ob_020.phpt =================================================================== RCS file: tests/output/ob_020.phpt diff -N tests/output/ob_020.phpt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ tests/output/ob_020.phpt 18 Sep 2008 18:38:19 -0000 @@ -0,0 +1,38 @@ +--TEST-- +output buffering - ob_list_handlers +--FILE-- + +--EXPECT-- +Array +( +) +Array +( + [0] => default output handler +) +Array +( + [0] => default output handler + [1] => default output handler +) +Array +( + [0] => default output handler +) +Array +( +)