<?php
require_once __DIR__ . '/../MPHX/common.php';

header('Content-Type: application/json; charset=UTF-8');

function api_debug_log($message) {
    // 调试日志已禁用
    return;
    $log_file = __DIR__ . '/api_debug.log';
    $date = date('Y-m-d H:i:s');
    $line = '[' . $date . '] ' . $message . "\n";
    @file_put_contents($log_file, $line, FILE_APPEND);
}

api_debug_log('收到请求: GET=' . json_encode($_GET) . ' POST=' . json_encode($_POST));

if ($conf['apiqk'] != 'true') {
    api_debug_log('API接口已关闭');
    exit(json_encode(array('code' => -1, 'msg' => 'API接口已关闭')));
}

$api_key = '';
$mn_bh = '';
$mn_keye = '';

if (isset($_POST['mn_key']) && !empty($_POST['mn_key'])) {
    $api_key = $_POST['mn_key'];
    $mn_bh = isset($_POST['mn_bh']) ? $_POST['mn_bh'] : '';
    $mn_keye = isset($_POST['mn_keye']) ? $_POST['mn_keye'] : '';
    api_debug_log('魔方对接模式: mn_key=' . $api_key . ' mn_bh=' . $mn_bh . ' mn_keye=' . $mn_keye);
} elseif (isset($_GET['api']) && !empty($_GET['api'])) {
    $api_key = $_GET['api'];
} elseif (isset($_GET['key']) && !empty($_GET['key'])) {
    $api_key = $_GET['key'];
} elseif (isset($_POST['api']) && !empty($_POST['api'])) {
    $api_key = $_POST['api'];
} elseif (isset($_POST['key']) && !empty($_POST['key'])) {
    $api_key = $_POST['key'];
}

api_debug_log('接收到的API密钥: ' . $api_key . ' | 配置的API密钥: ' . $conf['api']);

if ($api_key != $conf['api']) {
    api_debug_log('API密钥验证失败');
    exit(json_encode(array('code' => -1, 'msg' => 'API密钥错误')));
}

api_debug_log('API密钥验证成功');

// 获取功能代码
$gn = '';
if (isset($_GET['gn']) && !empty($_GET['gn'])) {
    $gn = $_GET['gn'];
} elseif (isset($_POST['gn']) && !empty($_POST['gn'])) {
    $gn = $_POST['gn'];
}

api_debug_log('功能代码: [' . $gn . ']');
api_debug_log('所有GET参数: ' . json_encode($_GET));
api_debug_log('所有POST参数: ' . json_encode($_POST));

// 调试模式：如果请求中包含debug=1，直接返回调试信息
if (isset($_GET['debug']) || isset($_POST['debug'])) {
    exit(json_encode(array(
        'code' => -1,
        'msg' => '调试信息',
        'debug' => array(
            'gn' => $gn,
            'get' => $_GET,
            'post' => $_POST,
            'raw_input' => file_get_contents('php://input')
        )
    )));
}

$api = new api_class();

switch ($gn) {
    case 'addzj':
    case 'kt':
    case '开通host':
    case 'createHost':
    case 'create':
        $result = $api->addzj();
        break;
    case 'delzj':
    case 'tz':
    case '删除host':
    case 'deleteHost':
    case 'delete':
        $result = $api->delzj();
        break;
    case 'getzj':
        $result = $api->getzj();
        break;
    case 'getbt':
        $result = $api->getbt();
        break;
    case 'getym':
        $result = $api->getym();
        break;
    case 'getStats':
        $result = $api->getStats();
        break;
    case 'zt':
        $result = $api->ztzj();
        break;
    case 'jc':
        $result = $api->jczj();
        break;
    case 'czmm':
        $result = $api->czmm();
        break;
    case 'cfif':
        $result = array('code' => '200', 'msg' => '连接成功');
        break;
    case 'test':
        $result = array('code' => '200', 'msg' => 'API连接成功');
        break;
    default:
        api_debug_log('不支持的功能代码: ' . $gn);
        $result = array('code' => -1, 'msg' => '不支持的方法: ' . $gn);
}

api_debug_log('返回结果: ' . json_encode($result));
echo json_encode($result);

class api_class {
    
