<?php
/*
WHMCS MachPanel Integration Package: v 4.4
Supported MachPanel version: 6.3.30 Or Later
Last modified: 01-Jun-2023


Caution: any changes in this file except for those mentioned in corresponding MachPanel integration guide 
may result in failure in server module commands.

Developed and maintained by: MachSol, Inc.
web url: https://www.machsol.com
Machsol support: support@machsol.com
*/

#Note: for WHMCS V5.0 and onwards, place this file in machpanel server module folder.


//Add reference to Client Handler
if(!class_exists('MachPanelClientHandler'))
{
  require_once('MachPanelClientHandler.php');
}

if(!class_exists('MachPanelClientHandler'))
{
  require_once(dirname(__FILE__)."/MachPanelClientHandler.php");
}

if(!class_exists('MachPanelClientHandler'))
{
	die("Module handler file not found: \'".dirname(__FILE__)."/MachPanelClientHandler.php\'");
}

function hook_ClientAdd($params)
{
	try
	{
		
		$customer = Array();
		$clientObj = new MachPanelClientHandler();
		$customerObject = $clientObj->createCustomerObject($params);
		
		$customerSoapObject = new SoapVar($customerObject, // the object to wrap
									SOAP_ENC_OBJECT,
									get_class($customerObject), // the concrete class name
									'http://machsol.com/' // namespace that proxy classes belong to (in WSDL)
									);	
									
		if(get_class($customerObject) == 'ResellerCustomer')
		{
			$customer['RC'] = $customerSoapObject;
			$RespArgs = $clientObj->SvcClient->CreateReseller($customer)->CreateResellerResult;
		}
		else
		{
			$customer['C'] = $customerSoapObject;
			$RespArgs = $clientObj->SvcClient->CreateCustomer($customer)->CreateCustomerResult;
		}
    
		if(!$RespArgs->ResponseStatus)
		{
			#echo "Error creating client in machpanel: ".$RespArgs->ResponseMessage;
			die("Error creating client in machpanel: ".$RespArgs->ResponseMessage);
		}
		else
		{
			return "success";
		}
	}
	catch(Exception $sf)
	{
		$errMsg = 'Error: '.$sf->getMessage();
		$errMsg = $errMsg.'<br /><b>Request:</b> '.$clientObj->SvcClient->__getLastRequest();
		$errMsg = $errMsg.'<br /><b>Response:</b> '.$clientObj->SvcClient->__getLastResponse();
		$errMsg = $errMsg.'<br /><b>Request Header:</b> '.$clientObj->SvcClient->__getLastRequestHeaders();
		$errMsg = $errMsg.'<br /><b>Response Header:</b> '.$clientObj->SvcClient->__getLastResponseHeaders();
		die('Error creating client in machpanel. Details: '.$errMsg);
	}
}

function hook_ClientEdit($params)
{
	try
	{
		$customer = Array();
		$clientObj = new MachPanelClientHandler();
		$customerObject = $clientObj->createCustomerObject($params);
		
		$customerSoapObject = new SoapVar($customerObject, // the object to wrap
									SOAP_ENC_OBJECT,
									get_class($customerObject), // the concrete class name
									'http://machsol.com/' // namespace that proxy classes belong to (in WSDL)
									);	
									
		$customer['C'] = $customerSoapObject;
		$customer['CustomerId'] = '';
    

		$RespArgs = $clientObj->SvcClient->UpdateCustomer($customer)->UpdateCustomerResult;
		
		if(!$RespArgs->ResponseStatus)
		{
			#echo "Error updating client in machpanel: ".$RespArgs->ResponseMessage;
			return "Error updating client in machpanel: ".$RespArgs->ResponseMessage;
		}
		else
		{
			return "success";
		}
	}
	catch(Exception $sf)
	{
		$errMsg = 'Error: '.$sf->getMessage();
		$errMsg = $errMsg.'<br /><b>Request:</b> '.$clientObj->SvcClient->__getLastRequest();
		$errMsg = $errMsg.'<br /><b>Response:</b> '.$clientObj->SvcClient->__getLastResponse();
		$errMsg = $errMsg.'<br /><b>Request Header:</b> '.$clientObj->SvcClient->__getLastRequestHeaders();
		$errMsg = $errMsg.'<br /><b>Response Header:</b> '.$clientObj->SvcClient->__getLastResponseHeaders();
		return 'Error updating client in machpanel. Details: '.$errMsg;
	}
}

function hook_ClientChangePassword($params)
{
	try
	{
		$clientObj = new MachPanelClientHandler();

		$RespArgs = $clientObj->SvcClient->ResetWHMCSUserPassword($params)->ResetWHMCSUserPasswordResult;
		
		if(!$RespArgs->ResponseStatus)
		{
			#echo "Error updating client password in machpanel: ".$RespArgs->ResponseMessage;
			return "Error updating client password in machpanel: ".$RespArgs->ResponseMessage;
		}
		else
		{
			return "success";
		}
	}
	catch(Exception $sf)
	{
		$errMsg = 'Error: '.$sf->getMessage();
		$errMsg = $errMsg.'<br /><b>Request:</b> '.$clientObj->SvcClient->__getLastRequest();
		$errMsg = $errMsg.'<br /><b>Response:</b> '.$clientObj->SvcClient->__getLastResponse();
		$errMsg = $errMsg.'<br /><b>Request Header:</b> '.$clientObj->SvcClient->__getLastRequestHeaders();
		$errMsg = $errMsg.'<br /><b>Response Header:</b> '.$clientObj->SvcClient->__getLastResponseHeaders();
		return 'Error updating client password in machpanel. Details: '.$errMsg;
	}
}

function hook_ClientDelete($params)
{
	try
	{
		$clientObj = new MachPanelClientHandler();

		$RespArgs = $clientObj->SvcClient->DeleteWHMCSUser($params)->DeleteWHMCSUserResult;
		
		if(!$RespArgs->ResponseStatus)
		{
			#echo "Error deleting client from machpanel: ".$RespArgs->ResponseMessage;
			return "Error deleting client from machpanel: ".$RespArgs->ResponseMessage;
		}
		else
		{
			return "success";
		}
	}
	catch(Exception $sf)
	{
		$errMsg = 'Error: '.$sf->getMessage();
		$errMsg = $errMsg.'<br /><b>Request:</b> '.$clientObj->SvcClient->__getLastRequest();
		$errMsg = $errMsg.'<br /><b>Response:</b> '.$clientObj->SvcClient->__getLastResponse();
		$errMsg = $errMsg.'<br /><b>Request Header:</b> '.$clientObj->SvcClient->__getLastRequestHeaders();
		$errMsg = $errMsg.'<br /><b>Response Header:</b> '.$clientObj->SvcClient->__getLastResponseHeaders();
		return 'Error deleting client from machpanel. Details: '.$errMsg;
	}
}

add_hook("ClientAdd",1,"hook_ClientAdd");
add_hook("ClientEdit",1,"hook_ClientEdit");
add_hook("ClientChangePassword",1,"hook_ClientChangePassword");
add_hook("ClientDelete",1,"hook_ClientDelete");

function hook_AddonActivation($params)
{
	try
	{
		$clientObj = new MachPanelClientHandler();

		$RespArgs = $clientObj->SvcClient->SubscribeWHMCSAddOn($params)->SubscribeWHMCSAddOnResult;
		
		if(!$RespArgs->ResponseStatus)
		{
			#echo "Error activating/subscribing addon in machpanel: ".$RespArgs->ResponseMessage;
			return "Error activating/subscribing addon in machpanel: ".$RespArgs->ResponseMessage;
		}
		else
		{
			return "success";
		}
	}
	catch(Exception $sf)
	{
		$errMsg = 'Error: '.$sf->getMessage();
		$errMsg = $errMsg.'<br /><b>Request:</b> '.$clientObj->SvcClient->__getLastRequest();
		$errMsg = $errMsg.'<br /><b>Response:</b> '.$clientObj->SvcClient->__getLastResponse();
		$errMsg = $errMsg.'<br /><b>Request Header:</b> '.$clientObj->SvcClient->__getLastRequestHeaders();
		$errMsg = $errMsg.'<br /><b>Response Header:</b> '.$clientObj->SvcClient->__getLastResponseHeaders();
		return 'Error activating/subscribing addon in machpanel. Details: '.$errMsg;
	}
}

function hook_AddonCancelled($params)
{
	try
	{
		$clientObj = new MachPanelClientHandler();

		$RespArgs = $clientObj->SvcClient->CancelWHMCSAddOn($params)->CancelWHMCSAddOnResult;
		
		if(!$RespArgs->ResponseStatus)
		{
			#echo "Error cancelling addon in machpanel: ".$RespArgs->ResponseMessage;
			return "Error cancelling addon in machpanel: ".$RespArgs->ResponseMessage;
		}
		else
		{
			return "success";
		}
	}
	catch(Exception $sf)
	{
		$errMsg = 'Error: '.$sf->getMessage();
		$errMsg = $errMsg.'<br /><b>Request:</b> '.$clientObj->SvcClient->__getLastRequest();
		$errMsg = $errMsg.'<br /><b>Response:</b> '.$clientObj->SvcClient->__getLastResponse();
		$errMsg = $errMsg.'<br /><b>Request Header:</b> '.$clientObj->SvcClient->__getLastRequestHeaders();
		$errMsg = $errMsg.'<br /><b>Response Header:</b> '.$clientObj->SvcClient->__getLastResponseHeaders();
		return 'Error cancelling addon in machpanel. Details: '.$errMsg;
	}
}

function hook_AddonDeleted($params)
{
	try
	{
		$clientObj = new MachPanelClientHandler();

		$RespArgs = $clientObj->SvcClient->DeleteWHMCSAddOn($params)->DeleteWHMCSAddOnResult;
		
		if(!$RespArgs->ResponseStatus)
		{
			#echo "Error deleting addon from machpanel: ".$RespArgs->ResponseMessage;
			return "Error deleting addon from machpanel: ".$RespArgs->ResponseMessage;
		}
		else
		{
			return "success";
		}
	}
	catch(Exception $sf)
	{
		$errMsg = 'Error: '.$sf->getMessage();
		$errMsg = $errMsg.'<br /><b>Request:</b> '.$clientObj->SvcClient->__getLastRequest();
		$errMsg = $errMsg.'<br /><b>Response:</b> '.$clientObj->SvcClient->__getLastResponse();
		$errMsg = $errMsg.'<br /><b>Request Header:</b> '.$clientObj->SvcClient->__getLastRequestHeaders();
		$errMsg = $errMsg.'<br /><b>Response Header:</b> '.$clientObj->SvcClient->__getLastResponseHeaders();
		return 'Error deleting addon from machpanel. Details: '.$errMsg;
	}
}

function hook_ContactAdd($vars)
{
	try
	{
		$clientObj = new MachPanelClientHandler();
    $params = Array();
    $params['params'] = $vars;
		$RespArgs = $clientObj->SvcClient->AddWHMCSContact($params)->AddWHMCSContactResult;
		
		if(!$RespArgs->ResponseStatus)
		{
			#echo "Error creating contact in machpanel: ".$RespArgs->ResponseMessage;
			return "Error creating contact in machpanel: ".$RespArgs->ResponseMessage;
		}
		else
		{
			return "success";
		}
	}
	catch(Exception $sf)
	{
		$errMsg = 'Error: '.$sf->getMessage();
		$errMsg = $errMsg.'<br /><b>Request:</b> '.$clientObj->SvcClient->__getLastRequest();
		$errMsg = $errMsg.'<br /><b>Response:</b> '.$clientObj->SvcClient->__getLastResponse();
		$errMsg = $errMsg.'<br /><b>Request Header:</b> '.$clientObj->SvcClient->__getLastRequestHeaders();
		$errMsg = $errMsg.'<br /><b>Response Header:</b> '.$clientObj->SvcClient->__getLastResponseHeaders();
		return 'Error creating contact in machpanel. Details: '.$errMsg;
	}
}

function hook_ContactChangePassword($vars)
{
	try
	{
		$clientObj = new MachPanelClientHandler();
    $params = Array();
    $params['params'] = $vars;
		$RespArgs = $clientObj->SvcClient->UpdateWHMCSContactChangePassword($params)->UpdateWHMCSContactChangePasswordResult;
		
		if(!$RespArgs->ResponseStatus)
		{
			#echo "Error updating contact password in machpanel: ".$RespArgs->ResponseMessage;
			return "Error updating contact password in machpanel: ".$RespArgs->ResponseMessage;
		}
		else
		{
			return "success";
		}
	}
	catch(Exception $sf)
	{
		$errMsg = 'Error: '.$sf->getMessage();
		$errMsg = $errMsg.'<br /><b>Request:</b> '.$clientObj->SvcClient->__getLastRequest();
		$errMsg = $errMsg.'<br /><b>Response:</b> '.$clientObj->SvcClient->__getLastResponse();
		$errMsg = $errMsg.'<br /><b>Request Header:</b> '.$clientObj->SvcClient->__getLastRequestHeaders();
		$errMsg = $errMsg.'<br /><b>Response Header:</b> '.$clientObj->SvcClient->__getLastResponseHeaders();
		return 'Error updating contact password in machpanel. Details: '.$errMsg;
	}
}

function hook_ContactDelete($vars)
{
	try
	{
		$clientObj = new MachPanelClientHandler();
    $params = Array();
    $params['params'] = $vars;
		$RespArgs = $clientObj->SvcClient->DeleteWHMCSContact($params)->DeleteWHMCSContactResult;
		
		if(!$RespArgs->ResponseStatus)
		{
			#echo "Error deleting contact from machpanel: ".$RespArgs->ResponseMessage;
			return "Error deleting contact from machpanel: ".$RespArgs->ResponseMessage;
		}
		else
		{
			return "success";
		}
	}
	catch(Exception $sf)
	{
		$errMsg = 'Error: '.$sf->getMessage();
		$errMsg = $errMsg.'<br /><b>Request:</b> '.$clientObj->SvcClient->__getLastRequest();
		$errMsg = $errMsg.'<br /><b>Response:</b> '.$clientObj->SvcClient->__getLastResponse();
		$errMsg = $errMsg.'<br /><b>Request Header:</b> '.$clientObj->SvcClient->__getLastRequestHeaders();
		$errMsg = $errMsg.'<br /><b>Response Header:</b> '.$clientObj->SvcClient->__getLastResponseHeaders();
		return 'Error deleting contact from machpanel. Details: '.$errMsg;
	}
}

