// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
error_reporting(E_ALL & ~E_NOTICE);
foreach (glob("functions/*.php") as $filename) {
    require $filename;
}
require 'inc/header.php';
echo "<div class='content'><section id='result'>";
if (isset($_POST['email']) && !empty($_POST['email']) && isset($_POST['domains']) && !empty($_POST['domains'])) {
    $errors = array();
    if (validate_email($_POST['email'])) {
        $email = htmlspecialchars($_POST['email']);
    } else {
        $errors[] = "Invalid email address.";
    }
    $domains = validate_domains($_POST['domains']);
    if (count($domains['errors']) >= 1) {
        foreach ($domains['errors'] as $key => $value) {
            $errors[] = $value;
        }
    }
    if (is_array($errors) && count($errors) != 0) {
        $errors = array_unique($errors);
        foreach ($errors as $key => $value) {
            echo "<div class='alert alert-danger' role='alert'>";
            echo htmlspecialchars($value);
            echo "</div>";
        }
        echo "Please return and try again.<br>";
    } elseif (is_array($errors) && count($errors) == 0 && is_array($domains['domains']) && count($domains['domains']) != 0 && count($domains['domains']) < 21) {
        echo "<div class='alert alert-info' role='alert'>";
 }
 $json_a = json_decode($file, true);
 if ($json_a === null && json_last_error() !== JSON_ERROR_NONE) {
     $result['errors'][] = "Can't read database: " . htmlspecialchars(json_last_error());
 }
 if (count($json_a) == 0) {
     echo "Empty checklist.\n";
     exit;
 }
 echo "===== start " . date('Y-m-d H:i:s') . "=====\n";
 foreach ($json_a as $key => $value) {
     $domain = $value['domain'];
     $email = $value['email'];
     echo "Domain: " . $domain . ".\n";
     echo "Email: " . $email . ".\n";
     $val_domain = validate_domains($domain);
     if (count($val_domain['errors']) >= 1) {
         $errors = $val_domain['errors'];
         $errortexts = '';
         foreach ($errors as $error_value) {
             echo "\t" . $error_value . ". \n";
             $errortexts .= $error_value . "\n";
             if (strpos($error_value, 'Domain has expired certificate in chain') === false) {
                 send_error_mail($domain, $email, $errors);
             }
         }
         $json_a[$key]['errors'] += 1;
         $check_json = json_encode($json_a);
         if (file_put_contents($check_file, $check_json, LOCK_EX)) {
             echo "\tError count updated to " . $json_a[$key]['errors'] . ".\n";
         } else {
function add_domain_check($id, $visitor_ip)
{
    global $current_domain;
    global $pre_check_file;
    global $check_file;
    $result = array();
    $pre_check_json_file = file_get_contents($pre_check_file);
    if ($file === FALSE) {
        $result['errors'][] = "Can't open database.";
        return $result;
    }
    $pre_check_json_a = json_decode($pre_check_json_file, true);
    if ($pre_check_json_a === null && json_last_error() !== JSON_ERROR_NONE) {
        $result['errors'][] = "Can't read database: " . htmlspecialchars(json_last_error());
        return $result;
    }
    if (!is_array($pre_check_json_a[$id])) {
        $result['errors'][] = "Can't find record in database for: " . htmlspecialchars($id);
        return $result;
    }
    $file = file_get_contents($check_file);
    if ($file === FALSE) {
        $result['errors'][] = "Can't open database.";
        return $result;
    }
    $json_a = json_decode($file, true);
    if ($json_a === null && json_last_error() !== JSON_ERROR_NONE) {
        $result['errors'][] = "Can't read database: " . htmlspecialchars(json_last_error());
        return $result;
    }
    foreach ($json_a as $key => $value) {
        if ($key == $id) {
            $result['errors'][] = "Domain/email combo for  " . htmlspecialchars($pre_check_json_a[$id]['domain']) . " already exists.";
            return $result;
        }
        if ($value["domain"] == $pre_check_json_a[$id]['domain'] && $value["email"] == $pre_check_json_a[$id]['email']) {
            $result['errors'][] = "Domain / email combo for  " . htmlspecialchars($pre_check_json_a[$id]['domain']) . " already exists.";
            return $result;
        }
    }
    $domains = validate_domains($pre_check_json_a[$id]['domain']);
    if (count($domains['errors']) >= 1) {
        $result['errors'][] = $domains['errors'];
        return $result;
    }
    $json_a[$id] = array("domain" => $pre_check_json_a[$id]['domain'], "email" => $pre_check_json_a[$id]['email'], "errors" => 0, "visitor_pre_register_ip" => $pre_check_json_a[$id]['visitor_pre_register_ip'], "pre_add_date" => $pre_check_json_a[$id]['pre_add_date'], "visitor_confirm_ip" => $visitor_ip, "confirm_date" => time());
    $json = json_encode($json_a);
    if (file_put_contents($check_file, $json, LOCK_EX)) {
        $result['success'][] = true;
    } else {
        $result['errors'][] = "Can't write database.";
        return $result;
    }
    unset($pre_check_json_a[$id]);
    $pre_check_json = json_encode($pre_check_json_a);
    if (file_put_contents($pre_check_file, $pre_check_json, LOCK_EX)) {
        $result['success'][] = true;
    } else {
        $result['errors'][] = "Can't write database.";
        return $result;
    }
    $unsublink = "https://" . $current_domain . "/unsubscribe.php?id=" . $id;
    $to = $json_a[$id]['email'];
    $subject = "Certificate Expiry Monitor subscription confirmed for " . htmlspecialchars($json_a[$id]['domain']) . ".";
    $message = "Hello,\n\nSomeone, hopefully you, has confirmed the subscription of their website to the Certificate Expiry Monitor. This is a service which monitors an SSL certificate on a website, and notifies you when it is about to expire. This extra notification helps you remember to renew your certificate on time.\n  \nDomain : " . trim(htmlspecialchars($json_a[$id]['domain'])) . "\nEmail  : " . trim(htmlspecialchars($json_a[$id]['email'])) . "\nIP subscription confirmed from: " . htmlspecialchars($visitor_ip) . "\nDate subscribed confirmed: " . date("Y-m-d H:i:s T") . "\n\nWe will monitor the certificates for this website. You will receive emails when it is about to expire as described in the FAQ on our website. You can view the FAQ here: https://" . $current_domain . ".\n\nTo unsubscribe from notifications for this domain please click or copy and paste the below link in your browser:\n\n  " . $unsublink . "\n\nHave a nice day,\nThe Certificate Expiry Monitor Service.\nhttps://" . $current_domain . "";
    $message = wordwrap($message, 70, "\r\n");
    $headers = 'From: noreply@' . $current_domain . "\r\n" . 'Reply-To: noreply@' . $current_domain . "\r\n" . 'Return-Path: noreply@' . $current_domain . "\r\n" . 'X-Visitor-IP: ' . $visitor_ip . "\r\n" . 'X-Coffee: Black' . "\r\n" . 'List-Unsubscribe: <https://' . $current_domain . "/unsubscribe.php?id=" . $id . ">" . "\r\n" . 'X-Mailer: PHP/4.1.1';
    if (mail($to, $subject, $message, $headers) === true) {
        $result['success'][] = true;
    } else {
        $result['errors'][] = "Can't send email.";
        return $result;
    }
    return $result;
}