2025.11.02

日志以及文章完善了数据库存储
接下来就是完成上传数据库的前端与后端的融合了

**署名**
SkimrMe

 # 修改部分
 Changes to be committed:
	modified:   public/blog/index.php
	modified:   public/blog/logs.php
	new file:   public/blog/post/index.php
	new file:   public/blog/post/s/index.php
	deleted:    public/blog/read_file.php
	modified:   public/index.php
	deleted:    public/request/posts.php
	deleted:    src/views/index.php
	deleted:    src/views/post.php
	deleted:    src/views/posts.php
This commit is contained in:
2025-11-02 14:54:04 +08:00
parent 7727a97e4a
commit 0c70b66966
10 changed files with 162 additions and 162 deletions

View File

@@ -13,7 +13,7 @@
<tr> <tr>
<td id="td_navbar_40px"><b><a href="https://ww3.tw/" id="url_">首页</a></b></td> <td id="td_navbar_40px"><b><a href="https://ww3.tw/" id="url_">首页</a></b></td>
<td id="td_navbar_40px"><b><a href="https://ww3.tw/blog/about/" id="url_">关于</a></b></td> <td id="td_navbar_40px"><b><a href="https://ww3.tw/blog/about/" id="url_">关于</a></b></td>
<td id="td_navbar_40px"><b><a href="https://ww3.tw/blog/md/post/" id="url_">文章</a></b></td> <td id="td_navbar_40px"><b><a href="https://ww3.tw/blog/post/" id="url_">文章</a></b></td>
<td id="td_navbar_75px"><b><a href="https://ww3.tw/jumping-message/" id="url_">弹幕留言</a></b></td> <td id="td_navbar_75px"><b><a href="https://ww3.tw/jumping-message/" id="url_">弹幕留言</a></b></td>
<td id="td_navbar_75px"><b><a href="https://ww3.tw/short-url/" id="url_">短链生成</a></b></td> <td id="td_navbar_75px"><b><a href="https://ww3.tw/short-url/" id="url_">短链生成</a></b></td>
<td id="td_navbar_52px"><b><a href="https://gitea.ww3.tw/skimrme/" id="url_">gitea</a></b></td> <td id="td_navbar_52px"><b><a href="https://gitea.ww3.tw/skimrme/" id="url_">gitea</a></b></td>
@@ -46,7 +46,7 @@
<img id="open-ww3-project-img" width="10%" src="./src/img/open-ww3-project.png" alt="open-ww3-project-img"> <img id="open-ww3-project-img" width="10%" src="./src/img/open-ww3-project.png" alt="open-ww3-project-img">
<div id="post_content"> <div id="post_content">
<?php <?php
include "./read_file.php"; include "post/index.php";
?> ?>
</div> </div>
<!-- gitea 图标 --> <!-- gitea 图标 -->
@@ -81,7 +81,7 @@
color: rgb(202, 202, 202); color: rgb(202, 202, 202);
overflow: auto; overflow: auto;
/* up、right、down、left */ /* up、right、down、left */
margin: -250px 0px 0px 700px; margin: -255px 0px 0px 700px;
} }
body body
{ {

View File

@@ -1,6 +1,6 @@
<?php <?php
// 设定数据库路径 // 设定数据库路径
$db_path = '../../databases/sqlite/owp.db'; $db_path = '/var/www/owp/open-ww3-project-ww3-tw/databases/sqlite/owp.db';
try { try {
// 连接数据库 // 连接数据库
@@ -15,7 +15,7 @@
// 内容区 // 内容区
// 执行sql命令 查询表单 // 执行sql命令 查询表单
$select_id_date_content_from_logs_b_to_s /*查询logs表单中的id date content id大到小排列*/ = $db->query('SELECT id, date, content FROM logs ORDER BY id DESC'); // 执行查询logs表单中的id date content id大到小排列的命令 $select_id_date_content_from_logs_b_to_s /*查询logs表单中的id date content id大到小排列*/ = $db->query('SELECT id, date, content FROM logs ORDER BY id DESC'); // 执行查询logs表单中的id date content id大到小排列的命令
// 循环 写入 // 循环 写入
while ($row = $select_id_date_content_from_logs_b_to_s->fetchArray(SQLITE3_ASSOC)) { while ($row = $select_id_date_content_from_logs_b_to_s->fetchArray(SQLITE3_ASSOC)) {

View File

@@ -0,0 +1,46 @@
<?php
// 设定数据库路径
$db_path = '/var/www/owp/open-ww3-project-ww3-tw/databases/sqlite/owp.db';
try {
// 连接数据库
$db = new SQLite3($db_path);
// 连接成功输出连接成功
echo "<!--数据库连接成功-->";
// 如果连接成功,但是内部状态有问题
if ($db->lastErrorCode() !==0) {
// 依旧显示为连接失败
die("数据库连接失败");
}
// 内容区
// 执行sql命令 查询表单
$select_id_date_title_from_posts /*查询posts表单中的id date title id从小到大排列*/ = $db->query('SELECT id, date, title FROM posts ORDER BY id ASC'); // 执行查询posts表单中的id date title id从小到大排列的命令
// 循环 写入
echo "<center><b><h2>全部文章</h2></b></center>";
echo "文章如下↓";
echo "<br>";
echo "<br>";
while ($row = $select_id_date_title_from_posts->fetchArray(SQLITE3_ASSOC)) {
echo "文章id: " . $row['id'] . "<br>";
echo $row['date'] . "&nbsp;&nbsp;&nbsp;" . "文章标题: " . $row['title'] . "<br>";
echo "<a href='https://ww3.tw/blog/post/s/?id=" . $row['id'] . "'>页面跳转</a><br><br>";
}
// 关闭数据库连接
$db->close();
// 捕获php报错
} catch (Exception $e) {
// 依旧显示为连接失败
die("数据库连接失败");
// 关闭数据库连接
$db->close();
}
?>

View File

@@ -0,0 +1,110 @@
<?php
/**
* ==========================================================
* 文章内容显示程序 (post.php)
* 功能: 根据 URL 中的 ID (例如: ?id=1) 从 SQLite 数据库中查询并显示文章内容。
* 安全性: 采用预处理语句防止 SQL 注入,并对 ID 进行了严格的类型和范围检查。
* ==========================================================
*/
// 设置最大可接受的文章 ID 阈值(文章数预估)
const MAX_ARTICLE_ID = 100000;
// 1. 获取 URL 中的 ID 参数
$post_id = $_GET['id'] ?? null;
// ==========================================================
// 🚨 您的 SQLite 数据库文件路径 🚨
// 已更新为您的指定路径:
$db_path = '/var/www/owp/open-ww3-project-ww3-tw/databases/sqlite/owp.db';
// ==========================================================
/**
* 2. 严格的输入验证 (健壮性检查)
* 拦截所有非数字、无效范围或为空的输入
*/
if (
empty($post_id) ||
!is_numeric($post_id) ||
(int)$post_id <= 0 ||
(int)$post_id > MAX_ARTICLE_ID
) {
http_response_code(400); // HTTP 400 Bad Request
$error_msg = "错误:文章 ID 格式不正确。ID 必须是 1 到 " . MAX_ARTICLE_ID . " 之间的整数。";
die($error_msg);
}
// 通过检查后,将 ID 强制转换为整数
$post_id = (int)$post_id;
try {
/**
* 3. 连接 SQLite 数据库
* 注意:如果 PHP 运行用户没有读取此路径文件的权限,这里会失败。
*/
$db = new SQLite3($db_path);
/**
* 4. 准备 SQL 查询语句 (核心安全防线:预处理语句)
* 使用占位符 :id 代替用户输入。
*/
$sql = 'SELECT title, content FROM posts WHERE id = :id';
// 准备语句
$stmt = $db->prepare($sql);
// 绑定参数:将 :id 替换为 $post_id 的值,并明确指定它是一个整数类型
$stmt->bindValue(':id', $post_id, SQLITE3_INTEGER);
/**
* 5. 执行查询并获取结果
*/
$result = $stmt->execute();
// 从结果集中获取一行数据
$row = $result->fetchArray(SQLITE3_ASSOC);
/**
* 6. 显示内容或 404 错误
*/
if ($row) {
// 使用 htmlspecialchars() 函数防止 XSS 攻击
$title = $row['title'];
$content = $row['content'];
// --- 页面输出开始 ---
echo "<!DOCTYPE html>";
echo "<html lang='zh-CN'>";
echo "<head><meta charset='UTF-8'><title>{$title}</title></head>";
echo "<body>";
echo "<h1>{$title}</h1>";
// nl2br 用于将换行符转换成 <br>
echo "<article>" . nl2br($content) . "</article>";
echo "</body>";
echo "</html>";
// --- 页面输出结束 ---
} else {
// 文章未找到
http_response_code(404); // 404 Not Found
echo "<h1>404 Not Found</h1>";
echo "<p>抱歉ID 为 {$post_id} 的文章不存在。</p>";
}
/**
* 7. 清理资源
*/
$db->close();
//$stmt->close();
} catch (Exception $e) {
// 处理异常
http_response_code(500); // 500 Internal Server Error
error_log("SQLite 错误: " . $e->getMessage());
die("系统错误,请稍后重试。");
}
?>

View File

@@ -1,30 +0,0 @@
<?php
$file = '../../databases/no_db/post.text'; //定义文件的位置
$content = file_get_contents($file);
//分割输出内容
$lines = explode("\n",$content);
$sequence_id = 1; // 设定id起始数字
echo "文章如下: ";
foreach ($lines as $line) {
if (preg_match('/\{([^}]+)\};\{"([^"]+)\"};/', $line, $matches)) {
$data = $matches[1]; // 提取出数据
$title = $matches[2]; // 提取出标题
echo "<br><br>";
echo "文章顺序ID: " . $sequence_id . "<br>"; // 输出顺序ID
echo "" . $data . "\n";
echo "" . $title . "\n";
echo "<a href='https://ww3.tw/blog/md/post/s/$sequence_id' style='color: black;'> 跳转页面</a>";
//echo '链接 (URL): <a herf="">链接</a>';
$sequence_id++; // 每次循环后顺序ID加1
}
}

View File

@@ -15,7 +15,7 @@
} }
// posts 路由 // posts 路由
include "./request/posts.php"; // 整改中
// 所有路由匹配结束后,检查是否找到路由 // 所有路由匹配结束后,检查是否找到路由

View File

@@ -1,50 +0,0 @@
<?php
switch($request_uri){
#############################
case '/blog/md/';
include '/var/www/owp/open-ww3-project-ww3-tw/src/md/inite.php';
exit(); // 执行成功立即结束
break;
case '/blog/md';
header("Location: https://ww3.tw/blog/md/");
exit(); // 执行成功立即结束
break;
##############################
case '/blog/image';
include '/var/www/owp/open-ww3-project-ww3-tw/src/image/s/2025/7/30/index.php';
exit(); // 执行成功立即结束
break;
case '/blog/md/src/php-markdown/';
header("Location: https://ww3.tw/blog/md/");
exit(); // 执行成功立即结束
break;
case '/blog/md/post/';
include '../src/views/post.php';
exit(); // 执行成功立即结束
break;
case '/blog/md/post/s/2025/07/30/';
header("Location: https://ww3.tw/blog/md/post/s/1");
exit(); // 执行成功立即结束
break;
case '/blog/md/post/s/2025/07/30';
header("Location: https://ww3.tw/blog/md/post/s/1");
exit(); // 执行成功立即结束
break;
}
$post_id = null; // 初始化 post_id 变量
switch(true){
case preg_match('/^\/blog\/md\/post\/s\/(\d+)$/', $request_uri, $matches):
$post_id = (int)$matches[1];
include "../src/views/posts.php";
exit(); // 执行成功立即结束
break;
}

View File

@@ -1,5 +0,0 @@
<?php
require './MD.php';
$md=new MD('114514.md');
$md->output();

View File

@@ -1,35 +0,0 @@
<center>
<h1>
全部文章
</h1>
</center>
<?php
$file = '../databases/no_db/post.text';
$content = file_get_contents($file);
//分割输出内容
$lines = explode("\n",$content);
$sequence_id = 1; // 设定id起始数字
echo "文章如下: ";
// 输出提取到的内容
foreach ($lines as $line) {
if (preg_match('/\{([^}]+)\};\{"([^"]+)\"};/', $line, $matches)) {
$data = $matches[1]; // 提取出数据
$title = $matches[2]; // 提取出标题
echo "<br><br>";
echo "文章顺序ID: " . $sequence_id . "<br>"; // 输出顺序ID
echo "" . $data . "\n";
echo "" . $title . "\n";
echo "<a href='https://ww3.tw/blog/md/post/s/$sequence_id' style='color: black;'> 跳转页面</a>";
//echo '链接 (URL): <a herf="">链接</a>';
$sequence_id++; // 每次循环后顺序ID加1
}
}
//print_r ($lines);

View File

@@ -1,36 +0,0 @@
<?php
// 在文件顶部启动 session
session_start();
if (isset($post_id)) {
$file_path = "../md/" . $post_id . ".md";
if (file_exists($file_path)) {
// 检查是否是重定向后的请求
if (isset($_SESSION['redirected_to_post']) && $_SESSION['redirected_to_post'] == $post_id) {
// 是重定向后的请求,直接显示内容
$content = file_get_contents($file_path);
sleep(2);
unset($_SESSION['redirected_to_post']); // 清除 session 标记,避免影响其他请求
require_once '../md/MD.php';
$md = new MD('../md/' . $post_id . '.md');
// 输出处理后的内容
echo $md->output();
exit();
} else {
$random_string = substr(str_shuffle("a_bc-def_ghij-klmno_-pq-rs_tu-vwxyz-_$&_#12-34-567-8-0"), 0, 16);
// 这是第一次请求,执行重定向
echo "<h2>" . "找到id为" . $post_id . "的文章<br>正在为您跳转" . "</h2>";
$delay = 0.5;
$_SESSION['redirected_to_post'] = $post_id; // 设置 session 标记
header("Refresh: $delay; url=/blog/md/post/s/$post_id#content&$random_string");
exit();
}
} else {
echo "<h2>没有id为" . $post_id . "的文章</h2>";
}
} else {
echo "<h1>抱歉没有找到对应id文章请检查</h1>";
}