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

$_Pass_Path="/opt/local/apache/htdocs/config_file/chk_attachment_count";
$_Pass_PathTmp="/tmp/chk_attachment_count";
$_DELETE_DIGITAL_PATH = "../remove/";


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

	//--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_IP					 = $HTTP_POST_VARS["QUERY_IP"];
	$QUERY_IP_1			         = $HTTP_POST_VARS["QUERY_IP_1"];
	$QUERY_ID				     = $HTTP_POST_VARS["QUERY_ID"];
	$QUERY_ACCOUNT				 = $HTTP_POST_VARS["QUERY_ACCOUNT"];
	$QUERY_CONTENT			     = $HTTP_POST_VARS["QUERY_CONTENT"];

	$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"];

	$QQ						     = $HTTP_POST_VARS['QQ'];		    //--全文檢索 -auto 值
	$mime						 = $HTTP_POST_VARS['mime'];			//--全文檢索 flag
	$digital					 = $HTTP_POST_VARS['digital'];	    //--數位資產 flag		
	$sizepage					 = $HTTP_POST_VARS["sizepage"];       //--每頁筆數
	$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="_AUTO";
	include($_SORT_PATH);

	//--判斷點選那一個顯示模式 
	switch ($Ip_Name){
		case "_IP":
			$QQ_IP_NAME = $QQ_MAIN_IP;
			$QQ_IP_NAME_1 = "IP";
			$chk_IP = "checked";
			break;
		case "_USERNAME":
			$QQ_IP_NAME =$QQ_MAIN_NAME;
			$QQ_IP_NAME_1 = "NAME";
			$chk_USERNAME = "checked";
			break;
	}
	//--更改每頁的筆數設定
	if ($RunAs == 'UpdatePageSize'){
		if ($sizepage){ 
			$HTTP_SESSION_VARS["size_page"] = $sizepage;
		}
	}

	//檢查並刪除需刪除的資料，
	if($RunAs == "Delete"){
		$_FILE = "QQ_".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 QQ where _AUTO=".$chkbox[$i];
			$intSQL = mysql_query($strSQL);
			if ((fn_digital_asset()==1)||(fn_data_mining()==1)) fputs($fp,"QQ.".$chkbox[$i]."\n");
			if (!$intSQL) $flag = 1;
			$strSQL = "Delete from QQ_MSG where _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 = $QQ_MAINM_DELFAIL;
			if ((fn_digital_asset()==1)||(fn_data_mining()==1)) unlink($TMP_FILE);
		}
		else{
			mysql_query("OPTIMIZE TABLE QQ");
			mysql_query("OPTIMIZE TABLE QQ_MSG");
			$ErrorMsg = $QQ_MAINM_DELSUCCESS;
			if ((fn_digital_asset()==1)||(fn_data_mining()==1)){
				copy($TMP_FILE,$_DELETE_DIGITAL_PATH.$_FILE);
				unlink($TMP_FILE);
			}
		}
	}
   		


	$strSQL = "Select count(*) From QQ ";

	if ($mime == 1){
		$strSQL = $strSQL." Where _AUTO in (".$QQ.")";
	}else{
		$strSQL = $strSQL." where _IP not in (".fn_NotShowIP().")";
		if ($HTTP_SESSION_VARS['GroupNo_Sess']!=0){
			$strSQL = $strSQL." and _IP in (".fn_IPcontrol($HTTP_SESSION_VARS['GroupNo_Sess']).")";
		}
		if($strWhere["QQ"] != "") $strSQL = $strSQL.stripcslashes($strWhere["QQ"]);
	}

	$condition = search($QUERY_START_DATETIME,$QUERY_END_DATETIME,$QUERY_IP,$QUERY_IP_1,$QUERY_OWNER,$QUERY_WHOM,$QUERY_ID,$QUERY_ACCOUNT,$QUERY_CONTENT,'qq');
	
	$strSQL = $strSQL.$condition;
    //計算符合文章總數 & 初始化分頁變數
    include($_PAGE_PATH);
   
    //為了畫面排序的sql語法
    $strSQL = "Select _AUTO,_TIMESTAMP,_IP,_MAC,_OWNER,_WHOM,_ACCOUNT From QQ ";
	if ($mime == 1){
		$strSQL = $strSQL.fn_Datamining($QQ,"_AUTO",$m,$n,$to_page);
		$m = 0;
	}else{
		$strSQL = $strSQL." where _IP  not in (".fn_NotShowIP().") ";
		if ($HTTP_SESSION_VARS['GroupNo_Sess']!=0){
			$strSQL = $strSQL." and _IP in (".fn_IPcontrol($HTTP_SESSION_VARS['GroupNo_Sess']).")";
		}
		if($strWhere["QQ"] != "") $strSQL = $strSQL.stripcslashes($strWhere["QQ"]);
	}

	$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){
		$QQList = "<tr height=\"30\" class=\"NoData\"><td colspan=\"";
		if (($HTTP_SESSION_VARS["OperateRole_Sess"]=="Super")&&($HTTP_SESSION_VARS["GroupNo_Sess"]=='0')){
			if(fn_ad()) $QQList = $QQList."10"; else $QQList = $QQList."9";
		}else{
			if(fn_ad()) $QQList = $QQList."9"; else $QQList = $QQList."8";
		}
		include("../no_data.php");
		$QQList = $QQList."\"".$NoDataList;
		$_Disabled = "disabled";
	}
	else{	
		$QQList = "";
		$chkCount = ($to_page-1)*$sizepage+1; 
		while ($row=mysql_fetch_array($intResult)){
            if ($chk_count !=''){
                $strSQL1 = "Select count(*) from QQ_MSG where _IDX = ".$row["_AUTO"];
                $intResult1 = mysql_fetch_array(mysql_query($strSQL1));
                $count = $intResult1[0];
            }

           if($chk_attachment !=''){
                $strSQL1 = "Select count(_DB_NAME) from QQ_MSG where _IDX = ".$row["_AUTO"]." and _TYPE>0";
                $intResult1 = mysql_fetch_array(mysql_query($strSQL1)) or die(mysql_error());
                $count_file = $intResult1[0];
			}

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

			//--file yes or no
			$QQList = $QQList."<td>";
			if($count_file>0) $QQList = $QQList."<img src=\"../".$DIR_IMAGE.$QQP_ATTACH."\">"; else $QQList = $QQList."&nbsp;";
			$QQList = $QQList."</td>";
			
			//--datetime 
			$QQList = $QQList."<td><a href=\"#\" onClick=\"ChangeCondition('_TIMESTAMP','".$row["_TIMESTAMP"]."');\">".fn_DateTime($row["_TIMESTAMP"])."</a></td>\n";
			
			//--IP
			switch ($Ip_Name){
			case "_IP":
				$QQList = $QQList."<td><p title=\"".$row["_MAC"]."\"><a href=\"#\" onClick=\"ChangeCondition('_IP','".$row["_IP"]."');\">".$row["_IP"]."</a></p></td>\n";
				break;
			case "_USERNAME":
				$SQL = "Select username From IPCONTROL Where ip = '".$row["_IP"]."'";
				$Result = mysql_query($SQL);
				if (mysql_num_rows($Result)>0){
						$row_sub = mysql_fetch_array($Result);
						$QQList = $QQList."<td align=\"center\"><p title=\"".$row["_MAC"]."\"><a href=\"#\" onClick=\"ChangeCondition('_IP','".$row["_IP"]."');\">";
						
						if ($row_sub["username"] == '***') $QQList = $QQList.$row["_IP"]; else $QQList = $QQList.$row_sub["username"];

					$QQList = $QQList."</a></p></td>";
					}
					else{
						$QQList = $QQList."<td align=\"center\"><p title=\"".$row["_MAC"]."\"><a href=\"#\" onClick=\"ChangeCondition('_IP','".$row["_IP"]."');\">".$row["_IP"]."</a></p></td>";
					}
					break;
			
			}
			//--owner
			$QQList = $QQList."<td><a href=\"#\" onClick=\"ChangeCondition('_OWNER','".$row["_OWNER"]."');\">".$row["_OWNER"]."</a></td>\n";
			
			//--whom
			$QQList = $QQList."<td><a href=\"#\" onClick=\"ChangeCondition('_WHOM','".$row["_WHOM"]."');\">".$row["_WHOM"]."</a></td>\n";
			
			if(fn_ad()){
				$QQList = $QQList."<td align=\"center\"><a href=\"#\" onClick=\"ChangeCondition('_ACCOUNT','".$row["_ACCOUNT"]."')\">".$row["_ACCOUNT"]."</a></td>\n";
            }
			
			//converstion
			$QQList = $QQList."<td title=\"$QQ_MAIN_RECORDFILE\"><a href=\"qq_msg.php?_IDX=".$row["_AUTO"]."\" target='blank';\">".$QQ_MAIN_RECORDFILE_SHOW."</a></td><td>".$count."</td>";

			$QQList = $QQList."</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 $QQ_MAINM_NOTSELECT;?>");
		return;
	}

	if (confirm("<?echo $QQ_MAINM_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="qq.php" method="post">
<input type="hidden" name="strWhere[QQ]" value="<?echo stripcslashes($strWhere["QQ"]);?>">
<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="QQ" value="<?echo $QQ;?>">
<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.$QQP_QQ?>">
	  <a href="qq.php" title="<?echo $QQ_MAIN_QQ?>"><?echo $QQ_MAIN_QQ_SHOW?></a> | 

	  <img src="../<?echo $DIR_IMAGE.$QQP_SEARCH?>">&nbsp;<a href="javascript:fn_OpenWin('../_search/chat_search_list.php?Status=qq','NewWin','width=420,height=280,left=320,top=250,menubar=no,toolbar=no,location=no,directories=no,scrollbars=yes,status=no,resizable=yes,fullscreen=no');" title="<?echo $QQ_MAIN_SEARCH?>"><?echo $QQ_MAIN_SEARCH_SHOW?></a> 
	</td>

	<td align="right">
	  <span class="ShowMode" title="<?echo $QQ_MAIN_SHOWMODE?>"><?echo $QQ_MAIN_SHOWMODE_SHOW?> : </span>
	  <input type="radio" name="Ip_Name" value="_IP" <? echo $chk_IP;?> onclick="document.frm.submit();">
	  <span title="<?echo $QQ_MAIN_IP?>"><?echo $QQ_MAIN_IP_SHOW?></span>
	  <input type="radio" name="Ip_Name" value="_USERNAME" <?echo $chk_USERNAME;?> onclick="document.frm.submit();">
	  <span title="<?echo $QQ_MAIN_NAME?>"><?echo $QQ_MAIN_NAME_SHOW?> |</span> 
	  <span class="EveryPage" title="<?echo $QQ_MAIN_PAGEVIEW;?>"><?echo $QQ_MAIN_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 $QQ_MAINB_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 $QQ_MAIN_NO?>"><?echo $QQ_MAIN_NO_SHOW?></td>

	<td width="3%" title="<?echo $QQ_MAINA_ATTACH?>"><img src="../<?echo $DIR_IMAGE.$QQP_ATTACH;?>"></td>
    <td width="10%" <?echo $colDate?>>
	  <a href="javascript:ChangeSort('_TIMESTAMP','<? echo $sortDate;?>');" class="TT" title="<?echo $QQ_MAIN_DATETIME?>"><?echo $QQ_MAIN_DATETIME_SHOW?><? echo $imgDate;?></a>
	</td>
    <td width="10%" <?echo $colIP?>>
	  <a href="javascript:ChangeSort('_IP','<? echo $sortIP;?>');" class="TT" title="<?echo $QQ_MAIN_IP?>"><?echo $QQ_IP_NAME_1?><? echo $imgIP;?></a>
	</td>
    <td width="20%" <?echo $colOwner?>>
	  <a href="javascript:ChangeSort('_OWNER','<? echo $sortOwner;?>');"  title="<?echo $QQ_MAIN_USERID?>"><?echo $QQ_MAIN_USERID_SHOW?><? echo $imgOwner;?></a>
	</td>
	<td width="20%" <?echo $colWhom?>>
	  <a href="javascript:ChangeSort('_WHOM','<? echo $sortWhom;?>');"  title="<?echo $QQ_MAIN_OTHERID?>"><?echo $QQ_MAIN_OTHERID_SHOW?><? echo $imgWhom;?></a>
	</td>
	<? if (fn_ad()){?>
		<td width="8%" <?echo $colAccount;?>>
			<a href="javascript:ChangeSort('_ACCOUNT', '<?echo $sortAccount;?>');"  title="<?echo $QQ_MAIN_ACCOUNT;?>"><?echo $QQ_MAIN_ACCOUNT_SHOW;?><?echo $imgAccount;?>
		</td>    <?}?>
	<td width="9%" title="<?echo $QQ_MAIN_RECORDFILE;?>"><?echo $QQ_MAIN_RECORDFILE_SHOW;?></td>
	<td width="4%" title="<?echo $QQ_MAIN_COUNT;?>"><?echo $QQ_MAIN_COUNT_SHOW;?></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="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="4%"></td>
  </tr>
  <?echo $QQList;?>
  <tr bgcolor="<?echo $titletrbgcol;?>" height="24" valign="middle">
    <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>