tougao/config/database.php

162 lines
6.8 KiB
PHP
Raw Permalink Normal View History

2025-05-26 15:23:18 +08:00
<?php
/**
* 数据库配置文件
* 支持MySQL和SQLite数据库
*/
class Database {
private $host;
private $port;
private $db_name;
private $username;
private $password;
private $use_sqlite;
private $is_mysql_57;
2025-05-26 15:23:18 +08:00
private $conn;
public function __construct() {
$db_config = @include __DIR__ . '/db_config.php';
$this->host = $db_config['host'] ?? 'localhost';
$this->port = $db_config['port'] ?? '3306';
$this->db_name = $db_config['db_name'] ?? 'submission_system';
$this->username = $db_config['username'] ?? 'root';
$this->password = $db_config['password'] ?? '';
$this->use_sqlite = $db_config['use_sqlite'] ?? false;
$this->is_mysql_57 = $db_config['is_mysql_57'] ?? false;
}
2025-05-26 15:23:18 +08:00
public function getConnection() {
$this->conn = null;
try {
if ($this->use_sqlite) {
// SQLite配置
$this->conn = new PDO('sqlite:' . __DIR__ . '/../data/submission.db');
2025-05-26 15:23:18 +08:00
} else {
// MySQL配置
$dsn = "mysql:host=" . $this->host . ";port=" . $this->port . ";dbname=" . $this->db_name . ";charset=utf8mb4";
$this->conn = new PDO($dsn, $this->username, $this->password, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
]);
2025-05-26 15:23:18 +08:00
}
return $this->conn;
} catch(PDOException $e) {
throw new Exception("数据库连接失败: " . $e->getMessage());
2025-05-26 15:23:18 +08:00
}
}
public function initDatabase() {
$conn = $this->getConnection();
// 创建管理员表
$admin_table = "
CREATE TABLE IF NOT EXISTS admins (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
";
// 创建网址投稿表
$website_table = "
CREATE TABLE IF NOT EXISTS website_submissions (
id INT AUTO_INCREMENT PRIMARY KEY,
url VARCHAR(500) NOT NULL,
title VARCHAR(200),
description TEXT,
keywords VARCHAR(500),
platforms TEXT,
contact VARCHAR(100),
ip_address VARCHAR(45) NOT NULL,
status ENUM('pending', 'approved', 'rejected') DEFAULT 'pending',
admin_note TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)
";
// 创建APP/软件投稿表
$app_table = "
CREATE TABLE IF NOT EXISTS app_submissions (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(200) NOT NULL,
platform VARCHAR(100) NOT NULL,
version VARCHAR(50),
icon_url VARCHAR(500),
download_url VARCHAR(500),
website_url VARCHAR(500),
description TEXT,
platforms TEXT,
contact VARCHAR(100),
ip_address VARCHAR(45) NOT NULL,
status ENUM('pending', 'approved', 'rejected') DEFAULT 'pending',
admin_note TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)
";
// 创建IP限制表
$ip_limit_table = "
CREATE TABLE IF NOT EXISTS ip_submissions (
id INT AUTO_INCREMENT PRIMARY KEY,
ip_address VARCHAR(45) NOT NULL,
submission_date DATE NOT NULL,
count INT DEFAULT 1,
UNIQUE KEY unique_ip_date (ip_address, submission_date)
)
";
if ($this->use_sqlite) {
// SQLite语法调整
$admin_table = str_replace('AUTO_INCREMENT', '', $admin_table);
$admin_table = str_replace('INT AUTO_INCREMENT PRIMARY KEY', 'INTEGER PRIMARY KEY AUTOINCREMENT', $admin_table);
$admin_table = str_replace('TIMESTAMP DEFAULT CURRENT_TIMESTAMP', 'DATETIME DEFAULT CURRENT_TIMESTAMP', $admin_table);
$website_table = str_replace('AUTO_INCREMENT', '', $website_table);
$website_table = str_replace('INT AUTO_INCREMENT PRIMARY KEY', 'INTEGER PRIMARY KEY AUTOINCREMENT', $website_table);
$website_table = str_replace('ENUM(\'pending\', \'approved\', \'rejected\')', 'TEXT CHECK(status IN (\'pending\', \'approved\', \'rejected\'))', $website_table);
$website_table = str_replace('TIMESTAMP DEFAULT CURRENT_TIMESTAMP', 'DATETIME DEFAULT CURRENT_TIMESTAMP', $website_table);
$website_table = str_replace('ON UPDATE CURRENT_TIMESTAMP', '', $website_table);
$app_table = str_replace('AUTO_INCREMENT', '', $app_table);
$app_table = str_replace('INT AUTO_INCREMENT PRIMARY KEY', 'INTEGER PRIMARY KEY AUTOINCREMENT', $app_table);
$app_table = str_replace('ENUM(\'pending\', \'approved\', \'rejected\')', 'TEXT CHECK(status IN (\'pending\', \'approved\', \'rejected\'))', $app_table);
$app_table = str_replace('TIMESTAMP DEFAULT CURRENT_TIMESTAMP', 'DATETIME DEFAULT CURRENT_TIMESTAMP', $app_table);
$app_table = str_replace('ON UPDATE CURRENT_TIMESTAMP', '', $app_table);
$ip_limit_table = str_replace('AUTO_INCREMENT', '', $ip_limit_table);
$ip_limit_table = str_replace('INT AUTO_INCREMENT PRIMARY KEY', 'INTEGER PRIMARY KEY AUTOINCREMENT', $ip_limit_table);
}
try {
$conn->exec($admin_table);
$conn->exec($website_table);
$conn->exec($app_table);
$conn->exec($ip_limit_table);
// 插入默认管理员账户
$check_admin = $conn->prepare("SELECT COUNT(*) FROM admins WHERE username = 'admin'");
$check_admin->execute();
if ($check_admin->fetchColumn() == 0) {
$default_password = password_hash('admin', PASSWORD_DEFAULT);
$insert_admin = $conn->prepare("INSERT INTO admins (username, password) VALUES ('admin', ?)");
$insert_admin->execute([$default_password]);
}
return true;
} catch(PDOException $e) {
echo "数据库初始化失败: " . $e->getMessage();
return false;
}
}
public function isMySQL57() {
return $this->is_mysql_57;
}
2025-05-26 15:23:18 +08:00
}
?>