moodle_local_treestudyplan/test.php

83 lines
3.0 KiB
PHP
Raw Normal View History

2024-05-10 15:22:52 +02:00
<?php
2024-06-02 19:23:40 +02:00
function sitematch($key, $site) {
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, shashes or @s.
if (!\preg_match_all('#^[^./@]*?//#', $key )) {
2024-05-10 15:22:52 +02:00
$key = "//".$key;
}
2024-06-02 19:23:40 +02:00
if (!\preg_match_all('#^[^./@]*?//#', $site)) {
2024-05-10 15:22:52 +02:00
$site = "//".$site;
}
// Use parse_url() to split path and host.
$keyurl = (object)\parse_url($key);
$siteurl = (object)\parse_url($site);
// No match if host is empty on key or site
if (empty($keyurl->host) || empty($siteurl->host)) {
return false;
}
if ($keyurl->host == "*") {
// * matches all
return true;
}
// 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-05-10 15:22:52 +02:00
// Trim starting www from both parts, since site.domain and www.site.domain should be treated as the same.
2024-06-02 19:23:40 +02:00
if (($x = \array_pop($keyparts)) != "www") {\array_push($keyparts, $x);}
if (($x = \array_pop($siteparts)) != "www") {\array_push($siteparts, $x);}
2024-05-10 15:22:52 +02:00
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 *
if (!isset($siteparts[$i]) ) {
2024-06-02 19:23:40 +02:00
if ($keyparts[$i] != "*") {
2024-05-10 15:22:52 +02:00
return false;
} else {
$i++; //increment $i by one before break, to make sure the comparison following this loop holds.
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 19:23:40 +02:00
if ( ! \fnmatch($keyparts[$i], $siteparts[$i], \FNM_CASEFOLD)) {
2024-05-10 15:22:52 +02:00
return false;
}
}
// Fail if the site has a deeper subdomain than the key, unless the deepest key subdomain is *
if ($keyparts[$i-1] != '*' && count($siteparts) > ($i)) {
return false;
}
// If we made it here then the host part matches. Now check the path.
// 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-05-10 15:22:52 +02:00
// Trim trailing / from both paths before comparison
if (\strlen($sitepath) > 1) {
2024-06-02 19:23:40 +02:00
$sitepath = \rtrim($sitepath, "/");
2024-05-10 15:22:52 +02:00
}
if (\strlen($keypath) > 1) {
2024-06-02 19:23:40 +02:00
$keypath = \rtrim($keypath, "/");
2024-05-10 15:22:52 +02: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-05-10 15:22:52 +02:00
}
$tests = [
["*", "https://www.miqra.nl"],
["*/*", "https://www.miqra.nl"],
["*", "https://clients.openedu.nl/fith"],
["clients.openedu.nl/fith", "https://clients.openedu.nl/fith/"],
2024-06-02 19:23:40 +02:00
["clients.openedu.nl/fith/", "https://clients.openedu.nl/fith"],
2024-05-10 15:22:52 +02:00
];
foreach($tests as $test) {
[$key, $site] = $test;
2024-06-02 19:23:40 +02:00
print("Checking key '{$key}' on site '{$site}': " . (sitematch($key, $site)?"MATCH":"FAIL") . "\n");
2024-05-10 15:22:52 +02:00
}