function hook_ContactDetailsValidation($vars)
{
	try
	{
		$clientObj = new MachPanelClientHandler();
		$params = Array();
		$params['params'] = $vars;
		$errorMsgs = $clientObj->SvcClient->ValidateWHMCSContact($params)->ValidateWHMCSContactResult;
		if(!empty($errorMsgs) && !empty(join("", $errorMsgs)))
			return join(",", $errorMsgs);
		else
			return "";
	}
	catch(Exception $sf)
	{
		$errMsg = 'Error: '.$sf->getMessage();
		$errMsg = $errMsg.'<br /><b>Request:</b> '.$clientObj->SvcClient->__getLastRequest();
		$errMsg = $errMsg.'<br /><b>Response:</b> '.$clientObj->SvcClient->__getLastResponse();
		$errMsg = $errMsg.'<br /><b>Request Header:</b> '.$clientObj->SvcClient->__getLastRequestHeaders();
		$errMsg = $errMsg.'<br /><b>Response Header:</b> '.$clientObj->SvcClient->__getLastResponseHeaders();
    throw new Exception('Soap Fault in submitting the contact validation request. Details: '.$errMsg);
	}
}

function hook_ContactEdit($vars)
{
	try
	{
		$clientObj = new MachPanelClientHandler();
    $params = Array();
    $params['params'] = $vars;
		$RespArgs = $clientObj->SvcClient->EditWHMCSContact($params)->EditWHMCSContactResult;
		
		if(!$RespArgs->ResponseStatus)
		{
			#echo "Error updating contact in machpanel: ".$RespArgs->ResponseMessage;
			return "Error updating contact in machpanel: ".$RespArgs->ResponseMessage;
		}
		else
		{
			return "success";
		}
	}
	catch(Exception $sf)
	{
		$errMsg = 'Error: '.$sf->getMessage();
		$errMsg = $errMsg.'<br /><b>Request:</b> '.$clientObj->SvcClient->__getLastRequest();
		$errMsg = $errMsg.'<br /><b>Response:</b> '.$clientObj->SvcClient->__getLastResponse();
		$errMsg = $errMsg.'<br /><b>Request Header:</b> '.$clientObj->SvcClient->__getLastRequestHeaders();
		$errMsg = $errMsg.'<br /><b>Response Header:</b> '.$clientObj->SvcClient->__getLastResponseHeaders();
		throw new Exception('Soap Fault in submitting the edit contact request. Details: '.$errMsg);
	}
}


function hook_UserChangePassword($vars)
{
	try
	{
		$clientObj = new MachPanelClientHandler();
		$clientid = $clientObj->getUserOwnerClientId($vars['userid']);
		if($clientid > 0)
		{
		    $params = array();
			$params['userid'] = $clientid;
			$params['password'] = $vars['password'];

			$RespArgs = $clientObj->SvcClient->ResetWHMCSUserPassword($params)->ResetWHMCSUserPasswordResult;
		
			if(!$RespArgs->ResponseStatus)
			{
				#echo "Error updating user password in machpanel: ".$RespArgs->ResponseMessage;
				return "Error updating user password in machpanel: ".$RespArgs->ResponseMessage;
			}
			else
			{
				return "success";
			}
		}
	}
	catch(Exception $sf)
	{
		$errMsg = 'Error: '.$sf->getMessage();
		#echo($errMsg);
		throw new Exception($errMsg);
	}
}

add_hook("UserChangePassword",1, "hook_UserChangePassword");

add_hook('ContactEdit', 1, "hook_ContactEdit");
add_hook('ContactDetailsValidation', 1, "hook_ContactDetailsValidation");
add_hook('ContactDelete', 1, "hook_ContactDelete");
add_hook('ContactChangePassword', 1, "hook_ContactChangePassword");
add_hook('ContactAdd', 1, "hook_ContactAdd");
add_hook("AddonActivation",1,"hook_AddonActivation");
add_hook("AddonAdd",1,"hook_AddonActivation"); 
//add_hook("AddonCancelled",1,"hook_AddonCancelled");
//add_hook("AddonTerminated",1,"hook_AddonCancelled");
add_hook("AddonDeleted",1,"hook_AddonDeleted");

?>