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

$_SearchPath = "../_search/mail_search_list.php";
$Host_ip = $HTTP_SERVER_VARS["HTTP_HOST"];

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

	$QUERY_START_DATETIME	= $HTTP_POST_VARS["QUERY_START_DATETIME"];  
	$QUERY_END_DATETIME		= $HTTP_POST_VARS["QUERY_END_DATETIME"];
	$QUERY_IP		= $HTTP_POST_VARS["QUERY_IP"];
	$QUERY_IP_1		= $HTTP_POST_VARS["QUERY_IP_1"];
	$QUERY_NAME		= $HTTP_POST_VARS["QUERY_NAME"];
	$QUERY_URL		= $HTTP_POST_VARS["QUERY_URL"];  
	$QUERY_TAG		= $HTTP_POST_VARS["QUERY_TAG"]; 
	$QUERY_ID		= $HTTP_POST_VARS["QUERY_ID"]; 
	$QUERY_ACCOUNT	= $HTTP_POST_VARS["QUERY_ACCOUNT"]; 

	$sizepage      = $HTTP_POST_VARS["sizepage"];       //--每頁筆數
	$RunAs         = $HTTP_POST_VARS["RunAs"];			//--刪除 或 忽略
	$Checked       = $HTTP_POST_VARS["Checked"];
	$SortField     = $HTTP_POST_VARS["SortField"];      //--排序欄位
	$SortType      = $HTTP_POST_VARS["SortType"];       //--排序方式
	$Ip_Name       = $HTTP_POST_VARS["Ip_Name"];        //--顥示的模式
	$strWhere	   = $HTTP_POST_VARS["strWhere"];		//--search all
	$WEBMAILR	   = $HTTP_POST_VARS['WEBMAILR'];			//--全文檢索 -auto 值
	$mime		   = $HTTP_POST_VARS['mime'];			//--全文檢索 flag
	$digital	   = $HTTP_POST_VARS['digital'];	    //--數位資產 flag
	$to_page	   = $HTTP_POST_VARS['to_page'];

	//相關變數初始化 
	if (!isset($to_page) || empty($to_page)){
		$to_page = 1;
	}//End : if (相關變數初始化)

	if (!isset($sizepage) || empty($sizepage)){
		$sizepage = $HTTP_SESSION_VARS["size_page"];
	}

	if ($Ip_Name == '')  $Ip_Name = '_IP';
	$ErrorMsg = "";
	$_Disabled = "";
	//$bgcolorCol = "bgcolor=\"#A0C6E5\"";

	
	//--判斷點選那一個顯示模式
	switch ($Ip_Name){
		case "_IP":
			$chk_IP = "checked";
			break;
		case "_NAME":
			$chk_NAME = "checked";
			break;
	}

	//--畫面設定排序時使用
	if ($SortField == "") $SortField = $DB_HTTP_PAGE_AUTO;
	include($_SORT_PATH);

	//判斷每頁秀的資料筆數
	if ($RunAs == 'UpdatePageSize'){
		if ($sizepage){ 
			$HTTP_SESSION_VARS["size_page"] = $sizepage;
		}//End : if()
	}//End : if(判斷每頁秀的資料筆數)

	
	//--刪除資料
	if($RunAs == "Delete"){
		$flag = 0;
		for($i=0; $i<count($chkbox); $i++){
			$strSQL = "Select * From ".$DB_HTTP_PAGE." where ".$DB_HTTP_PAGE_AUTO."='".$chkbox[$i]."'";
			$intSQL = mysql_query($strSQL);
			$rowSQL = mysql_fetch_array($intSQL);
			$DelFilePath = $DATAS_PATH_WEBMAILS.$rowSQL[$DB_HTTP_PAGE_DBTAG]."/".$rowSQL[$DB_HTTP_PAGE_HTML];

			if (file_exists($DelFilePath)){
				system("echo rm -f $DelFilePath >> /tmp/dc.cmd");
			}

			$strDEL = "Delete From ".$DB_HTTP_PAGE." where ".$DB_HTTP_PAGE_AUTO."='".$chkbox[$i]."'";
			$intDEL = mysql_query($strDEL);
			if (!$intDEL) $flag = 1;
		}

		if ($flag){
			$ErrorMsg = $WEBMAILRM_DELFAIL;
		}
		else{
			mysql_query("OPTIMIZE TABLE HTTP_PAGE");
			$ErrorMsg = $WEBMAILRM_DELSUCCESS;
		}
	}
    
	
	//-- System super user(PROGRAM ENTRY PLACE)
	$strSQL = "Select count(*) From ".$DB_HTTP_PAGE;
	if ($mime == 1)
	{
		$strSQL = $strSQL." Where ".$DB_HTTP_PAGE_AUTO." in (".$WEBMAILR.")";
	}
	else{
		$strSQL = $strSQL." Where ".$DB_HTTP_PAGE_TAG."<>'' and ".$DB_HTTP_PAGE_TAG."<>'Not-Found'  ";
		if(fn_NotShowIP()!="''")$strSQL = $strSQL." and ".$DB_HTTP_PAGE_IP." not in (".fn_NotShowIP().")";

		if ($HTTP_SESSION_VARS['GroupNo_Sess']!=0){
			$strSQL = $strSQL." and ".$DB_HTTP_PAGE_IP." in (".fn_IPcontrol($HTTP_SESSION_VARS['GroupNo_Sess']).")";
		}
		if($strWhere["WEBMAILR"] != "") $strSQL = $strSQL.stripcslashes($strWhere["WEBMAILR"]);
	}

	$condition = fn_SearchWebmail($QUERY_START_DATETIME,$QUERY_END_DATETIME,$QUERY_TAG,$QUERY_IP,$QUERY_IP_1,$QUERY_URL,$QUERY_ID,$QUERY_ACCOUNT);

	$strSQL = $strSQL.$condition;
	
   //print $strSQL;
   //計算符合文章總數 & 初始化分頁變數
   include($_PAGE_PATH);

   //為了畫面排序的sql語法
    $strSQL = "Select ".$DB_HTTP_PAGE_AUTO.",".$DB_HTTP_PAGE_DATETIME.",".$DB_HTTP_PAGE_IP.",".$DB_HTTP_PAGE_MAC.",".$DB_HTTP_PAGE_HTML.",".$DB_HTTP_PAGE_HOST.",".$DB_HTTP_PAGE_DBTAG.",".$DB_HTTP_PAGE_ACCOUNT.",".$DB_HTTP_PAGE_TAG;
    $strSQL = $strSQL." From ".$DB_HTTP_PAGE;
    if ($mime == 1)
	{
		$strSQL = $strSQL.fn_Datamining($WEBMAILR,$DB_HTTP_PAGE_AUTO,$m,$n,$to_page);
		$m = 0;
	}
	else{
		$strSQL = $strSQL." Where ".$DB_HTTP_PAGE_TAG."<>'' and ".$DB_HTTP_PAGE_TAG."<>'Not-Found'  ";
		if(fn_NotShowIP()!="''")$strSQL = $strSQL." and ".$DB_HTTP_PAGE_IP." not in (".fn_NotShowIP().")";

		if ($HTTP_SESSION_VARS['GroupNo_Sess']!=0){
			$strSQL = $strSQL." and ".$DB_HTTP_PAGE_IP." in (".fn_IPcontrol($HTTP_SESSION_VARS['GroupNo_Sess']).")";
		}
		if($strWhere["WEBMAILR"] != "") $strSQL = $strSQL.stripcslashes($strWhere["WEBMAILR"]);
	}

    $strSQL = $strSQL.$condition;

    $strSQL = $strSQL." Order by ".$SortField." ".$SortType." limit ".$m.", ".$n;
    $intResult = mysql_query($strSQL);
	
	//<--組show畫面字串
	if (mysql_num_rows($intResult) == 0){
		$WebmailrList = "<tr height=\"30\" class=\"NoData\"><td colspan=\"";
		if (($HTTP_SESSION_VARS["OperateRole_Sess"]=="Super")&&($HTTP_SESSION_VARS["GroupNo_Sess"]=='0')){
			if(fn_ad()) $WebmailrList = $WebmailrList."7"; else $WebmailrList = $WebmailrList."6";
		}else{
			if(fn_ad()) $WebmailrList = $WebmailrList."6"; else $WebmailrList = $WebmailrList."5";
		}
		include("../no_data.php");
		$WebmailrList = $WebmailrList."\"".$NoDataList;
		$_Disabled = "disabled";
	}
	else{
		$WebmailrList = "";
		$chkCount = ($to_page-1)*$sizepage+1; 
		while ($row=mysql_fetch_array($intResult)){
			$_HTML = $row[$DB_HTTP_PAGE_HTML];

			if (trim($_HOST)=='(null)')   $CUT_HOST = "None";
			if (strlen($row[$DB_HTTP_PAGE_HOST])>60)        $CUT_HOST=substr($row[$DB_HTTP_PAGE_HOST],0,60)."...";  else $CUT_HOST = $row[$DB_HTTP_PAGE_HOST]; 
			
			if ($chkCount%2 == 1){
				$WebmailrList = $WebmailrList."<tr  class=\"Tr_Do\" onmouseover=\"this.className='Tr_Do_Out'\" onmouseout=\"this.className='Tr_Do_Over'\">";
			}else{
				$WebmailrList = $WebmailrList."<tr class=\"Tr_De\" onmouseover=\"this.className='Tr_De_Out'\" onmouseout=\"this.className='Tr_De_Over'\">";
			}

			$WebmailrList = $WebmailrList."<td align=\"center\" title=\"".$row[_AUTO]."\">".$chkCount.".</td>\n";


			//--datetime 
			$WebmailrList = $WebmailrList."<td align=\"center\"><a href=\"#\" onClick=\"ChangeCondition('_DATETIME','".$row[$DB_HTTP_PAGE_DATETIME]."')\">".fn_DateTime($row[$DB_HTTP_PAGE_DATETIME])."</a></td>\n";

			switch ($Ip_Name){
			case "_IP":
				$WebmailrList = $WebmailrList."<td align=\"center\"><a href=\"#\" onClick=\"ChangeCondition('_IP','".$row[$DB_HTTP_PAGE_IP]."');\"><p title=\"".$row[$DB_HTTP_PAGE_MAC]."\">".$row[$DB_HTTP_PAGE_IP]."</p></a></td>\n";
				break;
			case "_NAME":
				$SQL = "Select * From ".$DB_IPCONTROL." Where ".$DB_IPCONTROL_IP." = '".$row[$DB_HTTP_PAGE_IP]."'";
			    $Result = mysql_query($SQL);
				//print $SQL;
                if (mysql_num_rows($Result)>0){
					$row_sub = mysql_fetch_array($Result);
					$row_username = $row_sub[$DB_IPCONTROL_USERNAME];
					$WebmailrList = $WebmailrList."<td align=\"center\"><p title=\"".$row[$DB_HTTP_PAGE_MAC]."\"><a href=\"#\" onClick=\"ChangeCondition('_IP','".$row[$DB_HTTP_PAGE_IP]."');\">";
					if ($row_sub[$DB_IPCONTROL_USERNAME] != '***') $WebmailrList = $WebmailrList.$row_sub[$DB_IPCONTROL_USERNAME]; else $WebmailrList = $WebmailrList.$row[$DB_HTTP_PAGE_IP];
					$WebmailrList = $WebmailrList."</a></p></td>";
				}
				else{
					$WebmailrList = $WebmailrList."<td align=\"center\"><p title=\"".$row[$DB_HTTP_PAGE_MAC]."\"><a href=\"#\" onClick=\"ChangeCondition('_IP','".$row[$DB_HTTP_PAGE_IP]."');\">".$row[$DB_HTTP_PAGE_IP]."</a></p></td>";
				}
				break;
			}
		//--轉換 DBTAG 的格式
			$_DBTAG ='';
			$_DBTAG =str_replace("-","_",$row[$DB_HTTP_PAGE_DBTAG]);
			$_DBTAG =str_replace(":","_",$_DBTAG);
			if (file_exists($CD_DIR.":\\".$_DBTAG."\\".$row[$DB_HTTP_PAGE_HTML])){

				//Host address
				$WebmailrList .="<td><span title=\"".$row[$DB_HTTP_PAGE_HOST]."\"><a href=\"../download.php?file=".$CD_DIR.":\\".$_DBTAG."/".$row[$DB_HTTP_PAGE_HTML]."\" target=\"_blank\" class=\"A_U\">".$CUT_HOST."</a></span></td>";
			}
			else{
				$WebmailrList = $WebmailrList."<td><p title=\"".$row[$DB_HTTP_PAGE_HOST]."\">".$CUT_HOST."</p></td>";
			}

			if (fn_ad()){
				$WebmailrList = $WebmailrList."<td align=\"center\"><a href=\"#\" onClick=\"ChangeCondition('_ACCOUNT','".$row[$DB_HTTP_PAGE_ACCOUNT]."')\">".$row[$DB_HTTP_PAGE_ACCOUNT]."</a></td>\n";
            }
			
			//Type
			$WebmailrList = $WebmailrList."<td align=\"center\"><a href=\"#\" onClick=\"ChangeCondition('_TAG','".$row[$DB_HTTP_PAGE_TAG]."');\">".$row[$DB_HTTP_PAGE_TAG]."</a></td>";
			$WebmailrList = $WebmailrList."</tr>";
			$chkCount++;
		}//End : while(組show畫面字串)
	}
}
else{
	include("../no_privilege.php");
}
?>
<html><head><title></title>
<link href="../Display/display.css" type="text/css" rel="stylesheet">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<SCRIPT language=JavaScript src="../inc/fun.js"></SCRIPT>
<SCRIPT LANGUAGE="JavaScript">

