-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.php
More file actions
282 lines (251 loc) · 11.7 KB
/
index.php
File metadata and controls
282 lines (251 loc) · 11.7 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
<?php
session_start();
require_once 'dbconnect.php';
ini_set("display_errors", 1);
error_reporting(E_ALL);
// セッションにログイン情報がない場合はログインページにリダイレクト
if (!isset($_SESSION['email'])) {
header('Location: ./login.php');
exit;
}
$email = $_SESSION['email'];
$username = $_SESSION['username'];
//====================================//
//==============bbs関係===============//
//====================================//
// スレッドを作成する
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['thread_name']) && !empty($_POST['thread_name']) && isset($_POST['comment'])) {
$threadName = $_POST['thread_name'];
$comment = $_POST['comment'];
// 新しいテーブルを作成
$sql = "CREATE TABLE IF NOT EXISTS thread_{$threadName} (id INT AUTO_INCREMENT PRIMARY KEY, username VARCHAR(255), comment TEXT, created_at DATETIME)";
$stmt = $dbh->query($sql);
if ($stmt) {
// スレッド作成成功したら1番目の書き込みを追加する
$now = date('Y-m-d H:i:s');
$sql = "INSERT INTO thread_{$threadName} (username, comment, created_at) VALUES (?, ?, ?)";
$stmt = $dbh->prepare($sql);
$stmt->execute([$username, $comment, $now]);
echo 'スレッドが作成されました。';
// リダイレクトによりGETリクエストを行う
header('Location: thread.php?name=' . urlencode($threadName));
exit;
} else {
echo 'スレッドの作成に失敗しました。';
}
} else {
echo 'スレッド名とコメントを入力してください。';
}
}
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>BBS|home</title>
<link rel="stylesheet" href="style.css">
<!-- Google tag (gtag.js) -->
<!--<script async src="https://www.googletagmanager.com/gtag/js?id=G-BN5KGMB0GN"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'G-BN5KGMB0GN');
</script>-->
</head>
<body id="dark-mode">
<header>
<ul class="header-ul">
<li class="title-256"><a href="#">256</a></li>
<li class="nav-link">
<nav>
<ul>
<li><a href="https://256server.com">home</a></li>
<li><a href="https://256server.com/tools">tools</a></li>
<li><a href="https://256server.com/history/index.html">history</a></li>
<li><a href="#">BBS</a></li>
</ul>
</nav>
</li>
</ul>
</header>
<main>
<h1>bbs.256server <div style="color:red">試運転</div></h1>
<div class="test">
<div class="popup-container">
<div class="popup-content">
<p>ようこそ!</p>
<p><?php echo $username; ?>さん!</p>
</div>
</div>
</div>
<a href="/bbs-rule.html">BBSのルール</a>
<a href="logout.php">ログアウト</a>
<a href="https://256server.com/bbs/index.php">誰でも書き込めるbbs</a>
<button id="darkModeButton">ダークモード</button>
<input type="button" value="リロード" onclick="window.location.reload();" />
<h2>画像アップローダー</h2>
<form action="img-upload.php" method="post" enctype="multipart/form-data">
<label for="image">画像を選択してください:</label>
<input type="file" id="image" name="image" accept="image/*" required>
<input type="submit" value="アップロード">
</form>
<h2>スレッド一覧</h2><!--ここはすべてChatGPTが書きました 動かないからって私にモンク言わないで-->
<form action="" method="get">
<p>表示順:
<select name="sort" onchange="this.form.submit()">
<option value="new" <?php if (isset($_GET['sort']) && $_GET['sort'] === 'new') echo 'selected'; ?>>新しい順</option>
<option value="old" <?php if (isset($_GET['sort']) && $_GET['sort'] === 'old') echo 'selected'; ?>>古い順</option>
<option value="popular" <?php if (isset($_GET['sort']) && $_GET['sort'] === 'popular') echo 'selected'; ?>>人気順</option>
</select>
</p>
</form>
</p>
<ul id="threadList">
<?php
// スレッド一覧の取得
$sql = "SHOW TABLES LIKE 'thread_%'";
$stmt = $dbh->query($sql);
$threads = $stmt->fetchAll(PDO::FETCH_COLUMN);
// ソート方法に応じてスレッド一覧を並び替える
if (isset($_GET['sort'])) {
$sort = $_GET['sort'];
if ($sort === 'old') {
// 古い順はスレッドの最初の書き込み時間でソート
usort($threads, function ($a, $b) use ($dbh) {
$sql = "SELECT MIN(created_at) AS first_created_at FROM {$a}";
$stmt = $dbh->query($sql);
$firstCreatedAtA = $stmt->fetchColumn();
$sql = "SELECT MIN(created_at) AS first_created_at FROM {$b}";
$stmt = $dbh->query($sql);
$firstCreatedAtB = $stmt->fetchColumn();
return strtotime($firstCreatedAtA) - strtotime($firstCreatedAtB);
});
} elseif ($sort === 'new') {
// 新しい順はスレッドの最初の書き込み時間でソート
usort($threads, function ($a, $b) use ($dbh) {
$sql = "SELECT MIN(created_at) AS first_created_at FROM {$a}";
$stmt = $dbh->query($sql);
$firstCreatedAtA = $stmt->fetchColumn();
$sql = "SELECT MIN(created_at) AS first_created_at FROM {$b}";
$stmt = $dbh->query($sql);
$firstCreatedAtB = $stmt->fetchColumn();
return strtotime($firstCreatedAtB) - strtotime($firstCreatedAtA);
});
} elseif ($sort === 'popular') {
// 人気順の場合はスレッドの最後のidの値でソート
usort($threads, function ($a, $b) use ($dbh) {
$sql = "SELECT MAX(id) AS last_id FROM {$a}";
$stmt = $dbh->query($sql);
$lastIdA = $stmt->fetchColumn();
$sql = "SELECT MAX(id) AS last_id FROM {$b}";
$stmt = $dbh->query($sql);
$lastIdB = $stmt->fetchColumn();
return $lastIdB - $lastIdA;
});
}
}
// スレッド一覧を表示
foreach ($threads as $thread) {
$sql = "SELECT MAX(id) AS last_id, MIN(created_at) AS first_created_at FROM {$thread}";
$stmt = $dbh->query($sql);
$threadInfo = $stmt->fetch(PDO::FETCH_ASSOC);
$lastId = $threadInfo['last_id'];
$firstCreatedAt = $threadInfo['first_created_at'];
echo '<li><a href="thread.php?name=' . htmlspecialchars(substr($thread, 7)) . '">' . htmlspecialchars(substr($thread, 7)) . '(' . $lastId . ')</a></li>';
}
?>
</ul>
<h2>新しいスレッドを立てる</h2>
<form action="index.php" method="POST">
<label for="thread_name">スレッド名:</label>
<input type="text" id="thread_name" name="thread_name" required><br>
<label for="comment">コメント:</label>
<textarea id="comment" name="comment" rows="1.5" required></textarea><br>
<input type="submit" value="作成"><a href="/thread-rule.html">スレッドを立てる前に</a>
</form>
</main>
<script>
document.addEventListener("DOMContentLoaded", () => {
const popupContainer = document.querySelector(".popup-container");
const cookieName = "popupShown";
// Cookieがすでに保存されている場合はポップアップを表示しない
if (!getCookie(cookieName)) {
setTimeout(() => {
popupContainer.style.top = "8px"; // 上から表示するために位置を0に設定
setTimeout(() => {
closePopup();
}, 2000); // 2秒後にclosePopup関数を実行
// Cookieに情報を保存して再訪時にポップアップを表示しないようにする
setCookie(cookieName, "true", 1); // 1日間有効なCookieを設定
}, 900); // 0.9秒後にポップアップ表示の処理を実行
}
});
const popupContainer = document.querySelector(".popup-container");
function closePopup() {
popupContainer.classList.add("closing"); // クラスを追加
setTimeout(() => {
popupContainer.classList.remove("closing"); // クラスを削除
popupContainer.style.top = "-100%"; // 画面外に戻すために位置を-100%に設定
}, 2000); // 2秒後に実行
}
// Cookieを設定する関数
function setCookie(name, value, days) {
const expires = new Date();
expires.setTime(expires.getTime() + days * 24 * 60 * 60 * 1000);
document.cookie = name + "=" + value + ";expires=" + expires.toUTCString();
}
// Cookieを取得する関数
function getCookie(name) {
const cookieArr = document.cookie.split(";");
for (let i = 0; i < cookieArr.length; i++) {
const cookiePair = cookieArr[i].split("=");
if (name === cookiePair[0].trim()) {
return decodeURIComponent(cookiePair[1]);
}
}
return null;
}
</script>
<script>
const darkModeButton = document.getElementById('darkModeButton');
const content = document.getElementById('dark-mode');
const DARK_MODE_COOKIE_NAME = 'darkMode';
darkModeButton.addEventListener('click', toggleDarkMode);
// ページ読み込み時に保存されたダークモードの状態を復元
if (getDarkModeCookie()) {
content.classList.add('dark-mode');
}
header-dark
function toggleDarkMode() {
content.classList.toggle('dark-mode');
const darkMode = content.classList.contains('dark-mode');
setDarkModeCookie(darkMode);
}
function setDarkModeCookie(value) {
document.cookie = `${DARK_MODE_COOKIE_NAME}=${value}; path=/`;
}
function getDarkModeCookie() {
const cookies = document.cookie.split(';');
for (const cookie of cookies) {
const [name, value] = cookie.split('=');
if (name.trim() === DARK_MODE_COOKIE_NAME) {
return value.trim() === 'true';
}
}
return false;
}
</script>
<script>
// 表示順を切り替えるJavaScript
function changeSort() {
const sort = document.querySelector('select[name="sort"]').value;
const currentUrl = new URL(window.location.href);
currentUrl.searchParams.set('sort', sort);
window.location.href = currentUrl.href;
}
</script>
</body>
</html>