basic logic

This commit is contained in:
mei 2024-10-29 13:13:37 +08:00
parent e13e386e32
commit 07ab18a47b
13 changed files with 259 additions and 0 deletions

12
core/clean.php Normal file
View File

@ -0,0 +1,12 @@
<?php
// 引入错误报告
error_reporting(E_ALL);
// 定义一个用于清理和验证输入的函数
function sanitizeInput($input) {
$input = trim($input);
$input = stripslashes($input);
$input = htmlspecialchars($input);
return $input;
}
?>

13
core/config.php Normal file
View File

@ -0,0 +1,13 @@
<?php
$host = '1Panel-mysql-x'; // 数据库地址
$db = ''; // 数据库名
$user = ''; // 数据库用户名
$pass = ''; // 数据库密码
try {
$pdo = new PDO("mysql:host=$host;dbname=$db", $user, $pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>

7
core/router.php Normal file
View File

@ -0,0 +1,7 @@
<?php
if (isset($_SESSION['user_id'])) {
passer();
} else {
header("application/json");
}

39
import.sql Normal file
View File

@ -0,0 +1,39 @@
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
`email` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
`group` ENUM('admin', 'user') DEFAULT 'user',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
contact VARCHAR(255) NULL
`phone_number` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL
);
CREATE TABLE love_wall (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT,
content TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id)
);
CREATE TABLE articles (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
content TEXT NOT NULL,
`type` ENUM('activity', 'news') NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE comments (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT,
content TEXT NOT NULL,
love_wall_id INT DEFAULT NULL,
article_id INT DEFAULT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (love_wall_id) REFERENCES love_wall(id),
FOREIGN KEY (article_id) REFERENCES articles(id)
);

81
includes/account.php Normal file
View File

@ -0,0 +1,81 @@
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['username'])) {
$username = trim($_POST['username']);
$password = $_POST['password'];
$contact = isset($_POST['contact']) ? trim($_POST['contact']) : null;
// 检查密码长度
if (strlen($password) < 5) {
$title = '密码太短至少需要5个字符';
$right_word = '注册';
$href_url = '/?page=account';
include('includes/time.php');
} else {
$password = password_hash($password, PASSWORD_DEFAULT);
// 检查用户名是否为空
if (empty($username)) {
$title = '用户名不能为空';
$right_word = '注册';
$href_url = '/?page=account';
include('includes/time.php');
} else {
// 检查用户名是否已存在
$checkStmt = $pdo->prepare("SELECT username FROM users WHERE username = ?");
$checkStmt->execute([$username]);
if ($checkStmt->rowCount() > 0) {
$title = '用户名已存在';
$right_word = '注册';
$href_url = '/?page=account';
include('includes/time.php');
} else {
$stmt = $pdo->prepare("INSERT INTO users (username, password, contact) VALUES (?, ?, ?)");
if ($stmt->execute([$username, $password, $contact])) {
$title = '注册成功';
$right_word = '登录';
$href_url = '/?page=account';
include('includes/time.php');
} else {
$title = '注册失败';
$right_word = '注册';
$href_url = '/?page=account';
include('includes/time.php');
}
}
}
}
}
// 登录
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['login_username'])) {
$login_username = trim($_POST['login_username']);
$login_password = $_POST['login_password'];
// 检查登录用户名是否为空
if (empty($login_username)) {
$title = '用户名不能为空';
$right_word = '登录';
$href_url = '/?page=account';
include('includes/time.php');
} else {
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?");
$stmt->execute([$login_username]);
$user = $stmt->fetch();
if ($user && password_verify($login_password, $user['password'])) {
$_SESSION['user_id'] = $user['id'];
$title = '登录成功';
$right_word = '首';
$href_url = '/';
include('includes/time.php');
} else {
$title = '用户名或密码错误';
$right_word = '登录';
$href_url = '/?page=account';
include('includes/time.php');
}
}
}
?>

View File

@ -0,0 +1,12 @@
<?php
include 'db.php';
if (isset($_GET['love_wall_id'])) {
$love_wall_id = $_GET['love_wall_id'];
$stmt = $pdo->prepare("SELECT comments.content, users.username FROM comments JOIN users ON comments.user_id = users.id WHERE love_wall_id = ? ORDER BY comments.created_at DESC");
$stmt->execute([$love_wall_id]);
while ($row = $stmt->fetch()) {
echo "<div class='comment'><strong>{$row['username']}:</strong> {$row['content']}</div>";
}
}
?>

View File

