From 0cc01d33552c95b32339d310774999301ac3b799 Mon Sep 17 00:00:00 2001 From: Djamil Legato Date: Tue, 27 Sep 2016 11:29:59 -0700 Subject: [PATCH] Added new `Licenses::validate` method to validate the format of a License --- system/src/Grav/Common/GPM/Licenses.php | 33 +++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/system/src/Grav/Common/GPM/Licenses.php b/system/src/Grav/Common/GPM/Licenses.php index 510711d35..9a8798595 100644 --- a/system/src/Grav/Common/GPM/Licenses.php +++ b/system/src/Grav/Common/GPM/Licenses.php @@ -11,8 +11,21 @@ namespace Grav\Common\GPM; use Grav\Common\Grav; use RocketTheme\Toolbox\File\YamlFile; +/** + * Class Licenses + * + * @package Grav\Common\GPM + */ class Licenses { + + /** + * Regex to validate the format of a License + * + * @var string + */ + protected static $regex = '^(?:[A-F0-9]{8}-){3}(?:[A-F0-9]{8}){1}$'; + /** * Returns the license for a Premium package * @@ -66,6 +79,26 @@ class Licenses return $data['licenses'][$slug]; } + + /** + * Validates the License format + * + * @param $license + * + * @return bool + */ + public static function validate($license = null) + { + if (!is_string($license)) { + return false; + } + + return preg_match('#' . self::$regex. '#', $license); + } + + /** + * @return string + */ protected static function getLicensePath() { $path = Grav::instance()['locator']->findResource('user://data') . '/licenses.yaml';