<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
use QL\QueryList;
use WordAnalysis\Analysis;
use Think\Config;
class Set extends Common
{
    public function caiji()
    {
        if (config(pa('NP6ZgJozVhyuF')) != pa('K7Q03OgSv')) {
            $this->error(config('web.sq'));
        } else {
            $cms = Db::name('caiji')->find(input('id'));
            if (input('id') == '') {
                $this->assign('cms', 1);
            } else {
                $this->assign('cms', $cms);
            }
            return view();
        }
    }
    public function index()
    {
        if (config(pa('NP6ZgJozVhyuF')) != pa('K7Q03OgSv')) {
            die(config('web.sq'));
        } else {
            $tptc = Db::name('caiji')->order('id desc')->paginate(15);
            $this->assign('tptc', $tptc);
            return view();
        }
    }
    public function dels()
    {
        if (config(pa('NP6ZgJozVhyuF')) != pa('K7Q03OgSv')) {
            $this->error(config('web.sq'));
        } else {
            $banner = Db::name('caiji');
            if ($banner->delete(input('post.id'))) {
                return json(array('code' => 200, 'msg' => '删除成功'));
            } else {
                return json(array('code' => 0, 'msg' => '删除失败'));
            }
        }
    }
    //提交
    public function tijiao()
    {
        if (config(pa('NP6ZgJozVhyuF')) != pa('K7Q03OgSv')) {
            $this->error(config('web.sq'));
        } else {
            $cha = Db::name('caiji')->where(array('name' => input('name')))->count();
            $datare = $this->request->post();
            $datare['times'] = time();
            if ($cha == 0) {
                //添加规则记录
                $datare['key'] = generate_password(20);
                Db::name('caiji')->data($datare)->insert();
                $this->success('添加规则成功');
            } else {
                //更新
                Db::name('caiji')->where(array('name' => input('name')))->data($datare)->update();
                $this->success('更新规则成功');
            }
        }
    }
    public function delss()
    {
        $banner = Db::name('caiji');
        $params = input('post.');
        $ids = implode(',', $params['ids']);
        $result = $banner->delete($ids);
        if ($result) {
            return json(array('code' => 200, 'msg' => '批量删除成功'));
        } else {
            return json(array('code' => 0, 'msg' => '批量删除失败'));
        }
    }
    public function add()
    {
        if (config(pa('NP6ZgJozVhyuF')) != pa('K7Q03OgSv')) {
            $this->error(config('web.sq'));
        } else {
            //伪原创
            $words = array();
            $content = file_get_contents('extend/WordAnalysis/dict/not-build/seo.txt');
            $content = str_replace("\r", "", $content);
            $content = preg_split('/\\n/', $content, -1, PREG_SPLIT_NO_EMPTY);
            foreach ($content as $k => $v) {
                $str_data = explode('→', $v);
                $words += array("{$str_data[0]}" => "{$str_data[1]}");
            }
            $cha = Db::name('caiji')->where(array('name' => input('name')))->count();
            $datare = $this->request->post();
            $datare['times'] = time();
            if ($cha == 0) {
                $datare['key'] = generate_password(20);
                //添加规则记录
                Db::name('caiji')->data($datare)->insert();
            } else {
                //更新
                Db::name('caiji')->where(array('name' => input('name')))->data($datare)->update();
            }
            $url1 = preg_replace("/(.*)\\[/", "", input('url'));
            $s = preg_replace("/\\](.*)/", "", $url1);
            if (preg_replace("/\\-(.*)/", "", $s) == input('url')) {
                $sz = 1;
            } else {
                $sz = preg_replace("/\\-(.*)/", "", $s);
            }
            if (preg_replace("/(.*)\\-/", "", $s) == input('url')) {
                $sy = 1;
            } else {
                $sy = preg_replace("/(.*)\\-/", "", $s);
            }
            $arr = explode('-', $s);
            $tj = array();
            $o = 1;
            for ($ii = $sz; $ii < $sy + 1; $ii++) {
                $url = preg_replace("/\\[(.*)\\]/", "{$ii}", input('url'));
                $rules = array('link' => array(input('link'), 'href'));
                //开始采集
                $data = QueryList::Query($url, $rules)->data;
                //var_dump($data);
                if (input('shu')) {
                    $j = input('shu');
                } else {
                    $j = count($data);
                }
                if (!$data[0]['link']) {
                    $this->error('获取列表链接失败,没有指向正确的a标签');
                } 
                if ($data) {
                    for ($i = 0; $i <= $j - 1; $i++) {
                        $values = ['link' => $data[$i]['link']];
                        $_POST['url'] = $data[$i]['link'];
                        if (!preg_match("/^(http|ftp):/", $_POST['url'])) {
                            $url1 = input('www') . $_POST['url'];
                        } else {
                            $url1 = $_POST['url'];
                        }
                        $rules1 = array('description' => array(input('content'), 'text'), 'time' => array(input('time'), 'text'),'content' => array(input('content'), input('html')), 'time' => array(input('time'), 'text'), 'title' => array(input('title'), 'text'), 'fujian' => array(input('fujian'), 'href'));
                        $data1 = QueryList::Query($url1, $rules1)->data;
                        if (input('time')) {
                            if (strtotime($data1[0]['time']) == '') {
                                $times = time();
                            } else {
                                $times = strtotime(preg_replace('/[\\x{4E00}-\\x{9FFF}]+/u', '', $data1[0]['time']));
                            }
                        } else {
                            $times = time();
                        }
                        if (preg_replace(explode("|", input('title1')), explode("|", input('title2')), $data1[0]['title']) == '') {
                            $_data['title'] = '获取标题失败';
                            $this->error('获取标题失败,没有指向正确标题容器');
                        } else {
                            $_data['title'] = preg_replace(explode("|", input('title1')), explode("|", input('title2')), $data1[0]['title']);
                        }
                        //开启转载链接
                        if (input('laiyuan') == 1) {
                            $laiyuan = '<p>原文链接:' . $url1 . '</p>';
                        } else {
                            $laiyuan = ' ';
                        }
                        if (preg_replace(explode("|", input('content1')), explode("|", input('content2')), $data1[0]['content']) == '') {
                            $_data['content'] = '获取内容失败';
                            $this->error('获取内容失败,没有指向正确内容容器');
                        } else {
                            //远程图片本地化
                            if (input('bd') == 1) {
                                //开启
                                //是否开启伪原创seo
                                if (input('seo') == 1) {
                                    $_data['content'] = getCurContent(strtr(preg_replace(explode("|", input('content1')), explode("|", input('content2')), $data1[0]['content']), $words)) . $laiyuan;
                                } else {
                                    $_data['content'] = getCurContent(preg_replace(explode("|", input('content1')), explode("|", input('content2')), $data1[0]['content'])) . $laiyuan;
                                }
                            } else {
                                //关闭
                                if (input('seo') == 1) {
                                    $_data['content'] = strtr(preg_replace(explode("|", input('content1')), explode("|", input('content2')), $data1[0]['content']), $words) . $laiyuan;
                                } else {
                                    $_data['content'] = preg_replace(explode("|", input('content1')), explode("|", input('content2')), $data1[0]['content']) . $laiyuan;
                                }
                            }
                        }
                        if (input('fujian1') != '') {
                            if ($data1[0]['fujian'] == '') {
                                $_data[input('fujian1')] = '';
                            } else {
                                $_data[input('fujian1')] = $data1[0]['fujian'];
                            }
                        }
                        //获取关键字
                        $_ks = preg_replace("/[a-z,A-Z]|[0-9]|\-|\./","",$_data['content'].$_data['title']);
                        $_data['keywords'] = Analysis::getKeywords($_ks,20);
                        $_data['description'] = cut_c($data1[0]['description']);
                        $_data['time'] = $times;
                        $_data['view'] = rand(100, 200);
                        $_data['tid'] = input('tid');
                        $_data['uid'] = 1;
                        //添加记录
                        $tidx = Db::name('news');
                        $chax = $tidx->where(array('title' => $_data['title']))->count();
                        if (input('jin') == 1) {
                            //获取今天的数据
                            if ($times > Date_k('0')) {
                                if ($_data['title'] != '获取标题失败'&&$_data['content'] != '获取内容失败') {
                                    if ($chax == 0) {
                                        $tidx->insert($_data);
                                        $tj[$o++] = $o++;
                                    }
                                }
                            }
                        } else {
                            if ($_data['title'] != '获取标题失败'&&$_data['content'] != '获取内容失败') {
                                if ($chax == 0) {
                                    $tidx->insert($_data);
                                    $tj[$o++] = $o++;
                                }
                            }
                        }
                    }
                }
            }
            $this->success('当前采集到' . count($tj) . '条数据');
        }
    }
    public function sql()
    {
        return view();
    }
    //收录联盟
    public function shoulu()
    {
        return view();
    }
    //执行sql
    public function sqlzx()
    {
        $data = $this->request->post();
        $db_config = array();
        $db_config['prefix'] = Config::get('database.prefix');
        $sql_array = preg_split("/;[\r\n]+/", str_replace('my_',prefix(),input('sql')));
        foreach ($sql_array as $k => $v) {
            if (!empty($v)) {
                Db::query($v);
            }
        }
        $this->success('执行成功');
    }
    // 分站
    public function sn()
    {
        return view();
    }
        //添加
    public function sn_insert()
    {
        if (config(pa('NP6ZgJozVhyuF')) != pa('K7Q03OgSv')) {
            $this->error(config('web.sq'));
        } 
        $data = $this->request->post();
        if (fun_W('sn','open',input('name'))!=0) {
            $this->error('当前地区存在');
        }
         
        $data['time'] = time();
        if (!Db::name('sn')->data($data)->insert()) {
            $this->error('添加失败');
        } else {
            $this->success('添加成功');
        }
    }
    //更新
    public function sn_upda()
    {
        if (config(pa('NP6ZgJozVhyuF')) != pa('K7Q03OgSv')) {
            $this->error(config('web.sq'));
        } 
        $id = input('id');
        $data = $this->request->post();
        if (Db::name('sn')->where(array('id'=>$id))->data($data)->update($data)) {
            $this->success('更新成功');
        } else {
            $this->error('更新失败');
        }
    }
    //删除
	public function sn_dels(){
	    if (config(pa('NP6ZgJozVhyuF')) != pa('K7Q03OgSv')) {
            die(config('web.sq'));
        } 
        $id = input('id');
        $data = $this->request->post();
        if (Db::name('sn')->where(array('id'=>$id))->data($data)->delete($data)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
	}
// 	手机注册配置
	public function phone(){
	    return view();
	}
}