function validate_domains($domains)
{
    $errors = array();
    $domains = explode("\n", $domains);
    $domains = array_map('strtolower', $domains);
    $domains = array_filter($domains);
    $domains = array_unique($domains);
    foreach ($domains as $key => $value) {
        $value = trim(mb_strtolower($value));
        // check if reasonably valid domain
        if (!preg_match("/^([a-z\\d](-*[a-z\\d])*)(\\.([a-z\\d](-*[a-z\\d])*))*\$/i", $value) && !preg_match("/^.{1,253}\$/", $value) && !preg_match("/^[^\\.]{1,63}(\\.[^\\.]{1,63})*\$/", $value)) {
            $errors[] = "Invalid domain name: " . htmlspecialchars($value) . ".";
        }
        // check valid dns record
        $ips = dns_get_record($value, DNS_A + DNS_AAAA);
        sort($ips);
        if (count($ips) >= 1) {
            if (!empty($ips[0]['type'])) {
                if ($ips[0]['type'] === "AAAA") {
                    $ip = $ips[0]['ipv6'];
                    if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
                        $errors[] = "Invalid domain AAAA record for: " . htmlspecialchars($value) . ".";
                    }
                } elseif ($ips[0]['type'] === "A") {
                    $ip = $ips[0]['ip'];
                    if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
                        $errors[] = "Invalid domain A record for: " . htmlspecialchars($value) . ".";
                    }
                }
            } else {
                $errors[] = "No DNS A/AAAA records for: " . htmlspecialchars($value) . ".";
            }
        } else {
            $errors[] = "Error resolving domain: " . htmlspecialchars($value) . ".";
        }
    }
    if (is_array($errors) && count($errors) == 0) {
        foreach ($domains as $key => $value) {
            $raw_chain = get_raw_chain(trim($value));
            if (!$raw_chain) {
                $errors[] = "Domain has invalid or no certificate: " . htmlspecialchars($value) . ".";
            } else {
                foreach ($raw_chain['chain'] as $raw_key => $raw_value) {
                    $cert_expiry = cert_expiry($raw_value);
                    $cert_subject = cert_subject($raw_value);
                    if ($cert_expiry['cert_expired']) {
                        $errors[] = "Domain has expired certificate in chain: " . htmlspecialchars($value) . ". Cert Subject: " . htmlspecialchars($cert_subject) . ".";
                    }
                }
            }
        }
    }
    if (is_array($errors) && count($errors) >= 1) {
        $result = array();
        foreach ($errors as $key => $value) {
            $result['errors'][] = $value;
        }
        return $result;
    } else {
        $result = array();
        foreach ($domains as $key => $value) {
            $result['domains'][] = $value;
        }
        return $result;
    }
}
     }
     if ($json_a[$key]['errors'] >= 7) {
         echo "\tToo many errors. Adding domain to removal queue.\n";
         $removal_queue[] = $key;
     }
     if (strpos($errortexts, 'Domain has expired certificate in chain') === false) {
         continue;
     }
 }
 $raw_chain = get_raw_chain($domain);
 $counter = 0;
 foreach ($raw_chain['chain'] as $chain_key => $chain_value) {
     $counter += 1;
     $cert_exp_date = cert_expiry_date($chain_value);
     $cert_cn = cert_cn($chain_value);
     $cert_expiry = cert_expiry($chain_value);
     echo "\tCert Chain #" . $counter . ". Expiry Date: " . date("Y-m-d H:i:s T", $cert_exp_date) . ". Common Name: " . $cert_cn . "\n";
     cert_expiry_emails($domain, $email, $cert_expiry, $chain_value);
 }
 $file = file_get_contents($check_file);
 if ($file === FALSE) {
     echo "\tCan't open database.\n";
     continue;
 }
 $json_a = json_decode($file, true);
 if ($json_a === null && json_last_error() !== JSON_ERROR_NONE) {
     echo "\tCan't read database\n";
     continue;
 }
 if ($json_a[$key]['errors'] != 0) {
     if (strpos($errortexts, 'Domain has expired certificate in chain') === false) {