-
Notifications
You must be signed in to change notification settings - Fork 1
/
datapack-archive.php
130 lines (123 loc) · 3.32 KB
/
datapack-archive.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
$cachebasepath='/tmp/';
$cache=true;
function filewrite($file,$content)
{
if($filecurs=fopen($file, 'w'))
{
if(fwrite($filecurs,$content) === false)
die('Unable to write the file: '.$file);
fclose($filecurs);
}
else
die('Unable to write or create the file: '.$file);
}
if(!is_dir($cachebasepath))
mkdir($cachebasepath,0755,true);
if(!isset($_GET['main']))
{
$folder='datapack/';
$cachetar=$cachebasepath.'datapack.tar';
$cachetarxz=$cachetar.'.zst';
}
else
{
if(!preg_match('#^[a-z0-9]+$#',$_GET['main']))
die('main with wrong char');
if(!isset($_GET['sub']))
{
$folder='datapack/map/main/'.$_GET['main'].'/';
$cachetar=$cachebasepath.'datapack-main-'.$_GET['main'].'.tar';
$cachetarxz=$cachetar.'.zst';
}
else
{
if(!preg_match('#^[a-z0-9]+$#',$_GET['sub']))
die('sub with wrong char: "'.$_GET['sub'].'"');
$folder='datapack/map/main/'.$_GET['main'].'/sub/'.$_GET['sub'].'/';
$cachetar=$cachebasepath.'datapack-sub-'.$_GET['main'].'-'.$_GET['sub'].'.tar';
$cachetarxz=$cachetar.'.zst';
}
}
if(isset($argv))
foreach ($argv as $arg)
{
$e=explode("=",$arg);
if(count($e)==2)
$_GET[$e[0]]=$e[1];
else
$_GET[$e[0]]=0;
}
if($cache)
{
if(file_exists($cachetar))
{
if(filemtime($cachetar)<=time())
{
if(filemtime($cachetar)>(time()-5*60))
{
header('Content-type: application/x-xz');
header('From-cache: yes');
echo file_get_contents($cachetarxz);
exit;
}
else
header('From-cache: too old: '.filemtime($cachetar).'>'.(time()-5*60).' for '.$cachetar);
}
else
header('From-cache: into the future');
}
else
header('From-cache: no file');
}
else
header('From-cache: not probed');
$file = fopen("/tmp/cc-gen-tar56465","w+");
if(flock($file,LOCK_EX))
{
if($cache)
{
if(file_exists($cachetar))
{
if(filemtime($cachetar)<=time())
{
if(filemtime($cachetar)>(time()-5*60))
{
header('Content-type: application/x-xz');
header('From-cache: yes');
echo file_get_contents($cachetarxz);
flock($file,LOCK_UN);
fclose($file);
exit;
}
else
header('From-cache: too old: '.filemtime($cachetar).'>'.(time()-5*60).' for '.$cachetar);
}
else
header('From-cache: into the future');
}
else
header('From-cache: no file');
}
else
header('From-cache: not probed');
ob_start();
@exec('./datapack-archive.sh',$output);
ob_end_flush();
flock($file,LOCK_UN);
fclose($file);
}
else
die('Error locking file!');
header('Content-type: application/x-xz');
if($cachetarxz<3*1024*1024)
filewrite('/tmp/bug2',implode("\n",$output));
else if(count($output)>0)
filewrite('/tmp/bug3',implode("\n",$output));
if(file_exists($cachetarxz))
echo file_get_contents($cachetarxz);
else
{
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found", true, 404);
die();
}