    public function addzj() {
        global $DB, $conf;
        
        $btid = isset($_GET['btid']) ? intval($_GET['btid']) : (isset($_POST['btid']) ? intval($_POST['btid']) : 0);
        $user = isset($_GET['user']) ? $_GET['user'] : (isset($_POST['user']) ? $_POST['user'] : (isset($_POST['username']) ? $_POST['username'] : ''));
        $pass = isset($_GET['pass']) ? $_GET['pass'] : (isset($_POST['pass']) ? $_POST['pass'] : (isset($_POST['password']) ? $_POST['password'] : ''));
        
        // 转义用户输入防止SQL注入
        $user_escaped = $DB->escape($user);
        $pass_escaped = $DB->escape($pass);
        
        // 网页空间和数据库空间直接以MB存储
        $wypj = isset($_GET['wypj']) ? floatval($_GET['wypj']) : (isset($_POST['wypj']) ? floatval($_POST['wypj']) : (isset($_POST['webdx']) ? floatval($_POST['webdx']) : 1024));

        $sypj = isset($_GET['sypj']) ? floatval($_GET['sypj']) : (isset($_POST['sypj']) ? floatval($_POST['sypj']) : (isset($_POST['sqldx']) ? floatval($_POST['sqldx']) : 1024));
        
        $llxz = isset($_GET['llxz']) ? floatval($_GET['llxz']) : (isset($_POST['llxz']) ? floatval($_POST['llxz']) : (isset($_POST['sizemax']) ? floatval($_POST['sizemax']) : 10));
        $llxz_unit = isset($_POST['llxz_unit']) ? strtoupper($_POST['llxz_unit']) : (isset($_POST['sizemax_unit']) ? strtoupper($_POST['sizemax_unit']) : 'GB');
        if ($llxz_unit === 'MB') {
            $llxz = $llxz / 1024;
        }
        $dqsj_raw = isset($_GET['dqsj']) ? $_GET['dqsj'] : (isset($_POST['dqsj']) ? $_POST['dqsj'] : (isset($_POST['dqtime']) && $_POST['dqtime'] > 0 ? $_POST['dqtime'] : date('Y-m-d', strtotime('+1 year'))));
        // 处理时间戳格式（如果是纯数字且长度>8，认为是时间戳）
        if (is_numeric($dqsj_raw) && strlen($dqsj_raw) > 8) {
            $dqsj = date('Y-m-d', intval($dqsj_raw));
        } else {
            $dqsj = $dqsj_raw;
        }
        $dqsj_escaped = $DB->escape($dqsj);
        $cplx = isset($_GET['cplx']) ? $_GET['cplx'] : (isset($_POST['cplx']) ? $_POST['cplx'] : (isset($_POST['type']) ? $_POST['type'] : '主机'));
        $cplx_escaped = $DB->escape($cplx);
        $yms = isset($_GET['yms']) ? intval($_GET['yms']) : (isset($_POST['yms']) ? intval($_POST['yms']) : (isset($_POST['ymbds']) ? intval($_POST['ymbds']) : 1));
        
        $mn_bh = isset($_POST['mn_bh']) ? $_POST['mn_bh'] : '';
        $mn_keye = isset($_POST['mn_keye']) ? $_POST['mn_keye'] : '';
        $mn_bh_escaped = $DB->escape($mn_bh);
        $mn_keye_escaped = $DB->escape($mn_keye);
        
        if (empty($user) || empty($pass)) {
            return array('code' => -1, 'msg' => '用户名或密码不能为空');
        }
        
        if (!empty($mn_bh)) {
            $bt = $DB->get_row("SELECT * FROM `bt` WHERE `bzbh`='{$mn_bh_escaped}'");
            if (!$bt) {
                return array('code' => -1, 'msg' => '宝塔编号不存在');
            }
            if (!empty($mn_keye) && $bt['ktmy'] != $mn_keye) {
                return array('code' => -1, 'msg' => '调用密钥错误');
            }
            $btid = $bt['id'];
        } else {
            $bt = $DB->get_row("SELECT * FROM `bt` WHERE `id`='{$btid}'");
            if (!$bt) {
                return array('code' => -1, 'msg' => '宝塔服务器不存在');
            }
        }
        
        $zj = $DB->get_row("SELECT * FROM `zj` WHERE `user`='{$user_escaped}'");
        if ($zj) {
            return array('code' => -1, 'msg' => '用户名已存在');
        }
        
        $bt_api = new bt_api();
        // 使用HTTPS协议，与后台手动添加保持一致
        $protocol = ($bt['aqfw'] == 'true') ? 'https' : 'https';
        $bt_api->BT_PANEL = $protocol . '://' . $bt['ip'] . ':' . $bt['dk'];
        $bt_api->BT_KEY = $bt['key'];
        
        $path = $bt['czzt'] == 'Linux' ? $conf['hxi'] . '/' . $user : $conf['hxo'] . '/' . $user;
        
        // 域名使用用户名.主域名格式
        $main_domain = isset($conf['main_domain']) && !empty($conf['main_domain']) ? $conf['main_domain'] : 'a116.vip';
        $domain = $user . '.' . $main_domain;
        
        // 使用正确的PHP版本格式，与后台添加主机保持一致
        $php_version = '74'; // 使用PHP 7.4
        
        api_debug_log('宝塔API调用: BT_PANEL=' . $bt_api->BT_PANEL . ' path=' . $path);
        api_debug_log('开通参数: user=' . $user . ' pass=' . $pass . ' webdx=' . $wypj . ' sqldx=' . $sypj . ' domain=' . $domain . ' php=' . $php_version);
        
        $result = $bt_api->webkt($user, $pass, $domain, 'PHP', 'true', 'true', $php_version, $path);
        
        api_debug_log('webkt返回结果: ' . json_encode($result));
        
        if ($result && isset($result['siteId'])) {
            $webid = $result['siteId'];
            // 使用与后台添加主机一致的字段名
            $ftpuser = isset($result['ftpUser']) ? $result['ftpUser'] : (isset($result['ftp_username']) ? $result['ftp_username'] : $user);
            $ftppass = isset($result['ftpPass']) ? $result['ftpPass'] : (isset($result['ftp_password']) ? $result['ftp_password'] : $pass);
            $sqluser = isset($result['databaseUser']) ? $result['databaseUser'] : (isset($result['database_name']) ? $result['database_name'] : $user);
            $sqlpass = isset($result['databasePass']) ? $result['databasePass'] : (isset($result['database_password']) ? $result['database_password'] : $pass);
            $sqldb = isset($result['databaseName']) ? $result['databaseName'] : (isset($result['database_name']) ? $result['database_name'] : $user);
            
            // 转义所有插入的数据
            $ftpuser_escaped = $DB->escape($ftpuser);
            $ftppass_escaped = $DB->escape($ftppass);
            $sqluser_escaped = $DB->escape($sqluser);
            $sqlpass_escaped = $DB->escape($sqlpass);
            $sqldb_escaped = $DB->escape($sqldb);
            
            $DB->query("INSERT INTO `zj` (`user`,`pass`,`btid`,`webid`,`cplx`,`wypj`,`sypj`,`llxz`,`dqsj`,`yms`,`ftpuser`,`ftppass`,`sqluser`,`sqlpass`,`sqldb`,`email`,`zt`) VALUES ('{$user_escaped}','{$pass_escaped}','{$btid}','{$webid}','{$cplx_escaped}','{$wypj}','{$sypj}','{$llxz}','{$dqsj_escaped}','{$yms}','{$ftpuser_escaped}','{$ftppass_escaped}','{$sqluser_escaped}','{$sqlpass_escaped}','{$sqldb_escaped}','','正常')");
            logjl($user, 'API添加主机', 'API添加主机: ' . $user, true);
            return array('code' => '200', 'msg' => '添加成功', 'data' => array('user' => $user, 'pass' => $pass));
        }
        
        $error_msg = '未知错误';
        if (isset($result['msg'])) {
            $error_msg = $result['msg'];
        } elseif (is_string($result)) {
            $error_msg = $result;
        } elseif (isset($result['error'])) {
            $error_msg = $result['error'];
        }
        api_debug_log('添加失败: ' . $error_msg);
        return array('code' => -1, 'msg' => '添加失败: ' . $error_msg);
    }
    
