aboutsummaryrefslogtreecommitdiffstats
path: root/getzip.php
blob: 7429494766ebf5e58c74ee0c6a7e422e958930a1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php 
require_once("databaseconnect.php");
require_once("logger.php");

set_time_limit(0);

$count = $_POST["count"];
if($count <= 0)
{
	echo "No hashes.";
	exit;
}
$query = "SELECT file,track,artist,album,title,format,sha1 FROM musictags WHERE ";
for($i = 0; $i < $_POST["count"]; $i++)
{
	if($i != 0)
	{
		$query .= " OR ";
	}
	$query .= "sha1 = ".nullString($_POST["hash".$i]);
}
$query = mysql_query($query);
while($row = mysql_fetch_assoc($query))
{
	$rowList[] = $row;
}
@mysql_free_result($query);
if(count($rowList) == 0)
{
	exit;
}
logDownload($rowList, true);

if(@eregi("MSIE", $_SERVER['HTTP_USER_AGENT']) || @eregi("Internet Explorer", $_SERVER['HTTP_USER_AGENT']))
{
	header("Content-Type: application/octet-stream");
}
else
{
	header("Content-Type: application/zip");
}
header('Content-Transfer-Encoding: binary');
header('Content-Disposition: attachment; filename="'.str_replace(" ", "", SITE_NAME)."-Download-".date("Ymd-His").".zip".'"');
$centralDirectory = "";
$offset = 0;
foreach($rowList as $file)
{
	if(!file_exists($file["file"]))
		continue;
	$filename = getPrettyFilename($file);
	echo "\x50\x4b\x03\x04";	// local file header signature
	echo "\x14\x00";		// version needed to extract
	echo "\x00\x00";		// general purpose bit flag
	echo "\x00\x00";		// compression method
	echo "\x00\x00";		// last mod file time
	echo "\x00\x00";		// last mod file date
	$crc32 = hash_file('crc32b', $file["file"]);
	$crc32 = unpack('N', pack('H*', $crc32));
	$crc32 = pack('V', $crc32[1]);
	echo $crc32;			//crc-32
	$size = filesize($file["file"]);
	echo pack('V', $size);		// compressed size
	echo pack('V', $size);		// uncompressed size
	$filenameLength = strlen($filename);
	echo pack('v', $filenameLength);// file name length
	echo "\x00\x00";		// extra field length
	echo $filename;			// file name
	readfile($file["file"]);	// file data
	$centralDirectory .= "\x50\x4b\x01\x02";
	$centralDirectory .= "\x00\x00";		// version made by
	$centralDirectory .= "\x14\x00";		// version needed to extract
	$centralDirectory .= "\x00\x00";		// gen purpose bit flag
	$centralDirectory .= "\x00\x00";		// compression method
	$centralDirectory .= "\x00\x00";		// last mod file time
	$centralDirectory .= "\x00\x00";		// last mod file date
	$centralDirectory .= $crc32;			// crc-32
	$centralDirectory .= pack('V', $size);		// compressed filesize
	$centralDirectory .= pack('V', $size);		// uncompressed filesize
	$centralDirectory .= pack('v', $filenameLength);// length of filename
	$centralDirectory .= "\x00\x00";		// extra field length
	$centralDirectory .= "\x00\x00";		// file comment length
	$centralDirectory .= "\x00\x00";		// disk number start
	$centralDirectory .= "\x00\x00";		// internal file attributes
	$centralDirectory .= "\x20\x00\x00\x00";	// external file attributes - 'archive' bit set (32)
	$centralDirectory .= pack('V', $offset);	// relative offset of local header
        $offset += 30 + $filenameLength + $size;
	$centralDirectory .= $filename;
}
echo $centralDirectory;		// central directory
echo "\x50\x4b\x05\x06";	// end of central directory signature	
echo "\x00\x00";		// number of this disk
echo "\x00\x00";		// number of the disk with the start of the central directory
echo pack('v', sizeof($rowList)); // number of entries on disk
echo pack('v', sizeof($rowList)); // number of entries
echo pack('V', strlen($centralDirectory)); // size of central directory
echo pack('V', $offset);	// offset to start of central directory
echo "\x00\x00";		// zip comment size
?>