<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Keyworlds extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->permission->is_admin(false);
        $this->load->model('InfoKeywords_model', 'Keywords_model');
        $this->load->model('InfoKeywordsrank_model', 'Keywordsrank_model');
        $this->load->model('InfoKeywordsanalytics_model', 'analytics_model');
        $this->load->model('InfoStructures_model');
    }

    public function index($kw_is_id = '') {
        $this->permission->is_admin();
        //查询结构列表信息
        $data['keyworldsList'] = $this->Keywords_model->get_structure_list();
        if (empty($data['keyworldsList'])) {
            $this->add(0, false);
            redirect(site_url("Keyworlds/index"));
        }

        $root_key = false;
        $data['keyworldsList'][0]->id == $kw_is_id ? $kw_is_id = '' : null;

        if (empty($kw_is_id) || $kw_is_id == 'asc' || $kw_is_id == 'desc') {
            $root_key = true;
            $data['order'] = empty($kw_is_id) ? 'asc' : $kw_is_id;
            $kw_is_id = $data['keyworldsList'][0]->id;
        }

        $today = date('Y-m-d', time());
        $startdate = strtotime("$today -1 month");
        $enddate = time();
        if ($this->input->post('startdate') != '')
            $startdate = strtotime($this->input->post('startdate'));
        if ($this->input->post('enddate') != '')
            $enddate = strtotime($this->input->post('enddate'));

        //查询当前关键词的排名情况
        $data['rank_json'] = '';
        $data['current_keyworld'] = $this->Keywords_model->get_keyworld_bytreeid($kw_is_id);
        //如果是根目录，则展示最新关键词排名变化情况
        if ($root_key) {
            $data['compare_rank'] = $this->Keywords_model->get_compare_rank($data['order']);
            $this->load->view('bootstrap/header', $data);
            $this->load->view('keyworlds/compare_rank');
            $this->load->view('bootstrap/footer');
            return;
        }

        $data['wordcount'] = 1;
        if ($data['current_keyworld']) {
            //获取当前节所有直接子节点
            $is_id_string = $data['current_keyworld']->kw_id;
            $i = 0;
            foreach ($data['keyworldsList'] as $kw) {
                if ($i > 9)
                    break;
                if ($kw->pId == $kw_is_id) {
                    $is_id_string.=',' . $kw->kw_id;
                    $i++;
                    $data['wordcount'] = 2;
                }
            }

            $data['rank_info'] = $this->Keywordsrank_model->get_rank_list($is_id_string, 30, $startdate, $enddate);
            empty($data['rank_info']) && $data['rank_info'] = $this->Keywordsrank_model->get_rank_list($is_id_string, 30);
            if (!empty($data['rank_info'])) {
                $rank_info = $data['rank_info'];
                asort($rank_info);
                $data_time = array();
                foreach ($rank_info as $key => $r) {
                    if (!in_array(date('Y-m-d', $r->kwr_datetime), $data_time))
                        $data_time[] = date('Y-m-d', $r->kwr_datetime);
                    $rank_json[$r->kwr_kw_id][date('Y-m-d', $r->kwr_datetime)] = $r;
                    $rank_json2[$r->kwr_kw_id][] = $r;
                }

                foreach ($data_time as $d) {
                    foreach ($rank_json as $kw_id => &$value) {
                        if (!isset($pre[$kw_id]))
                            $pre[$kw_id] = $rank_json2[$kw_id][0];
                        if (!isset($value[$d])) {
                            $value[$d] = $pre[$kw_id];
                        } else {
                            $pre[$kw_id] = $value[$d];
                        }
                    }
                }
                // 拼接highcharts数据格式
                $data['rank_json'] = '[';
                foreach ($rank_json as &$ra) {
                    ksort($ra);
                    // 每条线
                    $data['rank_json'] .='{"name":"' . $ra[$data_time[0]]->kw_keyworlds . '","data":[';
                    // 每个点
                    $tmp_rank = "null";
                    foreach ($ra as $v) {
                        $tmp_rank = trim($v->kwr_rank)=="" ? $tmp_rank : $v->kwr_rank;
                        // $kwr_rank = intval(trim($v->kwr_rank))>0 ? $v->kwr_rank : "null";
                        $kwr_rank = ($v->kwr_rank==151) ? "null" : $tmp_rank;
                        // $kwr_rank = trim($kwr_rank)=="" ? "null" : $kwr_rank;
                        if ($v->kwr_rank == '>150') {
                            $kwr_rank = 100;
                        }
                        $data['rank_json'] .=$kwr_rank . ',';
                    }
                    $data['rank_json'] = substr($data['rank_json'], 0, -1);
                    $data['rank_json'] .=']},';
                    // 叶子节点 显示mobile记录
                    if ($data['wordcount'] == 1) {
                        $data['rank_json'] .='{"name":"' . $ra[$data_time[0]]->kw_keyworlds . '(mobile)","data":[';
                        foreach ($ra as $v) {
                            $kwr_mobile_rank = (intval($v->kwr_mobile_rank) > 0) ? $v->kwr_mobile_rank : "null" ;
                            $kwr_mobile_rank = ($v->kwr_mobile_rank==151) ? "null" : $v->kwr_mobile_rank;
                            $kwr_mobile_rank = trim($kwr_mobile_rank)=="" ? "null" : $kwr_mobile_rank;
                            if ($v->kwr_mobile_rank == '>150') {
                                $kwr_mobile_rank = 100;
                            }
                            $data['rank_json'] .= $kwr_mobile_rank . ',';
                        }
                        $data['rank_json'] = substr($data['rank_json'], 0, -1);
                        $data['rank_json'] .=']},';
                    }
                }
                $data['rank_json'] = substr($data['rank_json'], 0, -1) . ']';
                $data['data_time'] = json_encode($data_time);
                $data['rank_info'] = '';
                isset($rank_json[$data['current_keyworld']->kw_id]) && $data['rank_info'] = $rank_json[$data['current_keyworld']->kw_id];
                if($data['wordcount'] == 1) {
                    arsort($data['rank_info']);
                }
            }
        } else {
            redirect(site_url("Keyworlds/index"));
        }

        $data['site_code'] = strtolower($this->config->item('site_code'));
        $this->load->view('bootstrap/header', $data);
        $this->load->view('keyworlds/index');
        $this->load->view('bootstrap/footer');
    }

    //搜索关键词
    public function search_keywords() {
        $keyworlds = $this->input->post('keyworlds');
        if (empty($keyworlds)) {
            echo 0;
            return;
        }
        $data['keyworld'] = $keyworlds;
        $data['keyworld_info'] = $this->Keywords_model->get_keyworld_by_word($keyworlds);
        $this->load->view('keyworlds/search', $data);
    }

    //添加关键词
    public function add($is_parent_id, $is_ajax = true) {
        $insert_id = $this->InfoStructures_model->Add($is_parent_id);
        if ($insert_id && $kw_id = $this->Keywords_model->add_keyworlds('New Keywords', $this->config->item('site_code'), time(), $insert_id)) {
            $data[] = array('name' => 'ok', 'value' => $this->Keywords_model->get_keyworld($kw_id));
        } else {
            $data[] = array('name' => 'no', 'value' => $this->lang->line('form_info_error'));
        }
        if ($is_ajax) {
            echo json_encode($data);
            return TRUE;
        }
        return $insert_id;
    }

    //编辑关键词
    public function edit() {
        $this->form_validation->set_rules('keyworlds', 'lang:kw_keyworlds', 'required');
        $this->form_validation->set_rules('kw_id', 'lang:kw_id', 'required');
        if ($this->form_validation->run() == FALSE) {
            $data = array();
            foreach ($this->form_validation->_error_array as $key => $value) {
                $data[] = array('name' => $key, 'value' => $value);
            }
            echo json_encode($data);
        } else {
            $isupdate = $this->Keywords_model->edit_keyworlds($this->input->post('keyworlds'), $this->input->post('kw_id'), $this->input->post('status'));
            if ($isupdate) {
                $data[] = array('name' => 'ok', 'value' => $this->lang->line('form_keyworlds_success'));
                echo json_encode($data);
            } else {
                $data[] = array('name' => 'no', 'value' => $this->lang->line('form_keyworlds_error'));
                echo json_encode($data);
            }
        }
    }

    //删除某个排名
    public function delete_one_rank($kwr_id) {
        $this->Keywordsrank_model->delete_one_rank($kwr_id);
        header("Location:" . $_SERVER['HTTP_REFERER']);
    }

    //删除关键词
    public function delete_keyworlds() {
        $this->form_validation->set_rules('kw_id', 'lang:kw_id', 'required');
        $this->form_validation->set_rules('kw_is_id', 'lang:kw_is_id', 'required');
        if ($this->form_validation->run() == FALSE) {
            $data = array();
            foreach ($this->form_validation->_error_array as $key => $value) {
                $data[] = array('name' => $key, 'value' => $value);
            }
            echo json_encode($data);
        } else {
            $isdelete = $this->Keywords_model->delete($this->input->post('kw_id'));
            if ($isdelete) {
                $this->Keywordsrank_model->delete($this->input->post('kw_id'));
                $this->InfoStructures_model->Delete($this->input->post('kw_is_id'));
                $data[] = array('name' => 'ok', 'value' => site_url('keyworlds/index'));
                echo json_encode($data);
            } else {
                $data[] = array('name' => 'no', 'value' => $this->lang->line('form_keyworlds_error'));
                echo json_encode($data);
            }
        }
    }

    //移动关键词排序顺序
    public function move() {
        //网站会提交一个同级节点id列表字符串，按照这个去排序
        $parent_id = $this->input->post('pid');
        $idsStr = $this->input->post('ids');
        $idsArray = explode(',', $idsStr);
        foreach ($idsArray as $key => $value) {
            if ($value) {
                //设置排序
                $this->InfoStructures_model->set_sort($value, $key);
                //设置path
                $this->InfoStructures_model->set_path($parent_id, $value);
            }
        }
        $data[] = array('name' => 'ok', 'value' => $this->lang->line('structures_success_move'));
        echo json_encode($data);
        return TRUE;
    }

    public function get_google_rank() {
        $keyworlds = $this->input->post('keyworlds'); //搜索关键字
        $kw_id     = $this->input->post('kw_id');
        $site_url  = $this->config->item('site_url');

        $engines = array(
            'cht'  => 'get_search_content_by_google',
            'gm'   => 'get_search_content_by_google',
            'vc'   => 'get_search_content_by_google',
            'jp'   => 'get_search_content_by_google',
            'vac'  => 'get_search_content_by_google',
            'it'   => 'get_search_content_by_google',
            'ru'   => 'get_search_content_by_google',
            'tbt'  => 'get_search_content_by_google',
            'sht'  => 'get_search_content_by_google',
            'yz'   => 'get_search_content_by_google',
            'gl'   => 'get_search_content_by_google',
            'mbj'  => 'get_search_content_by_google',
            'ct'   => 'get_search_content_by_google',
            'mct'  => 'get_search_content_by_google',
            'dct'  => 'get_search_content_by_google',
            'ah'   => 'get_search_content_by_google'
        );
        $site_code = $this->config->item('site_code');
        $method = $engines[$site_code];

        $today = strtotime(date('Y-m-d'));
        $client_flag = 'PC';
        $rank_flag   = 'rank';
        $source_flag = 'source';
        $last_rank   = $this->Keywordsrank_model->get_rank_list($kw_id, $top = 1);
        if (isset($last_rank[0]->kwr_datetime) and ( time() - $last_rank[0]->kwr_datetime) < 180) {
            $data[] = array('name' => 'rank', 'value' => '查询过于频繁,请等待三分钟!');
            echo json_encode($data);
            return TRUE;
        }
        if ((intval($last_rank[0]->kwr_mobile_rank) == 0 || !isset($last_rank[0]->kwr_mobile_rank)) &&
            (intval($last_rank[0]->kwr_datetime) >= intval($today)) ) {
            $client_flag = 'm';
            $rank_flag   = 'rank_m';
            $source_flag = 'source_m';
        }

        $today_0         = strtotime(date('Ymd000000'));
        $today_24        = strtotime(date('Ymd235959'));
        $last_rank_today = $this->Keywordsrank_model->get_rank_list($kw_id,1,$today_0,$today_24);
        // $last_rank_today = $last_rank;

        $orignal = array();
        //获取排名详细情况,排名超过150的停止搜索
        for ($i = 1; $i <= 15; $i++) {
            $j = $i;
            $rank = $this->$method($client_flag, $keyworlds, $site_url, $j, $site_code);
            $orignal[] = $rank['orignal'];
            // 仅更新一个字段
            if ($client_flag === 'm') {
                $rank['rank']   = isset($last_rank_today[0]) ? $last_rank_today[0]->kwr_rank : null;
                $rank['source'] = isset($last_rank_today[0]) ? $last_rank_today[0]->kwr_source : null;
            } else {
                $rank['rank_m']   = isset($last_rank_today[0]) ? $last_rank_today[0]->kwr_mobile_rank : null;
                $rank['source_m'] = isset($last_rank_today[0]) ? $last_rank_today[0]->kwr_mobile_source : null;
            }
            if (intval($rank[$rank_flag]) != 0) {
                if ($rank[$rank_flag] > 150)
                    $rank[$rank_flag] = '151';
                break;
            }elseif ($i == 15) {
                $rank[$rank_flag] = '151';
                break;
            } elseif ($rank[$rank_flag] == -2) {
                break;
            }
        }
        $data[] = array(
            'name'    => 'rank',
            'value'   => ($rank['rank'] . " / " . $rank["rank_m"]),
            'real'    => $rank_flag . " : " . $rank[$rank_flag]
            // ,'lastid' => $last_rank[0]->kwr_id . " / " . $last_rank_todaytmp[0]->kwr_id
            // ,'tmp'     => intval($last_rank[0]->kwr_datetime) . "-" . intval($today) . $client_flag
            // ,'orignal' => $orignal
            // ,"site"    => $site_url
        );
        if ($rank[$rank_flag] < 0) {
            echo json_encode($data);
            return false;
        }
        //保存排名数据
        if ( isset($last_rank_today[0]) && ! empty($last_rank_today[0])) {
            $this->Keywordsrank_model->update_rank($last_rank_today[0]->kwr_id, $rank['rank'], $rank['rank_m'], $rank['source'], $rank['source_m'], $rank['engines']);
        } else {
            $add = $this->Keywordsrank_model->add_rank($kw_id, $rank['rank'], $rank['rank_m'], $rank['source'], $rank['source_m'], $rank['engines']);
        }
        // rank 变化
        if ($client_flag === 'PC') {
            $this->Keywords_model->update_time($kw_id);
            $last_rank_pc = $this->Keywordsrank_model->get_last_pc_rank($kw_id, 2);
            if (trim($last_rank_pc[1]->kwr_rank) != "") {
                $this->load->model('InfoSMS_model');
                $last_rank_pc = isset($last_rank_pc[1]) ? $last_rank_pc[1]->kwr_rank : $rank['rank'];
                $this->InfoSMS_model->readed_for_info($kw_id, 'keywords_rank');
                $this->InfoSMS_model->add('keywords_rank', $kw_id, $rank['rank'], $last_rank_pc, (string) ($rank['rank'] - $last_rank_pc));
            }
        }
        echo json_encode($data);
        return TRUE;
    }

    //自动爬取排名
    public function get_auto_rank($engines = '', $fix_site_code = '', $client='PC') {
        set_time_limit(60);
        $rank        = array();
        $orignal     = array();
        $worldsList  = array();
        $site_config = $this->config->item('site');
        $engines     = trim($engines);
        //取出需要更新排名的关键词
        // 先取时间最久没更新的
        $today        = strtotime(date('Y-m-d'));
        $client_flag  = $client ? $client : "PC";
        $rank_flag    = 'rank';
        $source_flag  = 'source';
        if ($client_flag == 'PC') {
            $worldsList   = $this->Keywords_model->get_keyworld_list_old(1, $engines, $fix_site_code);
        }
        if (empty($worldsList) || $client_flag === 'm') {
            $worldsList  = $this->Keywords_model->get_keyworld_list(1, $engines, $fix_site_code);
            //如果没有需要更新的则返回
            if (empty($worldsList)) {
                echo 'all done';
                return;
            }
            $client_flag = "m";
            $rank_flag   = 'rank_m';
            $source_flag = 'source_m';
        }

        $today_0         = mktime(0,0,0,date("m"),(date("d")-$this->Keywords_model->catch_day), date("Y"));
        $today_24        = strtotime(date('Ymd235959'));
        $last_rank_today = $this->Keywordsrank_model->get_rank_list($worldsList[0]->kw_id,1,$today_0,$today_24);
        // $last_rank_today = $last_rank_pc;

        //获取排名并保存
        foreach ($worldsList as $v) {
            $site_config = $site_config[$v->kw_sitecode];
            $site_url = $site_config['site_url'];
            $site_code = $site_config['site_code'];

            for ($i = 1; $i <= 15; $i++) {
                if ($engines == '' || $engines == 'en' || $engines == 'eu') {//没有输入搜索引擎的时候根据站点自动分配获取排名的搜索引擎
                    $rank = $this->get_search_content_by_google($client_flag,$v->kw_keyworlds, $site_url, $i, $site_code);
                } else if ($engines == 'yandex') {//俄语站的yangdex
                    $rank = $this->get_search_content_by_yandex($client_flag,$v->kw_keyworlds, $site_url, $i - 1);
                }
                if ( $rank[$rank_flag] < 0 ) {
                    break;
                }
                $orignal[] = $rank['orignal'];
                if ($client_flag === 'm') {
                    $rank['rank']   = isset($last_rank_today[0]) ? $last_rank_today[0]->kwr_rank : null;
                    $rank['source'] = isset($last_rank_today[0]) ? $last_rank_today[0]->kwr_source : null;
                } else {
                    $rank['rank_m']   = isset($last_rank_today[0]) ? $last_rank_today[0]->kwr_mobile_rank : null;
                    $rank['source_m'] = isset($last_rank_today[0]) ? $last_rank_today[0]->kwr_mobile_source : null;
                }
                if ($rank[$rank_flag] != 0) {
                    if (intval($rank[$rank_flag]) > 150)
                        $rank[$rank_flag] = '151';
                    break;
                }elseif ($i == 15) {
                    $rank[$rank_flag] = '151';
                    break;
                } elseif ($rank[$rank_flag] == -2) {
                    break;
                }
            }
            if (isset($last_rank_today[0]) && ! empty($last_rank_today[0])) {
                $this->Keywordsrank_model->update_rank($last_rank_today[0]->kwr_id, $rank['rank'], $rank['rank_m'], $rank['source'], $rank['source_m'], $rank['engines']);
            } else {
                $add = $this->Keywordsrank_model->add_rank($v->kw_id, $rank['rank'], $rank['rank_m'], $rank['source'], $rank['source_m'], $rank['engines']);
            }
            // rank 变化
            if ($client_flag === 'PC') {
                $this->Keywords_model->update_time($v->kw_id);
                $last_rank_pc = $this->Keywordsrank_model->get_last_pc_rank($v->kw_id, 2);
                if (trim($last_rank_pc[1]->kwr_rank) != "") {
                    $this->load->model('InfoSMS_model');
                    $last_rank_pc = isset($last_rank_pc[1]) ? $last_rank_pc[1]->kwr_rank : $rank['rank'];
                    $this->InfoSMS_model->readed_for_info($v->kw_id, 'keywords_rank');
                    $this->InfoSMS_model->add('keywords_rank', $v->kw_id, $rank['rank'], $last_rank_pc, (string) ($rank['rank'] - $last_rank_pc));
                }
            }
        }
        var_dump("rank $client_flag " . $rank[$rank_flag]);
        unset($rank['orignal']);
        var_dump($worldsList[0]->kw_keyworlds, $rank, $orignal);
    }

    //计算关键词在google搜索引擎的排名
    public function get_search_content_by_google($client_flag,$keyworlds, $site_url, $page, $site_code) {
        $site_url .= "/";
        $rank['rank'] = 0;
        $rank['source'] = $site_url;
        $rank['rank_m'] = 0;
        $rank['source_m'] = $site_url;
        $rank['engines'] = 'google';
        $rank["orignal"] = '';
        if ($page > 3) {
            $rank['rank'] = 151;
            $rank['rank_m'] = 151;
            return $rank;
        }

        $search_worlds = urlencode($keyworlds);

        //拼接搜索url链接
        $start = ($page - 1) * 50;
        $google_url = 'google.com';
        $google_domain = array(
            'gm'  => 'google.de',
            'vc'  => 'google.fr',
            'vac' => 'google.es',
            'ru'  => 'google.ru',
            'it'  => 'google.it',
            'jp'  => 'google.co.jp'
        );
        if (!empty($google_domain[$site_code]))
            $google_url = $google_domain[$site_code];
        //搜索使用的语种,默认是英语
        $language = 'en';
        $language_array = array('gm' => 'de', 'vc' => 'fr', 'vac' => 'es', 'ru' => 'ru', 'it' => 'it', 'jp' => 'jp');
        if (!empty($language_array[$site_code]))
            $language = $language_array[$site_code];

        //抓取google搜索结果
        $url = "https://www.$google_url/search?q=$search_worlds&start=$start&num=50&hl=$language";
        $match_fun   = '/<h3 class=\"r\">(.*)<\/h3>/smUi';
        $rank_flag   = 'rank';
        $source_flag = 'source';
        if ($client_flag == 'm') {
            $match_fun   = '/<a class=\"_Olt _bCp\"(.*)<div/smUi';
            $rank_flag   = 'rank_m';
            $source_flag = 'source_m';
        }
        $response = $this->curl($url,$client_flag);
        if ($response) {
            //分析google搜索结果，计算排名
            if (false !== stripos($response, '<h1>302 Moved</h1>')  ||
                false !== stripos($response, 'recaptcha') || false !== stripos($response, 'CAPTCHA')) {
                $rank['rank'] = -2;
                $rank["orignal"] = $response;
            } elseif (false !== stripos($response, $site_url)) {
                preg_match_all($match_fun, $response, $store);
                foreach ($store[1] as $k => $v) {
                    $ismatch = stripos($v, $site_url);
                    $ismatch = $ismatch ? $ismatch : stripos($v, addcslashes(addslashes($site_url), "/"));
                    if (false !== $ismatch) {
                        //计算排名
                        $rank[$rank_flag] = $start + $k + 1;
                        //抓取关键词来源
                        $end = strpos($v, '"', $ismatch);
                        $source = substr($v, $ismatch, $end);
                        $source = explode('"', $source);
                        $rank[$source_flag] = $source[0];
                        break;
                    }
                }
            } else {
                $rank["orignal"] = $response;
                // log_message('error','google search response not found CH');
            }
        } else {
            $rank["orignal"] = $response;
            $rank[$rank_flag] = -1;
        }
        return $rank;
    }

    public function test() {
        // $ranks = $this->get_search_content_by_yandex('Путеводитель по городу гуйлинь', 'www.chinahighlights.ru');
        // $ranks = $this->get_search_content_by_google('PC','travel china', 'https://www.chinahighlights.com',1,"en");
        var_dump($ranks);
    }

    //计算关键词在yandex搜索引擎的排名
    public function get_search_content_by_yandex($client_flag,$keyworlds, $site_url, $page = 0, $sitecode = false) {
        $site_url .= "/";
        $rank['rank'] = 0;
        $rank['rank_m'] = 0;
        $rank['source'] = $site_url;
        $rank['source_m'] = $site_url;
        $rank['engines'] = 'yandex';
        if ($page > 3) {
            $rank['rank'] = 151;
            $rank['rank_m'] = 151;
            return $rank;
        }
        $startrank = $page * 10;
        $search_worlds = urlencode($keyworlds);

        //抓取搜索结果数据
        $url         = "https://www.yandex.ru/yandsearch?p=$page&text=$search_worlds&lr=21436";
        $refer       = "https://www.yandex.ru/";
        $match_fun   = '/<a class=\"link link_theme_normal organic__url link_cropped_no i-bem\"(.*)class=/smUi';
        $rank_flag   = 'rank';
        $source_flag = 'source';
        if ($client_flag == 'm') {
            $match_fun   = '/<a class=\"link link_theme_normal organic__url i-bem\"(.*)class=/smUi';
            $rank_flag   = 'rank_m';
            $source_flag = 'source_m';
        }
        $response = $this->curl($url,$client_flag,$refer);
        if ($response) {
            // 分析YANDEX搜索结果，计算排名
            if (false !== stripos($response, '<h1>302 Found</h1>') ||
                false !== stripos($response, 'капчей') || false !== stripos($response, 'captcha')) {
                $rank['rank'] = -2;
            } elseif (false !== stripos($response, $site_url)) {
                preg_match_all($match_fun, $response, $store);
                foreach ($store[1] as $k => $v) {
                    $start_s = stripos($v, $site_url);
                    $start_s = $start_s ? $start_s : stripos($v, addcslashes(addslashes($site_url), "/"));
                    if (false !== $start_s) {
                        $rank[$rank_flag] = $startrank + $k + 1;
                        $end_s = stripos($v, '" ', $start_s);
                        $source = substr($v, $start_s, $end_s);
                        $sources = explode('" ', $source);
                        $rank[$source_flag] = $sources[0];
                        break;
                    }
                }
            } else {
                // log_message('error','yandex not found CH');
            }
        } else {
            $rank[$rank_flag] = -1;
        }
        return $rank;
    }

    //从cvs文件导入关键词
    public function hand_add($parentid, $bookname) {
        $row = 1; //第一行开始
        $filename = APPPATH . $bookname . '.csv';
        if (($handle = fopen($filename, "r")) !== false) {
            while (($dataSrc = fgetcsv($handle)) !== false) {
                $num = count($dataSrc);
                for ($c = 0; $c < $num; $c++) {//列 column
                    if ($row === 1) {//第一行作为字段
                        $dataName[] = $dataSrc[$c]; //字段名称
                    } else {
                        foreach ($dataName as $k => $v) {
                            if ($k == $c) {//对应的字段
                                $dataRtn[$v] = @iconv('GBK', 'UTF-8', $dataSrc[$c]);
                            }
                        }
                    }
                }
                if (!empty($dataRtn)) {
                    $insert_id = $this->InfoStructures_model->Add($parentid);
                    if ($insert_id && $kw_id = $this->Keywords_model->add_keyworlds($dataRtn['keyword'], $dataRtn['site'], time() - 24 * 60 * 60, $insert_id, 1)) {
                        $datas[] = array('name' => 'ok', 'value' => $this->Keywords_model->get_keyworld($kw_id));
                    } else {
                        $datas[] = array('name' => 'no', 'value' => $this->lang->line('form_info_error'));
                    }
                    echo $insert_id;
                    $data[] = $dataRtn;
                }

                $row++;
            }
            fclose($handle);
            var_dump($data);
            echo 'ok';
        }
    }

    //定时批量更新CDN
    public function update_cdn() {
        $this->load->model('Logs_model');
        $last_update = $this->Logs_model->get_last_cdn_update_info();
        if (empty($last_update)) {
            $last_log = $this->Logs_model->get_last_log();
            $this->Logs_model->write('update_cdn', $last_log->log_id);
            $last_update->log_res_id = $last_log->log_id;
        }
        $last_update_id = $last_update->log_res_id;
        $list = $this->Logs_model->get_uncdn_list($last_update_id);
        if (empty($list)) {
            echo 'updated';
            die();
        }
        //设置需要更新的url路径
        $site_path = array();
        $path_array = array();
        $site_array = array('vac', 'ru', 'it'); //需要更新的站点
        foreach ($list as $v) {
            if (in_array($v->is_sitecode, $site_array)) {
                if (!isset($site_path[$v->is_sitecode])) {
                    $site_path[$v->is_sitecode] = '';
                    $path_array[$v->is_sitecode] = array();
                }
                if (!in_array($v->ic_url, $path_array[$v->is_sitecode])) {
                    $site_path[$v->is_sitecode].="&path=" . $v->ic_url;
                    $path_array[$v->is_sitecode][] = $v->ic_url;
                }
            }
        }
        //已经给是最新的了
        if (empty($site_path)) {
            echo 'updated.';
            die();
        }

        //更新CDN缓存
        $notice = array();
        $flag = false;
        $siteconfig = $this->config->item('site');
        foreach ($site_path as $k => $cdn) {
            $pad = str_replace('http://', '', $siteconfig[$k]['site_url']);
            $result = file_get_contents('https://openapi.us.cdnetworks.com/purge/rest/doPurge?user=ycc@chinahighlights.com&pass=cXi2UbsTrw9Urv&pad=' . $pad . '&type=item' . $cdn . '&output=json');
            $result = json_decode($result, true);
            if ($result['resultCode'] != 200) {
                $notice[] = $k . ':' . $result['notice'] . $result['details'];
            } else {
                $notice[] = $k . ':' . $result['resultCode'] . '<br>' . $cdn;
                $flag = TRUE;
            }
        }
        //修改最近更新的信息的log_id,方便下次更新读取数据
        if ($flag) {
            $lastinfo = end($list);
            $this->Logs_model->update('update_cdn', $lastinfo->log_id);
        }

        var_dump($notice);
    }

    protected function curl($url, $useragent = 'PC', $referer = false) {
        if ($useragent == 'PC') {
            $useragent = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22";
        } else {
            $useragent = "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Mobile Safari/537.36";
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_FAILONERROR, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
        // curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
        // curl_setopt($ch, CURLOPT_PROXY, "202.103.68.28:11111"); // 本地代理
        if ($referer) {
            curl_setopt($ch, CURLOPT_REFERER, $referer);
        }

        $headers = array('content-type: application/x-www-form-urlencoded;charset=UTF-8');
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        $reponse = curl_exec($ch);

        if (curl_errno($ch)) {
            log_message('error', "curl error code: ".curl_error($ch).";");
        } else {
            $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            if (200 !== $httpStatusCode) {
                log_message('error', "curl Request html Status Code: ".$httpStatusCode.";");
            }
        }
        curl_close($ch);
        return $reponse;
    }

    public function testrank($kw_is_id=0, $top=1)
    {
        $data['current_keyworld'] = $this->Keywords_model->get_keyworld_bytreeid($kw_is_id);
        $is_id_string = $data['current_keyworld']->kw_id;
        $rank_info = $this->Keywordsrank_model->get_rank_list($is_id_string, $top);
        foreach ($rank_info as $key => &$value) {
            $value['ddd'] = date('Y-m-d H:i:s', $value->kwr_datetime);
        }
        var_dump($rank_info);
    }

    public function testpc()
    {
        $last_rank_pc = $this->Keywordsrank_model->get_last_pc_rank('297');
        var_dump($last_rank_pc);
        var_dump($last_rank_pc[0]->kwr_rank);
        var_dump(trim($last_rank_pc[0]->kwr_rank));
    }

    public function test_next($fix_site_code='', $client_flag = 'PC')
    {
        if ($client_flag == 'PC') {
            $worldsList = $this->Keywords_model->get_keyworld_list_old(1, 'en', $fix_site_code);
        }
        if (empty($worldsList) || $client_flag === 'm') {
            $worldsList  = $this->Keywords_model->get_keyworld_list(1, 'en', $fix_site_code);
            // 如果没有需要更新的则返回
            if (empty($worldsList)) {
                echo 'all done';
                return;
            }
            $client_flag = "m";
        }
        var_dump($client_flag);
        var_dump($worldsList);
    }

}
