- var_dump: documentation ( source)
- json_decode: documentation ( source)
- explode: documentation ( source)
- in_array: documentation ( source)
- max: documentation ( source)
<?php
$hostname = "lekkere.koekjes.men";
$certificates = json_decode('[{"commonName":"*.men"},{"commonName":"*.hans.nl"},{"commonName":"*.koekjes.men"}]');
$hostnameParts = explode(".", $hostname);
foreach ($certificates as $certificate) {
// commonName = *.lekkere.koekjes.men
// hostname = www.lekkere.koekjes.men
$commonName = $certificate->commonName;
$commonNameParts = explode(".", $commonName);
for ($i = 0; $i < max(count($commonNameParts), count($hostnameParts)); $i++) {
$commonNamePart = count($commonNameParts) > ($i + 1) ? $commonNameParts[count($commonNameParts) - $i - 1] : null;
$hostnamePart = count($hostnameParts) > ($i + 1) ? $hostnameParts[count($hostnameParts) - $i - 1] : null;
if (! (
$commonNamePart === '*'
||
($commonNamePart === null && $hostnamePart === 'www' && !in_array("*", $commonNameParts))
||
($commonNamePart === $hostnamePart)
)
) {
continue 2;
}
}
var_dump($certificate);
}