<?
ob_end_clean();

if (!$permissions['download']) {
?>
<script language="JavaScript1.2" type="text/javascript">
<?echo iframeAlert($lang->get("You are not allowed to download files"));?>
</script>
<?
	exit();
}

if ($sys['path']) {
	$fromfolder	= $fm->dirname($str->fromHTML($sys['path']));
	$filename = $fm->basename($str->fromHTML($sys['path']));
} else {
	$fromfolder	= $str->fromHTML($_GET['fromfolder']);
	$filename = $str->fromHTML($_GET['filename']);
}

$base_dir = gluePath($permissions['path'], $fromfolder);
$path = gluePath($base_dir, $filename);


/*
$relativePath = gluePath($fromfolder, $filename);

$imageSizes = @getimagesize($path);

if ($img->isImage($filename)) {
?>
<script>
parent.popup('imageviewer.php?path=<?echo $str->safeJS($str->forURL($fromfolder))?>&file=<?echo $str->safeJS($str->forURL($filename))?>', 'imageviewer', <?echo $imageSizes[1]+40?>, <?echo $imageSizes[0]+40?>, 'yes');
</script>
<?
exit();
}
*/


if (!isCleanPath($fromfolder)) {
?>
<script language="JavaScript1.2" type="text/javascript">
<?echo iframeAlert($lang->get("Ilegal path"));?>
</script>
<?
	exit();
}


if (!isCleanFilename($filename)) {
?>
<script language="JavaScript1.2" type="text/javascript">
<?echo iframeAlert($lang->get("Ilegal filename"));?>
</script>
<?
	exit();
}

if ($permissions['unlimitedTraffic'] || $quota->canAddTraffic()) {
	if (isCleanFilename($filename)) {
		if (@file_exists($path)) {
			$filesize = $fm->getFileSize($path);
			$fm->sendHTTPFile($path);
			$quota->addTraffic($filesize);
			
			$actionInfo = "";
			$actionInfo .= "File name: " . $filename ."\n";
			$actionInfo .= "File size: " . $fm->formatFileSize($filesize) ."\n";
			$logInfo = $actionInfo;
			$logInfo .= "Path: " . $path ."\n";
			$log->act("0", "download", $logInfo);
			
			/* send email notice to admin */
			if ($options['downloadNotice'] && $config->settings['adminemail']) {
				$mail = new htmlMimeMail();
				$message = "\n";
				$message .= "User \"".$auth->currentUserInfo['username']."\" downloaded:\n";
				$message .= "Date: " . date("n/d/Y H:i") ."\n";
				$message .= $actionInfo;
				$message .= "Folder: " . $fromfolder ."\n";
				
				$mail->setText($message);
				$customFrom = "";
				if ($customFrom) {
					$mail->setFrom($customFrom);
				} else {
					$mail->setFrom(($sys['license']['unbranded']?$sys['license']['unbranded']:"Afian"));
				}
				$mail->setSubject("User ".$auth->currentUserInfo['username']." downloaded a file.");
				$result = @$mail->send(array($config->settings['adminemail']));
			}
			
		} else {
		?>
		<script language="JavaScript1.2" type="text/javascript">
		<?echo iframeAlert($lang->get("Download: '%1' not available", array(gluePath($fromfolder, $filename))));?>
		</script>
		<?
		}
	} else {
		//bad filename
	}
} else {
?>
<script language="JavaScript1.2" type="text/javascript">
<?echo iframeAlert($lang->get("Download: traffic quota excedeed"));?>
</script>
<?
}
exit();
?>