-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.php
More file actions
116 lines (92 loc) · 2.81 KB
/
Copy pathdb.php
File metadata and controls
116 lines (92 loc) · 2.81 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
require_once __DIR__ . '/integritie.php'; integritie_gate(__FILE__);
ini_set('display_errors', '0');
ini_set('log_errors', '1');
function getDb() {
static $pdo = null;
if ($pdo !== null) {
return $pdo;
}
$dbPath = __DIR__ . '/storage/v31n.sqlite3';
$isNew = !file_exists($dbPath);
$pdo = new PDO('sqlite:' . $dbPath);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec('PRAGMA journal_mode=WAL');
$pdo->exec('PRAGMA foreign_keys=ON');
$pdo->exec('PRAGMA busy_timeout=5000');
if ($isNew) {
createSchema($pdo);
migrateChunkIndex($pdo);
migrateMaps($pdo);
}
return $pdo;
}
function createSchema(PDO $pdo) {
$pdo->exec('
CREATE TABLE IF NOT EXISTS chunks (
hash TEXT PRIMARY KEY,
offset INTEGER NOT NULL,
length INTEGER NOT NULL
)
');
$pdo->exec('
CREATE TABLE IF NOT EXISTS maps (
file_id TEXT PRIMARY KEY,
encrypted_payload TEXT NOT NULL,
payload_iv TEXT NOT NULL,
salt TEXT NOT NULL,
created_at INTEGER NOT NULL
)
');
$pdo->exec('
CREATE TABLE IF NOT EXISTS embed_bans (
file_id TEXT PRIMARY KEY,
banned_at INTEGER NOT NULL
)
');
}
function migrateChunkIndex(PDO $pdo) {
$indexPath = __DIR__ . '/storage/chunk_index.json';
if (!file_exists($indexPath)) {
return;
}
$content = file_get_contents($indexPath);
$index = json_decode($content, true);
if (!$index || !is_array($index)) {
return;
}
$pdo->beginTransaction();
$stmt = $pdo->prepare('INSERT OR IGNORE INTO chunks (hash, offset, length) VALUES (?, ?, ?)');
foreach ($index as $hash => $entry) {
$stmt->execute([$hash, $entry['offset'], $entry['length']]);
}
$pdo->commit();
}
function migrateMaps(PDO $pdo) {
$mapsDir = __DIR__ . '/storage/maps';
if (!is_dir($mapsDir)) {
return;
}
$files = glob($mapsDir . '/*.json');
if (empty($files)) {
return;
}
$pdo->beginTransaction();
$stmt = $pdo->prepare('INSERT OR IGNORE INTO maps (file_id, encrypted_payload, payload_iv, salt, created_at) VALUES (?, ?, ?, ?, ?)');
foreach ($files as $file) {
$fileId = basename($file, '.json');
$data = json_decode(file_get_contents($file), true);
if (!$data || !isset($data['encrypted_payload'], $data['payload_iv'], $data['salt'])) {
continue;
}
$createdAt = filemtime($file) ?: time();
$stmt->execute([
$fileId,
$data['encrypted_payload'],
$data['payload_iv'],
$data['salt'],
$createdAt
]);
}
$pdo->commit();
}