<? require_once("../ihs/config.php"); ?>
<? require_once("top_inc.php"); ?>
<?
	$link = mysql_connect($db_server, $db_user, $db_password) or die("Could not connect.");
	mysql_select_db($db_name) or die("Could not select database.");

	// delete selected images, if delete button clicked
	$btn = $_REQUEST['btn'];
	if ($btn == "delete") {
		$deleteList = $_REQUEST['deleteFN'];
		foreach ($deleteList as $fn) {
			$query = "SELECT filename, tn_filename, filepath from images where filename = '$fn'";
    		$result = mysql_query($query) or die("Query failed.");
    		while ($line = mysql_fetch_array($result)) {
				// remove image
				unlink($_SERVER['DOCUMENT_ROOT'] . $line[filepath] . $line[filename]);
				// remove image from database
				$query2 = "delete from images where filename = '" . $line[filename] . "'";
				mysql_query($query2) or die("Could not delete.");
				$query2 = "delete from imagehits where filename = '" . $line[filename] . "'";
				mysql_query($query2) or die("Could not delete.");
				// remove image thumbnail
				if ($line[tn_filename] != "") {
					unlink($_SERVER['DOCUMENT_ROOT'] . $line[filepath] . $line[tn_filename]);
    			}
			}
			mysql_free_result($result);
		}
	}

	$limit = $_REQUEST['limit'];
	if ($limit == "") {
		$limit = 25;
	}
	$start = $_REQUEST['start'];
	if ($start == "") {
		$start = 0;
	}
	$sort = $_REQUEST['sort'];
	if ($sort == "") {
		$sort = "added";
	}
	$tn = $_REQUEST['tn'];
	if ($tn == "") {
		$tn = 0;
	}
	$details = $_REQUEST['details'];
	if ($details == "") {
		$details = 1;
	}

	$totalcount = 0;
	$query = "SELECT count(filename) as ct from images";
    $result = mysql_query($query) or die("Query failed.");
	while ($line = mysql_fetch_array($result)) {
			$totalcount = $line[ct];
	}
	mysql_free_result($result);

	$displaystart = $start + 1;
	$displayend = ($start + $limit > $totalcount ? $totalcount : $start + $limit);
	if ($displayend == $totalcount) {
		$nextset = 0;
	} else {
		$nextset = ($totalcount - $displayend > $limit ? $limit: ($totalcount - $displayend));
	}

	if ($details) {
		$query = "select i.filename as filename, i.tn_filename as tn_filename, i.filepath as filepath, unix_timestamp(i.added) as add_dt, i.email as email, i.ip as ip, i.filesize as filesize, sum(ih.kb) as bandwidth, count( ih.filename ) as ctr from images i left outer join imagehits ih on i.filename = ih.filename group by filename order by " . $sort . " desc limit " . $start . "," . $limit;
	} else {
		$query = "select i.filename as filename, i.tn_filename as tn_filename, i.filepath as filepath from images i left outer join imagehits ih on i.filename = ih.filename group by filename order by " . $sort . " desc limit " . $start . "," . $limit;
	}
    $result = mysql_query($query) or die("Query failed.");
?>
&nbsp;&nbsp;<span class="ctracks">IMAGE MANAGEMENT</span><br>
<br>
<hr>
<br>
<form name="images" action="<?= $_SERVER['PHP_SELF'] ?>" method="post" style="margin-bottom: 0px;">
<div align="center" style="color: #000080;">Images per page: <input type="text" name="limit" value="<?= $limit ?>" size="3" maxlength="4">&nbsp;&nbsp;&nbsp;&nbsp;Sort by: <select name="sort" size="1"><option value="filename" <?= ($sort == "filename" ? "selected" : "") ?>>Image Name</option><option value="added" <?= ($sort == "added" ? "selected" : "") ?>>Date/Time Added</option><option value="email" <?= ($sort == "email" ? "selected" : "") ?>>E-mail Address</option><option value="ip" <?= ($sort == "ip" ? "selected" : "") ?>>IP Address</option><option value="filesize" <?= ($sort == "filesize" ? "selected" : "") ?>>File Size</option><option value="ctr" <?= ($sort == "ctr" ? "selected" : "") ?>>Views</option><option value="bandwidth" <?= ($sort == "bandwidth" ? "selected" : "") ?>>Bandwidth</option></select>&nbsp;&nbsp;&nbsp;&nbsp;View Thumbnails: <select name="tn" size="1"><option value="1" <?= ($tn == "1" ? "selected" : "") ?>>Yes</option><option value="0" <?= ($tn == "0" ? "selected" : "") ?>>No</option></select>&nbsp;&nbsp;&nbsp;&nbsp;View Details: <select name="details" size="1"><option value="1" <?= ($details == "1" ? "selected" : "") ?>>Yes</option><option value="0" <?= ($details == "0" ? "selected" : "") ?>>No</option></select>&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="btn" value="view" style="font-size: smaller;"></div>
<br>
<br>
<table border="0" width="100%" cellpadding="2" cellspacing="0">
	<tr>
		<td width="20%" valign="top" align="left" nowrap>&nbsp;</td>
		<td width="60%" valign="top" align="center" nowrap><strong>Displaying images <?= $displaystart ?> to <?= $displayend ?> of <?= $totalcount ?>.</strong></td>
		<td width="20%" valign="top" align="right" nowrap><? if ($start > 0) { ?><a style="color: #0000cd;" href="<?= $PHP_SELF ?>?limit=<?= $limit ?>&sort=<?= $sort ?>&start=<?= $start - $limit?>&tn=<?= $tn ?>&details=<?= $details ?>">Previous <?= $limit ?></a> - <? } ?><? if ($nextset > 0) { ?><a style="color: #0000cd;" href="<?= $PHP_SELF ?>?limit=<?= $limit ?>&sort=<?= $sort ?>&start=<?= $start + $limit?>&tn=<?= $tn ?>&details=<?= $details ?>">Next <?= $nextset ?></a><? } ?></td>
	</tr>
