<? require_once("../ihs/config.php"); ?>
<? require_once("top_inc.php"); ?>
<?
	$days = $_POST['days'];
	$button = $_POST['button'];

	if ($days != "") {
		$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");

		// find images added before date selected with zero hits
		$result = mysql_query("SELECT i.filename as filename, i.filepath as filepath, i.tn_filename as tn_filename FROM images i LEFT OUTER JOIN imagehits ih ON i.filename = ih.filename WHERE i.added <= date_add(now(),  interval -" . $days . " day) GROUP BY i.filename HAVING count( ih.filename ) = 0") or die("Query failed");
		$ct1 = mysql_num_rows($result);

		if ($button == "Delete Images") {
			while ($line = mysql_fetch_array($result)) {
				// remove image
				unlink($_SERVER['DOCUMENT_ROOT'] . $line['filepath'] . $line['filename']);
				// remove image from database
				$queryX = "delete from images where filename = '" . $line['filename'] . "'";
				mysql_query($queryX) 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);

		// find images added before date selected with last hit before date selected
		$result = mysql_query("SELECT ih.filename as filename, i.filepath as filepath, i.tn_filename as tn_filename FROM imagehits ih LEFT OUTER JOIN images i ON i.filename = ih.filename where ih.filename not like 'tn_%' and i.filepath is not null and i.tn_filename is not null group by ih.filename having max(ih.timestamp) < date_add(now(),  interval -" . $days . " day)") or die("Query failed");
		$ct2 = mysql_num_rows($result);

		if ($button == "Delete Images") {
			while ($line = mysql_fetch_array($result)) {
				// remove image
				unlink($_SERVER['DOCUMENT_ROOT'] . $line['filepath'] . $line['filename']);
				// remove image from database
				$queryX = "delete from images where filename = '" . $line['filename'] . "'";
				mysql_query($queryX) 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);

		mysql_close($link);
	}
?>
&nbsp;&nbsp;<span class="ctracks">DELETE UNUSED IMAGES</span><br>
<br>
<hr>
<br>
<? if ($days == "" || $button == "Cancel") { ?>
Many images will be uploaded and then never accessed again. Deleting images that have not been viewed can reduce the amount of disk space used.<br>
<br>
<form name="delete" action="delete.php" method="post" style="margin-top: 0px; margin-bottom: 0px;">
Images not viewed in the last <input type="text" name="days" size="3" maxlength="3"> days will be deleted.<br>
<br>
Pressing 'Continue' will show a total count of images that can be deleted. You must confirm the deletion on the next page.<br>
<br>
<input type="submit" name="button" value="Continue"><br>
</form>
<? } else if ($button != "Delete Images") { ?>
<form name="delete" action="delete.php" method="post" style="margin-top: 0px; margin-bottom: 0px;">
There are <?= $ct1 ?> images older then <?= $days ?> days with zero hits.<br>
<br>
There are <?= $ct2 ?> images older then <?= $days ?> days where the last hit was before <?= $days ?> ago.<br>
<br>
<input type="hidden" name="days" value="<?= $days ?>">
<input type="submit" name="button" value="Delete Images" onclick="return verifyAction('Are you sure you want to delete these images?');">&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="button" value="Cancel"><br>
</form>
<? } else { ?>
The images have been removed from the system. This includes file removal and database removal.<br>
<br>
Image statistics for these images have been left in the database.<br>
<? } ?>
<br>
<? require_once("bottom_inc.php"); ?>