<?php 

function ipmi_client_area($params){
	
	if($params['status']!="Active") return false;
	global $_LANG;


	$serviceid = $params['serviceid'];
    $customData = ipmi_GetCustomIpmi($serviceid);

    $ipmi_id = $customData['ipmi_id'];
	if($_GET['status']=="ipmiPowerSync"){
		# 调用api
		$getDetailFlowData = ipmi_req('ipmiPowerSync', array("id"=>$ipmi_id), $params, 60);
		echo json_encode($getDetailFlowData);exit;
	}
    $hosting = getHosting($serviceid);
    if (empty($ipmi_id) || $hosting['domainstatus'] != "Active") {
        return false;
    }
    if($_GET['traffic'] == 'true' || $_POST['check_reinstall'] == "true" || $_GET['ipmi_cancellReinstall'] == "true"){
        return false;
    }
    $ipmi_panel_pass = $customData['value3'];
    $template = 'six';  //模板
    $params['serversecure'] = $hosting['secure'];
    $params['serverip'] = $hosting['ipaddress'];
    $params['serverusername'] = $hosting['username'];
    $params['serverpassword'] = decrypt($hosting['password']);
    $params['serverport'] = $hosting['serverport'];
    $params['client'] = $params['clientsdetails']['email'];  //客户邮箱
    $accesshash = serverAccessHash($hosting['server'], $hosting['accesshash']); 
    $userid = $params['userid'];  //用户id

    $params['configoption1'] = $hosting['configoption1'];

    $bwt = isset($params['configoptions']['bwt']);
    if ($_POST['a']) {
        $sms_check_on = ipmi_checkSms();
        $a = $_POST['a'];
        if ($a == "reboot") {  //重启
        } else if ($a == "on") {  //开机
        } else if ($a == "off") {  //关机
        } else if ($a == "resetbmc") {   //重置BMC
        } else if ($a == "kvm") {  //kvm
            if($accesshash['kvm'] == "on"){
                if(!$sms_check_on && in_array('kvm', $accesshash['sms_check'])){
                    $res['status'] = 'nocheck';
                    $res['msg'] = $_LANG['ipmi_client_code_error'] ;
                    echo json_encode($res);exit;
                }
                $res = ipmi_kvm($params);
                if($res['status'] == 'success'){
                    $res['url'] = 'clientarea.php?action=productdetails&id='.$serviceid.'&download=true&java=true&name='.$res['filename'];
                    unset($res['name']);
                }else{
                    $res['msg'] = $_LANG['ipmi_client_ikvm_down_error'];
                }
                echo json_encode($res);exit;
            }else{
                if ($template == "IDCSMART") {
                    exit(json_encode(array("status" => "error", "msg" => $_LANG['ipmi_client_kvm_off'] )));
                }
            }
        } else if ($a == "ikvm") {  //ikvm
            if($accesshash['ikvm'] == "on"){
                if(!$sms_check_on && in_array('ikvm', $accesshash['sms_check'])){
                    $res['status'] = 'nocheck';
                    $res['msg'] = $_LANG['ipmi_client_code_error'];
                    echo json_encode($res);exit;
                }
                $res = ipmi_ikvm($params);
                if($res['status'] == 'success'){
                    $res['url'] = 'clientarea.php?action=productdetails&id='.$serviceid.'&download=true&java=true&name='.$res['filename'];
                    unset($res['name']);
                }else{
                    $res['msg'] = $_LANG['ipmi_client_ikvm_down_error'];
                }
                echo json_encode($res);exit;
            }else{
                if ($template == "IDCSMART") {
                    exit(json_encode(array("status" => "error", "msg" => $_LANG['ipmi_client_ikvm_off'] )));
                }
            }
        } 
    }
    if ($hosting['configoption1'] == "机柜租用") {
        $title = $hosting['assignedips'];
        $hosting['assignedips'] = str_replace("\r\n", '<br>', $hosting['assignedips']);
        $is_mc = ipmi_checkTemplateIsMcSpace();
        return array(
            'templatefile' => 'templates/clientarea2.tpl',
            'vars'         => array(
                'accesshash' => $accesshash,
                'serviceid' => $serviceid,
                'title' => $title,
                'assignedips' => $hosting['assignedips'],
                'is_mc'=>ipmi_checkTemplateIsMcSpace()
            ),
        );
    } else {
        $responses = ipmi_get_adminAreaData($params);

        $ipmi_statusarr = $responses['ipmiPowerSync'];// ipmi_GetPowerStatus($params);
        $reinstallStatus = $responses['reinstallStatus']; //ipmi_reinstallStatus($params); 
        //$idc_status = $ipmi_statusarr['msg'];
        $idc_status = '';

        if (!empty($reinstallStatus['data'])) {
            if($reinstallStatus['data']['windows_finish']){
                $idc_status .= "<span class='reload_status'> Windows安装完成,正在初次重启自动配置中，请稍候尝试远程桌面连接</span>";
            }else{
                if($reinstallStatus['data']['task_type'] == 3){
                    $info = $_LANG['ipmi_admin_area_reinstall_getinfo1'] ;
                    $cancel = $_LANG['ipmi_admin_area_reinstall_cancelinfo1'] ;
                    $span = '';
                }else if($reinstallStatus['data']['task_type'] == 2){
                    $info = $_LANG['ipmi_admin_area_reinstall_getinfo2'];
                    $cancel = $_LANG['ipmi_admin_area_reinstall_cancelinfo2'];
                    $span = '';
                }else if($reinstallStatus['data']['task_type'] == 1){
                    $info = $_LANG['ipmi_admin_area_reinstall_getinfo3'];
                    $cancel = $_LANG['ipmi_admin_area_reinstall_cancelinfo3'];
                    $span = '';
                }else{
                    $info = $_LANG['ipmi_admin_area_reinstall_getinfo4'] ;
                    $cancel = $_LANG['ipmi_admin_area_reinstall_cancelinfo4'];
                    $part_info = [$_LANG['ipmi_admin_area_reinstall_part_info1'], $_LANG['ipmi_admin_area_reinstall_part_info2'] ];
					$span = $reinstallStatus['data']['name'].','.$_LANG['ipmi_admin_area_reinstall_info1'] .'：disk'.$reinstallStatus['data']['disk'].','.$_LANG['ipmi_admin_area_reinstall_info2'].':'.$part_info[$reinstallStatus['data']['part_type']];
				}
                $idc_status .= "<span class='reload_status'><span style='color:red;'> {$info}：</span>".$span."  ".$_LANG['ipmi_admin_area_reinstall_info3'] ."：".$reinstallStatus['data']['progress']."% ".$reinstallStatus['data']['step']."</span>";
            }
        } 
        //$ipmi_statusarr['msg'] = $idc_status;
        $server_detail = $responses['detail'] ;//ipmi_Detail($params);
        $detail = $server_detail['server'];
        $root = $hosting['operate_user'];
        $rootpass = $detail['ospassword']; //decrypt($hosting['operate_password']);
        $pos = strrpos($detail['crack_success_info'], '为');
        if($pos !== false){
            $new_pass = str_replace('为', '', substr($detail['crack_success_info'], $pos));
            $detail['crack_success_info'] = substr($detail['crack_success_info'], 0, $pos).'为';
        }else{
            $new_pass = '';
        }
    }
    $sms_check_on = mysql_fetch_assoc(full_query("SELECT * FROM tbladdonmodules WHERE setting='access' AND module='idcsmart_message'"));
    if(!empty($sms_check_on)){
        $phoneBound = true;
        $clientinfo = mysql_fetch_assoc(full_query("SELECT phonenumber from tblclients where id='{$_SESSION['uid']}'"));
        if (!$clientinfo['phonenumber']) {
            $phoneBound = false;
        }
        $psql = 'select * from idcsmart_notification_verifyphone where uid='.intval($_SESSION['uid']).';';
        $phoneNotify = mysql_fetch_assoc(full_query($psql));
        if ((!$phoneNotify)||$phoneNotify['phoneVerify']!=0) {
            $phoneBound = false;
        }
    }
    $need_phone_check = ipmi_checkSms();
    // 获取流量包
    $flowpacket = ipmi_GetFlowPack($params);
    // 是否显示用量图
    $show_traffic_usage = ipmi_showTrafficUsage();
    // 获取用量信息
    $traffic_use = ipmi_getTrafficUsageTable($params['serviceid'], $userid, $responses['traffic']);
    // 
	//获取重装状态
	$userid = $params['clientsdetails']['userid'];
	$params["url"] = '?userid='.$userid.'&id='.$serviceid."&ipmi_cancellReinstall=true";
	//获取操作系统
	$os = ipmi_os($params);
	foreach ($os['os'] as $o) { 
		if(strpos($o['name'],"救援系统")===false)$os_option .= '<option value="'.$o['id'].'">'.$o['name'].'</option>';
	}
	$rescue_option = '<option value="0">'.$_LANG['ipmi_layer_content_rescue'].'</option>';
	foreach($os['rescue'] as $v){
		$rescue_option .= '<option value="'.$v['id'].'">'.$v['name'].'</option>';
	}
	$os_config = (is_array($os['config']) && count($os['config'])>0) ? json_encode($os['config']) : '""';
	if($accesshash['custompartition'] == 'on'){
		$custompartition = '<label>'.$_LANG['ipmi_layer_content_os_partition_field'].' <input type="radio" name="ipmiaction" value="2"></label>';
	}else{
		$custompartition = '';
	}
	if($_GET['tpl']!="index"){
		
		return array(
		'templatefile' => 'templates/clientarea_iframe.tpl',
			'templateVariables' => array(
				'id' => $params['serviceid'],
			),
		);
		
	}
	//getDetailFlowData
    # 调用api
    $getDetailFlowData = ipmi_req('getDetailFlowData', array("id"=>$ipmi_id), $params, 60);
	global $_LANG;
	$ipmiDetail['hardware']=$detail['wltag'];
	$ipmiDetail['osname']=$detail['osname'];
	foreach($server_detail['ip']['subnet_ip'] as $ip_k=>$ip){
		if($ip['server_mainip']=="true"){
			$ipmiDetail['ip']['name']=$ip['name'];
		}else{
			$ipmiDetail['ip']['ipaddress'][$ip_k]=$ip['ipaddress'];
			if($ip['detail']) $ipmiDetail['ip']['iplist'][$ip_k]=$ip['detail'];
		}		
	}
	$ipmiDetail['ip']['zhuip']=$detail['zhuip'];
	$ipmiDetail['ip']['gateway']=$server_detail['ip']['gateway'];
	$ipmiDetail['ip']['subnetmask']=$server_detail['ip']['subnetmask'];
		$web_root = DI::make("asset")->getWebRoot();
        $serverDetailed=array(
            'accesshash' => $accesshash,
            'serviceid' => $serviceid,
            'userid' => $userid,
            'root' => $root,
            'rootpass' => $rootpass,
            'ipmi_statusarr' => $ipmi_statusarr,
            'reinstallStatus' => $reinstallStatus,
            'ipmi_panel_pass' => $ipmi_panel_pass,
            'idc_status' => $idc_status,
            'port' => $detail['port'],
            'phoneBound'=>$phoneBound,
            'need_phone_check'=>$need_phone_check,
            'disk_num'=>$detail['disk_num'],
            'crack_success_info'=>$detail['crack_success_info'],
            'has_switch'=>!empty($server_detail['switch']),
            'new_pass'=>$new_pass,
            'flowpacket'=>$flowpacket,
            'show_traffic_usage'=>$show_traffic_usage,
            'traffic_use'=>$traffic_use,
            'bwt'=>$bwt,
            'bwusage'=>$hosting['bwusage'],
            'bwlimit'=>$hosting['bwlimit'],
            'ipmiDetail'=>$ipmiDetail,
            'detailFlowData'=>$getDetailFlowData,
            'os_option'=>$os_option,
            'os_config'=>$os_config,
            'rescue_option'=>$rescue_option,
            'custompartition'=>$custompartition,
            'token'=>$params['token'],
            'telephoneNumber'=>$params['clientsdetails']['telephoneNumber'],
        );
	include IPMI_FILE ."templates/index.php";exit;
	
}