<?php
class advert_post{
    const data =[];
    function __construct() {
        add_action('wp_ajax_advert_get_vip', [$this,'advert_get_vip']);
        add_action('wp_ajax_advert_vip', [$this,'advert_vip']);
        add_action('wp_ajax_advert_guanggao', [$this,'advert_guanggao']);
        add_action('wp_ajax_advert_get_guanggao', [$this,'advert_get_guanggao']);
        add_action('wp_ajax_advert_get_gonggao', [$this,'advert_get_gonggao']);
        add_action('wp_ajax_advert_gonggao_read', [$this,'advert_gonggao_read']);
        $this->advert_tongji();
    }
    public function advert_tongji()
    {
        $advert_tongji = get_option('advert_tongji');
        if (!$advert_tongji || (isset($advert_tongji) && $advert_tongji['time'] < time())) {
            $wp_version =  get_bloginfo('version');
            $data =   advert_ad::advert_url(0);
            global $advert_key;
            $url = "https://api.seoceo.cn/api/advert/index?url={$data}&type=11&url1=" . md5($data . 'seohnzz.com') . "&theme_version=" . ADVERT_VERSION . "&php_version=" . PHP_VERSION . "&wp_version={$wp_version}&token={$advert_key}";
            $defaults = array(
                'timeout' => 120,
                'connecttimeout' => 120,
                'redirection' => 3,
                'user-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
                'sslverify' => FALSE,
            );
            $result = wp_remote_get($url, $defaults);
            if ($advert_tongji !== false) {
                update_option('advert_tongji', ['time' => time() + 7 * 3600 * 24]);
            } else {
                add_option('advert_tongji', ['time' => time() + 7 * 3600 * 24]);
            }
        }
    }
    public function advert_vip(){
        if(isset($_POST['nonce'])  && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])),'advert')){
            $key = isset($_POST['key'])?sanitize_text_field(wp_unslash($_POST['key'])):'';
            $data = advert_ad::advert_url();
            $url1 = isset($_SERVER['SERVER_NAME'])?sanitize_text_field(wp_unslash($_SERVER['SERVER_NAME'])):'';
            $url = 'https://api.seoceo.cn/api/money/log2?url='.$data.'&url1='.$url1.'&key='.$key.'&type=11';
            $defaults = array(
                'timeout' => 120,
                'connecttimeout'=>120,
                'redirection' => 3,
                'user-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
                'sslverify' => FALSE,
            );
            $result = wp_remote_get($url,$defaults);
            if(!is_wp_error($result)){
                $content = wp_remote_retrieve_body($result);
                if($content){
                    $baiduseo_wzt_log = get_option('advert_log');
                    if($baiduseo_wzt_log!==false){
            	        update_option('advert_log',$key);
            	    }else{
            	        add_option('advert_log',$key);
            	    }
                    echo wp_json_encode(['code'=>1]);exit;
                }
        	}
        	echo wp_json_encode(['code'=>0]);exit;
        	
        
        }
        echo wp_json_encode(['code'=>0]);exit;
    }
    public function advert_get_vip(){
        
        if(isset($_POST['nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])),'advert')){
            global $advert_log;
             $defaults = array(
                'timeout' => 4000,
                'connecttimeout'=>4000,
                'redirection' => 3,
                'user-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
                'sslverify' => FALSE,
            );
            $advert_level = get_option('advert_level');
            if(!isset($advert_level[5]) || $advert_level[5]<time()-24*3600 || (int)str_replace('.','',ADVERT_VERSION)>(int)str_replace('.','',$advert_level[3])){
                $url = 'https://api.seoceo.cn/api/money/level11?url='.advert_ad::advert_url();
                $result = wp_remote_get($url,$defaults);
                if(!is_wp_error($result)){
                    $level = wp_remote_retrieve_body($result);
                    $level = json_decode($level,true);
                    
                    $level1 = explode(',',$level['level']);
                    $level1[2] = ADVERT_VERSION;
                    $level1[3] = $level['version'];
                    $level1[4] = $advert_log;
                    $level2 = $level1;
                    if(isset($level1[0]) && ($level1[0]==1 || $level1[0]==2)){
                        $level2[5] = time();
                        
                        update_option('advert_level',$level2);
                        
                    }
                }
            }else{
                $level1 = $advert_level;
                $level1[4] = $advert_log;
               
            }
            $data['level'] = $level1;                                   
            
            echo wp_json_encode(['code'=>1,'data'=>$data]);exit;
        }
        echo wp_json_encode(['code'=>0]);exit;
    }
   
    public function advert_gonggao_read(){
        if(isset($_POST['nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])),'advert')){
            global $wpdb;
            $res = $wpdb->insert($wpdb->prefix."advert_gonggao",['gid'=>isset($_POST['id'])?(int)$_POST['id']:0]);
        }
    }
    public function advert_get_gonggao(){
         if(isset($_POST['nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])),'advert')){
            global $wpdb;
            
            $defaults = array(
                'timeout' => 400,
                'connecttimeout'=>400,
                'redirection' => 3,
                'user-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
                'sslverify' => FALSE,
            );
            $page = isset($_POST['page'])?(int)$_POST['page']:0;
            $url = 'https://api.seoceo.cn/api/rank/gonggao?type=11&page='.$page;
            $result = wp_remote_get($url,$defaults);
            if(!is_wp_error($result)){
                $result = wp_remote_retrieve_body($result);
                $result = json_decode($result,true);
                foreach($result['data'] as $k=>$v){
                   $exists = $wpdb->get_var(
                        $wpdb->prepare(
                            "SELECT 1 FROM {$wpdb->prefix}advert_gonggao WHERE gid = %d LIMIT 1",
                            $v['id']
                        )
                    );
                    
                    $result['data'][$k]['is_read'] = $exists ? 1 : 0;
                }
                echo wp_json_encode(['code'=>1,'data'=>$result['data'],'total'=>(int)$result['total']]);exit; 
            }else{
                echo wp_json_encode(['code'=>0]);exit;
            }
        }
    }
    public function  advert_guanggao(){
        if(isset($_POST['nonce'])  && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])),'advert')){
            global $advert_log;
            if(!$advert_log){
               echo wp_json_encode(['code'=>0,'msg'=>'请先授权']);exit;
            }
            global $advert_key;
            if(!$advert_key){
                if(self::pay_money()){
                
                }else{
                    echo wp_json_encode(['code'=>0,'msg'=>'请先授权']);exit;
                }
            }
            
            $list = [];
            $list['left'] = isset($_POST['left'])?(int)$_POST['left']:0;
            $list['right'] = isset($_POST['right'])?(int)$_POST['right']:0;
            // $list['mobile_auto'] = (int)$data['mobile_auto'];
            $list['bottom'] = isset($_POST['bottom'])?(int)$_POST['bottom']:0;
            $list['mobile'] = isset($_POST['mobile'])?(int)$_POST['mobile']:0;
            $list['leftad'] = [];
            $leftad = isset($_POST['leftad']) ? wp_unslash($_POST['leftad']) : [];
            if(isset($leftad) && !empty($leftad)){
                foreach($leftad as $k=>$v){
                    $list['leftad'][] = [
                        'pic'=>sanitize_url(wp_unslash($v['pic'])),
                        'link'=>sanitize_url(wp_unslash($v['link'])),
                    ];
                }
            }
            $list['rightad'] = [];
            $rightad = isset($_POST['rightad']) ? wp_unslash($_POST['rightad']) : [];
            if(isset($rightad) && !empty($rightad)){
                foreach($rightad as $k=>$v){
                    $list['rightad'][] = [
                        'pic'=>isset($v['pic'])?sanitize_url(wp_unslash($v['pic'])):'',
                        'link'=>isset($v['link'])?sanitize_url(wp_unslash($v['link'])):"",
                    ];
                }
            }
            if(isset($_POST['bottad']) && !empty($_POST['bottad'])){
                $list['bottad'] = [
                    'pic'=>isset($_POST['bottad']['pic'])?sanitize_url(wp_unslash($_POST['bottad']['pic'])):"",
                    'link'=>isset($_POST['bottad']['link'])?sanitize_url(wp_unslash($_POST['bottad']['link'])):"",
                    'time'=>isset($_POST['bottad']['time'])?(int)$_POST['bottad']['time']:"",
                    'showtime'=>isset($_POST['bottad']['showtime'])?(int)$_POST['bottad']['showtime']:''
                    ];
            }
            if(isset($_POST['mobtad']) && !empty($_POST['mobtad'])){
                $list['mobtad'] = [
                    'pic'=>isset($_POST['mobtad']['pic'])?sanitize_url(wp_unslash($_POST['mobtad']['pic'])):"",
                    'link'=>isset($_POST['mobtad']['link'])?sanitize_url(wp_unslash($_POST['mobtad']['link'])):"",
                    'time'=>isset($_POST['mobtad']['time'])?(int)$_POST['mobtad']['time']:"",
                    'showtime'=>isset($_POST['mobtad']['showtime'])?(int)$_POST['mobtad']['showtime']:''
                    ];
            }
            $get = get_option('advert_guanggao');
            if($get===false){
                add_option('advert_guanggao',$list);
            }else{
                update_option('advert_guanggao',$list);
            }
            echo wp_json_encode(['code'=>1]);exit;
        }
        echo wp_json_encode(['code'=>0]);exit;
    }
    public function advert_get_guanggao(){
        if(isset($_POST['nonce'])  && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])),'advert')){
            $get = get_option('advert_guanggao');
            echo wp_json_encode(['code'=>1,'data'=>$get]);exit;
        }
        echo wp_json_encode(['code'=>0]);exit;
    }
    public function  website_key(){
        
    }
    public static function pay_money(){
         global $advert_log;
        if(!$advert_log){
            return 0;
        }
        $data =  advert_ad::advert_url(0);
        
        $url = "https://api.seoceo.cn/api/index/pay_money?url={$data}&type=11&log={$advert_log}";
        $defaults = array(
            'timeout' => 4000,
            'connecttimeout'=>4000,
            'redirection' => 3,
            'user-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
            'sslverify' => FALSE,
        );
        $result = wp_remote_get($url,$defaults);
        
        if(!is_wp_error($result)){
            
            $content = wp_remote_retrieve_body($result);
            
            $content = json_decode($content,true);
            
            if(isset($content['status']) && $content['status']==1){
                 if(isset($content['token1']) && $content['token1']){
                    $advert_wzt_token1 = get_option('advert_wzt_token1');
                    if($advert_wzt_token1===false){
                        add_option('advert_wzt_token1',$content['token1']);
                    }else{
                        update_option('advert_wzt_token1',$content['token1']);
                    }
                }
                if(isset($content['token2']) && $content['token2']){
                    $advert_wzt_token2 = get_option('advert_wzt_token2');
                    if($advert_wzt_token2===false){
                        add_option('advert_wzt_token2',$content['token2']);
                    }else{
                        update_option('advert_wzt_token2',$content['token2']);
                    }
                }
                if(isset($content['token3']) && $content['token3']){
                    $advert_wzt_token3 = get_option('advert_wzt_token3');
                    if($advert_wzt_token3===false){
                        add_option('advert_wzt_token3',$content['token3']);
                    }else{
                        update_option('advert_wzt_token3',$content['token3']);
                    }
                }
                return 1;
            }elseif(isset($content['status']) && $content['status']==0){
                return 0;
            }else{
                $url = "https://www.seoceo.cn/api/money/pay_money?url={$data}&type=8";
            
                $result = wp_remote_get($url,$defaults);
                
                if(!is_wp_error($result)){
                    $content = wp_remote_retrieve_body($result);
                    
                    $content = json_decode($content,true);
                    if(isset($content['status']) && $content['status']==1){
                        return 1;
                    }
                    return 0;
                }
            }
        }else{
            $url = "https://www.seoceo.cn/api/money/pay_money?url={$data}&type=8";
        
            $result = wp_remote_get($url,$defaults);
            
            if(!is_wp_error($result)){
                $content = wp_remote_retrieve_body($result);
                
                $content = json_decode($content,true);
                if(isset($content['status']) && $content['status']==1){
                    return 1;
                }
               
            }
             return 0;
        }
            
    }
}
?>