<?php

/*
	[SupeSite/X-Space] (C)2001-2006 Comsenz Inc.
	静态文件自动更新js

	$RCSfile: batch.html.php,v $
	$Revision: 1.24 $
	$Date: 2007/05/30 21:44:53 $
*/

@define('IN_SUPESITE', TRUE);

$code = empty($_GET['code'])?'':trim($_GET['code']);
if(empty($code)) exit();

$codearr = explode('/', $code);

$url = empty($codearr[0])?'':rawurldecode(($codearr[0]));
if(empty($url)) exit();

$maketime = empty($codearr[1])?0:intval($codearr[1]);
if(empty($maketime)) exit();

$updatetime = empty($codearr[2])?0:intval($codearr[2]);
if(empty($updatetime)) exit();

$uid = empty($codearr[3])?0:intval($codearr[3]);
$itemid = empty($codearr[4])?0:intval($codearr[4]);
$action = empty($codearr[5])?'':trim($codearr[5]);
$lastmodified = empty($_GET['lastmodified'])?'0':$_GET['lastmodified'];

//数据统计
if(!empty($itemid) && ($action == 'viewspace' || $action == 'viewnews')) {
	$isupdate = freshcookie($itemid);
	if($isupdate) {
		$logfile = './log/viewcount.log';
		if(@$fp = fopen($logfile, 'a+')) {
			fwrite($fp, $itemid."\n");
			fclose($fp);
			@chmod($logfile, 0777);
		}
	}
}
if(!empty($uid) && ($action == 'space' || $action == 'viewspace')) {
	$isupdate = freshcookie('u'.$uid);
	if($isupdate) {
		$logfile = './log/spaceviewcount.log';
		if(@$fp = fopen($logfile, 'a+')) {
			fwrite($fp, $uid."\n");
			fclose($fp);
			@chmod($logfile, 0777);
		}
	}
}

//获取参数正确
$timestamp = time();
$needupdate = false;
if($timestamp - $maketime > $updatetime) {
	$needupdate = true;
}

$cookie = '';
if (!$needupdate && @include_once('./data/system/html.cache.php')) {
	if($htmlupdatemode == 1) {
		//游客不更新
		include_once('./config.php');
		$cookie = empty($_COOKIE[$cookiepre.'auth'])?'':$_COOKIE[$cookiepre.'auth'];
		if(empty($cookie)) $needupdate = false;
	} else {
		$cookie = 1;
	}
	if(!empty($cookie) && !empty($htmltime) && $maketime < $htmltime && $htmltime < $timestamp) {
		$needupdate = true;
	}
}
if (!empty($cookie) && !$needupdate && $uid) {
	$idvalue = ($uid>9)?substr($uid, -2, 2):$uid;
	$userhtml = './user/'.$idvalue.'/html_'.$uid.'.cache.php';
	if(@include_once($userhtml)) {
		if(!empty($uhtmltime) && $maketime < $uhtmltime && $uhtmltime < $timestamp) {
			$needupdate = true;
		}
	}
}

//ajax无刷新更新
if($needupdate) {
	print<<<END
	function Browser() {
		this.IsIE = function() {
			try {
				return this.Test(document.all && !document.contains)!=false;
			} catch(e) {
				// for check IE 5.01
				if (document.all) return true;
				return false;
			}
		}
		this.Test = function(test) {
			if (test==undefined) {
				return false;
			} else {
				return test;
			}
		}
	}
	
	var brs = new Browser();
	
	var xmlHttp = false;
	if(window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
		if(xmlHttp.overrideMimeType) {
			xmlHttp.overrideMimeType('text/xml');
		}
	} else if(window.ActiveXObject) {
		var versions = ['Microsoft.XMLHTTP', 'MSXML.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.7.0', 'Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP'];
		for(var i=0; i<versions.length; i++) {
			try {
				xmlHttp = new ActiveXObject(versions[i]);
			} catch(e) {
			}
		}
	}
	
	try {
		try {
			if (!brs.IsIE() && netscape.security.PrivilegeManager.enablePrivilege) {
				netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
			}
		} catch (e) {
		}
		xmlHttp.open("GET", "$url/php/1/modified/$lastmodified", true);
		xmlHttp.onreadystatechange=function() {
			if (xmlHttp.readyState==4) {
			}
		}
		xmlHttp.send("");
	} catch (e) {
	}
END;
}

function freshcookie($itemid) {
	$isupdate = 1;
	if(empty($_COOKIE)) {//必须由cookie
		$isupdate = 0;
	} else {
		$old = empty($_COOKIE['supe_batch_html_refresh_items'])?0:trim($_COOKIE['supe_batch_html_refresh_items']);
		$itemidarr = explode('_', $old);
		if(in_array($itemid, $itemidarr)) {
			$isupdate = 0;
		} else {
			$itemidarr[] = trim($itemid);
			setcookie('supe_batch_html_refresh_items', implode('_', $itemidarr));
		}
	}
	return $isupdate;
}

?>
