<?php
/*
		[FuShow.cn] (C) 2013-2014 HXWY Inc.
		FuShow Friend V3.0				Id: js.php
		Email <service@fushow.cn>		http://fushow.cn/
*/

@define('IN_FUSHOW', TRUE);

$id = empty($_GET['id'])?0:intval($_GET['id']);
$adid = empty($_GET['adid'])?0:intval($_GET['adid']);

if($id) {
	//模块
	include_once('./data/data_block.php');
	if(!isset($_SGLOBAL['block'][$id])) {
		echo 'document.write(\'No data.\')';
		exit();
	}
	
	$updatetime = $_SGLOBAL['block'][$id];
	
	//缓存
	$cachefile = "./data/block_cache/block_$id.js";
	if($updatetime > 0 && file_exists($cachefile) && (time() - filemtime($cachefile) < $updatetime)) {
		if(@$fp = fopen($cachefile, 'r')) {
			@$content = fread($fp, filesize($cachefile));
			fclose($fp);
		} else {
			$content = 'document.writeln(\'No data.\')';
		}
		echo $content;
		exit();
	}
	
	//读取数据
	include('./common.php');
	
	//外链接禁止显示导航条
	$_SCONFIG['linkguide'] = 0;
	
	//禁止缓存
	$_SCONFIG['allowcache'] = 0;
	
	include template("data/blocktpl/$id");
	
	$obcontent = ob_get_contents();
	obclean();
	
	$s = array("/(\r|\n)/", "/\<div\s+class=\"pages\"\>.+?\<\/div\>/is", "/\s+(href|src)=\"(.+?)\"/ie");
	$r = array("\n", '', "js_mkurl('\\1', '\\2')");

	$content = '';
	if($obcontent) {
		$obcontent = preg_replace($s, $r, $obcontent);
		$lines = explode("\n", $obcontent);
		foreach ($lines as $line) {
			$line = addcslashes(trim($line), '/\'\\');
			$content .= "document.writeln('$line');\n";
		}
	} else {
		$content .= "document.writeln('NO DATA')";
	}
	if($updatetime > 0) swritefile($cachefile, $content);
	echo $content;

} elseif ($adid) {
	//读取广告文件
	$file = './data/adtpl/'.$adid.'.htm';
	if(@$lines = file($file)) {
		foreach ($lines as $line) {
			$line = addcslashes(trim($line), '/\'\\');
			echo "document.writeln('$line');\n";
		}
	} else {
		echo "document.writeln('NO AD.')";
	}
}

function js_mkurl($tag, $url) {
	if(!preg_match("/^(http\:\/\/|ftp\:\/\/|https\:\/\/|\/)/i", $url)) {
		$url = getsiteurl().$url;
	}
	return " {$tag}=\"$url\"";
}

?>