<?
session_start();
$selelag = $HTTP_SESSION_VARS["SelLanguage"];
if (empty($selelag)) header("location:../break.php");
require("../config/".$selelag);
require("../_search/_chat_search.php");
require("../_search/_datamining.php");
require("../_ConnectString.php");                                         //--連接參數設定值 
require("../_function.php");
require("../_read_source/_icq.php");
require("../_read_source/_icq_msg.php");
require("../_read_source/_ipcontrol.php");
require("../_image.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){
		$QUERY_IP			= $HTTP_GET_VARS["QUERY_IP"];     //--HOST IP
	}else{
		$QUERY_IP			= $HTTP_POST_VARS["QUERY_IP"];     //--HOST IP
	}
	//--From search screen
	$QUERY_START_DATETIME		 = $HTTP_POST_VARS["QUERY_START_DATETIME"];
	$QUERY_END_DATETIME			 = $HTTP_POST_VARS["QUERY_END_DATETIME"];
	$QUERY_OWNER				 = $HTTP_POST_VARS["QUERY_OWNER"];  
	$QUERY_WHOM					 = $HTTP_POST_VARS["QUERY_WHOM"];
	$QUERY_ID			         = $HTTP_POST_VARS["QUERY_ID"];
	$QUERY_IP_1			         = $HTTP_POST_VARS["QUERY_IP_1"];
	$QUERY_ACCOUNT			     = $HTTP_POST_VARS["QUERY_ACCOUNT"];
 	$QUERY_CONTENT			     = $HTTP_POST_VARS["QUERY_CONTENT"];

	$sizepage                    = $HTTP_POST_VARS["sizepage"];       //--每頁筆數
	$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
	$ICQ						 = $HTTP_POST_VARS['ICQ'];		//--全文檢索 -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 ($SortField == "") $SortField=$DB_ICQ_AUTO;
	include($_SORT_PATH);

	//--判斷點選那一個顯示模式 
	switch ($Ip_Name){
		case "_IP":
			$ICQ_IP_NAME = $ICQ_IP;
			$ICQ_IP_NAME_1 = "IP";
			$chk_IP = "checked";
			break;
		case "_USERNAME":
			$ICQ_IP_NAME = $ICQ_NAME;
			$ICQ_IP_NAME_1 = "NAME";
			$chk_USERNAME = "checked";
			break;
	}

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

	//檢查並刪除需刪除的資料，
	if($RunAs == "Delete"){
		$_FILE = "ICQ_".date("Y-m-d")."_".date("H:i:s");
		$TMP_FILE = "/tmp/".$_FILE;
		if ((fn_digital_asset()==1)||(fn_data_mining()==1)) $fp = fopen($TMP_FILE,"w+");
		$flag = 0;
		for($i=0; $i<count($chkbox); $i++){
			$strSQL = "Delete from ".$DB_ICQ." where ".$DB_ICQ_AUTO."='".$chkbox[$i]."'";
			$intSQL = mysql_query($strSQL);
			if ((fn_digital_asset()==1)||(fn_data_mining()==1)) fputs($fp,"ICQ.".$chkbox[$i]."\n");
			if (!$intSQL) $flag = 1;

			//刪除傳檔
			$strSQL="Select * From ".$DB_ICQ_MSG." where ".$DB_ICQ_MSG_IDX."='".$chkbox[$i]."'";
			$intSQL = mysql_query($strSQL);
			while($row = mysql_fetch_array($intSQL)){							 
				$DelFilePath=	$DATAS_PATH_ICQ.$row[$DB_ICQ_MSG_DBTAG]."/".$row[$DB_ICQ_MSG_DB_NAME];
				if (file_exists($DelFilePath)&&($row[$DB_ICQ_MSG_DB_NAME]!="")){
						system("echo rm -f $DelFilePath >> /tmp/dc.cmd");
				}
			}

			$strSQL = "Delete from ".$DB_ICQ_MSG." where ".$DB_ICQ_MSG_IDX."='".$chkbox[$i]."'";
			$intSQL = mysql_query($strSQL);
			if (!$intSQL) $flag = 1;
		}
		if ((fn_digital_asset()==1)||(fn_data_mining()==1)) fclose($fp);

		if ($flag){
			$ErrorMsg = $ICQM_DELFAIL;
			if ((fn_digital_asset()==1)||(fn_data_mining()==1)) unlink($TMP_FILE);
		}
		else{
			mysql_query("OPTIMIZE TABLE ICQ");
			mysql_query("OPTIMIZE TABLE ICQ_MSG");
			$ErrorMsg = $ICQM_DELSUCCESS;
			if ((fn_digital_asset()==1)||(fn_data_mining()==1)){
				copy($TMP_FILE,$_DELETE_DIGITAL_PATH.$_FILE);
				unlink($TMP_FILE);
			}
		}
	}
    		
	//-- System super user(PROGRAM ENTRY PLACE)
	$strSQL = "Select distinct ".$DB_ICQ_MSG_IDX." From ".$DB_ICQ_MSG;
    $intResult = mysql_query($strSQL);
	$num_row = mysql_num_rows($intResult);
	$index = 0;

	while($row=mysql_fetch_array($intResult)){
	   if ($index == ($num_row-1)){ 
		   $str_IDX = "'".$row[$DB_ICQ_MSG_IDX]."'";
	   }
	   else{
		   $str_IDX = "'".$row[$DB_ICQ_MSG_IDX]."',";
	   }	 
	   $in_str_IDX=$in_str_IDX.$str_IDX;
	   $index++;
	}
    	
	if ($in_str_IDX=="") $in_str_IDX = "''";
  
	$strSQL = "Select count(*) From ".$DB_ICQ;
	if ($mime == 1)
	{
		$strSQL = $strSQL." Where ".$DB_ICQ_AUTO." in (".$ICQ.")";
	}
	else{
		$strSQL = $strSQL." where ".$DB_ICQ_IP." not in (".fn_NotShowIP().") and ".$DB_ICQ_AUTO." in (".$in_str_IDX.")";
		if ($HTTP_SESSION_VARS['GroupNo_Sess']!=0){
			$strSQL = $strSQL." and ".$DB_ICQ_IP." in (".fn_IPcontrol($HTTP_SESSION_VARS['GroupNo_Sess']).")";
		}
		if($strWhere["ICQ"] != "") $strSQL = $strSQL.stripcslashes($strWhere["ICQ"]);
	}
	$condition = search($QUERY_START_DATETIME,$QUERY_END_DATETIME,$QUERY_IP,$QUERY_IP_1,$QUERY_OWNER,$QUERY_WHOM,$QUERY_ID,$QUERY_ACCOUNT,$QUERY_CONTENT,'icq');
	
	$strSQL = $strSQL.$condition;
    //計算符合文章總數 & 初始化分頁變數
    include($_PAGE_PATH);
   
    //為了畫面排序的sql語法
    $strSQL = "Select ".$DB_ICQ_AUTO.",".$DB_ICQ_DATE.",".$DB_ICQ_IP.",".$DB_ICQ_MAC.",".$DB_ICQ_OWNER.",".$DB_ICQ_WHOM.",".$DB_ICQ_ACCOUNT;
    $strSQL = $strSQL." From ".$DB_ICQ;
	if ($mime == 1)
	{
		$strSQL = $strSQL.fn_Datamining($ICQ,$DB_ICQ_AUTO,$m,$n,$to_page);
		$m = 0;
	}
	else{
		$strSQL = $strSQL." where ".$DB_ICQ_IP." not in (".fn_NotShowIP().") and ".$DB_ICQ_AUTO." in (".$in_str_IDX.")";

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

	$strSQL = $strSQL." Order by ".$SortField." ".$SortType." limit ".$m.", ".$n;
    $intResult = mysql_query($strSQL);
	
	//<--組show畫面字串
	if (mysql_num_rows($intResult) == 0){
		$ICQList = "<tr height=\"30\" class=\"NoData\"><td colspan=\"";
		if (($HTTP_SESSION_VARS["OperateRole_Sess"]=="Super")&&($HTTP_SESSION_VARS["GroupNo_Sess"]=='0')){
			if(fn_ad()) $ICQList = $ICQList."10"; else $ICQList = $ICQList."9";
		}else{
			if(fn_ad()) $ICQList = $ICQList."9"; else $ICQList = $ICQList."8";
		}
		include("../no_data.php");
		$ICQList = $ICQList."\"".$NoDataList;
		$_Disabled = "disabled";
	}
	else{	
		$ICQList = "";
		$chkCount = ($to_page-1)*$sizepage+1; 
		while ($row=mysql_fetch_array($intResult)){
            if ($chk_count !=''){
                $strSQL1 = "Select count(*) from ".$DB_ICQ_MSG." where ".$DB_ICQ_MSG_IDX." = ".$row[$DB_ICQ_AUTO];
                $intResult1 = mysql_fetch_array(mysql_query($strSQL1));
                $count = $intResult1[0];
         }

			if($chk_attachment !=''){
                $strSQL1 = "Select count(".$DB_ICQ_MSG_FILENAME.") from ".$DB_ICQ_MSG." where ".$DB_ICQ_MSG_IDX." = ".$row[$DB_ICQ_AUTO]." and ".$DB_ICQ_MSG_FILENAME."<>''";
                $intResult1 = mysql_fetch_array(mysql_query($strSQL1));
                $count_file = $intResult1[0];
            }
			
			if ($chkCount%2 == 1){
				$ICQList = $ICQList."<tr align=\"center\" class=\"Tr_Do\" onmouseover=\"this.className='Tr_Do_Out'\" onmouseout=\"this.className='Tr_Do_Over'\">";
			}else{
				$ICQList = $ICQList."<tr align=\"center\" class=\"Tr_De\" onmouseover=\"this.className='Tr_De_Out'\" onmouseout=\"this.className='Tr_De_Over'\">";
			}
			
			$ICQList = $ICQList."<td title=\"".$row[_AUTO]."\">".$chkCount++.".</td>\n";

			//--file yes or no
			$ICQList = $ICQList."<td>";
			if($count_file>0) $ICQList = $ICQList."<img src=\"../".$DIR_IMAGE.$ICQP_ATTACH."\">"; else $ICQList = $ICQList."&nbsp;";
			$ICQList = $ICQList."</td>";

			//--datetime 
			$ICQList = $ICQList."<td><a href=\"#\" onClick=\"ChangeCondition('".$DB_ICQ_DATE."','".$row[$DB_ICQ_DATE]."');\">".fn_DateTime($row[$DB_ICQ_DATE])."</a></td>\n";
			
			//--IP
			$ICQList = $ICQList."<td><p title=\"".$row[$DB_ICQ_MAC]."\"><a href=\"#\" onClick=\"ChangeCondition('".$DB_ICQ_IP."','".$row[$DB_ICQ_IP]."');\">".$row[$DB_ICQ_IP]."</a></p></td>\n";
		
			
		//--owner
		$ICQList = $ICQList."<td><a href=\"#\" onClick=\"ChangeCondition('".$DB_ICQ_OWNER."','".$row[$DB_ICQ_OWNER]."');\">".$row[$DB_ICQ_OWNER]."</a></td>\n";
		
			
			//--whom
			$ICQList = $ICQList."<td><a href=\"#\" onClick=\"ChangeCondition('".$DB_ICQ_WHOM."','".$row[$DB_ICQ_WHOM]."');\">".$row[$DB_ICQ_WHOM]."</a></td>\n";
			
			if (fn_ad()){
				$ICQList = $ICQList."<td align=\"center\"><a href=\"#\" onClick=\"ChangeCondition('_ACCOUNT','".$row[$DB_ICQ_ACCOUNT]."')\">".$row[$DB_ICQ_ACCOUNT]."</a></td>\n";
            }

			$ICQList = $ICQList."<td title=\"$ICQ_RECORDFILE\"><a href=\"icq_msg.php?_IDX=".$row[$DB_ICQ_AUTO]."\" target='blank';\">".$ICQ_RECORDFILE_SHOW."</a></td><td width=\"7%\" align=\"center\" >".$count."</td>";	
			$ICQList = $ICQList."</tr>\n";	
		}
	}
}
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 $ICQM_NOTSELECT;?>");
		return;
	}

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

