Browse Source

feat: QR code support of cli

master
dnomd343 3 years ago
parent
commit
739cd5e005
  1. 1717
      backend/qrcode.php
  2. 89
      backend/queryInfo.php

1717
backend/qrcode.php

File diff suppressed because it is too large

89
backend/queryInfo.php

@ -1,5 +1,6 @@
<?php
include("qrcode.php");
include("getInfo.php");
function getClientIP() { // 获取客户端IP
@ -10,6 +11,51 @@ function formatDate($str) { // 将YYYYMMDD处理为YYYY-MM-DD
return substr($str, 0, 4) . '-' . substr($str, 4, 2) . '-' . substr($str, 6, 2);
}
function getQrCode($str, $block) {
$qrString = '';
$qr = QRCode::getMinimumQRCode($str, QR_ERROR_CORRECT_LEVEL_L);
for ($y = 0; $y < $qr->getModuleCount(); $y++) {
for ($x = 0; $x < $qr->getModuleCount(); $x++) {
$qrString .= ($qr->isDark($y, $x) ? $block : ' ');
}
$qrString .= PHP_EOL;
}
return $qrString;
}
function getQrCodeUtf($str) {
$qr = QRCode::getMinimumQRCode($str, QR_ERROR_CORRECT_LEVEL_L);
$length = $qr->getModuleCount();
for ($y = 0; $y < $length; $y++) {
for ($x = 0; $x < $length; $x++) {
$table[$y][$x] = $qr->isDark($y, $x);
}
if ($length % 2) {
$table[$y][$length] = false;
}
}
if ($length % 2) {
for ($i = 0; $i <= $length; $i++) {
$table[$length][$i] = false;
}
$length++;
}
for ($y = 0; $y < $length; $y += 2) {
for ($x = 0; $x < $length; $x++) {
if ($table[$y][$x] && $table[$y + 1][$x]) {
echo '█';
} else if ($table[$y][$x] && !$table[$y + 1][$x]) {
echo '▀';
} else if (!$table[$y][$x] && $table[$y + 1][$x]) {
echo '▄';
} else {
echo ' ';
}
}
echo PHP_EOL;
}
}
function preRount() { // 解析请求路径
global $request;
$requestUri = $_SERVER['DOCUMENT_URI'];
@ -22,6 +68,13 @@ function preRount() { // 解析请求路径
} else if ($requestUri == '/version') { // URI -> /version
$request['version'] = true;
return;
} else if ($requestUri == '/qr') { // URI -> /qr
$request['qr'] = true;
return;
} else if ($requestUri == '/qr/') { // URI -> /qr/
$request['qr'] = true;
$request['qr_fill'] = '██';
return;
} else if ($requestUri == '/info' || $requestUri == '/info/') { // URI -> /info or /info/
$request['ip'] = getClientIP();
return;
@ -37,6 +90,12 @@ function preRount() { // 解析请求路径
if (isset($_GET['ip'])) { $request['ip'] = $_GET['ip']; }
return;
}
preg_match('#^/qr/([^/]{2})$#', $requestUri, $match); // URI -> /qr/{qr_fill}
if (count($match) > 0) {
$request['qr'] = true;
$request['qr_fill'] = $match[1];
return;
}
preg_match('#^/([^/]+?)$#', $requestUri, $match); // URI -> /{ip}
if (count($match) > 0) {
if ($request['cli']) { // 命令行模式
@ -71,10 +130,13 @@ function routeParam() {
// version -> 获取版本数据
// cli -> 来自命令行下的请求
// gbk -> 返回数据使用GBK编码
// qr -> 生成二维码
// qr_fill -> 二维码填充符号
// justip -> 仅查询IP地址
// ip -> 请求指定IP的数据
global $request;
global $webUri;
if ($request['error']) { // 请求出错
if ($request['cli']) { // 命令行模式
echo 'Illegal Request' . PHP_EOL;
@ -98,6 +160,21 @@ function routeParam() {
exit; // 退出
}
if ($request['qr']) { // 生成二维码
if (!$request['cli']) { // 网页模式不输出
header('HTTP/1.1 302 Moved Temporarily');
header('Location: /error');
} else {
echo $webUri . PHP_EOL;
if (isset($request['qr_fill'])) { // 使用字符填充生成二维码
echo getQrCode($webUri . '?ip=' . getClientIP(), $request['qr_fill']);
} else { // 使用特殊Unicode字符生成二维码
echo getQrCodeUtf($webUri . '?ip=' . getClientIP());
}
}
exit;
}
if ($request['justip']) { // 仅查询IP地址
if ($request['cli']) { // 命令行模式
echo getClientIP() . PHP_EOL;
@ -124,7 +201,6 @@ function routeParam() {
}
$info = getIPInfo($ip); // 查询目标IP
if ($request['gbk']) {
echo 'ijijjiasdjflsdajflsdavasldvmas';
$info = iconv('UTF-8', 'gbk', $info);
}
echo $info;
@ -136,14 +212,25 @@ function main() {
}
$myVersion = 'v1.2';
$request = array(
'error' => false,
'version' => false,
'cli' => false,
'gbk' => false,
'qr' => false,
'justip' => false
);
$webUri = 'ip.343.re';
if (isset($_SERVER['HTTP_HOST'])) {
preg_match('#^127.0.0.1#', $_SERVER['HTTP_HOST'], $match); // 排除127.0.0.1下的host
if (count($match) == 0) {
$webUri = $_SERVER['HTTP_HOST'];
}
}
$webUri = 'http://' . $webUri . '/';
main();
?>
Loading…
Cancel
Save