//--Check input pagesize num	
function ChangePageSize(){
	var flag = "";
	switch(CheckNum(document.frm.sizepage)){
		case "1":
			alert("<?echo $NUMBERM_INPUTISNULL;?>");
			flag = "1";
		break;
		case "2":
			alert("<?echo $NUMBERM_INPUTISZERO;?>");
			flag = "1";
		break;
		case "3":
			alert("<?echo $NUMBERM_INPUTBIG100;?>");
			flag = "1";
		break;
		case "4":
			alert("<?echo $NUMBERM_INPUTNOTNUM;?>");
			flag = "1";
		break;
	}	
	if (flag == 1){
		document.frm.sizepage.focus();
		return;
	}
	document.frm.RunAs.value = "UpdatePageSize";
	document.frm.submit();
}


//--DELETE DATA
function checkDEL(){
<?if ($_Disabled == ""){?>
	if (!MainCheck()){
		alert("<?echo $WEBMAILRM_NOTSELECT;?>");
		return;
	}
	if (confirm("<?echo $WEBMAILRM_CHECKDEL;?>")){
		document.frm.RunAs.value = "Delete";
		document.frm.submit();
	}
<?}?>
}

<?if ($ErrorMsg != ""){?>
	fn_ErrorMsg('<?echo $ErrorMsg?>')
<?}?>

