|
<?php |
|
|
|
|
|
class Settings { |
|
private $db = null; |
|
private $defaultSettings = [ |
|
'site_name' => 'MoeNote', |
|
'site_description' => 'A simple online note that focuses on safe sharing.', |
|
'data_directory' => '/var/www/html/data/', |
|
'db_path' => '/var/www/html/data/notes.db', |
|
'favicon_path' => '/favicon.png', |
|
'max_expire_time' => 31536000, |
|
]; |
|
|
|
public function __construct() { |
|
$this->initializeDatabase(); |
|
} |
|
|
|
private function initializeDatabase() { |
|
|
|
$dataDir = dirname($this->defaultSettings['db_path']); |
|
if (!file_exists($dataDir)) { |
|
if (!mkdir($dataDir, 0777, true)) { |
|
throw new Exception('无法创建数据目录,请检查权限'); |
|
} |
|
|
|
|
|
$htaccess = $dataDir . '/.htaccess'; |
|
if (!file_exists($htaccess)) { |
|
$htaccess_content = "Require all denied\n"; |
|
$htaccess_content .= "Options -Indexes\n"; |
|
file_put_contents($htaccess, $htaccess_content); |
|
} |
|
|
|
|
|
$index_html = $dataDir . '/index.html'; |
|
if (!file_exists($index_html)) { |
|
file_put_contents($index_html, '<!DOCTYPE html><html><head><title>403 Forbidden</title></head><body><h1>403 Forbidden</h1></body></html>'); |
|
} |
|
} |
|
|
|
|
|
$this->db = new SQLite3($this->defaultSettings['db_path']); |
|
|
|
|
|
$this->db->exec(' |
|
CREATE TABLE IF NOT EXISTS settings ( |
|
key TEXT PRIMARY KEY, |
|
value TEXT |
|
) |
|
'); |
|
|
|
$this->db->exec(' |
|
CREATE TABLE IF NOT EXISTS notes ( |
|
uuid TEXT PRIMARY KEY, |
|
content TEXT, |
|
created_at INTEGER, |
|
expires_at INTEGER, |
|
max_views INTEGER DEFAULT 0, |
|
current_views INTEGER DEFAULT 0, |
|
is_markdown INTEGER DEFAULT 0, |
|
is_encrypted INTEGER DEFAULT 0 |
|
) |
|
'); |
|
|
|
|
|
$this->db->exec(' |
|
CREATE TABLE IF NOT EXISTS files ( |
|
code TEXT PRIMARY KEY, -- 提取码 |
|
filename TEXT, -- 原始文件名 |
|
filepath TEXT, -- 存储路径 |
|
content TEXT, -- 文本内容(如果是文本类型) |
|
type TEXT, -- 类型:file 或 text |
|
created_at INTEGER, -- 创建时间 |
|
expires_at INTEGER, -- 过期时间 |
|
max_downloads INTEGER DEFAULT 0, -- 最大下载/查看次数 |
|
current_downloads INTEGER DEFAULT 0 -- 当前下载/查看次数 |
|
) |
|
'); |
|
|
|
|
|
foreach ($this->defaultSettings as $key => $value) { |
|
$stmt = $this->db->prepare('INSERT OR IGNORE INTO settings (key, value) VALUES (:key, :value)'); |
|
$stmt->bindValue(':key', $key, SQLITE3_TEXT); |
|
$stmt->bindValue(':value', $value, SQLITE3_TEXT); |
|
$stmt->execute(); |
|
} |
|
} |
|
|
|
public function getSetting($key, $default = null) { |
|
try { |
|
$stmt = $this->db->prepare('SELECT value FROM settings WHERE key = :key'); |
|
$stmt->bindValue(':key', $key, SQLITE3_TEXT); |
|
$result = $stmt->execute(); |
|
|
|
if ($row = $result->fetchArray(SQLITE3_ASSOC)) { |
|
return $row['value']; |
|
} |
|
} catch (Exception $e) { |
|
|
|
} |
|
|
|
return isset($this->defaultSettings[$key]) ? $this->defaultSettings[$key] : $default; |
|
} |
|
|
|
public function setSetting($key, $value) { |
|
$stmt = $this->db->prepare('INSERT OR REPLACE INTO settings (key, value) VALUES (:key, :value)'); |
|
$stmt->bindValue(':key', $key, SQLITE3_TEXT); |
|
$stmt->bindValue(':value', $value, SQLITE3_TEXT); |
|
return $stmt->execute(); |
|
} |
|
|
|
public function getDefaultSettings() { |
|
return $this->defaultSettings; |
|
} |
|
} |
|
|