class Database {
private $host, $user, $pass, $dbname;
private $pdo;
public function __construct($host, $user, $pass, $dbname) {
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->dbname = $dbname;
$this->connect();
}
private function connect() {
try {
$dsn = "mysql:host={$this->host};dbname={$this->dbname};charset=utf8mb4";
$this->pdo = new PDO($dsn, $this->user, $this->pass, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false
]);
} catch (PDOException $e) {
die("数据库连接失败: " . $e->getMessage());
}
}
public function query($sql, $params = []) {
$stmt = $this->pdo->prepare($sql);
$stmt->execute($params);
return $stmt;
}
public function getPDO() {
return $this->pdo;
}
}class SectigoAPI {
private $loginName, $loginPassword;
public function __construct($loginName, $loginPassword) {
$this->loginName = $loginName;
$this->loginPassword = $loginPassword;
}
// 申请证书
public function applyCertificate($params) {
$params['loginName'] = $this->loginName;
$params['loginPassword'] = $this->loginPassword;
$params['responseFormat'] = 1; // 使用URL编码响应
return $this->sendRequest(AUTO_APPLY_API, $params);
}
// 收集证书
public function collectCertificate($params) {
$params['loginName'] = $this->loginName;
$params['loginPassword'] = $this->loginPassword;
$params['responseFormat'] = 1; // 使用URL编码响应
return $this->sendRequest(COLLECT_SSL_API, $params);
}
// 查询订单状态
public function checkOrderStatus($orderNumber = '', $certificateId = '') {
$params = [
'loginName' => $this->loginName,
'loginPassword' => $this->loginPassword,
'responseFormat' => 1
];
if ($orderNumber) {
$params['orderNumber'] = $orderNumber;
} elseif ($certificateId) {
$params['certificateID'] = $certificateId;
} else {
throw new Exception('订单号或证书ID必须提供一个');
}
return $this->sendRequest(COLLECT_SSL_API, $params);
}
// 发送API请求
private function sendRequest($url, $params) {
$postData = http_build_query($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/x-www-form-urlencoded'
]);
$response = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if ($error) {
throw new Exception("API请求错误: " . $error);
}
parse_str($response, $responseParams);
return $responseParams;
}
}class User {
private $db;
private $sessionKey = 'user_id';
public function __construct($db) {
$this->db = $db;
session_start();
}
public function register($username, $password) {
// 简单加密密码
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
$stmt = $this->db->query(
"INSERT INTO users (username, password) VALUES (:username, :password)",
[':username' => $username, ':password' => $hashedPassword]
);
return $stmt->rowCount() > 0;
}
public function login($username, $password) {
$stmt = $this->db->query(
"SELECT * FROM users WHERE username = :username",
[':username' => $username]
);
$user = $stmt->fetch();
if ($user && password_verify($password, $user['password'])) {
$_SESSION[$this->sessionKey] = $user['id'];
return true;
}
return false;
}
public function logout() {
session_unset();
session_destroy();
}
public function isLoggedIn() {
return isset($_SESSION[$this->sessionKey]);
}
public function getCurrentUserId() {
return $_SESSION[$this->sessionKey] ?? null;
}
}class Certificate {
private $db, $sectigoAPI;
public function __construct($db, $sectigoAPI) {
$this->db = $db;
$this->sectigoAPI = $sectigoAPI;
}
// 申请证书
public function apply($params) {
try {
// 验证参数
$this->validateApplicationParams($params);
// 调用Sectigo API
$response = $this->sectigoAPI->applyCertificate($params);
// 存储证书订单
$this->storeCertificateOrder($params, $response);
return [
'success' => true,
'message' => '证书申请已提交',
'data' => $response
];
} catch (Exception $e) {
return [
'success' => false,
'message' => '证书申请失败: ' . $e->getMessage()
];
}
}
// 验证申请参数
private function validateApplicationParams($params) {
if (empty($params['domain'])) {
throw new Exception('域名是必填项');
}
if (empty($params['csr'])) {
throw new Exception('CSR是必填项');
}
if (empty($params['product_id'])) {
throw new Exception('请选择证书类型');
}
if (empty($params['validation_method'])) {
throw new Exception('请选择验证方式');
}
}
// 存储证书订单
private function storeCertificateOrder($params, $response) {
$userId = (new User($this->db))->getCurrentUserId();
$domain = $params['domain'];
$csr = $params['csr'];
$productId = $params['product_id'];
$validationMethod = $params['validation_method'];
$orderNumber = $response['orderNumber'] ?? '';
$certificateId = $response['certificateID'] ?? '';
$statusCode = $response['errorCode'] ?? -1;
$statusMessage = $response['errorMessage'] ?? '订单处理中';
$stmt = $this->db->query(
"INSERT INTO certificates (user_id, domain, csr, product_id, validation_method, order_number, certificate_id, status_code, status_message, created_at)
VALUES (:user_id, :domain, :csr, :product_id, :validation_method, :order_number, :certificate_id, :status_code, :status_message, NOW())",
[
':user_id' => $userId,
':domain' => $domain,
':csr' => $csr,
':product_id' => $productId,
':validation_method' => $validationMethod,
':order_number' => $orderNumber,
':certificate_id' => $certificateId,
':status_code' => $statusCode,
':status_message' => $statusMessage
]
);
return $stmt->rowCount() > 0;
}
// 获取用户证书列表
public function getCertificatesByUser() {
$userId = (new User($this->db))->getCurrentUserId();
$stmt = $this->db->query(
"SELECT * FROM certificates WHERE user_id = :user_id ORDER BY created_at DESC",
[':user_id' => $userId]
);
return $stmt->fetchAll();
}
// 获取证书详情
public function getCertificateDetails($certificateId) {
$stmt = $this->db->query(
"SELECT * FROM certificates WHERE certificate_id = :certificate_id",
[':certificate_id' => $certificateId]
);
$certificate = $stmt->fetch();
if ($certificate) {
// 从API获取最新状态
try {
$status = $this->sectigoAPI->checkOrderStatus('', $certificateId);
$certificate['api_status'] = $status;
} catch (Exception $e) {
$certificate['api_status_error'] = $e->getMessage();
}
}
return $certificate;
}
// 更新验证方式
public function updateValidationMethod($params) {
$certificateId = $params['certificate_id'] ?? '';
$validationMethod = $params['validation_method'] ?? '';
$dcvEmail = $params['dcv_email'] ?? '';
if (empty($certificateId) || empty($validationMethod)) {
throw new Exception('缺少必要参数');
}
// 更新数据库
$stmt = $this->db->query(
"UPDATE certificates SET validation_method = :validation_method, dcv_email = :dcv_email, updated_at = NOW() WHERE certificate_id = :certificate_id",
[
':certificate_id' => $certificateId,
':validation_method' => $validationMethod,
':dcv_email' => $dcvEmail
]
);
if ($stmt->rowCount() > 0) {
// 调用API更新验证方式
$apiParams = [
'certificateID' => $certificateId,
'dcvEmailAddress' => $dcvEmail,
'dcvMethod' => $this->mapValidationMethod($validationMethod)
];
$this->sectigoAPI->collectCertificate($apiParams);
return true;
}
return false;
}
// 映射验证方式到API参数
private function mapValidationMethod($method) {
$map = [
'dv' => 'EMAIL',
'ov' => 'HTTP_CSR_HASH',
'ev' => 'CNAME_CSR_HASH'
];
return $map[$method] ?? 'EMAIL';
}
// 下载证书
public function downloadCertificate($certificateId) {
if (empty($certificateId)) {
throw new Exception('证书ID不能为空');
}
$certificate = $this->getCertificateDetails($certificateId);
if (!$certificate) {
throw new Exception('未找到该证书');
}
try {
$params = [
'certificateID' => $certificateId,
'queryType' => 1, // 返回状态和证书数据
'responseType' => 0, // ZIP归档
'responseEncoding' => 0, // BASE64编码
'showValidityPeriod' => 'Y',
'showFQDN' => 'Y'
];
$response = $this->sectigoAPI->collectCertificate($params);
if (isset($response['errorCode']) && $response['errorCode'] < 0) {
throw new Exception('证书下载失败: ' . ($response['errorMessage'] ?? '未知错误'));
}
if (isset($response['netscapeCertificateSequence'])) {
$certData = base64_decode($response['netscapeCertificateSequence']);
$this->sendDownloadHeaders('certificate_bundle.zip', 'application/zip', strlen($certData));
echo $certData;
exit;
}
throw new Exception('未找到证书数据');
} catch (Exception $e) {
header('Location: index.php?action=manage&error=' . urlencode($e->getMessage()));
exit;
}
}
// 发送下载头信息
private function sendDownloadHeaders($fileName, $mimeType, $fileSize) {
header("Content-Type: $mimeType");
header("Content-Disposition: attachment; filename=\"$fileName\"");
header("Content-Length: $fileSize");
header("Cache-Control: max-age=0");
}
}
Fatal error: Uncaught Error: Class "Database" not found in /www/wwwroot/new.allssl.cn/index.php:23
Stack trace:
#0 {main}
thrown in /www/wwwroot/new.allssl.cn/index.php on line 23