-
Notifications
You must be signed in to change notification settings - Fork 1
/
dldata.php
70 lines (58 loc) · 1.96 KB
/
dldata.php
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
<?php
require_once('inc_lib.php');
$dldata=array();
$dldecode=array();
$dlname=array();
$dlfolder=array();
foreach ($_REQUEST as $key=>$val)
{
if (preg_match("{dldata([0-9]*)}",$key,$matches)) $dldata[$matches[1]+0]=$val;
if (preg_match("{dldecode([0-9]*)}",$key,$matches)) $dldecode[$matches[1]+0]=$val;
if (preg_match("{dlname([0-9]*)}",$key,$matches)) $dlname[$matches[1]+0]=$val;
if (preg_match("{dlfolder([0-9]*)}",$key,$matches)) $dlfolder[$matches[1]+0]=$val;
}
if (count($dldecode)>0)
foreach($dldecode as $key=>$val) if ($val=='true')
{
$dldata[$key]=base64_decode(preg_replace("{^[^,]+,}","",$dldata[$key]));
}
// ------------------------------------------------------------
// one file is sent as is
if (count($dlname)==1)
{
$name=reset($dlname);
$data=reset($dldata);
if ($name!='') sendDownloadHeaders($name);
echo $data;
die();
}
// ------------------------------------------------------------
// multiple files are zipped and sent together
// optionally put in subfolders
// create empty unique temporary directory
// we assume that the full path doesn't contain any quotes character " later
$dir=getcwd()."/tmp/".time();
while (!mkdir($dir)) $dir.="x";
$target=$dir.".out";
function cleanup()
{
global $dir;
global $target;
shell_exec("rm -Rf \"$dir\"");
unlink($target);
rmdir($dir);
}
register_shutdown_function('cleanup');
foreach($dlname as $key=>$name)
{
$name=str_replace("/","_",$name);
$subdir=str_replace("/","_",$dlfolder[$key]);
if ($subdir!='') @mkdir($dir."/".$subdir);
file_put_contents($dir."/".($subdir!=''?$subdir."/":"").$name,$dldata[$key]);
}
shell_exec("cd \"$dir\" && zip -1 -r \"$target\" .");
sendDownloadHeaders($_REQUEST['zipname']."_".date("Y-m-d_H-i-s").".zip");
readfile($target);
// cleanup is called automatically
exit;
?>