<?php

// ===== CONFIG LOAD =====
$config = json_decode(file_get_contents("myfiles.json"), true);

$visited = [];
$results = [];
$pageCount = 0;

// ===== SIMPLE CRAWLER =====
function crawl($url, $depth, &$visited, &$results, &$pageCount, $config){
    if ($depth > $config['maxDepth']) return;
    if (in_array($url, $visited)) return;
    if ($pageCount >= $config['maxPages']) return;

    $visited[] = $url;
    $pageCount++;

    $html = @file_get_contents($url);
    if (!$html) return;

    libxml_use_internal_errors(true);
    $dom = new DOMDocument();
    $dom->loadHTML($html);
    libxml_clear_errors();

    $xpath = new DOMXPath($dom);

    // ===== IMAGES =====
    foreach ($xpath->query("//img") as $img) {
        $src = $img->getAttribute("src");
        if ($src) {
            $results[] = [
                "type" => "image",
                "url" => resolveUrl($src, $url)
            ];
        }
    }

    // ===== VIDEOS =====
    foreach ($xpath->query("//video | //source") as $vid) {
        $src = $vid->getAttribute("src");
        if ($src) {
            $results[] = [
                "type" => "video",
                "url" => resolveUrl($src, $url)
            ];
        }
    }

    // ===== LINKS (DEEPER CRAWL) =====
    foreach ($xpath->query("//a") as $a) {
        $href = $a->getAttribute("href");
        if ($href) {
            $next = resolveUrl($href, $url);
            crawl($next, $depth + 1, $visited, $results, $pageCount, $config);
        }
    }
}

// ===== URL RESOLVER =====
function resolveUrl($relative, $base){
    if (parse_url($relative, PHP_URL_SCHEME) != '') return $relative;

    if ($relative[0] == '/') {
        $parts = parse_url($base);
        return $parts['scheme'] . "://" . $parts['host'] . $relative;
    }

    return rtrim($base, '/') . '/' . $relative;
}

// ===== HANDLE SCAN REQUEST =====
if (isset($_GET['scan'])) {

    foreach ($config['seeds'] as $seed) {
        crawl($seed, 0, $visited, $results, $pageCount, $config);
    }

    header('Content-Type: application/json');
    echo json_encode(["media" => array_values(array_unique($results, SORT_REGULAR))]);
    exit;
}

?>

<!DOCTYPE html>
<html>
<head>
    <title>PHP Media Scraper</title>
    <style>
        body { font-family: Arial; background:#111; color:#eee; }
        button { padding:10px; margin:10px 0; }
        .grid { display:flex; flex-wrap:wrap; gap:10px; }
        img, video { max-width:200px; border-radius:8px; }
    </style>
</head>
<body>

<h1>Media Scraper</h1>
<button onclick="scan()">Start Scan</button>
<p id="status"></p>

<div class="grid" id="results"></div>

<script>
async function scan(){
    document.getElementById("status").innerText = "Scanning...";
    const res = await fetch("?scan=1");
    const data = await res.json();

    document.getElementById("status").innerText = "Done";

    const container = document.getElementById("results");
    container.innerHTML = "";

    data.media.forEach(item => {
        if(item.type === "image"){
            const img = document.createElement("img");
            img.src = item.url;
            container.appendChild(img);
        }
        if(item.type === "video"){
            const vid = document.createElement("video");
            vid.src = item.url;
            vid.controls = true;
            container.appendChild(vid);
        }
    });
}
</script>

</body>
</html>