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

use think\Controller;
use think\Db;
use app\index\model\Html as HtmlModel;
use QL\QueryList;
class Html extends Controller
{
    public function _initialize()
    {
    }
    public function add()
    {
        session_start();
        $category = Db::name('category');
        $html = new HtmlModel();
        if (request()->isPost()) {
            $da = input('post.');
            //查询栏目是否存在
            if (funt('category', array('id' => $da['tid'])) == 0) {
                $this->error('栏目不存在，请自重');
            }
            $data['time'] = time();
            $data['open'] = 0;
            $data['view'] = 1;
            $data['uid'] = 1;
            $data['qz'] = 0;
            $config = array('lianjie' => input('lianjie'));
            $data['content'] = remove_xss($da['content']);
            $data['tid'] = $da['tid'];
            $data['title'] = remove_xss($da['title']);
            $data['lianjie'] = remove_xss($da['lianjie']);
            $data['qq'] = remove_xss($da['qq']);
            $data['keywords'] = remove_xss($da['keywords']);
            $hasuserhead = $html->where($config)->count();
            $this->assign('hasuserhead', $hasuserhead);
            if (warning() == shul()) {
                if (guojiz('jiance') == 1) {
                    ini_set("user_agent", "Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)");
                    $conn = file_get_contents('http://' . $da['lianjie']);
                    if (strpos($conn, $_SERVER['SERVER_NAME']) === false) {
                        return json(array('code' => 0, 'msg' => '请在贵站添加本站的链接,自动通过显示'));
                    } else {
                        $data['open'] = 1;
                    }
                }
                if (config('web.WEB_QZ') == 1) {
                    $url = 'https://baidurank.aizhan.com/baidu/' . $da['lianjie'] . '/';
                    $rules = array('link' => array('.active .ip li img', 'src'));
                    $dat = QueryList::Query($url, $rules)->data;
                    $qz = preg_replace("/(\\/\\/statics.aizhan.com\\/images\\/br\\/)|.png/si", " ", $dat[0][link]);
                    if ($qz <= config('web.WEB_QZS')) {
                        return json(array('code' => 0, 'msg' => '您的站点权重不符合本站收录'));
                    }
                }
            }
            if ($hasuserhead >= 1) {
                $this->error('网址已存在');
            }
            if (config('web.WEB_REG') == 0) {
                return json(array('code' => 0, 'msg' => '当前状态关闭普通提交 请登录快审'));
            }
            if (isset($_REQUEST['authcode'])) {
                if (strtolower($_REQUEST['authcode']) == $_SESSION['checkkey']) {
                    //strtolower转化为小写的函数
                    $data['description'] = mb_substr(strip_tags($da['content']), 0, 200, 'utf-8');
                    if ($html->add($data)) {
                        if (guojiz('tijiaotz') == 1) {
                            send_mail(guojiz('mailUsername'), '有人提交网站了' . guojiz('title'), '有人在' . guojiz('title') . '提交网站了,快去看看吧');
                        }
                        if (config('lm.openp') == 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('html_add.html'));
    }
    public function lm()
    {
        $category = Db::name('category');
        $html = new HtmlModel();
        if (config('lm.open') == 1) {
            if (request()->isPost()) {
                $da = input('post.');
                if (strpos(config('lm.www'), $da['www']) !== false) {
                    $data['time'] = time();
                    $data['open'] = config('lm.open1');
                    $data['view'] = 1;
                    $data['uid'] = 1;
                    $data['qz'] = 0;
                    $data['content'] = remove_xss($da['content']);
                    if (config('lm.fl')==0) {
                    	$data['tid'] = $da['tid'];
                    } else {
                    	$data['tid'] = config('lm.fl');
                    }
                    $data['title'] = remove_xss($da['title']);
                    $data['lianjie'] = remove_xss($da['lianjie']);
                    $data['qq'] = remove_xss($da['qq']);
                    $data['keywords'] = remove_xss($da['keywords']);
                    $config = array('lianjie' => $da['lianjie']);
                    $hasuserhead = $html->where($config)->count();
                    if ($hasuserhead == 0) {
                         $html->add($data);
                    }
                }
            }
        }
    }
}