diff --git a/classes/premium.php b/classes/premium.php
index e73f8de..b8c6271 100644
--- a/classes/premium.php
+++ b/classes/premium.php
@@ -125,18 +125,39 @@ Klc5I28bGbvxIV5pnL6ZSjHEDp2WreM8HB0XFJwU+Q==
}
}
-
+ /**
+ * Check if premium status is enabled
+ * @return bool
+ */
public static function enabled() {
$status = self::premiumStatus();
return $status->enabled;
}
+ /**
+ * Check if the premium key includes a specific intent in addition to the treestudyplan.
+ * @param string $intent The intent to search for
+ * @return bool
+ */
+ public static function has_intent($intent){
+ $status = self::premiumStatus();
+ if ($status->enabled) {
+ return \in_array(\strtolower($intent),$status->intents);
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * Determine, cache and retrieve premium status
+ * @return object
+ */
protected static function premiumStatus() {
if (!isset(self::$cachedpremiumstatus)) {
// Initialize default object.
$o = new \stdClass;
$o->enabled = false;
- $o->intent = "";
+ $o->intents = [];
$o->name = "";
$o->website = "";
$o->expires = "";
@@ -155,14 +176,19 @@ Klc5I28bGbvxIV5pnL6ZSjHEDp2WreM8HB0XFJwU+Q==
if (is_object($decoded)) {
- $keys = ["intent","name","website","expires","issued"];
+ // Copy basic data/
+ $keys = ["name","website","expires","issued"];
foreach ( $keys as $k) {
if (isset($decoded->$k)) {
$o->$k = $decoded->$k;
}
}
- // Convert dates to user dates
+ if(!empty($decoded->intent)) {
+ $o->intents = explode(",",$decoded->intent);
+ }
+
+ // Convert dates to DateTime for
$now = new \DateTime();
$issuedate = new \DateTime($o->issued);
$expirydate = new \DateTime(); // Default to now
@@ -174,7 +200,10 @@ Klc5I28bGbvxIV5pnL6ZSjHEDp2WreM8HB0XFJwU+Q==
$expirydate = new \DateTime($o->expires);
} catch (\Exception $x) {}
}
- if ($o->intent == 'treestudyplan'
+
+
+
+ if ( \in_array('treestudyplan',$o->intents)
&& !empty($o->issued)
&& self::website_match($o->website)
) {
@@ -205,15 +234,18 @@ Klc5I28bGbvxIV5pnL6ZSjHEDp2WreM8HB0XFJwU+Q==
return self::$cachedpremiumstatus;
}
+ /**
+ * Check if the current site matches the provided key
+ */
private static function website_match($key) {
global $CFG;
$site = $CFG->wwwroot;
// Add double slashes to key and site if no scheme is set.
// Basically: if no double slashes present before any dots,shashes or @s.
- if(!preg_match_all('#^[^./@]*?//#',$key )) {
+ if(!\preg_match_all('#^[^./@]*?//#',$key )) {
$key = "//".$key;
}
- if(!preg_match_all('#^[^./@]*?//#',$site)) {
+ if(!\preg_match_all('#^[^./@]*?//#',$site)) {
$site = "//".$site;
}
// Use parse_url() to split path and host.
@@ -222,26 +254,16 @@ Klc5I28bGbvxIV5pnL6ZSjHEDp2WreM8HB0XFJwU+Q==
// No match if host is empty on key or site
if (empty($keyurl->host) || empty($siteurl->host)) {
- if(empty($keyurl->host)){
- print "\e[91mError: no host in keyurl '{$key}'\n";
- print_r($keyurl);
- print "\e[0m";
- }
- if(empty($siteurl->host)){
- print "\e[91mError: no host in siteurl '{$site}'\n";
- print_r($siteurl);
- print "\e[0m";
- }
return false;
}
// First match the host part.
- $keyparts = array_reverse(explode(".",$keyurl->host));
- $siteparts = array_reverse(explode(".",$siteurl->host));
+ $keyparts = \array_reverse(\explode(".",$keyurl->host));
+ $siteparts = \array_reverse(\explode(".",$siteurl->host));
// Trim starting www from both parts, since site.domain and www.site.domain should be treated as the same.
- if (($x = array_pop($keyparts)) != "www") {array_push($keyparts,$x);}
- if (($x = array_pop($siteparts)) != "www") {array_push($siteparts,$x);}
+ if (($x = \array_pop($keyparts)) != "www") {\array_push($keyparts,$x);}
+ if (($x = \array_pop($siteparts)) != "www") {\array_push($siteparts,$x);}
for ($i = 0; $i < count($keyparts); $i++) {
// No match if the site does not have a part, but the key does. Unless the key part is *
@@ -271,10 +293,10 @@ Klc5I28bGbvxIV5pnL6ZSjHEDp2WreM8HB0XFJwU+Q==
$sitepath = empty($siteurl->path)?"/":$siteurl->path;
// Trim trailing / from both paths before comparison
- if (strlen($sitepath) > 1) {
+ if (\strlen($sitepath) > 1) {
$sitepath = \rtrim($sitepath,"/");
}
- if (strlen($keypath) > 1) {
+ if (\strlen($keypath) > 1) {
$keypath = \rtrim($keypath,"/");
}
@@ -282,6 +304,9 @@ Klc5I28bGbvxIV5pnL6ZSjHEDp2WreM8HB0XFJwU+Q==
return \fnmatch($keypath,$sitepath,\FNM_CASEFOLD);
}
+ /**
+ * Parameter description for webservice function get_premiumstatus
+ */
public static function get_premiumstatus_parameters() : \external_function_parameters {
return new \external_function_parameters([]);
}
@@ -296,6 +321,7 @@ Klc5I28bGbvxIV5pnL6ZSjHEDp2WreM8HB0XFJwU+Q==
"name" => new \external_value(PARAM_TEXT, 'premium registration name'),
"expires" => new \external_value(PARAM_TEXT, 'premium registration expiry date'),
"expired" => new \external_value(PARAM_BOOL, 'premium status expired'),
+ "intents" => new \external_multiple_structure(new \external_value(PARAM_TEXT),'additional intents included in the license '),
]);
}
@@ -311,6 +337,7 @@ Klc5I28bGbvxIV5pnL6ZSjHEDp2WreM8HB0XFJwU+Q==
"name",
"expires",
"expired",
+ "intents",
];
$result = [];
@@ -320,6 +347,10 @@ Klc5I28bGbvxIV5pnL6ZSjHEDp2WreM8HB0XFJwU+Q==
return $result;
}
+ /**
+ * Get a descriptive text of the current premium status
+ * @return string HTML description of premium status.
+ */
public static function statusdescription() {
$status = self::premiumStatus();
@@ -330,13 +361,15 @@ Klc5I28bGbvxIV5pnL6ZSjHEDp2WreM8HB0XFJwU+Q==
if ($status->website != "*") {
$msg->sitestatus = \get_string("premium:onsite",'local_treestudyplan',$status);
} else {
- $msg->sitestatus = "";
+ $msg->sitestatus = \get_string("premium:anywhere",'local_treestudyplan');;
}
if($status->enabled) {
return \get_string("premium:active",'local_treestudyplan',$msg);
} else if ($status->expired) {
return \get_string("premium:expired",'local_treestudyplan',$msg);
+ } else if (!self::website_match($status->website)) {
+ return \get_string("premium:siteinvalid",'local_treestudyplan',$status->website);
} else {
return \get_string("premium:notregistered",'local_treestudyplan',$msg);
}
diff --git a/lang/en/local_treestudyplan.php b/lang/en/local_treestudyplan.php
index e5e759e..bba2649 100644
--- a/lang/en/local_treestudyplan.php
+++ b/lang/en/local_treestudyplan.php
@@ -436,10 +436,12 @@ $string["error:nostudyplaneditaccess"] = 'Error: You do not have access to manag
$string["error:nocategoriesvisible"] = 'Error: You have no viewing permissions in any category. Therefore the course list remains empty.';
$string["premium:never"] = 'never';
+$string["premium:anywhere"] = 'for use anywhere';
$string["premium:onsite"] = 'for use on site {$a->website}';
$string["premium:active"] = 'Premium access enabled.
Registered to {$a->name} {$a->sitestatus}
Expires {$a->expires}';
$string["premium:notregistered"] = 'Premium access disabled ';
$string["premium:invalidactivationcontent"] = 'Premium activation key not recognized';
+$string["premium:siteinvalid"] = 'Premium access disabled. Activation key is only valid for use on {$a}';
$string["premium:expired"] = 'Premium access expired on {$a->expires}
Was registered to {$a->name} {$a->sitestatus}';
$string["settingspage_premium"] = 'Premium registration';
$string["setting_premium_heading"] = 'Premium features';
@@ -447,11 +449,13 @@ $string["settingdesc_premium_heading"] = 'To access premium features, you need a
Premium features include: