|
92 | 92 | #include "SAPI.h"
|
93 | 93 | #include "rfc1867.h"
|
94 | 94 |
|
| 95 | +#include "ext/standard/html_tables.h" |
| 96 | + |
95 | 97 | #if HAVE_MMAP || defined(PHP_WIN32)
|
96 | 98 | # if HAVE_UNISTD_H
|
97 | 99 | # include <unistd.h>
|
@@ -124,6 +126,31 @@ PHPAPI int core_globals_id;
|
124 | 126 |
|
125 | 127 | #define SAFE_FILENAME(f) ((f)?(f):"-")
|
126 | 128 |
|
| 129 | +static char *get_safe_charset_hint(void) { |
| 130 | + static char *lastHint = NULL; |
| 131 | + static char *lastCodeset = NULL; |
| 132 | + char *hint = SG(default_charset); |
| 133 | + size_t len = strlen(hint); |
| 134 | + size_t i = 0; |
| 135 | + |
| 136 | + if (lastHint == SG(default_charset)) { |
| 137 | + return lastCodeset; |
| 138 | + } |
| 139 | + |
| 140 | + lastHint = hint; |
| 141 | + lastCodeset = NULL; |
| 142 | + |
| 143 | + for (i = 0; i < sizeof(charset_map)/sizeof(charset_map[0]); i++) { |
| 144 | + if (len == charset_map[i].codeset_len |
| 145 | + && zend_binary_strcasecmp(hint, len, charset_map[i].codeset, len) == 0) { |
| 146 | + lastCodeset = (char*)charset_map[i].codeset; |
| 147 | + break; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + return lastCodeset; |
| 152 | +} |
| 153 | + |
127 | 154 | /* {{{ PHP_INI_MH
|
128 | 155 | */
|
129 | 156 | static PHP_INI_MH(OnSetPrecision)
|
@@ -722,10 +749,10 @@ PHPAPI ZEND_COLD void php_verror(const char *docref, const char *params, int typ
|
722 | 749 | buffer_len = (int)vspprintf(&buffer, 0, format, args);
|
723 | 750 |
|
724 | 751 | if (PG(html_errors)) {
|
725 |
| - replace_buffer = php_escape_html_entities((unsigned char*)buffer, buffer_len, 0, ENT_COMPAT, SG(default_charset)); |
| 752 | + replace_buffer = php_escape_html_entities((unsigned char*)buffer, buffer_len, 0, ENT_COMPAT, get_safe_charset_hint()); |
726 | 753 | /* Retry with substituting invalid chars on fail. */
|
727 | 754 | if (!replace_buffer || ZSTR_LEN(replace_buffer) < 1) {
|
728 |
| - replace_buffer = php_escape_html_entities((unsigned char*)buffer, buffer_len, 0, ENT_COMPAT | ENT_HTML_SUBSTITUTE_ERRORS, SG(default_charset)); |
| 755 | + replace_buffer = php_escape_html_entities((unsigned char*)buffer, buffer_len, 0, ENT_COMPAT | ENT_HTML_SUBSTITUTE_ERRORS, get_safe_charset_hint()); |
729 | 756 | }
|
730 | 757 |
|
731 | 758 | efree(buffer);
|
@@ -792,7 +819,7 @@ PHPAPI ZEND_COLD void php_verror(const char *docref, const char *params, int typ
|
792 | 819 | }
|
793 | 820 |
|
794 | 821 | if (PG(html_errors)) {
|
795 |
| - replace_origin = php_escape_html_entities((unsigned char*)origin, origin_len, 0, ENT_COMPAT, SG(default_charset)); |
| 822 | + replace_origin = php_escape_html_entities((unsigned char*)origin, origin_len, 0, ENT_COMPAT, get_safe_charset_hint()); |
796 | 823 | efree(origin);
|
797 | 824 | origin = ZSTR_VAL(replace_origin);
|
798 | 825 | }
|
@@ -1106,7 +1133,7 @@ static ZEND_COLD void php_error_cb(int type, const char *error_filename, const u
|
1106 | 1133 |
|
1107 | 1134 | if (PG(html_errors)) {
|
1108 | 1135 | if (type == E_ERROR || type == E_PARSE) {
|
1109 |
| - zend_string *buf = php_escape_html_entities((unsigned char*)buffer, buffer_len, 0, ENT_COMPAT, SG(default_charset)); |
| 1136 | + zend_string *buf = php_escape_html_entities((unsigned char*)buffer, buffer_len, 0, ENT_COMPAT, get_safe_charset_hint()); |
1110 | 1137 | php_printf("%s<br />\n<b>%s</b>: %s in <b>%s</b> on line <b>%d</b><br />\n%s", STR_PRINT(prepend_string), error_type_str, ZSTR_VAL(buf), error_filename, error_lineno, STR_PRINT(append_string));
|
1111 | 1138 | zend_string_free(buf);
|
1112 | 1139 | } else {
|
|
0 commit comments