<?php
if (file_exists(__DIR__."/core.php")) {
    require_once(__DIR__.'/core.php');
}
else {
    die("<p align=center><b style='color:red'>Fatal ErrorV1 : </b>Application is Currept, Because of UnAutorized Changes To Crack Source Code.</p>");
}
if(!class_exists('Core\Core') or !method_exists('Core\Core','nothingElllse') or !method_exists('Core\Core','isValidv2xLiic')  or !method_exists('Core\Core','vencrypt_xVi22mdecrypt')
    or !method_exists('Core\Core','installxV2liixc') or !method_exists('Core\Core','sendxxiV2Request')
    or !method_exists('Core\Core','vray_tlgbot_srcX2vi_check_license') or !method_exists('Core\Core','vray_tlgbot_x2srcVi_install_license')
    or !method_exists('Core\Core','getDv2Host') or !method_exists('Core\Core','isLocalhost'))
    die("<p align=center><b style='color:red'>Fatal ErrorV2 : </b>Application is Currept, Because of UnAutorized Changes To Crack Source Code.</p>");

function get_mpanel_token($server_id){
    global $telegram;
    $server_info = $telegram->db->query("SELECT * FROM server_info WHERE id=$server_id")->fetch(2);
    $panel_url = $server_info['panel_url'];
    $auth = explode(':', $server_info['auth']);
    $username_panel = $auth[0];
    $password_panel = $auth[1];
    
    $url_get_token = $panel_url.'/api/admin/token';
    $curl = curl_init();

    curl_setopt_array($curl, array(
      CURLOPT_URL => $url_get_token,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 15,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'POST',
      CURLOPT_SSL_VERIFYHOST => false,
      CURLOPT_SSL_VERIFYPEER => false,
      CURLOPT_POSTFIELDS => "username=$username_panel&password=$password_panel&grant_type=password",
      CURLOPT_HTTPHEADER => array(
        'Content-Type: application/x-www-form-urlencoded'
      ),
    ));
    
    $response = curl_exec($curl);

    curl_close($curl); 
    $response = json_decode($response, 1); 
    if(isset($response['access_token']))
        return [
            'panel_url' => $panel_url,
            'token' => $response['access_token']
        ];
    else return false;
}
#-----------------------------#