<?if ($ErrorMsg != ""){?>
	fn_ErrorMsg('<?echo $ErrorMsg?>')
<?}?>
</SCRIPT>
</head>
<body onLoad="fix();" onScroll="fix();" onResize="fix();">
<form name="frm" action="icq.php" method="post">
<input type="hidden" name="strWhere[ICQ]" value="<?echo stripcslashes($strWhere["ICQ"]);?>">
<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_OWNER" value="<?echo $QUERY_OWNER;?>">
<input type="hidden" name="QUERY_WHOM" value="<?echo $QUERY_WHOM;?>">
<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="mime" value="<?echo $mime;?>">
<input type="hidden" name="ICQ" value="<?echo $ICQ;?>">
<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.$ICQP_ICQ?>">
	  <a href="icq.php"  title="<?echo $ICQ_ICQ?>"><?echo $ICQ_ICQ_SHOW?></a> | 
	  <?if (($HTTP_SESSION_VARS["OperateRole_Sess"]=="Super")&&($HTTP_SESSION_VARS["GroupNo_Sess"]=='0')){?> 
		<img src="../<?echo $DIR_IMAGE.$ICQP_DEL?>">&nbsp;<a href="javascript:checkDEL();"  title="<?echo $ICQ_DELSELECT?>"><?echo $ICQ_DELSELECT_SHOW?></a> | 
	  <?}?>
	  <img src="../<?echo $DIR_IMAGE.$ICQP_SEARCH?>">&nbsp;<a href="javascript:fn_OpenWin('../_search/chat_search_list.php?Status=icq&digital=<?echo $digital;?>','NewWin','width=430,height=280,left=320,top=250,menubar=no,toolbar=no,location=no,directories=no,scrollbars=yes,status=no,resizable=yes,fullscreen=no');"  title="<?echo $ICQ_SEARCH?>"><?echo $ICQ_SEARCH_SHOW?></a>
	</td>
	<td align="right">
	  <span class="ShowMode" title="<?echo $ICQ_SHOWMODE?>"><?echo $ICQ_SHOWMODE_SHOW?> : </span>
	  <input type="radio" name="Ip_Name" value="_IP" <? echo $chk_IP;?> onclick="document.frm.submit();">
	  <span title="<?echo $ICQ_IP?>"><?echo $ICQ_IP_SHOW?></span>
	  <input type="radio" name="Ip_Name" value="_USERNAME" <?echo $chk_USERNAME;?> onclick="document.frm.submit();">
	  <span title="<?echo $ICQ_NAME?>"><?echo $ICQ_NAME_SHOW?> |</span>
	  <span class="EveryPage" title="<?echo $ICQ_PAGEVIEW;?>"><?echo $ICQ_PAGEVIEW_SHOW;?> : </span>
	  <input type="text" size="1" name="sizepage" value="<?echo $sizepage?>" style="HEIGHT: 20px; WIDTH: 30px; border:1 solid">
	  <input type="button" name="bntPageSize" value="<?echo $ICQB_OK_SHOW;?>" onClick="ChangePageSize();" class="Btn_C" title="<?echo $ICQB_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="3%" title="<?echo $ICQ_NO?>"><?echo $ICQ_NO_SHOW?></td>
	<td width="3%" title="<?echo $ICQA_ATTACH?>"><img src="../<?echo $DIR_IMAGE.$ICQP_ATTACH;?>"></td>
    <td width="10%" <?echo $colDate?>>
	  <a href="javascript:ChangeSort('_DATE','<? echo $sortDate;?>');"  title="<?echo $ICQ_DATETIME ?>"><?echo $ICQ_DATETIME_SHOW ?><? echo $imgDate;?></a>
	</td>
    <td width="10%" <?echo $colIP?>>
	  <a href="javascript:ChangeSort('_IP','<?echo $sortIP;?>');"  title="<?echo $ICQ_IP_NAME?>"><?echo $ICQ_IP_NAME_1?><?echo $imgIP;?></a>
	</td>
    <td width="20%" <?echo $colOwner?>>
	  <a href="javascript:ChangeSort('_OWNER','<? echo $sortOwner;?>');"  title="<?echo $ICQ_USERID ?>"><?echo $ICQ_USERID_SHOW?><? echo $imgOwner;?></a>
	</td>
	<td width="20%" <?echo $colWhom?>>
	  <a href="javascript:ChangeSort('_WHOM','<? echo $sortWhom;?>');"  title="<?echo $ICQ_OTHERID ?>"><?echo $ICQ_OTHERID_SHOW ?><? echo $imgWhom;?></a>
	</td>
    <? if (fn_ad()){?>
		<td width="8%" <?echo $colAccount;?>>
			<a href="javascript:ChangeSort('_ACCOUNT', '<?echo $sortAccount;?>');"  title="<?echo $ICQ_ACCOUNT;?>"><?echo $ICQ_ACCOUNT_SHOW;?><?echo $imgAccount;?>
		</td>		
    <?}?>
	<td width="9%" title="<?echo $ICQ_RECORDFILE;?>"><?echo $ICQ_RECORDFILE_SHOW;?></td>
	<td width="7%" title="<?echo $ICQ_COUNT;?>"><?echo $ICQ_COUNT_SHOW;?></td>
  </tr>
  </table>
</span>

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

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

	<td width="3%"></td>
    <td width="10%"></td>
    <td width="10%"></td>
    <td width="20%"></td>
	<td width="20%"></td>
    <? if (fn_ad()){?>
		<td width="8%"></td>		
    <?}?>
	<td width="9%"></td>
	<td width="7%"></td>
  </tr>
  <?echo $ICQList;?>
  <tr>
    <td colSpan="
	<?
		if (($HTTP_SESSION_VARS["OperateRole_Sess"]=="Super")&&($HTTP_SESSION_VARS["GroupNo_Sess"]=='0')){
			if(fn_ad()) echo "10"; else echo "9";
		}else{
			if(fn_ad()) echo "9"; else echo "8";
		}
	?>
	" 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>