<?php
// Cancel out the default layout.
$t->assign("pgLayout", "don't use one");

function addFolderToZip(&$zip, $path, $rootPath) {
	echo "path = $path".BR;
	echo "rootPath = $rootPath".BR;
	return;
	
	if (!$fp = @opendir($path))
		return false;
	
	while ($file = @readdir($fp)) {
		if (is_dir($path.$file))
			; //addFolderToZip($zip, $path.$file, $rootPath);
		else
			addFileToZip($zip, $path.$file, $rootPath);
		
	}
	closedir($fp);
}

function addFileToZip(&$zip, $path, $rootPath = "") {
	echo "ADDING FILE...".BR."path = $path".BR;
	if ($rootPath) {
		//echo('$zipFileName = preg_replace("/^'.preg_replace("/([\/\'\"\.])/i", "\\\\$1", $rootPath).'/", "", $path);').BR;
		eval('$zipFileName = preg_replace("/^'.preg_replace("/([\/\'\"\.])/i", "\\\\$1", $rootPath).'/", "", $path);');
	} else {
		$zipFileName = $path;
	}
	
	$fp = fopen($path, 'r');
	$curData = fread($fp, filesize($path));
	fclose($fp);
	//echo "zipFileName = $zipFileName".BR;
	//echo "rootPath = $rootPath".BR;
	//echo "path = $path".BR;
	$zip->addFile($curData, $zipFileName);

}

if (!$_REQUEST['fileList']) {
	die('No files specified.');
}

$files = split(';;', urldecode($_REQUEST['fileList']));

$folder = array_pop($files);

display($files);

if (!file_exists($folder) || !is_dir($folder)) {
	die ('Error: Invalid folder passed.');
}

if (!$folderId = $db->query("SELECT id FROM _folders WHERE path='$folder'", DB_VAL)) {
	die ('Error: No rules for folder.');
}

// Replace .'s in folder path for root
$folder = preg_replace("/^[\.]{0,1}[\/]?$/", "root/", $folder);

if (!$oAcm->checkAccess('view', '_folders', $folderId)) {
	die ('Error: Access denied.');
}

if (!$c['availableModules']['zipfile']) { // If zip class is not available, just download first file
	$temp = $files;
	unset($files);
	$files = array($temp[0]);	
} else {
	include_once($c['availableModules']['zipfile']);
}

if (count($files) > 1) {
	// Create a zip file...
	$zipFile = new Zipfile();
	// Add the base files to zip, if a folder, call addFolderToZip();
	// Looping through is neccessary (versus just zipping the folder)
	//  because the entire folder contents may not have been requested
	foreach($files as $file) {
		if (!(file_exists($folder.$file) || is_dir($file))) {
			continue;
		}
		echo "file = $file".BR;
		if (is_dir($file)) {
			echo "is dir...".BR;
			addFolderToZip($zipFile, $file, $folder);
			continue;
		}
		addFileToZip($zipFile, $folder.$file, $folder);
		//$fp = fopen($folder.$file, 'r');
		//$curData = fread($fp, filesize($folder.$file));
		//fclose($fp);
		//$zipFile->addFile($curData, $file);
	}
	$fileData = $zipFile->file();	
	// Replaces .'s with -'s and /'s with _'s in path
	$fileName = str_replace('/', '_', str_replace('.', '-', $folder)).'files.zip';
} else {
	$file = array_pop($files);
	if (!file_exists($folder.$file))
		die('Error: File does not exist.');
	$fp = fopen($folder.$file, 'r');
	//while ($curData .= fgets($fp)) {
	//	;
	//}
	$fileData = fread($fp, filesize($folder.$file));
	//$fileData = $curData;
	$fileName = $file;
	fclose($fp);
}

unset($curData);

exit();
header('Cache-Control: public'); // no-cache, must-revalidate');
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$fileName."\"");


echo $fileData;

exit();
?>
