<?
session_start();
$selelag = $HTTP_SESSION_VARS["SelLanguage"];
if (empty($selelag)) header("location:../break.php");
require("../config/".$selelag);
require("../_search/_http_search.php");
require("../_search/_datamining.php");
require("../_ConnectString.php");                                         
require("../_function.php");
require("../_read_source/_httplog.php");
require("../_read_source/_httplog_detail.php");
require("../_read_source/_ipcontrol.php");
require("../_image.php");

	function conver2url ($str){
		$res = "";
		$tmp = "";
		$flag = 0;
		for($x=0; $x<strlen($str); $x++){
			if(ord($str[$x]) >= 128 || $flag%2){
				$flag++;
				$tmp .= $str[$x];
			}else{
				if($flag > 0){
					$res .= urlencode($tmp);
					$tmp = "";
					$flag = 0;
				}
				$res .= $str[$x];
			}
		}
		return $res;
	}


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){
		$QUERY_IP			= $HTTP_GET_VARS["QUERY_IP"];     //--HOST IP
	}else{
		$QUERY_IP			= $HTTP_POST_VARS["QUERY_IP"];     //--HOST IP
	}
	$QUERY_HOST				  = $HTTP_POST_VARS["QUERY_HOST"];   //--CHECKBOX 使用
	$QUERY_ID 			      = $HTTP_POST_VARS["QUERY_ID"];   //--CHECKBOX 使用
    $QUERY_IP_1			      = $HTTP_POST_VARS["QUERY_IP_1"];   
	$QUERY_ACCOUNT            = $HTTP_POST_VARS["QUERY_ACCOUNT"];
    $QUERY_START_DATETIME     = $HTTP_POST_VARS["QUERY_START_DATETIME"];
    $QUERY_END_DATETIME       = $HTTP_POST_VARS["QUERY_END_DATETIME"];

	$sizepage                 = $HTTP_POST_VARS["sizepage"];       //--每頁筆數
    $Ip_Name				  = $HTTP_POST_VARS["Ip_Name"];    //--顥示的模式
	$Checked				  = $HTTP_POST_VARS["Checked"];   //--CHECKBOX 使用
	$RunAs					  = $HTTP_POST_VARS["RunAs"];
	$SortField				  = $HTTP_POST_VARS["SortField"];      //--排序欄位
	$SortType				  = $HTTP_POST_VARS["SortType"];       //--排序方式
	$strWhere	              = $HTTP_POST_VARS["strWhere"];		//--search all
	$HTTP					  = $HTTP_POST_VARS['HTTP'];		//--全文檢索 -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;
	}

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

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

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

    //--更改每頁的筆數設定 
	if ($RunAs == 'UpdatePageSize'){
		if ($sizepage){ 
			$HTTP_SESSION_VARS["size_page"] = $sizepage;
		}
	}

	//檢查並刪除需刪除的資料 
	if($RunAs == "Delete"){
		$flag = 0;
		for($i=0; $i<count($chkbox); $i++){

			//delete HTTPLOG_DETAIL
			$strSQL="Select ".$DB_HTTPLOG_STRING." From ".$DB_HTTPLOG." where  ".$DB_HTTPLOG_AUTO."='".$chkbox[$i]."'";
			$intResult = mysql_query($strSQL);
			$row = mysql_fetch_array($intResult);

			$strSQL ="Delete from ".$DB_HTTPLOG_DETAIL." where ".$DB_HTTPLOG_DETAIL_AUTO." = '".$row[$DB_HTTPLOG_STRING]."'";
			$intSQL = mysql_query($strSQL);
			if (!$intSQL) $flag = 1;

			//delete HTTPLOG
			$strSQL = "Delete from ".$DB_HTTPLOG." where ".$DB_HTTPLOG_AUTO."='".$chkbox[$i]."'";
			$intSQL = mysql_query($strSQL);
			if (!$intSQL) $flag = 1;
		}

		if ($flag){
			$ErrorMsg = $HTTPM_DELFAIL;
		}
		else{
			mysql_query("OPTIMIZE TABLE HTTPLOG");
			$ErrorMsg = $HTTPM_DELSUCCESS;
		}
	}
	
	//--判斷點選那一個顯示模式 
	switch ($Ip_Name){
		case "_IP":
			$HTTP_IP_NAME = $HTTP_IP;
			$HTTP_IP_NAME_1 = "IP";
			$chk_IP = "checked";
			break;
		case "_USERNAME":
			$HTTP_IP_NAME = $HTTP_NAME;
			$HTTP_IP_NAME_1 = "NAME";
			$chk_USERNAME = "checked";
			break;
	}

	//-- System super user(PROGRAM ENTRY PLACE)
	$strSQL = "Select count(*) From ".$DB_HTTPLOG;
	if ($mime == 1)
	{
		$strSQL = $strSQL." Where ".$DB_HTTPLOG_AUTO." in (".$HTTP.")";
	}
	else{
		$strSQL = $strSQL." Where ".$DB_HTTPLOG_IP." not in (".fn_NotShowIP().") and ".$DB_HTTPLOG_MAC." not in (".fn_NotShowMAC().") ";
		if ($HTTP_SESSION_VARS['GroupNo_Sess']!=0){
			$strSQL = $strSQL." and ".$DB_HTTPLOG_IP." in (".fn_IPcontrol($HTTP_SESSION_VARS['GroupNo_Sess']).")";
		}
		if($strWhere["HTTP"] != "") $strSQL = $strSQL.stripcslashes($strWhere["HTTP"]);
	}


	$condition = search($QUERY_START_DATETIME,$QUERY_END_DATETIME,$QUERY_IP,$QUERY_IP_1,$QUERY_HOST,$QUERY_ID,$QUERY_ACCOUNT,'http');
	$strSQL = $strSQL.$condition;

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

   //為了畫面排序的sql語法
   $strSQL = "Select ".$DB_HTTPLOG_AUTO.",".$DB_HTTPLOG_DATETIME.",".$DB_HTTPLOG_IP.",".$DB_HTTPLOG_MAC.",".$DB_HTTPLOG_HOST.",".$DB_HTTPLOG_STRING.",".$DB_HTTPLOG_ACCOUNT;
   $strSQL = $strSQL." From ".$DB_HTTPLOG;
   if ($mime == 1)
	{
		$strSQL = $strSQL.fn_Datamining($HTTP,$DB_HTTPLOG_AUTO,$m,$n,$to_page);
		$m = 0;
	}
	else{
	   $strSQL = $strSQL." Where ".$DB_HTTPLOG_IP." not in (".fn_NotShowIP().") and ".$DB_HTTPLOG_MAC." not in (".fn_NotShowMAC().") ";
	   if ($HTTP_SESSION_VARS['GroupNo_Sess']!=0){
			$strSQL = $strSQL." and ".$DB_HTTPLOG_IP." in (".fn_IPcontrol($HTTP_SESSION_VARS['GroupNo_Sess']).")";
	   }
	   if($strWhere["HTTP"] != "") $strSQL = $strSQL.stripcslashes($strWhere["HTTP"]);
	}

   $strSQL = $strSQL.$condition;

   $strSQL = $strSQL." Order by ".$SortField." ".$SortType." limit ".$m.", ".$n;

   $intResult = mysql_query($strSQL);
	//print $strSQL;
	//<--組show畫面字串
	if (mysql_num_rows($intResult) == 0){
		$Listhttp = "<tr class=\"NoData\"><td colspan=\"";
		if (($HTTP_SESSION_VARS["OperateRole_Sess"]=="Super")&&($HTTP_SESSION_VARS["GroupNo_Sess"]=='0')){
			if(fn_ad()) $Listhttp = $Listhttp."6"; else $Listhttp = $Listhttp."5";
		}else{
			if(fn_ad()) $Listhttp = $Listhttp."5"; else $Listhttp = $Listhttp."4";
		}
		include("../no_data.php");

		$Listhttp = $Listhttp."\"".$NoDataList;
		$_Disabled = "disabled";
	}
	else{	
		$Listhttp = "";
		$chkCount = ($to_page-1)*$sizepage+1;	  
		while ($row=mysql_fetch_array($intResult)){

			if ($chkCount%2 == 1){
					$Listhttp = $Listhttp."<tr  align=\"center\" class=\"Tr_Do\" onmouseover=\"this.className='Tr_Do_Out'\" onmouseout=\"this.className='Tr_Do_Over'\">";
				}else{
					$Listhttp = $Listhttp."<tr align=\"center\" class=\"Tr_De\" onmouseover=\"this.className='Tr_De_Out'\" onmouseout=\"this.className='Tr_De_Over'\">";
				}
				
			$Listhttp = $Listhttp."<td title=\"".$row[_AUTO]."\">".$chkCount.".</td>\n";

			//--datetime 
			$Listhttp = $Listhttp."<td><a href=\"#\" onClick=\"ChangeCondition('".$DB_HTTPLOG_DATETIME."','".$row[$DB_HTTPLOG_DATETIME]."');\">".fn_DateTime($row[$DB_HTTPLOG_DATETIME])."</a></td>\n";

			//--IP
			switch ($Ip_Name){
				case "_IP":
					$Listhttp = $Listhttp."<td><a href=\"#\" onClick=\"ChangeCondition('".$DB_HTTPLOG_IP."','".$row[$DB_HTTPLOG_IP]."');\"><p title=\"".$row[$DB_HTTPLOG_MAC]."\">".$row[$DB_HTTPLOG_IP]."</p></a></td>";
					break;
				case "_USERNAME":
					$SQL = "Select ".$DB_IPCONTROL_USERNAME." From ".$DB_IPCONTROL." Where ".$DB_IPCONTROL_IP." = '".$row[$DB_HTTPLOG_IP]."'";
					$Result = mysql_query($SQL);
					if (mysql_num_rows($Result)>0){
						$row_sub = mysql_fetch_array($Result);
						$Listhttp = $Listhttp."<td><p title=\"".$row[$DB_HTTPLOG_MAC]."\"><a href=\"#\" onClick=\"ChangeCondition('".$DB_HTTPLOG_IP."','".$row[$DB_HTTPLOG_IP]."');\">";
						
						if ($row_sub[$DB_IPCONTROL_USERNAME] == '***') $Listhttp = $Listhttp.$row[$DB_HTTPLOG_IP]; else $Listhttp = $Listhttp.$row_sub[$DB_IPCONTROL_USERNAME];
						
						$Listhttp = $Listhttp."</a></p></td>";
					}
					else{
						$Listhttp = $Listhttp."<td><p title=\"".$row[$DB_HTTPLOG_MAC]."\"><a href=\"#\" onClick=\"ChangeCondition('".$DB_HTTPLOG_IP."','".$row[$DB_HTTPLOG_IP]."');\">".$row[$DB_HTTPLOG_IP]."</a></p></td>";
					}
					break;
			}
			
			$strSQL_s = "Select ".$DB_HTTPLOG_DETAIL_STRING." From ".$DB_HTTPLOG_DETAIL." WHERE ".$DB_HTTPLOG_DETAIL_AUTO." = '".$row[$DB_HTTPLOG_STRING]."'";
			$result_s = mysql_query($strSQL_s);

			//URL
			if (mysql_num_rows($result_s)>0){
				$row_s = mysql_fetch_array($result_s);
				$Listhttp = $Listhttp."<td align=\"left\" ><a href=\"".conver2url($row_s[$DB_HTTPLOG_DETAIL_STRING])."\" target=\"_blank\" class=\"A_U\">".$row[$DB_HTTPLOG_HOST]."</a></td>";	
			}
			else{
				$Listhttp = $Listhttp."<td align=\"left\">".$row[$DB_HTTPLOG_HOST]."</td>";
			}

			if (fn_ad()){
				$Listhttp = $Listhttp."<td align=\"center\"><a href=\"#\" onClick=\"ChangeCondition('_ACCOUNT','".$row[$DB_HTTPLOG_ACCOUNT]."')\">".$row[$DB_HTTPLOG_ACCOUNT]."</a></td>\n";
            }

			$Listhttp = $Listhttp."</tr>";
			$chkCount++;
		}
	}
}
else{
	include("../no_privilege.php");
}	
?>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="../Display/display.css" type="text/css" rel="stylesheet">
<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 $HTTPM_NOTSELECT;?>");
		return;
	}

	if (confirm("<?echo $HTTPM_CHECKDEL;?>")){
		document.frm.RunAs.value = "Delete";
		document.frm.submit();
	}
