spqr/app/controller/Server.php

145 lines
4.8 KiB
PHP
Raw Normal View History

2024-11-05 12:10:06 +08:00
<?php
namespace app\controller;
use support\Request;
use yzh52521\EasyHttp\Response;
use yzh52521\EasyHttp\RequestException;
use yzh52521\EasyHttp\Http;
use support\Db;
class Server
{
public function short(Request $request)
{
$session = $request->session();
$url = $request->input('url',null);
$link = $request->input('link',null);
$type = $request->input('type','false');
if($link==null && Db::table('links')->where('source', "$url")->where('type',0)->exists() ){
$link=Db::table('links')->where('source', "$url")->where('type',0)->value('link');
return json(['code' => 200, 'msg' => 'success','link'=>'https://SPQR.top/'.$link]);
}
$user=$session->get('user',null);
if($user==null&&($type!='false'||$link!=null)){
return json(['code' => 505, 'msg' => '302直链和自定义链接后缀需要登陆后才可使用']);
}
$realname=$session->get('realname',null);
2024-11-07 21:44:19 +08:00
if(str_starts_with($url, 'http://')||str_starts_with($url, 'https://')){
$domain=$url;
}else{
$domain='http://'.$url;
}
$domain_name=parse_url($domain)['host'];
2024-11-05 12:10:06 +08:00
$response = Http::post('https://api.uutool.cn/beian/icp/', ['site' => "$domain"])->json();
if(isset($response->data->is_icp)){
if($response->data->is_icp==1){
$icp=$response->data;
$owner=$icp->icp_org;
$webid=$icp->icp_no;
}else{
if(str_ends_with($domain_name, '.gov.cn')){
$icp='GOV';
$owner='政府机构';
$webid='GOVERNMENT_WEBSITE';
2024-11-05 12:10:06 +08:00
}else{
$icp=null;
if($user==null){
$owner='anonymous';
}else{
$owner=$realname;
}
$webid=null;
2024-11-05 12:10:06 +08:00
}
}
}else{
return json(['code' => 405, 'msg' => '备案查询接口响应异常,请稍后重试']);
}
if($icp==null&&($user==null||$type!='false')){
return json(['code' => 502, 'msg' => '该域名未备案,需要登陆后才可缩短,且不支持302直链']);
}
if($link==null){
for($i=4;$i>0;$i++){
$link=substr(md5($url).rand(0,pow(10,$i)),0,$i);
if(Db::table('links')->where('link', "$link")->exists()){
continue;
}else{
break;
}
}
}else{
if(Db::table('links')->where('link', "$link")->exists()){
return json(['code' => 408, 'msg' => '自定义链接已存在']);
}
}
if($user==null){
$user='anonymous';
}
if($type=='false'){
$type=0;
}elseif($type=='true'){
$type=1;
}
$data=[
'link'=>$link,
2024-11-07 22:01:12 +08:00
'source'=>$domain,
2024-11-05 12:10:06 +08:00
'owner'=>$owner,
'webid'=>$webid,
'user'=>$user,
'type'=>$type,
'time'=>time()
];
Db::table('links')->insert($data);
return json(['code' => 200, 'msg' => 'success','link'=>'https://SPQR.top/'.$link]);
}
public function view(Request $request,$link)
{
$source=Db::table('links')->where('link', "$link")->first();
if($source==null){
return redirect('/');
}
if($source->type==0){
2024-11-08 00:09:27 +08:00
$etag='W/"'.md5($source->source).'"';
2024-11-07 23:29:13 +08:00
$if_none_match = $request->header('If-None-Match',null);
if($if_none_match==$etag){
return response('',304);
}
2024-11-05 12:10:06 +08:00
if($source->webid==null){
$ba='block';
}else{
$ba='none';
}
2024-11-07 23:29:13 +08:00
return view('jump', ['source' => $source,'ba'=>$ba])->header('ETag', $etag);
2024-11-05 12:10:06 +08:00
}elseif($source->type==1){
return redirect($source->source);
}
}
public function delete(Request $request,$link)
{
$session = $request->session();
$user=$session->get('user',null);
if($user==null){
return json(['code' => 505, 'msg' => '需要登陆后才可使用']);
}
if($link==null){
return json(['code' => 404, 'msg' => '缺少参数']);
}
$source=Db::table('links')->where('link', "$link")->first();
if($source==null){
return json(['code' => 404, 'msg' => '链接不存在']);
}
if($source->user!=$user){
return json(['code' => 403, 'msg' => '无权删除']);
}
Db::table('links')->where('link', "$link")->delete();
return redirect('/!/my');
}
2024-11-05 12:10:06 +08:00
}