changed git call from https to git readonly
[atutor.git] / mods / wiki / plugins / page / minidump.php
1 <?php
2
3 #-- glue
4 $ewiki_plugins["page"]["MiniDump"] = "ewiki_page_wiki_mini_tarball_dump";
5
6
7
8
9 function ewiki_page_wiki_mini_tarball_dump($id, $data, $action) {
10
11    global $ewiki_config, $ewiki_plugins;
12
13
14    #-- get all pages / binary files
15    $result = ewiki_db::GETALL(array("id", "version", "flags"));
16    if ($result) {
17
18       #-- HTTP headers
19       header("Content-Type: application/x-tar");
20       header("Content-Disposition: attachment; filename=\"InitPages.tar.gz\"");
21
22       #-- start tar file
23       $tarball = new ewiki_virtual_tarball();
24       $tarball->open(0);
25
26       #-- convert all pages
27       while ($row=$result->get(0, 0x1037)) {
28
29          $id = $row["id"];
30          $row = ewiki_db::GET($id);
31          $content = &$row["content"];
32          $fn = ($id);
33
34          if (!$row || !$row["id"] || !$row["content"]) {
35             continue;
36          }
37
38          #-- for tarball
39          $perms = array(
40             "mtime" => $row["lastmodified"],
41             "uname" => "ewiki",
42             "mode" => 0664 | (($row["flags"]&EWIKI_DB_F_WRITEABLE)?0002:0000),
43          );
44
45          #-- add file
46          $tarball->add(
47             $fn,
48             $content,
49             $perms
50          );
51       }
52
53       #-- end output
54       $tarball->close();
55    }
56
57    #-- fin 
58    die();
59 }
60
61
62
63
64 ############################################################################
65
66
67
68 if (!class_exists("ewiki_virtual_tarball")) {
69
70 #-- allows to generate a tarball from virtual files
71 #   (supports no directories or symlinks and other stuff)
72 class ewiki_virtual_tarball {
73
74    var $f = 0;
75
76    function open($fn="/dev/stdout") {
77
78       #-- init;
79       $this->f = 0;
80
81       #-- file output?
82       if ($fn && ($fn != "-")) {
83          $this->f = gzopen("$fn", "wb9");
84       }
85       else {
86          $_ENV["HTTP_ACCEPT_ENCODING"] = "gzip, deflate";
87          $_SERVER["HTTP_ACCEPT_ENCODING"] = "gzip, deflate";
88          ob_start("ob_gzhandler");
89       }
90    }
91
92
93    function close() {
94
95       #-- fill up file
96       $this->write(str_repeat("\000", 9*1024));
97
98       #-- close file handle
99       if ($this->f) {
100          gzclose($this->f);
101       }
102    }
103
104
105    function write($str) {
106       if ($this->f) {
107          gzwrite($this->f, $str);
108          fflush($this->f);
109       }
110       else {
111          echo $str;
112          ob_flush();
113       }
114    }
115
116
117    function oct($int, $len) {
118       $o = "\000";
119       while (--$len) {
120          $o = ($int & 0x07) . $o;
121          $int = $int >> 3;
122       }
123       return($o);
124    }
125
126
127    #-- add virtual file
128    function add($filename, $content, $args=array()) {
129
130       $args = array_merge($args, array(
131          "mode" => 000664,
132          "mtime" => time(),
133          "ctime" => time(),
134          "uid" => 65534,       #-- common for user "nobody"
135          "gid" => 65534,
136          "uname" => "nobody",
137          "gname" => "nobody",
138          "type" => "0",
139       ));
140       $args["mode"] |= 0100000;
141       $args["size"] = strlen($content);
142       $checksum = "        ";
143       $magic = "ustar  \000";
144       $filename = substr($filename, 0, 99);
145
146       #-- header record
147       $header  = str_pad($filename, 100, "\000")            # 0x0000
148                . $this->oct($args["mode"], 8)               # 0x0064
149                . $this->oct($args["uid"], 8)                # 0x006C
150                . $this->oct($args["gid"], 8)                # 0x0074
151                . $this->oct($args["size"], 12)              # 0x007C
152                . $this->oct($args["mtime"], 12)             # 0x0088
153                . ($checksum)                                # 0x0094
154                . ($args["type"])                            # 0x009C
155                . str_repeat("\000", 100)                    # 0x009D
156                . ($magic)                                   # 0x0101
157                . str_pad($args["uname"], 32, "\000")        # 0x0109
158                . str_pad($args["gname"], 32, "\000")        # 0x0129
159                ;                                            # 0x0149
160       $header = str_pad($header, 512, "\000");
161
162       #-- calculate and add header checksum
163       $cksum = 0;
164       for ($n=0; $n<512; $n++) {
165          $cksum += ord($header[$n]);
166       }
167       $header = substr($header, 0, 0x0094)
168               . $this->oct($cksum, 7) . " "
169               . substr($header, 0x009C);
170
171       #-- output
172       if ($fill = (512 - (strlen($content) % 512))) {
173          $content .= str_repeat("\000", $fill);
174       }
175       $this->write($header . $content);
176    }
177
178
179 }
180
181 }
182
183 ?>