<?php
/*
WHMCS MachPanel Integration Package: v 4.4
Supported MachPanel version: 6.3.30 Or Later
Last modified: 20-Jan-2021


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 below V5.0, place this file in WHMCSroot\includes\hooks\ folder.

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

function hook_mp_hooks_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 "Request ended with message: ".$RespArgs->ResponseMessage;
			return "Request ended with message: ".$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 order. Details: '.$errMsg);
	}
}

function hook_mp_hooks_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)
		{
			return "Request ended with message: ".$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 order. Details: '.$errMsg);
	}
}

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

		$RespArgs = $clientObj->SvcClient->ResetWHMCSUserPassword($params)->ResetWHMCSUserPasswordResult;
		
		if(!$RespArgs->ResponseStatus)
		{
			return "Request ended with message: ".$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 order. Details: '.$errMsg);
	}
}

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

		$RespArgs = $clientObj->SvcClient->DeleteWHMCSUser($params)->DeleteWHMCSUserResult;
		
		if(!$RespArgs->ResponseStatus)
		{
			return "Request ended with message: ".$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 order. Details: '.$errMsg);
	}
}

add_hook("ClientAdd",1,"hook_mp_hooks_ClientAdd");
add_hook("ClientEdit",1,"hook_mp_hooks_ClientEdit");
add_hook("ClientChangePassword",1,"hook_mp_hooks_ClientChangePassword");
add_hook("ClientDelete",1,"hook_mp_hooks_ClientDelete");

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

		$RespArgs = $clientObj->SvcClient->SubscribeWHMCSAddOn($params)->SubscribeWHMCSAddOnResult;
		
		if(!$RespArgs->ResponseStatus)
		{
			return "Request ended with message: ".$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 order. Details: '.$errMsg);
	}
}

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

		$RespArgs = $clientObj->SvcClient->CancelWHMCSAddOn($params)->CancelWHMCSAddOnResult;
		
		if(!$RespArgs->ResponseStatus)
		{
			return "Request ended with message: ".$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 order. Details: '.$errMsg);
	}
}

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

		$RespArgs = $clientObj->SvcClient->DeleteWHMCSAddOn($params)->DeleteWHMCSAddOnResult;
		
		if(!$RespArgs->ResponseStatus)
		{
			return "Request ended with message: ".$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 order. Details: '.$errMsg);
	}
}

function hook_mp_hooks_ContactAdd($vars)
{
	try
	{
		$clientObj = new MachPanelClientHandler();
    $params = Array();
    $params['params'] = $vars;
		$RespArgs = $clientObj->SvcClient->AddWHMCSContact($params)->AddWHMCSContactResult;
		
		if(!$RespArgs->ResponseStatus)
		{
			return "Request ended with message: ".$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 contact add request. Details: '.$errMsg);
	}
}

function hook_mp_hooks_ContactChangePassword($vars)
{
	try
	{
		$clientObj = new MachPanelClientHandler();
    $params = Array();
    $params['params'] = $vars;
		$RespArgs = $clientObj->SvcClient->UpdateWHMCSContactChangePassword($params)->UpdateWHMCSContactChangePasswordResult;
		
		if(!$RespArgs->ResponseStatus)
		{
			return "Request ended with message: ".$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 contact change password request. Details: '.$errMsg);
	}
}

function hook_mp_hooks_ContactDelete($vars)
{
	try
	{
		$clientObj = new MachPanelClientHandler();
    $params = Array();
    $params['params'] = $vars;
		$RespArgs = $clientObj->SvcClient->DeleteWHMCSContact($params)->DeleteWHMCSContactResult;
		
		if(!$RespArgs->ResponseStatus)
		{
			return "Request ended with message: ".$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 delete contact request. Details: '.$errMsg);
	}
}

function hook_mp_hooks_ContactDetailsValidation($vars)
{
	try
	{
		$clientObj = new MachPanelClientHandler();
    $params = Array();
    $params['params'] = $vars;
		$errorMsgs = $clientObj->SvcClient->ValidateWHMCSContact($params)->ValidateWHMCSContactResult;
		
		return $errorMsgs;
	}
	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_mp_hooks_ContactEdit($vars)
{
	try
	{
		$clientObj = new MachPanelClientHandler();
    $params = Array();
    $params['params'] = $vars;
		$RespArgs = $clientObj->SvcClient->EditWHMCSContact($params)->EditWHMCSContactResult;
		
		if(!$RespArgs->ResponseStatus)
		{
			return "Request ended with message: ".$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_mp_hooks_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)
			{
				return "Request ended with message: ".$RespArgs->ResponseMessage;
			}
			else
			{
				return "success";
			}
		}
	}
	catch(Exception $sf)
	{
		$errMsg = 'Error: '.$sf->getMessage();
		echo($errMsg);
		throw new Exception($errMsg);
	}
}

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

add_hook('ContactEdit', 1, "hook_mp_hooks_ContactEdit");
add_hook('ContactDetailsValidation', 1, "hook_mp_hooks_ContactDetailsValidation");
add_hook('ContactDelete', 1, "hook_mp_hooks_ContactDelete");
add_hook('ContactChangePassword', 1, "hook_mp_hooks_ContactChangePassword");
add_hook('ContactAdd', 1, "hook_mp_hooks_ContactAdd");
add_hook("AddonActivation",1,"hook_mp_hooks_AddonActivation");
add_hook("AddonAdd",1,"hook_mp_hooks_AddonActivation"); 
//add_hook("AddonCancelled",1,"hook_mp_hooks_AddonCancelled");
//add_hook("AddonTerminated",1,"hook_mp_hooks_AddonCancelled");
add_hook("AddonDeleted",1,"hook_mp_hooks_AddonDeleted");
?>