From 8e0e3e871849a14cbaef6254fed34e49534e9625 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Mon, 14 May 2018 21:23:56 +0300 Subject: [PATCH 01/11] Add special handling for User authenticated and authorized properties --- system/src/Grav/Common/User/User.php | 52 ++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/system/src/Grav/Common/User/User.php b/system/src/Grav/Common/User/User.php index c1e628d50..8baec6333 100644 --- a/system/src/Grav/Common/User/User.php +++ b/system/src/Grav/Common/User/User.php @@ -17,6 +17,9 @@ use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator; class User extends Data { + protected $authenticated; + protected $authorized; + /** * Load user account. * @@ -102,6 +105,55 @@ class User extends Data return false; } + + public function offsetExists($offset) + { + if ($offset === 'authenticated') { + return null !== $this->authenticated; + } + if ($offset === 'authorized') { + return null !== $this->authorized; + } + + return parent::offsetExists($offset); + } + + public function offsetGet($offset) + { + if ($offset === 'authenticated') { + return null !== $this->authenticated ? $this->authenticated : false; + } + if ($offset === 'authorized') { + return null !== $this->authorized ? $this->authorized : $this->authenticated; + } + + return parent::offsetGet($offset); + } + + public function offsetSet($offset, $value) + { + if ($offset === 'authenticated') { + $this->authenticated = (bool)$value; + } + if ($offset === 'authorized') { + $this->authorized = (bool)$value; + } + + parent::offsetSet($offset, $value); + } + + public function offsetUnset($offset) + { + if ($offset === 'authenticated') { + $this->authenticated = null; + } + if ($offset === 'authorized') { + $this->authorized = null; + } + + parent::offsetUnset($offset); + } + /** * Authenticate user. * From 2f17b3fa7d4dd1db96f40e140523e00ac33b4622 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Mon, 14 May 2018 18:25:19 -0600 Subject: [PATCH 02/11] Revert "Add special handling for User authenticated and authorized properties" This reverts commit 8e0e3e871849a14cbaef6254fed34e49534e9625. --- system/src/Grav/Common/User/User.php | 52 ---------------------------- 1 file changed, 52 deletions(-) diff --git a/system/src/Grav/Common/User/User.php b/system/src/Grav/Common/User/User.php index 8baec6333..c1e628d50 100644 --- a/system/src/Grav/Common/User/User.php +++ b/system/src/Grav/Common/User/User.php @@ -17,9 +17,6 @@ use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator; class User extends Data { - protected $authenticated; - protected $authorized; - /** * Load user account. * @@ -105,55 +102,6 @@ class User extends Data return false; } - - public function offsetExists($offset) - { - if ($offset === 'authenticated') { - return null !== $this->authenticated; - } - if ($offset === 'authorized') { - return null !== $this->authorized; - } - - return parent::offsetExists($offset); - } - - public function offsetGet($offset) - { - if ($offset === 'authenticated') { - return null !== $this->authenticated ? $this->authenticated : false; - } - if ($offset === 'authorized') { - return null !== $this->authorized ? $this->authorized : $this->authenticated; - } - - return parent::offsetGet($offset); - } - - public function offsetSet($offset, $value) - { - if ($offset === 'authenticated') { - $this->authenticated = (bool)$value; - } - if ($offset === 'authorized') { - $this->authorized = (bool)$value; - } - - parent::offsetSet($offset, $value); - } - - public function offsetUnset($offset) - { - if ($offset === 'authenticated') { - $this->authenticated = null; - } - if ($offset === 'authorized') { - $this->authorized = null; - } - - parent::offsetUnset($offset); - } - /** * Authenticate user. * From d4494cb502d61e5ae6fa4f29b6e50be5a91c3143 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Tue, 15 May 2018 16:14:36 +0300 Subject: [PATCH 03/11] Fixed users getting logged out after Grav upgrade --- system/src/Grav/Common/User/User.php | 36 +++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/system/src/Grav/Common/User/User.php b/system/src/Grav/Common/User/User.php index c1e628d50..512b29203 100644 --- a/system/src/Grav/Common/User/User.php +++ b/system/src/Grav/Common/User/User.php @@ -95,11 +95,41 @@ class User extends Data public static function remove($username) { $file_path = Grav::instance()['locator']->findResource('account://' . $username . YAML_EXT); - if ($file_path && unlink($file_path)) { - return true; + + return $file_path && unlink($file_path); + } + + /** + * @param string $offset + * @return bool + */ + public function offsetExists($offset) + { + $value = parent::offsetExists($offset); + + // Handle special case where user was logged in before 'authorized' was added to the user object. + if (false === $value && $offset === 'authorized') { + $value = $this->offsetExists('authenticated'); } - return false; + return $value; + } + + /** + * @param string $offset + * @return mixed + */ + public function offsetGet($offset) + { + $value = parent::offsetGet($offset); + + // Handle special case where user was logged in before 'authorized' was added to the user object. + if (null === $value && $offset === 'authorized') { + $value = $this->offsetGet('authenticated'); + $this->offsetSet($offset, $value); + } + + return $value; } /** From e1d52181a3c97b0aae9add785415691b0dac9db8 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Tue, 15 May 2018 11:36:20 -0600 Subject: [PATCH 04/11] updated changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d6504836f..4154bf9f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# v1.4.5 +## 05/11/2018 + +1. [](#bugfix) + * Fixed an issue with some users getting **2FA** prompt after upgrade [admin#1442](https://github.com/getgrav/grav-plugin-admin/issues/1442) + # v1.4.4 ## 05/11/2018 From 7f90ad8474d074505dacb874ce4864e56f1c4bd1 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Tue, 15 May 2018 19:37:18 +0200 Subject: [PATCH 05/11] Do not crash when generating URLs with arrays as parameters (#2018) When using a default image derivatives (#1979) setting like images: defaults: derivatives: [300,600] then Grav crashes when a video gets embedded. The following error occurs: rawurlencode() expects parameter 1 to be string, array given system/src/Grav/Common/Page/Medium/Medium.php:521 This patch encodes array URL parameters correctly. --- system/src/Grav/Common/Page/Medium/Medium.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/system/src/Grav/Common/Page/Medium/Medium.php b/system/src/Grav/Common/Page/Medium/Medium.php index 1c1c60f89..673368054 100644 --- a/system/src/Grav/Common/Page/Medium/Medium.php +++ b/system/src/Grav/Common/Page/Medium/Medium.php @@ -536,7 +536,12 @@ class Medium extends Data implements RenderableInterface { $qs = $method; if (count($args) > 1 || (count($args) == 1 && !empty($args[0]))) { - $qs .= '=' . implode(',', array_map(function ($a) { return rawurlencode($a); }, $args)); + $qs .= '=' . implode(',', array_map(function ($a) { + if (is_array($a)) { + $a = '[' . implode(',', $a) . ']'; + } + return rawurlencode($a); + }, $args)); } if (!empty($qs)) { From a1abcfd067c60a5b6edb4d444b2121bf778789b2 Mon Sep 17 00:00:00 2001 From: Davide Liessi Date: Tue, 15 May 2018 19:38:48 +0200 Subject: [PATCH 06/11] Move fix for #1114 to Truncator::truncateLetters (#2004) * Move fix for #1114 to Truncator::truncateLetters The original fix provided by #1125 in Utils::truncateHtml compared the summary size with the full HTML string length. Thus, the string was still being truncated (and the HTML rewritten) even when only the HTML string length, and not the text length, exceeded the summary size. * Add fix for #1114 also to Truncator::truncateWords --- system/src/Grav/Common/Helpers/Truncator.php | 20 ++++++++++++++++++-- system/src/Grav/Common/Utils.php | 4 ---- tests/unit/Grav/Common/UtilsTest.php | 4 ++-- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/system/src/Grav/Common/Helpers/Truncator.php b/system/src/Grav/Common/Helpers/Truncator.php index c2e734989..b2a19dbc2 100644 --- a/system/src/Grav/Common/Helpers/Truncator.php +++ b/system/src/Grav/Common/Helpers/Truncator.php @@ -47,6 +47,7 @@ class Truncator { // Iterate over words. $words = new DOMWordsIterator($body); + $truncated = false; foreach ($words as $word) { // If we have exceeded the limit, we delete the remainder of the content. @@ -70,12 +71,19 @@ class Truncator { self::insertEllipsis($curNode, $ellipsis); } + $truncated = true; + break; } } - return self::innerHTML($body); + // Return original HTML if not truncated. + if ($truncated) { + return self::innerHTML($body); + } else { + return $html; + } } /** @@ -98,6 +106,7 @@ class Truncator { // Iterate over letters. $letters = new DOMLettersIterator($body); + $truncated = false; foreach ($letters as $letter) { // If we have exceeded the limit, we want to delete the remainder of this document. @@ -111,11 +120,18 @@ class Truncator { self::insertEllipsis($currentText[0], $ellipsis); } + $truncated = true; + break; } } - return self::innerHTML($body); + // Return original HTML if not truncated. + if ($truncated) { + return self::innerHTML($body); + } else { + return $html; + } } /** diff --git a/system/src/Grav/Common/Utils.php b/system/src/Grav/Common/Utils.php index 2736fd904..02d5e158a 100644 --- a/system/src/Grav/Common/Utils.php +++ b/system/src/Grav/Common/Utils.php @@ -296,10 +296,6 @@ abstract class Utils */ public static function truncateHtml($text, $length = 100, $ellipsis = '...') { - if (mb_strlen($text) <= $length) { - return $text; - } - return Truncator::truncateLetters($text, $length, $ellipsis); } diff --git a/tests/unit/Grav/Common/UtilsTest.php b/tests/unit/Grav/Common/UtilsTest.php index c4abc32ac..889a7db3b 100644 --- a/tests/unit/Grav/Common/UtilsTest.php +++ b/tests/unit/Grav/Common/UtilsTest.php @@ -127,7 +127,7 @@ class UtilsTest extends \Codeception\TestCase\Test $this->assertEquals('

This...

', Utils::truncateHtml('

This is a string to truncate

', 4)); $this->assertEquals('

This is a...

', Utils::truncateHtml('

This is a string to truncate

', 10)); $this->assertEquals('

This is a string to truncate

', Utils::truncateHtml('

This is a string to truncate

', 100)); - $this->assertEquals('', Utils::truncateHtml('', 6)); + $this->assertEquals('', Utils::truncateHtml('', 6)); $this->assertEquals('
  1. item 1 so...
', Utils::truncateHtml('
  1. item 1 something
  2. item 2 bold
', 10)); $this->assertEquals("

This is a string.

\n

It splits two lines.

", Utils::truncateHtml("

This is a string.

\n

It splits two lines.

", 100)); } @@ -138,7 +138,7 @@ class UtilsTest extends \Codeception\TestCase\Test $this->assertEquals('

This is...

', Utils::safeTruncateHtml('

This is a string to truncate

', 2)); $this->assertEquals('

This is a string to...

', Utils::safeTruncateHtml('

This is a string to truncate

', 5)); $this->assertEquals('

This is a string to truncate

', Utils::safeTruncateHtml('

This is a string to truncate

', 20)); - $this->assertEquals('', Utils::safeTruncateHtml('', 6)); + $this->assertEquals('', Utils::safeTruncateHtml('', 6)); $this->assertEquals('
  1. item 1 something
  2. item 2...
', Utils::safeTruncateHtml('
  1. item 1 something
  2. item 2 bold
', 5)); } From e60fd824008be1c210908b25c945c54767ecd3ea Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Tue, 15 May 2018 19:39:50 +0200 Subject: [PATCH 07/11] Add MIME type to video tag (#1992) --- system/src/Grav/Common/Page/Medium/VideoMedium.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/system/src/Grav/Common/Page/Medium/VideoMedium.php b/system/src/Grav/Common/Page/Medium/VideoMedium.php index b3c8eed85..e36ccd71e 100644 --- a/system/src/Grav/Common/Page/Medium/VideoMedium.php +++ b/system/src/Grav/Common/Page/Medium/VideoMedium.php @@ -22,10 +22,13 @@ class VideoMedium extends Medium protected function sourceParsedownElement(array $attributes, $reset = true) { $location = $this->url($reset); + $path = parse_url($location, PHP_URL_PATH); + $extension = pathinfo($path, PATHINFO_EXTENSION); + $mimeType = \Grav\Common\Utils::getMimeByExtension($extension); return [ 'name' => 'video', - 'text' => 'Your browser does not support the video tag.', + 'text' => 'Your browser does not support the video tag.', 'attributes' => $attributes ]; } From b4d570fd2152130a42d0e229573078dc4da709cd Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Tue, 15 May 2018 11:43:37 -0600 Subject: [PATCH 08/11] updated changelog --- CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4154bf9f2..3adb60f28 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,10 @@ 1. [](#bugfix) * Fixed an issue with some users getting **2FA** prompt after upgrade [admin#1442](https://github.com/getgrav/grav-plugin-admin/issues/1442) - + * Do not crash when generating URLs with arrays as parameters [#2018](https://github.com/getgrav/grav/pull/2018) + * Utils::truncateHTML removes whitespace when generating summaries [#2004](https://github.com/getgrav/grav/pull/2004) + * Add MIME type to `