    public function delzj() {
        global $DB;
        
        $user = isset($_GET['user']) ? $_GET['user'] : (isset($_POST['user']) ? $_POST['user'] : (isset($_POST['username']) ? $_POST['username'] : ''));
        $user_escaped = $DB->escape($user);
        
        if (empty($user)) {
            return array('code' => -1, 'msg' => '用户名不能为空');
        }
        
        $zj = $DB->get_row("SELECT * FROM `zj` WHERE `user`='{$user_escaped}'");
        if (!$zj) {
            return array('code' => -1, 'msg' => '主机不存在');
        }
        
        $zj_id_escaped = $DB->escape($zj['id']);
        $bt = $DB->get_row("SELECT * FROM `bt` WHERE `id`='{$zj_id_escaped}'");
        if ($bt) {
            $bt_api = new bt_api();
            $bt_api->BT_PANEL = 'http://' . $bt['ip'] . ':' . $bt['dk'];
            $bt_api->BT_KEY = $bt['key'];
            $bt_api->delsite($zj['webid'], $zj['user']);
        }
        
        $DB->query("DELETE FROM `zj` WHERE `id`='{$zj_id_escaped}'");
        logjl($user, 'API删除主机', 'API删除主机: ' . $user, true);
        return array('code' => '200', 'msg' => '删除成功');
    }
    