<?}?>
}

<?if ($ErrorMsg != ""){?>
	fn_ErrorMsg('<?echo $ErrorMsg?>')
<?}?>
</SCRIPT>
</head>
<body onLoad="fix();" onScroll="fix();" onResize="fix();">  
<body>
<form name="frm" action="http.php" method="post">
<input type="hidden" name="strWhere[HTTP]" value="<?echo stripcslashes($strWhere["HTTP"]);?>">
<input type="hidden" name="RunAs" value="">
<input type="hidden" name="to_page" value="<?echo $to_page;?>"><!--current page-->
<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_IP" value="<?echo $QUERY_IP;?>">
<input type="hidden" name="QUERY_IP_1" value="<?echo $QUERY_IP_1;?>">
<input type="hidden" name="QUERY_HOST" value="<?echo $QUERY_HOST;?>">
<input type="hidden" name="QUERY_ACCOUNT" value="<?echo $QUERY_ACCOUNT;?>">
<input type="hidden" name="mime" value="<?echo $mime;?>">
<input type="hidden" name="HTTP" value="<?echo $HTTP;?>">
<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;<img src="../<?echo $DIR_IMAGE.$HTTPP_HTTP?>">
	  <a href="http.php"  title="<?echo $HTTP_HTTP?>"><?echo $HTTP_HTTP_SHOW?></a> | 

	  <img src="../<?echo $DIR_IMAGE.$HTTPP_SEARCH?>">&nbsp;<a href="javascript:fn_OpenWin('../_search/http_search_list.php?Status=http&digital=<?echo $digital;?>','NewWin','width=400,height=250,left=320,top=250,menubar=no,toolbar=no,location=no,directories=no,scrollbars=yes,status=no,resizable=yes,fullscreen=no');"  title="<?echo $HTTP_SEARCH?>"><?echo $HTTP_SEARCH_SHOW?></a>
	</td>
	<td align="right" valign="middle">
	  <span class="ShowMode" title="<?echo $HTTP_SHOWMODE?>"><?echo $HTTP_SHOWMODE_SHOW?> : </span>
	  <input type="radio" name="Ip_Name" value="_IP" <? echo $chk_IP;?> onclick="document.frm.submit();"><span title="<?echo $HTTP_IP?>"><?echo $HTTP_IP_SHOW?></span>	  
	  <input type="radio" name="Ip_Name" value="_USERNAME" <?echo $chk_USERNAME;?> onclick="document.frm.submit();">
	  <span title="<?echo $HTTP_NAME?>"><?echo $HTTP_NAME_SHOW?> |</span> 
	  <span class="EveryPage" title="<?echo $HTTP_PAGEVIEW;?>"><?echo $HTTP_PAGEVIEW_SHOW;?> :</span> 
	  <input type="text" size="1" name="sizepage" value="<?echo $sizepage?>"class="Text_P"> 
	  <input type="button" name="bntPageSize" value="<?echo $HTTPB_OK_SHOW;?>" onClick="ChangePageSize();" class="Btn_C" title="<?echo $HTTPB_OK;?>">&nbsp;
	</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="5%" title="<?echo $HTTP_NO?>"><?echo $HTTP_NO_SHOW?></td>
	<td width="15%" <?echo $colDate?>>
		<a href="javascript:ChangeSort('_DATETIME','<?echo $sortDate;?>');"  title="<?echo $HTTP_DATETIME?>"><?echo $HTTP_DATETIME_SHOW?><?echo $imgDate;?></a>
	</td>
	<td width="15%" <?echo $colIP?>>
		<a href="javascript:ChangeSort('_IP','<?echo $sortIP;?>');"  title="<?echo $HTTP_IP_NAME?>"><?echo $HTTP_IP_NAME_1?><?echo $imgIP;?></a>
	</td>
	<td width="50%" <?echo $colHost?>>
		<a href="javascript:ChangeSort('_HOST','<?echo $sortHost;?>');"  title="<?echo $HTTP_URL?>"><?echo $HTTP_URL_SHOW?><?echo $imgHost;?></a>
	</td>
	<? if (fn_ad()){?>
		<td width="8%" <?echo $colAccount;?>>
			<a href="javascript:ChangeSort('_ACCOUNT', '<?echo $sortAccount;?>');"  title="<?echo $HTTP_ACCOUNT;?>"><?echo $HTTP_ACCOUNT_SHOW;?><?echo $imgAccount;?>
		</td>	
	<?}?>
  </tr>
  </table>
</span>

<table width="100%"  border="0" cellspacing="0" cellpadding="0">
  <tr  id="Tr_F" ><td height="24" valign="middle"></td></tr>
</table>

<table class="DataTb" >
  <tr class="Tr_Do">
	<td width="5%"></td>
	<td width="15%"></td>
	<td width="15%"></td>
	<td width="50%"></td>
	<? if (fn_ad()){?>
		<td width="8%"></td>	
	<?}?>
  </tr>
  <?echo $Listhttp;?>
  <tr>
	<td colSpan="
	<?
		if (($HTTP_SESSION_VARS["OperateRole_Sess"]=="Super")&&($HTTP_SESSION_VARS["GroupNo_Sess"]=='0')){
			if(fn_ad()) echo "6"; else echo "5";
		}else{
			if(fn_ad()) echo "5"; else echo "4";
		}
	?>
	" 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>