From fd858248af3c71896f23dbf1d6b38c6281b97acd Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Wed, 17 May 2017 10:31:27 -0600 Subject: [PATCH 1/9] Added `media` and `vardump` Twig functions --- system/src/Grav/Common/Twig/TwigExtension.php | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/system/src/Grav/Common/Twig/TwigExtension.php b/system/src/Grav/Common/Twig/TwigExtension.php index 71407c8b1..a8473e45d 100644 --- a/system/src/Grav/Common/Twig/TwigExtension.php +++ b/system/src/Grav/Common/Twig/TwigExtension.php @@ -980,4 +980,35 @@ class TwigExtension extends \Twig_Extension } } } + + /** + * Process a folder as Media and return a media object + * + * @param $media_dir + * @return Media + */ + public function mediaFunc($media_dir) + { + /** @var UniformResourceLocator $locator */ + $locator = $this->grav['locator']; + + if ($locator->isStream($media_dir)) { + $media_dir = $locator->findResource($media_dir); + } + + if (file_exists($media_dir)) { + return new Media($media_dir); + } + + } + + /** + * Dump a variable to the browser + * + * @param $var + */ + public function vardumpFunc($var) + { + var_dump($var); + } } From 18f97b067d429e33ea0c367254033cbf027bbfb7 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Wed, 17 May 2017 10:31:56 -0600 Subject: [PATCH 2/9] Various checks for Exif existence before using --- CHANGELOG.md | 10 ++++++- system/src/Grav/Common/Helpers/Exif.php | 17 ++++++++--- system/src/Grav/Common/Page/Media.php | 6 ++-- system/src/Grav/Common/Twig/TwigExtension.php | 30 ++++++++++++++----- 4 files changed, 48 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c394555e6..b26cdcbe7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ +# v1.3.0-rc.2 +## 05/17/2017 + +1. [](#new) + * Added new `media` and `vardump` Twig functions +1. [](#improved) + * Put in various checks to ensure Exif is available before trying to use + # v1.3.0-rc.1 -## 15/16/2017 +## 05/16/2017 1. [](#new) * Added support for a single array field in the forms diff --git a/system/src/Grav/Common/Helpers/Exif.php b/system/src/Grav/Common/Helpers/Exif.php index 9e7a423ae..e33cc7366 100644 --- a/system/src/Grav/Common/Helpers/Exif.php +++ b/system/src/Grav/Common/Helpers/Exif.php @@ -16,12 +16,21 @@ class Exif public function __construct() { - if (function_exists('exif_read_data') && class_exists('\PHPExif\Reader\Reader')) { - $this->reader = \PHPExif\Reader\Reader::factory(\PHPExif\Reader\Reader::TYPE_NATIVE); - } else { - if (Grav::instance()['config']->get('system.media.auto_metadata_exif')) { + if (Grav::instance()['config']->get('system.media.auto_metadata_exif')) { + if (function_exists('exif_read_data') && class_exists('\PHPExif\Reader\Reader')) { + $this->reader = \PHPExif\Reader\Reader::factory(\PHPExif\Reader\Reader::TYPE_NATIVE); + } else { throw new \Exception('Please enable the Exif extension for PHP or disable Exif support in Grav system configuration'); } } } + + public function getReader() + { + if ($this->reader) { + return $this->reader; + } + + return false; + } } diff --git a/system/src/Grav/Common/Page/Media.php b/system/src/Grav/Common/Page/Media.php index 3c5256a49..9d8a40cf6 100644 --- a/system/src/Grav/Common/Page/Media.php +++ b/system/src/Grav/Common/Page/Media.php @@ -64,7 +64,7 @@ class Media extends AbstractMedia protected function init() { $config = Grav::instance()['config']; - $exif = Grav::instance()['exif']; + $exif_reader = isset(Grav::instance()['exif']) ? Grav::instance()['exif']->getReader() : false; // Handle special cases where page doesn't exist in filesystem. if (!is_dir($this->path)) { @@ -124,9 +124,9 @@ class Media extends AbstractMedia } // Read/store Exif metadata as required - if (!empty($types['base']) && $medium->get('mime') === 'image/jpeg' && empty($types['meta']) && $config->get('system.media.auto_metadata_exif')) { + if (!empty($types['base']) && $medium->get('mime') === 'image/jpeg' && empty($types['meta']) && $config->get('system.media.auto_metadata_exif') && $exif_reader) { $file_path = $types['base']['file']; - $meta = $exif->reader->read($file_path); + $meta = $exif_reader->read($file_path); if ($meta) { $meta_path = $file_path . '.meta.yaml'; diff --git a/system/src/Grav/Common/Twig/TwigExtension.php b/system/src/Grav/Common/Twig/TwigExtension.php index a8473e45d..fc60bbdeb 100644 --- a/system/src/Grav/Common/Twig/TwigExtension.php +++ b/system/src/Grav/Common/Twig/TwigExtension.php @@ -9,6 +9,7 @@ namespace Grav\Common\Twig; use Grav\Common\Grav; +use Grav\Common\Page\Media; use Grav\Common\Utils; use Grav\Common\Markdown\Parsedown; use Grav\Common\Markdown\ParsedownExtra; @@ -109,6 +110,7 @@ class TwigExtension extends \Twig_Extension new \Twig_simpleFunction('authorize', [$this, 'authorize']), new \Twig_SimpleFunction('debug', [$this, 'dump'], ['needs_context' => true, 'needs_environment' => true]), new \Twig_SimpleFunction('dump', [$this, 'dump'], ['needs_context' => true, 'needs_environment' => true]), + new \Twig_SimpleFunction('vardump', [$this, 'vardumpFunc']), new \Twig_SimpleFunction('evaluate', [$this, 'evaluateStringFunc'], ['needs_context' => true, 'needs_environment' => true]), new \Twig_SimpleFunction('evaluate_twig', [$this, 'evaluateTwigFunc'], ['needs_context' => true, 'needs_environment' => true]), new \Twig_SimpleFunction('gist', [$this, 'gistFunc']), @@ -126,6 +128,8 @@ class TwigExtension extends \Twig_Extension new \Twig_SimpleFunction('range', [$this, 'rangeFunc']), new \Twig_SimpleFunction('isajaxrequest', [$this, 'isAjaxFunc']), new \Twig_SimpleFunction('exif', [$this, 'exifFunc']), + new \Twig_SimpleFunction('media', [$this, 'mediaFunc']), + ]; } @@ -967,15 +971,27 @@ class TwigExtension extends \Twig_Extension */ public function exifFunc($image, $raw = false) { - if (file_exists($image)) { + if (isset($this->grav['exif'])) { - $exif_data = $this->grav['exif']->reader->read($image); + /** @var UniformResourceLocator $locator */ + $locator = $this->grav['locator']; - if ($exif_data) { - if ($raw) { - return $exif_data->getRawData(); - } else { - return $exif_data->getData(); + if ($locator->isStream($image)) { + $image = $locator->findResource($image); + } + + $exif_reader = $this->grav['exif']->getReader(); + + if (file_exists($image) && $this->config->get('system.media.auto_metadata_exif') && $exif_reader) { + + $exif_data = $exif_reader->read($image); + + if ($exif_data) { + if ($raw) { + return $exif_data->getRawData(); + } else { + return $exif_data->getData(); + } } } } From 5c0d378d1e5cbd6aed3a588fe4973bbbc702e190 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Wed, 17 May 2017 11:08:07 -0600 Subject: [PATCH 3/9] changelog typos --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b26cdcbe7..9385f76dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,9 +2,9 @@ ## 05/17/2017 1. [](#new) - * Added new `media` and `vardump` Twig functions + * Added new `media` and `vardump` Twig functions 1. [](#improved) - * Put in various checks to ensure Exif is available before trying to use + * Put in various checks to ensure Exif is available before trying to use it # v1.3.0-rc.1 ## 05/16/2017 From 8dabe88246478c6aa0cb76a281d0bd69f6cdc31c Mon Sep 17 00:00:00 2001 From: Flavio Copes Date: Wed, 17 May 2017 19:42:27 +0200 Subject: [PATCH 4/9] Avoid validation on a YAML field, do not parse YAML (#1480) YAML parsing causes a fatal error if the field is not a string --- system/src/Grav/Common/Data/Validation.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/system/src/Grav/Common/Data/Validation.php b/system/src/Grav/Common/Data/Validation.php index b9ba8f90e..6f1bbe1c7 100644 --- a/system/src/Grav/Common/Data/Validation.php +++ b/system/src/Grav/Common/Data/Validation.php @@ -38,6 +38,11 @@ class Validation $field['type'] = 'text'; } + // If this is a YAML field, stop validation + if (isset($field['yaml']) && $field['yaml'] === true) { + return $messages; + } + // Get language class. $language = Grav::instance()['language']; @@ -98,13 +103,7 @@ class Validation // If this is a YAML field, simply parse it and return the value. if (isset($field['yaml']) && $field['yaml'] === true) { - try { - $yaml = new Parser(); - - return $yaml->parse($value); - } catch (ParseException $e) { - throw new \RuntimeException($e->getMessage()); - } + return $value; } // Validate type with fallback type text. From dd058155c8ade57fbaee0f68a3e0615792923c9e Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Wed, 17 May 2017 11:44:07 -0600 Subject: [PATCH 5/9] Updated changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9385f76dc..091bb3a94 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ * Added new `media` and `vardump` Twig functions 1. [](#improved) * Put in various checks to ensure Exif is available before trying to use it +1. [](#bugfix) + * Fix an issue saving YAML textarea fields in expert mode [#1480](https://github.com/getgrav/grav/pull/1480) # v1.3.0-rc.1 ## 05/16/2017 From cb36bdabdf014228807606a34479bad19bee2fa3 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Wed, 17 May 2017 12:11:48 -0600 Subject: [PATCH 6/9] Added timestamp on configurations #1480 --- CHANGELOG.md | 1 + .../src/Grav/Common/Config/CompiledBase.php | 19 ++++++++++++++++++- .../src/Grav/Common/Config/CompiledConfig.php | 1 + .../Grav/Common/Config/CompiledLanguages.php | 1 + system/src/Grav/Common/Config/Config.php | 10 ++++++++++ system/src/Grav/Common/Config/Languages.php | 9 +++++++++ 6 files changed, 40 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 091bb3a94..036095cd7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * Added new `media` and `vardump` Twig functions 1. [](#improved) * Put in various checks to ensure Exif is available before trying to use it + * Add timestamp to configuration settings [#1445](https://github.com/getgrav/grav/pull/1445) 1. [](#bugfix) * Fix an issue saving YAML textarea fields in expert mode [#1480](https://github.com/getgrav/grav/pull/1480) diff --git a/system/src/Grav/Common/Config/CompiledBase.php b/system/src/Grav/Common/Config/CompiledBase.php index f1e71f044..b4e2abb29 100644 --- a/system/src/Grav/Common/Config/CompiledBase.php +++ b/system/src/Grav/Common/Config/CompiledBase.php @@ -27,6 +27,11 @@ abstract class CompiledBase */ public $checksum; + /** + * @var string Timestamp of compiled configuration + */ + public $timestamp; + /** * @var string Cache folder to be used. */ @@ -59,9 +64,10 @@ abstract class CompiledBase throw new \BadMethodCallException('Cache folder not defined.'); } + $this->path = $path ? rtrim($path, '\\/') . '/' : ''; $this->cacheFolder = $cacheFolder; $this->files = $files; - $this->path = $path ? rtrim($path, '\\/') . '/' : ''; + $this->timestamp = 0; } /** @@ -84,6 +90,16 @@ abstract class CompiledBase */ public function modified() {} + /** + * Get timestamp of compiled configuration + * + * @return int Timestamp of compiled configuration + */ + public function timestamp() + { + return $this->timestamp ?: time(); + } + /** * Load the configuration. * @@ -196,6 +212,7 @@ abstract class CompiledBase } $this->createObject($cache['data']); + $this->timestamp = isset($cache['timestamp']) ? $cache['timestamp'] : 0; $this->finalizeObject(); diff --git a/system/src/Grav/Common/Config/CompiledConfig.php b/system/src/Grav/Common/Config/CompiledConfig.php index 1e064d90c..05d3ed9b1 100644 --- a/system/src/Grav/Common/Config/CompiledConfig.php +++ b/system/src/Grav/Common/Config/CompiledConfig.php @@ -77,6 +77,7 @@ class CompiledConfig extends CompiledBase protected function finalizeObject() { $this->object->checksum($this->checksum()); + $this->object->timestamp($this->timestamp()); } /** diff --git a/system/src/Grav/Common/Config/CompiledLanguages.php b/system/src/Grav/Common/Config/CompiledLanguages.php index b6fbe37b9..94d2e3a4e 100644 --- a/system/src/Grav/Common/Config/CompiledLanguages.php +++ b/system/src/Grav/Common/Config/CompiledLanguages.php @@ -38,6 +38,7 @@ class CompiledLanguages extends CompiledBase protected function finalizeObject() { $this->object->checksum($this->checksum()); + $this->object->timestamp($this->timestamp()); } diff --git a/system/src/Grav/Common/Config/Config.php b/system/src/Grav/Common/Config/Config.php index faa596831..c8cf5aec3 100644 --- a/system/src/Grav/Common/Config/Config.php +++ b/system/src/Grav/Common/Config/Config.php @@ -17,6 +17,7 @@ class Config extends Data { protected $checksum; protected $modified = false; + protected $timestamp = 0; public function key() { @@ -41,6 +42,15 @@ class Config extends Data return $this->modified; } + public function timestamp($timestamp = null) + { + if ($timestamp !== null) { + $this->timestamp = $timestamp; + } + + return $this->timestamp; + } + public function reload() { $grav = Grav::instance(); diff --git a/system/src/Grav/Common/Config/Languages.php b/system/src/Grav/Common/Config/Languages.php index 50073df26..997b842c8 100644 --- a/system/src/Grav/Common/Config/Languages.php +++ b/system/src/Grav/Common/Config/Languages.php @@ -30,6 +30,15 @@ class Languages extends Data return $this->modified; } + public function timestamp($timestamp = null) + { + if ($timestamp !== null) { + $this->timestamp = $timestamp; + } + + return $this->timestamp; + } + public function reformat() { if (isset($this->items['plugins'])) { From 725e6ab3036be1c8361835bbdeeb4d9da7c3728c Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Wed, 17 May 2017 12:17:57 -0600 Subject: [PATCH 7/9] Prepare for release --- system/defines.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/defines.php b/system/defines.php index a65cfd026..bca033553 100644 --- a/system/defines.php +++ b/system/defines.php @@ -8,7 +8,7 @@ // Some standard defines define('GRAV', true); -define('GRAV_VERSION', '1.3.0-rc.1'); +define('GRAV_VERSION', '1.3.0-rc.2'); define('GRAV_TESTING', true); define('DS', '/'); From f31f6f8e04b53cd5d4a0ec9ab28cd672d1d2efe2 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Wed, 17 May 2017 14:31:59 -0600 Subject: [PATCH 8/9] Moved `onOutputRendered()` back into Grav core --- CHANGELOG.md | 1 + system/src/Grav/Common/Grav.php | 2 ++ system/src/Grav/Common/Processors/RenderProcessor.php | 2 -- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 036095cd7..6ef65c7a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * Add timestamp to configuration settings [#1445](https://github.com/getgrav/grav/pull/1445) 1. [](#bugfix) * Fix an issue saving YAML textarea fields in expert mode [#1480](https://github.com/getgrav/grav/pull/1480) + * Moved `onOutputRendered()` back into Grav core # v1.3.0-rc.1 ## 05/16/2017 diff --git a/system/src/Grav/Common/Grav.php b/system/src/Grav/Common/Grav.php index 304838d17..c68a74da6 100644 --- a/system/src/Grav/Common/Grav.php +++ b/system/src/Grav/Common/Grav.php @@ -129,6 +129,8 @@ class Grav extends Container $debugger = $this['debugger']; $debugger->render(); + $this->fireEvent('onOutputRendered'); + register_shutdown_function([$this, 'shutdown']); } diff --git a/system/src/Grav/Common/Processors/RenderProcessor.php b/system/src/Grav/Common/Processors/RenderProcessor.php index 623be38e6..459ce2785 100644 --- a/system/src/Grav/Common/Processors/RenderProcessor.php +++ b/system/src/Grav/Common/Processors/RenderProcessor.php @@ -30,7 +30,5 @@ class RenderProcessor extends ProcessorBase implements ProcessorInterface echo $output; } - - $container->fireEvent('onOutputRendered'); } } From ffe286ea49fd5857542f6c4736293bbd2140ff03 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Wed, 17 May 2017 15:29:21 -0600 Subject: [PATCH 9/9] cleanup output --- system/src/Grav/Common/Processors/RenderProcessor.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/system/src/Grav/Common/Processors/RenderProcessor.php b/system/src/Grav/Common/Processors/RenderProcessor.php index 459ce2785..a55b42847 100644 --- a/system/src/Grav/Common/Processors/RenderProcessor.php +++ b/system/src/Grav/Common/Processors/RenderProcessor.php @@ -29,6 +29,9 @@ class RenderProcessor extends ProcessorBase implements ProcessorInterface $container->header(); echo $output; + + // remove any output + $container->output = ''; } } }