HEX
Server: Apache/2.4.37 (CentOS Stream) OpenSSL/1.1.1k
System: Linux ysnet.com.tw 4.18.0-553.5.1.el8.x86_64 #1 SMP Tue May 21 05:46:01 UTC 2024 x86_64
User: test (521)
PHP: 7.4.33
Disabled: NONE
Upload Files
File: //var/www/net/upload_backup.php
<?php
// --- 1. 初始化與接收主機名稱 ---
$identity = $_GET['identity'] ?? 'Unknown_Router';
$to_admin = "backup@ysnet.com.tw"; // 管理員信箱

// --- 2. 處理上傳檔案 ---
if (isset($_FILES['file'])) {
    $file_name = basename($_FILES['file']['name']);
    $target_path = "/tmp/" . $file_name;
    
    if (move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
        
        // --- 3. 嘗試寄送備份信件 ---
        $mail_result = send_backup_email($identity, $file_name, $target_path, $to_admin);
        
        if ($mail_result) {
            // --- 4. 寄信成功:更新或創建資料庫紀錄 ---
            update_database($identity);
            echo "Success: Backup processed and database updated.";
        } else {
            // --- 5. 寄信失敗:發送通知信給管理員 ---
            send_failure_notification($identity, $to_admin);
            echo "Error: Mail delivery failed. Notification sent.";
        }
        
        // 清理暫存檔
        if (file_exists($target_path)) {
            unlink($target_path);
        }
        
    } else {
        echo "Error: File upload move failed.";
    }
} else {
    echo "Error: No file received.";
}

// --- 函數區:資料庫更新 ---
function update_database($identity) {
    $db = new mysqli("localhost", "db_user", "db_pass", "db_name");
    
    // 使用 ON DUPLICATE KEY UPDATE:
    // 如果 identity 不存在則 INSERT,如果存在則 UPDATE last_seen
    $stmt = $db->prepare("INSERT INTO router_backups (identity, last_seen) 
                          VALUES (?, NOW()) 
                          ON DUPLICATE KEY UPDATE last_seen = NOW()");
    $stmt->bind_param("s", $identity);
    $stmt->execute();
    $db->close();
}

// --- 函數區:寄送備份檔 ---
function send_backup_email($identity, $file_name, $path, $to) {
    $subject = "Router Backup OK: [$identity] $file_name";
    $boundary = md5(time());
    $content = chunk_split(base64_encode(file_get_contents($path)));

    $headers = "From: backup-system@ysnet.com.tw\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n";

    $message = "--$boundary\r\n";
    $message .= "Content-Type: text/plain; charset=UTF-8\r\n\r\n";
    $message .= "主機名稱: $identity\n檔案名稱: $file_name\n狀態: 備份檔案已成功接收並轉發。\r\n";
    $message .= "--$boundary\r\n";
    $message .= "Content-Type: application/octet-stream; name=\"$file_name\"\r\n";
    $message .= "Content-Transfer-Encoding: base64\r\n";
    $message .= "Content-Disposition: attachment\r\n\r\n";
    $message .= $content . "\r\n";
    $message .= "--$boundary--";

    return mail($to, $subject, $message, $headers);
}

// --- 函數區:寄送失敗通知 ---
function send_failure_notification($identity, $to) {
    $subject = "🚨 ALERT: Backup Delivery Failed - $identity";
    $headers = "From: backup-system@ysnet.com.tw\r\nContent-Type: text/plain; charset=UTF-8";
    $body = "警告:伺服器已接收到來自主機 [$identity] 的備份檔案,但在嘗試將其轉發至電子信箱時失敗。\n\n請檢查伺服器的 Postfix 服務狀態或 mail 日誌。\n主機 Identity: $identity";
    
    mail($to, $subject, $body, $headers);
}
?>