first commit

This commit is contained in:
2026-04-14 09:47:00 +08:00
commit 56775e85f3
17 changed files with 1369 additions and 0 deletions
+61
View File
@@ -0,0 +1,61 @@
<?php
declare(strict_types=1);
function sendcard_get_pdo(string $sqlitePath): PDO
{
$dir = dirname($sqlitePath);
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
}
$pdo = new PDO('sqlite:' . $sqlitePath, null, null, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]);
return $pdo;
}
function sendcard_init_db(PDO $pdo): void
{
// 备档表:每次回调把外部接口 data 全量字段写入(token 不会返回给前端以外,只会返回给请求端)
$pdo->exec(
'CREATE TABLE IF NOT EXISTS cursor_login_backups (
id INTEGER PRIMARY KEY AUTOINCREMENT,
fetched_at TEXT DEFAULT CURRENT_TIMESTAMP,
external_msg TEXT,
external_code TEXT,
-- external data fields
external_id INTEGER,
email TEXT,
token TEXT,
createTime TEXT,
lastTokenTime TEXT,
status INTEGER,
deviceCode TEXT,
activationCode TEXT,
useTime TEXT,
lastId INTEGER,
deleted INTEGER,
emailLastStatus INTEGER,
useCount INTEGER,
pwd TEXT,
type INTEGER,
updateTime TEXT,
banName TEXT,
webToken TEXT,
cpName TEXT,
comeStatus INTEGER,
comePushTime TEXT,
freeSevenStatus INTEGER,
windsurfUseStatus INTEGER,
windsurfStatus INTEGER,
windsurfToken TEXT,
windsurfPwd TEXT,
windsurfUseTime TEXT,
raw_json TEXT
)'
);
}
+47
View File
@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
function sendcard_fetch_credentials(string $url, int $timeoutSeconds = 15): array
{
// 使用 cURL 取外部接口(外部接口返回 JSON)
$ch = curl_init();
if ($ch === false) {
throw new RuntimeException('curl_init failed');
}
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_CONNECTTIMEOUT => min(10, $timeoutSeconds),
CURLOPT_TIMEOUT => $timeoutSeconds,
CURLOPT_HTTPHEADER => [
'Accept: application/json',
'User-Agent: sendcard-php/1.0',
],
// 外部是 http,通常不需要证书校验;若换成 https,可再调整。
]);
$body = curl_exec($ch);
if ($body === false) {
$err = curl_error($ch);
curl_close($ch);
throw new RuntimeException('External request failed: ' . $err);
}
$statusCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$json = json_decode($body, true);
if (!is_array($json)) {
throw new RuntimeException('External response is not valid JSON');
}
// 返回:包含 HTTP 状态码与解析后的 JSON
return [
'http_status' => $statusCode,
'json' => $json,
];
}