<?php
function module_name_here_config()
{
    full_query("CREATE TABLE IF NOT EXISTS mod_hosting_hostarina (service_id INT,remote_id INT)");
    return [
        "name" => "module_title_here",
        "description" => "module_description_here",
        "version" => "1.0",
        "author" => "module_author_here",
        "author_url"=>"module_author_url_here",
        "RequiresServer"=>false,//true or false
        "ServiceSingleSignOnLabel"=>'',
        "AdminSingleSignOnLabel"=>'',
        "PackagesLoader"=>'PackagesLoader',
        "configs" => []
    ];
}
function module_name_here_ApiConfig($params=[])
{
    return [
        "token" => ["Name"=>"API Token","Type"=> "text","Description"=>"Enter your API Token here."
        ]
    ];
}
function module_name_here_call($act="",$post=[],$params=[])
{
    $post['token'] = $params["module_config_token"];
    //$post['token'] = "^pk8gV2fSHJ0^!#Ns8j!";
    $curl = curl_init();
    curl_setopt_array($curl, array(
      CURLOPT_URL => 'https://module_api_url_here/rapi/'.$act,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_TIMEOUT => 15,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_SSL_VERIFYHOST => false,
      CURLOPT_SSL_VERIFYPEER => false,
      CURLOPT_CUSTOMREQUEST => 'POST',
      CURLOPT_POSTFIELDS =>$post,
    ));
    $response = curl_exec($curl);
    if ($response === false)
    {
    	$response=_je(["status"=>"error","message"=>"cURL Error: ".curl_error($curl)]);
	}
    curl_close($curl);
    return json_decode($response,true);
}
function module_name_here_PackagesLoader($arg)
{
    $response = module_name_here_call("GetProducts",[],$arg);
    if($response["status"] == "error") return $response;
    $datas = $response["data"];
    if(empty($datas)) return ["status"=>"error","message"=>"Products not found"];
    $list = [];
    foreach ($datas as $id =>$data)
    {
    	$title = $data["group_name"]." - ".$data["title"];
    	$list[$id] = ucfirst($title);
    }
    return $list;
}
function module_name_here_CreateAccount($arg)
{
	$serviceid = $arg['serviceid'];
	$product = $arg['product'];
	$domain = $product['domain'];
	$package = $product['package'];
	$duration = $product['service_billing_cycle_months'];
    $client = $arg['client'];
    $email = $client['email'];
    $info = get_one("mod_hosting_hostarina",["service_id"=>$serviceid]);
    $remote_id = 0;
    if(!empty($info)) $remote_id = _s("remote_id",$info,0);
    //if($remote_id>0) return ["status"=>"error","message"=>"Remote ID exist against this Service, So please terminate this service then create again"];
    $response = module_name_here_call("ProductCreate",["service_id"=>$remote_id,"package_id"=>$package,"domain_name"=>$domain,"email"=>$email,"duration"=>$duration],$arg);
    if($response["status"] == "success")
    {
    	$remote_id = $response["data"]["id"];
	    $info = get_one("mod_hosting_hostarina",["service_id"=>$serviceid,"remote_id"=>$remote_id]);
	    if(empty($info) && $remote_id>0)
	    {
            $user_name = $response["data"]["user_name"];
            $password = $response["data"]["password"];
            update_query("hosting",["user_name"=>$user_name,"password"=>$password],["id"=>$serviceid]);
	    	insert_query("mod_hosting_hostarina",["service_id"=>$serviceid,"remote_id"=>$remote_id]);
	    }
    }
    return $response;
}
function module_name_here_TerminateAccount($arg)
{
	$serviceid = $arg['serviceid'];
    $info = get_one("mod_hosting_hostarina",["service_id"=>$serviceid]);
    if(empty($info)) return ["status"=>"error","message"=>"Services not exist, so please create 1st"];
    $remote_id = 0;
    if(!empty($info)) $remote_id = _s("remote_id",$info,0);
    $response = module_name_here_call("ProductTerminate",["service_id"=>$remote_id],$arg);
    if($response["status"] == "success") delete_query("mod_hosting_hostarina",["service_id"=>$serviceid]);
    return $response;
}
function module_name_here_Renew($arg)
{
    $serviceid = $arg['serviceid'];
    $info = get_one("mod_hosting_hostarina",["service_id"=>$serviceid]);
    if(empty($info)) return ["status"=>"error","message"=>"Services not exist, so please create 1st"];
    $remote_id = 0;
    if(!empty($info)) $remote_id = _s("remote_id",$info,0);
    $response = module_name_here_call("ProductRenew",["service_id"=>$remote_id],$arg);
    return $response;
}
function module_name_here_SuspendAccount($arg)
{
	$serviceid = $arg['serviceid'];
	$reason = $arg['reason'];
    $info = get_one("mod_hosting_hostarina",["service_id"=>$serviceid]);
    if(empty($info)) return ["status"=>"error","message"=>"Services not exist, so please create 1st"];
    $remote_id = 0;
    if(!empty($info)) $remote_id = _s("remote_id",$info,0);
    $response = module_name_here_call("ProductSuspend",["service_id"=>$remote_id,"reason"=>$reason],$arg);
    return $response;
}
function module_name_here_UnsuspendAccount($arg)
{
	$serviceid = $arg['serviceid'];
	$reason = $arg['reason'];
    $info = get_one("mod_hosting_hostarina",["service_id"=>$serviceid]);
    if(empty($info)) return ["status"=>"error","message"=>"Services not exist, so please create 1st"];
    $remote_id = 0;
    if(!empty($info)) $remote_id = _s("remote_id",$info,0);
    $response = module_name_here_call("ProductUnsuspend",["service_id"=>$remote_id,"reason"=>$reason],$arg);
    return $response;
}
function module_name_here_ChangePassword($arg)
{
	$product = $arg['product'];      
	$password = $product['password'];
	$serviceid = $arg['serviceid'];
    $info = get_one("mod_hosting_hostarina",["service_id"=>$serviceid]);
    if(empty($info)) return ["status"=>"error","message"=>"Services not exist, so please create 1st"];
    $remote_id = 0;
    if(!empty($info)) $remote_id = _s("remote_id",$info,0);
    $response = module_name_here_call("ProductChangePassword",["service_id"=>$remote_id,"password"=>$password],$arg);
    return $response;
}
function module_name_here_ChangePackage($arg)
{
	$product = $arg['product'];      
	$package = $product['package'];
	$serviceid = $arg['serviceid'];
    $info = get_one("mod_hosting_hostarina",["service_id"=>$serviceid]);
    if(empty($info)) return ["status"=>"error","message"=>"Services not exist, so please create 1st"];
    $remote_id = 0;
    if(!empty($info)) $remote_id = _s("remote_id",$info,0);
    $response = module_name_here_call("ProductChangePackage",["service_id"=>$remote_id,"package_id"=>$package],$arg);
    return $response;
}
function module_name_here_gblicensing_ChangeIp($arg)
{
    $product = $arg['product'];      
    $package = $product['package'];
    $serviceid = $arg['serviceid'];
    $info = get_one("mod_hosting_hostarina",["service_id"=>$serviceid]);
    if(empty($info)) return ["status"=>"error","message"=>"Services not exist, so please create 1st"];
    $remote_id = 0;
    if(!empty($info)) $remote_id = _s("remote_id",$info,0);
    $oIP = $params['customfields']['IP'];
    $IPc = _s("IPc",$_POST,"");
    $json = ['ip'=>$arg['customfields']['IP'],'ip_new'=>$IPc];
    $response = module_name_here_call("ProductCustom",["service_id"=>$remote_id,"function_name"=>"ChangeIp","function_json"=>_je($json)],$arg);
    return $response;
}
function module_name_here_licencepro_ChangeIp($arg)
{
    $product = $arg['product'];      
    $package = $product['package'];
    $serviceid = $arg['serviceid'];
    $info = get_one("mod_hosting_hostarina",["service_id"=>$serviceid]);
    if(empty($info)) return ["status"=>"error","message"=>"Services not exist, so please create 1st"];
    $remote_id = 0;
    if(!empty($info)) $remote_id = _s("remote_id",$info,0);
    $oIP = $params['customfields']['IP'];
    $IPc = _s("IPc",$_POST,"");
    $json = ['ip'=>$arg['customfields']['IP'],'ip_new'=>$IPc];
    $response = module_name_here_call("ProductCustom",["service_id"=>$remote_id,"function_name"=>"ChangeIp","function_json"=>_je($json)],$arg);
    return $response;
}
function module_name_here_ws_reissue($arg)
{
    $product = $arg['product'];      
    $package = $product['package'];
    $serviceid = $arg['serviceid'];
    $info = get_one("mod_hosting_hostarina",["service_id"=>$serviceid]);
    if(empty($info)) return ["status"=>"error","message"=>"Services not exist, so please create 1st"];
    $remote_id = 0;
    if(!empty($info)) $remote_id = _s("remote_id",$info,0);
    $response = module_name_here_call("ProductCustom",["service_id"=>$remote_id,"function_name"=>"ReissueLicenseByClient","function_json"=>_je([])],$arg);
    return $response;
}
function module_name_here_ServiceSingleSignOn($arg)
{
    $product = $arg['product'];      
    $package = $product['package'];
    $serviceid = $arg['serviceid'];
    $info = get_one("mod_hosting_hostarina",["service_id"=>$serviceid]);
    if(empty($info)) return ["status"=>"error","message"=>"Services not exist, so please create 1st"];
    $remote_id = _s("remote_id",$info,0);
    $app = "";
    if(isset($arg["app"])) $app = $arg["app"];
    $response = module_name_here_call("ProductSignin",["service_id"=>$remote_id,"app"=>$app],$arg);
    if($response["status"] == "error") return ["status"=>"error","message"=>$response["message"]];
    return ["status"=>"success","url"=>isset($response["data"]["url"]) ? $response["data"]["url"]:""];
}
function module_name_here_AdminSingleSignOn($arg){
    return module_name_here_ServiceSingleSignOn($arg);
}
function module_name_here_gblicensing_revoke($arg)
{
    $product = $arg['product'];      
    $package = $product['package'];
    $serviceid = $arg['serviceid'];
    $info = get_one("mod_hosting_hostarina",["service_id"=>$serviceid]);
    if(empty($info)) return ["status"=>"error","message"=>"Services not exist, so please create 1st"];
    $remote_id = 0;
    if(!empty($info)) $remote_id = _s("remote_id",$info,0);
    $response = module_name_here_call("ProductCustom",["service_id"=>$remote_id,"function_name"=>"revoke","function_json"=>_je([])],$arg);
    return $response;
}
function module_name_here_ClientAreaCustomButtonArray($arg)
{
    $buttons = [];
    $product = $arg['product'];      
    $package = $product['package'];
    $serviceid = $arg['serviceid'];
    $info = get_one("mod_hosting_hostarina",["service_id"=>$serviceid]);
    if(!empty($info))
    {
        $remote_id = _s("remote_id",$info,0);
        $response = module_name_here_call("GetClientProducts",["id"=>$remote_id],$arg);
        if($response["status"] == "success")
        {
            $Info = isset($response["data"]["Result"][$remote_id]) ? $response["data"]["Result"][$remote_id]:[];
            $server_type = _s("server_type",$Info,"");
            if($server_type == "license_manager")
            {
            $buttons = [
                'Download Now'=>'https://whasols.com/downloads',
                'Release Notes'=>'https://whasols.com/documentation/document/5-Release-Notes',
                'Installation Instructions'=>'https://whasols.com/documentation/document/3-Installing-WHASOLS',
                'Update Instructions'=>'https://whasols.com/documentation/document/4-Updating',
                ];
            }
        }
    }
    return $buttons;
}
function module_name_here_AdminCustomButtonArray($arg=[])
{
    $status = $arg["productstatus"];
    if(in_array($status,["Terminated","Pending","Completed","Cancelled","Fraud"])) return [];

    $product = $arg['product'];      
    $package = $product['package'];
    $serviceid = $arg['serviceid'];
    $info = get_one("mod_hosting_hostarina",["service_id"=>$serviceid]);
    if(!empty($info))
    {
        $remote_id = _s("remote_id",$info,0);
        $response = module_name_here_call("GetClientProducts",["id"=>$remote_id],$arg);
        if($response["status"] == "success")
        {
            $Info = isset($response["data"]["Result"][$remote_id]) ? $response["data"]["Result"][$remote_id]:[];
            $server_type = _s("server_type",$Info,"");
            if($server_type == "gblicensingv1")
            {
                if($status == "Active") return ["Renew license" =>"Renew"];
                if($status == "Suspended") return ["Delete license" =>"gblicensing_revoke"];
            }
            if($server_type == "license_manager") return ['Reissue'=>'Renew'];
        }
    }
    return ['Renew'=>'Renew'];
}
function module_name_here_ClientArea($arg)
{
    $product = $arg['product'];      
    $package = $product['package'];
    $serviceid = $arg['serviceid'];
    $info = get_one("mod_hosting_hostarina",["service_id"=>$serviceid]);
    $datas = [];
    if(empty($info))
    {
        $datas = ["status"=>"error","message"=>"Services not exist, so please create 1st"];
    }else{
        $remote_id = _s("remote_id",$info,0);
        $response = module_name_here_call("GetClientProducts",["id"=>$remote_id],$arg);
        if($response["status"] == "error")
        {
            $datas = ["status"=>"error","message"=>$response["message"]];
        }else{
            $datas = ["status"=>"success","service_data"=>isset($response["data"]["Result"][$remote_id]) ? $response["data"]["Result"][$remote_id]:[]];
        }
    }
    $templatepath = "modules/servers/module_name_here/templates/";
    $loadFile = "";
    if(isset($datas["service_data"]))
    {
        if(isset($datas["service_data"]["server_type"]))
        {
            $loadFilePath = $templatepath.$datas["service_data"]["server_type"]."/overview.tpl";
            if(file_exists($loadFilePath)) $loadFile = $loadFilePath;
        }
    }
    $datas["cp_template_path"] = $templatepath;
    $datas["cp_loadFile"] = $loadFile;
    return array(
        'templatefile' => 'overview.tpl',
        'templatepath' => $templatepath,
        'vars' => $datas,
    );
}