<?php
/**
 * List Controller
 */
require_once __DIR__ . '/../../../lib/DirScanner.php';
require_once __DIR__ . '/../../../plugins/Parsedown.php';
require_once __DIR__ . '/../../../plugins/Html.php';
require_once __DIR__ . '/../../../plugins/Common.php';
require_once __DIR__ . '/SiteController.php';

Class FrontApiController extends SiteController {

    public function actionIndex() {
        $code = 0;
        $err = 'Not allowed';

        return $this->renderJson(compact('code', 'err'));
    }

    public function actionTags() {
        //获取数据
        $menus = array();        //菜单,一级目录
        $htmlReadme = '';   //Readme.md 内容,底部网站详细介绍
        $htmlCateReadme = '';   //当前目录下的Readme.md 内容
        $menus_sorted = array(); //Readme_sort.txt 说明文件内容,一级目录菜单从上到下的排序
        
        $scanner = new DirScanner();
        $scanner->setWebRoot(FSC::$app['config']['content_directory']);
        $dirTree = $scanner->scan(__DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'], 3);

        $code = 1;
        $msg = '';
        $err = '';
        
        //获取tags分类
        $noFiles = true;
        $data = $this->getTags($dirTree, $noFiles);

        return $this->renderJson(compact('code', 'msg', 'err', 'data'));
    }

    /*
     * 参数:
     * content: 从抖音或其它平台复制出来的视频分享内容,或者视频网址
     * title: 视频标题
     * tag: 分类名称
     * tagid: 分类id
     * 其中title、tag和tagid为可选值。
     */
    public function actionAddfav() {
        $ip = $this->getUserIp();
        $check_time = 60;            //1 分钟内
        $max_time_in_minutes = 10;   //最多 10 次

        $isUserGotRequestLimit = $this->requestLimit($ip, $max_time_in_minutes, $check_time);
        if ($isUserGotRequestLimit) {
            $this->logError("Request limit got, ip: {$ip}");
            throw new Exception('Oops,操作太快了,请喝杯咖啡休息会吧...');
        }

        //只允许添加到自己的收藏夹
        $loginedUser = Common::getUserFromSession();
        if (empty($loginedUser['username'])) {
            throw new Exception('Oops,你还没登录哦');
        }else if (
            !empty(FSC::$app['config']['multipleUserUriParse'])
            && (empty(FSC::$app['user_id']) || FSC::$app['user_id'] != $loginedUser['username'])
        ) {
            throw new Exception('Oops,请求地址有误');
        }

        $content = $this->post('content', '');
        $title = $this->post('title', '');
        $tag = $this->post('tag', '');
        $tagid = $this->post('tagid', '');

        $code = 1;
        $msg = '';
        $err = '';

        if (empty($content)) {
            $code = 0;
            $err = '请粘贴填写分享内容!';
        }else {
            $content = urldecode($content);
        }

        //分享内容来源平台检查
        $shareUrl = $this->getShareUrlFromContent($content);
        $platform = Html::getShareVideosPlatform($shareUrl);
        if (!in_array($platform, FSC::$app['config']['tajian']['supportedPlatforms'])) {
            $code = 0;
            $err = '目前只支持抖音、快手、西瓜视频和Bilibili的分享网址哦!';
        }

        $tagName = '';
        if ($code == 1 && (!empty($tag) || !empty($tagid))) {        //检查分类名称或id是否存在
            $scanner = new DirScanner();
            $scanner->setWebRoot(FSC::$app['config']['content_directory']);
            $dirTree = $scanner->scan(__DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'], 3);
            //获取tags分类
            $tags = $this->getTags($dirTree);

            if (!empty($tagid) && empty($tags[$tagid])) {        //检查tagid是否存在
                $code = 0;
                $err = "分类ID {$tagid} 不存在!";
            }

            if (!empty($tag)) {        //检查tag是否存在
                $tag_exists = false;
                foreach($tags as $id => $item) {
                    if ($item['name'] == $tag) {
                        $tag_exists = true;
                        $tagName = $tag;
                        break;
                    }
                }

                if ($tag_exists == false) {
                    $code = 0;
                    $err = "分类 {$tag} 不存在!";
                }
            }

            if (empty($tagName) && !empty($tags[$tagid])) {
                $tagName = $tags[$tagid]['name'];
            }
        }

        if ($code == 1) {        //保存视频
            $msg = $this->saveShareVideo($content, $title, $tagName) ? '视频保存完成,系统开始自动处理,1 - 3 分钟后刷新就能看到新添加的视频了。' : '视频保存失败,请稍后重试!';
        }

        return $this->renderJson(compact('code', 'msg', 'err'));
    }

    protected function getVideoId($url) {
        return md5($url);
    }

    protected function getShareUrlFromContent($content) {
        $url = '';

        preg_match("/https:\/\/[\w\.]+(\/\w+){1,}\/?/i", $content, $matches);
        if (!empty($matches)) {
            $url = $matches[0];
        }

        return $url;
    }

    //保存分享视频
    protected function saveShareVideo($content, $title, $tagName) {
        $done = true;

        $shareUrl = $this->getShareUrlFromContent($content);
        if (!empty($shareUrl)) {
            $done = $done && $this->saveBotTask($shareUrl);

            if (!empty($tagName)) {
                $done = $done && $this->saveVideoToTag($shareUrl, $tagName);
            }

            //保存任务日志
            $this->saveTaskLog($shareUrl, $title, $tagName);

            //调用HeroUnion联盟接口,提交新的数据抓取任务
            if (!empty(FSC::$app['config']['heroUnionEnable'])) {
                $platformName = Html::getShareVideosPlatform($shareUrl);
                $heroUnionConfig = FSC::$app['config']['heroUnion'];
                $this->addHeroUnionTask($shareUrl, $heroUnionConfig['supportedPlatforms'][$platformName]);
            }
        }

        return $done;
    }

    //保存分享视频到任务文件
    protected function saveBotTask($url) {
        $task_dir = __DIR__ . '/../../../runtime/' . FSC::$app['config']['tajian']['task_dir'];
        if (!is_dir($task_dir)) {
            mkdir($task_dir, 0755, true);
        }

        $video_id = $this->getVideoId($url);
        $filepath = realpath($task_dir) . "/{$video_id}.task";
        return file_put_contents($filepath, $url) !== false;
    }

    //保存分享视频到tag分类
    //TODO: 如果高并发,需要避免数据被覆盖的问题
    protected function saveVideoToTag($url, $tagName) {
        $tag_dir = __DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'] . FSC::$app['config']['tajian']['tag_dir'];
        if (!is_dir($tag_dir)) {
            mkdir($tag_dir, 0755, true);
        }

        $video_id = $this->getVideoId($url);
        $filepath = realpath($tag_dir) . "/{$tagName}.txt";
        if (file_exists($filepath)) {
            $content = file_get_contents($filepath);
            $videos = explode("\n", $content);
            $last_id = array_pop($videos);
            if (!empty($last_id)) {
                array_push($videos, $last_id);
            }

            if (!in_array($video_id, $videos)) {
                array_push($videos, $video_id);
            }

            return file_put_contents($filepath, implode("\n", $videos)) !== false;
        }else {
            return file_put_contents($filepath, $vidoe_id) !== false;
        }
    }

    //保存任务日志
    protected function saveTaskLog($url, $title, $tagName) {
        $logFile = __DIR__ . '/../../../runtime/' . FSC::$app['config']['tajian']['task_log'];

        $saved = true;
        try {
            $fp = fopen($logFile, 'a');

            $content = array(
                'url' => $url,
                'title' => $title,
                'tag' => $tagName,
                'created' => time(),
            );

            if (!empty(FSC::$app['config']['multipleUserUriParse'])) {
                $content['user'] = !empty(FSC::$app['user_id']) ? FSC::$app['user_id'] : '';
            }

            fwrite($fp, json_encode($content) . "\n");
        }catch(Exception $err) {
            $saved = false;
        }

        return $saved;
    }

    protected function sign($params, $token) {                    //对参数做MD5签名
        ksort($params);
        return md5( json_encode($params, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . $token );
    }

    //提交视频抓取任务到HeroUnion英雄联盟
    protected function addHeroUnionTask($shareUrl, $platform) {
        $notify_prefix = '';
        if (!empty(FSC::$app['config']['multipleUserUriParse']) && !empty(FSC::$app['user_id'])) {
            $notify_prefix = '/' . FSC::$app['user_id'];
        }

        $heroUnionConfig = FSC::$app['config']['heroUnion'];
        $params = array(
            'uuid' => $heroUnionConfig['uuid'],
            'url' => $shareUrl,
            'platform' => $platform,
            'contract' => $heroUnionConfig['contract'],
            'data_mode' => $heroUnionConfig['data_mode'],
            'country' => $heroUnionConfig['country'],
            'lang' => $heroUnionConfig['lang'],
            'notify_url' => $heroUnionConfig['notify_domain'] . $notify_prefix . '/frontapi/hunotify/',
        );
        $params['sign'] = $this->sign($params, $heroUnionConfig['token']);

        $api = $heroUnionConfig['server_url'] . '/api/newtask/';
        $timeout = 10;
        $pc = false;
        $headers = array("Content-Type: application/json");
        //以json格式post数据
        $res = $this->request($api, json_encode($params), $timeout, $pc, $headers);

        return !empty($res) && $res['status'] == 200 ? $res['result'] : false;
    }

    //保存快捷方式
    protected function saveUrlShortCut($video_id, $task_url) {
        $data_dir = __DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'] . FSC::$app['config']['tajian']['data_dir'];
        if (!is_dir($data_dir)) {
            mkdir($data_dir, 0755, true);
        }

        $shortUrlContent = <<<eof
[InternetShortcut]
URL={$task_url}
eof;

        $filepath = realpath($data_dir) . "/{$video_id}.url";
        return file_put_contents($filepath, $shortUrlContent) !== false;
    }

    //保存描述文件:标题和图片
    protected function saveDescriptionFiles($video_id, $task_data) {
        $data_dir = __DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'] . FSC::$app['config']['tajian']['data_dir'];
        if (!is_dir($data_dir)) {
            mkdir($data_dir, 0755, true);
        }

        $done = true;

        try {
            $data_dir = realpath($data_dir);

            //保存标题
            $filepath_title = "{$data_dir}/{$video_id}_title.txt";
            file_put_contents($filepath_title, $task_data['title']);

            //保存图片文件
            if (!empty($task_data['cover_base64'])) {
                $filepath_cover = "{$data_dir}/{$video_id}.{$task_data['cover_type']}";
                file_put_contents($filepath_cover, base64_decode($task_data['cover_base64']));

                $filepath_desc = "{$data_dir}/{$video_id}_cover.txt";
                file_put_contents($filepath_desc, "{$video_id}.{$task_data['cover_type']}");
            }else if (!empty($task_data['cover'])) {
                $filepath_desc = "{$data_dir}/{$video_id}_cover.txt";
                file_put_contents($filepath_desc, "{$task_data['cover']}");
            }
        }catch(Exception $err) {
            $done = false;
        }

        return $done;
    }

    //HeroUnion任务数据通知回传接口
    /**
     * task_id
     * task_result
     * timestamp
     * sign
     **/
    public function actionHuNotify() {
        $task_id = $this->post('task_id', '');
        $task_result = $this->post('task_result', '');
        $timestamp = $this->post('timestamp', '');
        $sign = $this->post('sign', '');

        $code = 1;
        $msg = '';
        $err = '';

        //参数检查
        if (empty($task_id) || empty($task_result) || empty($timestamp) || empty($sign)) {
            $code = 0;
            $err = '参数缺失!';
        }

        $heroUnionConfig = FSC::$app['config']['heroUnion'];

        //验证签名
        if ($code == 1) {
            $checkParams = array(
                'task_id' => $task_id,
                'task_result' => $task_result,
                'timestamp' => $timestamp,
            );
            $mySign = $this->sign($checkParams, $heroUnionConfig['token']);

            if (strtolower($mySign) != strtolower($sign)) {
                $code = 0;
                $err = '签名验证不通过!';
            }else if (!empty($task_result['done'])) {    //如果任务成功抓取到数据
                $video_id = $this->getVideoId($task_result['url']);
                $saveUrlRes = $this->saveUrlShortCut($video_id, $task_result['url']);
                $saveDescRes = $this->saveDescriptionFiles($video_id, $task_result);
                if (!$saveUrlRes) {
                    $code = 0;
                    $err = '网址快捷方式文件保存失败!';
                }else if (!$saveDescRes) {
                    $code = 0;
                    $err = '标题文件、图片及其描述文件保存失败!';
                }else {
                    $msg = '视频相关文件保存完成。';
                }
            }
        }

        return $this->renderJson(compact('code', 'msg', 'err'));
    }

    //TODO: 把自己的收藏视频压缩成zip包
    protected function createZip() {
        
    }

    //TODO: 打包下载自己的收藏记录
    public function actionDownloadfavs() {
        $this->createZip();
        exit;
    }

    //请求频率限制
    /**
     * key: 检查频率限制的唯一标识
     * max: 最大次数
     * time: 检查时间,单位:秒
     */
    protected function requestLimit($key, $max, $time) {
        $isLimited = false;

        try {
            if(session_status() !== PHP_SESSION_ACTIVE) {
                session_start();
            }

            $current_time = microtime(true)*1000;

            $field = md5("requestLimit_by_{$key}");
            $field_update_time = "{$field}_updated";
            if (!empty($_SESSION[$field]) && !empty($_SESSION[$field_update_time]) && $current_time - $_SESSION[$field_update_time] <= $time*1000) {
                $_SESSION[$field] ++;
            }else {
                $_SESSION[$field] = 1;
                $_SESSION[$field_update_time] = $current_time;
            }

            if ($_SESSION[$field] > $max) {
                $isLimited = true;
            }
        }catch(Exception $e) {
            $this->logError("Request limit by session failed: " . $e->getMessage());
        }

        return $isLimited;
    }

    //生成4随机数,并保存生成时间,10 分钟内有效
    protected function generateRandSmsCode() {
        if(session_status() !== PHP_SESSION_ACTIVE) {
            session_start();
        }

        $rndCode = rand(1000, 9999);        //4位随机数

        $_SESSION['randSmsCode'] = $rndCode;
        $_SESSION['randSmsCode_created'] = time();

        return $rndCode;
    }

    //短信验证码 10 分钟内有效
    protected function getMySmsCode() {
        if(session_status() !== PHP_SESSION_ACTIVE) {
            session_start();
        }

        $rndCode = !empty($_SESSION['randSmsCode']) ? $_SESSION['randSmsCode'] : 0;
        $rndCode_created = !empty($_SESSION['randSmsCode_created']) ? $_SESSION['randSmsCode_created'] : 0;
        $current_time = time();

        $max_cache_time = !empty(FSC::$app['config']['sms_code_cache_time']) ? FSC::$app['config']['sms_code_cache_time'] : 600;
        if (!empty($rndCode_created) && $current_time - $rndCode_created > $max_cache_time) {
            $rndCode = 0;
        }

        return $rndCode;
    }

    //获取短信验证码
    public function actionSendsmscode() {
        $ip = $this->getUserIp();
        $check_time = 300;          //5 分钟内
        $max_time_in_minutes = 3;   //最多 3 次

        $isUserGotRequestLimit = $this->requestLimit($ip, $max_time_in_minutes, $check_time);
        if ($isUserGotRequestLimit) {
            $this->logError("Request limit got, ip: {$ip}");
            throw new Exception('Oops,操作太快了,请喝杯咖啡休息会吧...');
        }

        //返回给视图的变量
        $code = 0;
        $msg = '';
        $err = '';

        $postParams = $this->post();
        if (!empty($postParams)) {
            $cellphone = $this->post('phoneNum', '');
            $action = $this->post('action', 'register');

            if (empty($cellphone) || Common::isCellphoneNumber($cellphone) == false) {
                $err = "手机号码格式错误,请填写正确的手机号码";
            }else {
                //判断是否已经注册
                $userDataDir = Common::getUserDataDir($cellphone);
                if (!empty($userDataDir) && $action == 'register') {
                    $err = '你已经注册,请直接登录';
                    return $this->renderJson(compact('code', 'msg', 'err'));
                }else if (empty($userDataDir) && $action == 'login') {
                    $err = '你还没注册,请先注册';
                    return $this->renderJson(compact('code', 'msg', 'err'));
                }


                //尝试发送短信验证码
                $params = array(
                    'phoneNumber' => $cellphone,
                    'codeNumber' => $this->generateRandSmsCode(),
                    'action' => $action,
                );
                $params['sign'] = $this->sign($params, FSC::$app['config']['service_3rd_api_key']);

                $api = FSC::$app['config']['service_3rd_api_domain'] . '/aliyun/sendverifycode/';
                $timeout = 10;
                $pc = false;
                $headers = array("Content-Type: application/json");
                //以json格式post数据
                $res = $this->request($api, json_encode($params), $timeout, $pc, $headers);

                if (!empty($res) && $res['status'] == 200) {
                    $resData = json_decode($res['result'], true);
                    if ($resData['code'] == 1) {
                        $code = 1;
                        $msg = '短信验证码已成功发送';
                    }else {
                        $err = '短信验证码发送失败:' . $resData['message'];
                    }
                }else {
                    $err = '短信验证码发送失败,请稍后再试';
                }
            }
        }

        return $this->renderJson(compact('code', 'msg', 'err'));
    }

    //新用户注册
    public function actionCreateuser() {
        $ip = $this->getUserIp();
        $check_time = 120;          //2 分钟内
        $max_time_in_minutes = 5;   //最多 5 次

        $isUserGotRequestLimit = $this->requestLimit($ip, $max_time_in_minutes, $check_time);
        if ($isUserGotRequestLimit) {
            $this->logError("Request limit got, ip: {$ip}");
            throw new Exception('Oops,操作太快了,请喝杯咖啡休息会吧...');
        }

        //返回给视图的变量
        $code = 0;
        $msg = '';
        $err = '';
        $shareUrl = '';

        //用户提交的数据检查
        $postParams = $this->post();
        if (!empty($postParams)) {
            $friends_code = $this->post('friendscode', '');
            $cellphone = $this->post('username', '');
            $sms_code = $this->post('smscode', '');

            if (empty($friends_code) || empty($cellphone) || empty($sms_code)) {
                $err = "请填写注册邀请码、手机号码和短信验证码哦";
            }else if (Common::isCellphoneNumber($cellphone) == false) {
                $err = "手机号码格式错误,请填写正确的手机号码";
            }else if (Common::isFriendsCode($friends_code) == false) {
                $err = "邀请码格式错误,请填写邀请你的朋友的手机号码末 6 位,还可以加客服微信索取";
            }else if (Common::existFriendsCode($friends_code) == false) {
                $err = "邀请码不存在,请填写邀请你的朋友的手机号码末 6 位,或者加客服微信索取";
            }

            //验证短信验证码是否正确
            $mySmsCode = $this->getMySmsCode();
            if (empty($mySmsCode) || $mySmsCode != $sms_code) {
                $err = "{$sms_code} 验证码已过期或错误,请检查是否输入正确";
            }

            if (empty($err)) {      //如果数据检查通过,尝试注册新用户
                $userDataDir = Common::getUserDataDir($cellphone);
                if (empty($userDataDir)) {
                    $newUser = Common::saveUserIntoSession($cellphone, $friends_code);
                    if (!empty($newUser)) {
                        Common::saveFriendsCode($cellphone);
                        Common::initUserData($cellphone, $friends_code);

                        $shareUrl = "/{$newUser['username']}/";
                        $msg = "注册完成,开始收藏你喜欢的视频吧,正在为你跳转到专属网址...";
                        $code = 1;
                    }else {
                        $err = '注册失败,请稍后再试';
                    }
                }else {
                    $username = Common::getMappedUsername($cellphone);
                    $shareUrl = "/{$username}/";
                    $msg = "已注册,正在为你跳转到专属网址...";
                    $code = 1;
                }
            }
        }

        return $this->renderJson(compact('code', 'msg', 'err', 'shareUrl'));
    }

    //用户登录
    public function actionLoginuser() {
        $ip = $this->getUserIp();
        $check_time = 120;          //2 分钟内
        $max_time_in_minutes = 5;   //最多 5 次

        $isUserGotRequestLimit = $this->requestLimit($ip, $max_time_in_minutes, $check_time);
        if ($isUserGotRequestLimit) {
            $this->logError("Request limit got, ip: {$ip}");
            throw new Exception('Oops,操作太快了,请喝杯咖啡休息会吧...');
        }

        //返回给视图的变量
        $code = 0;
        $msg = '';
        $err = '';
        $shareUrl = '';

        //用户提交的数据检查
        $postParams = $this->post();
        if (!empty($postParams)) {
            $cellphone = $this->post('username', '');
            $sms_code = $this->post('smscode', '');

            if (empty($cellphone) || empty($sms_code)) {
                $err = "请填写注册邀请码、手机号码和短信验证码哦";
            }else if (Common::isCellphoneNumber($cellphone) == false) {
                $err = "手机号码格式错误,请填写正确的手机号码";
            }else if (Common::getUserDataDir($cellphone) == false) {
                $err = "{$cellphone}还没注册哦,先去注册吧";
            }

            //验证短信验证码是否正确
            $mySmsCode = $this->getMySmsCode();
            if (empty($mySmsCode) || $mySmsCode != $sms_code) {
                $err = "{$sms_code} 验证码已过期或错误,请检查是否输入正确";
            }

            if (empty($err)) {
                $userDataDir = Common::getUserDataDir($cellphone);
                if (empty($userDataDir)) {
                    $err = '你还没注册,请先注册';
                }
            }

            if (empty($err)) {      //如果数据检查通过,尝试登录
                $newUser = Common::saveUserIntoSession($cellphone);
                if (!empty($newUser)) {
                    $shareUrl = "/{$newUser['username']}/";

                    $msg = "登录成功,开始收藏你喜欢的视频吧";
                    $code = 1;
                }else {
                    $err = '登录失败,请稍后重试';
                }
            }
        }

        return $this->renderJson(compact('code', 'msg', 'err', 'shareUrl'));
    }

    //TODO: 分类管理

    //TODO: 昵称设置

    //TODO: 视频管理


}