<?php
/*
 * @Descripttion: 
 * @version: 
 * @Author: kangkang
 * @Date: 2020-11-07 11:13:08
 * @LastEditors: “wanyongkang” “937888580@qq.com”
 * @LastEditTime: 2024-03-29 16:18:50
 */
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Methods: *");
header('Access-Control-Allow-Headers:*');
header("Access-Control-Allow-Credentials: true");
$search = $_GET['info'];
if($_GET['type'] == 1){
    search_all_product(__DIR__.'/data/download/all.csv',$search);
} else {
    $product_id = $_GET['productid'];
    switch($product_id){
        case 0:
            search_all_product(__DIR__.'/data/download/all.csv',$search);
        break;
        case 8:
            search_product(__DIR__.'/data/download/xingxing.csv','xxip.vip');
        break;
        case 6:
            search_product(__DIR__.'/data/download/xianfeng.csv','xfip.vip');
        break;
        case 13:
            search_product(__DIR__.'/data/download/jike.csv','jkip.com');
        break;
        case 1:
            search_product(__DIR__.'/data/download/qiangzi.csv','qzip.vip');
        break;
        case 2:
            search_product(__DIR__.'/data/download/xunlian.csv','xlip.vip');
        break;
        case 14:
            search_product(__DIR__.'/data/download/tiantian.csv','ttip.cn');
        break;
        case 5:
            search_product(__DIR__.'/data/download/laoying.csv','lyip.vip');
        break;
        case 4:
            search_product(__DIR__.'/data/download/jinrui.csv','jrip.vip');
        break;
        case 10:
            search_product(__DIR__.'/data/download/wuxian.csv','wxip.net');
        break;
        case 16:
            search_product(__DIR__.'/data/download/shihui.csv','shihui');
        break;
        case 15:
            search_product(__DIR__.'/data/download/gongxiang.csv','shihui');
        break;
        case 17:
            search_product(__DIR__.'/data/download/xigua.csv','xgip.vip');
        break;
        case 18:
            search_product(__DIR__.'/data/download/mogu.csv','mgip.net');
        break;
        case 29:
            search_product(__DIR__.'/data/download/jihu.csv','jhip.net');
        break;
        case 3:
            search_product(__DIR__.'/data/download/jiguang.csv','jgip.cc');
        break;
        case 19:
            search_product(__DIR__.'/data/download/huohu.csv','hhip.vip');
        break;
        case 22:
            search_product(__DIR__ . '/data/download/huohu.csv','hhip.vip');
            break;
        
        case 23:
            search_product(__DIR__ . '/data/download/yunniu.csv','ynip.net');
        break;
        case 25:
            search_product(__DIR__ . '/data/download/qiangzijt.csv','qzip.vip');
        break;
        case 26:
            search_product(__DIR__ . '/data/download/liebao.csv','lbip.cc');
        break;
        case 27:
            search_product(__DIR__ . '/data/download/qilin.csv','qlip.cc');
        break;
        case 28:
            search_shuidi_product(__DIR__ . '/data/download/shuidi.csv','sdip.cc');
        break;
    }
}

function search_all_product($file_path,$search){
    $list = [];
    $dns = '';
    $file = fopen($file_path,'r'); 
    $province = '';
    $record = [];
    while ($info = fgetcsv($file)) { 

        if(!empty($info[7])){
            $info[10] = $province;
        } else {
            $province = $info[1];
        }

        $search_info = implode('------',$info).$province;
        if(strpos($search_info,'混播')||strpos($search_info,'混拨')){
            $search_info .= '混播混拨';
        }

        if(strpos($search_info,$search) !== false){
            $data['name'] = $info[0];
            switch($data['name']){
                case '星星':
                    $dns = 'xxip.vip';
                break;
                case '先锋':
                    $dns = 'xfip.vip';
                break;
                case '极客':
                    $dns = 'jkip.com';
                break;
                case '强子':
                    $dns = 'qzip.vip';
                break;
                case '迅连':
                    $dns = 'xlip.vip';
                break;
                case '天天':
                    $dns = 'ttip.cn';
                break;
                case '老鹰':
                    $dns = 'lyip.vip';
                break;
                case '金瑞':
                    $dns = 'jrip.vip';
                break;
                case '无限':
                    $dns = 'wxip.net';
                break;
                case '实惠':
                    $dns = 'shihui';
                break;
                case '共享':
                    $dns = 'shihui';
                break;
                case '西瓜':
                    $dns = 'xgip.vip';
                    break;
                case '蘑菇':
                    $dns = 'mgip.net';
                    break;
                case '极狐':
                    $dns = 'jhip.net';
                    break;
                case '极光':
                    $dns = 'jgip.cc';
                    break;
                case '火狐静态':
                    $dns = 'hhip.vip';
                    break;
                
                case '云牛':
                    $dns = 'ynip.net';
                    break;
                
                case '强子静态':
                    $dns = 'qzip.vip';
                    break;
                    
                case '猎豹':
                    $dns = 'lbip.cc';
                    break;
                case '麒麟':
                    $dns = 'qlip.cc';
                    break;
                
                case '水滴静态':
                    $dns = 'sdip.cc';
                    break;
            }
            
            $data['city'] = $info[1];
            $data['supply'] = $info[2];
            $data['ip'] = $info[3];
            $data['daikuan'] = $info[4];
            $data['onlineuser'] = $info[5];
            $data['maxuser'] = $info[6];
            $data['online'] = $info[7];
            $data['status'] = $info[8];
                $data['nasname'] = $info[9];
            $list[] = $data;
        }
        
    }
    fclose($file); 
    echo json_encode(['data'=>$list]);
}


