<?php
error_reporting(0);
define('SYSTEM_ROOT', dirname(__FILE__).'/');
define('ROOT', dirname(SYSTEM_ROOT).'/');
define('TIMESTAMP', time());
/**
 * [is_end_all true已过期false未过期]
 * @param  [type]  $endtime_arr [description]
 * @param  [type]  $login       [description]
 * @return boolean              [description]
 */
function is_end_all($endtime_arr,$login){
    global $date;
    if($login['apipwd'] != '')return false;
    $arr = json_decode($endtime_arr,true);
    foreach ($arr as $cid => $time) {
        if($time > $date){
            return false;
            break; 
        }
    }
    return true;
}
function curl_run($url) {
	global $conf;
	$ch=curl_init();
	$urlarr = parse_url($url);
	if($conf['localcron']==1 && $urlarr['host']==$_SERVER['HTTP_HOST']){
		$url=str_replace('http://'.$_SERVER['HTTP_HOST'].'/','http://127.0.0.1/',$url);
		$url=str_replace('https://'.$_SERVER['HTTP_HOST'].'/','https://127.0.0.1/',$url);
		$httpheader[] = 'Host: '.$_SERVER['HTTP_HOST'];
	}elseif($conf['localcron']==2 && $urlarr['host']==$_SERVER['HTTP_HOST'] && isset($_SERVER['SERVER_ADDR'])){
		$url=str_replace('http://'.$_SERVER['HTTP_HOST'].'/','http://'.$_SERVER['SERVER_ADDR'].'/',$url);
		$url=str_replace('https://'.$_SERVER['HTTP_HOST'].'/','https://'.$_SERVER['SERVER_ADDR'].'/',$url);
		$httpheader[] = 'Host: '.$_SERVER['HTTP_HOST'];
	}
	$httpheader[] = "Accept: */*";
	$httpheader[] = "Connection: close";
	curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
	curl_setopt($ch, CURLOPT_URL,$url);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
	//curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
	curl_setopt($ch, CURLOPT_TIMEOUT, 1);
	curl_setopt($ch, CURLOPT_NOBODY, true);
	curl_setopt($ch, CURLOPT_NOSIGNAL, true);
	curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36');
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_exec($ch);
	curl_close($ch);
	return true;
}

function run_once($row,$timeout,$ctimeout) {
	$curl=curl_init();
	if(!empty($row['realip'])){
		$urlarr = parse_url($row['url']);
		$row['url']=str_replace('http://'.$urlarr['host'].'/','http://'.$row['realip'].'/',$row['url']);
		$row['url']=str_replace('https://'.$urlarr['host'].'/','https://'.$row['realip'].'/',$row['url']);
		$httpheader[] = 'Host: '.$urlarr['host'];
	}
	if($row['usep']==1)
	{
		curl_setopt($curl,CURLOPT_PROXY,$row['proxy']);
	}
	$httpheader[] = "Accept: application/json, text/plain, */*";
	$httpheader[] = "Accept-Language: zh-CN,zh;q=0.8";
	$httpheader[] = "Connection: close";
	curl_setopt($curl,CURLOPT_HTTPHEADER,$httpheader);
	curl_setopt($curl,CURLOPT_URL,$row['url']);
	curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,$ctimeout);
	curl_setopt($curl,CURLOPT_TIMEOUT,$timeout);
	curl_setopt($curl,CURLOPT_NOBODY,1);
	curl_setopt($curl,CURLOPT_NOSIGNAL,true);
	curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($curl,CURLOPT_AUTOREFERER,1);
	curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
	curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false);
	if ($row['referer']!='')
	{
		curl_setopt($curl,CURLOPT_REFERER,$row['referer']);
	}
	if ($row['useragent']!='')
	{
		curl_setopt($curl,CURLOPT_USERAGENT,$row['useragent']);
	}
	else
	{
		curl_setopt($curl,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36');
	}
	if ($row['cookie']!='')
	{
		curl_setopt($curl,CURLOPT_COOKIE,$row['cookie']);
	}
	if($row['post']==1)
	{
		$postfields=str_replace('[时间]',$date,$row['postfields']);
		curl_setopt($curl,CURLOPT_POST,1);
		curl_setopt($curl,CURLOPT_POSTFIELDS,$postfields);
	}
	curl_exec($curl);
	curl_close($curl);
}

function coin_check($uid,$coin,$type=null,$qq=0){
	global $DB,$conf,$vip_func;
	if($conf['cron_free']){
		$DB->query("update ".DBQZ."_user set coin=coin-{$coin} where userid='{$conf['cron_free']}'");
		return true;
	}elseif($conf['vipmode']==1){
		$udata=$DB->getRow("select userid,coin from ".DBQZ."_user where userid='".$uid."' limit 1");
		$qqrow=$DB->getRow("select vip,vipdate from ".DBQZ."_qq where uid='".$uid."' limit 1");
		if($qqrow['vip']==1 && strtotime($qqrow['vipdate'])>time() || $qqrow['vip']==2 || $udata['userid']==1){
			$isvip=1;
		}else{
			$isvip=0;
		}
	}else{
		$udata=$DB->getRow("select userid,coin,vip,vipdate from ".DBQZ."_user where userid='".$uid."' limit 1");
		if($udata['vip']==1 && strtotime($udata['vipdate'])>time() || $udata['vip']==2 || $udata['userid']==1){
			$isvip=1;
		}else{
			$isvip=0;
		}
	}
	if($conf['vip_coin']==1 && $isvip==1){
		return true;
	}elseif(in_array($type,$vip_func) && $isvip==0){
		return false;
	}else{
		if($udata['coin']>=$coin){
			$DB->query("update ".DBQZ."_user set coin=coin-{$coin} where userid='".$uid."'");
			return true;
		}else{
			return false;
		}
	}
}
function dojob(){
	$fp=fsockopen($_SERVER["HTTP_HOST"],$_SERVER['SERVER_PORT']);
	$out="GET {$_SERVER['PHP_SELF']}?key={$_GET['key']} HTTP/1.0".PHP_EOL;
	$out.="Host: {$_SERVER['HTTP_HOST']}".PHP_EOL;
	$out.="Connection: Close".PHP_EOL.PHP_EOL;
	fputs($fp,$out);
	fclose($fp);
}

if(function_exists("set_time_limit")){
	@set_time_limit(0);
}
if(function_exists("ignore_user_abort")){
	@ignore_user_abort(true);
}

date_default_timezone_set("PRC");
$date=date("Y-m-d H:i:s");
$time=time();
$t=date("H");

include_once(ROOT."includes/autoloader.php");
Autoloader::register();
require ROOT.'config.php';
define('RUN_KEY',md5($dbconfig['user'].md5($dbconfig['pwd'])));
//连接数据库
$DB = new \lib\PdoHelper($dbconfig);
$conf_ = $DB->getAll("SELECT * FROM pre_config");
foreach($conf_ as $k){$conf[$k['k']] = $k['v'];}
define('SYS_KEY',$conf['syskey']);
$hosturl=($_SERVER['SERVER_PORT']==443 ? 'https://' : 'http://').$_SERVER['HTTP_HOST'];
$siteurl=$hosturl.$sitepath.'/';

include_once(ROOT."includes/functions.php");

$task_num=$conf['task_num']?$conf['task_num']:60;
?>