<?php
/**
 *  Guojiz网址导航系统
 *	www.guojiz.com
 *  作者：梦雨
 *  @ QQ50361804
 */
namespace app\index\controller;

use think\Controller;
use think\Db;
use Captcha\Captcha;
use think\Cache;
use think\Config;
use think\Session;
use think\Request;
use app\index\model\News as NewsModel;
use app\index\model\Html as HtmlModel;
use app\index\model\Member as MemberModel;
use WordAnalysis\Analysis;
class User extends Controller
{
    public function _initialize()
    {
        if (session('userid') || session('username')) {
            $uid = session('userid');
            $member = Db::name('member')->where("userid = {$uid}")->find();
            $this->assign('member', $member);
        }
        parent::_initialize();
    }
    public function comment()
    {
        if (!session('userid') || !session('username')) {
            $this->error('亲！请登录', url('login/index'));
        } else {
            $s = input('s');
            if ($s == 'p' || $s == '') {
                $comment = Db::name('comment');
                $uid = session('userid');
                $tptc = $comment->alias('c')->join('news f', 'f.id=c.fid')->field('c.*,f.title')->where("c.uid = {$uid}")->order('c.id desc')->paginate(8);
                $this->assign('tptc', $tptc);
            } elseif ($s == 't') {
                $kga = config('web.XIN_G');
                if ($kga == 1) {
                    $shiming = 1;
                } else {
                    $shiming = Db::name('shiming')->where('uid', session('userid'))->value('status');
                }
                $this->assign('shiming', $shiming);
                $news = Db::name('news');
                if (request()->isPost()) {
                    $times = Db::name('member')->where('userid', session('userid'))->value('times');
                    if ($times >= time()) {
                        $vip = Db::name('member')->where('userid', session('userid'))->value('vip');
                    } else {
                        $vip = 0;
                    }
                    $data = input('post.');
                    $data['time'] = time();
                    if ($vip == 1 || $vip == 2) {
                        $data['show'] = 1;
                    } else {
                        $data['show'] = 0;
                    }
                    if (warning() == shul()) {
                        $times = Db::name('member')->where('userid', session('userid'))->value('times');
                        if ($times >= time()) {
                            $vip = Db::name('member')->where('userid', session('userid'))->value('vip');
                        } else {
                            $vip = 0;
                        }
                    } else {
                        $vip = 0;
                    }
                    if ($vip == 1 || $vip == 2) {
                        $data['show'] = 1;
                    } else {
                        $data['show'] = 0;
                    }
                    $data['view'] = 1;
                    $data['uid'] = session('userid');
                    if ($data['keywords'] == '') {
                        $_ks = preg_replace("/[a-z,A-Z]|[0-9]/", "", $data['content']);
                        $data['keywords'] = Analysis::getKeywords($_ks);
                    }
                    $data['description'] = mb_substr(strip_tags($data['content']), 0, 200, 'utf-8');
                    $data['title'] = remove_xss($data['title']);
                    $data['content'] = remove_xss($data['content']);
                    $data['fujian'] = remove_xss($data['fujian']);
                    if (warning() == shul()) {
                        $data['jifen'] = remove_xss($data['jifen']);
                    } else {
                        $data['jifen'] = 0;
                    }
                    $member = Db::name('member');
                    $uid = session('userid');
                    $score = config('point.GJJF-TG');
                    $point = Db::name('member')->where('userid', $uid)->value('point');
                    $points = $score + $point;
                    if ($points < 0) {
                        $this->success('' . config('point.GJJF-MING') . '当前投稿需要扣除积分 你积分不足');
                    } else {
                        if ($news->insert($data)) {
                            Db::name('member')->where('userid', $uid)->setInc('point', $score);
                            if (guojiz('amailt') == 1) {
                                send_mail(guojiz('mailUsername'), '有人投稿了' . guojiz('title'), '有人在' . guojiz('title') . '提交文章投稿了,快去看看吧');
                            }
                            return json(array('code' => 200, 'msg' => '添加成功'));
                        } else {
                            return json(array('code' => 0, 'msg' => '添加失败'));
                        }
                    }
                }
                $category = Db::name('newscate');
                $tptc = $category->select();
                $this->assign('tptc', $tptc);
                $tags = config('web.WEB_TAG');
                $tagss = explode(',', $tags);
                $this->assign('tagss', $tagss);
            } elseif ($s == 'index') {
                $news = Db::name('news');
                $uid = session('userid');
                $tptc = $news->where("uid = {$uid}")->order('id desc')->paginate(10);
                $this->assign('tptc', $tptc);
            }
            $this->assign('s', $s);
            return view(WEB('user_comment.html'));
        }
    }
    public function kuai()
    {
        if (!session('userid') || !session('username')) {
            Logoi();
            $this->error('亲！请登录', url('login/index'));
        } else {
            $category = Db::name('category');
            $html = new HtmlModel();
            if (request()->isPost()) {
                $data = input('post.');
                $data['time'] = time();
                $data['view'] = 1;
                $data['uid'] = session('userid');
                $config = array('lianjie' => input('lianjie'));
                $data['content'] = remove_xss($data['content']);
                $data['title'] = remove_xss($data['title']);
                $data['lianjie'] = remove_xss($data['lianjie']);
                $data['qq'] = remove_xss($data['qq']);
                $data['qz'] = 1;
                $data['ks'] = 1;
                $data['keywords'] = remove_xss($data['keywords']);
                if (warning() == shul()) {
                    $times = Db::name('member')->where('userid', session('userid'))->value('times');
                    if ($times >= time()) {
                        $vip = Db::name('member')->where('userid', session('userid'))->value('vip');
                    } else {
                        $vip = 0;
                    }
                } else {
                    $vip = 0;
                }
                $hasuserhead = $html->where($config)->count();
                $this->assign('hasuserhead', $hasuserhead);
                if ($hasuserhead >= 1) {
                    $this->error('网址也存在');
                }
                if ($vip == 1 || $vip == 2) {
                    if (isset($_REQUEST['authcode'])) {
                         session_start();
                        if (strtolower($_REQUEST['authcode']) == $_SESSION['checkkey']) {
                            //strtolower转化为小写的函数
                            $data['description'] = mb_substr(strip_tags($data['content']), 0, 200, 'utf-8');
                            if ($html->add($data)) {
                                return json(array('code' => 200, 'msg' => '添加成功'));
                            } else {
                                return json(array('code' => 0, 'msg' => '添加失败'));
                            }
                        } else {
                            $this->error('验证码错误');
                        }
                    }
                } else {
                    $uid = session('userid');
                    $score = config('point.GJJF-KUAI');
                    $point = Db::name('member')->where('userid', $uid)->value('point');
                    if ($point < $score && $score > 0) {
                        $this->success('' . config('point.GJJF-MING') . '不足请充值');
                    } else {
                        if (isset($_REQUEST['authcode'])) {
                            // session_start();
                            if (strtolower($_REQUEST['authcode']) == $_SESSION['checkkey']) {
                                //strtolower转化为小写的函数
                                $data['description'] = mb_substr(strip_tags($data['content']), 0, 200, 'utf-8');
                                if ($html->add($data)) {
                                    points($score, session('userid'), '快审');
                                    if (config('lm.openk') == 1) {
			                            $arr = explode(',', config('lm.www'));
			                            foreach ($arr as $k => $v) {
			                                $url = $v . '/index.php/html/lm.html';
			                                $postData = array(
			                                	'tid' => input('tid'),
			                                	'title' => input('title'), 
			                                	'lianjie' => input('lianjie'), 
			                                	'qq' => input('qq'), 
			                                	'content' => input('content'), 
			                                	'www' => $_SERVER['HTTP_HOST'], 
			                                	'keywords' => input('keywords')
			                                );
			                                $postData = http_build_query($postData);
			                                $ch = curl_init();
			                                curl_setopt($ch, CURLOPT_URL, $url);
			                                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			                                curl_setopt($ch, CURLOPT_POST, 1);
			                                curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
			                                $result = curl_exec($ch);
			                                curl_close($ch);
			                            }
			                        }
                                    return json(array('code' => 200, 'msg' => '添加成功'));
                                } else {
                                    return json(array('code' => 0, 'msg' => '添加失败'));
                                }
                            } else {
                                $this->error('验证码错误');
                            }
                        }
                    }
                }
            }
            $tptc = $category->select();
            $this->assign('tptc', $tptc);
            $tags = config('web.WEB_TAG');
            $tagss = explode(',', $tags);
            $this->assign('tagss', $tagss);
            return view(WEB('user_kuai.html'));
        }
    }
    public function set()
    {
        if (!session('userid') || !session('username')) {
            $this->error('亲！请登录', url('login/index'));
        } else {
            $shiming = Db::name('shiming')->where('uid', session('userid'))->value('status');
            $this->assign('shiming', $shiming);
             $hasuserhead = Db::name('shiming')->where('uid', session('userid'))->count();
             if ($hasuserhead > 0) {
                $title = Db::name('shiming')->where('uid', session('userid'))->value('title');
                $this->assign('title', $title);
                $haoma = Db::name('shiming')->where('uid', session('userid'))->value('haoma');
                $this->assign('haoma', $haoma);
                $sfz = Db::name('shiming')->where('uid', session('userid'))->value('sfz');
                $this->assign('sfz', $sfz);
                $sfzf = Db::name('shiming')->where('uid', session('userid'))->value('sfzf');
                $this->assign('sfzf', $sfzf);
                $sfzq = Db::name('shiming')->where('uid', session('userid'))->value('sfzq');
                $this->assign('sfzq', $sfzq);
            } else {
                $title = '';
                $this->assign('title', $title);
                $haoma = '';
                $this->assign('haoma', $haoma);
                $sfz = '';
                $this->assign('sfz', $sfz);
                $sfzf = '';
                $this->assign('sfzf', $sfzf);
                $sfzq = '';
                $this->assign('sfzq', $sfzq);
            }
            $member = new MemberModel();
            $uid = session('userid');
            $tptc = $member->where(array('userid' => $uid))->find();
            if (request()->isPost()) {
                $data = $this->request->post();
                $data['userid'] = $uid;
                $_data['username'] = remove_xss($data['username']);
                if (is_numeric($_data['username']) || is_numeric(substr($_data['username'], 0, 1)) || mb_strwidth($_data['username']) < 4) {
                    return json(array('code' => 0, 'msg' => '不能是纯数字或数字开头，一个汉字算2个字符'));
                }
                $_data['userhome'] = remove_xss($data['userhome']);
                $_data['description'] = remove_xss($data['description']);
                $_data['sex'] = $data['sex'];
                $_data['usermail'] = $data['usermail'];
                if ($member->save($_data, ['userid' => $uid])) {
                    return json(array('code' => 200, 'msg' => '修改成功'));
                } else {
                    return json(array('code' => 0, 'msg' => '修改失败'));
                }
            }
            $uid = session('userid');
            $this->assign('tptc', $tptc);
            $this->assign('uid', $uid);
            return view(WEB('user_set.html'));
        }
    }
    public function setedit()
    {
        if (!session('userid') || !session('username')) {
            $this->error('亲！请登录', url('login/index'));
        } else {
            $member = new MemberModel();
            $uid = session('userid');
            $tptc = $member->find($uid);
            if (request()->isPost()) {
                $data = $this->request->post();
                $password = input('password');
                $datam['password'] = substr(md5($password), 8, 16);
                if ($member->save($datam, ['userid' => $uid])) {
                    return json(array('code' => 200, 'msg' => '修改成功'));
                } else {
                    return json(array('code' => 0, 'msg' => '修改失败'));
                }
            }
            $this->assign('tptc', $tptc);
            return view();
        }
    }
    public function headedit()
    {
        if (!session('userid') || !session('username')) {
            $this->error('亲！请登录', url('login/index'));
        } else {
            $member = new MemberModel();
            $uid = session('userid');
            if (request()->isPost()) {
                $data = input('post.');
                $_data['userhead'] = remove_xss($data['userhead']);
                if ($member->save($_data, ['userid' => $uid])) {
                    return json(array('code' => 200, 'msg' => '修改成功'));
                } else {
                    return json(array('code' => 0, 'msg' => '修改失败'));
                }
            }
            $tptc = $member->find($uid);
            $this->assign('tptc', $tptc);
            return view();
        }
    }
    public function cz()
    {
        if (!session('userid') || !session('username')) {
            $this->error('亲！请登录', url('login/index'));
        } else {
            Logoi();
            return view(WEB('user_cz.html'));
        }
    }
    public function shiming()
    {
        if (!session('userid') || !session('username')) {
            $this->error('亲！请登录', url('login/index'));
        } else {
            $shiming = Db::name('shiming')->where('uid', session('userid'))->value('status');
            $this->assign('shiming', $shiming);
            $hasuserhead = Db::name('shiming')->where('uid', session('userid'))->count();
            if ($hasuserhead > 0) {
                $title = Db::name('shiming')->where('uid', session('userid'))->value('title');
                $this->assign('title', $title);
                $haoma = Db::name('shiming')->where('uid', session('userid'))->value('haoma');
                $this->assign('haoma', $haoma);
                $sfz = Db::name('shiming')->where('uid', session('userid'))->value('sfz');
                $this->assign('sfz', $sfz);
                $sfzf = Db::name('shiming')->where('uid', session('userid'))->value('sfzf');
                $this->assign('sfzf', $sfzf);
                $sfzq = Db::name('shiming')->where('uid', session('userid'))->value('sfzq');
                $this->assign('sfzq', $sfzq);
            } else {
                $title = '';
                $this->assign('title', $title);
                $haoma = '';
                $this->assign('haoma', $haoma);
                $sfz = '';
                $this->assign('sfz', $sfz);
                $sfzf = '';
                $this->assign('sfzf', $sfzf);
                $sfzq = '';
                $this->assign('sfzq', $sfzq);
            }
            if (request()->isPost()) {
                $data = input('post.');
                $_data['uid'] = session('userid');
                $shiming = Db::name('shiming');
                $_data['title'] = remove_xss($data['title']);
                $_data['haoma'] = remove_xss($data['haoma']);
                $_data['sfz'] = remove_xss($data['sfz']);
                $_data['sfzf'] = remove_xss($data['sfzf']);
                $_data['sfzq'] = remove_xss($data['sfzq']);
                $_data['status'] = -1;
                $_data['time'] = time();
                //查询是否有申请记录
                $map['uid'] = session('userid');
                $hasuserhead = $shiming->where($map)->count();
                if ($hasuserhead > 0) {
                    if (!Db::name('shiming')->where('uid', session('userid'))->data($_data)->update($data)) {
                        $this->error('更新失败');
                    } else {
                        $this->success('更新成功');
                    }
                } else {
                    if (!Db::name('shiming')->insert($_data)) {
                        $this->error('提交失败');
                    } else {
                        //  Db::name('member')->where('userid', session('userid'))->setDec('point', remove_xss($data['jifen']));
                        $this->success('提交成功，请等待处理...！');
                    }
                }
            }
            return view(WEB('user_shiming.html'));
        }
    }
    public function rl()
    {
        if (!session('userid') || !session('username')) {
            return json(array('code' => 0, 'msg' => '请登录'));
        } else {
            if (warning() == shul()) {
                ini_set("user_agent", "Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)");
                $conn = file_get_contents('http://' . input('lianjie'));
                if (strpos($conn, input('md5')) === false) {
                    return json(array('code' => 0, 'msg' => '请确定在首页添加了代码'));
                } else {
                    Db::name('html')->where(array('lianjie' => input('lianjie')))->setField('uid', session('userid'));
                    return json(array('code' => 200, 'msg' => '认领成功'));
                }
            }
        }
    }
    //卡密充值
    public function kamicz()
    {
        if (!session('userid') || !session('username')) {
            $this->error('亲！请登录', url('login/index'));
        } else {
            $urls = Db::name('kamicate')->select();
            $this->assign('cms', $urls);
            $dlist = Db::name('kami')->alias('d')->join('kamicate c', 'd.cid=c.id')->field('d.id,d.open_time,d.passwdstr,c.par')->where('uid', session('userid'))->order('id desc')->select();
            $this->assign('dlist', $dlist);
            Logoi();
            return view(WEB('user_kamicz.html'));
        }
    }
    
}