diff --git a/.env.example b/.env.example index 4d833f1..e62f315 100644 --- a/.env.example +++ b/.env.example @@ -7,3 +7,6 @@ REDIS_HOST=127.0.0.1 REDIS_PORT=6379 REDIS_PASSWD= REDIS_PREFIX=tgbot + +# ChinaZ API +ICP_KEY= diff --git a/cmdRoute.php b/cmdRoute.php index 6681649..a4438c4 100644 --- a/cmdRoute.php +++ b/cmdRoute.php @@ -5,6 +5,7 @@ require_once 'models/ipInfo.php'; require_once 'models/cfopPic.php'; require_once 'models/kmsCheck.php'; require_once 'models/ntpCheck.php'; +require_once 'models/icpQuery.php'; require_once 'models/punycode.php'; require_once 'models/whoisQuery.php'; @@ -13,6 +14,7 @@ $cmds = array( // 命令列表 'dc', 'kms', 'ntp', + 'icp', 'cfop', 'whois', 'punycode' @@ -32,6 +34,9 @@ function route($cmd, $rawParam) { // 命令请求路由 case 'ntp': $entry = new ntpCheckEntry; break; + case 'icp': + $entry = new icpQueryEntry; + break; case 'cfop': $entry = new cfopPicEntry; break; diff --git a/models/icpQuery.php b/models/icpQuery.php new file mode 100644 index 0000000..4e478a0 --- /dev/null +++ b/models/icpQuery.php @@ -0,0 +1,123 @@ +apiPath .= '?key=' . $env['ICP_KEY'] . '&domain='; + } + + private function getIcpInfo($domain) { // ICP备案查询 + $info = json_decode(file_get_contents($this->apiPath . $domain), true); + if ($info['StateCode'] === 1) { // 存在ICP备案 + $info = array( + 'status' => 'ok', + 'hasIcp' => true + ) + $info['Result']; + } else if ($info['StateCode'] === 0) { // 无ICP备案 + $info = array( + 'status' => 'ok', + 'hasIcp' => false, + 'icpMsg' => $info['Reason'] + ); + } else { + $info = array( + 'status' => 'error', // 服务错误 + 'message' => 'Server error' + ); + } + return $info; + } + + public function icpInfo($domain) { // ICP查询入口 带缓存 + $redis = new redisCache('icp'); + $info = $redis->getData($domain); // 查询缓存数据 + if (!$info) { // 缓存未命中 + $info = $this->getIcpInfo($domain); // 执行查询 + if ($info['status'] !== 'ok') { // 查询错误 + return $info; + } + unset($info['status']); + $redis->setData($domain, json_encode($info), 30 * 86400); // 缓存30day + } else { // 缓存命中 + $info = json_decode($info, true); // 使用缓存数据 + } + return array( + 'status' => 'ok' + ) + $info; + } +} + +class icpQueryEntry { + private function check($str) { // 检查输入参数 + $content = (new extractDomain)->analyse($str); + if (!isset($content['domain'])) { // 格式错误 + return array( + 'status' => 'error', + 'message' => 'Illegal Request' + ); + } + if (!isset($content['tld'])) { // 未知TLD + return array( + 'status' => 'error', + 'message' => 'Unknow TLD' + ); + } + return array( + 'status' => 'ok', + 'domain' => $content['site'] // 返回主域名 + ); + } + + public function query($rawParam) { // ICP备案查询入口 + if ($rawParam == '' || $rawParam === 'help') { // 显示使用说明 + tgApi::sendMessage(array( + 'parse_mode' => 'Markdown', + 'text' => '*Usage:* `/icp domain`', + )); + return; + } + $content = $this->check($rawParam); + if ($content['status'] !== 'ok') { // 请求参数错误 + tgApi::sendText($content['message']); + return; + } + $domain = $content['domain']; + $info = (new icpQuery)->icpInfo($domain); // 发起查询 + if ($info['status'] !== 'ok') { + tgApi::sendText($info['message']); // 查询出错 + return; + } + if (!$info['hasIcp']) { // 没有ICP备案 + tgApi::sendMessage(array( + 'parse_mode' => 'Markdown', + 'text' => '`' . $domain . '`' . PHP_EOL . $info['icpMsg'] + )); + return; + } + $msg = '`' . $domain . '`' . PHP_EOL; + $msg .= '*类型:*' . $info['CompanyType'] . PHP_EOL; + if ($info['Owner'] != '') { // 负责人不为空 + $msg .= '*负责人:*' . $info['Owner'] . PHP_EOL; + } + if ($info['Owner'] != $info['CompanyName']) { // 名称与负责人不重复 + $msg .= '*名称:*' . $info['CompanyName'] . PHP_EOL; + } + $msg .= '*主页:*'; + $pages = explode(';', $info['MainPage']); + foreach ($pages as $page) { // 可能存在多个主页 逐个输出 + $msg .= '`' . $page . '` '; + } + $msg .= PHP_EOL; + $msg .= '*网站名:*' . $info['SiteName'] . PHP_EOL; + $msg .= '*审核时间:*' . $info['VerifyTime'] . PHP_EOL; + $msg .= '*许可证号:*' . $info['SiteLicense'] . PHP_EOL; + tgApi::sendMessage(array( // 返回查询数据 + 'parse_mode' => 'Markdown', + 'text' => $msg + )); + } +} + +?>