<?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: iplog.php,v $ - $Revision: 1.12 $
// | $Date: 2004/07/21 02:33:30 $ - $Author: tubedogg $
// +-------------------------------------------------------------+

error_reporting(E_ALL & ~E_NOTICE);
require_once('./global.php');
require_once('../includes/events.php');
cp_header(' &raquo; IP Addresses Log', true, true);
cp_nav('logip');

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

$getusers = $DB_site->query('
	SELECT iplog.userid, user.username, user.realname
	FROM hive_iplog AS iplog
	LEFT JOIN hive_user AS user USING (userid)
	GROUP BY iplog.userid
');
$users = array();
while ($getuser = $DB_site->fetch_array($getusers)) {
	$users["$getuser[userid]"] = "$getuser[realname] ($getuser[username])";
}

// ############################################################################
// 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 iplog.$field < UNIX_TIMESTAMP('".addslashes($value)."')";
					} else {
						$sqlwhere .= " AND iplog.$field < '".intval($value)."'";
					}
					break;
				case 'm':
					if (substr($field, 0, 4) == 'date') {
						$sqlwhere .= " AND iplog.$field > UNIX_TIMESTAMP('".addslashes($value)."')";
					} else {
						$sqlwhere .= " AND iplog.$field > '".intval($value)."'";
					}
					break;
				case 'b':
					list($minvalue,$maxvalue) = split('-',$value);
					$sqlwhere .= " AND (iplog.$field > $minvalue AND iplog.$field < $maxvalue)";
					break;
				case 'e':
					$sqlwhere .= " AND iplog.$field = '".addslashes($value)."'";
					break;
				case 'i':
					$sqlwhere .= " AND iplog.$field IN ($value)";
					break;
				case 's':
					$sqlwhere .= " AND iplog.$field LIKE ('".str_replace("*", "%", addslashes($value))."')";
					break;
			}
		}
	}
	$pagenav = paginate('iplog', "iplog.php?cmd=list$link", "WHERE $sqlwhere");
	$logs = $DB_site->query("
		SELECT iplog.*, user.username, user.domain, user.realname
		FROM hive_iplog AS iplog
		LEFT JOIN hive_user AS user USING (userid)
		WHERE $sqlwhere
		ORDER BY $sortby $sortorder
		LIMIT ".($limitlower-1).", $perpage
	");

	$cells = array(
		'Log ID',
		'User (Real Name)',
		'IP',
		'First Used',
		'Last Used',
	);
	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('IP Address Log Entries Found');
		tablehead($cells);
		while ($log = $DB_site->fetch_array($logs)) {
			$cells = array(
				$log['iplogid'],
				'<a href="user.php?cmd=edit&auserid='.$log['userid'].'">'.$log['username'].$log['domain'].'</a><br />('.$log['realname'].')',
				$log['ipaddr'],
				hivedate($log['datefirstseen'], getop('dateformat').' '.getop('timeformat')),
				hivedate($log['datelastseen'], getop('dateformat').' '.getop('timeformat')),
			);
			tablerow($cells, true, false, true);
		}
	}
	tablehead(array("$pagenav&nbsp;"), count($cells));
	endtable();
}

// ############################################################################
// Display some options
if ($cmd == 'intro') {
	$sortBy = array(
		'username' => 'Username',
		'datefirstseen' => 'Date first seen',
		'datelastseen' => 'Date last seen',
		'ipaddr' => 'IP Address'
	);
	startform('iplog.php', 'list');
	starttable('IP Log Browser: Find Entries Where...', '550');
	selectbox('Generated by user:', 'filter[euserid]', $users, -1, 'any user');
	inputfield('IP Address:', 'filter[sipaddr]', '');
	datefield('First used after:<br /><span class="cp_small">Format: yyyy-mm-dd.</span>', 'filter[mdatefirstseen]', $find['mdatefirstseen']);
	datefield('First used before:<br /><span class="cp_small">Format: yyyy-mm-dd.</span>', 'filter[ldatefirstseen]', $find['ldatefirstseen']);
	datefield('Last used after:<br /><span class="cp_small">Format: yyyy-mm-dd.</span>', 'filter[mdatelastseen]', $find['mdatelastseen']);
	datefield('Last used before:<br /><span class="cp_small">Format: yyyy-mm-dd.</span>', 'filter[ldatelastseen]', $find['ldatelastseen']);
	inputfield('Entries to display per page:', 'perpage', '50');
	selectbox('Sort entries by:', 'sortby', $sortBy, 'username', '', '&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('iplog.php', 'doprune');
	starttable('Prune IP Log: Prune Entries Where...', '550');
	selectbox('Generated by user:', 'filter[euserid]', $users, -1, 'any user');
	inputfield('IP Address:', 'filter[sipaddr]', '');
	datefield('First used after:<br /><span class="cp_small">Format: yyyy-mm-dd.</span>', 'filter[mdatefirstseen]', $find['mdatefirstseen']);
	datefield('First used before:<br /><span class="cp_small">Format: yyyy-mm-dd.</span>', 'filter[ldatefirstseen]', $find['ldatefirstseen']);
	datefield('Last used after:<br /><span class="cp_small">Format: yyyy-mm-dd.</span>', 'filter[mdatelastseen]', $find['mdatelastseen']);
	datefield('Last used before:<br /><span class="cp_small">Format: yyyy-mm-dd.</span>', 'filter[ldatelastseen]', $find['ldatelastseen']);
	endform('Prune Log');
	endtable();
}

// ############################################################################
// Prune log
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 'b':
					list($minvalue,$maxvalue) = split('-',$value);
					$sqlwhere .= " AND ($field > $minvalue AND $field < $maxvalue)";
					break;
				case 'e':
					$sqlwhere .= " AND $field = '".addslashes($value)."'";
					break;
				case 'i':
					$sqlwhere .= " AND $field IN ($value)";
					break;
				case 's':
					$sqlwhere .= " AND iplog.$field LIKE ('".str_replace("*", "%", addslashes($value))."')";
					break;
			}
		}
	}

	$logs = $DB_site->query("DELETE FROM hive_iplog".iif($sqlwhere != '1 = 1', " WHERE $sqlwhere"));
	cp_redirect('Selected events have been pruned from the IP log', 'iplog.php');
}

cp_footer(true);
?>