<?php

namespace web\admin;

class index {
    public static function json_init() {
        global $db;
        $rsuser = verifyfast();
        $csql = new \ciy\sql('zc_icon');
        $csql->where('icontarget', 10);
        $csql->column('targetid as id,icon');
        $ret['icon'] = $db->get($csql);
        $csql = new \ciy\sql('zc_menu');
        $csql->where('isuse', 1)->order('csort desc,id')->column('id,upid,name,url,pow');
        $ret['menu'] = $db->get($csql);
        if ($ret['menu'] === false)
            return errjson('菜单获取失败:' . $db->error);
        if (count($ret['menu']) == 0)
            $ret['menu'][] = array('id' => 2, 'upid' => 0, 'name' => '您无任何菜单权限', 'url' => '', 'pow' => '', 'csort' => 0);

        $csql = new \ciy\sql('zc_mnufav');
        $csql->where('favtarget', 10);
        $csql->where('user', $rsuser['id']);
        $csql->column('menuid');
        $ret['mnufav'] = $db->get($csql);
        $ret['welcome'] = array('url' => 'welcome.html', 'name' => '控制台');
        $ret['title'] = 'Ciyon SaaS总控台';
        return succjson($ret);
    }
    public static function json_favadd() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $menuid = $post->getint('id');
        $csql = new \ciy\sql('zc_mnufav');
        $csql->where('favtarget', 10);
        $csql->where('user', $rsuser['id']);
        $csql->where('menuid', $menuid);
        $favrow = $db->getone($csql);
        if (!is_array($favrow)) {
            $updata = array();
            $updata['favtarget'] = 10;
            $updata['user'] = $rsuser['id'];
            $updata['menuid'] = $menuid;
            $updata['addtimes'] = tostamp();
            $csql = new \ciy\sql('zc_mnufav');
            $db->insert($csql, $updata);
        }
        return succjson();
    }
    public static function json_favdel() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $menuid = $post->getint('id');
        $csql = new \ciy\sql('zc_mnufav');
        $csql->where('favtarget', 10);
        $csql->where('user', $rsuser['id']);
        $csql->where('menuid', $menuid);
        $db->delete($csql);
        return succjson();
    }
    public static function json_godao() {
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $url = $post->get('url');
        $cfg = webini('ciyapi');
        $apiid = $cfg['apiid'];
        $apikey = $cfg['apikey'];
        $url = 'https://dao.local.ciy.cn' . $url;
        if (strpos($url, '?') === false)
            $url .= '?';
        else
            $url .= '&';
        $time = time();
        $url .= 'token=' . $apiid . 'I' . $time . 'I' . sha256($apiid . $time . $apikey);
        $ret['url'] = $url;
        return succjson($ret);
    }
    public static function json_setssh() {
        global $db;
        $rsuser = verifyfast();
        if (nopower($db, $rsuser['id'], 'x1ssh'))
            return errjson('您未被授权操作');
        $post = new \ciy\post();
        $able = $post->getbool('able');  //true，打开SSH允许远程访问。   false，关闭防火墙禁止访问
        if ($able) {
            //开启后，需定时关闭防火墙
        }
        return succjson();
    }
}
