<?
ob_end_clean();


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



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



/* no item selected */
if ($_POST['itemlist'] == "") {
	iframeAlert($lang->get('Empty file list.'));
	echo "parent.MyFilesEndAction(tb);</script>";
	exit();
}

/* checking input */
$split = explode("|", $str->fromHTML($_POST['itemlist']));
for ($i = 0 ; $i < sizeof($split) ; $i++) {
	if ($split[$i] != "") {
		$items[] = $split[$i];
	}
}

foreach ($items as $item) {
	if (!isCleanFilename($item)) {
	iframeAlert($lang->get('Ilegal filename: ').$item);
	echo "parent.MyFilesEndAction(tb);</script>";
	exit();
	}
}


if ($permissions['unlimitedTraffic'] || $quota->canAddTraffic()) {} else {
?>
<script language="JavaScript1.2" type="text/javascript">
<?echo iframeAlert("Download: traffic quota excedeed");?>
</script>
<?
}

$archName = microtime().".tmp.zip";
$base_dir = gluePath($permissions['path'], $fromfolder);
$archPath = gluePath($sys['root'], "/system/data/temp/", $archName);

$zip = new PclZip($archPath);

$logText = "";
foreach ($items as $filename) {
$path = gluePath($base_dir, $filename);
$zip->add($path, PCLZIP_OPT_REMOVE_PATH, gluePath($base_dir,"/"));
$logText .= $path."\r\n";
}


$filesize = $fm->getFileSize($archPath);
function rsf() {
	global $archPath;
	if (is_file($archPath)) {
	unlink($archPath);
	}
}
register_shutdown_function("rsf");
$fm->sendHTTPFile($archPath, $str->shorten(implode(", ",$items), 40, $symbol = "[...]").".zip");
$quota->addTraffic($filesize);
$log->act("0", "download", $logText);

if ($options['downloadNotice'] && $config->settings['adminemail']) {
	$mail = new htmlMimeMail();
	$message = "\n";
	$message .= "User \"".$auth->currentUserInfo['username']."\" downloaded:\n";
	$message .= "Date: ".date("d/n/Y H:i")."\n";
	$message .= "File: ".$path."\n";
	$message .= "Filesize: " . $fm->formatFileSize($filesize) ."\n";
	$mail->setText($message);
	$mail->setFrom(($sys['license']['unbranded']?$sys['license']['unbranded']:"Afian"));
	$mail->setSubject($auth->currentUserInfo['username']." downloaded");
	$result = @$mail->send(array($config->settings['adminemail']));
}

exit();

?>