From 29c6a70611074467c67bf170f65132285d316d74 Mon Sep 17 00:00:00 2001 From: Florian Schlittenbauer Date: Sat, 8 Feb 2020 21:47:17 +0100 Subject: [PATCH 1/3] [BUGFIX] .editorconfig not picking up .yml files (#2808) --- .editorconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From c2f374f0db4a61b964fe9022cca0b804a73ec82c Mon Sep 17 00:00:00 2001 From: Andreas Amos Date: Fri, 7 Feb 2020 18:35:29 +0100 Subject: [PATCH 2/3] Truncator: encoding problems solves #2154 As I am not a developer somebody should review the change. --- system/src/Grav/Common/Page/Page.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/src/Grav/Common/Page/Page.php b/system/src/Grav/Common/Page/Page.php index 75bf45340..2b64360a2 100644 --- a/system/src/Grav/Common/Page/Page.php +++ b/system/src/Grav/Common/Page/Page.php @@ -613,7 +613,7 @@ class Page implements PageInterface $summary = Utils::truncateHtml($content, $size); - return html_entity_decode($summary); + return html_entity_decode($summary,ENT_COMPAT | ENT_HTML401, 'utf-8'); } /** From e55b239536cb8c1bd76a780991ecbd318e850190 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Mon, 10 Feb 2020 09:50:39 +0200 Subject: [PATCH 3/3] Fixed encoding problems when PHP INI setting `default_charset` is not `utf-8` [#2154] --- CHANGELOG.md | 8 +++++++- bin/gpm | 7 +++++++ bin/grav | 15 +++++++++++---- bin/plugin | 7 +++++++ index.php | 19 ++++++++++--------- .../Common/Assets/Traits/AssetUtilsTrait.php | 4 ++-- .../Grav/Common/Page/Markdown/Excerpts.php | 4 ++-- system/src/Grav/Common/Page/Page.php | 14 +++++++------- system/src/Grav/Common/Twig/TwigExtension.php | 2 +- system/src/Grav/Common/Uri.php | 2 +- 10 files changed, 55 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c7e59d92a..fcd8cec11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ +# 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 7120218c0..b1e4c90e5 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 3c431e379..925549fa0 100755 --- a/bin/plugin +++ b/bin/plugin @@ -32,6 +32,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 64154df89..7be4df1c6 100644 --- a/index.php +++ b/index.php @@ -20,6 +20,16 @@ if (PHP_SAPI === 'cli-server' && !isset($_SERVER['PHP_CLI_ROUTER'])) { die("PHP webserver requires a router to run Grav, please use:
php -S {$_SERVER['SERVER_NAME']}:{$_SERVER['SERVER_PORT']} system/router.php
"); } +// 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)) { @@ -32,15 +42,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 0a1d149de..ce6183737 100644 --- a/system/src/Grav/Common/Assets/Traits/AssetUtilsTrait.php +++ b/system/src/Grav/Common/Assets/Traits/AssetUtilsTrait.php @@ -138,9 +138,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 eaa13cbe8..798ce082d 100644 --- a/system/src/Grav/Common/Page/Markdown/Excerpts.php +++ b/system/src/Grav/Common/Page/Markdown/Excerpts.php @@ -68,7 +68,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); @@ -152,7 +152,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 2b64360a2..17a7d4a73 100644 --- a/system/src/Grav/Common/Page/Page.php +++ b/system/src/Grav/Common/Page/Page.php @@ -608,12 +608,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,ENT_COMPAT | ENT_HTML401, 'utf-8'); + return html_entity_decode($summary, ENT_COMPAT | ENT_HTML5, 'UTF-8'); } /** @@ -1713,7 +1713,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 { @@ -1722,16 +1722,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')) { @@ -2718,7 +2718,7 @@ class Page implements PageInterface } foreach ($items as $item) { $item = rawurldecode($item); - if (empty($page->taxonomy[$taxonomy]) || !\in_array(htmlspecialchars_decode($item, ENT_QUOTES), $page->taxonomy[$taxonomy], true) + if (empty($page->taxonomy[$taxonomy]) || !\in_array(htmlspecialchars_decode($item, ENT_QUOTES | ENT_HTML5), $page->taxonomy[$taxonomy], true) ) { $collection->remove($page->path()); } diff --git a/system/src/Grav/Common/Twig/TwigExtension.php b/system/src/Grav/Common/Twig/TwigExtension.php index f84923ebd..62ee0c2b3 100644 --- a/system/src/Grav/Common/Twig/TwigExtension.php +++ b/system/src/Grav/Common/Twig/TwigExtension.php @@ -1055,7 +1055,7 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn */ 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 f875ab3de..4d7936e91 100644 --- a/system/src/Grav/Common/Uri.php +++ b/system/src/Grav/Common/Uri.php @@ -306,7 +306,7 @@ class Uri public function param($id) { 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 false;