-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.php
More file actions
301 lines (278 loc) · 14.9 KB
/
install.php
File metadata and controls
301 lines (278 loc) · 14.9 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
// 检查是否已安装
if (file_exists('install.lock')) {
die('<h2>系统已安装</h2><p>如需重新安装,请删除 install.lock 文件后重试。</p><p><a href="admin.php">进入管理后台</a></p>');
}
// 处理表单提交
$error = '';
$success = '';
$step = isset($_POST['step']) ? intval($_POST['step']) : 1;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if ($step === 1) {
// 第一步:验证数据库连接
$db_host = $_POST['db_host'] ?? 'localhost';
$db_user = $_POST['db_user'] ?? '';
$db_pass = $_POST['db_pass'] ?? '';
$db_name = $_POST['db_name'] ?? '';
// 测试数据库连接
$test_conn = @new mysqli($db_host, $db_user, $db_pass);
if ($test_conn->connect_error) {
$error = "数据库连接失败: " . $test_conn->connect_error;
} else {
// 检查数据库是否存在,如果不存在则创建
if (!$test_conn->select_db($db_name)) {
if ($test_conn->query("CREATE DATABASE IF NOT EXISTS `$db_name` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci")) {
$success = "数据库创建成功";
} else {
$error = "创建数据库失败: " . $test_conn->error;
}
}
if (empty($error)) {
$step = 2;
// 保存数据库配置到会话中
session_start();
$_SESSION['db_config'] = [
'host' => $db_host,
'user' => $db_user,
'pass' => $db_pass,
'name' => $db_name
];
}
}
} elseif ($step === 2) {
// 第二步:创建数据表和管理员账户
session_start();
if (!isset($_SESSION['db_config'])) {
$error = "会话已过期,请重新开始安装";
$step = 1;
} else {
$db_config = $_SESSION['db_config'];
$admin_user = $_POST['admin_user'] ?? '';
$admin_pass = $_POST['admin_pass'] ?? '';
$admin_pass_confirm = $_POST['admin_pass_confirm'] ?? '';
if (empty($admin_user) || empty($admin_pass)) {
$error = "管理员用户名和密码不能为空";
} elseif ($admin_pass !== $admin_pass_confirm) {
$error = "两次输入的密码不一致";
} else {
// 连接数据库
$conn = @new mysqli($db_config['host'], $db_config['user'], $db_config['pass'], $db_config['name']);
if ($conn->connect_error) {
$error = "数据库连接失败: " . $conn->connect_error;
} else {
$conn->set_charset("utf8mb4");
// 创建数据表
$sql = "CREATE TABLE IF NOT EXISTS hitokoto (
id INT AUTO_INCREMENT PRIMARY KEY,
content TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
if (!$conn->query($sql)) {
$error = "创建数据表失败: " . $conn->error;
} else {
// 创建管理员表
$sql = "CREATE TABLE IF NOT EXISTS admin_users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
if (!$conn->query($sql)) {
$error = "创建管理员表失败: " . $conn->error;
} else {
// 添加管理员账户
$hashed_password = password_hash($admin_pass, PASSWORD_DEFAULT);
$stmt = $conn->prepare("INSERT INTO admin_users (username, password) VALUES (?, ?)");
$stmt->bind_param("ss", $admin_user, $hashed_password);
if (!$stmt->execute()) {
$error = "创建管理员账户失败: " . $stmt->error;
} else {
// 添加示例数据
$sample = [
"生活不止眼前的苟且,还有诗和远方",
"代码改变世界",
"Hello, World!"
];
$stmt = $conn->prepare("INSERT INTO hitokoto (content) VALUES (?)");
$count = 0;
foreach ($sample as $text) {
$stmt->bind_param("s", $text);
if ($stmt->execute()) {
$count++;
}
}
// 生成配置文件
$config_content = "<?php\n";
$config_content .= "// 数据库配置\n";
$config_content .= "define('DB_HOST', '" . addslashes($db_config['host']) . "');\n";
$config_content .= "define('DB_USER', '" . addslashes($db_config['user']) . "');\n";
$config_content .= "define('DB_PASS', '" . addslashes($db_config['pass']) . "');\n";
$config_content .= "define('DB_NAME', '" . addslashes($db_config['name']) . "');\n";
$config_content .= "define('DB_TABLE', 'hitokoto');\n";
$config_content .= "define('ADMIN_TABLE', 'admin_users');\n";
$config_content .= "?>";
if (file_put_contents('config.inc.php', $config_content) === false) {
$error = "无法写入配置文件,请检查目录权限";
} else {
// 创建安装锁文件
file_put_contents('install.lock', '安装完成于: ' . date('Y-m-d H:i:s'));
$success = "安装完成!添加了 $count 条示例数据";
$step = 3;
// 清除会话
unset($_SESSION['db_config']);
session_destroy();
}
}
}
}
$conn->close();
}
}
}
}
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>一言系统安装</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body {
background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
height: 100vh;
display: flex;
align-items: center;
}
.install-card {
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
border-radius: 15px;
border: none;
}
.step-indicator {
display: flex;
justify-content: space-between;
margin-bottom: 2rem;
position: relative;
}
.step-indicator::before {
content: '';
position: absolute;
top: 20px;
left: 0;
right: 0;
height: 2px;
background-color: #dee2e6;
z-index: 1;
}
.step {
width: 40px;
height: 40px;
border-radius: 50%;
background-color: #dee2e6;
display: flex;
align-items: center;
justify-content: center;
position: relative;
z-index: 2;
}
.step.active {
background-color: #0d6efd;
color: white;
}
.step.completed {
background-color: #198754;
color: white;
}
</style>
</head>
<body>
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card install-card">
<div class="card-body p-5">
<h1 class="text-center mb-4">一言系统安装</h1>
<!-- 步骤指示器 -->
<div class="step-indicator">
<div class="step <?= $step >= 1 ? 'active' : '' ?> <?= $step > 1 ? 'completed' : '' ?>">1</div>
<div class="step <?= $step >= 2 ? 'active' : '' ?> <?= $step > 2 ? 'completed' : '' ?>">2</div>
<div class="step <?= $step >= 3 ? 'active' : '' ?>">3</div>
</div>
<?php if (!empty($error)): ?>
<div class="alert alert-danger" role="alert">
<?= $error ?>
</div>
<?php endif; ?>
<?php if (!empty($success)): ?>
<div class="alert alert-success" role="alert">
<?= $success ?>
</div>
<?php endif; ?>
<form method="POST" action="install.php">
<input type="hidden" name="step" value="<?= $step ?>">
<?php if ($step === 1): ?>
<h3 class="mb-4">第一步:数据库配置</h3>
<div class="mb-3">
<label for="db_host" class="form-label">数据库主机</label>
<input type="text" class="form-control" id="db_host" name="db_host" value="<?= htmlspecialchars($_POST['db_host'] ?? 'localhost') ?>" required>
</div>
<div class="mb-3">
<label for="db_user" class="form-label">数据库用户名</label>
<input type="text" class="form-control" id="db_user" name="db_user" value="<?= htmlspecialchars($_POST['db_user'] ?? '') ?>" required>
</div>
<div class="mb-3">
<label for="db_pass" class="form-label">数据库密码</label>
<input type="password" class="form-control" id="db_pass" name="db_pass" value="<?= htmlspecialchars($_POST['db_pass'] ?? '') ?>">
</div>
<div class="mb-3">
<label for="db_name" class="form-label">数据库名</label>
<input type="text" class="form-control" id="db_name" name="db_name" value="<?= htmlspecialchars($_POST['db_name'] ?? '') ?>" required>
</div>
<button type="submit" class="btn btn-primary w-100 py-2">下一步</button>
<?php elseif ($step === 2): ?>
<h3 class="mb-4">第二步:管理员账户设置</h3>
<div class="mb-3">
<label for="admin_user" class="form-label">管理员用户名</label>
<input type="text" class="form-control" id="admin_user" name="admin_user" value="<?= htmlspecialchars($_POST['admin_user'] ?? '') ?>" required>
</div>
<div class="mb-3">
<label for="admin_pass" class="form-label">管理员密码</label>
<input type="password" class="form-control" id="admin_pass" name="admin_pass" required>
</div>
<div class="mb-3">
<label for="admin_pass_confirm" class="form-label">确认密码</label>
<input type="password" class="form-control" id="admin_pass_confirm" name="admin_pass_confirm" required>
</div>
<div class="d-flex justify-content-between">
<button type="button" class="btn btn-secondary" onclick="window.history.back()">上一步</button>
<button type="submit" class="btn btn-primary">下一步</button>
</div>
<?php elseif ($step === 3): ?>
<h3 class="text-center mb-4">安装完成!</h3>
<div class="text-center">
<svg xmlns="http://www.w3.org/2000/svg" width="80" height="80" fill="currentColor" class="bi bi-check-circle text-success mb-3" viewBox="0 0 16 16">
<path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14zm0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16z"/>
<path d="M10.97 4.97a.235.235 0 0 0-.02.022L7.477 9.417 5.384 7.323a.75.75 0 0 0-1.06 1.06L6.97 11.03a.75.75 0 0 0 1.079-.02l3.992-4.99a.75.75 0 0 0-1.071-1.05z"/>
</svg>
<p>一言系统已成功安装。</p>
<div class="d-grid gap-2 d-md-block">
<a href="admin.php" class="btn btn-primary me-md-2">进入管理后台</a>
<a href="../" class="btn btn-outline-secondary">查看前台</a>
</div>
<div class="mt-4 text-muted small">
<p>为确保安全,建议删除 install.php 文件</p>
</div>
</div>
<?php endif; ?>
</form>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>