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() {
$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);
}

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["premium:never"] = 'never';
$string["premium:anywhere"] = 'for use anywhere';
$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:notregistered"] = 'Premium access <b>disabled</b> ';
$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["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
<br>Premium features include:
<ul>
<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>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>';
$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.';
$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["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:notregistered"] = 'Premium toegang staat <b>uitgeschakeld</n>';
$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["settingspage_premium"] = 'Premium registration';
$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:
<ul>
<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>Leerlijnen die student kan ontsluiten</li>
</ul>';
@ -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.';
$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.';