2024-02-14 23:01:34 +01:00
|
|
|
<?php
|
|
|
|
// This file is part of the Studyplan plugin for Moodle
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
/**
|
|
|
|
* Determine premium status
|
|
|
|
* @package local_treestudyplan
|
|
|
|
* @copyright 2023 P.M. Kuipers
|
|
|
|
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace local_treestudyplan;
|
|
|
|
defined('MOODLE_INTERNAL') || die();
|
|
|
|
|
|
|
|
require_once($CFG->libdir.'/externallib.php');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle badge information in the same style as the other classes
|
|
|
|
*/
|
|
|
|
class premium extends \external_api {
|
|
|
|
|
2024-06-02 23:23:32 +02:00
|
|
|
/**
|
|
|
|
* Toggle the variable below to enable support for premium stuff.
|
|
|
|
* If set to false, all premium features will be enabled and no premium settings panel will be visible.
|
|
|
|
* @var bool
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
private static $premiumsupported = false;
|
2024-02-18 13:47:08 +01:00
|
|
|
|
2024-06-02 23:23:32 +02:00
|
|
|
/**
|
|
|
|
* Certficate code
|
|
|
|
* @var string
|
|
|
|
*/
|
2024-02-14 23:01:34 +01:00
|
|
|
private static $premiumcrt = "-----BEGIN CERTIFICATE-----
|
|
|
|
MIIDSzCCAjMCFFlyhmKf1fN7U5lQL/dtlsyP24AQMA0GCSqGSIb3DQEBCwUAMGEx
|
|
|
|
CzAJBgNVBAYTAk5MMRYwFAYDVQQIDA1Ob29yZC1Ib2xsYW5kMRowGAYDVQQKDBFN
|
|
|
|
aXFyYSBFbmdpbmVlcmluZzEeMBwGA1UEAwwVVHJlZVN0dWR5cGxhbiBQcmVtaXVt
|
|
|
|
MCAXDTI0MDIxMDE2MDQwM1oYDzIxMjQwMTE3MTYwNDAzWjBhMQswCQYDVQQGEwJO
|
|
|
|
TDEWMBQGA1UECAwNTm9vcmQtSG9sbGFuZDEaMBgGA1UECgwRTWlxcmEgRW5naW5l
|
|
|
|
ZXJpbmcxHjAcBgNVBAMMFVRyZWVTdHVkeXBsYW4gUHJlbWl1bTCCASIwDQYJKoZI
|
|
|
|
hvcNAQEBBQADggEPADCCAQoCggEBAOD7+Nf5UBYGmIadI+kRM7vSPGA12F6cyZuZ
|
|
|
|
O/JsdCWzZx3cCgVYt29DxHRvFVGrhGGLsoaMY9iXc9LdeO02jKqL3RoPo2kc5moT
|
|
|
|
SNarsKZcGZXgqo5NATmdMLqQpKAy41H0ybgXZDLq5XKs9YIRlkwSpzQTNeP49mOl
|
|
|
|
48giVX3icbpMw1TdQotalKXAtcs62o+guQJNANpjBRxPXssrmDoNXrJcAtUjNOjx
|
|
|
|
8M+8tCmwkKwBoK8F3wWxIo04kZ9KILtybMmn4VJJ6SwLEf4StphTIoru8zS7XUt8
|
|
|
|
3HbV3PsiyYErPlwIcobfcjwZJpub23bzetvxRvhpeIpLhrTGrPMCAwEAATANBgkq
|
|
|
|
hkiG9w0BAQsFAAOCAQEAQwkbP6m3sdQgXEK3mYYZvvs6R/FI9QPu/9ICA+dgfj4y
|
|
|
|
7wvL0toYYR5oXdhO9At3MYmS+0bFUmqoTS+cxsC4COpEKFbRBWwbJ3NXAw14Hx2U
|
|
|
|
ELLqMZGJNOwNV+3ZdhADrwA++AjUqu144ObrcNUqo4+A4h9R8qj+o0J50Gvwja9R
|
|
|
|
Uh67LsF4Ls8fUtqzpqct94bUl6MPMHlH4qpZlgndmQdgOwLWeQEmM8X3WtSJH90S
|
|
|
|
n8FqBInMBhGu1uz0Qeo09ke0RHRnghP9EXfig/veMegASZeEhFqmS2Bdiy6gqeZ5
|
|
|
|
Klc5I28bGbvxIV5pnL6ZSjHEDp2WreM8HB0XFJwU+Q==
|
|
|
|
-----END CERTIFICATE-----";
|
|
|
|
|
2024-06-02 23:23:32 +02:00
|
|
|
/**
|
|
|
|
* Cached status
|
|
|
|
* @var object
|
|
|
|
*/
|
2024-02-14 23:01:34 +01:00
|
|
|
private static $cachedpremiumstatus = null;
|
|
|
|
|
2024-06-02 23:23:32 +02:00
|
|
|
/**
|
|
|
|
* Check if premium is supported
|
|
|
|
* @return bool
|
|
|
|
*/
|
2024-02-18 13:47:08 +01:00
|
|
|
public static function supported() {
|
2024-06-02 19:23:40 +02:00
|
|
|
return self::$premiumsupported;
|
2024-02-18 13:47:08 +01:00
|
|
|
}
|
|
|
|
|
2024-06-02 23:23:32 +02:00
|
|
|
/**
|
|
|
|
* Decrypt encrypted data
|
|
|
|
* @param string $encrypted Encrypted data
|
|
|
|
*/
|
2024-02-14 23:01:34 +01:00
|
|
|
private static function decrypt($encrypted) {
|
|
|
|
// Get the public key.
|
|
|
|
$key = \openssl_get_publickey(self::$premiumcrt);
|
2024-06-02 23:23:32 +02:00
|
|
|
if ($key === false) {
|
|
|
|
throw new \ParseError("Error parsing public key data)");
|
2024-02-14 23:01:34 +01:00
|
|
|
}
|
|
|
|
// Determine the key size.
|
|
|
|
$keysize = \openssl_pkey_get_details($key)["bits"];
|
|
|
|
$blocksize = ($keysize / 8); // Bits / 8. Whether padded or not.
|
2024-06-02 19:23:40 +02:00
|
|
|
|
2024-06-02 23:23:32 +02:00
|
|
|
// Decode data in.
|
2024-02-14 23:01:34 +01:00
|
|
|
$b64 = \base64_decode($encrypted);
|
|
|
|
if ($b64 === false) {
|
2024-06-02 23:23:32 +02:00
|
|
|
throw new \ParseError("Error in base64 decoding");
|
2024-02-14 23:01:34 +01:00
|
|
|
}
|
|
|
|
|
2024-06-02 19:23:40 +02:00
|
|
|
$data = \str_split($b64, $blocksize);
|
2024-02-14 23:01:34 +01:00
|
|
|
$decrypted = "";
|
|
|
|
$i = 0;
|
2024-06-02 23:23:32 +02:00
|
|
|
foreach ($data as $chunk) {
|
2024-06-02 19:23:40 +02:00
|
|
|
if (\openssl_public_decrypt($chunk, $dchunk, $key, \OPENSSL_PKCS1_PADDING)) {
|
2024-02-14 23:01:34 +01:00
|
|
|
$decrypted .= $dchunk;
|
|
|
|
} else {
|
2024-06-02 23:23:32 +02:00
|
|
|
throw new \ParseError("Error decrypting chunk $i ({$blocksize} bytes)");
|
2024-02-14 23:01:34 +01:00
|
|
|
}
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deprecated in PHP 8.0 and up, but included to be compatible with 7.4.
|
2024-06-02 23:23:32 +02:00
|
|
|
if (\PHP_MAJOR_VERSION < 8) {
|
2024-02-14 23:01:34 +01:00
|
|
|
\openssl_pkey_free($key);
|
2024-06-02 23:23:32 +02:00
|
|
|
}
|
2024-02-14 23:01:34 +01:00
|
|
|
|
|
|
|
return $decrypted;
|
|
|
|
}
|
2024-06-02 19:23:40 +02:00
|
|
|
|
2024-06-02 23:23:32 +02:00
|
|
|
/**
|
|
|
|
* Trim headers from key data
|
|
|
|
* @param string $data the key dat with headers
|
|
|
|
* @return string Key without headers
|
|
|
|
*/
|
2024-02-14 23:01:34 +01:00
|
|
|
private static function trim_headers($data) {
|
|
|
|
// Headers are repeated in this function for easier testing and copy-pasting into other projects.
|
2024-06-02 23:23:32 +02:00
|
|
|
$startheader = "----- BEGIN ACTIVATION KEY -----";
|
|
|
|
$endheader = "----- END ACTIVATION KEY -----";
|
2024-06-02 19:23:40 +02:00
|
|
|
|
|
|
|
$parts = preg_split("/\r?\n/", \trim($data));
|
2024-02-14 23:01:34 +01:00
|
|
|
if (count($parts) > 2) {
|
|
|
|
$start = -1;
|
|
|
|
$end = -1;
|
2024-06-02 23:23:32 +02:00
|
|
|
for ($i = 0; $i < count($parts); $i++) {
|
|
|
|
// Make sure all unicode spaces are converted to normal spaces before comparing.
|
|
|
|
$p = trim(preg_replace('/\s+/u', ' ', $parts[$i]));
|
2024-05-22 21:46:56 +02:00
|
|
|
|
2024-06-02 23:23:32 +02:00
|
|
|
if ($p == $startheader) {
|
|
|
|
$start = $i + 1;
|
2024-02-14 23:01:34 +01:00
|
|
|
}
|
2024-06-02 23:23:32 +02:00
|
|
|
if ($start > 0 && $p == $endheader) {
|
2024-02-14 23:01:34 +01:00
|
|
|
$end = $i;
|
|
|
|
}
|
|
|
|
}
|
2024-05-22 21:46:56 +02:00
|
|
|
|
2024-02-14 23:01:34 +01:00
|
|
|
if ($start < 0 || $end < 0 || $end - $start <= 0) {
|
2024-06-02 23:23:32 +02:00
|
|
|
throw new \ParseError("Invalid activation key wrappers");
|
2024-02-14 23:01:34 +01:00
|
|
|
} else {
|
|
|
|
$keyslice = array_slice($parts, $start, $end - $start);
|
2024-06-02 19:23:40 +02:00
|
|
|
return implode("\n", $keyslice);
|
2024-02-14 23:01:34 +01:00
|
|
|
}
|
|
|
|
} else {
|
2024-06-02 23:23:32 +02:00
|
|
|
throw new \ParseError("Invalid activation key");
|
2024-02-14 23:01:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-14 23:34:32 +01:00
|
|
|
/**
|
|
|
|
* Check if premium status is enabled
|
|
|
|
* @return bool
|
|
|
|
*/
|
2024-02-14 23:01:34 +01:00
|
|
|
public static function enabled() {
|
2024-06-02 19:23:40 +02:00
|
|
|
if (self::$premiumsupported) {
|
2024-06-02 23:23:32 +02:00
|
|
|
$status = self::premiumstatus();
|
2024-02-18 13:47:08 +01:00
|
|
|
return $status->enabled;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
2024-02-14 23:01:34 +01:00
|
|
|
}
|
|
|
|
|
2024-02-14 23:34:32 +01:00
|
|
|
/**
|
|
|
|
* Check if the premium key includes a specific intent in addition to the treestudyplan.
|
|
|
|
* @param string $intent The intent to search for
|
|
|
|
* @return bool
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public static function has_intent($intent) {
|
2024-06-02 23:23:32 +02:00
|
|
|
$status = self::premiumstatus();
|
2024-02-14 23:34:32 +01:00
|
|
|
if ($status->enabled) {
|
2024-06-02 19:23:40 +02:00
|
|
|
return \in_array(\strtolower($intent), $status->intents);
|
2024-02-14 23:34:32 +01:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-02 23:23:32 +02:00
|
|
|
/**
|
|
|
|
* Generate debug info.
|
|
|
|
*/
|
2024-05-10 15:22:52 +02:00
|
|
|
public static function debuginfo() {
|
|
|
|
$s = "<pre>";
|
|
|
|
try {
|
2024-06-02 19:23:40 +02:00
|
|
|
$activationkey = \get_config("local_treestudyplan", "premium_key");
|
2024-05-10 15:22:52 +02:00
|
|
|
if (strlen($activationkey) > 0) {
|
|
|
|
$keydata = self::trim_headers($activationkey);
|
|
|
|
$json = self::decrypt($keydata);
|
|
|
|
$s .= "Decoded key data:\n----\n";
|
|
|
|
$s .= $json . "\n----\n";
|
2024-06-02 19:23:40 +02:00
|
|
|
$decoded = \json_decode($json, false);
|
2024-05-10 15:22:52 +02:00
|
|
|
$s .= "Read as object:\n----\n";
|
2024-06-02 23:23:32 +02:00
|
|
|
$s .= \json_encode($decoded, \JSON_PRETTY_PRINT) ."\n----\n";
|
|
|
|
$status = self::premiumstatus();
|
2024-05-10 15:22:52 +02:00
|
|
|
$s .= "Parsed premium status block:\n----\n";
|
2024-06-02 23:23:32 +02:00
|
|
|
$s .= \json_encode($status, \JSON_PRETTY_PRINT) ."\n----\n";
|
2024-05-10 15:22:52 +02:00
|
|
|
$s .= "Message: " . self::statusdescription() . "\n";
|
|
|
|
} else {
|
|
|
|
$s .= "Premium key empty";
|
|
|
|
}
|
|
|
|
} catch (\Throwable $x) {
|
|
|
|
$s .= "!!! " . get_class($x) . ": " . $x->getCode() . " | " . $x->getMessage(). "\n";
|
2024-06-02 19:23:40 +02:00
|
|
|
$stack = explode("\n", $x->getTraceAsString());
|
2024-05-10 15:22:52 +02:00
|
|
|
foreach ($stack as $l) {
|
|
|
|
$s .= " " . trim($l) ."\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-02 23:23:32 +02:00
|
|
|
$s .= "</pre>";
|
2024-05-10 15:22:52 +02:00
|
|
|
return $s;
|
|
|
|
}
|
2024-06-02 23:23:32 +02:00
|
|
|
|
2024-06-02 19:23:40 +02:00
|
|
|
/**
|
2024-02-14 23:34:32 +01:00
|
|
|
* Determine, cache and retrieve premium status
|
|
|
|
* @return object
|
|
|
|
*/
|
2024-06-02 23:23:32 +02:00
|
|
|
protected static function premiumstatus() {
|
2024-02-14 23:01:34 +01:00
|
|
|
if (!isset(self::$cachedpremiumstatus)) {
|
2024-06-02 19:23:40 +02:00
|
|
|
|
2024-02-14 23:01:34 +01:00
|
|
|
// Initialize default object.
|
|
|
|
$o = new \stdClass;
|
|
|
|
$o->enabled = false;
|
2024-02-14 23:34:32 +01:00
|
|
|
$o->intents = [];
|
2024-02-14 23:01:34 +01:00
|
|
|
$o->name = "";
|
|
|
|
$o->website = "";
|
|
|
|
$o->expires = "";
|
|
|
|
$o->expired = false;
|
|
|
|
$o->issued = "";
|
2024-05-10 15:22:52 +02:00
|
|
|
try {
|
2024-06-02 19:23:40 +02:00
|
|
|
$activationkey = \get_config("local_treestudyplan", "premium_key");
|
2024-05-10 15:22:52 +02:00
|
|
|
if (strlen($activationkey) > 0) {
|
2024-06-02 19:23:40 +02:00
|
|
|
|
2024-02-14 23:01:34 +01:00
|
|
|
$keydata = self::trim_headers($activationkey);
|
|
|
|
$json = self::decrypt($keydata);
|
2024-06-02 19:23:40 +02:00
|
|
|
$decoded = \json_decode($json, false);
|
2024-02-14 23:01:34 +01:00
|
|
|
|
|
|
|
if (is_object($decoded)) {
|
|
|
|
|
2024-06-02 23:23:32 +02:00
|
|
|
// Copy basic data/.
|
2024-06-02 19:23:40 +02:00
|
|
|
$keys = ["name", "website", "expires", "issued"];
|
2024-06-02 23:23:32 +02:00
|
|
|
foreach ($keys as $k) {
|
2024-02-14 23:01:34 +01:00
|
|
|
if (isset($decoded->$k)) {
|
|
|
|
$o->$k = $decoded->$k;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-02 19:23:40 +02:00
|
|
|
if (!empty($decoded->intent)) {
|
|
|
|
$o->intents = explode(", ", $decoded->intent);
|
2024-02-14 23:34:32 +01:00
|
|
|
}
|
|
|
|
|
2024-06-02 23:23:32 +02:00
|
|
|
// Convert dates to DateTime for.
|
2024-02-14 23:01:34 +01:00
|
|
|
$now = new \DateTime();
|
|
|
|
$issuedate = new \DateTime($o->issued);
|
|
|
|
if ($o->expires == 'never') {
|
2024-06-02 23:23:32 +02:00
|
|
|
// If expiry date == never.
|
|
|
|
$expirydate = new \DateTime(); // Default to now and add a year.
|
2024-02-14 23:01:34 +01:00
|
|
|
$expirydate->add(new \DateInterval("P1Y"));
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
$expirydate = new \DateTime($o->expires);
|
2024-06-02 23:23:32 +02:00
|
|
|
} catch (\Exception $x) {
|
|
|
|
$expirydate = new \DateTime(); // Default to now.
|
|
|
|
}
|
2024-02-14 23:01:34 +01:00
|
|
|
}
|
2024-02-14 23:34:32 +01:00
|
|
|
|
2024-06-02 23:23:32 +02:00
|
|
|
if (\in_array('treestudyplan', $o->intents)
|
2024-02-14 23:01:34 +01:00
|
|
|
&& !empty($o->issued)
|
|
|
|
&& self::website_match($o->website)
|
|
|
|
) {
|
2024-06-02 23:23:32 +02:00
|
|
|
if ($expirydate > $now) {
|
2024-02-14 23:01:34 +01:00
|
|
|
$o->enabled = true;
|
|
|
|
$o->expired = false;
|
|
|
|
} else {
|
|
|
|
$o->expired = true;
|
|
|
|
$o->enabled = false;
|
|
|
|
}
|
|
|
|
// Format dates localized.
|
2024-06-02 19:23:40 +02:00
|
|
|
$o->issued = \userdate($issuedate->getTimestamp(), \get_string('strftimedate', 'langconfig'));
|
2024-02-14 23:01:34 +01:00
|
|
|
if ($o->expires == "never") {
|
2024-06-02 19:23:40 +02:00
|
|
|
$o->expires = \get_string("premium:never", 'local_treestudyplan');
|
2024-02-14 23:01:34 +01:00
|
|
|
} else {
|
2024-06-02 19:23:40 +02:00
|
|
|
$o->expires = \userdate($expirydate->getTimestamp(), \get_string('strftimedate', 'langconfig'));
|
2024-02-14 23:01:34 +01:00
|
|
|
}
|
2024-06-02 19:23:40 +02:00
|
|
|
}
|
2024-02-14 23:01:34 +01:00
|
|
|
}
|
|
|
|
}
|
2024-06-02 23:23:32 +02:00
|
|
|
} catch (\ParseError $x) {
|
2024-06-02 19:23:40 +02:00
|
|
|
$o->status = \get_string("premium:invalidactivationcontent", "local_treestudyplan");
|
2024-02-14 23:01:34 +01:00
|
|
|
}
|
|
|
|
self::$cachedpremiumstatus = $o;
|
|
|
|
|
|
|
|
}
|
|
|
|
return self::$cachedpremiumstatus;
|
|
|
|
}
|
|
|
|
|
2024-02-14 23:34:32 +01:00
|
|
|
/**
|
|
|
|
* Check if the current site matches the provided key
|
2024-06-03 04:00:46 +02:00
|
|
|
* @param string $key Website pattern to match against
|
2024-02-14 23:34:32 +01:00
|
|
|
*/
|
2024-02-14 23:01:34 +01:00
|
|
|
private static function website_match($key) {
|
|
|
|
global $CFG;
|
|
|
|
$site = $CFG->wwwroot;
|
2024-06-02 19:23:40 +02:00
|
|
|
|
2024-05-10 15:22:52 +02:00
|
|
|
// Add double slashes to key and site if no scheme is set.
|
2024-06-02 19:23:40 +02:00
|
|
|
// Basically: if no double slashes present before any dots, slashes or @s.
|
2024-06-02 23:23:32 +02:00
|
|
|
if (!\preg_match_all('#^[^./@]*?//#', $key)) {
|
2024-02-14 23:01:34 +01:00
|
|
|
$key = "//".$key;
|
|
|
|
}
|
2024-06-02 19:23:40 +02:00
|
|
|
if (!\preg_match_all('#^[^./@]*?//#', $site)) {
|
2024-02-14 23:01:34 +01:00
|
|
|
$site = "//".$site;
|
|
|
|
}
|
|
|
|
// Use parse_url() to split path and host.
|
|
|
|
$keyurl = (object)\parse_url($key);
|
|
|
|
$siteurl = (object)\parse_url($site);
|
|
|
|
|
2024-06-02 23:23:32 +02:00
|
|
|
// No match if host is empty on key or site.
|
2024-02-14 23:01:34 +01:00
|
|
|
if (empty($keyurl->host) || empty($siteurl->host)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-05-10 15:22:52 +02:00
|
|
|
if ($keyurl->host == "*") {
|
2024-06-02 23:23:32 +02:00
|
|
|
// Value * matches all.
|
2024-05-10 15:22:52 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-02-14 23:01:34 +01:00
|
|
|
// First match the host part.
|
2024-06-02 19:23:40 +02:00
|
|
|
$keyparts = \array_reverse(\explode(".", $keyurl->host));
|
|
|
|
$siteparts = \array_reverse(\explode(".", $siteurl->host));
|
2024-02-14 23:01:34 +01:00
|
|
|
|
|
|
|
// Trim starting www from both parts, since site.domain and www.site.domain should be treated as the same.
|
2024-06-02 23:23:32 +02:00
|
|
|
if (($x = \array_pop($keyparts)) != "www") {
|
|
|
|
\array_push($keyparts, $x);
|
|
|
|
}
|
|
|
|
if (($x = \array_pop($siteparts)) != "www") {
|
|
|
|
\array_push($siteparts, $x);
|
|
|
|
}
|
2024-02-14 23:01:34 +01:00
|
|
|
|
|
|
|
for ($i = 0; $i < count($keyparts); $i++) {
|
2024-06-02 23:23:32 +02:00
|
|
|
// No match if the site does not have a part, but the key does. Unless the key part is .
|
|
|
|
if (!isset($siteparts[$i])) {
|
2024-06-02 19:23:40 +02:00
|
|
|
if ($keyparts[$i] != "*") {
|
2024-02-14 23:01:34 +01:00
|
|
|
return false;
|
|
|
|
} else {
|
2024-06-02 23:23:32 +02:00
|
|
|
$i++; // Increment $i by one before break, to make sure the comparison following this loop holds.
|
2024-02-14 23:01:34 +01:00
|
|
|
break; // Stop comparison. Host part matches.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now do a proper case insensitive check for matching.
|
|
|
|
// Uses fnmatch to easily handle shell type wildcards.
|
2024-06-02 23:23:32 +02:00
|
|
|
if (! \fnmatch($keyparts[$i], $siteparts[$i], \FNM_CASEFOLD)) {
|
2024-02-14 23:01:34 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2024-06-02 23:23:32 +02:00
|
|
|
// Fail if the site has a deeper subdomain than the key, unless the deepest key subdomain is .
|
|
|
|
if ($keyparts[$i - 1] != '*' && count($siteparts) > ($i)) {
|
2024-02-14 23:01:34 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we made it here then the host part matches. Now check the path.
|
2024-06-02 23:23:32 +02:00
|
|
|
// If path is /*, matches all subpaths including /.
|
2024-06-02 19:23:40 +02:00
|
|
|
$keypath = empty($keyurl->path) ? "/" : $keyurl->path;
|
|
|
|
$sitepath = empty($siteurl->path) ? "/" : $siteurl->path;
|
|
|
|
|
2024-06-02 23:23:32 +02:00
|
|
|
// Trim trailing / from both paths before comparison.
|
2024-02-14 23:34:32 +01:00
|
|
|
if (\strlen($sitepath) > 1) {
|
2024-06-02 19:23:40 +02:00
|
|
|
$sitepath = \rtrim($sitepath, "/");
|
2024-02-14 23:01:34 +01:00
|
|
|
}
|
2024-02-14 23:34:32 +01:00
|
|
|
if (\strlen($keypath) > 1) {
|
2024-06-02 19:23:40 +02:00
|
|
|
$keypath = \rtrim($keypath, "/");
|
2024-02-14 23:01:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Do a case insensitive fnmatch on the site so wildcards are matched too.
|
2024-06-02 19:23:40 +02:00
|
|
|
return \fnmatch($keypath, $sitepath, \FNM_CASEFOLD);
|
2024-02-14 23:01:34 +01:00
|
|
|
}
|
|
|
|
|
2024-02-14 23:34:32 +01:00
|
|
|
/**
|
|
|
|
* Parameter description for webservice function get_premiumstatus
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public static function get_premiumstatus_parameters(): \external_function_parameters {
|
2024-02-14 23:01:34 +01:00
|
|
|
return new \external_function_parameters([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return value description for webservice function get_premiumstatus
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public static function get_premiumstatus_returns(): \external_description {
|
2024-02-14 23:01:34 +01:00
|
|
|
return new \external_single_structure([
|
|
|
|
"enabled" => new \external_value(PARAM_BOOL, 'premium status enabled'),
|
|
|
|
"website" => new \external_value(PARAM_TEXT, 'premium registration website'),
|
|
|
|
"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'),
|
2024-06-02 23:23:32 +02:00
|
|
|
"intents" => new \external_multiple_structure(
|
|
|
|
new \external_value(PARAM_TEXT), 'additional intents included in the license'
|
|
|
|
),
|
2024-02-14 23:01:34 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get premium status information for webservice
|
|
|
|
* @return object
|
|
|
|
*/
|
|
|
|
public static function get_premiumstatus() {
|
2024-06-02 19:23:40 +02:00
|
|
|
if (self::$premiumsupported) {
|
2024-06-02 23:23:32 +02:00
|
|
|
$status = self::premiumstatus();
|
2024-02-25 23:45:39 +01:00
|
|
|
$keys = [
|
|
|
|
"enabled",
|
|
|
|
"website",
|
|
|
|
"name",
|
|
|
|
"expires",
|
|
|
|
"expired",
|
|
|
|
"intents",
|
|
|
|
];
|
2024-06-02 19:23:40 +02:00
|
|
|
|
2024-02-25 23:45:39 +01:00
|
|
|
$result = [];
|
2024-06-02 23:23:32 +02:00
|
|
|
foreach ($keys as $param) {
|
2024-02-25 23:45:39 +01:00
|
|
|
$result[$param] = $status->$param;
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
} else {
|
|
|
|
return [
|
|
|
|
"enabled" => true,
|
|
|
|
"website" => '*',
|
|
|
|
"name" => '*',
|
|
|
|
"expires" => '',
|
|
|
|
"expired" => false,
|
|
|
|
"intents" => [],
|
|
|
|
];
|
2024-02-14 23:01:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-14 23:34:32 +01:00
|
|
|
/**
|
|
|
|
* Get a descriptive text of the current premium status
|
|
|
|
* @return string HTML description of premium status.
|
|
|
|
*/
|
2024-02-14 23:01:34 +01:00
|
|
|
public static function statusdescription() {
|
2024-06-02 23:23:32 +02:00
|
|
|
$status = self::premiumstatus();
|
2024-02-14 23:01:34 +01:00
|
|
|
|
|
|
|
$msg = new \stdClass;
|
|
|
|
$msg->name = $status->name;
|
|
|
|
$msg->issued = $status->issued;
|
|
|
|
$msg->expires = $status->expires;
|
|
|
|
if ($status->website != "*") {
|
2024-06-02 19:23:40 +02:00
|
|
|
$msg->sitestatus = \get_string("premium:onsite", 'local_treestudyplan', $status);
|
2024-02-14 23:01:34 +01:00
|
|
|
} else {
|
2024-06-02 19:23:40 +02:00
|
|
|
$msg->sitestatus = \get_string("premium:anywhere", 'local_treestudyplan');
|
2024-02-14 23:01:34 +01:00
|
|
|
}
|
|
|
|
|
2024-06-02 19:23:40 +02:00
|
|
|
if ($status->enabled) {
|
|
|
|
return \get_string("premium:active", 'local_treestudyplan', $msg);
|
2024-02-14 23:01:34 +01:00
|
|
|
} else if ($status->expired) {
|
2024-06-02 19:23:40 +02:00
|
|
|
return \get_string("premium:expired", 'local_treestudyplan', $msg);
|
2024-02-14 23:34:32 +01:00
|
|
|
} else if (!self::website_match($status->website)) {
|
2024-06-02 19:23:40 +02:00
|
|
|
return \get_string("premium:siteinvalid", 'local_treestudyplan', $status->website);
|
2024-02-14 23:01:34 +01:00
|
|
|
} else {
|
2024-06-02 19:23:40 +02:00
|
|
|
return \get_string("premium:notregistered", 'local_treestudyplan', $msg);
|
2024-02-14 23:01:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-09 22:51:34 +01:00
|
|
|
/**
|
|
|
|
* Throw an error if premium status is not enabled
|
2024-06-03 04:00:46 +02:00
|
|
|
* @param string $message Message translation key to use in exception
|
2024-03-09 22:51:34 +01:00
|
|
|
*/
|
|
|
|
public static function require_premium($message="premiumfeature:warning") {
|
|
|
|
if (! self::enabled()) {
|
2024-06-02 19:23:40 +02:00
|
|
|
throw new \moodle_exception($message, "local_treestudyplan");
|
2024-03-09 22:51:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-02 23:23:32 +02:00
|
|
|
}
|