diff --git a/ext/iconv/iconv.c b/ext/iconv/iconv.c index 433979f6ed1..068b99d5c87 100644 --- a/ext/iconv/iconv.c +++ b/ext/iconv/iconv.c @@ -120,12 +120,6 @@ ZEND_BEGIN_ARG_INFO(arginfo_iconv, 0) ZEND_ARG_INFO(0, str) 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) @@ -143,7 +137,6 @@ ZEND_END_ARG_INFO() */ zend_function_entry iconv_functions[] = { PHP_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_decode(smart_str *pretval, const char *st 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 */ @@ -275,6 +272,9 @@ PHP_MINIT_FUNCTION(miconv) if (php_iconv_stream_filter_register_factory(TSRMLS_C) != PHP_ICONV_ERR_SUCCESS) { 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; } @@ -313,6 +313,69 @@ 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, *output_encoding, *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 (UG(unicode)) { + output_encoding = INI_STR("unicode.output_encoding"); + if (output_encoding && *output_encoding && strcasecmp(INI_STR("unicode.output_encoding"), ICONVG(internal_encoding))) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "unicode.output_encoding differs from iconv.internal_encoding (%s, %s)", output_encoding, ICONVG(internal_encoding)); + efree(ICONVG(input_encoding)); + ICONVG(input_encoding) = estrdup(output_encoding); + } + } + + 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_status & PHP_OUTPUT_HANDLER_CLEAN)) { + 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, strlen(content_type), 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) { @@ -2272,55 +2335,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) { - spprintf(&content_type, 0, "Content-Type:%s; charset=%s", mimetype, ICONVG(output_encoding)); - if (content_type && sapi_add_header(content_type, strlen(content_type), 0) != FAILURE) { - SG(sapi_headers).send_default_content_type = 0; - } - 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) diff --git a/ext/iconv/php_iconv.h b/ext/iconv/php_iconv.h index 9a25903d187..b8a7de2ef0b 100644 --- a/ext/iconv/php_iconv.h +++ b/ext/iconv/php_iconv.h @@ -52,7 +52,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);