<?php
            
header('Content-Type: text/plain; charset=utf-8');
$dbServername = "localhost";
$dbUsername   = "tomyvpnu_panel";
$dbPassword   = "tomyvpnu_panel";
$dbName       = "tomyvpnu_panel";
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);


if(isset($_GET['update'])){
    
    $data = $_GET['update'];
    $data = json_decode($data, true);
    $customField = $data['customfield'];
    
    $account = array();
    foreach($customField as $key => $info){
        array_push($account, "$info");
        
    }
    
    $username = $account[0];
    $password = $account[1];
    $modelId  = $data['id'];
    $date = $data['nextduedate'];
    $date = explode('/', $date);
    $date = $date['2'].'-'.$date['0'].'-'.$date['1'];
    $dueDate = date("Y-m-d h:i:s", strtotime($date));
    $status = strtolower($data['domainstatus']);
    
    $sql = "UPDATE `vpns` SET `username` = '$username', `password` = '$password', `status` = '$status', `expired_on` = '$dueDate' WHERE `model_id` = '$modelId'";
    
    if(mysqli_query($conn, $sql)){
        
        //set note
        if($status != ''){
            if($status != 'active'){
                $status = '(whmcs) '.$status;
            } else {
                $status = 'whmcs';
            }
            $info = "SELECT id FROM vpns WHERE `model_id` = '$modelId'";
            $infoR = mysqli_query($conn, $info);
            $id = mysqli_fetch_assoc($infoR)['id'];
            mysqli_query($conn, "UPDATE `account_notes` SET `content` = '$status' WHERE `account_id` = '$id'");
        }
    }
    

    
}
if(isset($_GET['username'])){
    if(isset($_GET['resellers'])){
        
        $username = $_GET['username'];
        $username = mysqli_real_escape_string($conn, $username);
        $sql = "SELECT `name` FROM `users` WHERE `name` = '$username' OR `email` = '$username'";
        $user = mysqli_query($conn, $sql);
        $user_num_rows  = mysqli_num_rows($user);
    
        if($user_num_rows > 0){
            http_response_code(422);
        } else {
            echo "success";
            http_response_code(200);
        }
        
    } else {
        $username = $_GET['username'];
        $username = mysqli_real_escape_string($conn, $username);
        $sql = "SELECT username FROM `vpns` WHERE username = '$username'";
        $user = mysqli_query($conn, $sql);
        $user_num_rows  = mysqli_num_rows($user);
        
        if($user_num_rows > 0){
            http_response_code(422);
        } else {
            echo "success";
            http_response_code(200);
        }
    }
}
if (isset($_GET['create'])){

    if($_GET['create'] == 'reseller'){
        
        $date = date("Y-m-d h:i:s");
        $username = $_GET['username'];
        $username = mysqli_real_escape_string($conn, $username);    
        
        $password = $_GET['password'];
        //$password = mysqli_real_escape_string($conn, $password);
        $password = password_hash("$password", PASSWORD_BCRYPT);
        
        $modelId = $_GET['modelId'];
        $modelId = mysqli_real_escape_string($conn, $modelId);
        
        $client_role = $_GET['client_role'];
        $client_role = mysqli_real_escape_string($conn, $client_role);
        
        $client_balance = $_GET['client_balance'];
        $client_balance = mysqli_real_escape_string($conn, $client_balance);
        
        $client_api_token = $_GET['client_api_token'];
        $client_api_token = mysqli_real_escape_string($conn, $client_api_token);
        
        //check if username exist
        $check = "SELECT `email` FROM `users` WHERE `email` = '$username'";
        $checkR = mysqli_num_rows(mysqli_query($conn, $check));
        
        if($checkR > 0){
            http_response_code(422);
        } else {
            
            //proceed if username is not registered yet
            $sql = "INSERT INTO `users`(`model_id`, `name`, `email`, `password`, `role`, `images`, `balance`, `point`, `lock`, `suspend`, `api_token`, `remember_token`, `created_at`, `created_by`, `updated_at`) 
                    VALUES ('$modelId', '$username', '$username', '$password', '$client_role', 'http://lorempixel.com/200/200/cats/', '$client_balance', '0','0','0','$client_api_token', '','$date','1','$date')";
                     
            if(mysqli_query($conn,$sql)){
                echo "success";
                http_response_code(201);
                
            } else {
                echo $sql;
                //http_response_code(422);
            }
            
        }
        
            
    } else {
        
    
        $username = $_GET['username'];
        $username = mysqli_real_escape_string($conn, $username);    
        
        $password = $_GET['password'];
        $password = mysqli_real_escape_string($conn, $password);
        
        $modelId = $_GET['modelId'];
        $modelId = mysqli_real_escape_string($conn, $modelId);
        
        $duration = $_GET['duration'];
        $duration = mysqli_real_escape_string($conn, $duration);
        
        $date = date("Y-m-d h:i:s");
        $expired_on = date("Y-m-d h:i:s", strtotime("+$duration months", strtotime($date)));
        
        //check if username exist
        $check = "SELECT username FROM `vpns` WHERE `username` = '$username'";
        $checkR = mysqli_num_rows(mysqli_query($conn, $check));
        
        if($checkR > 0){
            http_response_code(422);
            
        } else {
        
            //proceed if username is not registered yet
            $sql = "INSERT INTO `vpns`(`reseller_email`, `username`, `password`, `status`, `online`, `duration`, `activated_at`, `expired_on`, `created_at`, `updated_at`, `count_online`, `model_id`) 
                    VALUES ('whmcs','$username','$password','active','0','$duration', '', '$expired_on','$date','$date', '0', '$modelId')";
                    
            if($result = mysqli_query($conn, $sql)){
                
                $last_id = $conn->insert_id;
                mysqli_query($conn, "INSERT INTO account_notes (`account_id`, `content`, `created_at`, `updated_at`) VALUES ('$last_id','whmcs','$date','$date')");
                echo "success";
                http_response_code(200);
                
            } else {
                
                http_response_code(422);
            }
        }
        
        
    }

}else if(isset($_GET['renew']))
{
    $modelId = $_GET['modelId'];
    $modelId = mysqli_real_escape_string($conn, $modelId);
        
    $duration = $_GET['duration'];
    $duration = mysqli_real_escape_string($conn, $duration);
    
    $date = date("Y-m-d h:i:s");
    $getExpiry = "SELECT `expired_on` FROM `vpns` WHERE `model_id` = '$modelId'";
    $expiryR = mysqli_query($conn, $getExpiry);
    $expiry = mysqli_fetch_assoc($expiryR)['expired_on'];
    
    $date = date("Y-m-d h:i:s", strtotime("+$duration months", strtotime($expiry)));
    
    
    $sql = "UPDATE `vpns` SET `expired_on` = '$date' WHERE `model_id` = '$modelId'";
    
    $result = mysqli_query($conn, $sql);
        if($result){
            echo "success";
            http_response_code(200);
        } else {
            http_response_code(422);
        }
    
}
else if (isset($_GET ['changePwd'])){
    
    if($_GET['changePwd'] == 'reseller'){
        $password = $_GET['password'];
        $password = password_hash("$password", PASSWORD_BCRYPT);
        $modelId = $_GET['modelId'];
        $modelId = mysqli_real_escape_string($conn, $modelId);
        
        $sql = "UPDATE `users` SET `password` = '$password' WHERE `model_id` = '$modelId'";
        $result = mysqli_query($conn, $sql);
        if(mysqli_num_rows($result)>0){
            echo "success";
            http_response_code(201);
        } else {
            http_response_code(422);
        }
        
    } else {
        $username = $_GET['username'];
        $username = mysqli_real_escape_string($conn, $username);    
        $password = $_GET['password'];
        $password = mysqli_real_escape_string($conn, $password);
        $modelId = $_GET['modelId'];
        $modelId = mysqli_real_escape_string($conn, $modelId);
        $date = date("Y-m-d h:i:s");
        
        $sql = "UPDATE vpns SET `password` = '$password' WHERE `model_id` = '$modelId'";
        $result = mysqli_query($conn, $sql);
        if(mysqli_num_rows($result)>0){
            echo "success";
            http_response_code(201);
        } else {
            http_response_code(422);
        }
    }
}
else if (isset($_GET['terminate'])){
    
    if($_GET['terminate'] == 'reseller'){
        
        $modelId = $_GET['modelId'];
        $modelId = mysqli_real_escape_string($conn, $modelId);
        
        $sql = "UPDATE `users` SET `lock` = 1 WHERE `model_id` = '$modelId'";
        
        if(mysqli_query($conn, $sql)){
           
            echo "success";
            http_response_code(200);
        } else {
            http_response_code(422);
        }
        
    } else {
    
        $modelId = $_GET['modelId'];
        $modelId = mysqli_real_escape_string($conn, $modelId);
        
        $info = "SELECT id FROM vpns WHERE `model_id` = '$modelId'";
        
        $infoR = mysqli_query($conn, $info);
        $id = mysqli_fetch_assoc($infoR)['id'];
        
        $sql = "UPDATE `vpns` SET `status` = 'terminated' WHERE `model_id` = '$modelId'";
        
        if(mysqli_query($conn, $sql)){
            mysqli_query($conn, "UPDATE `account_notes` SET `content` = '(whmcs) Terminated' WHERE `account_id` = '$id'");
            echo "success";
            http_response_code(200);
        } else {
            http_response_code(422);
        }
    }
    
}
else if (isset($_GET['suspend'])){
    
    if($_GET['suspend'] == 'reseller'){
        
        $modelId = $_GET['modelId'];
        $modelId = mysqli_real_escape_string($conn, $modelId);
        
        $sql = "UPDATE `users` SET `suspend` = 1 WHERE `model_id` = '$modelId'";
        
        if(mysqli_query($conn, $sql)){
           
            echo "success";
            http_response_code(200);
        } else {
            http_response_code(422);
        }
        
    } else {
        $modelId = $_GET['modelId'];
        $modelId = mysqli_real_escape_string($conn, $modelId);
        
        $reason = $_GET['suspend'];
        $reason = "(whmcs) ".mysqli_real_escape_string($conn, $reason);
        
        $info = "SELECT id FROM vpns WHERE `model_id` = '$modelId'";
        
        $infoR = mysqli_query($conn, $info);
        $id = mysqli_fetch_assoc($infoR)['id'];
        
        $sql = "UPDATE `vpns` SET `status` = 'suspended' WHERE `model_id` = '$modelId'";
        if(mysqli_query($conn, $sql)){
            mysqli_query($conn, "UPDATE `account_notes` SET `content` = '$reason' WHERE `account_id` = '$id'");
            echo "success";
            http_response_code(200);
        } else {
            http_response_code(422);
        }
    }
    
}
else if (isset($_GET['unsuspend'])){
    
    if($_GET['unsuspend'] == 'reseller'){
        
        $modelId = $_GET['modelId'];
        $modelId = mysqli_real_escape_string($conn, $modelId);
        
        $sql = "UPDATE `users` SET `suspend` = 0, `lock` = 0 WHERE `model_id` = '$modelId'";
        
        if(mysqli_query($conn, $sql)){
           
            echo "success";
            http_response_code(200);
        } else {
            http_response_code(422);
        }
        
    } else {
    
        $modelId = $_GET['modelId'];
        $modelId = mysqli_real_escape_string($conn, $modelId);
        
        $info = "SELECT id FROM vpns WHERE `model_id` = '$modelId'";
        
        $infoR = mysqli_query($conn, $info);
        $id = mysqli_fetch_assoc($infoR)['id'];
        
        $sql = "UPDATE `vpns` SET `status` = 'active' WHERE `model_id` = '$modelId'";
        
        if(mysqli_query($conn, $sql)){
            mysqli_query($conn, "UPDATE `account_notes` SET `content` = 'whmcs' WHERE `account_id` = '$id'");
            echo "success";
            http_response_code(200);
        } else {
            http_response_code(422);
        }
    }
}
else if(isset($_GET['addPins'])){
    $modelId = $_GET['serviceId'];
    $pins = $_GET['number'];
    
    $sql = "UPDATE `users` SET `balance` = balance + $pins WHERE `model_id` = '$modelId'";
    
    
     if(mysqli_query($conn, $sql)){
            
            echo "success";
            http_response_code(200);
        } else {
            http_response_code(422);
        }
}
else if(isset($_GET['getCredits'])){
    $modelId = $_GET['modelId'];
    
    $sql = "SELECT `balance` FROM `users` WHERE `model_id` = '$modelId'";
    $result = mysqli_query($conn, $sql);
    if($result){
         $credits = mysqli_fetch_assoc($result);
         echo $credits['balance'];
  
    } else {
         http_response_code(422);
    }
    
    
}
else if(isset($_GET['servers'])){
    
    $data = "SELECT `version`, `message` FROM `server_update`";
    $dataR = mysqli_query($conn, $data);
    $data = mysqli_fetch_assoc($dataR);
    $version = $data['version'];
    $version = number_format(floatval($version), 2, '.', '');
    $message = $data['message'];
    
    $sql = "SELECT `name`, `country`,`ip` FROM `servers`";
    $result = mysqli_query($conn, $sql);
    $numrows = mysqli_num_rows($result);
    
    $data = array();
    $data['ConfigVersion'] = $version;
    $data['ConfigUpdateMsg'] = $message;
    $data['SquidPort'] = "80";
    $data['ServHttp'] = "443";
    $data['ServSsl'] = "4433";
    
    $data['Servers'] = array();
    $data['Tweaks'] = array();
    
    for($i=0;$i<$numrows;$i++){
        $rows = mysqli_fetch_assoc($result);
        array_push($data['Servers'], array("Name"=>$rows['name'].', '.strtoupper($rows['country']), "Server"=>$rows['ip']));
    }
    array_push($data['Tweaks'], array("Name"=>"", "ModeHttp"=>false, "UseCustom"=>false, "CustomProxy"=>"", "Payload"=>"づぬてぴは〩つとな"));
    
    echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT );
}
















