<?php
//revision 1.2

define('IN_COPPERMINE', true);
define('LOGIN_PHP', true);

require('include/init.inc.php');
include ('include/config.inc.php');

/* Write here the name of the folder where the zip will be created */
$sFolderZip = "ziptmp/";

 /* Begin Auto déléstage - Auto Delete stored zip files 
// Comment out between lines if for some reason have problems  with this
// This will delete any files from the folder specified above that have a file creation date older than "today". */

$dir = opendir($sFolderZip);
$seconds_old = 3600;
while($filetodel = readdir($dir)) {
	if(filemtime($sFolderZip.$filetodel) < (time()-$seconds_old)){
	if (!is_dir($sFolderZip.$filetodel)) unlink($sFolderZip.$filetodel);
	}
}
closedir($dir);
/* End Auto déléstage - Auto Delete stored zip files */
    
/* Connecting to the server */
$db = @mysql_connect($CONFIG['dbserver'], $CONFIG['dbuser'], $CONFIG['dbpass'])
   or die (mysql_error());

/* Selecting the database */
$db_check = @mysql_select_db($CONFIG['dbname']);

$aid = (int) $_GET['aid'];

/* Begin security mod by Erostew */
/* Checking to see if the album is private or not */
$sql = 'SELECT visibility FROM `'.$CONFIG['TABLE_PREFIX'].'albums` where `aid` = ' . $aid;
$result = mysql_query($sql) OR die(mysql_error());
$priv = mysql_fetch_assoc($result);
/* updated Check up by FireBird2003 {*/
$sql = 'SELECT user_group_list FROM `'.$CONFIG['TABLE_PREFIX'].'users` where `user_id` = ' . USER_ID;
$result = mysql_query($sql) OR die(mysql_error());
$priv2 = mysql_fetch_assoc($result);
$user_group_list = split ("[, ]", $priv2["user_group_list"]);
if (($priv["visibility"] != '0') && (!in_array($priv["visibility"], $user_group_list))){
/* } // updated Check up by FireBird2003 */ 
// someone has entered the url manually, But the album is private

die("<h2><font color=red>You don't have permission to do that!</font></h2>");
} else {

// album is public go ahead 
/* End security mod by Erostew */

/* Retrieving pictures list */
$sql = 'SELECT filepath, filename FROM `'.$CONFIG['TABLE_PREFIX'].'pictures` where `aid` = ' . $aid;
$result = mysql_query($sql) OR die(mysql_error());
$bilder = "";
while ($row = mysql_fetch_assoc($result))
   $bilder .= "albums/" . $row['filepath'].$row['filename'].",";

/* Retrieving album name */
$sql = 'SELECT title FROM `'.$CONFIG['TABLE_PREFIX'].'albums` where `aid` = ' . $aid;
$result = mysql_query($sql) OR die(mysql_error());
$o_AlbumName = mysql_fetch_object($result);
$sDesiredZipName = urlencode($o_AlbumName->title);

/* Creating the zip file */
include_once('pclzip.lib.php');
$archive = new PclZip($sFolderZip.$sDesiredZipName . '.zip');
$v_list = $archive->create($bilder,PCLZIP_OPT_REMOVE_ALL_PATH); //Changed to remove path from zip. No need to have the files 3 or 4 folders deep.
if ($v_list == 0)
	die("Error : ".$archive->errorInfo(true));


$file = $sFolderZip.$sDesiredZipName.".zip";
$filename = basename($file);
$size = filesize($file);
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=".$filename);
header("Content-Length:".$size);
readfile($file);

}

?>
