<?php
/*
'**************************************************************************************************
' 软件名称: 搜一次 Content Management System
' 版本编号: Version 1.X
' 官方网站: http://www.syccms.com
' 官方论坛：http://bbs.syccms.com
' 版权所有: 搜一次运营团队
' 法律顾问: 杭州市元茂律师事务所 严飞律师
' 郑重声明:
'    1、任何个人或组织不得在未经授权的情况下删除、修改、拷贝本软件及其他副本上一切关于版权的信息；
'    2、搜一次网络事业部保留此软件的法律追究权利
'**************************************************************************************************
*/
if(!defined('S_CacheTime')){
	exit;
}

$timestamp = time();
$hours = date('G',$timestamp);
//避过高峰期 0-2 11-14 17-20 23-24
if (($hours>=0 && $hours<=2) || ($hours>=11 && $hours<=14) || ($hours>=17 && $hours<=20) || ($hours>=23 && $hours<=24)) {
	exit;
}

$cacheFile = _SYC_ROOT_.'cache/cache.lock';
$cacheTime = (int)S_CacheTime==0 ? 86400 : S_CacheTime;
$isupdate = 0;
$array = array();

if (is_file($cacheFile)) {
	$fp = fopen($cacheFile, 'a+');
	flock($fp, LOCK_EX);
	$str = fread($fp, 43);
	if (!empty($str)) {
		$array = explode(',', $str);
		if (isset($array[0])) {
			if ((int)$array[0]>$timestamp) {
				$isupdate = 1;
				include(_SYC_ROOT_.'include/class/clear.class.php');
				$path=_SYC_ROOT_.'cache/';
				$clear=new clear();
				if(!(clear::isEmpty($path))){
					clear::deleteDir($path);
				}
				$array[0] = $timestamp + $cacheTime;
			}
		} else {
			$isupdate = 1;
			$array[0] = $timestamp + $cacheTime;
		}
		
		if (isset($array[1])) {
			if ((int)$array[1]>$timestamp) {
				$isupdate = 1;
				$db->query('update s_video set S_HitsDay=0 where 1');
				$db->query('update s_article set S_HitsDay=0 where 1');
				$array[1] = strtotime( date('Y-m-d', $timestamp+86400) );
			}
		} else {
			$isupdate = 1;
			$array[1] = strtotime( date('Y-m-d', $timestamp+86400) );
		}
		
		if (isset($array[2])) {
			if ((int)$array[2]>$timestamp) {
				$isupdate = 1;
				$db->query('update s_video set S_HitsWeek=0 where 1');
				$db->query('update s_article set S_HitsWeek=0 where 1');
				$array[2] = strtotime( date('Y-m-d', $timestamp+86400*(8-date('N',$timestamp))) );			
			}
		} else {
			$isupdate = 1;
			$array[2] = strtotime( date('Y-m-d', $timestamp+86400*(8-date('N',$timestamp))) );
		}
		
		if (isset($array[3])) {
			if ((int)$array[3]>$timestamp) {
				$isupdate = 1;
				$db->query('update s_video set S_HitsMon=0 where 1');
				$db->query('update s_article set S_HitsMon=0 where 1');
				$array[3] = mktime(0, 0, 0, date('m', $timestamp)+1, 1, date('Y',$timestamp));
			}
		} else {
			$isupdate = 1;
			$array[3] = mktime(0, 0, 0, date('m', $timestamp)+1, 1, date('Y',$timestamp));
		}
	}
	if ($isupdate==1) {
		fwrite($fp, implode(',', $array));
	}
	flock($fp, LOCK_UN);
	fclose($fp);
} else {
	$array = array(
		$timestamp + $cacheTime,
		strtotime( date('Y-m-d', $timestamp+86400) ),
		strtotime( date('Y-m-d', $timestamp+86400*(8-date('N',$timestamp))) ),
		mktime(0, 0, 0, date('m', $timestamp)+1, 1, date('Y',$timestamp)),
	);
	$fp = fopen($cacheFile, 'a+');
	flock($fp, LOCK_EX);
	fwrite($fp, implode(',', $array));
	flock($fp, LOCK_UN);
	fclose($fp);
}
?>