function graph_hooks($vars)
{
    if ($_GET['a'] != 'graph') {
        return;
    }
    $var['pid'] = ion_mcrypt($_GET['id']);
    $var['menu'] = "<a href='clientarea.php?action=productdetails&id={$_GET['id']}'>{$vars['domain']}</a>";
    $var['title'] = $vars['hostname'];
    return $var;
}
function ion_ClientArea($params)
{
    $result = select_query('ion_module', 'ion_sid,acl', array('whmcs_sid' => $params['serviceid']));
    $ion_sid = null;
    if (mysql_num_rows($result) > 0) {
        $row = mysql_fetch_array($result, MYSQL_NUM);
        $ion_sid = $row[0];
        $acl = unserialize($row[1]);
    } else {
        return;
    }
    //------------APIs
    $ip = APIClient::serverAllIPs(ION_API, array('serverID' => $ion_sid));
    if (key($ip) !== 'error') {
        $ips = '';
        foreach ($ip as $v) {
            $ips .= $v . '<br>';
        }
        if (isset($_GET['reboot'])) {
            if ($_GET['reboot'] == "success") {
                echo "<script>alert('Reboot Successful.')</script>";
            } else {
                if ($_GET['reboot'] == "failed") {
                    echo "<script>alert('Operation Failed! Please try again.')</script>";
                }
            }
        }
    } else {
        $acl['ip'] = 0;
    }
    $os = APIClient::serverOS(ION_API, array('serverID' => $ion_sid));
    if (key($os) == 'error') {
        $acl['os'] = 0;
    }
    $serverInfo = APIClient::serverInfo(ION_API, array('serverID' => $ion_sid));
    if (key($serverInfo) == 'error') {
        $acl['server_info'] = 0;
    }
    $bws = APIClient::serverBwStatics(ION_API, array('serverID' => $ion_sid, 'period' => 'current', 'title' => $params['domain']));
    if (key($bws) !== 'error') {
        $bws['method'] = $bws['method'] == '95th' ? '95th Percentile' : $bws['method'];
        $pbws = APIClient::serverBwStatics(ION_API, array('serverID' => $ion_sid, 'period' => 'prev'));
        $pbws['method'] = $pbws['method'] == '95th' ? '95th Percentile' : $pbws['method'];
        $bws['95th_outbound'] = formatBytes($bws['95th_outbound']);
        $pbws['95th_outbound'] = formatBytes($pbws['95th_outbound']);
        $bws['95th_inbound'] = formatBytes($bws['95th_inbound']);
        $pbws['95th_inbound'] = formatBytes($pbws['95th_inbound']);
        $bws['outbound_traffic'] = formatBytes($bws['outbound_traffic']);
        $pbws['outbound_traffic'] = formatBytes($pbws['outbound_traffic']);
        $bws['inbound_traffic'] = formatBytes($bws['inbound_traffic']);
        $pbws['inbound_traffic'] = formatBytes($pbws['inbound_traffic']);
    } else {
        $acl['bw_statics'] = 0;
    }
    $button = $acl['reboot'] ? '<a class="btn btn-danger" style="margin:10px;" href="modules/servers/ion/reboot.php?sid=' . ion_mcrypt($ion_sid) . "' onclick=\"return confirm('Are you sure to Reboot Server?');\">Reboot</a>" : '';
    $button .= $acl['ipmi'] ? '<a class="btn btn-success" style="margin:10px;" href="modules/servers/ion/ipmi.php?sid=' . ion_mcrypt($ion_sid) . '&hostname=' . urlencode($params['domain']) . '">IPMI</a>' : '';
    $button .= $acl['vpn'] ? "<a class='btn btn-primary' style='margin:10px;' href='clientarea.php?action=productdetails&id={$_GET['id']}&modop=custom&a=vpn'>Set VPN</a>" : '';
    $button .= $acl['rdns'] ? "<a class='btn btn-warning' style='margin:10px;' href='clientarea.php?action=productdetails&id={$_GET['id']}&modop=custom&a=rdns'>rDNS</a>" : '';
    $button .= $acl['bw_graph'] ? "<a class='btn btn-info' style='margin:10px;' href='clientarea.php?action=productdetails&id={$_GET['id']}&modop=custom&a=graph'>Bandwidth Graph</a>" : '';
    $code = '';
    $code .= $acl['server_info'] ? "<p><h4 style='margin-bottom: 20px;'>Server Specification</h4><span><table width='100%' style='text-align: left;'><tr><th>Host Name:</th><td>" . ion_metafilter($serverInfo['hostname']) . "</td></tr><tr><th>Chassis</th><td>" . ion_metafilter($serverInfo['chassis']) . "</td></tr><tr><th>Main board</th><td>" . ion_metafilter($serverInfo['mainboard']) . "</td></tr><tr><th>Processor</th><td>" . ion_metafilter($serverInfo['processor']) . "</td></tr><tr><th>Memory</th><td>" . ion_metafilter($serverInfo['memory']) . "</td></tr><tr><th>Total Memory</th><td>" . ion_metafilter($serverInfo['total_memory']) . "</td></tr><tr><th>Internal Hard Drive:</th><td>" . ion_metafilter($serverInfo['drive0']) . "</td></tr><tr><th>Drive1</th><td>" . ion_metafilter($serverInfo['drive1']) . "</td></tr><tr><th>Drive2</th><tD>" . ion_metafilter($serverInfo['drive2']) . "</td></tr><tr><th>Drive3</th><td>" . ion_metafilter($serverInfo['drive3']) . "</td></tr><tr><th>Drive4</th><td>" . ion_metafilter($serverInfo['drive4']) . "</td></tr><tr><th>Raid Level</th><td>" . ion_metafilter($serverInfo['raid_level']) . "</td></tr><tr><th>Uplink</th><td>" . ion_metafilter($serverInfo['uplink']) . "</td></tr></table></span></p>" : '';
    $code .= $acl['server_os'] ? "<div class='container row-fluid' style='text-align: left;margin-top: 25px;padding-top: 25px;border-top: 1px solid #ccc;'><div class='col-md-6 span6'><h4>Server IPs: </h4><span>{$ips}</span></div>" : '<div>';
    $code .= $acl['ip'] ? "<div class='col-md-6 span6'><h4>Server OS: </h4><span>{$os}</span></div></div>" : '</div>';
    $code .= $acl['bw_statics'] ? "<p style='margin-top: 25px;padding-top:25px;border-top: 1px solid #ccc;'><h4 style='margin-bottom: 20px;'>Bandwidth Statics:</h4><span><table width='100%' style='text-align: left;'><tr><th width='30%'></th><th width='30%'>Current</th><th width='30%'>Previous</th></tr><tr><th>Date Range:</th><td>{$bws['date_range']}</td><td>{$pbws['date_range']}</td></tr><tr><th>Included Bandwidth:</th><td>{$bws['included_bandwidth']}</td><td >{$pbws['included_bandwidth']}</td></tr><tr><th>Inbound Traffic:</th><td>{$bws['inbound_traffic']}</td><td>{$pbws['inbound_traffic']}</td></tr><tr><th>Outbound Traffic:</th><td>{$bws['outbound_traffic']}</td><td>{$pbws['outbound_traffic']}</td></tr><tr><th>95th Inbound:</th><td>{$bws['95th_inbound']}</td><td>{$pbws['95th_inbound']}</td></tr><tr><th>95th Outbound:</th><td>{$bws['95th_outbound']}</td><td>{$pbws['95th_outbound']}</td></tr></table></span>" : '';
    $code .= "<p style='margin-top: 25px;padding-top:25px;border-top: 1px solid #ccc;'>{$button}</p>";
    return $code;
}