<?php
define('EmpireCMSAdmin','1');
require("../../../e/class/connect.php");
require("../../../e/class/functions.php");
require "../../../e/data/".LoadLang("pub/enews.php");
require "../../../e/data/".LoadLang("pub/fun.php");
require("../../../e/data/dbcache/class.php");
$link=db_connect();
$empire=new mysqlquery();
$editor=1;
//驗證用戶
$lur=is_login();
$logininid=(int)$lur['userid'];
$loginin=$lur['username'];
$loginrnd=$lur['rnd'];
$loginlevel=(int)$lur['groupid'];
$loginadminstyleid=(int)$lur['adminstyleid'];
//ehash
$ecms_hashur=hReturnEcmsHashStrAll();
//驗證權限
CheckLevel($logininid,$loginin,$classid,"log");

//刪除日誌
function DelDoLog($logid,$userid,$username){
	global $empire,$dbtbpre;
	//驗證權限
	CheckLevel($userid,$username,$classid,"log");
	$logid=(int)$logid;
	if(!$logid)
	{
		printerror("NotDelLogid","history.go(-1)");
	}
	$sql=$empire->query("delete from {$dbtbpre}enewsdolog where logid='$logid'");
	if($sql)
	{
		//操作日誌
		insert_dolog("logid=".$logid);
		printerror("DelLogSuccess","ListDolog.php".hReturnEcmsHashStrHref2(1));
	}
	else
	{
		printerror("DbError","history.go(-1)");
	}
}

//批量刪除日誌
function DelDoLog_all($logid,$userid,$username){
	global $empire,$dbtbpre;
	//驗證權限
	CheckLevel($userid,$username,$classid,"log");
	$logid=eCheckEmptyArray($logid);
	$count=count($logid);
	if(!$count)
	{
		printerror("NotDelLogid","history.go(-1)");
	}
	$add='';
	for($i=0;$i<$count;$i++)
	{
		$add.=" logid='".intval($logid[$i])."' or";
	}
	$add=substr($add,0,strlen($add)-3);
	$sql=$empire->query("delete from {$dbtbpre}enewsdolog where".$add);
	if($sql)
	{
		//操作日誌
		insert_dolog("");
		printerror("DelLogSuccess","ListDolog.php".hReturnEcmsHashStrHref2(1));
	}
	else
	{
		printerror("DbError","history.go(-1)");
	}
}

//日期刪除日誌
function DelDoLog_date($add,$userid,$username){
	global $empire,$dbtbpre;
	//驗證權限
	CheckLevel($userid,$username,$classid,"log");
	$start=RepPostVar($add['startday']);
	$end=RepPostVar($add['endday']);
	if(!$start||!$end)
	{
		printerror('EmptyDelLogTime','');
	}
	$startday=$start.' 00:00:00';
	$endday=$end.' 23:59:59';
	$sql=$empire->query("delete from {$dbtbpre}enewsdolog where logtime<='$endday' and logtime>='$startday'");
	if($sql)
	{
		//操作日誌
		insert_dolog("time=".$start."~".$end);
		printerror("DelLogSuccess","ListDolog.php".hReturnEcmsHashStrHref2(1));
	}
	else
	{
		printerror("DbError","history.go(-1)");
	}
}

$enews=$_POST['enews'];
if(empty($enews))
{$enews=$_GET['enews'];}
if($enews)
{
	hCheckEcmsRHash();
}
//刪除日誌
if($enews=="DelDoLog")
{
	$logid=$_GET['logid'];
	DelDoLog($logid,$logininid,$loginin);
}
//批量刪除日誌
elseif($enews=="DelDoLog_all")
{
	$logid=$_POST['logid'];
	DelDoLog_all($logid,$logininid,$loginin);
}
elseif($enews=="DelDoLog_date")
{
	DelDoLog_date($_POST,$logininid,$loginin);
}

