<?php

/*
	[SupeSite/X-Space] (C)2001-2006 Comsenz Inc.
	JavaScript½Õ¥Î¥\\¯à

	$RCSfile: batch.javascript.php,v $
	$Revision: 1.13 $
	$Date: 2006/10/31 04:58:44 $
*/

include_once('./include/main.inc.php');

$mincachetime = 600;

if(empty($_GET['param'])) jsecho('JavaScript Error');
$param = $_GET['param'];

include_once('./data/system/config.cache.php');
$param = js_passport_decrypt($param, $_SSCONFIG['sitekey']);
$paramkey = md5($param);

$timestamp = time();

$paramarr = array();
$params = explode('/', $param);
for ($i=0; $i<count($params)-1; $i=$i+2) {
	$j = $i + 1;
	if(!isset($params[$j])) jsecho('JavaScript Error');
	$paramarr[$params[$i]] = $params[$j];
}
if(empty($paramarr['blocktype']) || empty($paramarr['tpl'])) jsecho('JavaScript Error');
if(!empty($paramarr['cachetime']) && intval($paramarr['cachetime']) > $mincachetime) {
	$paramarr['cachetime'] = intval($paramarr['cachetime']);
} else {
	$paramarr['cachetime'] = $mincachetime;
}

$blocktype = $paramarr['blocktype'];
unset($paramarr['blocktype']);

$param = $mod = '';
foreach($paramarr as $key => $value) {
	$param .= $mod.$key.'/'.$value;
	$mod = '/';
}

if(!is_dir($cachedir = './cache/js')) @mkdir($cachedir);
$cachefile = $cachedir.'/'.$paramkey.'.html';
if(file_exists($cachefile) && $timestamp - filemtime($cachefile) < $paramarr['cachetime']) {
	$jsmessage = implode('', file($cachefile));
	jsecho($jsmessage);
} else {
	include_once(S_ROOT.'./include/common.inc.php');
	block($blocktype, $param);
	$jsmessage = ob_get_contents();
	obclean();
	if(!empty($jsmessage)) writefile($cachefile, $jsmessage, 'text', 'w', 0);
	jsecho($jsmessage);
}

function jsecho($message, $exit=1) {
	$jsmessage = '';
	$message = str_replace(">\r", '>', $message);
	$message = str_replace(">\n", '>', $message);
	preg_match("/\<script\>(.+?)\<\/script\>/is", $message, $mathes);
	if(!empty($mathes[1])) {
		$jsmessage = str_replace(array('<!--', '//-->'), '', $mathes[1]);
		$message = preg_replace("/\<script\>(.+?)\<\/script\>/is", '', $message);
	}
	$message = preg_replace("/(\r|\n)/s", '', $message);
	echo 'document.write(\''.addcslashes($message, '\'\\').'\');';
	echo $jsmessage;
	if($exit) exit;
}

function js_passport_decrypt($txt, $key) {
	$txt = js_passport_key(base64_decode(rawurldecode($txt)), $key);
	$tmp = '';
	for ($i = 0; $i < strlen($txt); $i++) {
		@$tmp .= $txt[$i] ^ $txt[++$i];
	}
	return $tmp;
}

function js_passport_key($txt, $encrypt_key) {
	$encrypt_key = md5($encrypt_key);
	$ctr = 0;
	$tmp = '';
	for($i = 0; $i < strlen($txt); $i++) {
		$ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
		$tmp .= $txt[$i] ^ $encrypt_key[$ctr++];
	}
	return $tmp;
}

?>