 <?php
class keyspider_cron{
    public function init(){
        add_action( 'keyspider_cronhook', [$this,'keyspider_cronexec'] );
        // if(isset($_GET['plan'])){
        //     $this->keyspider_cronexec();
        // }
        if(!wp_next_scheduled( 'keyspider_cronhook' )){
            wp_schedule_event( strtotime(current_time('Y-m-d H:i:00',1)), 'hourly', 'keyspider_cronhook' );
        }
    }
    public function keyspider_cronexec(){
        $keyspider_key = apply_filters('keyspider_dhdfkdksj',4);
        require_once(ABSPATH . 'wp-admin/includes/file.php');
        $keyspider_zhizhu = get_option('keyspider_zhizhu');
        if(isset($keyspider_zhizhu['open']) && $keyspider_zhizhu['open']==1){
            $this->keyspider_zhizhu();
        }
        $this->keyspider_tongji();
        $this->keyspider_liuliang($keyspider_key);
        $keyspider_zhizhu = get_option('keyspider_zhizhu');
        if(isset($keyspider_zhizhu['silian_open']) && $keyspider_zhizhu['silian_open']){
            $this->keyspider_silian();
        }
    }
    public function keyspider_silian(){
        keyspider_seo::silian(1);
    }
    public function keyspider_liuliang($keyspider_key){
        global $wpdb,$keyspider_wzt_log;
        if($keyspider_wzt_log){
            if(!$keyspider_key){
                $log = keyspider_seo::pay_money();
                if(!$log){
                    return;
                }
            }
            //删除超过限制的数据
            $timezone_offet = get_option( 'gmt_offset');
            $keyspider_liuliang = get_option('keyspider_liuliang');
            if(isset($keyspider_liuliang['log'])){
                if($keyspider_liuliang['log']==1){
                    $end = strtotime('-30 days')-$timezone_offet*3600;
                    $wpdb->query($wpdb->prepare("DELETE FROM " . $wpdb->prefix . "keyspider_liuliang  where unix_timestamp(time)<%d",$end));
                }elseif($keyspider_liuliang['log']==2){
                     $end = strtotime('-90 days')-$timezone_offet*3600;
                    $wpdb->query($wpdb->prepare("DELETE FROM " . $wpdb->prefix . "keyspider_liuliang  where unix_timestamp(time)<%d",$end));
                }elseif($keyspider_liuliang['log']==3){
                     $end = strtotime('-180 days')-$timezone_offet*3600;
                    $wpdb->query($wpdb->prepare("DELETE FROM " . $wpdb->prefix . "keyspider_liuliang  where unix_timestamp(time)<%d",$end));
                }elseif($keyspider_liuliang['log']==4){
                      $end = strtotime('-3 days')-$timezone_offet*3600;
                    $wpdb->query($wpdb->prepare("DELETE FROM " . $wpdb->prefix . "keyspider_liuliang  where unix_timestamp(time)<%d",$end));
                }elseif($keyspider_liuliang['log']==5){
                      $end = strtotime('-7 days')-$timezone_offet*3600;
                    $wpdb->query($wpdb->prepare("DELETE FROM " . $wpdb->prefix . "keyspider_liuliang  where unix_timestamp(time)<%d",$end));
                }elseif($keyspider_liuliang['log']==6){
                      $end = strtotime('-15 days')-$timezone_offet*3600;
                    $wpdb->query($wpdb->prepare("DELETE FROM " . $wpdb->prefix . "keyspider_liuliang  where unix_timestamp(time)<%d",$end));
                    
                }
            }
            if(isset($keyspider_liuliang['open']) && $keyspider_liuliang['open']){
                //深度和时长计算
                $res = $wpdb->get_results('select * from '.$wpdb->prefix . 'keyspider_liuliang where shichang=0',ARRAY_A);
                foreach($res as $key=>$val){
                    if($val['status']==1){
                         $today_start = current_time('Y-m-d') . ' 00:00:00';
                        $ip_exists = $wpdb->get_var($wpdb->prepare(
                            "SELECT COUNT(*) FROM {$wpdb->prefix}keyspider_liuliang 
                             WHERE ip = %s
                             AND time < %s",
                            $val['ip'],
                            $today_start
                        ));
                        if($ip_exists){
                            $wpdb->update($wpdb->prefix . 'keyspider_liuliang',['is_new'=>1],['ip'=>$val['ip']]);
                        }
                        $count = $wpdb->query($wpdb->prepare(' select * from  '.$wpdb->prefix.'keyspider_liuliang where session=%s and status=1',$val['session']),ARRAY_A);
                        if($ip_exists){
                            $wpdb->update($wpdb->prefix . 'keyspider_liuliang',['is_new'=>1],['ip'=>$val['ip']]);
                        }
                        if($count){
                            $wpdb->update($wpdb->prefix . 'keyspider_liuliang',['pinci'=>$count],['session'=>$val['session']]);
                        }
                        
                        $total_page = $wpdb->query($wpdb->prepare('select * from '.$wpdb->prefix . 'keyspider_liuliang where pid=%d',$val['id']),ARRAY_A);
                        
                        $shendu = $total_page+1;
                        $re = $wpdb->get_results($wpdb->prepare('select * from '.$wpdb->prefix . 'keyspider_liuliang where pid=%d',$val['id']),ARRAY_A);
                        if($val['updatetime']){
                            $totaltime = strtotime($val['updatetime'])-strtotime($val['time']);
                        }else{
                            $totaltime = 1;
                        }
                        if(!empty($re)){
                            foreach($re as $k=>$v){
                                if($v['shichang']){
                                    $totaltime+=$v['shichang'];
                                }else{
                                    if($v['updatetime']){
                                        $totaltime += strtotime($v['updatetime'])-strtotime($v['time']);
                                       
                                        
                                    }else{
                                        $totaltime += 1;
                                        
                                    }
                                }
                               
                            }
                            
                        }
                        $wpdb->update($wpdb->prefix . 'keyspider_liuliang',['shichang'=>$totaltime,'shendu'=>$shendu,'pinci'=>$count],['id'=>$val['id']]);
                    }else{
                        if($val['updatetime']){
                            $time = strtotime($val['updatetime'])-strtotime($val['time']);
                        }else{
                            $time = 1;
                        }
                        $wpdb->update($wpdb->prefix . 'keyspider_liuliang',['shichang'=>$time],['id'=>$val['id']]);
                    }
                }
            }

        }
        
    }
    public function keyspider_zhizhu(){
        global $wpdb;
        $table_name = $wpdb->prefix . 'keyspider_zhizhu'; // 表名（自动拼接前缀，如 wp_）
    
        $results = $wpdb->get_results("
          
            SELECT COUNT(*) AS spider_count
            FROM `{$table_name}`
            WHERE `time` BETWEEN DATE_SUB(NOW(), INTERVAL 1 HOUR) AND NOW()
            UNION ALL
           
            SELECT COUNT(*) AS spider_count
            FROM `{$table_name}`
            WHERE `time` BETWEEN DATE_SUB(NOW(), INTERVAL 2 HOUR) AND DATE_SUB(NOW(), INTERVAL 1 HOUR)
            UNION ALL
           
            SELECT COUNT(*) AS spider_count
            FROM `{$table_name}`
            WHERE `time` BETWEEN DATE_SUB(NOW(), INTERVAL 3 HOUR) AND DATE_SUB(NOW(), INTERVAL 2 HOUR)
        ",ARRAY_A); // 执行查询，获取结果集
        $keyspider_zhizhu_jieduan =1;
        $maxValue = 0; // 初始化最大值变量

        // 遍历数组提取并比较
        foreach ($results as $item) {
            // 将字符串类型的数值转为整数
            $current = (int)$item["spider_count"];
            // 若当前值大于现有最大值，则更新
            if ($current > $maxValue) {
                $maxValue = $current;
            }
        }
        if($maxValue>2000){
            $keyspider_zhizhu_jieduan =3;
        }elseif($maxValue>200){
            $keyspider_zhizhu_jieduan =2;
        }
        $keyspider_zhizhu_jieduan_copy = get_option('keyspider_zhizhu_jieduan');
       if($keyspider_zhizhu_jieduan_copy!==false){
           update_option('keyspider_zhizhu_jieduan',$keyspider_zhizhu_jieduan);
       }else{
           add_option('keyspider_zhizhu_jieduan',$keyspider_zhizhu_jieduan);
       }
        $timezone_offet = get_option( 'gmt_offset');
       
        $sta =strtotime(gmdate('Y-m-d 00:00:00'))-$timezone_offet*3600-24*3600;
    	$end = strtotime(gmdate('Y-m-d 00:00:00'))-$timezone_offet*3600;
       
    	$suoyin1 = $wpdb->get_results($wpdb->prepare('select * from '.$wpdb->prefix . 'keyspider_zhizhu_suoyin where unix_timestamp(time) >=%d and unix_timestamp(time)<%d and name="百度"  ',$sta,$end),ARRAY_A);
        $currnetTime= gmdate('Y-m-d H:i:s',strtotime(gmdate('Y-m-d 00:00:00'))-24*3600);
    	$baidu = $wpdb->query($wpdb->prepare('select id from '.$wpdb->prefix . 'keyspider_zhizhu  where name="百度" and unix_timestamp(time) >=%d and unix_timestamp(time)<%d',$sta,$end));
    	if(empty($suoyin1)){
    	    $wpdb->insert($wpdb->prefix."keyspider_zhizhu_suoyin",['name'=>'百度','num'=>0,'time'=>$currnetTime,'zhizhu_num'=>$baidu]);
    	}else{
    	    $res = $wpdb->update($wpdb->prefix . 'keyspider_zhizhu_suoyin',['zhizhu_num'=>$baidu],['id'=>$suoyin1[0]["id"]]);
    	    
    	}
    	$suoyin2 = $wpdb->get_results($wpdb->prepare('select * from '.$wpdb->prefix . 'keyspider_zhizhu_suoyin where unix_timestamp(time) >=%d and unix_timestamp(time)<%d and name="必应"  ',$sta,$end),ARRAY_A);
    
    	$bingying = $wpdb->query($wpdb->prepare('select id from '.$wpdb->prefix . 'keyspider_zhizhu  where name="必应" and unix_timestamp(time) >=%d and unix_timestamp(time)<%d',$sta,$end));
    		
    	if(empty($suoyin2)){
    	    $wpdb->insert($wpdb->prefix."keyspider_zhizhu_suoyin",['name'=>'必应','num'=>0,'time'=>$currnetTime,'zhizhu_num'=>$bingying]);
    	}else{
    	    $wpdb->update($wpdb->prefix . 'keyspider_zhizhu_suoyin',['zhizhu_num'=>$bingying],['id'=>$suoyin2[0]["id"]]);
    	}
    	$suoyin3 = $wpdb->get_results($wpdb->prepare('select * from '.$wpdb->prefix . 'keyspider_zhizhu_suoyin where unix_timestamp(time) >=%d and unix_timestamp(time)<%d and name="360"  ',$sta,$end),ARRAY_A);
    	
    	$a360 = $wpdb->query($wpdb->prepare('select id from '.$wpdb->prefix . 'keyspider_zhizhu  where name="360" and unix_timestamp(time) >=%d and unix_timestamp(time)<%d',$sta,$end));
    
    	if(empty($suoyin3)){
    	    $wpdb->insert($wpdb->prefix."keyspider_zhizhu_suoyin",['name'=>'360','num'=>0,'time'=>$currnetTime,'zhizhu_num'=>$a360]);
    	}else{
    	    $wpdb->update($wpdb->prefix . 'keyspider_zhizhu_suoyin',['zhizhu_num'=>$a360],['id'=>$suoyin3[0]["id"]]);
    	}
    	$suoyin4 = $wpdb->get_results($wpdb->prepare('select * from '.$wpdb->prefix . 'keyspider_zhizhu_suoyin where unix_timestamp(time) >=%d and unix_timestamp(time)<%d and name="搜狗"  ',$sta,$end),ARRAY_A);
    	
    	$sougou = $wpdb->query($wpdb->prepare('select id from '.$wpdb->prefix . 'keyspider_zhizhu  where name="搜狗" and unix_timestamp(time) >=%d and unix_timestamp(time)<%d',$sta,$end));
    
    	if(empty($suoyin4)){
    	    $wpdb->insert($wpdb->prefix."keyspider_zhizhu_suoyin",['name'=>'搜狗','num'=>0,'time'=>$currnetTime,'zhizhu_num'=>$sougou]);
    	}else{
    	    $wpdb->update($wpdb->prefix . 'keyspider_zhizhu_suoyin',['zhizhu_num'=>$sougou],['id'=>$suoyin4[0]["id"]]);
    	}
    	$suoyin5 = $wpdb->get_results($wpdb->prepare('select * from '.$wpdb->prefix . 'keyspider_zhizhu_suoyin where unix_timestamp(time) >=%d and unix_timestamp(time)<%d and name="谷歌"  ',$sta,$end),ARRAY_A);
    
    	$guge= $wpdb->query($wpdb->prepare('select id from '.$wpdb->prefix . 'keyspider_zhizhu  where name="谷歌" and unix_timestamp(time) >=%d and unix_timestamp(time)<%d',$sta,$end));
    	
    	if(empty($suoyin5)){
    	    $wpdb->insert($wpdb->prefix."keyspider_zhizhu_suoyin",['name'=>'谷歌','num'=>0,'time'=>$currnetTime,'zhizhu_num'=>$guge]);
    	}else{
    	    $wpdb->update($wpdb->prefix . 'keyspider_zhizhu_suoyin',['zhizhu_num'=>$guge],['id'=>$suoyin5[0]["id"]]);
    	}
    	$suoyin6 = $wpdb->get_results($wpdb->prepare('select * from '.$wpdb->prefix . 'keyspider_zhizhu_suoyin where unix_timestamp(time) >=%d and unix_timestamp(time)<%d and name="神马"  ',$sta,$end),ARRAY_A);
    	
    	$shenma= $wpdb->query($wpdb->prepare('select id from '.$wpdb->prefix . 'keyspider_zhizhu  where name="神马" and unix_timestamp(time) >=%d and unix_timestamp(time)<%d',$sta,$end));
    	
    	if(empty($suoyin6)){
    	    $wpdb->insert($wpdb->prefix."keyspider_zhizhu_suoyin",['name'=>'神马','num'=>0,'time'=>$currnetTime,'zhizhu_num'=>$shenma]);
    	}else{
    	    $wpdb->update($wpdb->prefix . 'keyspider_zhizhu_suoyin',['zhizhu_num'=>$shenma],['id'=>$suoyin6[0]["id"]]);
    	}
    	$suoyin7 = $wpdb->get_results($wpdb->prepare('select * from '.$wpdb->prefix . 'keyspider_zhizhu_suoyin where unix_timestamp(time) >=%d and unix_timestamp(time)<%d and name="头条"  ',$sta,$end),ARRAY_A);
    	
    	$toutiao= $wpdb->query($wpdb->prepare('select id from '.$wpdb->prefix . 'keyspider_zhizhu  where name="头条" and unix_timestamp(time) >=%d and unix_timestamp(time)<%d',$sta,$end));
    	
    	if(empty($suoyin7)){
    	    $wpdb->insert($wpdb->prefix."keyspider_zhizhu_suoyin",['name'=>'头条','num'=>0,'time'=>$currnetTime,'zhizhu_num'=>$toutiao]);
    	}else{
    	    $wpdb->update($wpdb->prefix . 'keyspider_zhizhu_suoyin',['zhizhu_num'=>$toutiao],['id'=>$suoyin7[0]["id"]]);
    	}
        $keyspider_zhizhu = get_option('keyspider_zhizhu');
    	if(isset($keyspider_zhizhu['log'])){
            $timezone_offet = get_option( 'gmt_offset');
            if($keyspider_zhizhu['log']==1){
                $end = strtotime('-7 days')-$timezone_offet*3600;
                $wpdb->query($wpdb->prepare("DELETE FROM " . $wpdb->prefix . "keyspider_zhizhu  where unix_timestamp(time)<%d",$end));
            }elseif($keyspider_zhizhu['log']==2){
                 $end = strtotime('-15 days')-$timezone_offet*3600;
                $wpdb->query($wpdb->prepare("DELETE FROM " . $wpdb->prefix . "keyspider_zhizhu  where unix_timestamp(time)<%d",$end));
            }elseif($keyspider_zhizhu['log']==3){
                 $end = strtotime('-30 days')-$timezone_offet*3600;
                $wpdb->query($wpdb->prepare("DELETE FROM " . $wpdb->prefix . "keyspider_zhizhu  where unix_timestamp(time)<%d",$end));
            }elseif($keyspider_zhizhu['log']==4){
                 $end = strtotime('-90 days')-$timezone_offet*3600;
                $wpdb->query($wpdb->prepare("DELETE FROM " . $wpdb->prefix . "keyspider_zhizhu  where unix_timestamp(time)<%d",$end));
            }elseif($keyspider_zhizhu['log']==5){
                 $end = strtotime('-180 days')-$timezone_offet*3600;
                $wpdb->query($wpdb->prepare("DELETE FROM " . $wpdb->prefix . "keyspider_zhizhu  where unix_timestamp(time)<%d",$end));
            }elseif($keyspider_zhizhu['log']==6){
                 $end = strtotime('-3 days')-$timezone_offet*3600;
                $wpdb->query($wpdb->prepare("DELETE FROM " . $wpdb->prefix . "keyspider_zhizhu  where unix_timestamp(time)<%d",$end));
                
            }
    	}
    }
    public function keyspider_tongji(){
        $keyspider_tongji = get_option('keyspider_tongji');
        if(!$keyspider_tongji || (isset($keyspider_tongji) && $keyspider_tongji['time']<time()) ){
            $wp_version =  get_bloginfo('version');
            $data =  keyspider_common::keyspider_url(0);
        	$url = "https://api.seoceo.cn/api/Keyspider/index?url={$data}&type=4&url1=".md5($data.'seohnzz.com')."&theme_version=".KEYSPIDER_VERSION."&php_version=".PHP_VERSION."&wp_version={$wp_version}";
        	$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($keyspider_tongji!==false){
                update_option('keyspider_tongji',['time'=>time()+7*3600*24]);
            }else{
                add_option('keyspider_tongji',['time'=>time()+7*3600*24]);
            }
        }
    }
    
}
?>