<?php

function hook_sab_suspend_account($vars) {
	 $id = $vars['id'];
	
/// GETTING ACCOUNT ID TO SUSPEND ///	
	 $getaccountid = "select accountid from sabreseller_rel where addonid=$id";
	 $rs_accountid = mysql_query($getaccountid);
	 
	 while($row = mysql_fetch_array($rs_accountid, MYSQL_ASSOC)){
		$accountid = $row['accountid'];
	 }	 

	$adminuser = 'sabapi';
	$command = "modulesuspend"; 
	$postfields["accountid"] = "$accountid";
	localAPI($command,$postfields,$adminuser);
	
	$sql_update = "update sabreseller_rel set status = 'Suspended' where addonid = $id";
	mysql_query($sql_update);
	
}



function hook_sab_terminate_account($vars) {
	 $id = $vars['id'];
	
/// GETTING ACCOUNT ID TO SUSPEND ///	
	 $getaccountid = "select accountid from sabreseller_rel where addonid = $id";
	 $rs_accountid = mysql_query($getaccountid);
	 
	 while($row = mysql_fetch_array($rs_accountid, MYSQL_ASSOC)){
		$accountid = $row['accountid'];
	 }	 

	$adminuser = 'sabapi';
	$command = "moduleterminate"; 
	$postfields["accountid"] = "$accountid";
	localAPI($command,$postfields,$adminuser);
	
	$sql_update = "update sabreseller_rel set status = 'Terminated' where addonid = $id";
	mysql_query($sql_update);
	
}





function hook_sab_create_account($vars) {
	 $id = $vars['id'];
     $addonid = $vars['addonid'];
	 $clientid = $vars['userid'];
	 $hostingid = $vars['serviceid'];

/// GETTING MAIN PRODUCT PAYMENT METHOD AND BILLING CYCLE ///
$sab_bill = "select paymentmethod,billingcycle from tblhosting where id=$hostingid";
$sabrs_bill = mysql_query($sab_bill);
while($row = mysql_fetch_array($sabrs_bill, MYSQL_ASSOC)){
	$sab_paymentmethod = $row['paymentmethod'];
	$sab_billingcycle = $row['billingcycle'];
	
}	

/// CHECKING IF ADDONID IS A SITEAUTOBACKUP ADDON ///	
	 $check_addon = "select product_id from sabreseller where addon_id=$addonid";
	 $rs_checkaddon = mysql_query($check_addon);
	 
	 while($row = mysql_fetch_array($rs_checkaddon, MYSQL_ASSOC)){
		$product_id = $row['product_id'];
	 }
	 
	 $sab_addon = mysql_num_rows($rs_checkaddon);
	 
if($sab_addon>=1){	 


/// CHECK IF ADDON IS MORE THAN 1 IN TBLHOSTINGADDONS, IF SO REMOVE NEW ONE.

$sql_cao = "select status from sabreseller_rel where addonid=$id and status='Suspended'";
$rs_cao = mysql_query($sql_cao);
$total_cao = mysql_num_rows($rs_cao);

if($total_cao == 1){

	/// GETTING ACCOUNT ID TO UNSUSPEND ///	
	 $getaccountid = "select accountid from sabreseller_rel where hostingid=$hostingid";
	 $rs_accountid = mysql_query($getaccountid);
	 
	 while($row = mysql_fetch_array($rs_accountid, MYSQL_ASSOC)){
		$accountid = $row['accountid'];
	 }	 

	$adminuser = 'sabapi';
	$command = "moduleunsuspend"; 
	$postfields["accountid"] = "$accountid";
	$result = localAPI($command,$postfields,$adminuser);

	}else{

function doapi($command,$postfields){
 $adminuser = 'sabapi';
 
 # Call API
 $results = localAPI($command,$postfields,$adminuser);
	if ($results["result"]=="success") {
	  # Result was OK!
	  //print "ok\r\n";
	} else {
	  # An error occured
	  echo "error: ".$results["message"]."\r\n";
	}

	if ($results["clientid"]){
		$clientid = $results["clientid"];
		return $clientid;
	}


	if ($results["orderid"]){
		$orderid = $results["orderid"];
	}

	if ($results["productids"]){
		$productids = $results["productids"];
	}

	if ($orderid) {
		return "$orderid $productids";
	}

}

	$command = "addorder";
	$postfields["clientid"] = "$clientid";
	$postfields["pid"] = "$product_id";
	$postfields["billingcycle"] = "$sab_billingcycle";
	$postfields["paymentmethod"] = "$sab_paymentmethod";
	$postfields["noemail"] = "true";
	
	//Adding order into WHMCS
	$orderaccountid = doapi($command,$postfields);
	
	preg_match("/(\d+) (\d+)/",$orderaccountid,$oamatch);
	$orderid = $oamatch[1];
	$accountid = $oamatch[2];
	
	$sab_insert_rel = "insert into sabreseller_rel (hostingid,accountid,addonid,status) values($hostingid,$accountid,$id,'Active')";
	mysql_query($sab_insert_rel);
	
	//Accepting order
	$command = "acceptorder";
	$postfields["orderid"] = "$orderid";
	
	doapi($command,$postfields);
	
	//Creating sab account
	$command = "modulecreate";
	$postfields["accountid"] = "$accountid";
	doapi($command,$postfields);

}

}
}

     
function hook_sab_client_edit_account($vars) {
	$sabreskey = "52C1v6tq7sBkPBrsjfayu6pfHfUKhAMBqdSIggJtJttLydXCfw2lit0ALBHv3pOI";
	$sabresid = "4409";
	$clientid = $vars['userid'];
	$clientemail = $vars['email'];
	$oldclientemail = $vars['olddata']['email'];
	
$sabparams = "resid=$sabresid&reskey=$sabreskey&action=updatewhmcsemail&whmcsemail=$clientemail&oldwhmcsemail=$oldclientemail";
$sabresume = file_get_contents("https://www.siteautobackup.com/api.php?$sabparams");
	
}

add_hook("AddonActivation",1,"hook_sab_create_account","");
add_hook("AddonAdd",1,"hook_sab_create_account","");
add_hook("AddonActivated",1,"hook_sab_create_account","");
add_hook("AddonSuspended",1,"hook_sab_suspend_account","");
add_hook("AddonDeleted",1,"hook_sab_terminate_account","");
add_hook("AddonTerminated",1,"hook_sab_terminate_account","");
add_hook("AddonCancelled",1,"hook_sab_terminate_account","");
add_hook("AddonFraud",1,"hook_sab_terminate_account","");
add_hook("ClientEdit",1,"hook_sab_client_edit_account","");



?>