<?php

//开通
function ipmi_create($params){
	global $_LANG;
	$serviceid = $params['serviceid'];
    $ipmiType = $params['configoption1'];
    $customData = ipmi_GetCustomIpmi($serviceid);
    $ipmi_id = $customData['ipmi_id'];
    if( !empty($ipmi_id) ) {
        return $_LANG['ipmi_system_create_error1'];
    }
    if('机柜租用' == $ipmiType) {
        return $_LANG['ipmi_system_create_error2'];
    }
    $hosting = getHosting($serviceid);
    $accesshash = serverAccessHash($hosting['server'], $hosting['accesshash']);
    $user_info = mysql_fetch_assoc(full_query("select id,email from tblclients where id='{$params["userid"]}'"));
	if($accesshash['post_emailorid_pre']){
		$post_emailorid_pre= ($accesshash['post_emailorid_pre'])?$accesshash['post_emailorid_pre']."_":'';
	}
    $postfields['user_id'] = ($accesshash['post_emailorid'] == "email") ? $user_info['email'] : $params['userid'];
	$postfields['user_id']=$post_emailorid_pre.$postfields['user_id'];
    $postfields['server_group'] = $params['configoptions']['server_group'];
    $postfields['hostid'] = $serviceid;
    $ip_num = $params['configoptions']['ip_num'];
    if(is_numeric($ip_num) || $ip_num == 'NO_CHANGE'){
        $postfields['ip_num'] = $params['configoptions']['ip_num'];
    }else{  //分组形式2_2,1_1  数量_分组id
        $ip_num = ipmi_ipnum($ip_num);
        if($ip_num === false){
            return $_LANG['ipmi_system_create_error3'];
        }
        $postfields['ip_num'] = $ip_num;
    }
    if(!empty($hosting['regdate'])){
        $postfields['starttime'] = $hosting['regdate'].' 00:00:00';
    }
    $postfields['os'] = $params['configoptions']['os'];
    //处理带宽修改参数
    $bw = $params['configoptions']['bw'];  //带宽
    if ($bw == 'NO_CHANGE') {
        $postfields['in_bw'] = 'NO_CHANGE';
        $postfields['out_bw'] = 'NO_CHANGE';
    } else {
        if (strpos($bw, ',') !== false) {
            $bw_arr = explode(',', $bw);
            $postfields['in_bw'] = $bw_arr[0];
            $postfields['out_bw'] = $bw_arr[1];
        } else {
            $postfields['in_bw'] = '';
            $postfields['out_bw'] = '';
        }
    }
    $postfields['limit_traffic'] = $params['configoptions']['bwt'];
    if (empty($postfields['server_group'])) {
        return $_LANG['ipmi_system_create_error4'];
    }
    if (empty($postfields['ip_num'])) {
        return $_LANG['ipmi_system_create_error5'];
    }
    if (empty($postfields['os'])) {
        return $_LANG['ipmi_system_create_error6'];
    }
    // dcim4.6添加到期时间传递
    $postfields['expiretime'] = $hosting['nextduedate'];
    $postfields['mscripts'] = $params['configoptions']['mscripts'] ?: 0;

    $port = isset($params['configoptions']['port']) ? $params['configoptions']['port'] : 0;
    if(!empty($port)){
        if($port === 'auto'){
            $port = mt_rand(1, 65535);
        }
        $postfields['port'] = $port;
    }

    # 调用api
    $result = ipmi_req('ipmiCreate', $postfields, $params, 180);
    if($result['status'] == 'success') {
        $data = $result['data'];
        # 添加value3 值为面板密码
        update_query('mod_custom_ipmi', array('ipmi_id' => $data['id'], 'value2'=>$data['wltag'], 'value3'=>$data['ippassword']), array('serviceid' => $serviceid));
        $arr['dedicatedip'] = $data['zhuip'];
        $arr['assignedips'] = $data['ips'];
        $arr['username'] = $data['username'];
        $arr['password'] = encrypt($data['password']);
        $arr['domainstatus'] = "Active";
        if ($accesshash['changehostname'] == 'hostname') {
            $arr['domain'] = $data['wltag'];
        }else if ($accesshash['changehostname'] == 'ip') {
            $arr['domain'] = $data['zhuip'];
        }else if ($accesshash['changehostname'] == 'hostnameip') {
            $arr['domain'] = $data['wltag'].'-'.$data['zhuip'];
        } 
        $arr['bwlimit'] = intval($data['limit_traffic']);
        $arr['bwusage'] = 0;
        $arr['lastupdate'] = 'now()';

        $where = array('id' => $serviceid);
        update_query('tblhosting', $arr, $where);
        $res = '开通成功';
        if (isset($result['ip_error'])) {
            $res .= ', '.$_LANG['ipmi_system_create_error7'].': '.$result['ip_error'];
        }
        if (isset($result['in_bw_error'])) {
            $res .= ', '.$_LANG['ipmi_system_create_error8'].': '.$result['in_bw_error'];
        }
        if (isset($result['out_bw_error'])) {
            $res .= ', '.$_LANG['ipmi_system_create_error9'].': '.$result['out_bw_error'];
        }
        if ($res == '开通成功') {
            $res = 'success';
        }
        ipmi_SetRemotePort($params, $data['port']);
    } else if ($result['status'] == 'error') {
        $res = $result['msg'];
    } else {
        $res = $_LANG['ipmi_system_create_error10'];
    }
    return $res;
}
//暂停
function ipmi_suspend($params){
    global $_LANG;
	$ipmiType = $params['configoption1'];
    $serviceid = $params['serviceid'];
    $customData = ipmi_GetCustomIpmi($serviceid);
    $ipmi_id = $customData['ipmi_id'];
    if(empty($ipmi_id)){
        return 'success'; //"该服务器还未成功创建";
    }
    if('机柜租用' ==  $ipmiType) {
        return $_LANG['ipmi_admin_cabinet_error'];
    }
    $postfields['id'] = $ipmi_id;
    $postfields['hostid'] = $serviceid;
    # 调用api
    $result = ipmi_req('ipmiSuspend', $postfields, $params, 150);
    if('success' == $result['status']){
        $res = 'success';
    }else{
        $res = $result['msg'];
    }
    return $res;	
}
// 解除暂停
function ipmi_unsuspend($params){
    global $_LANG;
	$ipmiType = $params['configoption1'];
    $serviceid = $params['serviceid'];
    $customData = ipmi_GetCustomIpmi($serviceid);
    $ipmi_id = $customData['ipmi_id'];
    if(empty($ipmi_id)){
        return 'success'; //'该服务器还未成功创建';
    }
    if('机柜租用' ==  $ipmiType) {
        return $_LANG['ipmi_admin_cabinet_error'];
    }
    $postfields['id'] = $ipmi_id;
    $postfields['hostid'] = $serviceid;
    # 调用api
    $result = ipmi_req('ipmiUnsuspend', $postfields, $params, 150);
    if('success' == $result['status']){
        $res = 'success';
    }else{
        $res = $result['msg'];
    }
    return $res;
}

// 删除
function ipmi_terminate($params){
    global $_LANG;
	$ipmiType = $params['configoption1'];
    $serviceid = $params['serviceid'];
    $customData = ipmi_GetCustomIpmi($serviceid);
    $ipmi_id = $customData['ipmi_id'];
    if(empty($ipmi_id)){
        return 'success'; //'该服务器还未成功创建';
    }
    if('机柜租用' ==  $ipmiType) {
        return $_LANG['ipmi_admin_cabinet_error'];
    }
    $postfields['id'] = $ipmi_id;
    $postfields['hostid'] = $serviceid;
    # 调用api
    $result = ipmi_req('ipmiTerminate', $postfields, $params, 150);
    if('success' == $result['status']){
        $where = array('id' => $serviceid);
        update_query("tblhosting", array('dedicatedip'=>'','assignedips'=>''), $where);
        delete_query("mod_custom_ipmi", "serviceid='{$serviceid}'"); 
        $res = 'success';
        // 删除该机器流量包
        if(ipmi_addonFlowPacket()){
            full_query("UPDATE idcsmart_flowpacket_record SET show_status=1 WHERE show_status=0 AND status=1 AND serviceid='{$serviceid}'");
        }
    }else{
        $res = $result['msg'];
    }
    return $res;
}