88 lines
		
	
	
	
		
			3.5 KiB
		
	
	
	
		
			PHP
		
	
	
		
			Executable file
		
	
	
	
	
			
		
		
	
	
			88 lines
		
	
	
	
		
			3.5 KiB
		
	
	
	
		
			PHP
		
	
	
		
			Executable file
		
	
	
	
	
<?php
 | 
						|
require_once __DIR__ . "/session_bootstrap.php";
 | 
						|
require_once "functions.php"; // Include your existing functions
 | 
						|
 | 
						|
// Handle POST with file upload
 | 
						|
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_FILES["session_file"])) {
 | 
						|
    $raw = file_get_contents($_FILES["session_file"]["tmp_name"]);
 | 
						|
    $imported = json_decode($raw, true);
 | 
						|
 | 
						|
    if (!is_array($imported)) {
 | 
						|
        http_response_code(400);
 | 
						|
        echo json_encode(["error" => "Invalid JSON session file"]);
 | 
						|
        exit();
 | 
						|
    }
 | 
						|
    
 | 
						|
    // Debug: Log the original import data structure
 | 
						|
    error_log("Import data structure: " . print_r(array_keys($imported), true));
 | 
						|
    if (isset($imported["characters"])) {
 | 
						|
        error_log("Characters in import: " . print_r(array_keys($imported["characters"]), true));
 | 
						|
    }
 | 
						|
    
 | 
						|
    // Thorough cleaning of the imported data
 | 
						|
    if (isset($imported["characters"]) && is_array($imported["characters"])) {
 | 
						|
        foreach (array_keys($imported["characters"]) as $charID) {
 | 
						|
            // Remove any entries with empty or non-numeric character IDs
 | 
						|
            if (empty($charID) || $charID === "" || !is_numeric($charID)) {
 | 
						|
                error_log("Removing invalid character ID: '$charID'");
 | 
						|
                unset($imported["characters"][$charID]);
 | 
						|
            }
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    // Only assign cleaned data to session
 | 
						|
    $_SESSION = $imported;
 | 
						|
 | 
						|
    // Refresh tokens for each character if needed
 | 
						|
    if (isset($_SESSION["characters"]) && is_array($_SESSION["characters"])) {
 | 
						|
        // Extra validation check
 | 
						|
        foreach (array_keys($_SESSION["characters"]) as $charID) {
 | 
						|
            if (empty($charID) || $charID === "") {
 | 
						|
                error_log("Removing empty character ID from session");
 | 
						|
                unset($_SESSION["characters"][$charID]);
 | 
						|
            }
 | 
						|
        }
 | 
						|
        
 | 
						|
        foreach ($_SESSION["characters"] as $charID => &$char) {
 | 
						|
            if (
 | 
						|
                empty($char["access_token"]) ||
 | 
						|
                !empty($char["refresh_token"]) // Always refresh on import
 | 
						|
            ) {
 | 
						|
                error_log("Refreshing token for character $charID");
 | 
						|
                $new_tokens = refresh_token($char["refresh_token"]);
 | 
						|
                if (!empty($new_tokens["access_token"])) {
 | 
						|
                    $char["access_token"] = $new_tokens["access_token"];
 | 
						|
                    if (!empty($new_tokens["refresh_token"])) {
 | 
						|
                        $char["refresh_token"] = $new_tokens["refresh_token"];
 | 
						|
                    }
 | 
						|
                } else {
 | 
						|
                    error_log("Failed to refresh token for character $charID");
 | 
						|
                }
 | 
						|
            }
 | 
						|
        }
 | 
						|
        unset($char); // Break the reference to avoid unexpected behavior
 | 
						|
        
 | 
						|
        // Final validation to ensure no empty entries
 | 
						|
        if (isset($_SESSION["characters"][""])) {
 | 
						|
            error_log("Final cleanup: Removing empty character ID");
 | 
						|
            unset($_SESSION["characters"][""]);
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    // Log the final session structure
 | 
						|
    error_log("Final character count: " . count($_SESSION["characters"]));
 | 
						|
    error_log("Character IDs after import: " . implode(", ", array_keys($_SESSION["characters"])));
 | 
						|
 | 
						|
    header("Content-Type: application/json");
 | 
						|
    echo json_encode([
 | 
						|
        "status" => "success",
 | 
						|
        "characters_imported" => count($_SESSION["characters"]),
 | 
						|
    ]);
 | 
						|
    // After successful import, redirect to index.php
 | 
						|
    header("Location: index.php");
 | 
						|
    exit();
 | 
						|
} else {
 | 
						|
    http_response_code(400);
 | 
						|
    echo json_encode(["error" => "POST with session_file required"]);
 | 
						|
    exit();
 | 
						|
}
 |