-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.php
More file actions
190 lines (160 loc) · 6.25 KB
/
setup.php
File metadata and controls
190 lines (160 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
$schritt = 1;
$meldung = '';
$fehler = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$host = trim($_POST['host']);
$dbname = trim($_POST['dbname']);
$dbuser = trim($_POST['dbuser']);
$dbpass = trim($_POST['dbpass']);
$adminuser = trim($_POST['adminuser']);
$adminpass = $_POST['adminpass'];
$tinymceKey = trim($_POST['tinymce'] ?? '');
try {
// Verbindung zur Datenbank (ohne DB-Name für initialen Zugriff)
$pdo = new PDO("mysql:host=$host", $dbuser, $dbpass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Datenbank anlegen
$pdo->exec("CREATE DATABASE IF NOT EXISTS `$dbname` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
$pdo->exec("USE `$dbname`");
// Tabellen anlegen
$sql = "
CREATE TABLE IF NOT EXISTS benutzer (
id INT AUTO_INCREMENT PRIMARY KEY,
benutzername VARCHAR(100) NOT NULL UNIQUE,
passwort_hash VARCHAR(255) NOT NULL
);
CREATE TABLE IF NOT EXISTS kategorien (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
CREATE TABLE IF NOT EXISTS beitraege (
id INT AUTO_INCREMENT PRIMARY KEY,
titel VARCHAR(255) NOT NULL,
inhalt TEXT NOT NULL,
kategorie_id INT,
erstellt_am DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (kategorie_id) REFERENCES kategorien(id) ON DELETE SET NULL
);
";
$pdo->exec($sql);
// Admin-Benutzer anlegen
$hash = password_hash($adminpass, PASSWORD_DEFAULT);
$stmt = $pdo->prepare("INSERT INTO benutzer (benutzername, passwort_hash) VALUES (:u, :p)");
$stmt->execute([':u' => $adminuser, ':p' => $hash]);
// Ordner prüfen und Dateien schreiben
$includesDir = __DIR__ . '/includes';
if (!is_dir($includesDir)) {
if (!mkdir($includesDir, 0755, true)) {
throw new Exception("Ordner 'includes/' konnte nicht erstellt werden.");
}
}
if (!is_writable($includesDir)) {
throw new Exception("Ordner 'includes/' ist nicht beschreibbar. Bitte Schreibrechte prüfen.");
}
// db.php erzeugen
$dbphp = "<?php\n".
"\$host = '$host';\n".
"\$dbname = '$dbname';\n".
"\$user = '$dbuser';\n".
"\$pass = '$dbpass';\n".
"try {\n".
" \$pdo = new PDO(\"mysql:host=\$host;dbname=\$dbname;charset=utf8mb4\", \$user, \$pass);\n".
" \$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\n".
"} catch (PDOException \$e) {\n".
" die(\"Verbindung fehlgeschlagen: \" . \$e->getMessage());\n".
"}\n?>";
file_put_contents($includesDir . '/db.php', $dbphp);
// config.php erzeugen (API-Keys o.Ä.)
$configphp = "<?php\n" .
"\$TINYMCE_API_KEY = '" . addslashes($tinymceKey) . "';\n" .
"?>";
file_put_contents($includesDir . '/config.php', $configphp);
$meldung = "✅ Setup abgeschlossen! Du kannst dich jetzt einloggen.";
$schritt = 2;
} catch (Exception $e) {
$fehler = "❌ Fehler: " . $e->getMessage();
}
}
?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<title>Setup</title>
<style>
body { font-family: Arial, sans-serif; background: #f4f4f4; }
.container {
max-width: 600px;
margin: 3rem auto;
background: white;
padding: 2rem;
border-radius: 6px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
label { display: block; margin-top: 1rem; font-weight: bold; }
input[type="text"], input[type="password"] {
width: 100%;
padding: 0.5rem;
margin-top: 0.3rem;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
button {
margin-top: 2rem;
padding: 0.7rem 1.5rem;
background: #0066cc;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
footer {
text-align: center;
color: #777;
font-size: 0.9rem;
margin-top: 3rem;
padding-bottom: 1rem;
}
.msg { margin-top: 1rem; padding: 1rem; border-radius: 5px; }
.success { background: #d4edda; color: #155724; border: 1px solid #c3e6cb; }
.error { background: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; }
</style>
</head>
<body>
<div class="container">
<h2>🔧 Setup</h2>
<?php if ($meldung): ?>
<div class="msg success"><?= htmlspecialchars($meldung) ?></div>
<p><a href="login.php">➡️ Weiter zum Login</a></p>
<?php elseif ($fehler): ?>
<div class="msg error"><?= htmlspecialchars($fehler) ?></div>
<?php endif; ?>
<?php if ($schritt === 1): ?>
<form method="post">
<label for="host">Datenbank-Host:</label>
<input type="text" name="host" id="host" required value="localhost">
<label for="dbname">Datenbank-Name:</label>
<input type="text" name="dbname" id="dbname" required>
<label for="dbuser">Datenbank-Benutzer:</label>
<input type="text" name="dbuser" id="dbuser" required>
<label for="dbpass">Datenbank-Passwort:</label>
<input type="password" name="dbpass" id="dbpass" required>
<hr>
<label for="adminuser">Admin-Benutzername:</label>
<input type="text" name="adminuser" id="adminuser" required>
<label for="adminpass">Admin-Passwort:</label>
<input type="password" name="adminpass" id="adminpass" required>
<hr>
<label for="tinymce">TinyMCE API-Key (optional):</label>
<input type="text" name="tinymce" id="tinymce" placeholder="z. B. to7f6t9h9i...">
<button type="submit">Setup starten</button>
</form>
<?php endif; ?>
</div>
<footer>
© 2021 - <?= date('Y') ?> <a href="https://wahke.lu">wahke.lu</a> | Kevin Wahl – Alle Rechte vorbehalten.
</footer>
</body>
</html>