Podcast-server/setup.php

227 lines
10 KiB
PHP

<?php
session_start();
$error = '';
$success = false;
$systemChecks = [];
// 1. Check folder permissions
$uploadDirs = [
'assets/uploads/audio',
'assets/uploads/images',
'includes'
];
foreach ($uploadDirs as $dir) {
$fullPath = __DIR__ . '/' . $dir;
if (!is_dir($fullPath)) {
@mkdir($fullPath, 0755, true);
}
$isWritable = is_writable($fullPath);
$systemChecks['dirs'][$dir] = [
'status' => $isWritable,
'message' => $isWritable ? 'Writable' : 'Not Writable'
];
}
// 2. Check PHP limits
$uploadMax = ini_get('upload_max_filesize');
$postMax = ini_get('post_max_size');
$systemChecks['php'] = [
'upload_max' => $uploadMax,
'post_max' => $postMax
];
if (file_exists('includes/config.php')) {
header("Location: index.php");
exit;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$host = $_POST['db_host'] ?? 'localhost';
$db_name = $_POST['db_name'] ?? 'church_podcast';
$db_user = $_POST['db_user'] ?? 'root';
$db_pass = $_POST['db_pass'] ?? '';
$admin_user = $_POST['admin_user'] ?? 'admin';
$admin_pass = $_POST['admin_pass'] ?? '';
try {
// 1. Try to connect to MySQL (without selecting DB first)
$dsn = "mysql:host=$host;charset=utf8mb4";
$pdo = new PDO($dsn, $db_user, $db_pass, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
// 2. Create Database
$pdo->exec("CREATE DATABASE IF NOT EXISTS `$db_name` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
$pdo->exec("USE `$db_name` ");
// 3. Run Schema
$schema = file_get_contents('sql/schema.sql');
$schema = preg_replace('/CREATE DATABASE IF NOT EXISTS .*?;/i', '', $schema);
$schema = preg_replace('/USE .*?;/i', '', $schema);
$pdo->exec($schema);
// 4. Create first Admin
$hashed_pass = password_hash($admin_pass, PASSWORD_DEFAULT);
$stmt = $pdo->prepare("INSERT INTO admins (username, password, role) VALUES (?, ?, 'admin')");
$stmt->execute([$admin_user, $hashed_pass]);
// 5. Save Config
$configContent = "<?php
\$host = '$host';
\$db = '$db_name';
\$user = '$db_user';
\$pass = '$db_pass';
\$charset = 'utf8mb4';
?>";
if (file_put_contents('includes/config.php', $configContent) === false) {
throw new Exception("Could not write config.php. Please check folder permissions for 'includes/' directory.");
}
$success = true;
} catch (Exception $e) {
$msg = $e->getMessage();
if (strpos($msg, 'Access denied') !== false) {
$error = "<strong>Access Denied:</strong> Your database user doesn't have permission. <br><br>
If you are using 'root' on MySQL 8.0, this is normal. Please run these commands in your terminal:<br>
<code style='display:block; background:#1e293b; padding:10px; margin-top:10px; font-size:0.8rem; text-align:left;'>
sudo mysql<br>
CREATE DATABASE IF NOT EXISTS $db_name;<br>
CREATE USER 'podcast_user'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password';<br>
GRANT ALL PRIVILEGES ON $db_name.* TO 'podcast_user'@'localhost';<br>
FLUSH PRIVILEGES;
</code><br>
Then use 'podcast_user' and your password in the form below.";
} else {
$error = "Setup failed: " . $msg;
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Setup - Podcast Server</title>
<link rel="stylesheet" href="assets/css/style.css">
<style>
.check-item { display: flex; justify-content: space-between; padding: 10px; border-bottom: 1px solid var(--glass-border); }
.status-ok { color: #10b981; }
.status-fail { color: #ef4444; }
.help-box { background: rgba(239, 68, 68, 0.1); border: 1px solid #ef4444; padding: 15px; border-radius: 10px; margin-top: 10px; font-size: 0.9rem; }
</style>
</head>
<body class="admin-login">
<div class="form-container" style="max-width: 900px;">
<h1 style="text-align: center; margin-bottom: 2rem;">🚀 Welcome to Podcast Server</h1>
<?php if ($success): ?>
<div style="background: rgba(16, 185, 129, 0.1); color: #10b981; padding: 2rem; border-radius: 12px; text-align: center;">
<h2>Setup Complete!</h2>
<p>The configuration file has been created and the database initialized.</p>
<a href="admin/login.php" class="btn btn-primary" style="margin-top: 1.5rem;">Go to Login</a>
</div>
<?php else: ?>
<div style="display: grid; grid-template-columns: 350px 1fr; gap: 3rem;">
<!-- System Checks -->
<div style="background: rgba(15, 23, 42, 0.5); padding: 1.5rem; border-radius: 15px; border: 1px solid var(--glass-border);">
<h3 style="margin-bottom: 1.5rem;">Environment Check</h3>
<div class="check-group">
<p style="font-weight: 600; font-size: 0.8rem; color: var(--text-muted); text-transform: uppercase;">Permissions</p>
<?php
$hasPermIssue = false;
foreach ($systemChecks['dirs'] as $dir => $data):
if (!$data['status']) $hasPermIssue = true;
?>
<div class="check-item">
<span style="font-size: 0.9rem;"><?php echo $dir; ?></span>
<span class="<?php echo $data['status'] ? 'status-ok' : 'status-fail'; ?>">
<?php echo $data['message']; ?>
</span>
</div>
<?php endforeach; ?>
<?php if ($hasPermIssue): ?>
<div class="help-box">
<strong>Fix Permissions:</strong><br>
Run this command in your Ubuntu terminal:<br>
<code style="display:block; margin-top:5px; font-size:0.75rem;">sudo chown -R www-data:www-data <?php echo __DIR__; ?></code>
</div>
<?php endif; ?>
</div>
<div class="check-group" style="margin-top: 2rem;">
<p style="font-weight: 600; font-size: 0.8rem; color: var(--text-muted); text-transform: uppercase;">PHP Settings</p>
<div class="check-item">
<span style="font-size: 0.9rem;">Max Upload</span>
<span><?php echo $systemChecks['php']['upload_max']; ?></span>
</div>
<div class="check-item">
<span style="font-size: 0.9rem;">Post Max Data</span>
<span><?php echo $systemChecks['php']['post_max']; ?></span>
</div>
<?php if (intval($systemChecks['php']['upload_max']) < 50): ?>
<div class="help-box">
<strong>Increase Limits:</strong><br>
Edit <code>/etc/php/8.0/apache2/php.ini</code> and set:<br>
<code>upload_max_filesize = 100M</code><br>
<code>post_max_size = 110M</code><br>
Then: <code>sudo systemctl restart apache2</code>
</div>
<?php endif; ?>
</div>
</div>
<!-- Setup Form -->
<div>
<?php if ($error): ?>
<div style="background: rgba(239, 68, 68, 0.1); border: 1px solid #ef4444; color: #ef4444; padding: 1.5rem; border-radius: 12px; margin-bottom: 2rem;">
<?php echo $error; ?>
</div>
<?php endif; ?>
<form method="POST">
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 1.5rem;">
<div>
<h4 style="margin-bottom: 1rem;">Database Configuration</h4>
<div class="form-group">
<label for="db_host">MySQL Host</label>
<input type="text" id="db_host" name="db_host" value="localhost" required>
</div>
<div class="form-group">
<label for="db_name">Database Name</label>
<input type="text" id="db_name" name="db_name" value="church_podcast" required>
</div>
<div class="form-group">
<label for="db_user">MySQL Username</label>
<input type="text" id="db_user" name="db_user" value="root" required>
</div>
<div class="form-group">
<label for="db_pass">MySQL Password</label>
<input type="password" id="db_pass" name="db_pass">
</div>
</div>
<div>
<h4 style="margin-bottom: 1rem;">Admin User</h4>
<div class="form-group">
<label for="admin_user">Username</label>
<input type="text" id="admin_user" name="admin_user" value="admin" required>
</div>
<div class="form-group">
<label for="admin_pass">Password</label>
<input type="password" id="admin_pass" name="admin_pass" required>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary" style="width: 100%; margin-top: 1rem;">Finish Setup</button>
</form>
</div>
</div>
<?php endif; ?>
</div>
</body>
</html>