145 lines
4.8 KiB
PHP
145 lines
4.8 KiB
PHP
<?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);
|
|
|
|
if(str_starts_with($url, 'http://')||str_starts_with($url, 'https://')){
|
|
$domain=$url;
|
|
}else{
|
|
$domain='http://'.$url;
|
|
}
|
|
$domain_name=parse_url($domain)['host'];
|
|
|
|
|
|
$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';
|
|
}else{
|
|
$icp=null;
|
|
if($user==null){
|
|
$owner='anonymous';
|
|
}else{
|
|
$owner=$realname;
|
|
}
|
|
$webid=null;
|
|
}
|
|
}
|
|
}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,
|
|
'source'=>$domain,
|
|
'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){
|
|
$etag='W/"'.md5($source->source).'"';
|
|
$if_none_match = $request->header('If-None-Match',null);
|
|
if($if_none_match==$etag){
|
|
return response('',304);
|
|
}
|
|
if($source->webid==null){
|
|
$ba='block';
|
|
}else{
|
|
$ba='none';
|
|
}
|
|
|
|
return view('jump', ['source' => $source,'ba'=>$ba])->header('ETag', $etag);
|
|
}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');
|
|
}
|
|
|
|
}
|