-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclipboard.php
267 lines (267 loc) · 11.4 KB
/
clipboard.php
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
define('Title', '通用剪切板');
define('Interval', 300);
define('InputTimeout', 200); // 输入延迟, 用于去除连续输出抖动造成的体验下降.
define('SessionName', 's'); // null 代表不使用.
define('Expiration', 3600); // Seconds, 0 意味着不过期.
define('VersionKey', 'DefaultVersionKey');
define('UseAuth', true);
define('VerifyClientVersionHash', true); // 校验客户端 Hash 可使 VersionKey 更改后废弃所有用户之前的剪切板, 可能可以防止 Session 模式下的剪切板跨账号访问 (但可能会导致内容清空).
define('AuthCookieName', 'UCLoginCredential');
define('AuthUserList', array('user1' => array('password' => ''), 'user2' => array('password' => '', 'sessionName' => null), 'user3' => array('password' => '0b7f849446d3383546d15a480966084442cd2193' /* user3 */, 'sessionName' => 'session', 'expiration' => 86400))); // SHA1 Encrypt
define('AnonymousUsername', 'Anonymous');
#function GetData(string $username): array|bool { // PHP 7 不支持联合类型.
function GetData(string $username) {
global $userSessionName, $userExpiration;
if ($userSessionName !== null) {
return $_SESSION;
}
$jsonData = false;
if (is_file("UCJSON/{$username}.json")) {
if ($userExpiration > 0 && ($jsonMTime = filemtime("UCJSON/{$username}.json")) !== false && (($jsonMTime + $userExpiration) < time())) {
unlink("UCJSON/{$username}.json");
} else {
$jsonContent = file_get_contents("UCJSON/{$username}.json");
$jsonData = ($jsonContent !== false ? json_decode($jsonContent, true) : false);
}
}
return ($jsonData ?? array());
}
function SaveData(string $username, ?array $data): bool {
global $userSessionName;
if ($userSessionName !== null) {
if ($data !== null) {
$_SESSION = $data;
}
return true;
}
if (!is_dir('UCJSON') && !mkdir('UCJSON')) {
return false;
}
if ($data === null) {
return touch("UCJSON/{$username}.json");
}
$arrJSON = json_encode($data, JSON_NUMERIC_CHECK);
if ($arrJSON === false) {
return false;
}
return file_put_contents("UCJSON/{$username}.json", "{$arrJSON}\n");
}
#function CheckUser(): string|int { // PHP 7 不支持联合类型.
function CheckUser() {
if (isset($_COOKIE[AuthCookieName])) {
if (count(($userArr = explode(':', $_COOKIE[AuthCookieName], 2))) === 2 && (empty(AuthUserList[$userArr[0]]['password']) || (!empty($userArr[1]) && AuthUserList[$userArr[0]]['password'] === $userArr[1]))) {
return $userArr[0];
} else {
return -1;
}
}
return -2;
}
function CheckLogin(string $username, string $password): bool {
if (isset(AuthUserList[$username]) && ((empty($password) && empty(AuthUserList[$username]['password'])) || (!empty($password) && AuthUserList[$username]['password'] === sha1($password)))) {
return true;
}
return false;
}
$isPOST = (strtoupper($_SERVER['REQUEST_METHOD']) === 'POST');
$auth = (UseAuth ? false : true);
$username = (UseAuth ? CheckUser() : AnonymousUsername);
$tryLogout = (isset($_GET['logout']) && $_GET['logout'] === '1');
$tryLogin = (UseAuth && !empty($_POST['username']));
if ($tryLogout) {
if (UseAuth) {
setcookie(AuthCookieName, '', time() - 1, '/', '', false, true);
}
header("Location: {$_SERVER['SCRIPT_NAME']}", true, 302);
die();
} else {
if ($username === -1) {
if (!$tryLogin) {
setcookie(AuthCookieName, '', time() - 1, '/', '', false, true);
}
} elseif ($username !== -2) {
$auth = true;
}
}
if ($tryLogin && !$auth) {
if (($auth = CheckLogin($_POST['username'], $_POST['password']))) {
setcookie(AuthCookieName, $_POST['username'] . ':' . AuthUserList[$_POST['username']]['password'], time() + 2592000, '/', '', false, true);
$username = $_POST['username'];
} else {
$loginMessage = '账号/密码不正确, 请检查后再试!';
}
}
$userSessionName = ($auth ? (isset(AuthUserList[$username]) && array_key_exists('sessionName', AuthUserList[$username]) ? AuthUserList[$username]['sessionName'] : SessionName) : null);
$userExpiration = (($userSessionName === null && isset(AuthUserList[$username]) && array_key_exists('expiration', AuthUserList[$username])) ? AuthUserList[$username]['expiration'] : Expiration); // 不允许 Session 模式下自定义过期时间, 以避免不确定行为.
if ($userSessionName !== null) {
ini_set('session.use_cookies', 0);
ini_set('session.use_trans_sid', 1);
ini_set('session.use_only_cookies', 0);
if ($userExpiration > 0) {
ini_set('session.gc_maxlifetime', $userExpiration);
}
if (!is_writable(session_save_path())) {
die('Session path ' . session_save_path() . " is not writable for PHP!\n");
}
session_name($userSessionName);
session_start();
$sessionID = session_id();
if (!isset($_GET[$userSessionName]) || $_GET[$userSessionName] !== $sessionID) {
header("Location: {$_SERVER['SCRIPT_NAME']}?" . $userSessionName . "={$sessionID}", true, 302);
die();
}
} else if (count($_GET) > 0 && !$isPOST) {
header("Location: {$_SERVER['SCRIPT_NAME']}", true, 302);
die();
}
if (!$tryLogin && $isPOST) {
header('Content-Type: application/json');
if (!$auth) {
die(json_encode(array('version' => -2)));
}
$clientJSON = json_decode(file_get_contents('php://input'), true);
$clientVersion = $clientJSON['version'] ?? null;
$clientVersionHash = $clientJSON['version_hash'] ?? null;
$clientVersionHashCalc = (VerifyClientVersionHash ? ((($clientVersionHash !== null) ? sha1(VersionKey . ($auth ? $username : AnonymousUsername) . $clientVersion . VersionKey) : null)) : $clientVersionHash);
if ($clientVersion !== -1) {
if ($clientVersion !== null && $clientVersionHash !== null && $clientVersionHash === $clientVersionHashCalc) {
$clientClipboard = isset($clientJSON['clipboard']) ? ($clientJSON['clipboard'] ?? null) : null;
$serverData = GetData($username);
$serverDataChanged = false;
$clientVersionChanged = false;
} else { // 版本不为 -1 说明可能是篡改或主动重设 VersionKey, 故清空剪切板.
$serverData = array();
}
} else { // 版本为 -1 说明客户端正在初始化, 直接发回内容使客户端获得当前版本.
$serverData = GetData($username);
$serverDataChanged = false;
$clientVersionChanged = true;
}
if (!isset($serverData['version']) || !isset($serverData['clipboard'])) {
$serverData['version'] = 0;
$serverData['clipboard'] = '';
$serverDataChanged = true;
$clientVersionChanged = true;
}
$serverVersionHashCalc = ($clientVersion !== $serverData['version']) ? sha1(VersionKey . ($auth ? $username : AnonymousUsername) . $serverData['version'] . VersionKey) : $clientVersionHashCalc;
if (!$clientVersionChanged) {
if ($clientVersion !== $serverData['version'] || $clientVersionHash !== $serverVersionHashCalc) { // 版本或 Hash 不一致说明客户端在还没有更新内容前就落后或超前, 需要重新发回内容使客户端获得正确版本.
$clientVersionChanged = true;
} elseif ($clientClipboard !== null && $clientClipboard !== $serverData['clipboard']) { // 否则如果剪切板不为 null (允许空) 且内容被修改, 则进行更新.
$serverDataChanged = true;
$clientVersionChanged = true;
if ($serverData['version'] < 23333 && !empty($clientClipboard)) {
$serverData['version']++;
} else {
$serverData['version'] = 0;
}
$serverData['clipboard'] = $clientClipboard;
$serverVersionHashCalc = sha1(VersionKey . ($auth ? $username : AnonymousUsername) . $serverData['version'] . VersionKey);
}
}
// 将处理结果存储并重新发回给客户端, 且如果版本没有发生变化, 就不发送剪切板内容以节省带宽.
SaveData($username, ($serverDataChanged ? $serverData : null));
die(json_encode(array('version' => $serverData['version'], 'version_hash' => $serverVersionHashCalc, 'clipboard' => ($clientVersionChanged ? $serverData['clipboard'] : null))));
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="dark.css">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<script src="https://cdn.bootcdn.net/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>
<script>
function remakeQRCodeByColorScheme() {
if (typeof qrcode !== 'undefined') {
document.getElementById('qrcode').innerHTML = '';
}
l = window.matchMedia('(prefers-color-scheme: light)').matches;
qrcode = new QRCode('qrcode', {'text': location.href, 'width': clientTextarea.scrollHeight, 'height': clientTextarea.scrollHeight, 'colorLight': (l ? '#fff' : '#666'), 'colorDark': '#000', 'correctLevel': QRCode.CorrectLevel.H});
}
function reqListener() {
if (this.responseText == null || this.responseText.length <= 0) {
isRequesting = false;
return;
}
json = JSON.parse(this.responseText);
if (json.version === undefined || json.version_hash === undefined || (json.version !== 0 && json.version < version)) {
if (json.version === -2) {
clearInterval(intervalTimer);
clientStatus.innerText = '状态: 未登录.';
}
} else {
version = json.version;
version_hash = json.version_hash;
clientTextarea.disabled = false;
clientStatus.innerText = `状态: 正常, 客户端版本: ${version} (${version_hash}).`;
if (!isInputting && json.clipboard !== undefined && json.clipboard !== null) {
clientTextarea.value = json.clipboard;
}
}
isRequesting = false;
}
function sync() {
if (isInputting || isRequesting) {
return;
}
isRequesting = true;
req.open('POST', location.href, true);
req.setRequestHeader('Content-Type', 'application/json');
req.send(JSON.stringify({'version': version, 'version_hash': version_hash, 'clipboard': (version !== -1 ? clientTextarea.value : null)}));
}
window.onload = function () {
isInputting = false;
isRequesting = false;
version = -1;
version_hash = null;
clientStatus = document.getElementById('status');
clientTextarea = document.getElementsByTagName('textarea')[0];
if (window.matchMedia) {
remakeQRCodeByColorScheme();
window.matchMedia('(prefers-color-scheme: light)').addEventListener('change', remakeQRCodeByColorScheme);
}
req = new XMLHttpRequest();
req.addEventListener('load', reqListener);
req.timeout = <?php echo Interval; ?>;
clientTextarea.addEventListener('input', function() {
isInputting = true;
if (typeof timeoutTimer !== 'undefined') {
clearTimeout(timeoutTimer);
}
timeoutTimer = setTimeout(function () { isInputting = false; }, <?php echo InputTimeout; ?>);
});
intervalTimer = setInterval(sync, <?php echo Interval; ?>);
}
</script>
<title><?php echo Title; ?></title>
</head>
<body>
<h1><?php echo Title; ?></h1>
<?php
if (!$auth) {
?>
<form method="post" enctype="multipart/form-data">
<div>
<label for="username">账号: </label>
<input type="text" id="username" name="username" required>
<label for="password">密码: </label>
<input type="password" id="password" name="password">
<button>登录</button>
</div>
</form>
<?php
} else {
echo " <p>当前登录账号为: {$username}" . (UseAuth ? ", <a href=\"?logout=1\">登出</a>" : '.') . "</p>\n";
}
if (isset($loginMessage)) {
echo " <p>{$loginMessage}</p>\n";
}
?>
<p id="status">状态: 初始化...</p>
<textarea spellcheck="false" autocomplete="off" style="width: calc(100% - 340px); min-width: 300px; height: 300px;" disabled="disabled"></textarea>
<div id="qrcode" style="display: inline-block; vertical-align: top; height: 300px;"></div>
</body>
</html>