From 0e4cf45a800cbc0aa475782be38d5f9196017feb Mon Sep 17 00:00:00 2001 From: PMKuipers Date: Wed, 14 Feb 2024 23:34:32 +0100 Subject: [PATCH] Tweaksed premium backend --- classes/premium.php | 81 +++++++++++++++++++++++---------- lang/en/local_treestudyplan.php | 8 +++- lang/nl/local_treestudyplan.php | 8 +++- 3 files changed, 69 insertions(+), 28 deletions(-) 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: '; $string["setting_premium_status"] = 'Current premium status'; $string["setting_premium_key"] = 'Activation key'; $string["settingdesc_premium_key"] = 'Paste the premium key you received in the box above.'; -$string["premiumfeature:morestudyplans"] = 'Creating more than 5 studyplans in a single category is a premium feature.'; \ No newline at end of file +$string["premiumfeature:morestudyplans"] = 'Creating more than 5 studyplans in a single category is a premium feature.'; +$string["premiumfeature:morecategories"] = 'Creating studyplans in more than 20 categories is a premium feature.'; \ No newline at end of file diff --git a/lang/nl/local_treestudyplan.php b/lang/nl/local_treestudyplan.php index d4cab91..919098b 100644 --- a/lang/nl/local_treestudyplan.php +++ b/lang/nl/local_treestudyplan.php @@ -436,10 +436,12 @@ $string["error:nostudyplaneditaccess"] = 'Fout: Je hebt geen rechten om studiepl $string["error:nocategoriesvisible"] = 'Fout: Je kunt geen cursussen in een categorie bekijken. Daarom blijft de cursuslijst leeg'; $string["premium:never"] = 'nooit'; -$string["premium:onsite"] = 'voor site {$a->website}'; +$string["premium:anywhere"] = 'voor gebruik overal'; +$string["premium:onsite"] = 'voor gebruik op {$a->website}'; $string["premium:active"] = 'Premium toegang ingeschakeld.
Uitgegeven aan {$a->name} {$a->sitestatus}
Verloopt op{$a->expires}'; $string["premium:notregistered"] = 'Premium toegang staat uitgeschakeld'; $string["premium:invalidactivationcontent"] = 'Premium activeringssleutel niet herkend'; +$string["premium:siteinvalid"] = 'Premium toegang staat uitgeschakeld. Activeringssleutel is alleen voor gebruik op {$a}'; $string["premium:expired"] = 'Premium toegang is verlopen op {$a->expires}
Was uitgegeven aan {$a->name} {$a->sitestatus}'; $string["settingspage_premium"] = 'Premium registration'; $string["setting_premium_heading"] = 'Premium features'; @@ -447,6 +449,7 @@ $string["settingdesc_premium_heading"] = 'Voor premium toegang is een activering
Premium toegang bevat onder andere:
  • Meer dan 5 studieplannen per categorie aan kunnen maken
  • +
  • In meer dan 20 categorieën studieplannen kunnen aanmaken
  • Overzichtsrapport van alle resultaten van studenten in een studieplan
  • Leerlijnen die student kan ontsluiten
'; @@ -454,4 +457,5 @@ $string["setting_premium_status"] = 'Premium status'; $string["setting_premium_key"] = 'Activation key'; $string["settingdesc_premium_key"] = 'Premium activation key'; -$string["premiumfeature:morestudyplans"] = 'Meer dan 5 studieplannen in één categorie aanmaken kan alleen met premium toegang.'; \ No newline at end of file +$string["premiumfeature:morestudyplans"] = 'Meer dan 5 studieplannen in één categorie aanmaken kan alleen met premium toegang.'; +$string["premiumfeature:morecategories"] = 'In meer dan 20 categoriën een studieplan aanmaken kan alleen met premium toegang.'; \ No newline at end of file