    public function ztzj() {
        global $DB;
        
        $user = isset($_GET['user']) ? $_GET['user'] : (isset($_POST['user']) ? $_POST['user'] : (isset($_POST['username']) ? $_POST['username'] : ''));
        $user_escaped = $DB->escape($user);
        
        if (empty($user)) {
            return array('code' => -1, 'msg' => '用户名不能为空');
        }
        
        $zj = $DB->get_row("SELECT * FROM `zj` WHERE `user`='{$user_escaped}'");
        if (!$zj) {
            return array('code' => -1, 'msg' => '主机不存在');
        }
        
        $zj_id_escaped = $DB->escape($zj['id']);
        $bt = $DB->get_row("SELECT * FROM `bt` WHERE `id`='{$zj_id_escaped}'");
        if ($bt) {
            $bt_api = new bt_api();
            $bt_api->BT_PANEL = 'http://' . $bt['ip'] . ':' . $bt['dk'];
            $bt_api->BT_KEY = $bt['key'];
            $bt_api->ztweb($zj['webid'], $zj['user']);
        }
        
        $DB->query("UPDATE `zj` SET `zt`='暂停' WHERE `id`='{$zj_id_escaped}'");
        logjl($user, 'API暂停主机', 'API暂停主机: ' . $user, true);
        return array('code' => '200', 'msg' => '暂停成功');
    }
    
    public function jczj() {
        global $DB;
        
        $user = isset($_GET['user']) ? $_GET['user'] : (isset($_POST['user']) ? $_POST['user'] : (isset($_POST['username']) ? $_POST['username'] : ''));
        $user_escaped = $DB->escape($user);
        
        if (empty($user)) {
            return array('code' => -1, 'msg' => '用户名不能为空');
        }
        
        $zj = $DB->get_row("SELECT * FROM `zj` WHERE `user`='{$user_escaped}'");
        if (!$zj) {
            return array('code' => -1, 'msg' => '主机不存在');
        }
        
        $zj_id_escaped = $DB->escape($zj['id']);
        $bt = $DB->get_row("SELECT * FROM `bt` WHERE `id`='{$zj_id_escaped}'");
        if ($bt) {
            $bt_api = new bt_api();
            $bt_api->BT_PANEL = 'http://' . $bt['ip'] . ':' . $bt['dk'];
            $bt_api->BT_KEY = $bt['key'];
            $bt_api->qdweb($zj['webid'], $zj['user']);
        }
        
        $DB->query("UPDATE `zj` SET `zt`='正常' WHERE `id`='{$zj_id_escaped}'");
        logjl($user, 'API解封主机', 'API解封主机: ' . $user, true);
        return array('code' => '200', 'msg' => '解封成功');
    }
    
    public function czmm() {
        global $DB;
        
        $user = isset($_GET['user']) ? $_GET['user'] : (isset($_POST['user']) ? $_POST['user'] : (isset($_POST['username']) ? $_POST['username'] : ''));
        $pass = isset($_GET['pass']) ? $_GET['pass'] : (isset($_POST['pass']) ? $_POST['pass'] : (isset($_POST['password']) ? $_POST['password'] : ''));
        $user_escaped = $DB->escape($user);
        $pass_escaped = $DB->escape($pass);
        
        if (empty($user) || empty($pass)) {
            return array('code' => -1, 'msg' => '用户名或密码不能为空');
        }
        
        $zj = $DB->get_row("SELECT * FROM `zj` WHERE `user`='{$user_escaped}'");
        if (!$zj) {
            return array('code' => -1, 'msg' => '主机不存在');
        }
        
        $zj_id_escaped = $DB->escape($zj['id']);
        $bt = $DB->get_row("SELECT * FROM `bt` WHERE `id`='{$zj_id_escaped}'");
        if ($bt) {
            $bt_api = new bt_api();
            $bt_api->BT_PANEL = 'http://' . $bt['ip'] . ':' . $bt['dk'];
            $bt_api->BT_KEY = $bt['key'];
            $bt_api->setftppass($zj['webid'], $zj['ftpuser'], $pass);
            $bt_api->setmysqlpass($zj['webid'], $zj['sqldb'], $pass);
        }
        
        $DB->query("UPDATE `zj` SET `pass`='{$pass_escaped}', `ftppass`='{$pass_escaped}', `sqlpass`='{$pass_escaped}' WHERE `id`='{$zj_id_escaped}'");
        logjl($user, 'API重置密码', 'API重置密码: ' . $user, true);
        return array('code' => '200', 'msg' => '重置成功');
    }
    
