Commit e9a82af0 authored by Elias Häußler's avatar Elias Häußler 🐛

[TASK] Extract sub domain code into separate methods in Utility class

parent 1851cf57
......@@ -50,52 +50,12 @@ try {
// Check if request is valid
\EliasHaeussler\CpanelRequests\Cpanel::isRequestValid($result, "data", true);
// Get subdomains
$sub_domains = $result->data->sub_domains;
// Filter subdomains on basis of given domain name
// Filter sub domains by given domain name
$domain_name = getenv("DOMAIN_NAME") ?: $_SERVER["SERVER_NAME"];
$domain_parts = array_map(function ($sub_domain) {
$parts = preg_split("/([^.]+\.[^.]*)$/", $sub_domain, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
return (object) [
"sub" => $parts[0],
"domain" => $parts[1],
];
}, $sub_domains);
if ($domain_name != "*") {
$sub_domains = array_filter($domain_parts, function ($sub_domain) use ($domain_name) {
return $sub_domain->domain == $domain_name;
});
usort($sub_domains, function ($a, $b) {
return strcmp($a->sub, $b->sub);
});
} else {
$sub_domains = $domain_parts;
$list_sub = array_column($sub_domains, "sub");
$list_domain = array_column($sub_domains, "domain");
array_multisort($list_domain, SORT_ASC, $list_sub, SORT_ASC, $sub_domains);
}
$sub_domains = \EliasHaeussler\CpanelRequests\Utility::filterSubDomainsByDomainName($result->data->sub_domains, $domain_name);
// Show sub domain links
if ($sub_domains) {
$domains = [];
array_walk($sub_domains, function ($sub_domain) use (&$domains) {
$domains[$sub_domain->domain][] = $sub_domain->sub;
});
echo "<ul>";
foreach ($domains as $current_domain => $current_sub_domains) {
echo sprintf(
"<li>%s%s</li>",
$current_domain,
\EliasHaeussler\CpanelRequests\Utility::buildListOfSubDomains($current_domain, $current_sub_domains)
);
}
echo "</ul>";
} else {
echo sprintf("No sub domains found for the domain \"%s\".", $domain_name);
}
\EliasHaeussler\CpanelRequests\Utility::printSubDomains($sub_domains);
} catch (Exception $e) {
\EliasHaeussler\CpanelRequests\ErrorHandling::showError($e);
......
......@@ -15,6 +15,75 @@ namespace EliasHaeussler\CpanelRequests;
class Utility
{
/**
* Filter list of sub domains by given domain name.
*
* @param array $sub_domains List of available sub domains
* @param string $domain_name Domain name whose sub domains should be returned, can be `*` to allow all domain names
* @return array List of filtered sub domains
*/
public static function filterSubDomainsByDomainName(array $sub_domains, string $domain_name = "*"): array
{
if (!$domain_name) {
$domain_name = "*";
}
// Filter sub domains on basis of given domain name
$domain_parts = array_map(function ($sub_domain) {
$parts = preg_split("/([^.]+\.[^.]*)$/", $sub_domain, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
return (object) [
"sub" => $parts[0],
"domain" => $parts[1],
];
}, $sub_domains);
// Sort sub domains
if ($domain_name != "*") {
$sub_domains = array_filter($domain_parts, function ($sub_domain) use ($domain_name) {
return $sub_domain->domain == $domain_name;
});
usort($sub_domains, function ($a, $b) {
return strcmp($a->sub, $b->sub);
});
} else {
$sub_domains = $domain_parts;
$list_sub = array_column($sub_domains, "sub");
$list_domain = array_column($sub_domains, "domain");
array_multisort($list_domain, SORT_ASC, $list_sub, SORT_ASC, $sub_domains);
}
return $sub_domains;
}
/**
* Print sub domains as HTML list.
*
* @param array $sub_domains List of sub domains to be printed as HTML list
*/
public static function printSubDomains(array $sub_domains)
{
if ($sub_domains) {
$domains = [];
array_walk($sub_domains, function ($sub_domain) use (&$domains) {
$domains[$sub_domain->domain][] = $sub_domain->sub;
});
echo "<ul>";
foreach ($domains as $current_domain => $current_sub_domains) {
echo sprintf(
"<li>%s%s</li>",
$current_domain,
self::buildListOfSubDomains($current_domain, $current_sub_domains)
);
}
echo "</ul>";
} else {
echo "No sub domains found.";
}
}
/**
* Build HTML list of sub domains for a given domain.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment