<?php
@session_start();
include('../inc/conn.php');
include('../checkstate.php');
include('trade.fun.php');
$userid=$_SESSION['guestid'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
a{ color:#990000}
body{ background:#0066CC; color:#FFFFFF;}
div{ margin:25px auto;}
</style>
</head>

<body>
<div>
<?php
$xmlpath="xml/$userid/";
$sql="select * from etw_web where usernameid=$userid and istrade=1";
$result=mysql_query($sql);
while($rs=mysql_fetch_array($result)){
	if($rs['langId']==1){
		$submiturl=$rs['website']."/get_trade.aspx?userId=".$userid;
	}else{
		$submiturl=$rs['website']."/get_trade.aspx?lang=".getlanguage($rs['langId'])."&userId=".$userid;
	}
	$website=$rs['website'];
	createxml($userid,$xmlpath,$website,$rs['langId']);
	$select="select proId from trade_product where userId=$userid and istranslated=0 and website='$website'";
	$result2=mysql_query($select);
	$num=mysql_num_rows($result2);
	
	if($num<>0){
		 updatetrade($userid,$website);
	?>
	<iframe width="200" height="20" src="http://<?php echo $submiturl; ?>" frameborder="0" scrolling="no"></iframe>
	<?php 
	}
}?>
<center ><a href='#' style=\"color:red;l ine-height: 25px;\" onclick='parent.location.reload();'>返回 </a></center>
</div>
<?php 
function updatetrade($userid,$website){
	$sql="update trade_product set istranslated=1 where userId=$userid and website='$website'";
	mysql_query($sql) or die('error');
	@mysql_free_result($result);
}
function getlanguage($langid){
	if($langid==1){
		$domain="";
	}else{
		$result=mysql_query("select kuozhan from etw_language where id=$langid");	
		$domain="_".mysql_result($result,0,'kuozhan');
	//	$domain=mysql_result($result,0,'kuozhan');
		mysql_free_result($result);
	}
	return $domain;
}
function createxml($userid,$path,$website,$langid){
	$xmlhead="<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\r";
	$prostring="0";
	$sql="select * from trade_product where userId=$userid and website='$website' order by updatetime";
	$body="<channel>\n\r";
	$result=mysql_query($sql);
	while($rs=mysql_fetch_array($result)){
		$prostring=$prostring.",".$rs['proId'];
		$body=$body."<item>\n\r";
		$body=$body."<userId>".$rs['userId']."</userId>\n\r";
		$body=$body."<proId>".$rs['proId']."</proId>\n\r";
		$body=$body."<proname>".$rs['proname']."</proname>\n\r";
		$body=$body."<keywords>".$rs['keywords']."</keywords>\n\r";
		$body=$body."<description>".urlencode($rs['description'])."</description>\n\r";
		$body=$body."<details>".urlencode(entertobr($rs['details']))."</details>\n\r";
		$body=$body."<picture>".$rs['picture']."</picture>\n\r";
		$body=$body."<minorder>".$rs['minorder']."</minorder>\n\r";
		$body=$body."<fobprice>".$rs['fobprice']."</fobprice>\n\r";
		$body=$body."<port>".$rs['port']."</port>\n\r";
		$body=$body."<payment>".$rs['payment']."</payment>\n\r";
		$body=$body."<deliverytime>".$rs['deliverytime']."</deliverytime>\n\r";
		$body=$body."<supplyability>".$rs['supplyability']."</supplyability>\n\r";
		$body=$body."<package>".$rs['package']."</package>\n\r";
		$body=$body."</item>\n\r\n\r";
	}
	mysql_free_result($result);
	$body=$body."</channel>";
	$pro_xml=$xmlhead.$body;
	
	if(!is_dir($path)){
	  		if(!mkdir($path , 0777 ,true)) die("创建站体文件夹失败");
	 }
	$xmlfile=$path."pro".getlanguage($langid).".xml";
	
	fopen($xmlfile,"w");
	file_put_contents($xmlfile,$pro_xml);	
	
	//$prostring=substr($prostring,1);
	$param_body="<channel>\n\r";
	$param_sql="select * from trade_pro_param where userId=$userid and proId in($prostring) order by proId";
	$para_result=mysql_query($param_sql);
	while($row=mysql_fetch_array($para_result)){
		$param_body=$param_body."<item>\n\r";
		$param_body=$param_body."<userId>".$row['userId']."</userId>\n\r";
		$param_body=$param_body."<proId>".$row['proId']."</proId>\n\r";
		$param_body=$param_body."<parameter>".urlencode($row['parameter'])."</parameter>\n\r";
		$param_body=$param_body."<paramvalue>".urlencode($row['paramvalue'])."</paramvalue>\n\r";
		$param_body=$param_body."</item>\n\r\n\r";
	}
	$param_xml=$xmlhead.$param_body."</channel>";
	$xmlparam=$path."param".getlanguage($langid).".xml";
	fopen($xmlparam,"w");
	file_put_contents($xmlparam,$param_xml);
}
function entertobr($val){
	$temp=ereg_replace("\n\r","<br>",$val);
	$temp=ereg_replace("\n","<br>",$temp);
	return $temp;
}
function replaceo($string){
	$temp=ereg_replace("&","&amp;",$string);
	$temp=ereg_replace("<","&lt;",$temp);
	return $temp;
}
?>
</body>
</html>