<?php
require("session_check.php");
require("proxy/proxy.php");

//语言包引入
$u_langpackage=new uilp;
$er_langpackage=new errorlp;
$client_tpl="../templates/";
$tpl_name=get_argg('tpl_name');
$setup_dir=get_argg('setup_dir');

if($tpl_name==''){
	echo '<script type="text/javascript">alert("'.$u_langpackage->u_none.'");window.history.go(-1);</script>';exit;
}
if($setup_dir==''){
	echo '<script type="text/javascript">alert("'.$u_langpackage->u_none_url.'");window.history.go(-1);</script>';exit;
}
if(file_exists('../templates/'.$setup_dir)){
	echo '<script type="text/javascript">alert("templates/'.$setup_dir.'/'.$u_langpackage->u_dir_used.'");window.history.go(-1);</script>';exit;
}

$serv_url=act_substitue("tpl","&folder=".$tpl_name);//远程模板代理地址
$tpl_file=file_get_contents($serv_url);//取得代理返回的数据
if(preg_match("/error\_\d/",$tpl_file)){
	echo '<script type="text/javascript">alert("'.$er_langpackage->{"er_".$tpl_file}.'");window.history.go(-1);</script>';exit;
}
mkdir($client_tpl.$setup_dir);//创建下载目录

//目录创建
preg_match_all("/<dir>[\w\/]+<\/dir>/",$tpl_file,$tpl_dir);
foreach($tpl_dir[0] as $val){
	$val=str_replace(array("<dir>","</dir>"),"",$val);
	if(!file_exists($client_tpl.$val)){
		mkdir($client_tpl.$setup_dir."/".$val);
		echo str_replace("{dir}",$client_tpl.$val,$u_langpackage->u_dir_suc);
	}
}
//文件下载
preg_match_all("/<file>[\w\/\.]+<\/file>/",$tpl_file,$file_name);
foreach($file_name[0] as $rs){
	$rs=str_replace(array("<file>","</file>"),"",$rs);
	$file_content=file_get_contents($serv_url."&fileName=".$rs);
	if(preg_match("/error\_\d/",$file_content)){
		echo $er_langpackage->{"er_".$file_content}."<br />";
	}else{
		$f_ref=fopen($client_tpl.$setup_dir."/".$rs,"w+");
		$write_num=fwrite($f_ref,$file_content);
		if($write_num==0){
			echo $u_langpackage->u_down_false;
		}else{
			echo str_replace("{file}",$client_tpl.$setup_dir."/".$rs,$u_langpackage->u_file_suc);
		}
	}
}

?>