Repository navigation
Expand file tree
/
Copy pathshare.php
More file actions
215 lines (202 loc) · 8.6 KB
/
Copy pathshare.php
File metadata and controls
215 lines (202 loc) · 8.6 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
// 分享查看页面:公开访问
require_once 'config.php';
require_once 'includes/functions.php';
// 连接数据库
try {
$db = getDB();
} catch (Exception $e) {
http_response_code(500);
echo '数据库连接失败';
exit;
}
// 确保share表存在
$db->exec("CREATE TABLE IF NOT EXISTS shares (
id INTEGER PRIMARY KEY AUTOINCREMENT,
token TEXT UNIQUE NOT NULL,
memo_id INTEGER NOT NULL,
encrypted INTEGER NOT NULL DEFAULT 0,
passcode_hash TEXT,
expires_at TEXT,
max_visits INTEGER,
visit_count INTEGER NOT NULL DEFAULT 0,
created_at TEXT NOT NULL DEFAULT (datetime('now')),
FOREIGN KEY (memo_id) REFERENCES memos(id) ON DELETE CASCADE
)");
// 兼容旧表
try { $db->exec("ALTER TABLE shares ADD COLUMN max_visits INTEGER"); } catch (Exception $e) {}
try { $db->exec("ALTER TABLE shares ADD COLUMN visit_count INTEGER NOT NULL DEFAULT 0"); } catch (Exception $e) {}
$token = $_GET['token'] ?? '';
if (empty($token)) {
http_response_code(400);
echo '缺少token';
exit;
}
// 查询分享记录
$stmt = $db->prepare("SELECT * FROM shares WHERE token = ?");
$stmt->execute([$token]);
$share = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$share) {
http_response_code(404);
echo '分享不存在或已被删除';
exit;
}
// 检查过期
if (!empty($share['expires_at'])) {
$now = new DateTime('now');
$exp = DateTime::createFromFormat('Y-m-d H:i', $share['expires_at']);
if ($exp && $now > $exp) {
http_response_code(410);
echo '分享已过期';
exit;
}
}
// 需要口令?
$needsPass = (int)$share['encrypted'] === 1;
$passOK = false;
if ($needsPass) {
// POST 校验
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$inputPass = trim($_POST['passcode'] ?? '');
if ($inputPass !== '' && password_verify($inputPass, $share['passcode_hash'])) {
$passOK = true;
} else {
$error = '口令不正确';
}
}
} else {
$passOK = true;
}
// 校验访问次数限制
if (!empty($share['max_visits'])) {
$max = (int)$share['max_visits'];
$visited = (int)$share['visit_count'];
if ($visited >= $max) {
http_response_code(410);
echo '访问链接已失效(超过最大访问次数)';
exit;
}
}
// 如通过校验,取memo
$memo = null;
if ($passOK) {
$memo = getMemoById($db, (int)$share['memo_id']);
if (!$memo) {
http_response_code(404);
echo '笔记不存在';
exit;
}
$memo['tags'] = getMemoTags($db, (int)$share['memo_id']);
$memo['attachments'] = getMemoAttachments($db, (int)$share['memo_id']);
// 通过访问校验与口令后,增加访问计数
try {
$db->prepare("UPDATE shares SET visit_count = visit_count + 1 WHERE id = ?")->execute([$share['id']]);
} catch (Exception $e) {}
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>分享查看</title>
<link rel="stylesheet" href="assets/css/style.css">
<!-- Prism.js - 代码高亮(保留用于代码块复制功能) -->
<link rel="stylesheet" href="assets/vendor/prism/themes/prism.min.css">
<script src="assets/vendor/prism/components/prism-core.min.js"></script>
<script src="assets/vendor/prism/plugins/autoloader/prism-autoloader.min.js"></script>
<!-- Vditor 预览渲染支持(数学公式、Graphviz、Mermaid等) -->
<link rel="stylesheet" href="assets/vendor/vditor/index.css">
<link rel="stylesheet" href="assets/vendor/vditor/dist/css/content-theme/light.css">
<link rel="stylesheet" href="assets/vendor/vditor/dist/js/katex/katex.min.css">
<script src="assets/vendor/vditor/dist/js/i18n/zh_CN.js"></script>
<script src="assets/vendor/vditor/index.min.js"></script>
</head>
<body style="background:#f8f9fa;">
<div style="max-width: 800px; margin: 40px auto; background: #fff; border-radius: 12px; box-shadow: var(--shadow); padding: 24px;">
<h2 style="margin-bottom: 12px;">分享内容</h2>
<?php if (!$passOK && $needsPass): ?>
<?php if (!empty($error)): ?>
<div class="toast error" style="margin-bottom:12px;">口令不正确</div>
<?php endif; ?>
<form method="post" style="display:flex; gap:8px; align-items:center;">
<input type="password" name="passcode" placeholder="请输入分享码" style="flex:1; padding:10px 12px; border:1px solid var(--border-color); border-radius:8px;">
<button type="submit" class="btn-primary">查看</button>
</form>
<?php else: ?>
<?php if ($memo): ?>
<div class="memo-card" style="box-shadow:none;">
<div class="memo-header">
<span class="memo-time"><?php echo e($memo['created_at']); ?></span>
</div>
<?php if (!empty($memo['tags'])): ?>
<div class="memo-tags">
<?php foreach ($memo['tags'] as $t): ?>
<span class="memo-tag">#<?php echo e($t['name']); ?></span>
<?php endforeach; ?>
</div>
<?php endif; ?>
<div class="memo-content" id="memoContentHtml"></div>
<?php if (!empty($memo['attachments'])): ?>
<div class="memo-attachments">
<?php foreach ($memo['attachments'] as $att): ?>
<?php if (strpos($att['file_type'], 'image/') === 0): ?>
<img src="<?php echo e($att['url']); ?>" alt="<?php echo e($att['original_name']); ?>" class="attachment-thumbnail">
<?php else: ?>
<a href="<?php echo e($att['url']); ?>" class="attachment-item">
<span><?php echo e($att['original_name']); ?></span>
</a>
<?php endif; ?>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
<script>
(function(){
const raw = <?php echo json_encode($memo['content'] ?? '', JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); ?>;
const container = document.getElementById('memoContentHtml');
// 优先使用 Vditor 预览渲染(支持数学公式、Graphviz、Mermaid等)
if (typeof Vditor !== 'undefined' && typeof Vditor.preview === 'function') {
try {
container.className = 'memo-content vditor-reset';
// 使用 Vditor.preview() 方法(注意是大写V,直接方法)
Vditor.preview(container, raw, {
cdn: './assets/vendor/vditor',
math: {
engine: 'KaTeX',
inlineDigit: true
},
markdown: {
toc: true,
mark: true,
footnotes: true,
autoSpace: true
},
speech: {
enable: false
},
hljs: {
enable: false // 禁用 highlight.js,使用 Prism.js
},
mode: 'light',
after: () => {
console.log('✅ 分享页面 Vditor 渲染完成');
}
});
} catch (error) {
console.error('❌ Vditor预览渲染失败:', error);
// 如果渲染失败,显示纯文本
container.innerText = raw;
}
} else {
// Vditor 未加载,显示纯文本
console.warn('Vditor 未加载');
container.innerText = raw;
}
})();
</script>
<?php endif; ?>
<?php endif; ?>
</div>
</body>
</html>