    public function getzj() {
        global $DB;
        
        $user = isset($_GET['user']) ? $_GET['user'] : (isset($_POST['user']) ? $_POST['user'] : (isset($_POST['username']) ? $_POST['username'] : ''));
        $user_escaped = $DB->escape($user);
        
        if (empty($user)) {
            $zjs = $DB->query("SELECT * FROM `zj` ORDER BY `id` DESC");
            $list = array();
            while ($zj = $DB->fetch($zjs)) {
                $list[] = array(
                    'id' => $zj['id'],
                    'user' => $zj['user'],
                    'cplx' => $zj['cplx'],
                    'wypj' => $zj['wypj'],
                    'sypj' => $zj['sypj'],
                    'llxz' => $zj['llxz'],
                    'dqsj' => $zj['dqsj'],
                    'zt' => $zj['zt']
                );
            }
            return array('code' => '200', 'msg' => '获取成功', 'data' => $list);
        }
        
        $zj = $DB->get_row("SELECT * FROM `zj` WHERE `user`='{$user_escaped}'");
        if (!$zj) {
            return array('code' => -1, 'msg' => '主机不存在');
        }
        
        return array('code' => '200', 'msg' => '获取成功', 'data' => array(
            'id' => $zj['id'],
            'user' => $zj['user'],
            'cplx' => $zj['cplx'],
            'wypj' => $zj['wypj'],
            'sypj' => $zj['sypj'],
            'llxz' => $zj['llxz'],
            'dqsj' => $zj['dqsj'],
            'zt' => $zj['zt'],
            'ftpuser' => $zj['ftpuser'],
            'ftppass' => $zj['ftppass'],
            'sqluser' => $zj['sqluser'],
            'sqlpass' => $zj['sqlpass'],
            'sqldb' => $zj['sqldb']
        ));
    }
    
    public function getbt() {
        global $DB;
        
        $bts = $DB->query("SELECT * FROM `bt` ORDER BY `id` DESC");
        $list = array();
        while ($bt = $DB->fetch($bts)) {
            $list[] = array(
                'id' => $bt['id'],
                'ip' => $bt['ip'],
                'dk' => $bt['dk'],
                'czzt' => $bt['czzt'],
                'jkkg' => $bt['jkkg']
            );
        }
        
        return array('code' => '200', 'msg' => '获取成功', 'data' => $list);
    }
    
    public function getym() {
        global $DB;
        
        $yms = $DB->query("SELECT * FROM `ym` ORDER BY `id` DESC");
        $list = array();
        while ($ym = $DB->fetch($yms)) {
            $list[] = array(
                'id' => $ym['id'],
                'ym' => $ym['ym'],
                'jg' => $ym['jg'],
                'js' => $ym['js']
            );
        }
        
        return array('code' => '200', 'msg' => '获取成功', 'data' => $list);
    }
    