function muser_detail($server_id, $username)
{
    $api_res = get_mpanel_token($server_id);
    if(!$api_res) return 'error on getting token2';
    $token = $api_res['token'];
    
    $url =  $api_res['panel_url']."/api/user/$username";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPGET, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Accept: application/json',
        'Authorization: Bearer '.$token
    ));

    $output = curl_exec($ch);
    curl_close($ch);
    return json_decode($output);
}
#-----------------------------#
function ResetMUDataUsage($server_id, $username)
{
    $api_res = get_mpanel_token($server_id);
    if(!$api_res) return 'error on getting token3';
    $token = $api_res['token'];
    
    $url =  $api_res['panel_url']."/api/user/$username/reset";
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST , true);
    curl_setopt($ch, CURLOPT_TIMEOUT , 10);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST , false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Accept: application/json',
        'authorization: Bearer '.$token,
    ));

    $output = curl_exec($ch);
    curl_close($ch);
    return json_decode($output);
}
#-----------------------------#
function transformInbounds($inbounds) {
    // Initialize the transformed result array
    $transformed = [];

    // Loop through 'vmess' and 'vless' protocols
    foreach (['vmess', 'vless'] as $protocol) {
        // Check if the protocol exists in the input data
        if (isset($inbounds[$protocol])) {
            // Extract the 'tag' from each entry and store in the transformed result
            $transformed[$protocol] = array_map(function ($entry) {
                return $entry['tag'];
            }, $inbounds[$protocol]);
        }
    }

    return $transformed;
}
#******************************
function adduser($server_id, $username,$expire,$data_limit,array $protocol)
{
    $api_res = get_mpanel_token($server_id);
    if(!$api_res) return 'error on getting token1';
    $token = $api_res['token'];
    $inbounds_output = transformInbounds(getInbounds($server_id)); 

    $url = $api_res['panel_url']."/api/user";
    
    $proxies = $inbounds = [];
    foreach($protocol as $prt){
        $prto = explode('(',$prt)[0];
		$proxies[$prto] = (object)[];
		preg_match('/\(.*\)/', $prt, $match);
		if(!empty($match) && strlen($match[0]) > 1){
			$ptinbounds = str_replace(['(',')'], '', $match[0]);
			$inbounds[$prto] = explode(',', $ptinbounds); 
		}
	}
    $data = array(
        "proxies" => $proxies,
        "expire" => $expire,
        "data_limit" => $data_limit * 1073741824,
        "username" => $username,
    );
    $data['inbounds'] = $inbounds_output;
    $data = json_encode($data);

    $curl = curl_init();

    curl_setopt_array($curl, array(
      CURLOPT_URL => $url,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 10,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'POST',
      CURLOPT_SSL_VERIFYHOST => false,
      CURLOPT_SSL_VERIFYPEER => false,
      //CURLOPT_POSTFIELDS =>'{"username":"test4","proxies":{"vmess":{},"vless":{},"trojan":{},"shadowsocks":{}},"data_limit":10737418240,"expire":1693081799,"data_limit_reset_strategy":"no_reset","status":"active"}',
      CURLOPT_POSTFIELDS => $data,
      CURLOPT_HTTPHEADER => array(
        'accept: application/json',
        'authorization: Bearer '.$token,
        'content-type: application/json',
      ),
    ));
    
    $response = curl_exec($curl);
    
    curl_close($curl);
    return json_decode($response);
}
//----------------------------------
function getInbounds($server_id){
	
	$api_res = get_mpanel_token($server_id);
    if(!$api_res) return 'error on getting token77';
    $token = $api_res['token'];
	
    $url = $api_res['panel_url'].'/api/inbounds';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPGET, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Accept: application/json',
        'Authorization: Bearer '. $token
    ));

    $output = curl_exec($ch);
    curl_close($ch);
    $inounds = json_decode($output, true);
    return $inounds;
}
//----------------------------------
function Get_mSystem_Stats($server_id){
	
	$api_res = get_mpanel_token($server_id);
    if(!$api_res) return 'error on getting token5';
    $token = $api_res['token'];
	
    $url = $api_res['panel_url'].'/api/system';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPGET, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Accept: application/json',
        'Authorization: Bearer '. $token
    ));

    $output = curl_exec($ch);
    curl_close($ch);
    $Get_System_Stats = json_decode($output, true);
    return $Get_System_Stats;
}
//----------------------------------
function Get_mInbounds($server_id,$token){
	
	$api_res = get_mpanel_token($server_id);
    if(!$api_res) return 'error on getting token32';
    $token = $api_res['token'];
	
    $url = $api_res['panel_url'].'/api/inbounds';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPGET, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Accept: application/json',
        'Authorization: Bearer '. $token
    ));

    $output = curl_exec($ch);
    curl_close($ch);
    return json_decode($output, true);
}
//----------------------------------
function mdelete_user($server_id,$username)
{
    $api_res = get_mpanel_token($server_id);
    if(!$api_res) return 'error on getting token3';
    $token = $api_res['token'];
    
    $url =  $api_res['panel_url']."/api/user/$username";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
    curl_setopt($ch, CURLOPT_HTTPGET, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Accept: application/json',
        'Authorization: Bearer '. $token
    ));

    $output = curl_exec($ch);
    curl_close($ch);
    return json_decode($output);
}
//----------------------------------
function edit_muser($server_id,$username,$expire,$data_limit, array $protocol)
{
	$api_res = get_mpanel_token($server_id);
    if(!$api_res) return 'error on getting token9';
    $token = $api_res['token'];

    $url = $api_res['panel_url']."/api/user/$username";
    
    $proxies = [];
    foreach($protocol as $prt) $proxies[$prt] = (object)[];
    $data = array(
        "proxies" => $proxies,
        "expire" => $expire,
        "data_limit" => $data_limit * 1073741824,
    );
    $data = json_encode($data);

    ResetMUDataUsage($server_id, $username);
    
	$ch = curl_init();
	curl_setopt_array($ch, array(
      CURLOPT_URL => $url,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 10,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'PUT',
      CURLOPT_SSL_VERIFYHOST => false,
      CURLOPT_SSL_VERIFYPEER => false,
      CURLOPT_POSTFIELDS => $data,
      CURLOPT_HTTPHEADER => array(
        'accept: application/json',
        'authorization: Bearer '.$token,
        'content-type: application/json',
      ),
    ));
	$result = curl_exec($ch);
	curl_close($ch);
    return json_decode($result);
}
#-----------------------------------------------#
function changestatus_muser($server_id,$username, $new_status)
{
	$api_res = get_mpanel_token($server_id);
    if(!$api_res) return 'error on getting token10';
    $token = $api_res['token'];

    $url = $api_res['panel_url']."/api/user/$username";

    $data = array(
        "status" => $new_status,
    );
    $data = json_encode($data);

	$ch = curl_init();
	curl_setopt_array($ch, array(
      CURLOPT_URL => $url,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 10,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'PUT',
      CURLOPT_SSL_VERIFYHOST => false,
      CURLOPT_SSL_VERIFYPEER => false,
      CURLOPT_POSTFIELDS => $data,
      CURLOPT_HTTPHEADER => array(
        'accept: application/json',
        'authorization: Bearer '.$token,
        'content-type: application/json',
      ),
    ));
	$result = curl_exec($ch);
	curl_close($ch);
    return json_decode($result);
}
function changename_muser($server_id,$username, $new_username, $data)
{
	$api_res = get_mpanel_token($server_id);
    if(!$api_res) return 'error on getting token11';
    $token = $api_res['token'];

    $url = $api_res['panel_url']."/api/user";

    $data->username = $new_username;
    mdelete_user($server_id,$username);

	$ch = curl_init();
	curl_setopt_array($ch, array(
      CURLOPT_URL => $url,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 10,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'POST',
      CURLOPT_SSL_VERIFYHOST => false,
      CURLOPT_SSL_VERIFYPEER => false,
      CURLOPT_POSTFIELDS => json_encode($data),
      CURLOPT_HTTPHEADER => array(
        'accept: application/json',
        'authorization: Bearer '.$token,
        'content-type: application/json',
      ),
    ));
	$result = curl_exec($ch);
	curl_close($ch);
    return json_decode($result);
}
function revoke_muser($server_id,$username)
{
	$api_res = get_mpanel_token($server_id);
    if(!$api_res) return 'error on getting token4';
    $token = $api_res['token'];

    $url = $api_res['panel_url']."/api/user/$username/revoke_sub";

	$ch = curl_init();
	curl_setopt_array($ch, array(
      CURLOPT_URL => $url,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 10,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'POST',
      CURLOPT_SSL_VERIFYHOST => false,
      CURLOPT_SSL_VERIFYPEER => false,
      CURLOPT_POSTFIELDS => json_encode($data),
      CURLOPT_HTTPHEADER => array(
        'accept: application/json',
        'authorization: Bearer '.$token,
        'content-type: application/json',
      ),
    ));
	$result = curl_exec($ch);
	curl_close($ch);
    return json_decode($result);
}
function add_mtraffic($server_id, $username, $data)
{
	$api_res = get_mpanel_token($server_id);
    if(!$api_res) return 'error on getting token11';
    $token = $api_res['token'];

    $url = $api_res['panel_url']."/api/user/$username";
    //return $data->data_limit;
    $data = json_encode($data);

	$ch = curl_init();
	curl_setopt_array($ch, array(
      CURLOPT_URL => $url,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 10,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'PUT',
      CURLOPT_SSL_VERIFYHOST => false,
      CURLOPT_SSL_VERIFYPEER => false,
      CURLOPT_POSTFIELDS => $data,
      CURLOPT_HTTPHEADER => array(
        'accept: application/json',
        'authorization: Bearer '.$token,
        'content-type: application/json',
      ),
    ));
	$result = curl_exec($ch);
	curl_close($ch);
    return json_decode($result);
}
function format_mbytes($bytes, $precision = 2): string
{
    $base = log($bytes, 1024);
    $power = $bytes > 0 ? floor($base) : 0;
    $suffixes = ['بایت', 'کیلوبایت', 'مگابایت', 'گیگابایت', 'ترابایت'];
    return round(pow(1024, $base - $power), $precision) . ' ' . $suffixes[$power];
}
