<?php
require '../../zciyphp/comm.php'; //需要全局函数
require '../projectcomm.php';
$qstr = get('func');  //   例: admin/rig/user.list
if (!empty($qstr)) {
    $qstrarr = explode('.', $qstr);
    $ms = timems();
    if (count($qstrarr) != 2) {
        echo '{"errmsg":"' . $qstr . '格式错误"}';
        die;
    }
    $qs0 = $qstrarr[0];
    $qstrarr[0] = str_replace('\\\\', '\\', 'web\\' . str_replace('/', '\\', $qstrarr[0]));
    //先找fun，没有找mock，最后提示错误
    $retarr = null;
    if (!class_exists($qstrarr[0])) {
        $retarr = array('code' => 9, 'errmsg' => $qstrarr[0] . '无效类');
    } else if (!method_exists($qstrarr[0], 'json_' . $qstrarr[1])) {
        $retarr = array('code' => 9, 'errmsg' => $qstr . '无效函数');
    }
    $db = new ciy\db(); //默认数据库全局变量
    if ($retarr === null) {
        $qs0 = substr($qs0, 0, strpos($qs0, '/'));
        if (file_exists(PATH_WEB . $qs0 . '/common.php'))
            require PATH_WEB . $qs0 . '/common.php'; //需要全局业务函数和全局变量
        // $dbrd1 = new ciy\db('rd1');//读库  可实现注册中心取一个空闲实例  业务端判断
        // $dbag = new ciy\db('ag');//独立ag库
        // web\admin\autotask\task::json_run
        $retarr = call_user_func($qstrarr[0] . '::json_' . $qstrarr[1]);
    } else {
        $csql = new \ciy\sql('zc_ymock');
        $csql->where('ufunc', $qstr);
        $csql->order('id desc');
        $mockrow = $db->getone($csql);
        if (is_array($mockrow)) {
            $updata = array();
            $updata['lastrequest'] = $_SERVER['REQUEST_URI'] . '<br/>' . file_get_contents('php://input');
            $updata['lasttimes'] = time();
            $csql = new \ciy\sql('zc_ymock');
            $csql->where('id', $mockrow['id']);
            $db->update($csql, $updata);
            echo $mockrow['mockjson'];
            die;
        }
    }
    if(!is_array($retarr))
        die('{"errmsg":"无succ返回值"}');
    if ($retarr['code'] == 302) {
        header('Location: ' . $retarr['redirect']);
        die;
    }
    ciy_ouputJSON($retarr, timems() - $ms, $_SERVER['REQUEST_URI']);
    die;
}
$qstr = get('sse');  //   例: admin/rig/user.xxxsse
//https://html.spec.whatwg.org/multipage/server-sent-events.html
if (!empty($qstr)) {
    $qstrarr = explode('.', $qstr);
    $ms = timems();
    if (count($qstrarr) < 2) {
        echo '{"errmsg":"' . $qstr . '格式错误"}';
        die;
    }
    $qs0 = $qstrarr[0];
    $qstrarr[0] = 'web\\' . str_replace('/', '\\', $qstrarr[0]);
    if (!class_exists($qstrarr[0]))
        $retarr = array('code' => 9, 'errmsg' => $qstrarr[0] . '无效类');
    else if (!method_exists($qstrarr[0], 'sse_' . $qstrarr[1]))
        $retarr = array('code' => 9, 'errmsg' => $qstr . '无效函数');
    else {
        $qs0 = substr($qs0, 0, strpos($qs0, '/'));
        require PATH_WEB . $qs0 . '/common.php'; //需要全局业务函数和全局变量
        $db = new ciy\db(); //默认数据库全局变量

        if (get('debug') == 'yes') {
            call_user_func($qstrarr[0] . '::sse_' . $qstrarr[1], function ($data, $type = '') {
                if ($type === true)
                    return;
                //$data = '<code style="color:#199d01;">' . $data . '</code>';
                else if ($type != '')
                    $data = '<kbd style="color:#ffffff;background:#000000;margin:0.2em 0.5em; padding:0.2em;">' . $type . '</kbd>' . $data;
                echo $data . '<br/>';
            });
        } else {
            set_time_limit(0);
            ini_set('output_buffering', 'off');
            ini_set('zlib.output_compression', false);
            while (@ob_end_flush()) {
            }
            header('Content-Type: text/event-stream');
            header('Cache-Control: no-cache');
            header('Connection: keep-alive');
            header('X-Accel-Buffering: no');
            ob_start();
            $retarr = call_user_func($qstrarr[0] . '::sse_' . $qstrarr[1], function ($data, $id = '') {
                if (is_object($data) || is_array($data))
                    $data = json_encode($data, JSON_UNESCAPED_UNICODE);
                // if (strpos($data, "\n") !== false)
                //     $data = 'base64,' . base64_encode($data);
                if (empty($id))
                    echo 'data: ' . $data . "\n\n";
                else
                    echo 'data: ' . $data . "\n" . 'id: ' . $id . "\n\n";
                ob_flush();
                flush();
            }, function ($data, $event = '_t_') {
                echo 'data: ' . $data . "\n" . 'event: ' . $event . "\n\n";
                ob_flush();
                flush();
            });
        }
    }
    if (isset($retarr))
        ciy_ouputJSON($retarr, timems() - $ms, $_SERVER['REQUEST_URI']);
    die;
}
header('HTTP/1.1 488 Function Not Found');