$line=20;//每頁顯示條數
$page_line=18;//每頁顯示鏈接數
$page=(int)$_GET['page'];
$page=RepPIntvar($page);
$start=0;
$offset=$page*$line;//總偏移量
//搜索
$search='';
$search.=$ecms_hashur['ehref'];
$where='';
$and=' where ';
//信息ID
$classid=(int)$_GET['classid'];
$id=(int)$_GET['id'];
if($classid&&$id)
{
	$pubid=ReturnInfoPubid($classid,$id);
	$where.=$and."pubid='$pubid'";
	$and=' and ';
	$search.="&classid=$classid&id=$id";
}
if($_GET['sear']==1)
{
	$search.="&sear=1";
	$startday=RepPostVar($_GET['startday']);
	$endday=RepPostVar($_GET['endday']);
	if($startday&&$endday)
	{
		$where.=$and."logtime<='".$endday." 23:59:59' and logtime>='".$startday." 00:00:00'";
		$and=' and ';
		$search.="&startday=$startday&endday=$endday";
	}
	$keyboard=RepPostVar($_GET['keyboard']);
	if($keyboard)
	{
		$show=RepPostStr($_GET['show'],1);
		if($show==1)
		{
			$where.=$and."username like '%$keyboard%'";
		}
		elseif($show==2)
		{
			$where.=$and."logip like '%$keyboard%'";
		}
		elseif($show==3)
		{
			$where.=$and."enews like '%$keyboard%'";
		}
		elseif($show==4)
		{
			$where.=$and."doing like '%$keyboard%'";
		}
		elseif($show==5)
		{
			$where.=$and."userid='$keyboard'";
		}
		else
		{
			$where.=$and."(username like '%$keyboard%' or logip like '%$keyboard%')";
		}
		$and=' and ';
		$search.="&keyboard=$keyboard&show=$show";
	}
}
$search2=$search;
//排序
$mydesc=(int)$_GET['mydesc'];
$desc=$mydesc?'asc':'desc';
$orderby=(int)$_GET['orderby'];
if($orderby==1)//登陸用戶
{
	$order="username ".$desc.",logid desc";
	$usernamedesc=$mydesc?0:1;
}
elseif($orderby==2)//IP
{
	$order="logip ".$desc.",logid desc";
	$logipdesc=$mydesc?0:1;
}
elseif($orderby==3)//操作時間
{
	$order="logtime ".$desc.",logid desc";
	$logtimedesc=$mydesc?0:1;
}
else//ID
{
	$order="logid ".$desc;
	$logiddesc=$mydesc?0:1;
}
$search.="&orderby=$orderby&mydesc=$mydesc";
$query="select logid,logip,logtime,username,enews,doing,ipport,userid from {$dbtbpre}enewsdolog".$where;
$totalquery="select count(*) as total from {$dbtbpre}enewsdolog".$where;
$num=$empire->gettotal($totalquery);//取得總條數
$query=$query." order by ".$order."".do_dblimit($line,$offset);
$sql=$empire->query($query);
$returnpage=page2($num,$line,$page_line,$start,$page,$search);
?>
<html>
<head>
<link href="../adminstyle/<?=$loginadminstyleid?>/adminstyle.css" rel="stylesheet" type="text/css"> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>管理操作日誌</title>
<script type="text/javascript" src="../ecmseditor/js/jstime/WdatePicker.js"></script>
<script>
function CheckAll(form)
  {
  for (var i=0;i<form.elements.length;i++)
    {
    var e = form.elements[i];
    if (e.name != 'chkall')
       e.checked = form.chkall.checked;
    }
  }
</script>

</head>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1">
  <tr> 
    <td width="50%" height="32">位置：日誌管理 &gt; <a href="ListDolog.php<?=$ecms_hashur['whehref']?>">管理操作日誌</a></td>
    <td><div align="right" class="emenubutton">
        <input type="button" name="Submit5" value="管理登錄日誌" onclick="self.location.href='ListLog.php<?=$ecms_hashur['whehref']?>';">
      </div></td>
  </tr>
</table>
  
<br>
<table width="100%" align=center cellpadding=0 cellspacing=0>
  <form name=searchlogform method=get action='ListDolog.php'>
  <?=$ecms_hashur['eform']?>
    <tr> 
      <td height="25"> <div align="center">時間從 
          <input name="startday" type="text" value="<?=$startday?>" size="15" class="Wdate" onClick="WdatePicker({skin:'default',dateFmt:'yyyy-MM-dd'})">
          到 
          <input name="endday" type="text" value="<?=$endday?>" size="15" class="Wdate" onClick="WdatePicker({skin:'default',dateFmt:'yyyy-MM-dd'})">
          ，關鍵字： 
          <input name="keyboard" type="text" id="keyboard" value="<?=$keyboard?>">
          <select name="show" id="show">
            <option value="1"<?=$show==1?' selected':''?>>用戶名</option>
			<option value="5"<?=$show==5?' selected':''?>>用戶ID</option>
            <option value="2"<?=$show==2?' selected':''?>>操作IP</option>
			<option value="3"<?=$show==3?' selected':''?>>操作事件</option>
			<option value="4"<?=$show==4?' selected':''?>>操作內容</option>
          </select>
          欄目ID：
          <input name="classid" type="text" id="classid" value="<?=$classid?>" size="10">
          信息ID：
          <input name="id" type="text" id="id" value="<?=$id?>" size="10">
          <input name=submit1 type=submit id="submit12" value=搜索>
          <input name="sear" type="hidden" id="sear" value="1">
        </div></td>
    </tr>
  </form>
