<?php
require_once __DIR__ . '/../MPHX/common.php';

$today = date('Y-m-d');

$certificates = $DB->query("SELECT * FROM `ssl_certificates` WHERE `status`='active'");

while ($cert = $DB->fetch($certificates)) {
    $valid_to = date('Y-m-d', strtotime($cert['valid_to']));
    $days_until_expiry = (strtotime($valid_to) - strtotime($today)) / 86400;
    
    if ($days_until_expiry <= 0) {
        $DB->query("UPDATE `ssl_certificates` SET `status`='expired' WHERE `id`='{$cert['id']}'");
        
        $user = $DB->get_row("SELECT * FROM `zj` WHERE `id`='{$cert['user_id']}'");
        
        if ($user) {
            $DB->query("INSERT INTO `notifications` (`user_id`,`type`,`title`,`content`,`is_read`,`create_time`) VALUES ('{$user['id']}','other','SSL证书已过期','您的SSL证书 {$cert['domain']} 已过期，请及时续期',0,NOW())");
            
            $settings = $DB->get_row("SELECT * FROM `notification_settings` WHERE `user_id`='{$user['id']}'");
            
            if ($settings && !empty($user['email'])) {
                sendEmail($user['email'], 'SSL证书已过期通知', "您的SSL证书 {$cert['domain']} 已过期，请及时续期");
            }
        }
    } elseif ($days_until_expiry <= $cert['renew_days']) {
        if ($cert['auto_renew'] == 1) {
            $valid_from = date('Y-m-d H:i:s');
            $valid_to = date('Y-m-d H:i:s', strtotime('+90 days'));
            
            $DB->query("UPDATE `ssl_certificates` SET `valid_from`='{$valid_from}',`valid_to`='{$valid_to}',`last_renew_time`=NOW() WHERE `id`='{$cert['id']}'");
            
            $user = $DB->get_row("SELECT * FROM `zj` WHERE `id`='{$cert['user_id']}'");
            
            if ($user) {
                $DB->query("INSERT INTO `notifications` (`user_id`,`type`,`title`,`content`,`is_read`,`create_time`) VALUES ('{$user['id']}','other','SSL证书已自动续期','您的SSL证书 {$cert['domain']} 已自动续期，新到期时间：{$valid_to}',0,NOW())");
                
                $settings = $DB->get_row("SELECT * FROM `notification_settings` WHERE `user_id`='{$user['id']}'");
                
                if ($settings && !empty($user['email'])) {
                    sendEmail($user['email'], 'SSL证书自动续期通知', "您的SSL证书 {$cert['domain']} 已自动续期，新到期时间：{$valid_to}");
                }
            }
        } else {
            $user = $DB->get_row("SELECT * FROM `zj` WHERE `id`='{$cert['user_id']}'");
            
            if ($user) {
                $existing_notification = $DB->get_row("SELECT * FROM `notifications` WHERE `user_id`='{$user['id']}' AND `title` LIKE '%SSL证书即将到期%' AND `content` LIKE '%{$cert['domain']}%' AND `create_time` > DATE_SUB(NOW(), INTERVAL 7 DAY)");
                
                if (!$existing_notification) {
                    $DB->query("INSERT INTO `notifications` (`user_id`,`type`,`title`,`content`,`is_read`,`create_time`) VALUES ('{$user['id']}','other','SSL证书即将到期','您的SSL证书 {$cert['domain']} 将在 " . floor($days_until_expiry) . " 天后到期，请及时续期',0,NOW())");
                    
                    $settings = $DB->get_row("SELECT * FROM `notification_settings` WHERE `user_id`='{$user['id']}'");
                    
                    if ($settings && !empty($user['email'])) {
                        sendEmail($user['email'], 'SSL证书即将到期通知', "您的SSL证书 {$cert['domain']} 将在 " . floor($days_until_expiry) . " 天后到期，请及时续期");
                    }
                }
            }
        }
    }
}

echo "SSL证书检查完成\n";
?>
