<!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">
<?php
	//模板添加翻译标签， 英文的Templates1下的文件，带_的文件被删除
	session_start();
	require_once('inc/conn.php');
	require_once('inc/constant.php');
	require_once('inc/ls_syscode.php');
	require_once('inc/ls_inccode.php');
	
	require_once('inc/check.php');
	$opendb=new mydb();
	require_once('inc/char_set.php');
	
	$ls=$_GET['ls'];
 
	$sourcea=explode(",","../Templates1,../Templates"); //dwt 加标签
	//$allcopyfiles="bootstrap,css";
	$skiptranswords=explode(",",'\r\n,\n,\r," />,-->,"/>,">,*,'); // 跳过关键字


	$submitaddlang=$_POST['submitaddlang'];

	if($submitaddlang=="确定"){ //
	foreach($sourcea as $source){
		$handle=dir($source);   
		while($entry=$handle->read()) {   
			if(($entry!=".")&&($entry!="..")){ 
				  $scrFile=$source."/".$entry;
				  if( strpos($entry,"_")>1) {unlink($scrFile); echo "--> 已删除 ".$scrFile."<br/>";} //删除带_的备份文件
				  //if( file_exists( $scrFile ) ) { rename( $scrFile,$source."/bak/".$entry);  }
				  
				  if(!is_dir( $scrFile)) {    //目录 
				  $fp = fopen($scrFile,"r");
				  $content  = fread($fp,filesize ($scrFile));
				  fclose ($fp); //关闭指针
	
				  $content1=$content;
				  echo "<strong>===>".$scrFile."</strong><br/><br/>";
				  $str1=">";
				  $str2="<"; 	 
				  $p = "%".$str1."(.*?)".$str2."%si";    //正则表达式
				  preg_match_all($p, $content, $arr);
				  $temarr = $arr[1];
				  foreach($temarr as $val){
					  $val2=trim($val);
					  if( $val2==""&& !strpos($val2,"<translate>")===false) continue; // 为空，有翻译标签跳过
					  if(  !strpos($content, "<translate>".$val."</translate>")===false) continue; //有翻译标签跳过
					 
					  if( strin($val2,$skiptranswords)) continue; // 跳过数组
					  
					  if( skiptrans($val,$content) ) continue; // 前置判断js等
					  echo $val."<br/><br/>";
					  $content1= str_replace($str1.$val.$str2, $str1."<translate>".$val."</translate>".$str2, $content1);
					  //echo $content."<br/><br/><br/>";
				 }
				 
				  $wfp   = fopen($scrFile,"w"); //打开文件指针
				  if (fwrite ($wfp,$content1))
				  {  //将信息写入文件
					 $ls_msg.=$scrFile."<br/>";
				  } 
				 fclose ($wfp); //关闭指针
				 
				}
			}
		}
		}
	$ls_msg.="模板已全部添加；";
 	fclose ($handle); //关闭指针
}	
	

	
// 前置判断js等				 					
function skiptrans($val,$content){
	$skipwords=explode(",","script,script"); // 跳过关键字
	
	$pos=strpos($content,$val);

	$aheadstr=substr($content,0,$pos-1);
	$pos2=strrpos($aheadstr,"<");
	//echo  $pos."=========".$pos2."<br/>"; //$aheadstr."<br/><br/>
	$aheadstr=substr($content,$pos2,$pos-$pos2);
	//echo $aheadstr."<br/>";
	for($i=0;$i<count($skipwords);$i++){
		if(!(strpos($aheadstr,$skipwords[$i])===false)) return true;
	
	}
	return false;
}
?>

<link href="css/admin.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="css/style2.css" />
<script language="JavaScript" src="js/jquery.js"></script>
<script language="JavaScript" src="js/ls.js"></script>
<title>mutiLang</title>
</head>
<body bgcolor="#f4f4f4">
<table width="100%" border="0" align="center" cellpadding="1" cellspacing="1">
	<tr>
        <td height="18" style="border-bottom: 5px solid #00c0ef; padding-bottom:5px;"><div align="center">模板增加翻译标签</div>
	  </td>
	</tr>
	<tr>
		<td height="198" align="center" valign="top" bgcolor="#dee7ff">                    <form id="fmconfig" name="fmconfig" method="post" enctype="multipart/form-data"  action="<?php echo $PHP_SELF;?>">
                      <table width="99%" height="120" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
                      
                        <tr>
                          <td height="118" align="left"  bgcolor='#eee' style='padding-left:5px;'>提示：
                            <?=$ls_msg;?></td>
                        </tr>
            </table>
                    </form>
          <p>
          </p>
          <form id="adfastmanual" name="adfastmanual" method="post" enctype="multipart/form-data"  action="<?php echo $PHP_SELF;?>">
        
         <p><br/> </p>
         <p></p>
         <p></button>
          <button name="submitaddlang" type="submit" value="确定" onClick=" "/> 确定</button>
		  <button type="reset" name="button" id="button" value="重置" />重置</button>
	      <br /><br />
	      </p>
        </form>   

      </p></td>
       
  </tr>
</table>
</body>
</html>