<?php

# 产品开通后
add_hook('AfterModuleCreate', 1, function($vars) {
	$id = $vars['serviceid'];
    upstreamSyncHost($id, 'module_create');

});
# 产品暂停后
add_hook('AfterModuleSuspend', 1, function($vars) {
    $id = $vars['serviceid'];
    upstreamSyncHost($id, 'module_suspend');

});
# 产品删除后
add_hook('AfterModuleTerminate', 1, function($vars) {
    $id = $vars['serviceid'];
    upstreamSyncHost($id, 'module_terminate');

});
# 产品解除暂停后
add_hook('AfterModuleUnsuspend', 1, function($vars) {
    $id = $vars['serviceid'];
    upstreamSyncHost($id, 'module_unsuspend');

});
# 产品升降级后
add_hook('AfterModuleChangePackage', 1, function($vars) {
    $id = $vars['serviceid'];
    upstreamSyncHost($id, 'update_host');

});
# 产品修改密码后
add_hook('AfterModuleChangePassword', 1, function($vars) {
    $id = $vars['serviceid'];
    upstreamSyncHost($id, 'update_host');

});
# 产品删除后
add_hook('ServiceDelete', 1, function($vars) {
    $id = $vars['serviceid'];
    upstreamSyncHost($id, 'delete_host');

});	
# 产品修改后
add_hook('ServiceEdit', 1, function($vars) {
    $id = $vars['serviceid'];
    upstreamSyncHost($id, 'update_host');

});	

function upstreamSyncHost($id, $action = '')
{
	$sql = "select servertype from tblproducts where id = '$pid'";
	$host = mysql_fetch_array(full_query("select id,domainstatus,nextduedate from tblhosting where id = '$id'"));
    if(empty($host)){
        return false;
    }
    $resellerHost = mysql_fetch_array(full_query("select hosting_id,downstream_info,downstream_host_id from idcsmart_reseller_hosts where hosting_id = '$id'"));
    if(empty($resellerHost['downstream_host_id'])){
        return false;
    }
    $downstreamInfo = json_decode($resellerHost['downstream_info'], true) ?? [];
    if(empty($downstreamInfo)){
        return false;
    }
    $public_key = '';

    $statusArr = [
    	'Pending' => 'Pending',
    	'Active' => 'Active',
    	'Suspended' => 'Suspended',
    	'Terminated' => 'Deleted',
    	'Cancelled' => 'Cancelled',
    	'Fraud' => 'Failed',
    	'Completed' => 'Failed',
    ];
    $host['status'] = $statusArr[$host['domainstatus']];

    $data = json_encode(['action' => $action, 'host' => $host]);

    $crypto = '-----BEGIN PUBLIC KEY-----
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAun/Mk6ckj8sPl7tHUJwU
GLuMQjld9B1d2b6Yj7QDW+zlo/g7MURdronbS9Rlh5gMyWmujKuNRXT9pVja+aPQ
YnRMW+eUwwAMVktylDg6Dr1+oLqlQ5jd3h8O7LBYQrO9OICpEGOtmi+4VkbszhMp
Fe9TvmnzEOHGYPDcRO3HtCsKw+brqiF6NFujq3V4KtbeOltjiVOQCmxhIR/7x5/Q
zjTpZiFp8BV42qIjr5fbpc2UNbXuZjIbuLJk5J+/scYxf+a7kApU8c5q2bkPbOiR
qiHsI+KpLTD8prGps/+2bi1wHE0zCH1nkLZ++AZmdr28erhx6k79g2PXnZZTwv83
FdZ6RD9RIgPshgItahBqd8k8vsgiL9bnMJvqm374R7XKTgJbSArNKzplADT7BTco
0w8REW8bOP15QmhQD63owFIpNBKJX/crCqVycLK7fjiLPK92lu/g2NvsQLXr2D/h
DlglX5OoOplaNLxBuRiQIhtZg/siWx2JWylVlz+VnUZWsO3rF5hx6wMGAK51jOlN
DXsbbpyLXKP7/MHMAiRyiUw0ZWT5GWmkGiUu6ub8Zwme2IKV44d23iVuF5H+Nesb
hd3nZ3ULKymlUt+H+bR8WTLBQKT2GCOzYLADvmNdYlRrD2/Q777zM9mJ58tEUI1m
uqKyDOVeDS6xsGZEqVVt97UCAwEAAQ==
-----END PUBLIC KEY-----';

    foreach (str_split($data, 117) as $chunk) {

        openssl_public_encrypt($chunk, $encryptData, $api['public_key']);

        $crypto .= $encryptData;
    }

    $data = base64_encode($crypto);

    $res = upstreamSyncCurl(rtrim($downstreamInfo['url'],'/').'/console/v1/upstream/sync', ['host_id' => $resellerHost['downstream_host_id'], 'data' => $data], 30, 'POST');

    return true;
}

function upstreamSyncCurl($url, $data = [], $timeout = 30, $request = 'POST', $header = [])
{
    $curl = curl_init();
    $request = strtoupper($request);

    if($request == 'GET'){
        $s = '';
        if(!empty($data)){
            foreach($data as $k=>$v){
                if(empty($v)){
                    $data[$k] = '';
                }
            }
            $s = http_build_query($data);
        }
        if(strpos($url, '?') !== false){
            if($s){
                $s = '&'.$s;
            }
        }else{
            if($s){
                $s = '?'.$s;
            }
        }
        curl_setopt($curl, CURLOPT_URL, $url.$s);
    }else{
        curl_setopt($curl, CURLOPT_URL, $url);
    }
    curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    if($request == 'GET'){
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HTTPGET, 1);
    }
    if($request == 'POST'){
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POST, 1);
        if(is_array($data)){
            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
        }else{
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
    }
    if($request == 'PUT' || $request == 'DELETE'){
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $request);
        if(is_array($data)){
            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
        }else{
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
    }
    if(!empty($header)){
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    }
    $content = curl_exec($curl);
    $error = curl_error($curl);
    $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
	curl_close($curl);
	return ['http_code'=>$http_code, 'error'=>$error , 'content' => $content];
}
?>