<?php
/**
 * @version $Id: log.inc.php v2.0 $
 * @package PBDigg
 * @copyright Copyright (C) 2007 - 2008 PBDigg.com. All Rights Reserved.
 * @license PBDigg is free software and use is subject to license terms
 */

if(!defined('IN_ADMIN') || !isset($_PBDEV['PHP_SELF']) || !preg_match('/[\/\\\\]admincp\.php$/i', $_PBDEV['PHP_SELF']))
{
	exit('Access Denied');
}
define('PB_CSCRIPT', 'log');
allowdo('setlog');
$baseurl = 'admincp.php?action=log&amp;type=';
$mult = $totalRecords = $log = '';
$pagesize = 20;
$page = !isPage($page) ? 1 : $page;
$start_limit = ($page -1) * $pagesize;

if ($type == 'admin' || empty($type))
{
	if ($step == 'del')
	{
		if ($_SUPERMANAGER)
		{
			$DB->db_query("DELETE FROM {$db_prefix}adminlogs WHERE islog = 0 AND logdate < ($timestamp - 259200)");
			redirect($a_message['log_del_success'], $actionurl);
		}
		else
		{
			redirect($a_message['admin_nopermission'], $actionurl);
		}
	}
	else
	{
		$rs = $DB->db_fetch_one_array("SELECT COUNT(*) num FROM {$db_prefix}adminlogs WHERE islog = 0");
		$totalRecords = $rs['num'] ? $rs['num'] : 0;
		$mult = cpmultLink($page, $totalRecords, 'admincp.php?action=log&amp;type=admin&amp;', $pagesize);
		$SQL = "SELECT l.*, m.username FROM {$db_prefix}adminlogs l 
				LEFT JOIN {$db_prefix}members m 
				ON l.uid = m.uid 
				WHERE l.islog = 0 
				ORDER BY l.logdate DESC 
				LIMIT $start_limit, $pagesize";
		$DB->db_query($SQL);
		while ($rs = $DB->db_fetch_array())
		{
			$rs['logdate'] = gdate($rs['logdate'], 'Y-m-d H:i');
			$rs['description'] = $rs['description'] ? $rs['description'] : '&nbsp;';
			$rs['action'] = wordwrap($rs['action'], 50, '<br />', 1);
			$log[] =  $rs;
		}
		$type = 'admin';
		$log_description = $cp_message['admin_log'];
	}
}
elseif ($type == 'login')
{
	if ($step == 'del')
	{
		if ($_SUPERMANAGER)
		{
			$DB->db_query("DELETE FROM {$db_prefix}adminlogs WHERE islog = 1 AND logdate < ($timestamp - 259200)");
			redirect($a_message['log_del_success'], $actionurl);
		}
		else
		{
			redirect($a_message['admin_nopermission'], $actionurl);
		}
	}
	else
	{
		$rs = $DB->db_fetch_one_array("SELECT COUNT(*) num FROM {$db_prefix}adminlogs WHERE islog = 1");
		$totalRecords = $rs['num'] ? $rs['num'] : 0;
		$mult = cpmultLink($page, $totalRecords, 'admincp.php?action=log&amp;type=login&amp;', $pagesize);
		$SQL = "SELECT l.*, m.username FROM {$db_prefix}adminlogs l 
				LEFT JOIN {$db_prefix}members m 
				ON l.uid = m.uid 
				WHERE l.islog = 1 
				ORDER BY l.logdate DESC 
				LIMIT $start_limit, $pagesize";
		$DB->db_query($SQL);
		while ($rs = $DB->db_fetch_array())
		{
			$rs['logdate'] = gdate($rs['logdate'], 'Y-m-d H:i');
			$rs['username'] = $rs['username'] ? $rs['username'] : '&nbsp;';
			if ($rs['result'])
			{
				$rs['result'] = '<span class="n">'.$a_message['admin_login_success'].'</span>';
			}
			else
			{
				$rs['result'] = '<span class="r">'.$a_message['admin_login_failed'].'</span>';
			}
			$rs['description'] = $rs['result'].$rs['description'];
			$log[] = $rs;
		}
		$log_description = $cp_message['login_log'];
	}
}
elseif ($type == 'common')
{
	if ($step == 'del')
	{
		if ($_SUPERMANAGER)
		{
			$DB->db_query("TRUNCATE TABLE {$db_prefix}commonlogs");
			redirect($a_message['log_del_success'], $actionurl);
		}
		else
		{
			redirect($a_message['admin_nopermission'], $actionurl);
		}
	}
	else
	{
		$rs = $DB->db_fetch_one_array("SELECT COUNT(*) num FROM {$db_prefix}commonlogs");
		$totalRecords = $rs['num'] ? $rs['num'] : 0;
		$mult = cpmultLink($page, $totalRecords, 'admincp.php?action=log&amp;type=login&amp;', $pagesize);
		$SQL = "SELECT l.*, m.username FROM {$db_prefix}commonlogs l 
				LEFT JOIN {$db_prefix}members m 
				ON l.uid = m.uid 
				ORDER BY l.logdate DESC 
				LIMIT $start_limit, $pagesize";
		$DB->db_query($SQL);
		while ($rs = $DB->db_fetch_array())
		{
			$rs['logdate'] = gdate($rs['logdate'], 'Y-m-d H:i');
			$rs['username'] = $rs['username'] ? $rs['username'] : '&nbsp;';
			$log[] = $rs;
		}
		$log_description = $cp_message['common_log'];
	}
}
else
{
	redirect($a_message['unknow_operate'], 'admincp.php?action=main');
}

$TPL->assign('log', $log);
$TPL->assign('mult', $mult);
$TPL->assign('total_records', $totalRecords);
$TPL->assign('menu', $m_log);
$actionurl .= '&amp;step=del';
?>
