-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
253 lines (219 loc) · 8.73 KB
/
index.php
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
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";
// データベースに接続
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("データベース接続エラー: " . $conn->connect_error);
}
// パスワードのセットアップ
$hashedPassword = password_hash("pass7019", PASSWORD_DEFAULT);
// 投稿フォームの表示判定
$adminMode = isset($_GET['admin']) && $_GET['admin'] == 'on';
// 投稿フォーム処理
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// パスワードの確認
$providedPassword = $_POST['password'];
if (password_verify($providedPassword, $hashedPassword)) {
// パスワードが正しい場合
$name = $_POST['name'];
$tags = $_POST['tags'];
$current_time = date("Y-m-d H:i:s");
$time_blog = date("Y年m月d日");
//内容
$content = $_POST['content'];
// 行ごとに改行を分割
$lines = explode("\n", $content);
// 各行に対して処理
$content_with_line_breaks = '';
foreach ($lines as $line) {
if (!preg_match('/<\/[hH]3>|<\/[hH]4>/', trim($line))) {
$content_with_line_breaks .= $line . '<br>';
} else {
$content_with_line_breaks .= $line;
}
}
// 複数ファイルのアップロード処理
$uploadDirectory = $tags . '/' . $name . '/';
if (!is_dir($uploadDirectory)) {
mkdir($uploadDirectory, 0777, true);
}
foreach ($_FILES['image']['tmp_name'] as $key => $tempName) {
if ($_FILES['image']['error'][$key] == UPLOAD_ERR_OK) {
$originalName = $_FILES['image']['name'][$key];
// ファイル名を一意に生成
$filename = $uploadDirectory . $originalName;
// アップロードされたファイルを移動
move_uploaded_file($tempName, $filename);
}
}
$uploadDirectory = $tags . '/' . $name . '/';
if (!is_dir($uploadDirectory)) {
mkdir($uploadDirectory, 0777, true);
}
// ファイル名を一意に生成
$filename = $uploadDirectory . 'index.html';
// ブログの中身テンプレートを生成
$blogContent = <<<HTML
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8" />
<title>$name</title>
<meta name="keywords" content="">
<meta name="description" content="">
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico">
<meta name="keywords" content="">
<meta name="copyright" content="Copyright © 2024 user. All rights reserved." />
</head>
<body>
<main id="main">
<div class="button-container">
<a href=""><i class="fa-brands fa-twitter"></i></a>
<a href=""><i class="fa-brands fa-facebook"></i></a>
</div>
<div id="content">
<h2 class="title">$name<span style="font-size: 20px; margin-left: 10px;">$time_blog<i style="margin-left: 10px;" class="fa-solid fa-hashtag">$tags</i></span></h2>
<p>
$content_with_line_breaks
</p>
<br>
</div>
</main>
<script src="https://kit.fontawesome.com/.js" crossorigin="anonymous"></script>
</body>
</html>
HTML;
// ブログの中身をファイルに保存
file_put_contents($filename, $blogContent);
// データベースに記事情報を保存
$sql = "INSERT INTO posts (name, content, tags, filepath, date) VALUES ('$name', '$content', '$tags', '$filename', '$current_time')";
if ($conn->query($sql) === TRUE) {
echo "投稿が成功しました";
} else {
echo "エラー: " . $sql . "<br>" . $conn->error;
}
} else {
// パスワードが正しくない場合
echo "エラー: パスワードが正しくありません";
}
}
// 記事一覧を取得
$sql = "SELECT * FROM posts ORDER BY date DESC";
$result = $conn->query($sql);
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<meta name="keywords" content="">
<meta name="description" content="">
<meta name="copyright" content="Copyright © 2024 user. All rights reserved." />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico">
</head>
<main id="main">
<div id="content">
<!-- 投稿フォーム(Admin Only) -->
<?php if ($adminMode): ?>
<h2>投稿フォーム(Admin Only)</h2>
<form method="POST" action="index.php" enctype="multipart/form-data">
名前: <input type="text" name="name"><br>
内容: <textarea name="content"></textarea><br>
タグ: <input type="text" name="tags"><br>
画像アップロード: <input type="file" name="image[]" multiple><br>
パスワード: <input type="password" name="password"><br>
<input type="hidden" name="current_time" value="<?php echo date('Y年m月d日'); ?>">
<input type="submit" value="投稿">
</form>
<br>
<?php endif; ?>
<!-- ここから情報をいろいろ -->
<h2>最近の投稿</h2>
<ul class="styled-list">
<?php
$sql = "SELECT * FROM posts ORDER BY date DESC LIMIT 5";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
$url = $row['filepath'];
$title = $row['name'];
$date = date('Y年m月d日', strtotime($row['date']));
$category = $row['tags'];
echo '<li><a href="' . $url . '" style="color: #e8e6e3;">' . $title . ' <span style="color: #bcb6ad; font-size: 15px;">' . $date . ' <i class="fa-solid fa-hashtag">' . $category . '</i></span></a></li>';
}
?>
</ul>
<p class="kuu1"></p>
<h2>#日記</h2>
<ul class="styled-list">
<?php
// タグが "日記" の記事を取得
$sql = "SELECT * FROM posts WHERE tags = '日記' ORDER BY date DESC";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
$url = $row['filepath'];
$title = $row['name'];
$date = date('Y年m月d日', strtotime($row['date']));
$category = $row['tags'];
echo '<li><a href="' . $url . '" style="color: #e8e6e3;">' . $title . ' <span style="color: #bcb6ad; font-size: 15px;">' . $date . ' <i class="fa-solid fa-hashtag">' . $category . '</i></span></a></li>';
}
?>
</ul>
<p class="kuu1"></p>
<h2>#PC</h2>
<ul class="styled-list">
<?php
// タグが "PC" の記事を取得
$sql = "SELECT * FROM posts WHERE tags = 'PC' ORDER BY date DESC";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
$url = $row['filepath'];
$title = $row['name'];
$date = date('Y年m月d日', strtotime($row['date']));
$category = $row['tags'];
echo '<li><a href="' . $url . '" style="color: #e8e6e3;">' . $title . ' <span style="color: #bcb6ad; font-size: 15px;">' . $date . ' <i class="fa-solid fa-hashtag">' . $category . '</i></span></a></li>';
}
?>
</ul>
<p class="kuu1"></p>
<h2>#ウイルス</h2>
<ul class="styled-list">
<?php
// タグが "テスト" の記事を取得
$sql = "SELECT * FROM posts WHERE tags = 'ウイルス' ORDER BY date DESC";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
$url = $row['filepath'];
$title = $row['name'];
$date = date('Y年m月d日', strtotime($row['date']));
$category = $row['tags'];
echo '<li><a href="' . $url . '" style="color: #e8e6e3;">' . $title . ' <span style="color: #bcb6ad; font-size: 15px;">' . $date . ' <i class="fa-solid fa-hashtag">' . $category . '</i></span></a></li>';
}
?>
</ul>
<p class="kuu1"></p>
<h2>一覧の投稿</h2>
<ul class="styled-list">
<?php
// 記事一覧を取得(最新の投稿が一番上に来るように)
$sql = "SELECT * FROM posts ORDER BY date DESC";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
$url = $row['filepath'];
$title = $row['name'];
$date = date('Y年m月d日', strtotime($row['date']));
$category = $row['tags'];
echo '<li><a href="' . $url . '" style="color: #e8e6e3;">' . $title . ' <span style="color: #bcb6ad; font-size: 15px;">' . $date . ' <i class="fa-solid fa-hashtag">' . $category . '</i></span></a></li>';
}
?>
</ul>
<br>
</div>
</main>
<script src="https://kit.fontawesome.com/.js" crossorigin="anonymous"></script>
</body>
</html>