<?php 

function kanglehost_CreateSign($a, $r)
{
    return md5($a . "通信密钥" . $r);
}

function kanglehost_GetUrl($info, $skey, $r)
{
    $url = "";
    foreach ($info as $k => $v) {
        $url .= $k . "=" . $v . "&";
    }
    return "http://185.216.117.230:3312/api/index.php?" . $url . "r=" . $r . "&s=" . $skey . "&json=1";
}


function open_products($data,$post) {
    $a = "add_vh";
    $r = rand(100000, 999999);

    $user = onlystr(10); // 随机生成用户名
    $pass = onlystr(15); // 随机生成密码

    $currentDate = date('Y-m-d'); // 获取当前日期
    $futureDate = date('Y-m-d', strtotime($currentDate . ' +30 days')); // 计算30天后的日期

    $info = [
        "c" => "whm",
        "a" => $a,
        "init" => 1,
        "name" => $user,
        "passwd" => $pass,
        "product_id" => $data["ptid"]
    ];

    $skey = kanglehost_CreateSign($a, $r); // 修正函数名称大小写
    $url = kanglehost_GetUrl($info, $skey, $r); // 修正函数名称大小写
    $re = @file_get_contents($url);
    $re = json_decode($re, true);

    if (!isset($re['result'])) {
        $code = 100; // 错误处理：如果result不存在，设为100
    } else {
        $code = $re['result'];
    }

    $data['dqtime'] = $futureDate;
    $data['username'] = $user;
    $data['password'] = $pass;

    if ($code == 200) {
        $data['mid'] = onlystr(8);
        $data['msg'] = $re;
        $data['code'] = 200;
        $data['status'] = '已激活';
    } else {
        $data['mid'] = onlystr(8);
        $data['msg'] = $re;
        $data['code'] = 100;
        $data['status'] = '待开通';
    }

    return $data; 
}

function show_product($data) {
    $templatePath = './mods/hk_ep/index.html'; // 模板路径
    $variables = $data;

    return h_t($templatePath, $variables);
}

function long_product($data) {
    $date = new DateTime($data['dqtime']);
    $date->modify('+' . $data['Today'] . ' days'); // 假设'Today'是$data数组中的一个键

    $data['dqtime'] = $date->format('Y-m-d');

    
    return $data;
}




function stop_product($data) {
    
    
        $data['status'] = '已到期';
        
            $a = "del_vh";
    $r = rand(100000, 999999);
                $info = ["c" => "whm", "a" => $a, "name" => $data['username']];
        
        
            $skey = kanglehost_CreateSign($a, $r); // 修正函数名称大小写
    $url = kanglehost_GetUrl($info, $skey, $r); // 修正函数名称大小写
    $re = @file_get_contents($url);
    $data['msg'] = $re;
    $re = json_decode($re, true);

        $data['code'] = $re["result"];
        
        
    return $data;
}



function ready_products($data) {
$templatePath = './mods/hk_ep/ready.html'; // 模板路径
    $variables = $data;
    $variables["id"] = $_GET["id"];
    return h_t($templatePath, $variables);
}