function search_product($file_path,$dns = ''){
    global $search;
    $list = [];
    $file = fopen($file_path,'r'); 
    $province = '';
    $record = [];
    while ($info = fgetcsv($file)) { 
        if(!empty($info[9])){
            $info[10] = $province;
        } else {
            $province = $info[1];
        }

        $search_info = implode('------',$info).$province;
        if(strpos($search_info,'混播')||strpos($search_info,'混拨')){
            $search_info .= '混播混拨';
        }

        if(strpos($search_info,$search) !== false){
            $data['name'] = $info[0];
            $data['city'] = $info[1];
            $data['supply'] = $info[2];
            $data['ip'] = $info[3];
            $data['daikuan'] = $info[4];
            $data['onlineuser'] = $info[5];
            $data['maxuser'] = $info[6];
            $data['online'] = $info[7];
            $data['status'] = $info[8];
            $ip = explode('.',$info[9]);
            if (count($ip) == 4) {
                $data['nasname'] = $info[9];
            } else {
                $data['nasname'] = $ip[0]?ip_repeat($record,$ip[0]).'.'.$dns:'';
            }
            $list[] = $data;
        }
    }
    fclose($file); 
    echo json_encode(['data'=>$list]);
}


function search_shuidi_product($file_path,$dns = ''){
    global $search;
    $list = [];
    $file = fopen($file_path,'r'); 
    $province = '';
    $record = [];
    while ($info = fgetcsv($file)) { 
        if(!empty($info[9])){
            $info[10] = $province;
        } else {
            $province = $info[1];
        }

        $search_info = implode('------',$info).$province;
        if(strpos($search_info,'混播')||strpos($search_info,'混拨')){
            $search_info .= '混播混拨';
        }

        if(strpos($search_info,$search) !== false){
            $data['name'] = $info[0];
            $data['city'] = $info[1];
            $data['supply'] = $info[2];
            $data['ip'] = $info[3];
            $data['daikuan'] = $info[4];
            $data['onlineuser'] = $info[5];
            $data['maxuser'] = $info[6];
            $data['online'] = $info[7];
            $data['status'] = $info[8];
            $ip = explode('.',$info[9]);
            if (count($ip) == 4) {
                $data['nasname'] = $info[9];
            } else {
                $data['nasname'] = $info[9]??'';
            }
            $list[] = $data;
        }
    }
    fclose($file); 
    echo json_encode(['data'=>$list]);
}
function search_xingxing($file_path1,$file_path2,$dns){
    global $search;
    $list1 = [];
    $list2 = [];
    $file1 = fopen($file_path1,'r'); 
    $file2 = fopen($file_path2,'r'); 
    $province = '';
    $record = [];
    while ($info = fgetcsv($file1)) { 
        if(isset($info[7])){
            $info[10] = $province;
        } else {
            $province = $info[1];
        }

        $search_info = implode('',$info);
        if(strpos($search_info,'混播')||strpos($search_info,'混拨')){
            $search_info .= '混播混拨';
        }

        if(strpos($search_info,$search) !== false){
            $data['name'] = $info[0];
            $data['city'] = $info[1];
            $data['supply'] = $info[2];
            $data['ip'] = $info[3];
            $data['daikuan'] = $info[4];
            $data['onlineuser'] = $info[5];
            $data['maxuser'] = $info[6];
            $data['online'] = $info[7];
            $data['status'] = $info[8];
            $ip = explode('.',$info[9]);
            $data['nasname'] = $ip[0]?ip_repeat($record,$ip[0]).'.'.$dns:'';
            $list1[] = $data;
        }
    }
    while ($info = fgetcsv($file2)) { 
        if(isset($info[7])){
            $info[10] = $province;
        } else {
            $province = $info[1];
        }

        $search_info = implode('',$info);
        if(strpos($search_info,'混播')||strpos($search_info,'混拨')){
            $search_info .= '混播混拨';
        }

        if(strpos($search_info,$search) !== false){
            $data['name'] = $info[0];
            $data['city'] = $info[1];
            $data['supply'] = $info[2];
            $data['ip'] = $info[3];
            $data['daikuan'] = $info[4];
            $data['onlineuser'] = $info[5];
            $data['maxuser'] = $info[6];
            $data['online'] = $info[7];
            $data['status'] = $info[8];
            $ip = explode('.',$info[9]);
            $data['nasname'] = $ip[0]?ip_repeat($record,$ip[0]).'.'.$dns:'';
            $list2[] = $data;
        }
    }

    fclose($file1); 
    fclose($file2); 
    $list = array_merge($list1,$list2);
    echo json_encode(['data'=>$list]);
}

function ip_repeat(&$record, $ip, $dns ='')
{
    for ($i = 2; $i < 100; $i++) {
        if (in_array($ip.$dns, $record)) {
            if (in_array($ip .$dns. 'v' . $i, $record)) {
                continue;
            } else {
                $record[] = $ip .$dns. 'v' . $i;
                return $ip . 'v' . $i;
            }
        } else {
            $record[] = $ip.$dns;
            return $ip;
        }
    }
}