churchtube/install.php

272 lines
13 KiB
PHP

<?php
$config_file = 'includes/config.php';
if (file_exists($config_file)) {
header('Location: index.php');
exit;
}
$step = isset($_GET['step']) ? (int)$_GET['step'] : 1;
$error = '';
$success = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if ($step === 2) {
// DB Setup
$db_host = $_POST['db_host'];
$db_name = $_POST['db_name'];
$db_user = $_POST['db_user'];
$db_pass = $_POST['db_pass'];
try {
$pdo = new PDO("mysql:host=$db_host", $db_user, $db_pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec("CREATE DATABASE IF NOT EXISTS `$db_name` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
$pdo->exec("USE `$db_name`");
// Create tables
$sql = "
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
email VARCHAR(100) NOT NULL UNIQUE,
role ENUM('admin', 'moderator', 'editor', 'user') DEFAULT 'user',
avatar_url TEXT,
theme_preference ENUM('dark', 'light') DEFAULT 'dark',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS videos (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
description TEXT,
release_date DATE,
source_type ENUM('upload', 'link') NOT NULL,
video_url TEXT NOT NULL,
thumbnail_url TEXT,
uploader_id INT,
views INT DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (uploader_id) REFERENCES users(id) ON DELETE SET NULL
);
CREATE TABLE IF NOT EXISTS comments (
id INT AUTO_INCREMENT PRIMARY KEY,
video_id INT NOT NULL,
user_id INT NOT NULL,
comment_text TEXT NOT NULL,
is_reported BOOLEAN DEFAULT FALSE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (video_id) REFERENCES videos(id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS reactions (
id INT AUTO_INCREMENT PRIMARY KEY,
comment_id INT NOT NULL,
user_id INT NOT NULL,
reaction_type ENUM('thumb', 'heart', 'pray', 'insight', 'clap') NOT NULL,
UNIQUE KEY unique_reaction (comment_id, user_id, reaction_type),
FOREIGN KEY (comment_id) REFERENCES comments(id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS tags (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL UNIQUE
);
CREATE TABLE IF NOT EXISTS video_tags (
video_id INT NOT NULL,
tag_id INT NOT NULL,
PRIMARY KEY (video_id, tag_id),
FOREIGN KEY (video_id) REFERENCES videos(id) ON DELETE CASCADE,
FOREIGN KEY (tag_id) REFERENCES tags(id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS bookmarks (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
video_id INT NOT NULL,
video_timestamp FLOAT DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (video_id) REFERENCES videos(id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS logs (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NULL,
type VARCHAR(50),
message TEXT,
ip_address VARCHAR(45),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL
);
CREATE TABLE IF NOT EXISTS settings (
setting_key VARCHAR(50) PRIMARY KEY,
setting_value TEXT
);
";
$pdo->exec($sql);
// Default settings
$pdo->exec("INSERT IGNORE INTO settings (setting_key, setting_value) VALUES
('site_title', 'ChurchTube'),
('primary_color', '#7c4dff'),
('secondary_color', '#ff4081'),
('logo_url', ''),
('footer_text', '&copy; 2024 ChurchTube. All rights reserved.')");
// Save config temporarily in session
session_start();
$_SESSION['db_config'] = [
'host' => $db_host,
'name' => $db_name,
'user' => $db_user,
'pass' => $db_pass
];
header('Location: install.php?step=3');
exit;
} catch (PDOException $e) {
$msg = $e->getMessage();
$hint = "";
if (strpos($msg, 'Access denied') !== false) {
$hint = "<strong>Hint:</strong> Check your username and password. On Ubuntu, 'root' often requires a password or 'sudo' access. Try creating a dedicated database user.";
} elseif (strpos($msg, 'Connection refused') !== false || strpos($msg, 'Can\'t connect') !== false) {
$hint = "<strong>Hint:</strong> MySQL might not be running. Try: <code>sudo systemctl start mysql</code>";
} elseif (strpos($msg, 'getaddrinfo failed') !== false) {
$hint = "<strong>Hint:</strong> The host '$db_host' could not be found. If MySQL is local, use 'localhost' or '127.0.0.1'.";
}
$error = "Database Connection Failed: " . $msg . "<br><br>" . $hint;
}
} elseif ($step === 3) {
// Admin Setup
session_start();
$db = $_SESSION['db_config'];
$admin_user = $_POST['admin_user'];
$admin_email = $_POST['admin_email'];
$admin_pass = password_hash($_POST['admin_pass'], PASSWORD_DEFAULT);
try {
$pdo = new PDO("mysql:host={$db['host']};dbname={$db['name']}", $db['user'], $db['pass']);
$stmt = $pdo->prepare("INSERT INTO users (username, email, password, role) VALUES (?, ?, ?, 'admin')");
$stmt->execute([$admin_user, $admin_email, $admin_pass]);
// Generate config file
$config_content = "<?php
define('DB_HOST', '{$db['host']}');
define('DB_NAME', '{$db['name']}');
define('DB_USER', '{$db['user']}');
define('DB_PASS', '{$db['pass']}');
define('SITE_NAME', 'ChurchTube');
?>";
if (!file_exists('includes')) mkdir('includes');
file_put_contents($config_file, $config_content);
session_destroy();
header('Location: install.php?step=4');
exit;
} catch (PDOException $e) {
$error = "Admin Creation Failed: " . $e->getMessage();
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ChurchTube | Installation</title>
<link rel="stylesheet" href="assets/css/style.css">
</head>
<body>
<div class="centered-container">
<h1 class="logo" style="text-align: center; margin-bottom: 24px;">ChurchTube</h1>
<?php if ($error): ?>
<div style="background: rgba(255,0,0,0.2); padding: 12px; border-radius: 8px; margin-bottom: 20px; border: 1px solid rgba(255,0,0,0.3);">
<?= $error ?>
</div>
<?php endif; ?>
<?php if ($step === 1): ?>
<div class="wizard-step active">
<h2>Welcome to ChurchTube</h2>
<p style="margin: 16px 0; color: var(--text-muted);">This wizard will help you set up your sermon video platform in just a few minutes.</p>
<div style="background: var(--glass); padding: 16px; border-radius: 8px; margin-bottom: 24px;">
<strong>System Check:</strong>
<ul style="margin-top: 8px; list-style: none;">
<li><?= version_compare(PHP_VERSION, '7.4.0', '>=') ? '✅' : '❌' ?> PHP 7.4+ (Current: <?= phpversion() ?>)</li>
<li><?= extension_loaded('pdo_mysql') ? '✅' : '❌' ?> PDO MySQL Extension</li>
<li><?= is_writable('.') ? '✅' : '❌' ?> Directory Permissions</li>
<?php
$u_max = ini_get('upload_max_filesize');
$p_max = ini_get('post_max_size');
$is_low = (int)$u_max < 100 || (int)$p_max < 100;
?>
<li><?= $is_low ? '⚠️' : '✅' ?> Upload Limits: <?= $u_max ?> / <?= $p_max ?>
<?php if ($is_low): ?>
<div style="font-size: 0.8rem; color: #ffab40; margin-top: 4px;">
<strong>Recommendation:</strong> Your upload limit is low for videos.
Edit <code>/etc/php/7.4/apache2/php.ini</code> and set:
<pre style="margin-top: 4px; font-size: 0.75rem;">upload_max_filesize = 500M&#10;post_max_size = 500M</pre>
</div>
<?php endif; ?>
</li>
</ul>
</div>
<a href="?step=2" class="btn btn-primary" style="width: 100%;">Get Started</a>
</div>
<?php elseif ($step === 2): ?>
<div class="wizard-step active">
<h2>Database Configuration</h2>
<form method="POST">
<div class="form-group">
<label class="form-label">Database Host</label>
<input type="text" name="db_host" class="form-control" value="localhost" required>
</div>
<div class="form-group">
<label class="form-label">Database Name</label>
<input type="text" name="db_name" class="form-control" value="churchtube_db" required>
</div>
<div class="form-group">
<label class="form-label">Username</label>
<input type="text" name="db_user" class="form-control" required>
</div>
<div class="form-group">
<label class="form-label">Password</label>
<input type="password" name="db_pass" class="form-control">
</div>
<button type="submit" class="btn btn-primary" style="width: 100%;">Connect & Initialize</button>
</form>
</div>
<?php elseif ($step === 3): ?>
<div class="wizard-step active">
<h2>Create Admin Account</h2>
<form method="POST">
<div class="form-group">
<label class="form-label">Admin Username</label>
<input type="text" name="admin_user" class="form-control" required>
</div>
<div class="form-group">
<label class="form-label">Email Address</label>
<input type="email" name="admin_email" class="form-control" required>
</div>
<div class="form-group">
<label class="form-label">Password</label>
<input type="password" name="admin_pass" class="form-control" required>
</div>
<button type="submit" class="btn btn-primary" style="width: 100%;">Complete Setup</button>
</form>
</div>
<?php elseif ($step === 4): ?>
<div class="wizard-step active" style="text-align: center;">
<h2 style="color: var(--accent);">Installation Successful!</h2>
<p style="margin: 20px 0; color: var(--text-muted);">Your ChurchTube instance is now ready to use.</p>
<div style="font-size: 3rem; margin-bottom: 24px;">🎉</div>
<a href="index.php" class="btn btn-primary" style="width: 100%;">Go to Homepage</a>
</div>
<?php endif; ?>
</div>
</body>
</html>