</table>
<form name="form2" method="post" action="ListDolog.php" onsubmit="return confirm('確認要刪除?');">
  <table width="100%" border="0" align="center" cellpadding="0" cellspacing="1">
  <?=$ecms_hashur['form']?>
  <?php echo heformhash_get('DelDoLog_all'); ?>
    <tr class="header"> 
      <td width="8%"> 
        <div align="center"><a href="ListDolog.php?orderby=0&mydesc=<?=$logiddesc.$search2?>">ID</a></div></td>
      <td width="24%" height="25"> 
        <div align="center"><a href="ListDolog.php?orderby=1&mydesc=<?=$usernamedesc.$search2?>">操作者</a></div></td>
      <td width="23%" height="25"> 
        <div align="center"><a href="ListDolog.php?orderby=2&mydesc=<?=$logipdesc.$search2?>">IP</a></div></td>
      <td width="27%"> 
        <div align="center"><a href="ListDolog.php?orderby=3&mydesc=<?=$logtimedesc.$search2?>">操作時間</a></div></td>
      <td width="18%" height="25"> 
        <div align="center">刪除</div></td>
    </tr>
    <?php
	//formhash
	$efh=heformhash_get('DelDoLog',1);
	
  while($r=$empire->fetch($sql))
  {
	  $dologenews=$enews_r[$r['enews']]?$enews_r[$r['enews']]:$r['enews'];
	  $showuid=$r['userid']?' ('.$r['userid'].')':'';
  ?>
    <tr bgcolor="#DBEAF5" id=log<?=$r['logid']?>> 
      <td> 
        <div align="center"><?=$r['logid']?></div></td>
      <td height="25"> 
        <div align="center"> 
          <?=$r['username'].$showuid?>
        </div></td>
      <td height="25"> 
        <div align="center"> 
          <?=$r['logip']?>:<?=$r['ipport']?>
        </div></td>
      <td> 
        <div align="center"> 
          <?=$r['logtime']?>
        </div></td>
      <td height="25"> 
        <div align="center">[<a href="ListDolog.php?enews=DelDoLog&logid=<?=$r['logid']?><?=$ecms_hashur['href'].$efh?>" onclick="return confirm('確認要刪除此日誌?');">刪除</a> 
          <input name="logid[]" type="checkbox" id="logid[]" value="<?=$r['logid']?>" onclick="if(this.checked){log<?=$r['logid']?>.style.backgroundColor='#cccccc';}else{log<?=$r['logid']?>.style.backgroundColor='#DBEAF5';}">
          ]</div></td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td height="25" colspan="5"><table width="100%" border="0" cellspacing="1" cellpadding="3">
          <tr>
            <td width="9%">動作：</td>
            <td width="25%"><?=$dologenews?></td>
            <td width="10%">操作對像：</td>
            <td width="56%"><?=stripSlashes($r['doing'])?></td>
          </tr>
        </table></td>
    </tr>
    <?php
  }
  ?>
    <tr bgcolor="#FFFFFF"> 
      <td height="25" colspan="5"> 
        <?=$returnpage?>&nbsp;&nbsp;
        <input type="submit" name="Submit" value="批量刪除"> <input name="enews" type="hidden" id="phome" value="DelDoLog_all">&nbsp;
        <input type=checkbox name=chkall value=on onClick=CheckAll(this.form)>
        選中全部 </td>
    </tr>
  </table>
</form>
<form action="ListDolog.php" method="post" name="dellogform" id="dellogform" onsubmit="return confirm('確認要刪除?');">
  <table width="100%" border="0" cellspacing="1" cellpadding="3">
  <?=$ecms_hashur['form']?>
  <?php echo heformhash_get('DelDoLog_date'); ?>
    <tr> 
      <td><div align="center">
          <input name="enews" type="hidden" id="enews" value="DelDoLog_date">
          刪除從 
          <input name="startday" type="text" id="startday" value="<?=$startday?>" size="15" class="Wdate" onClick="WdatePicker({skin:'default',dateFmt:'yyyy-MM-dd'})">
          到 
          <input name="endday" type="text" id="endday" value="<?=$endday?>" size="15" class="Wdate" onClick="WdatePicker({skin:'default',dateFmt:'yyyy-MM-dd'})">
          之間的日誌
<input type="submit" name="Submit2" value="提交">
          </div></td>
    </tr>
  </table>
</form>
<?php
db_close();
$empire=null;
?>
<br>
</body>
</html>