<?php
// ================= 授权系统接入 =================
// 授权服务器地址写死,不要用 $_SERVER['HTTP_HOST'](那是你自己的域名)
define('LICENSE_API', 'https://sq.sgkm.cn/api');
// 你的卡密,激活成功后可以删掉这一行
define('LICENSE_CARD', 'EYE-XXXX-XXXX-XXXX-XXXX');
function get_machine_id() {
foreach (array('/etc/machine-id', '/var/lib/dbus/machine-id') as $p) {
if (@is_readable($p)) {
$v = trim(@file_get_contents($p));
if ($v !== '') { return strtolower(preg_replace('/[^0-9A-Za-z]/', '', $v)); }
}
}
if (stripos(PHP_OS, 'WIN') === 0) {
// wmic 在新版 Windows 已移除,取不到时退回 PowerShell
$cmds = array(
'wmic csproduct get uuid 2>NUL',
'powershell -NoProfile -Command "(Get-CimInstance Win32_ComputerSystemProduct).UUID" 2>NUL',
);
foreach ($cmds as $cmd) {
$v = @shell_exec($cmd);
if (!$v) { continue; }
$v = strtolower(preg_replace('/[^0-9A-Za-z]/', '', str_ireplace('UUID', '', $v)));
if (strlen($v) >= 8) { return $v; }
}
}
return strtolower(md5(php_uname('n') . '|' . php_uname('m')));
}
function license_post($path, $data) {
$ch = curl_init(LICENSE_API . $path);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // 必须设超时,否则授权服务器故障会拖死你的站
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
$raw = curl_exec($ch);
curl_close($ch);
$json = json_decode((string) $raw, true);
return is_array($json) ? $json : array('authorized' => false, 'message' => '授权服务器无响应');
}
// 首次激活(只调用一次,成功后可以删掉这段)
function license_activate($code, $domain, $allowed_ip, $machine_id) {
return license_post('/auth_apply.php', array(
'auth_code' => $code,
'domain_or_ip' => $domain,
'allowed_ip' => $allowed_ip,
'machine_id' => $machine_id,
));
}
// 授权校验
function license_check($domain, $machine_id) {
return license_post('/auth_check.php', array(
'domain_or_ip' => $domain,
'machine_id' => $machine_id,
));
}
// ================= 使用 =================
$domain = $_SERVER['SERVER_NAME'];
$machine_id = get_machine_id();
// 第一次部署时解除下面两行注释,激活一次即可(把出口 IP 换成你服务器的)
// $r = license_activate(LICENSE_CARD, $domain, '1.2.3.4', $machine_id);
// if ($r['status'] !== 'success') { echo '激活失败:' . $r['message']; exit; }
$r = license_check($domain, $machine_id);
if (empty($r['authorized'])) {
echo '未授权使用!请联系管理员获取授权。';
exit;
}