</table>
<? if ($details) { ?>
<table border="0" width="100%" cellpadding="2" cellspacing="0">
	<tr bgcolor="#dcdcdc">
		<td align="left" class="cell">&nbsp;</td>
		<td align="left" class="cell"><strong>Image Name</strong></td>
		<? if ($tn) { ?><td align="left" class="cell"><strong>Thumbnail</strong></td><? } ?>
		<td align="left" class="cell"><strong>Date/Time Added</strong></td>
		<td align="left" class="cell"><strong>E-mail</strong></td>
		<td align="left" class="cell"><strong>IP Address</strong></td>
		<td align="right" class="cell"><strong>File Size</strong></td>
		<td align="right" class="cell"><strong>Views</strong></td>
		<td align="right" class="cell"><strong>Bandwidth Used</strong></td>
	</tr>
<? while ($line = mysql_fetch_array($result)) { ?>
	<tr>
		<td align="left" class="cell"><input type="checkbox" name="deleteFN[]" value="<?= $line[filename] ?>"></td>
		<td align="left" class="cell"><a style="color:#0000cd;" href="view.php?limit=<?= $limit ?>&sort=<?= $sort ?>&start=<?= $start ?>&tn=<?= $tn ?>&filename=<?= $line[filename] ?>"><?= $line[filename] ?></a>&nbsp;</td>
		<? if ($tn) { ?><td align="left" class="cell"><img src="<?= $line[filepath] . $line[tn_filename] ?>" border="0"></td><? } ?>
		<td align="left" class="cell"><?= date("m/d/Y h:i:s A", $line[add_dt]) ?>&nbsp;</td>
		<td align="left" class="cell"><?= $line[email] ?>&nbsp;</td>
		<td align="left" class="cell"><?= $line[ip] ?>&nbsp;</td>
		<td align="right" class="cell"><?= number_format($line[filesize]) ?>&nbsp;</td>
		<td align="right" class="cell"><a href="referers.php?limit=<?= $limit ?>&sort=<?= $sort ?>&start=<?= $start ?>&tn=<?= $tn ?>&details=<?= $details ?>&filename=<?= $line[filename] ?>"><?= number_format($line[ctr]) ?></a>&nbsp;</td>
		<td align="right" class="cell"><?= number_format($line[bandwidth]) ?>&nbsp;</td>
	</tr>
<? } ?>
</table>
<? } else { ?>
<table border="1" width="100%" cellpadding="2" cellspacing="0">
	<tr>
<?
		$ctr = 0;
		while ($line = mysql_fetch_array($result)) {
			$ctr++;
?>
			<td valign="bottom" align="center"><a href="view.php?limit=<?= $limit ?>&sort=<?= $sort ?>&start=<?= $start ?>&tn=<?= $tn ?>&details=<?= $details ?>&filename=<?= $line[filename] ?>"><img src="<?= $line[filepath] . $line[tn_filename] ?>" border="0"></a><br><input type="checkbox" name="deleteFN[]" value="<?= $line[filename] ?>"></td>
<?
			if ($ctr % 5 == 0) {
?>
				</tr>
				<tr>
<?
			}
		}
?>
	</tr>
</table>
<? } ?>
<?
    mysql_free_result($result);
	mysql_close($link);
?>
<table border="0" width="100%" cellpadding="2" cellspacing="0">
	<tr bgcolor="#dcdcdc">
		<td align="center" class="cell"><input type="submit" name="btn" value="delete" onclick="return verifyAction('Are you sure you want to delete the selected images?');"></td>
	</tr>
</form>
<br>
<br>
<? require_once("bottom_inc.php"); ?>