@ -0,0 +1,8 @@
<?php
include 'db.php';
$stmt = $pdo->query("SELECT love_wall.content, users.username FROM love_wall JOIN users ON love_wall.user_id = users.id ORDER BY love_wall.created_at DESC");
while ($row = $stmt->fetch()) {
echo "<div class='love-message'><strong>{$row['username']}:</strong> {$row['content']}</div>";
}
?>

34
includes/onesay.php Normal file
View File

@ -0,0 +1,34 @@
<?php
// 定义一个关联数组,包含名言和它们的作者
$quotes = [
"这个年龄抽烟,不是装逼就是二逼" => "秦涛",
"天涯何处无芳草,何必要在身边找,本来数量就不多,质量还不咋地" => "秦涛",
"弱小和无知,不是生存的障碍,傲慢才是" => "《三体》",
"你的无畏来源于无知" => "《三体》",
"要想逃避现实,最好的方式就是深深介入现实之中" => "《三体》",
"我爱你,与你有何相干?毁灭你,又与你有何相干?" => "《三体》",
"宇宙很大,生活更大,也许以后还有缘相见" => "《三体》",
"大多数人到死都没有向尘世之外瞥一眼" => "《三体》",
"碑是那么小,与其说是为了纪念,更像是为了忘却" => "《三体》",
"人们习惯将凡事分出黑与白,但很遗憾,现实全是灰色的" => "《三体》",
"没有不散的宴席,一切都有个尽头" => "《三体》",
"编程本身虽然是一种智力活动,但是中国的现实却更像一种体力劳动" => "《未来世界的幸存者》",
"我们只是让某些局部变得更有秩序,把混乱转移到另一些领域。" => "《未来世界的幸存者》",
"苦难就是苦难,苦难不会带来成功" => "《活着》",
"我的职业建议是任何工作要么让你学习learn要么让你赚钱earn。如果既学不到新东西又赚不到钱你就应该走了" => "Garry Tan",
"编程既不是短跑,也不是马拉松,而是日记。在日复一日的累积当中,完成你的事业" => "《四十年编程感想》"
];
$randomKey = array_rand($quotes);
$quote = $randomKey;
$author = $quotes[$randomKey];
// 创建一个数组来存储名言和作者信息
$response = [
'quote' => $quote,
'author' => $author
];
// 将数组转换为 JSON 格式并输出
echo json_encode($response);

0
includes/public/home.php Normal file
View File

View File

@ -0,0 +1,17 @@
<?php
if (isset($_SESSION['user_id']) && $_SERVER['REQUEST_METHOD'] == 'POST') {
// 获取表单数据并清理
$content = htmlspecialchars($_POST['commentContent'], ENT_QUOTES, 'UTF-8');
$user_id = $_SESSION['user_id'];
if (isset($_POST['love_wall_id'])) {
$love_wall_id = intval($_POST['love_wall_id']);
$stmt = $pdo->prepare("INSERT INTO comments (user_id, content, love_wall_id) VALUES (?, ?, ?)");
$stmt->execute([$user_id, $content, $love_wall_id]);
} elseif (isset($_POST['article_id'])) {
$article_id = intval($_POST['article_id']);
$stmt = $pdo->prepare("INSERT INTO comments (user_id, content, article_id) VALUES (?, ?, ?)");
$stmt->execute([$user_id, $content, $article_id]);
}
}
?>

19
includes/submit_love.php Normal file
View File

@ -0,0 +1,19 @@
<?php
// 检查session中的user_id是否存在并且请求方法是POST
if (isset($_SESSION['user_id']) && $_SERVER['REQUEST_METHOD'] == 'POST') {
// 获取表单数据并清理
$content = htmlspecialchars($_POST['content'], ENT_QUOTES, 'UTF-8');
$user_id = $_SESSION['user_id'];
// 准备SQL语句
$stmt = $pdo->prepare("INSERT INTO love_wall (user_id, content) VALUES (?, ?)");
// 执行SQL语句
if ($stmt->execute([$user_id, $content])) {
echo "发表成功!";
} else {
echo "发表失败!";
}
}
?>

16
index.php Normal file
View File

@ -0,0 +1,16 @@
<?php
include 'core/config.php';
include 'core/clean.php';
session_start();
// 获取参数
$api_class = isset($_GET['class']) ? sanitizeInput($_GET['class']) : '';
$api = isset($_GET['api']) ? sanitizeInput($_GET['api']) : '';
// 如果不是 public api 则启用路由
if ($api_class != 'public') {
include 'core/login_router.php';
}
include '/includes/' . $api_class . '/' . $api . '.php';

1
rewrite.conf Normal file
View File

@ -0,0 +1 @@
rewrite ^/([^.]+)/([^.]+)/$ /?class=article&api=$2 last;