-
Notifications
You must be signed in to change notification settings - Fork 0
/
tardir.php
273 lines (256 loc) · 8.49 KB
/
tardir.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?PHP
/**
* @author Jari Pennanen
* @license MIT License
*
* Tar balls a current directory and sends to php://output
*
* If given $_GET["dir"] it only tarballs that directory
*
* Repository: https://github.com/Ciantic/archive-to-output-stream/
*/
$allow_root_tar = true;
if (empty($_GET["dir"])) {
if (!$allow_root_tar) {
die("give ?dir=DIRECTORY_NAME_HERE");
}
$the_directory = ".";
// Some fake filename from the domain and url
$fake_file_name = str_replace("_tardir_php", "",
preg_replace("#[^a-zA-Z0-9]#", "_",
"{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}") . ".tar"
);
} else {
// Sanitize . and / and \ away
$the_directory = preg_replace("#[./\\\]*#", "", $_GET["dir"]);
$fake_file_name = $_GET["dir"] . '.tar';
if (!$the_directory || !file_exists($the_directory)) {
die("Directory '$the_directory' not found or given in incorrect format");
}
}
// Open the directory for recursive iteration
$dirIterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($the_directory, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST
);
// Open the tar ball
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $fake_file_name);
$tar = new Tar("php://output");
foreach ($dirIterator as $path => $splFileInfo) {
if ($splFileInfo->isFile()) {
// Remove "." directory from tar ball when using root tars
if ($the_directory === "." && strpos($path, "./") === 0) {
$path = substr($path, 2);
}
// Add file to tarball
$tar->addFile($path, $splFileInfo);
}
}
$tar->close();
return;
/**
* Tar ball class
*
* @author Jari Pennanen
* @license MIT License
*
* Basically stripped the TAR parts from a Andreas Gohr's PHP Archive:
* https://github.com/splitbrain/php-archive and made to work directly on
* php://output so that it does not need to use memory besides the buffer size,
* the hardest work was already done by Andreas. I only cleaned the unneeded
* parts away and converted to use just SplFileInfo.
*/
class Tar {
protected $file = '';
protected $fh;
protected $closed = true;
protected $writeaccess = false;
/**
* @param string $file
*/
public function __construct($file = '')
{
$this->file = $file;
$this->fh = 0;
if ($this->file) {
$this->fh = @fopen($this->file, 'wb');
if (!$this->fh) {
throw new Exception('Could not open file for writing: '.$this->file);
}
}
$this->writeaccess = true;
$this->closed = false;
}
/**
* Add a file to the current TAR archive using an existing file in the
* filesystem
*
* @param string $file path to the original file (and the name of the tar
* file)
* @param string|SplFileInfo $splFileinfo either the name to us in archive
* (string) or a FileInfo oject with all meta data, empty to take from
* original
*/
public function addFile($file, $splFileinfo = '')
{
if (is_string($splFileinfo)) {
$splFileinfo = new SplFileInfo($file);
}
if ($this->closed) {
throw new Exception('Archive has been closed, files can no longer be added');
}
$fp = @fopen($splFileinfo->getPathname(), 'rb');
if (!$fp) {
throw new Exception('Could not open file for reading: ' . $splFileinfo->getPathname());
}
// create file header
$this->writeFileHeader($file, $splFileinfo);
// write data
$read = 0;
while (!feof($fp)) {
$data = fread($fp, 512);
$read += strlen($data);
if ($data === false) {
break;
}
if ($data === '') {
break;
}
$packed = pack("a512", $data);
$this->writebytes($packed);
}
fclose($fp);
if ($read != $splFileinfo->getSize()) {
$this->close();
throw new Exception("The size of $file changed while reading" .
", archive corrupted. read $read expected ".$splFileinfo->getSize());
}
}
/**
* Add a file to the current TAR archive using the given $data as content
*
* @param string $data binary content of the file to add
* @param int $uid
* @param int $gid
* @param int $perm
* @param int $size
* @param int $mtime
* @throws ArchiveIOException
*/
public function addData($data, $name, $uid, $gid, $perm, $mtime, $typeflag)
{
if ($this->closed) {
throw new Exception('Archive has been closed, files can no longer be added');
}
$size = strlen($data);
$this->writeRawFileHeader($name, $uid, $gid, $perm, $size, $mtime, $typeflag);
for ($s = 0; $s < $len; $s += 512) {
$this->writebytes(pack("a512", substr($data, $s, 512)));
}
}
public function close()
{
if ($this->closed) {
return;
} // we did this already
// write footer
if ($this->writeaccess) {
$this->writebytes(pack("a512", ""));
$this->writebytes(pack("a512", ""));
}
// close file handles
if ($this->file) {
fclose($this->fh);
$this->file = '';
$this->fh = 0;
}
$this->writeaccess = false;
$this->closed = true;
}
/**
* Write to the open filepointer or memory
*
* @param string $data
* @return int number of bytes written
*/
protected function writebytes($data)
{
$written = @fwrite($this->fh, $data);
if ($written === false) {
throw new Exception('Failed to write to archive stream');
}
return $written;
}
/**
* Write the given file meta data as header
*
* @param string $filePath
* @param SplFileInfo $fileinfo
* @throws ArchiveIOException
*/
protected function writeFileHeader($filePath, SplFileInfo $fileinfo)
{
$this->writeRawFileHeader(
$filePath,
$fileinfo->getOwner(),
$fileinfo->getGroup(),
$fileinfo->getPerms(),
$fileinfo->getSize(),
$fileinfo->getMTime(),
$fileinfo->isDir() ? '5' : '0'
);
}
/**
* Write a file header to the stream
*
* @param string $name
* @param int $uid
* @param int $gid
* @param int $perm
* @param int $size
* @param int $mtime
* @param string $typeflag Set to '5' for directories
* @throws ArchiveIOException
*/
protected function writeRawFileHeader($name, $uid, $gid, $perm, $size, $mtime, $typeflag = '')
{
// handle filename length restrictions
$prefix = '';
$namelen = strlen($name);
if ($namelen > 100) {
$file = basename($name);
$dir = dirname($name);
if (strlen($file) > 100 || strlen($dir) > 155) {
// we're still too large, let's use GNU longlink
$this->writeRawFileHeader('././@LongLink', 0, 0, 0, $namelen, 0, 'L');
for ($s = 0; $s < $namelen; $s += 512) {
$this->writebytes(pack("a512", substr($name, $s, 512)));
}
$name = substr($name, 0, 100); // cut off name
} else {
// we're fine when splitting, use POSIX ustar
$prefix = $dir;
$name = $file;
}
}
// values are needed in octal
$uid = sprintf("%6s ", decoct($uid));
$gid = sprintf("%6s ", decoct($gid));
$perm = sprintf("%6s ", decoct($perm));
$size = sprintf("%11s ", decoct($size));
$mtime = sprintf("%11s", decoct($mtime));
$data_first = pack("a100a8a8a8a12A12", $name, $perm, $uid, $gid, $size, $mtime);
$data_last = pack("a1a100a6a2a32a32a8a8a155a12", $typeflag, '', 'ustar', '', '', '', '', '', $prefix, "");
for ($i = 0, $chks = 0; $i < 148; $i++) {
$chks += ord($data_first[$i]);
}
for ($i = 156, $chks += 256, $j = 0; $i < 512; $i++, $j++) {
$chks += ord($data_last[$j]);
}
$this->writebytes($data_first);
$chks = pack("a8", sprintf("%6s ", decoct($chks)));
$this->writebytes($chks.$data_last);
}
}