Skip to content

Commit

Permalink
Fixed encoding problems when PHP INI setting default_charset is not…
Browse files Browse the repository at this point in the history
… `utf-8` [#2154]
  • Loading branch information
mahagr committed Feb 10, 2020
1 parent c2f374f commit e55b239
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 27 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
7 changes: 7 additions & 0 deletions bin/gpm
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
}
Expand Down
15 changes: 11 additions & 4 deletions bin/grav
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand All @@ -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!');
}
Expand Down
7 changes: 7 additions & 0 deletions bin/plugin
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
}
Expand Down
19 changes: 10 additions & 9 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@
die("PHP webserver requires a router to run Grav, please use: <pre>php -S {$_SERVER['SERVER_NAME']}:{$_SERVER['SERVER_PORT']} system/router.php</pre>");
}

// 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)) {
Expand All @@ -32,15 +42,6 @@
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(
Expand Down
4 changes: 2 additions & 2 deletions system/src/Grav/Common/Assets/Traits/AssetUtilsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ protected function renderAttributes()
}

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;
Expand Down
4 changes: 2 additions & 2 deletions system/src/Grav/Common/Page/Markdown/Excerpts.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function fireInitializedEvent($markdown): void
*/
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);

Expand Down Expand Up @@ -152,7 +152,7 @@ static function ($carry, $item) {
*/
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;
Expand Down
14 changes: 7 additions & 7 deletions system/src/Grav/Common/Page/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -608,12 +608,12 @@ public function summary($size = null, $textOnly = false)
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');
}

/**
Expand Down Expand Up @@ -1713,7 +1713,7 @@ public function metadata($var = null)
$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 {
Expand All @@ -1722,16 +1722,16 @@ public function metadata($var = null)
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')) {
Expand Down Expand Up @@ -2718,7 +2718,7 @@ public function collection($params = 'content', $pagination = true)
}
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());
}
Expand Down
2 changes: 1 addition & 1 deletion system/src/Grav/Common/Twig/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,7 @@ public function nonceFieldFunc($action, $nonceParamName = 'nonce')
*/
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);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion system/src/Grav/Common/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public function params($id = null, $array = false)
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;
Expand Down

0 comments on commit e55b239

Please sign in to comment.