-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.php
More file actions
98 lines (89 loc) · 3.4 KB
/
Copy pathdatabase.php
File metadata and controls
98 lines (89 loc) · 3.4 KB
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
<?php
class Database {
private $pdo;
public function __construct() {
if (!DB_ENABLE) {
return;
}
try {
if (DB_TYPE === 'sqlite') {
$dsn = 'sqlite:' . DB_SQLITE_PATH;
$this->pdo = new PDO($dsn, null, null, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);
} else {
$dsn = sprintf(
'mysql:host=%s;dbname=%s;charset=%s',
DB_HOST,
DB_NAME,
DB_CHARSET
);
$this->pdo = new PDO($dsn, DB_USER, DB_PASS, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false
]);
}
$this->createTable();
$this->cleanOldRecords();
} catch (PDOException $e) {
throw new Exception('Database connection failed: ' . $e->getMessage());
}
}
private function createTable() {
if (DB_TYPE === 'sqlite') {
$sql = "CREATE TABLE IF NOT EXISTS access_logs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
ip VARCHAR(45) NOT NULL,
country VARCHAR(2) DEFAULT NULL,
mode VARCHAR(10) DEFAULT 'simple',
user_agent TEXT,
request_uri VARCHAR(255),
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)";
$this->pdo->exec($sql);
$this->pdo->exec("CREATE INDEX IF NOT EXISTS idx_created_at ON access_logs(created_at)");
$this->pdo->exec("CREATE INDEX IF NOT EXISTS idx_ip ON access_logs(ip)");
} else {
$sql = "CREATE TABLE IF NOT EXISTS access_logs (
id INT AUTO_INCREMENT PRIMARY KEY,
ip VARCHAR(45) NOT NULL,
country VARCHAR(2) DEFAULT NULL,
mode VARCHAR(10) DEFAULT 'simple',
user_agent TEXT,
request_uri VARCHAR(255),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
INDEX idx_created_at (created_at),
INDEX idx_ip (ip)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
$this->pdo->exec($sql);
}
}
public function logAccess($ip, $country, $mode, $userAgent, $requestUri) {
if (!DB_ENABLE) {
return;
}
$sql = "INSERT INTO access_logs (ip, country, mode, user_agent, request_uri)
VALUES (:ip, :country, :mode, :user_agent, :request_uri)";
$stmt = $this->pdo->prepare($sql);
$stmt->execute([
':ip' => $ip,
':country' => $country,
':mode' => $mode,
':user_agent' => $userAgent,
':request_uri' => $requestUri
]);
}
private function cleanOldRecords() {
if (!DB_ENABLE) {
return;
}
if (DB_TYPE === 'sqlite') {
$sql = "DELETE FROM access_logs WHERE created_at < datetime('now', '-30 days')";
} else {
$sql = "DELETE FROM access_logs WHERE created_at < DATE_SUB(NOW(), INTERVAL 30 DAY)";
}
$this->pdo->exec($sql);
}
}