<?php
// +-------------------------------------------------------------+
// | HiveMail™ version 1.3.1
// +-------------------------------------------------------------+
// | HIVEMAIL™ IS NOT FREE SOFTWARE
// | If you have downloaded this software from a website other
// | than Hivemail or if you have otherwise received
// | this software from someone who is not a representative of
// | this organization you are involved in an illegal activity.
// +-------------------------------------------------------------+
// | $RCSfile: adminlog.php,v $ - $Revision: 1.24 $
// | $Date: 2004/07/21 02:33:30 $ - $Author: tubedogg $
// +-------------------------------------------------------------+

error_reporting(E_ALL & ~E_NOTICE);
require_once('./global.php');
cp_header(' &raquo; Admin Log', ($cmd != 'getinfo'), ($cmd != 'getinfo'));
cp_nav('logadmin');

// ############################################################################
// Set the default cmd
default_var($cmd, 'intro');

// ############################################################################
// Grab useful information
$getusers = $DB_site->query('
	SELECT adminlog.userid, user.username, user.realname
	FROM hive_adminlog AS adminlog
	LEFT JOIN hive_user AS user USING (userid)
	GROUP BY adminlog.userid
');
$users = array();
while ($getuser = $DB_site->fetch_array($getusers)) {
	$users["$getuser[userid]"] = "$getuser[realname] ($getuser[username])";
}
$getfiles = $DB_site->query('
	SELECT filename
	FROM hive_adminlog
	GROUP BY filename
');
$files = array();
while ($getfile = $DB_site->fetch_array($getfiles)) {
	$files["$getfile[filename]"] = $getfile['filename'];
}

$successOptions = array(
	'1' => 'Successful',
	'1,0' => 'Successful or Unsuccessful',
	'0' => 'Unsuccessful',
	'0,-1' => 'Unsuccessful or N/A',
	'-1' => 'N/A',
	'-1,1' => 'N/A or Successful',
);
$sortBy = array(
	'dateline' => 'Date and time',
	'filename' => 'Script',
	'username' => 'Username',
	'do' => 'Action performed',
	'success' => 'Action status',
	'ipaddress' => 'IP address'
);

// ############################################################################
// Prune
if ($cmd == 'getinfo') {
	$adminlog = getinfo('adminlog', $adminlogid);
	adminlog($adminlogid);

	echo "<div align=\"center\">\n";
	starttable('Entry Information');
	tablerow(array('In script:', $adminlog['filename']), true);
	tablerow(array('Action that was performed:', iif(empty($adminlog['do']), 'N/A', $adminlog['do'])), true);
	tablerow(array('Action performed on item ID:', iif($adminlog['id'] == 0, 'N/A', $adminlog['id'])), true);
	tablerow(array('Generated by user:', $adminlog['userid']), true);
	tablerow(array('With IP address:', $adminlog['ipaddress']), true);
	tablerow(array('Status of action:', $successOptions["$adminlog[success]"]), true);
	tablerow(array('Date and time:', hivedate($adminlog['dateline'], getop('dateformat').' '.getop('timeformat'))), true);
	tablerow(array('Additional information:', iif(empty($adminlog['notes']), 'N/A', $adminlog['notes'])), true);
	endtable();
	echo '</div>';
}

// ############################################################################
// List the admin log
if ($cmd == 'list') {
	//adminlog();

	$sqlwhere = '1 = 1';
	$link = "&sortby=$sortby&sortorder=$sortorder";
	if (is_array($filter)) {
		foreach ($filter as $subject => $value) {
			$value = trim($value);
			if (empty($value) or $value == -1) {
				continue;
			}

			$field = substr($subject, 1);
			$link .= "&filter[$subject]=".urlencode($value);

			switch (substr($subject, 0, 1)) {
				case 'l':
					if (substr($field, 0, 4) == 'date') {
						$sqlwhere .= " AND adminlog.$field < UNIX_TIMESTAMP('".addslashes($value)."')";
					} else {
						$sqlwhere .= " AND adminlog.$field < '".intval($value)."'";
					}
					break;
				case 'm':
					if (substr($field, 0, 4) == 'date') {
						$sqlwhere .= " AND adminlog.$field > UNIX_TIMESTAMP('".addslashes($value)."')";
					} else {
						$sqlwhere .= " AND adminlog.$field > '".intval($value)."'";
					}
					break;
				case 'e':
					$sqlwhere .= " AND adminlog.$field = '".addslashes($value)."'";
					break;
				case 'i':
					$sqlwhere .= " AND adminlog.$field IN ($value)";
					break;
			}
		}
	}

	?><script language="JavaScript">
	<!--
	function moreinfo(logid) {
		window.open('adminlog.php?cmd=getinfo&adminlogid='+logid, 'moreInfo'+logid, 'width=520,height=290,resizable=yes,scrollbars=yes');
	}
	// -->
	</script><?php

	$pagenav = paginate('adminlog', "adminlog.php?cmd=list$link", "WHERE $sqlwhere");
	$logs = $DB_site->query("
		SELECT adminlog.*, user.username
		FROM hive_adminlog AS adminlog
		LEFT JOIN hive_user AS user USING (userid)
		WHERE $sqlwhere
		ORDER BY $sortby $sortorder
		LIMIT ".($limitlower-1).", $perpage
	");

	$cells = array(
		'Log ID',
		'Script',
		'Action',
		'User',
		'Status',
		'Item ID',
		'Date/Time',
		'IP Address',
		'More Info'
	);
	if ($DB_site->num_rows($logs) < 1) {
		starttable('Error');
		tablehead($cells);
		textrow('No log entries found. Try some different terms.', count($cells), 1);
	} else {
		starttable('Admin Log Entries Found');
		tablehead($cells);
		while ($log = $DB_site->fetch_array($logs)) {
			$cells = array(
				$log['adminlogid'],
				$log['filename'],
				$log['do'],
				"<a href=\"user.php?cmd=edit&auserid=$log[userid]\">$log[username]</a>",
				$successOptions["$log[success]"],
				iif($log['id'] == 0, 'N/A', $log['id']),
				hivedate($log['dateline'], getop('dateformat').' '.getop('timeformat')),
				$log['ipaddress'],
				"[<a href=\"#\" onClick=\"moreinfo($log[adminlogid]); return false;\">more info</a>]"
			);
			tablerow($cells, true);
		}
	}
	tablehead(array("$pagenav&nbsp;"), count($cells));
	endtable();
}

// ############################################################################
// Display some options
if ($cmd == 'intro') {
	adminlog();

	startform('adminlog.php', 'list');
	starttable('Admin Log Browser: Find Entries Where...', '550');
	selectbox('Logs for script:', 'filter[efilename]', $files, -1, 'any file');
	selectbox('Generated by user:', 'filter[euserid]', $users, -1, 'any user');
	selectbox('Status of actions:', 'filter[isuccess]', $successOptions, -2, 'any status');
	datefield('Generated after:<br /><span class="cp_small">Format: yyyy-mm-dd.</span>', 'filter[mdateline]', $find['mdatelastvisit']);
	datefield('Generated before:<br /><span class="cp_small">Format: yyyy-mm-dd.</span>', 'filter[ldateline]', $find['ldatelastvisit']);
	inputfield('Entries to display per page:', 'perpage', '50');
	selectbox('Sort entries by:', 'sortby', $sortBy, 'dateline', '', '&nbsp;in&nbsp;<select name="sortorder" id="sortorder">
			<option value="desc" selected="selected">descending order</option>
			<option value="asc">ascending order</option>
		</select>');
	endform('Display Log');
	endtable();

	echo '<br />';
	startform('adminlog.php', 'doprune');
	starttable('Prune Admin Log: Prune Entries Where...', '550');
	selectbox('Logs for script:', 'filter[efilename]', $files, -1, 'any file');
	selectbox('Generated by user:', 'filter[euserid]', $users, -1, 'any user');
	selectbox('Status of actions:', 'filter[isuccess]', $successOptions, -2, 'any status');
	datefield('Generated after:<br /><span class="cp_small">Format: yyyy-mm-dd.</span>', 'filter[mdateline]', $find['mdatelastvisit']);
	datefield('Generated before:<br /><span class="cp_small">Format: yyyy-mm-dd.</span>', 'filter[ldateline]', $find['ldatelastvisit']);
	endform('Prune Log');
	endtable();
}

// ############################################################################
// Prune
if ($_POST['cmd'] == 'doprune') {
	$sqlwhere = '1 = 1';
	if (is_array($filter)) {
		foreach ($filter as $subject => $value) {
			$value = trim($value);
			if (empty($value) or $value == -1) {
				continue;
			}

			$field = substr($subject, 1);
			
			switch (substr($subject, 0, 1)) {
				case 'l':
					if (substr($field, 0, 4) == 'date') {
						$sqlwhere .= " AND $field < UNIX_TIMESTAMP('".addslashes($value)."')";
					} else {
						$sqlwhere .= " AND $field < '".intval($value)."'";
					}
					break;
				case 'm':
					if (substr($field, 0, 4) == 'date') {
						$sqlwhere .= " AND $field > UNIX_TIMESTAMP('".addslashes($value)."')";
					} else {
						$sqlwhere .= " AND $field > '".intval($value)."'";
					}
					break;
				case 'e':
					$sqlwhere .= " AND $field = '".addslashes($value)."'";
					break;
				case 'i':
					$sqlwhere .= " AND $field IN ($value)";
					break;
			}
		}
	}

	$logs = $DB_site->query("DELETE FROM hive_adminlog".iif($sqlwhere != '1 = 1', " WHERE $sqlwhere"));
	cp_redirect('Selected entries have been pruned from the adming log.', 'adminlog.php');
}

cp_footer(($cmd != 'getinfo'));
?>