From 8ff20034118183f8671cd233b0d7447dfcfbdfc6 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Fri, 12 May 2017 14:47:55 -0600 Subject: [PATCH] Added base32/64 encode/decode twig filters --- CHANGELOG.md | 1 + system/src/Grav/Common/Twig/TwigExtension.php | 50 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ee0181f0..c7f7f94c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ 1. [](#new) * Added `Pages::baseUrl()`, `Pages::homeUrl()` and `Pages::url()` functions + * Added `base32_encode`, `base32_decode`, `base64_encode`, `base64_decode` Twig filters * Added `Debugger::getCaller()` to figure out where the method was called from * Added support for custom output providers like Slim Framework * Added `Grav\Framework\Collection` classes for creating collections diff --git a/system/src/Grav/Common/Twig/TwigExtension.php b/system/src/Grav/Common/Twig/TwigExtension.php index e23d2cfc9..96f26020b 100644 --- a/system/src/Grav/Common/Twig/TwigExtension.php +++ b/system/src/Grav/Common/Twig/TwigExtension.php @@ -13,6 +13,7 @@ use Grav\Common\Markdown\Parsedown; use Grav\Common\Markdown\ParsedownExtra; use Grav\Common\Uri; +use Grav\Common\Helpers\Base32; use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator; class TwigExtension extends \Twig_Extension @@ -71,6 +72,10 @@ public function getFilters() new \Twig_SimpleFilter('ltrim', [$this, 'ltrimFilter']), new \Twig_SimpleFilter('markdown', [$this, 'markdownFilter']), new \Twig_SimpleFilter('md5', [$this, 'md5Filter']), + new \Twig_SimpleFilter('base32_encode', [$this, 'base32EncodeFilter']), + new \Twig_SimpleFilter('base32_decode', [$this, 'base32DecodeFilter']), + new \Twig_SimpleFilter('base64_encode', [$this, 'base64EncodeFilter']), + new \Twig_SimpleFilter('base64_decode', [$this, 'base64DecodeFilter']), new \Twig_SimpleFilter('nicetime', [$this, 'nicetimeFilter']), new \Twig_SimpleFilter('randomize', [$this, 'randomizeFilter']), new \Twig_SimpleFilter('modulus', [$this, 'modulusFilter']), @@ -274,6 +279,51 @@ public function md5Filter($str) return md5($str); } + /** + * Return Base32 encoded string + * + * @param $str + * @return string + */ + public function base32EncodeFilter($str) + { + return Base32::encode($str); + } + + /** + * Return Base32 decoded string + * + * @param $str + * @return bool|string + */ + public function base32DecodeFilter($str) + { + return Base32::decode($str); + } + + /** + * Return Base64 encoded string + * + * @param $str + * @return string + */ + public function base64EncodeFilter($str) + { + return base64_encode($str); + } + + /** + * Return Base64 decoded string + * + * @param $str + * @return bool|string + */ + public function base64DecodeFilter($str) + { + return base64_decode($str); + } + + /** * Sorts a collection by key *