Tweaksed premium backend

This commit is contained in:
PMKuipers 2024-02-14 23:34:32 +01:00
parent 3cf6459ce8
commit 0e4cf45a80
3 changed files with 69 additions and 28 deletions

View file

@ -125,18 +125,39 @@ Klc5I28bGbvxIV5pnL6ZSjHEDp2WreM8HB0XFJwU+Q==
} }
} }
/**
* Check if premium status is enabled
* @return bool
*/
public static function enabled() { public static function enabled() {
$status = self::premiumStatus(); $status = self::premiumStatus();
return $status->enabled; 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() { protected static function premiumStatus() {
if (!isset(self::$cachedpremiumstatus)) { if (!isset(self::$cachedpremiumstatus)) {
// Initialize default object. // Initialize default object.
$o = new \stdClass; $o = new \stdClass;
$o->enabled = false; $o->enabled = false;
$o->intent = ""; $o->intents = [];
$o->name = ""; $o->name = "";
$o->website = ""; $o->website = "";
$o->expires = ""; $o->expires = "";
@ -155,14 +176,19 @@ Klc5I28bGbvxIV5pnL6ZSjHEDp2WreM8HB0XFJwU+Q==
if (is_object($decoded)) { if (is_object($decoded)) {
$keys = ["intent","name","website","expires","issued"]; // Copy basic data/
$keys = ["name","website","expires","issued"];
foreach ( $keys as $k) { foreach ( $keys as $k) {
if (isset($decoded->$k)) { if (isset($decoded->$k)) {
$o->$k = $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(); $now = new \DateTime();
$issuedate = new \DateTime($o->issued); $issuedate = new \DateTime($o->issued);
$expirydate = new \DateTime(); // Default to now $expirydate = new \DateTime(); // Default to now
@ -174,7 +200,10 @@ Klc5I28bGbvxIV5pnL6ZSjHEDp2WreM8HB0XFJwU+Q==
$expirydate = new \DateTime($o->expires); $expirydate = new \DateTime($o->expires);
} catch (\Exception $x) {} } catch (\Exception $x) {}
} }
if ($o->intent == 'treestudyplan'
if ( \in_array('treestudyplan',$o->intents)
&& !empty($o->issued) && !empty($o->issued)
&& self::website_match($o->website) && self::website_match($o->website)
) { ) {
@ -205,15 +234,18 @@ Klc5I28bGbvxIV5pnL6ZSjHEDp2WreM8HB0XFJwU+Q==
return self::$cachedpremiumstatus; return self::$cachedpremiumstatus;
} }
/**
* Check if the current site matches the provided key
*/
private static function website_match($key) { private static function website_match($key) {
global $CFG; global $CFG;
$site = $CFG->wwwroot; $site = $CFG->wwwroot;
// Add double slashes to key and site if no scheme is set. // Add double slashes to key and site if no scheme is set.
// Basically: if no double slashes present before any dots,shashes or @s. // Basically: if no double slashes present before any dots,shashes or @s.
if(!preg_match_all('#^[^./@]*?//#',$key )) { if(!\preg_match_all('#^[^./@]*?//#',$key )) {
$key = "//".$key; $key = "//".$key;
} }
if(!preg_match_all('#^[^./@]*?//#',$site)) { if(!\preg_match_all('#^[^./@]*?//#',$site)) {
$site = "//".$site; $site = "//".$site;
} }
// Use parse_url() to split path and host. // Use parse_url() to split path and host.
@ -222,26 +254,16 @@ Klc5I28bGbvxIV5pnL6ZSjHEDp2WreM8HB0XFJwU+Q==
// No match if host is empty on key or site // No match if host is empty on key or site
if (empty($keyurl->host) || empty($siteurl->host)) { 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; return false;
} }
// First match the host part. // First match the host part.
$keyparts = array_reverse(explode(".",$keyurl->host)); $keyparts = \array_reverse(\explode(".",$keyurl->host));
$siteparts = array_reverse(explode(".",$siteurl->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. // 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($keyparts)) != "www") {\array_push($keyparts,$x);}
if (($x = array_pop($siteparts)) != "www") {array_push($siteparts,$x);} if (($x = \array_pop($siteparts)) != "www") {\array_push($siteparts,$x);}
for ($i = 0; $i < count($keyparts); $i++) { 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 * // 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; $sitepath = empty($siteurl->path)?"/":$siteurl->path;
// Trim trailing / from both paths before comparison // Trim trailing / from both paths before comparison
if (strlen($sitepath) > 1) { if (\strlen($sitepath) > 1) {
$sitepath = \rtrim($sitepath,"/"); $sitepath = \rtrim($sitepath,"/");
} }
if (strlen($keypath) > 1) { if (\strlen($keypath) > 1) {
$keypath = \rtrim($keypath,"/"); $keypath = \rtrim($keypath,"/");
} }
@ -282,6 +304,9 @@ Klc5I28bGbvxIV5pnL6ZSjHEDp2WreM8HB0XFJwU+Q==
return \fnmatch($keypath,$sitepath,\FNM_CASEFOLD); return \fnmatch($keypath,$sitepath,\FNM_CASEFOLD);
} }
/**
* Parameter description for webservice function get_premiumstatus
*/
public static function get_premiumstatus_parameters() : \external_function_parameters { public static function get_premiumstatus_parameters() : \external_function_parameters {
return new \external_function_parameters([]); return new \external_function_parameters([]);
} }
@ -296,6 +321,7 @@ Klc5I28bGbvxIV5pnL6ZSjHEDp2WreM8HB0XFJwU+Q==
"name" => new \external_value(PARAM_TEXT, 'premium registration name'), "name" => new \external_value(PARAM_TEXT, 'premium registration name'),
"expires" => new \external_value(PARAM_TEXT, 'premium registration expiry date'), "expires" => new \external_value(PARAM_TEXT, 'premium registration expiry date'),
"expired" => new \external_value(PARAM_BOOL, 'premium status expired'), "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", "name",
"expires", "expires",
"expired", "expired",
"intents",
]; ];
$result = []; $result = [];
@ -320,6 +347,10 @@ Klc5I28bGbvxIV5pnL6ZSjHEDp2WreM8HB0XFJwU+Q==
return $result; return $result;
} }
/**
* Get a descriptive text of the current premium status
* @return string HTML description of premium status.
*/
public static function statusdescription() { public static function statusdescription() {
$status = self::premiumStatus(); $status = self::premiumStatus();
@ -330,13 +361,15 @@ Klc5I28bGbvxIV5pnL6ZSjHEDp2WreM8HB0XFJwU+Q==
if ($status->website != "*") { if ($status->website != "*") {
$msg->sitestatus = \get_string("premium:onsite",'local_treestudyplan',$status); $msg->sitestatus = \get_string("premium:onsite",'local_treestudyplan',$status);
} else { } else {
$msg->sitestatus = ""; $msg->sitestatus = \get_string("premium:anywhere",'local_treestudyplan');;
} }
if($status->enabled) { if($status->enabled) {
return \get_string("premium:active",'local_treestudyplan',$msg); return \get_string("premium:active",'local_treestudyplan',$msg);
} else if ($status->expired) { } else if ($status->expired) {
return \get_string("premium:expired",'local_treestudyplan',$msg); 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 { } else {
return \get_string("premium:notregistered",'local_treestudyplan',$msg); return \get_string("premium:notregistered",'local_treestudyplan',$msg);
} }

View file

@ -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["error:nocategoriesvisible"] = 'Error: You have no viewing permissions in any category. Therefore the course list remains empty.';
$string["premium:never"] = 'never'; $string["premium:never"] = 'never';
$string["premium:anywhere"] = 'for use anywhere';
$string["premium:onsite"] = 'for use on site <b>{$a->website}</b>'; $string["premium:onsite"] = 'for use on site <b>{$a->website}</b>';
$string["premium:active"] = 'Premium access <b class="text-success">enabled</b>.<br> Registered to <b>{$a->name}</b> {$a->sitestatus}<br>Expires <b>{$a->expires}</b>'; $string["premium:active"] = 'Premium access <b class="text-success">enabled</b>.<br> Registered to <b>{$a->name}</b> {$a->sitestatus}<br>Expires <b>{$a->expires}</b>';
$string["premium:notregistered"] = 'Premium access <b>disabled</b> '; $string["premium:notregistered"] = 'Premium access <b>disabled</b> ';
$string["premium:invalidactivationcontent"] = 'Premium activation key not recognized'; $string["premium:invalidactivationcontent"] = 'Premium activation key not recognized';
$string["premium:siteinvalid"] = 'Premium access <b>disabled</b>. Activation key is only valid for use on <b>{$a}</b>';
$string["premium:expired"] = 'Premium access <b class="text-danger">expired</b> on <b>{$a->expires}</b><br>Was registered to <b>{$a->name}</b> {$a->sitestatus}'; $string["premium:expired"] = 'Premium access <b class="text-danger">expired</b> on <b>{$a->expires}</b><br>Was registered to <b>{$a->name}</b> {$a->sitestatus}';
$string["settingspage_premium"] = 'Premium registration'; $string["settingspage_premium"] = 'Premium registration';
$string["setting_premium_heading"] = 'Premium features'; $string["setting_premium_heading"] = 'Premium features';
@ -447,11 +449,13 @@ $string["settingdesc_premium_heading"] = 'To access premium features, you need a
<br>Premium features include: <br>Premium features include:
<ul> <ul>
<li>Creating more than 5 studyplans per category</li> <li>Creating more than 5 studyplans per category</li>
<li>Creating studyplans in more than 20 categories</li>
<li>Overview of grades for all students in a studyplan</li> <li>Overview of grades for all students in a studyplan</li>
<li>Rows in a studplan which a student can choose to unlock</li> <li>Rows in a studyplan which a student can choose to unlock</li>
</ul>'; </ul>';
$string["setting_premium_status"] = 'Current premium status'; $string["setting_premium_status"] = 'Current premium status';
$string["setting_premium_key"] = 'Activation key'; $string["setting_premium_key"] = 'Activation key';
$string["settingdesc_premium_key"] = 'Paste the premium key you received in the box above.'; $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.'; $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.';

View file

@ -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["error:nocategoriesvisible"] = 'Fout: Je kunt geen cursussen in een categorie bekijken. Daarom blijft de cursuslijst leeg';
$string["premium:never"] = 'nooit'; $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 <b class="text-success">ingeschakeld</b>.<br>Uitgegeven aan <b>{$a->name}</b> {$a->sitestatus}<br>Verloopt op<b>{$a->expires}</b>'; $string["premium:active"] = 'Premium toegang <b class="text-success">ingeschakeld</b>.<br>Uitgegeven aan <b>{$a->name}</b> {$a->sitestatus}<br>Verloopt op<b>{$a->expires}</b>';
$string["premium:notregistered"] = 'Premium toegang staat <b>uitgeschakeld</n>'; $string["premium:notregistered"] = 'Premium toegang staat <b>uitgeschakeld</n>';
$string["premium:invalidactivationcontent"] = 'Premium activeringssleutel niet herkend'; $string["premium:invalidactivationcontent"] = 'Premium activeringssleutel niet herkend';
$string["premium:siteinvalid"] = 'Premium toegang staat <b>uitgeschakeld</n>. Activeringssleutel is alleen voor gebruik op <b>{$a}</b>';
$string["premium:expired"] = 'Premium toegang is <b class="text-danger">verlopen</b> op <b>{$a->expires}</b><br>Was uitgegeven aan <b>{$a->name}</b> {$a->sitestatus}'; $string["premium:expired"] = 'Premium toegang is <b class="text-danger">verlopen</b> op <b>{$a->expires}</b><br>Was uitgegeven aan <b>{$a->name}</b> {$a->sitestatus}';
$string["settingspage_premium"] = 'Premium registration'; $string["settingspage_premium"] = 'Premium registration';
$string["setting_premium_heading"] = 'Premium features'; $string["setting_premium_heading"] = 'Premium features';
@ -447,6 +449,7 @@ $string["settingdesc_premium_heading"] = 'Voor premium toegang is een activering
<br>Premium toegang bevat onder andere: <br>Premium toegang bevat onder andere:
<ul> <ul>
<li>Meer dan 5 studieplannen per categorie aan kunnen maken</li> <li>Meer dan 5 studieplannen per categorie aan kunnen maken</li>
<li>In meer dan 20 categorieën studieplannen kunnen aanmaken</li>
<li>Overzichtsrapport van alle resultaten van studenten in een studieplan</li> <li>Overzichtsrapport van alle resultaten van studenten in een studieplan</li>
<li>Leerlijnen die student kan ontsluiten</li> <li>Leerlijnen die student kan ontsluiten</li>
</ul>'; </ul>';
@ -455,3 +458,4 @@ $string["setting_premium_key"] = 'Activation key';
$string["settingdesc_premium_key"] = 'Premium 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.'; $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.';