diff --git a/.editorconfig b/.editorconfig index 3b95e6dba..da1477309 100644 --- a/.editorconfig +++ b/.editorconfig @@ -13,5 +13,5 @@ indent_style = space indent_size = 4 # 2 space indentation -[*.{yaml,.yml}] +[*.{yaml,yml}] indent_size = 2 diff --git a/CHANGELOG.md b/CHANGELOG.md index 44971fb5b..427bb887b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -273,8 +273,14 @@ * Optimization: Initialize debugbar only after the configuration has been loaded * Optimization: Combine some early Grav processors into a single one +# v1.6.21 +## mm/dd/2020 + +1. [](#bugfix) + * Fixed encoding problems when PHP INI setting `default_charset` is not `utf-8` [#2154](https://github.com/getgrav/grav/issues/2154) + # v1.6.20 -## 03/02/2020 +## 02/03/2020 1. [](#bugfix) * Fixed incorrect routing caused by `str_replace()` in `Uri::init()` [#2754](https://github.com/getgrav/grav/issues/2754) diff --git a/bin/gpm b/bin/gpm index 7ad1e4ac5..88b2ee8dd 100755 --- a/bin/gpm +++ b/bin/gpm @@ -28,6 +28,13 @@ if (!ini_get('date.timezone')) { date_default_timezone_set('UTC'); } +// Set internal encoding. +if (!\extension_loaded('mbstring')) { + die("'mbstring' extension is not loaded. This is required for Grav to run correctly"); +} +@ini_set('default_charset', 'UTF-8'); +mb_internal_encoding('UTF-8'); + if (!file_exists(GRAV_ROOT . '/index.php')) { exit('FATAL: Must be run from ROOT directory of Grav!'); } diff --git a/bin/grav b/bin/grav index 7d365cbfc..4b5aee470 100755 --- a/bin/grav +++ b/bin/grav @@ -25,6 +25,17 @@ if (version_compare($ver = PHP_VERSION, $req = GRAV_PHP_MIN, '<')) { exit(sprintf("You are running PHP %s, but Grav needs at least PHP %s to run.\n", $ver, $req)); } +if (!ini_get('date.timezone')) { + date_default_timezone_set('UTC'); +} + +// Set internal encoding. +if (!\extension_loaded('mbstring')) { + die("'mbstring' extension is not loaded. This is required for Grav to run correctly"); +} +@ini_set('default_charset', 'UTF-8'); +mb_internal_encoding('UTF-8'); + $climate = new League\CLImate\CLImate; $climate->arguments->add([ 'environment' => [ @@ -42,10 +53,6 @@ $environment = $climate->arguments->get('environment'); $grav = Grav::instance(array('loader' => $autoload)); $grav->setup($environment); -if (!ini_get('date.timezone')) { - date_default_timezone_set('UTC'); -} - if (!file_exists(GRAV_ROOT . '/index.php')) { exit('FATAL: Must be run from ROOT directory of Grav!'); } diff --git a/bin/plugin b/bin/plugin index 9350a1f77..5ead831b9 100755 --- a/bin/plugin +++ b/bin/plugin @@ -33,6 +33,13 @@ if (!ini_get('date.timezone')) { date_default_timezone_set('UTC'); } +// Set internal encoding. +if (!\extension_loaded('mbstring')) { + die("'mbstring' extension is not loaded. This is required for Grav to run correctly"); +} +@ini_set('default_charset', 'UTF-8'); +mb_internal_encoding('UTF-8'); + if (!file_exists(GRAV_ROOT . '/index.php')) { exit('FATAL: Must be run from ROOT directory of Grav!'); } diff --git a/index.php b/index.php index 633570274..59cbbcabf 100644 --- a/index.php +++ b/index.php @@ -23,6 +23,16 @@ if (PHP_SAPI === 'cli-server') { } } +// Set timezone to default, falls back to system if php.ini not set +date_default_timezone_set(@date_default_timezone_get()); + +// Set internal encoding. +if (!\extension_loaded('mbstring')) { + die("'mbstring' extension is not loaded. This is required for Grav to run correctly"); +} +@ini_set('default_charset', 'UTF-8'); +mb_internal_encoding('UTF-8'); + // Ensure vendor libraries exist $autoload = __DIR__ . '/vendor/autoload.php'; if (!is_file($autoload)) { @@ -35,15 +45,6 @@ $loader = require $autoload; use Grav\Common\Grav; use RocketTheme\Toolbox\Event\Event; -// Set timezone to default, falls back to system if php.ini not set -date_default_timezone_set(@date_default_timezone_get()); - -// Set internal encoding if mbstring loaded -if (!\extension_loaded('mbstring')) { - die("'mbstring' extension is not loaded. This is required for Grav to run correctly"); -} -mb_internal_encoding('UTF-8'); - // Get the Grav instance $grav = Grav::instance( array( diff --git a/system/src/Grav/Common/Assets/Traits/AssetUtilsTrait.php b/system/src/Grav/Common/Assets/Traits/AssetUtilsTrait.php index c3104e690..3cd87ddcd 100644 --- a/system/src/Grav/Common/Assets/Traits/AssetUtilsTrait.php +++ b/system/src/Grav/Common/Assets/Traits/AssetUtilsTrait.php @@ -154,9 +154,9 @@ trait AssetUtilsTrait } if (\in_array($key, $no_key, true)) { - $element = htmlentities($value, ENT_QUOTES, 'UTF-8', false); + $element = htmlentities($value, ENT_QUOTES | ENT_HTML5, 'UTF-8', false); } else { - $element = $key . '="' . htmlentities($value, ENT_QUOTES, 'UTF-8', false) . '"'; + $element = $key . '="' . htmlentities($value, ENT_QUOTES | ENT_HTML5, 'UTF-8', false) . '"'; } $html .= ' ' . $element; diff --git a/system/src/Grav/Common/Page/Markdown/Excerpts.php b/system/src/Grav/Common/Page/Markdown/Excerpts.php index f68524972..8a71f60eb 100644 --- a/system/src/Grav/Common/Page/Markdown/Excerpts.php +++ b/system/src/Grav/Common/Page/Markdown/Excerpts.php @@ -83,7 +83,7 @@ class Excerpts */ public function processLinkExcerpt(array $excerpt, string $type = 'link'): array { - $url = htmlspecialchars_decode(rawurldecode($excerpt['element']['attributes']['href'])); + $url = htmlspecialchars_decode(rawurldecode($excerpt['element']['attributes']['href']), ENT_QUOTES | ENT_HTML5); $url_parts = $this->parseUrl($url); @@ -167,7 +167,7 @@ class Excerpts */ public function processImageExcerpt(array $excerpt): array { - $url = htmlspecialchars_decode(urldecode($excerpt['element']['attributes']['src'])); + $url = htmlspecialchars_decode(urldecode($excerpt['element']['attributes']['src']), ENT_QUOTES | ENT_HTML5); $url_parts = $this->parseUrl($url); $media = null; diff --git a/system/src/Grav/Common/Page/Page.php b/system/src/Grav/Common/Page/Page.php index cc05f9cc4..e319d5db9 100644 --- a/system/src/Grav/Common/Page/Page.php +++ b/system/src/Grav/Common/Page/Page.php @@ -652,12 +652,12 @@ class Page implements PageInterface return $content; } - return mb_strimwidth($content, 0, $size, '...', 'utf-8'); + return mb_strimwidth($content, 0, $size, '...', 'UTF-8'); } $summary = Utils::truncateHtml($content, $size); - return html_entity_decode($summary); + return html_entity_decode($summary, ENT_COMPAT | ENT_HTML5, 'UTF-8'); } /** @@ -1647,7 +1647,7 @@ class Page implements PageInterface $this->metadata[$prop_key] = [ 'name' => $prop_key, 'property' => $prop_key, - 'content' => htmlspecialchars($prop_value, ENT_QUOTES, 'UTF-8') + 'content' => htmlspecialchars($prop_value, ENT_QUOTES | ENT_HTML5, 'UTF-8') ]; } } else { @@ -1656,16 +1656,16 @@ class Page implements PageInterface if (\in_array($key, $header_tag_http_equivs, true)) { $this->metadata[$key] = [ 'http_equiv' => $key, - 'content' => htmlspecialchars($value, ENT_QUOTES, 'UTF-8') + 'content' => htmlspecialchars($value, ENT_QUOTES | ENT_HTML5, 'UTF-8') ]; } elseif ($key === 'charset') { - $this->metadata[$key] = ['charset' => htmlspecialchars($value, ENT_QUOTES, 'UTF-8')]; + $this->metadata[$key] = ['charset' => htmlspecialchars($value, ENT_QUOTES | ENT_HTML5, 'UTF-8')]; } else { // if it's a social metadata with separator, render as property $separator = strpos($key, ':'); $hasSeparator = $separator && $separator < strlen($key) - 1; $entry = [ - 'content' => htmlspecialchars($value, ENT_QUOTES, 'UTF-8') + 'content' => htmlspecialchars($value, ENT_QUOTES | ENT_HTML5, 'UTF-8') ]; if ($hasSeparator && !Utils::startsWith($key, 'twitter')) { diff --git a/system/src/Grav/Common/Twig/TwigExtension.php b/system/src/Grav/Common/Twig/TwigExtension.php index defbfbf66..ae433f0ee 100644 --- a/system/src/Grav/Common/Twig/TwigExtension.php +++ b/system/src/Grav/Common/Twig/TwigExtension.php @@ -1098,7 +1098,7 @@ class TwigExtension extends AbstractExtension implements GlobalsInterface */ public function jsonDecodeFilter($str, $assoc = false, $depth = 512, $options = 0) { - return json_decode(html_entity_decode($str), $assoc, $depth, $options); + return json_decode(html_entity_decode($str, ENT_COMPAT | ENT_HTML5, 'UTF-8'), $assoc, $depth, $options); } /** diff --git a/system/src/Grav/Common/Uri.php b/system/src/Grav/Common/Uri.php index 0b1dc73b4..52fb6b3b4 100644 --- a/system/src/Grav/Common/Uri.php +++ b/system/src/Grav/Common/Uri.php @@ -334,7 +334,7 @@ class Uri public function param($id, $default = false) { if (isset($this->params[$id])) { - return html_entity_decode(rawurldecode($this->params[$id])); + return html_entity_decode(rawurldecode($this->params[$id]), ENT_COMPAT | ENT_HTML5, 'UTF-8'); } return $default;