//Expand
function fn_expand(){
	<?if ($_Disabled == ""){?>
		if (!MainCheck()){
			alert("<?echo $WEBMAILRM_NOTSELECT;?>");
			return;
		}
		var str_id = '';
		var flag = 0;
		for(var i=0;i<document.frm.elements['chkbox[]'].length;i++){
			if (document.frm.elements['chkbox[]'][i].checked){
				str_id += document.frm.elements['chkbox[]'][i].value+'/';
				flag = 1;
			}
		}
		if (flag == 1)			window.open('expand.php?str_id='+str_id,'expand','width=900,height=650,left=50,top=25,menubar=no,toolbar=no,location=no,directories=no,scrollbars=yes,status=no,resizable=yes,fullscreen=no');
	<?}?>
}

</SCRIPT>
</head>
<body onLoad="fix();" onScroll="fix();" onResize="fix();">
<!-- search result-->
<form name="frm" action="webmailr.php" method="post">
<input type="hidden" name="strWhere[WEBMAILR]" value="<?echo stripcslashes($strWhere["WEBMAILR"]);?>">
<input type="hidden" name="RunAs" value="">
<input type="hidden" name="SortField" value="<? echo $SortField ?>"><!--sort column-->
<input type="hidden" name="SortType" value="<? echo  $SortType ?>"><!--sort type-->
<input type="hidden" name="Checked" value="<? echo $Checked?>"><!--set checkbox checked or no-->
<input type="hidden" name="QUERY_START_DATETIME" value="<?echo $QUERY_START_DATETIME;?>">
<input type="hidden" name="QUERY_END_DATETIME" value="<?echo $QUERY_END_DATETIME;?>">
<input type="hidden" name="QUERY_URL" value="<?echo ($QUERY_URL);?>">
<input type="hidden" name="QUERY_TAG" value="<?echo $QUERY_TAG;?>">
<input type="hidden" name="QUERY_NAME" value="<?echo $QUERY_NAME;?>">
<input type="hidden" name="QUERY_IP" value="<?echo $QUERY_IP;?>">
<input type="hidden" name="QUERY_IP_1" value="<?echo $QUERY_IP_1;?>">
<input type="hidden" name="QUERY_ACCOUNT" value="<?echo $QUERY_ACCOUNT;?>">
<input type="hidden" name="to_page" value="<?echo $to_page;?>"><!--current page-->
<input type="hidden" name="mime" value="<?echo $mime;?>">
<input type="hidden" name="WEBMAILR" value="<?echo $WEBMAILR;?>">
<input type="hidden" name="digital" value="<?echo $digital;?>">

<span id="bar" style="position:absolute;z-index:2;  width:100%; height:100%" align="center">
<table width="100%" cellSpacing="0" cellPadding="0" border="0">
  <tr id="Tr_F">
    <td>
	  &nbsp;&nbsp;<a href="webmailr.php"  title="<?echo $WEBMAILR_WEBMAILR;?>"><img src="../<?echo $DIR_IMAGE.$WEBMAILRP_WEBMAILR;?>" border="0" align="top"> <?echo $WEBMAILR_WEBMAILR_SHOW;?></a> | 
	  <a href="javascript:fn_OpenWin('<?echo $_SearchPath;?>?Status=webmailr&digital=<?echo $digital;?>','NewWin','width=420,height=300,left=320,top=200,menubar=no,toolbar=no,location=no,directories=no,scrollbars=yes,status=no,resizable=yes,fullscreen=no');"  title="<?echo $WEBMAILR_SEARCH;?>">
	  <img src="../<?echo $DIR_IMAGE.$WEBMAILRP_SEARCH;?>" align="top" border="0"> <?echo $WEBMAILR_SEARCH_SHOW;?></a>
	</td>
	  <td align="right" valign="middle">
	  <span class="ShowMode" title="<?echo $WEBMAILR_SHOWMODE;?>"><?echo $WEBMAILR_SHOWMODE_SHOW;?> </span>
	  <input type="radio" name="Ip_Name" value="_IP" <? echo $chk_IP;?> onclick="document.frm.submit();">
	  <span title="<?echo $WEBMAILR_IP;?>"><?echo $WEBMAILR_IP_SHOW;?></span>
	  <input type="radio" name="Ip_Name" value="_NAME" <?echo $chk_NAME;?> onclick="document.frm.submit();">
	  <span title="<?echo $WEBMAILR_NAME;?>"><?echo $WEBMAILR_NAME_SHOW;?></span> | 
	  <span class="EveryPage" title="<?echo $WEBMAILR_PAGEVIEW;?>"><?echo $WEBMAILR_PAGEVIEW_SHOW;?> </span><input type="text" size="1" name="sizepage" value="<?echo $sizepage?>"  class="Text_P"> <input type="button" name="bntPageSize" value="Confirm" onClick="ChangePageSize();" class="Btn_C" title="<?echo $WEBMAILRB_OK;?>">&nbsp;</span>
	</td>
  </tr>
