1<?php
2
3date_default_timezone_set('UTC');
4
5$manifest = (binary)'';
6$glags = 0;
7
8foreach($files as $name => $cont)
9{
10    global $gflags, $files;
11
12    $comp = NULL;
13    $crc32= NULL;
14    $clen = NULL;
15    $ulen = NULL;
16    $time = isset($ftime) ? $ftime : @mktime(12, 0, 0, 3, 1, 2006);
17    $flags= 0;
18    $perm = 0x000001B6;
19    $meta = NULL;
20
21    // overwrite if array
22    if (is_array($cont))
23    {
24        foreach(array('comp','crc32','clen','ulen','time','flags','perm','meta','cont') as $what)
25        {
26            if (isset($cont[$what]))
27            {
28                $$what = $cont[$what];
29            }
30        }
31    }
32
33    // create if not yet done
34    if (empty($comp)) $comp = $cont;
35    if (empty($ulen)) $ulen = strlen($cont);
36    if (empty($clen)) $clen = strlen($comp);
37    if (empty($crc32))$crc32= crc32((binary)$cont);
38    if (isset($meta)) $meta = serialize($meta);
39
40    // write manifest entry
41    $manifest .= pack('V', strlen($name)) . (binary)$name;
42    $manifest .= pack('VVVVVV', $ulen, $time, $clen, $crc32, $flags|$perm, strlen($meta)) . (binary)$meta;
43
44    // globals
45    $gflags |= $flags;
46    $files[$name] = $comp;
47}
48
49if (!isset($alias)) $alias = 'hio';
50
51if (isset($pmeta)) $pmeta = serialize($pmeta); else $pmeta = '';
52
53$manifest = pack('VnVV', count($files), isset($hasdir) ? 0x1110 : 0x1000, $gflags, strlen($alias)) . (binary)$alias . pack('V', strlen($pmeta)) . (binary)$pmeta . $manifest;
54$file = (binary)$file;
55$file .= pack('V', strlen($manifest)) . $manifest;
56
57foreach($files as $cont)
58{
59    $file .= (binary)$cont;
60}
61
62file_put_contents($fname, $file);
63
64if (@$gzip) {
65    $fp = gzopen($fname, 'w');
66    fwrite($fp, $file);
67    fclose($fp);
68}
69
70if (@$bz2) {
71    $fp = bzopen($fname, 'w');
72    fwrite($fp, $file);
73    fclose($fp);
74}
75
76?>
77