<?php
/**
 *  Guojiz网址导航系统
 *	www.guojiz.com
 *  作者：梦雨
 *  @ QQ50361804
 */
namespace app\index\controller;
use think\Controller;
use think\Db;
use app\index\model\Html as HtmlModel;
class Html extends Controller
{
    public function _initialize()
    {
        if (!session('userid') || !session('username')) {
        $show['show'] = 1;
        $tpto = Db::name('category')->where($show)->order('sort desc')->limit(120)->select();
        $this->assign('tpto', $tpto);
        $GZdh = Db::name('links')->where($show)->order('px desc')->select();
        $this->assign('GZdh', $GZdh);
        $keyss = Db::name('member')->where('userid = 1')->value('key');
        $this->assign('keyss', $keyss);
        $newscate = Db::name('newscate')->where($show)->order('id desc')->limit(120)->select();
        $this->assign('newscate', $newscate);
        $guojizgg = Db::name('banner')->where($show)->order('id desc')->select();
        $this->assign('guojizgg', $guojizgg);
        $html = Db::name('html')->where('open = 1')->select();
        $this->assign('html', $html);
        $htmls = Db::name('html')->order('id desc')->select();
        $this->assign('htmls', $htmls);
        $news = Db::name('news')->where($show)->order('id desc')->select();
        $this->assign('news', $news);
        $newscate = Db::name('newscate')->where($show)->order('id desc')->select();
        $this->assign('newscate', $newscate);
        $wang = Db::name('wang')->where('open = 1')->order('id desc')->select();
        $this->assign('wang', $wang);
        $wangcate = Db::name('wangcate')->where($show)->order('id desc')->select();
        $this->assign('wangcate', $wangcate);
        $guojizcai = Db::name('html')->where('open = 1')->orderRaw('rand()')->select();
        $this->assign('guojizcai', $guojizcai);
        $dan = Db::name('dan')->where($show)->order('id desc')->select();
        $this->assign('dan', $dan);
        //分类展示网址
        $artbycatelist = Db::name('category')->where($show)->order('sort desc')->select();
        foreach ($artbycatelist as $k => $v) {
            $artbycatelist[$k]['artlists'] = get_articles_by_cid($v['id'], 5);
        }
        $this->assign('artbycatelist', $artbycatelist);

        //最近30天文章排行
        $maptop30['open'] = 1;
        $maptop30['time'] = array('egt', strtotime("-1 month"));
        $news_top30 = Db::name('news')->where($show)->order('view desc')->select();
        $this->assign('news_top30', $news_top30);  
          
          
        } else {
        $show['show'] = 1;
        $tpto = Db::name('category')->where($show)->order('sort desc')->limit(120)->select();
        $this->assign('tpto', $tpto);
        $GZdh = Db::name('links')->where($show)->order('px desc')->select();
        $this->assign('GZdh', $GZdh);
        $keyss = Db::name('member')->where('userid = 1')->value('key');
        $this->assign('keyss', $keyss);
        $newscate = Db::name('newscate')->where($show)->order('id desc')->limit(120)->select();
        $this->assign('newscate', $newscate);
        $guojizgg = Db::name('banner')->where($show)->order('id desc')->select();
        $this->assign('guojizgg', $guojizgg);
        $html = Db::name('html')->where('open = 1')->select();
        $this->assign('html', $html);
        $htmls = Db::name('html')->order('id desc')->select();
        $this->assign('htmls', $htmls);
        $news = Db::name('news')->where($show)->order('id desc')->select();
        $this->assign('news', $news);
        $newscate = Db::name('newscate')->where($show)->order('id desc')->select();
        $this->assign('newscate', $newscate);
        $wang = Db::name('wang')->where('open = 1')->order('id desc')->select();
        $this->assign('wang', $wang);
        $wangcate = Db::name('wangcate')->where($show)->order('id desc')->select();
        $this->assign('wangcate', $wangcate);
        $guojizcai = Db::name('html')->where('open = 1')->orderRaw('rand()')->select();
        $this->assign('guojizcai', $guojizcai);
        $dan = Db::name('dan')->where($show)->order('id desc')->select();
        $this->assign('dan', $dan);
        //分类展示网址
        $artbycatelist = Db::name('category')->where($show)->order('sort desc')->select();
        foreach ($artbycatelist as $k => $v) {
            $artbycatelist[$k]['artlists'] = get_articles_by_cid($v['id'], 5);
        }
        $this->assign('artbycatelist', $artbycatelist);
            $uid = session('userid');
            $member = Db::name('member')->where("userid = {$uid}")->find();
            $this->assign('member', $member);
        //最近30天文章排行
        $maptop30['open'] = 1;
        $maptop30['time'] = array('egt', strtotime("-1 month"));
        $news_top30 = Db::name('news')->where($show)->order('view desc')->select();
        $this->assign('news_top30', $news_top30); 
        
      }
    }
    public function add()
    {
        $www = input('www');
        $wwws = $www;
        $this->assign('wwws', $wwws);
        $chaxuna = config('web.XIN_Y');
        $this->assign('chaxuna', $chaxuna);
        if ($chaxuna == -1) {
         $this->error(config('web.GZ_TS'));     
        } else {
        if (empty($www)) {
            $titles = '';
            $this->assign('titles', $titles);
            $descriptions = '';
            $this->assign('descriptions', $descriptions);
            $category = Db::name('category');
            $html = new HtmlModel();
            if (request()->isPost()) {
                $data = input('post.');
                $data['time'] = time();
                $data['open'] = config('web.WEB_OPE');
                $data['view'] = 1;
                $data['uid'] = 1;
                $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['keywords']= remove_xss($data['keywords']);	
                $hasuserhead = $html->where($config)->count();
                $this->assign('hasuserhead', $hasuserhead);
                if ($hasuserhead >= 1) {
                    $this->error('网址也存在');
                }
                if (isset($_REQUEST['authcode'])) {
                 //   session_start();
                    if (strtolower($_REQUEST['authcode']) == $_SESSION['authcode']) {
                        //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('验证码错误');
                    }
                }
            }
            $tptc = $category->select();
            $this->assign('tptc', $tptc);
            $tags = config('web.WEB_TAG');
            $tagss = explode(',', $tags);
            $this->assign('tagss', $tagss);
            $ico = '/favicon.ico';
            $icos = '';
            $this->assign('ico', $ico);
            $this->assign('icos', $icos);
            return view();
        } else {
            header("Content-Type:text/html;charset=utf-8");
            $http = preg_replace('|[0-9]+|', '', 'h0t1t2p0:3/4/');
            $url = '' . $http . '' . $www . '';
            $urlas = '' . $http . '' . $www . '/favicon.ico';
            if (@fopen($urlas, 'r')) {
                function get_photo($urls, $filename = '', $savefile = 'uploads/ico/')
                {
                    $imgArr = array('ico');
                    if (!$urls) {
                        return false;
                    }
                    if (!$filename) {
                        $name_arr = explode('.', $urls);
                        $ext = array_pop($name_arr);
                        if (!in_array($ext, $imgArr)) {
                            return false;
                        }
                        $filename = date("dMYHis") . '.' . $ext;
                    }
                    if (!is_dir($savefile)) {
                        mkdir($savefile, 0777);
                    }
                    if (!is_readable($savefile)) {
                        chmod($savefile, 0777);
                    }
                    $filename = $savefile . $filename;
                    ob_start();
                    readfile($urls);
                    $img = ob_get_contents();
                    ob_end_clean();
                    $size = strlen($img);
                    $fp2 = @fopen($filename, "a");
                    fwrite($fp2, $img);
                    fclose($fp2);
                    return $filename;
                }
                $img = get_photo('' . $url . '/favicon.ico');
                $icos = $img ? '<pre><img src="' . $img . '"></pre>' : "false";
                $ico = $img ? '/' . $img . '' : "false";
            } else {
                $icos = '<img src="/favicon.ico">';
                $ico = '/favicon.ico';
            }
            $this->assign('ico', $ico);
            $this->assign('icos', $icos);
            $ch = curl_init();
            $timeout = 10;
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HEADER, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
            function httpcode($url)
            {
                $ch = curl_init();
                $timeout = 3;
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_HEADER, 1);
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_exec($ch);
                return $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
                curl_close($ch);
            }
            $panduan = '' . httpcode($url);
            $contents = curl_exec($ch);
            if (false == $contents) {
                $this->error('请认真填写要收录的网站不要带http');
            } else {
                if ($panduan == 200) {
                    $data = file_get_contents($url);
                    if ($chaxuna == config('web.XIN_Zc')) {
                        function _charset($url)
                        {
                            $text = file_get_contents($url);
                            $mode = '/charset=(.*)\\"/iU';
                            preg_match($mode, $text, $result);
                            $modes = [];
                            if ($result == $modes) {
                                $result = 1;
                                return $result[1];
                            } else {
                                return $result[1];
                            }
                        }
                        $charset = _charset($url);
                        function _title($url, $charset)
                        {
                            $text = file_get_contents($url);
                            if ($charset == 'gb2312') {
                                $text = iconv('gb2312', 'utf-8', $text);
                            }
                            $mode = '/<title>(.*)<\\/title>/iU';
                            preg_match($mode, $text, $result);
                            $modes = [];
                            if ($result == $modes) {
                                $result = 1;
                                return $result[1];
                            } else {
                                return $result[1];
                            }
                        }
                        $titles = '' . ($title = _title($url, $charset));
                        $this->assign('titles', $titles);
                        function _description($url, $charset)
                        {
                            $text = file_get_contents($url);
                            if ($charset == 'gb2312') {
                                $text = iconv('gb2312', 'utf-8', $text);
                            }
                            $mode = '/<meta\\s+name=\\"description\\"\\s+content=\\"(.*)\\"\\s?\\/?>/iU';
                            preg_match($mode, $text, $result);
                            $modes = [];
                            if ($result == $modes) {
                                $result = 1;
                                return $result[1];
                            } else {
                                return $result[1];
                            }
                        }
                        $descriptions = $description = _description($url, $charset);
                        $titles = '' . ($title = _title($url, $charset));
                        $this->assign('descriptions', $descriptions);
                    } else {
                        $this->error(config('web.GZ_GJTS'));
                    }
                } else {
                    $descriptions = '';
                    $this->assign('descriptions', $descriptions);
                    $titles = '' ;
                    $this->assign('titles', $titles);
                }
            }
            $category = Db::name('category');
            $html = new HtmlModel();
            if (request()->isPost()) {
                $data = input('post.');
                $data['time'] = time();
                $data['open'] = config('web.WEB_OPE');
                $data['view'] = 1;
                $data['uid'] = 1;
                $config = array('lianjie' => input('lianjie'));
                $hasuserhead = $html->where($config)->count();
                $this->assign('hasuserhead', $hasuserhead);
                if ($hasuserhead >= 1) {
                    $this->error('网址也存在');
                }
                $data['description'] = mb_substr(strip_tags($data['content']), 0, 200, 'utf-8');
                if (isset($_REQUEST['authcode'])) {
                  //  session_start();
                    if (strtolower($_REQUEST['authcode']) == $_SESSION['authcode']) {
                        //strtolower转化为小写的函数
                        $data['description'] = mb_substr(strip_tags($data['content']), 0, 200, 'utf-8');
                $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['keywords']= remove_xss($data['keywords']);	
                        if ($html->add($data)) {
                            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);
            }
            }
       Logoing();return view();
    }
    public function guanyu()
    {
        return view();
    }
    public function ad()
    {
        return view();
    }
    public function doUploadPics()
    {
        $file = request()->file('FileName');
        $info = $file->move(ROOT_PATH . DS . 'uploads');
        if ($info) {
            $path = WEB_URL . DS . 'uploads' . DS . $info->getSaveName();
            echo str_replace("\\", "/", $path);
        }
    }
    public function doUploadPic()
    {
        if (!session('userid') || !session('username')) {
            $this->error('亲！请登录', url('login/index'));
        } else {
            $file = request()->file('FileName');
            $info = $file->move(ROOT_PATH . DS . 'uploads');
            if ($info) {
                $path = WEB_URL . DS . 'uploads' . DS . $info->getSaveName();
                echo str_replace("\\", "/", $path);
            }
        }
    }
    public function yjgx()
    {
        if (!session('userid') || !session('username')) {
            $this->error('不能搞破坏');
        } else {
            $id = input('id');
            $lianjie = Db::name('html')->where('id', $id)->value('lianjie');
            $http = preg_replace('|[0-9]+|', '', 'h0t1t2p0:3/4/');
            $url = '' . $http . '' . $lianjie . '';
            $chaxuna = config('web.XIN_Y');
            if ($chaxuna == '2') {
                header("Content-Type:text/html;charset=utf-8");
                $urlas = '' . $http . '' . $lianjie . '/favicon.ico';
                if (@fopen($urlas, 'r')) {
                    function _charset($url)
                    {
                        $text = file_get_contents($url);
                        $mode = '/charset=(.*)\\"/iU';
                        preg_match($mode, $text, $result);
                        $modes = [];
                        if ($result == $modes) {
                            $result = 1;
                            return $result[1];
                        } else {
                            return $result[1];
                        }
                    }
                    $charset = _charset($url);
                    function _title($url, $charset)
                    {
                        $text = file_get_contents($url);
                        if ($charset == 'gb2312') {
                            $text = iconv('gb2312', 'utf-8', $text);
                        }
                        $mode = '/<title>(.*)<\\/title>/iU';
                        preg_match($mode, $text, $result);
                        $modes = [];
                        if ($result == $modes) {
                            $result = 1;
                            return $result[1];
                        } else {
                            return $result[1];
                        }
                    }
                    $titles = '' . ($title = _title($url, $charset));
                    function _description($url, $charset)
                    {
                        $text = file_get_contents($url);
                        if ($charset == 'gb2312') {
                            $text = iconv('gb2312', 'utf-8', $text);
                        }
                        $mode = '/<meta\\s+name=\\"description\\"\\s+content=\\"(.*)\\"\\s?\\/?>/iU';
                        preg_match($mode, $text, $result);
                        $modes = [];
                        if ($result == $modes) {
                            $result = 1;
                            return $result[1];
                        } else {
                            return $result[1];
                        }
                    }
                    $descriptions = $description = _description($url, $charset);
                    $titles = '' . ($title = _title($url, $charset));
                    if ($descriptions == '') {
                    } else {
                        $data['content'] = $descriptions;
                    }
                    if ($titles == '') {
                    } else {
                        $data['title'] = $titles;
                    }
                    function get_photo($urls, $filename = '', $savefile = 'uploads/ico/')
                    {
                        $imgArr = array('ico');
                        if (!$urls) {
                            return false;
                        }
                        if (!$filename) {
                            $name_arr = explode('.', $urls);
                            $ext = array_pop($name_arr);
                            if (!in_array($ext, $imgArr)) {
                                return false;
                            }
                            $filename = date("dMYHis") . '.' . $ext;
                        }
                        if (!is_dir($savefile)) {
                            mkdir($savefile, 0777);
                        }
                        if (!is_readable($savefile)) {
                            chmod($savefile, 0777);
                        }
                        $filename = $savefile . $filename;
                        ob_start();
                        readfile($urls);
                        $img = ob_get_contents();
                        ob_end_clean();
                        $size = strlen($img);
                        $fp2 = @fopen($filename, "a");
                        fwrite($fp2, $img);
                        fclose($fp2);
                        return $filename;
                    }
                    $img = get_photo('' . $url . '/favicon.ico');
                    $xx = '/';
                    $data['ico'] = $xx . $img;
                } else {
                    $img = '/favicon.ico';
                    $data['ico'] = $img;
                }
                if (!Db::name('html')->where('id', $id)->data($data)->update($data)) {
                    $this->error('更新失败');
                } else {
                    $this->success('更新成功');
                }
            } else {
                $this->success(config('web.GZ_TS'));
            }
        }
        return view();
    }
}