Merge pull request #1569 from ArthurHoaro/fix/bad-encoding
Fix warning if the encoding retrieved from external headers is invalid
This commit is contained in:
commit
c3fca560b6
3 changed files with 15 additions and 2 deletions
|
@ -26,7 +26,7 @@ function html_extract_title($html)
|
|||
*/
|
||||
function header_extract_charset($header)
|
||||
{
|
||||
preg_match('/charset="?([^; ]+)/i', $header, $match);
|
||||
preg_match('/charset=["\']?([^; "\']+)/i', $header, $match);
|
||||
if (! empty($match[1])) {
|
||||
return strtolower(trim($match[1]));
|
||||
}
|
||||
|
|
|
@ -69,7 +69,7 @@ public function displayCreateForm(Request $request, Response $response): Respons
|
|||
$retrieveDescription
|
||||
)
|
||||
);
|
||||
if (! empty($title) && strtolower($charset) !== 'utf-8') {
|
||||
if (! empty($title) && strtolower($charset) !== 'utf-8' && mb_check_encoding($charset)) {
|
||||
$title = mb_convert_encoding($title, 'utf-8', $charset);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,6 +42,19 @@ public function testHeadersExtractExistentCharset()
|
|||
$this->assertEquals(strtolower($charset), header_extract_charset($headers));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test headers_extract_charset() when the charset is found with odd quotes.
|
||||
*/
|
||||
public function testHeadersExtractExistentCharsetWithQuotes()
|
||||
{
|
||||
$charset = 'x-MacCroatian';
|
||||
$headers = 'text/html; charset="' . $charset . '"otherstuff="test"';
|
||||
$this->assertEquals(strtolower($charset), header_extract_charset($headers));
|
||||
|
||||
$headers = 'text/html; charset=\'' . $charset . '\'otherstuff="test"';
|
||||
$this->assertEquals(strtolower($charset), header_extract_charset($headers));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test headers_extract_charset() when the charset is not found.
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue