[formats] Use custom characterset in all formats

The specified characterset will now apply to all formats
thus allowing other charactersets than 'UTF-8'
This commit is contained in:
logmanoriginal 2016-11-07 20:49:44 +01:00
parent 4f4fb11789
commit 3a2cb9ea1e
5 changed files with 35 additions and 17 deletions

View file

@ -47,10 +47,11 @@ EOD;
} }
$feedTimestamp = date(DATE_ATOM, time()); $feedTimestamp = date(DATE_ATOM, time());
$charset = $this->getCharset();
/* Data are prepared, now let's begin the "MAGIE !!!" */ /* Data are prepared, now let's begin the "MAGIE !!!" */
$toReturn = '<?xml version="1.0" encoding="UTF-8"?>'; $toReturn = <<<EOD
$toReturn .= <<<EOD <?xml version="1.0" encoding="{$charset}"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0"> <feed xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0">
<title type="text">{$title}</title> <title type="text">{$title}</title>
@ -64,15 +65,15 @@ EOD;
</feed> </feed>
EOD; EOD;
// Remove invalid non-UTF8 characters // Remove invalid characters
ini_set('mbstring.substitute_character', 'none'); ini_set('mbstring.substitute_character', 'none');
$toReturn = mb_convert_encoding($toReturn, 'UTF-8', 'UTF-8'); $toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8');
return $toReturn; return $toReturn;
} }
public function display(){ public function display(){
$this $this
->setContentType('application/atom+xml; charset=UTF-8') ->setContentType('application/atom+xml; charset=' . $this->getCharset())
->callContentType(); ->callContentType();
return parent::display(); return parent::display();

View file

@ -42,12 +42,14 @@ class HtmlFormat extends FormatAbstract {
EOD; EOD;
} }
$charset = $this->getCharset();
/* Data are prepared, now let's begin the "MAGIE !!!" */ /* Data are prepared, now let's begin the "MAGIE !!!" */
$toReturn = <<<EOD $toReturn = <<<EOD
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<meta charset="UTF-8"> <meta charset="{$charset}">
<title>{$title}</title> <title>{$title}</title>
<link href="css/HtmlFormat.css" rel="stylesheet"> <link href="css/HtmlFormat.css" rel="stylesheet">
<meta name="robots" content="noindex, follow"> <meta name="robots" content="noindex, follow">
@ -64,6 +66,9 @@ EOD;
</html> </html>
EOD; EOD;
// Remove invalid characters
ini_set('mbstring.substitute_character', 'none');
$toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8');
return $toReturn; return $toReturn;
} }

View file

@ -7,12 +7,17 @@ class JsonFormat extends FormatAbstract {
public function stringify(){ public function stringify(){
$items = $this->getItems(); $items = $this->getItems();
return json_encode($items, JSON_PRETTY_PRINT); $toReturn = json_encode($items, JSON_PRETTY_PRINT);
// Remove invalid non-UTF8 characters
ini_set('mbstring.substitute_character', 'none');
$toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8');
return $toReturn;
} }
public function display(){ public function display(){
$this $this
->setContentType('application/json') ->setContentType('application/json; charset=' . $this->getCharset())
->callContentType(); ->callContentType();
return parent::display(); return parent::display();

View file

@ -48,12 +48,14 @@ class MrssFormat extends FormatAbstract {
EOD; EOD;
} }
$charset = $this->getCharset();
/* Data are prepared, now let's begin the "MAGIE !!!" */ /* Data are prepared, now let's begin the "MAGIE !!!" */
$toReturn = '<?xml version="1.0" encoding="UTF-8"?>'; $toReturn = <<<EOD
$toReturn .= <<<EOD <?xml version="1.0" encoding="{$charset}"?>
<rss version="2.0" <rss version="2.0"
xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:media="http://search.yahoo.com/mrss/" xmlns:media="http://search.yahoo.com/mrss/"
xmlns:atom="http://www.w3.org/2005/Atom"> xmlns:atom="http://www.w3.org/2005/Atom">
<channel> <channel>
<title>{$title}</title> <title>{$title}</title>
@ -69,13 +71,13 @@ EOD;
// Remove invalid non-UTF8 characters // Remove invalid non-UTF8 characters
ini_set('mbstring.substitute_character', 'none'); ini_set('mbstring.substitute_character', 'none');
$toReturn = mb_convert_encoding($toReturn, 'UTF-8', 'UTF-8'); $toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8');
return $toReturn; return $toReturn;
} }
public function display(){ public function display(){
$this $this
->setContentType('application/rss+xml; charset=UTF-8') ->setContentType('application/rss+xml; charset=' . $this->getCharset())
->callContentType(); ->callContentType();
return parent::display(); return parent::display();

View file

@ -7,12 +7,17 @@ class PlaintextFormat extends FormatAbstract {
public function stringify(){ public function stringify(){
$items = $this->getItems(); $items = $this->getItems();
return print_r($items, true); $toReturn = print_r($items, true);
// Remove invalid non-UTF8 characters
ini_set('mbstring.substitute_character', 'none');
$toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8');
return $toReturn;
} }
public function display(){ public function display(){
$this $this
->setContentType('text/plain;charset=' . $this->getCharset()) ->setContentType('text/plain; charset=' . $this->getCharset())
->callContentType(); ->callContentType();
return parent::display(); return parent::display();