<?
session_start();
$selelag = $HTTP_SESSION_VARS["SelLanguage"];
if (empty($selelag)) header("location:../break.php");
require("../config/".$selelag);
require("../_ConnectString.php");                                         //--連接參數設定值 
require("../_function.php");
require("../_image.php");
require("../_read_source/_webmail.php");

if ((isset($HTTP_SESSION_VARS["LoginID_Sess"])) && (isset($HTTP_SESSION_VARS["OperateRole_Sess"])) && (isset($HTTP_SESSION_VARS["GroupNo_Sess"]))){


	if(strlen($HTTP_SERVER_VARS["QUERY_STRING"])>0){
		$_AUTO    = $HTTP_GET_VARS["_AUTO"];
		$_CHARSET = $HTTP_GET_VARS["_CHARSET"];
		$TYPE= $HTTP_GET_VARS["TYPE"];
		$user_sel=$HTTP_GET_VARS["user_sel"];	
	}else{
		$_AUTO	  = $HTTP_POST_VARS["_AUTO"];
		$_CHARSET = $HTTP_POST_VARS["_CHARSET"];
		$RunAs    = $HTTP_POST_VARS["RunAs"];
		$TYPE= $HTTP_POST_VARS["TYPE"];
		$user_sel=$HTTP_POST_VARS["user_sel"];

	}

	$original     = $HTTP_POST_VARS["original"];
	// decode_mime
	function decode_mime($string) { 
		$pos = strpos($string, '=?'); 
		if (!is_int($pos)) { 
			 return $string; 
		 } 
		$preceding = substr($string, 0, $pos); // save any preceding text 
		 $search = substr($string, $pos+2); /* the mime header spec says this is the longest a single encoded word can be */ 
		 $d1 = strpos($search, '?'); 
		if (!is_int($d1)) { 
		return $string; 
		} 
		$charset = substr($string, $pos+2, $d1); //取出字元集的定義部分 
		$search = substr($search, $d1+1); //字元集定義以後的部分＝>$search; 
		$d2 = strpos($search, '?'); 
		if (!is_int($d2)) { 
			return $string; 
		} 		
		$encoding = substr($search, 0, $d2); ////兩個?　之間的部分編碼方式　：ｑ　或　ｂ　 
		$search = substr($search, $d2+1); 
		$end = strpos($search, '?='); //$d2+1 與 $end 之間是編碼了　的內容：=> $endcoded_text; 
		if (!is_int($end)) { 
			return $string; 
		} 
		$encoded_text = substr($search, 0, $end); 
		$rest = substr($string, (strlen($preceding . $charset . $encoding . $encoded_text)+6)); //+6 是前面去掉的　=????=　六個字元 
		switch ($encoding) { 
			case 'Q': 
			case 'q': 
			//註:$encoded_text = str_replace('_', '%20', $encoded_text); 
			//註:$encoded_text = str_replace('=', '%', $encoded_text); 
			//註:$decoded = urldecode($encoded_text); 
			$decoded=quoted_printable_decode($encoded_text); 
			if (strtolower($charset) == 'windows-1251') { 
				$decoded = convert_cyr_string($decoded, 'w', 'k'); 
			} 
			break; 
			case 'B': 
			case 'b': 
			$decoded = base64_decode($encoded_text); 
			if (strtolower($charset) == 'windows-1251') { 
				$decoded = convert_cyr_string($decoded, 'w', 'k'); 
			} 
			break; 
			default: 
				$decoded = '=?'.$charset.'?'.$encoding.'?'.$encoded_text.'?='; 
			break; 
		} 
		//return $preceding.$decoded.$this->decode_mime($rest); 
		return $charset."/&&/".$decoded; 
	}

	Switch($TYPE){
		case "POP3":
		case "IMAP":
		case "SMTP":
			$Table=$TYPE;
			$SUBJECT="_SUBJECT";
		break;
	}

	 if(!empty($_AUTO)&&isset($_AUTO)){
		 $strSQL = "select ".$SUBJECT." from ".$Table." where _AUTO ='".$_AUTO."'";
		 $Result = mysql_query($strSQL);
		 $row    = mysql_fetch_row($Result);
		 //$original=htmlentities($row[0]);
		 $original=convert($row[0]);
	 }
			
	//$SUB_ORG
	$sql = "select * from ".$Table." where _AUTO =".$_AUTO;
	$result = mysql_query($sql);

	if($result && mysql_num_rows($result) > 0){
		$row = mysql_fetch_array($result);
		$file = "/datas/$row[_DBTAG]/$row[_BODY_NAME]";
		system("grep Subject $file>/tmp/subject");
		$subfile=file("/tmp/subject");
		$grep_keywrod="Subject:";

		if(empty($subfile[0])){
			system("grep subject $file>/tmp/subject");
			$grep_keywrod="subject:";
		}
		$arr_subfile=explode($grep_keywrod,$subfile[0]);
		$sub_source=$arr_subfile[1];//get  surcecode of subject
		//print $sub_source;
		$qm= strpos($sub_source,'=?');//判斷是否被編碼
		if(!is_int($qm)){	
			$sub_decode=decode_mime($sub_source);
			$after_decode=$sub_decode;
		}else{
			$sub_source=str_replace("==?=","?=",$sub_source);// =?結尾
			$sub_source=str_replace("=?=","?=",$sub_source);// =?結尾
			$arr_sub_source=explode("=?",$sub_source);//=?開始
			if(count($arr_sub_source)>2){ 	//如果由很多個部份組成則>2
				for($i=1;$i<count($arr_sub_source);$i++){
					$j=$i;		
					if($flag)$j++;
					$sub_part[$j]="=?".$arr_sub_source[$i];		
					$arr=explode("?=",$sub_part[$i]);
					/*
					$flag=0;					
					if(strlen(trim($arr[1]))>1){
						$sub_part[($i+1)]=$arr[1];
						$flag=1;
					}
					*/
				}	
				for($i=1;$i<=count($sub_part);$i++){
					$sub_decode_part=decode_mime($sub_part[$i]);
					$arr_sdp=explode("/&&/",$sub_decode_part);
					
					if(count($arr_sdp)<=1){
						$after_decode.=$arr_sdp[0];
					}
					else{
						$after_decode.=$arr_sdp[1];
						$chrset_decode=$arr_sdp[0];
					}
				}
			}
			else{//只有一個部份組成
				$sub_decode=decode_mime($sub_source);
				$arr_sd=explode("/&&/",$sub_decode);
					if(count($arr_sd)<=1){
						$after_decode=$arr_sd[0];
					}
					else{
						$after_decode=$arr_sd[1];
						$chrset_decode=$arr_sd[0];
					}

			}

		}
		if(!isset($user_sel)||($user_sel!=1)){
			if(!empty($chrset_decode))$_CHARSET=trim($chrset_decode);
			$SUB_ORG=$after_decode;
			$destination = iconv($_CHARSET,"UTF-8",$SUB_ORG);
		}
		else{
			$SUB_ORG=$after_decode;
			$destination = iconv($_CHARSET,"UTF-8",$SUB_ORG);
		}

	}
	//charset list
	function fn_charset($charset){
		$value   = array("GB18030","GB2312","BIG5","HZ","ISO-8859-1","UTF-8","Shift-JIS");
		$display = array("zh-ch","zh-sg","zh-tw","zh-hk","en","utf-8","JP");
		for($i=0;$i<count($value);$i++){
			 if (!strnatcasecmp($value[$i],$charset)){
				 $opt.="<option value=\"".$value[$i]. "\" selected>".$display[$i]."</option>";
			 }else{
				 $opt.="<option value=\"".$value[$i]. "\" >".$display[$i]."</option>"; 
			 }
		}
		return $opt;
	}

	if($RunAs == "Submit"){
		$SQL = "UPDATE ".$Table." SET _DATETIME = _DATETIME , ".$SUBJECT."='".$destination."' WHERE _AUTO= '".$_AUTO."'";
		print $SQL;
		$result = mysql_query($SQL) or die(mysql_error());
		if(!$result)	$Msg=$WEBMAILS_MSG_FAIL;	
	}
}else{
	include("../no_privilege.php");
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
<link href="../Display/display.css" type="text/css" rel="stylesheet">
<Script>
function fn_bnSubmit(){
	document.frm.RunAs.value = "Submit";
	document.frm.submit();
}
function Change_charset(value){
	document.frm.user_sel.value=1;
	document.frm.submit();
}
</Script>
</head>
<body>
<form name="frm" method="" action="">
<input type="hidden" name="RunAs" value="">
<input type="hidden" name="user_sel" value="0">
<input type="hidden" name="_AUTO" value="<? echo $_AUTO;?>">
<input type="hidden" name="TYPE" value="<? echo $TYPE;?>">

<center>
<table height="100%" width="100%" cellSpacing="0" cellPadding="0" border="0" align="center"  style="border-collapse: collapse">
<tr><td align="center" valign="middle">
  <table width="92%" class="OpTb">
		<tr class="Op_Tr">
			<td align="center" colspan="2">
			<img src="../<?echo $DIR_IMAGE.$WEBMAILRP_BCCchange?>"> <span title="<?echo $Title?>"> Converse Subject</span>
			</td>
		</tr>		
		<tr>
			<td align="right"  title="<?echo $CONVERT_CHARSET?>">Charsets :</td>
			<td><select name="_CHARSET" onChange="Change_charset(this.value)"><? echo fn_charset($_CHARSET);?></select></td>
		</tr>
		  <tr>
			<td align="right"  title="<?echo $CONVERT_ORIGINAL?>">Original :</td>
			<td><input type="text" size="60" name="original" value="<?echo $original;?>">
			</td>
		</tr>
		<tr>
			<td align="right"  title="<?echo $CONVERT_VIEW?>">View :</td>
			<td><input type="text" size="60" name="destination" value="<?echo $destination;?>">
			</td>
		</tr>
		<tr height="30">
			<td colspan="2" align="center" title="<?echo $CONVERT_SUBMIT;?>"><input name="bn_submit" type="button" onClick="fn_bnSubmit();" value="Submit"></td>
		</tr>
	</table>
</td></tr></table>
</center>
</form>
</body>
</html>
<?if(($RunAs == "Submit")&&($Msg == '')){?>
	<script language="javascript">
	   window.opener.document.frm.submit();
		window.close();
	</script>
<?}?>

<?if(($RunAs == "Submit")&&($Msg!='')){?>
	<script language="javascript">
		alert(<? echo $Msg;?>);
	</script>
<?}?>