    /**
     * 获取主机实时统计信息
     * 包括空间使用、数据库使用、流量使用
     */
    public function getStats() {
        global $DB;
        
        $user = isset($_GET['user']) ? $_GET['user'] : (isset($_POST['user']) ? $_POST['user'] : (isset($_POST['username']) ? $_POST['username'] : ''));
        $user_escaped = $DB->escape($user);
        
        if (empty($user)) {
            return array('code' => -1, 'msg' => '用户名不能为空');
        }
        
        // 查询主机信息
        $zj = $DB->get_row("SELECT * FROM `zj` WHERE `user`='{$user_escaped}'");
        if (!$zj) {
            return array('code' => -1, 'msg' => '主机不存在');
        }
        
        // 获取配额信息（从数据库）- wypj和sypj以MB存储
        $disk_total = intval($zj['wypj']) * 1024 * 1024;      // 网页空间配额（字节）
        $db_total = intval($zj['sypj']) * 1024 * 1024;        // 数据库配额（字节）
        $traffic_limit = intval($zj['llxz']) * 1024 * 1024 * 1024;  // 流量限制（字节）
        
        // 初始化使用量
        $disk_used = 0;
        $db_used = 0;
        $traffic_used = 0;
        
        // 查询宝塔服务器信息并获取实时使用量
        $zj_btid_escaped = $DB->escape($zj['btid']);
        $bt = $DB->get_row("SELECT * FROM `bt` WHERE `id`='{$zj_btid_escaped}'");
        if ($bt) {
            $bt_api = new bt_api();
            $protocol = ($bt['aqfw'] == 'true') ? 'https' : 'https';
            $bt_api->BT_PANEL = $protocol . '://' . $bt['ip'] . ':' . $bt['dk'];
            $bt_api->BT_KEY = $bt['key'];
            
            // 获取网站信息（包含大小）
            $site_info = $bt_api->GetSiteInfo($zj['webid']);
            if ($site_info && is_array($site_info)) {
                // 尝试从不同字段获取大小
                if (isset($site_info['total_size'])) {
                    $disk_used = intval($site_info['total_size']);
                } elseif (isset($site_info['size'])) {
                    $disk_used = intval($site_info['size']);
                } elseif (isset($site_info['data']) && isset($site_info['data']['total_size'])) {
                    $disk_used = intval($site_info['data']['total_size']);
                }
            }
            
            // 获取数据库信息（通过宝塔API）
            // 使用 GetDatabaseAccess 或其他可用方法
            $db_info = $bt_api->GetDatabaseAccess($zj['sqluser']);
            if ($db_info && is_array($db_info)) {
                if (isset($db_info['total_size'])) {
                    $db_used = intval($db_info['total_size']);
                } elseif (isset($db_info['size'])) {
                    $db_used = intval($db_info['size']);
                }
            }
        }
        
        // 查询当月流量统计
        $month_start = date('Y-m-01');
        $month_end = date('Y-m-t');
        $traffic_stats = $DB->get_row("SELECT SUM(traffic_total) as total FROM `traffic_stats` WHERE `user_id`='{$zj['id']}' AND `stat_date`>='{$month_start}' AND `stat_date`<='{$month_end}'");
        if ($traffic_stats && $traffic_stats['total']) {
            $traffic_used = intval($traffic_stats['total']);
        }
        
        // 计算百分比和剩余量
        $disk_percent = $disk_total > 0 ? round(($disk_used / $disk_total) * 100, 2) : 0;
        $disk_free = $disk_total - $disk_used;
        
        $db_percent = $db_total > 0 ? round(($db_used / $db_total) * 100, 2) : 0;
        $db_free = $db_total - $db_used;
        
        $traffic_percent = $traffic_limit > 0 ? round(($traffic_used / $traffic_limit) * 100, 2) : 0;
        $traffic_free = $traffic_limit - $traffic_used;
        
        return array(
            'code' => '200',
            'msg' => '获取成功',
            'data' => array(
                'user' => $user,
                'status' => $zj['zt'],
                'expire_date' => $zj['dqsj'],
                // 空间使用情况
                'disk' => array(
                    'total' => $disk_total,
                    'used' => $disk_used,
                    'free' => $disk_free,
                    'percent' => $disk_percent,
                    'total_mb' => intval($zj['wypj']),
                    'used_mb' => round($disk_used / 1024 / 1024, 2),
                    'free_mb' => round($disk_free / 1024 / 1024, 2)
                ),
                // 数据库使用情况
                'database' => array(
                    'total' => $db_total,
                    'used' => $db_used,
                    'free' => $db_free,
                    'percent' => $db_percent,
                    'total_mb' => intval($zj['sypj']),
                    'used_mb' => round($db_used / 1024 / 1024, 2),
                    'free_mb' => round($db_free / 1024 / 1024, 2)
                ),
                // 流量使用情况
                'traffic' => array(
                    'limit' => $traffic_limit,
                    'used' => $traffic_used,
                    'free' => $traffic_free,
                    'percent' => $traffic_percent,
                    'limit_gb' => intval($zj['llxz']),
                    'used_gb' => round($traffic_used / 1024 / 1024 / 1024, 2),
                    'free_gb' => round($traffic_free / 1024 / 1024 / 1024, 2)
                )
            )
        );
    }
}
?>