</table>
<!--  blank TABLE-->
<table width="100%" class="BlankTb">
	<tr  height="24">
		<td ></td>
	</tr>
</table>
<table class="TitleTb">
  <tr id="Tr_T">
    <td width="3%"><span title="<?echo $WEBMAILR_NO;?>"><?echo $WEBMAILR_NO_SHOW;?></span></td>

    <td width="15%" <?echo $colDate;?>>
	  <a href="javascript:ChangeSort('_DATETIME','<? echo $sortDate;?>');"  title="<?echo $WEBMAILR_DATETIME?>"><?echo $WEBMAILR_DATETIME_SHOW?><?echo $imgDate;?></a>
	</td>

	<td width="20%" 
	  <? if ($Ip_Name == '_IP'){?>
	    <?echo $colIP;?>>
		<a href="javascript:ChangeSort('_IP','<?echo $sortIP;?>');"  title="<?echo $WEBMAILR_IP;?>"><?echo $WEBMAILR_IP_SHOW;?><? echo $imgIP;?></a>
	  <?}elseif ($Ip_Name == '_NAME'){
		echo $colNAME.">"."<span title=\"".$WEBMAILR_NAME."\">Name</span>";
	  }?>
	</td>

    <td><span title="<?echo $WEBMAILR_URL;?>"><?echo $WEBMAILR_URL_SHOW;?></span></td>
    <? if (fn_ad()){?>
		<td width="8%" <?echo $colAccount;?>>
			<a href="javascript:ChangeSort('_ACCOUNT', '<?echo $sortAccount;?>');"  title="<?echo $WEBMAILR_ACCOUNT;?>"><?echo $WEBMAILR_ACCOUNT_SHOW;?><?echo $imgAccount;?>
		</td>		
    <?}?>
    <td width="8%" align="center" <?echo $colTag;?>>
	  <a href="javascript:ChangeSort('_TAG','<? echo $sortTag;?>');"  title="<?echo $WEBMAILR_WEBID;?>"><?echo $WEBMAILR_WEBID_SHOW;?><? echo $imgTag;?></a>
	</td>
  </tr>
  </table>
</span>

<table width="100%" cellSpacing="0" cellPadding="0" border="0" >
<tr  id="Tr_F"><td height="32"></td><td align="right" valign="middle"></td></tr>
</table>

<table class="DataTb" >
  <tr class="Tr_Do">
    <td width="3%"></td>

    <td width="15%"></td>
	<td width="20%"></td><td></td>
    <? if (fn_ad()){?>
		<td width="8%" <?echo $colAccount;?>></td>		
    <?}?>
    <td width="8%" align="center"></td>
  </tr>
 <!--show screen-->
  <? echo $WebmailrList;?>
  <tr>
    <td colSpan="
	<?
		if (($HTTP_SESSION_VARS["OperateRole_Sess"]=="Super")&&($HTTP_SESSION_VARS["GroupNo_Sess"]=='0')){
			if(fn_ad()) echo "7"; else echo "6";
		}else{
			if(fn_ad()) echo "6"; else echo "5";
		}
	?>
	" align="center">
	  <table width="100%"  border="0" cellspacing="0" cellpadding="0">
        <tr id="Tr_P"><? include("../list_page.php");?></tr>
      </table>
	</td>
 </tr>
</table>
</form>
</body>	
</html>