<?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);

        #$domain=parse_url($url)['host'];
        $domain=$url;
        $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{
                $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'=>$url,
            '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){
            if($source->webid==null){
                $ba='block';
            }else{
                $ba='none';
            }
            return view('jump', ['source' => $source,'ba'=>$ba]);
        }elseif($source->type==1){
            return redirect($source->source);
        }
        
    }


}
