]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/ziplib.php
Delete ctime() and rfc1123date() from lib/ziplib.php in favor of
[SourceForge/phpwiki.git] / lib / ziplib.php
1 <?php
2 rcs_id('$Id: ziplib.php,v 1.10 2001-12-11 18:21:38 dairiki Exp $');
3
4 /**
5  * GZIP stuff.
6  *
7  * Note that we use gzopen()/gzwrite() instead of gzcompress() even
8  * if gzcompress() is available.  Gzcompress() puts out data with different
9  * headers --- in particular it includes an "adler-32" checksum rather than
10  * a "CRC32" checksum.  Since we need the CRC-32 checksum, and since not
11  * all PHP's have gzcompress(), we'll just stick with gzopen(). 
12  */
13 function gzip_cleanup () {
14   global $gzip_tmpfile;
15     
16   if ($gzip_tmpfile)
17       @unlink($gzip_tmpfile);
18 }
19
20 function gzip_tempnam () {
21   global $gzip_tmpfile;
22
23   if (!$gzip_tmpfile)
24     {
25       //FIXME: does this work on non-unix machines?
26       $gzip_tmpfile = tempnam("/tmp", "wkzip");
27       register_shutdown_function("gzip_cleanup");
28     }
29     return $gzip_tmpfile;
30   }
31
32 function gzip_compress ($data) {
33   $filename = gzip_tempnam();
34   if (!($fp = gzopen($filename, "wb")))
35       ExitWiki("gzopen failed");
36   gzwrite($fp, $data, strlen($data));
37   if (!gzclose($fp))
38       ExitWiki("gzclose failed");
39     
40   $size = filesize($filename);
41
42   if (!($fp = fopen($filename, "rb")))
43       ExitWiki("fopen failed");
44   if (!($z = fread($fp, $size)) || strlen($z) != $size)
45       ExitWiki("fread failed");
46   if (!fclose($fp))
47       ExitWiki("fclose failed");
48
49   unlink($filename);
50   return $z;
51 }
52
53 function gzip_uncompress ($data) {
54   $filename = gzip_tempnam();
55   if (!($fp = fopen($filename, "wb")))
56       ExitWiki("fopen failed");
57   fwrite($fp, $data, strlen($data));
58   if (!fclose($fp))
59       ExitWiki("fclose failed");
60
61   if (!($fp = gzopen($filename, "rb")))
62       ExitWiki("gzopen failed");
63   $unz = '';
64   while ($buf = gzread($fp, 4096))
65       $unz .= $buf;
66   if (!gzclose($fp))
67       ExitWiki("gzclose failed");
68
69   unlink($filename);
70   return $unz;
71 }
72
73 /**
74  * CRC32 computation.  Hacked from Info-zip's zip-2.3 source code.
75  */
76
77 function zip_crc32 ($str, $crc = 0) 
78 {
79     static $zip_crc_table;
80
81     if (empty($zip_crc_table)) {
82         /* NOTE: The range of PHP ints seems to be -0x80000000 to 0x7fffffff.
83          * So, had to munge these constants.
84          */
85         $zip_crc_table
86             = array (0x00000000,  0x77073096, -0x11f19ed4, -0x66f6ae46,  0x076dc419,
87                      0x706af48f, -0x169c5acb, -0x619b6a5d,  0x0edb8832,  0x79dcb8a4,
88                      -0x1f2a16e2, -0x682d2678,  0x09b64c2b,  0x7eb17cbd, -0x1847d2f9,
89                      -0x6f40e26f,  0x1db71064,  0x6ab020f2, -0x0c468eb8, -0x7b41be22,
90                      0x1adad47d,  0x6ddde4eb, -0x0b2b4aaf, -0x7c2c7a39,  0x136c9856,
91                      0x646ba8c0, -0x029d0686, -0x759a3614,  0x14015c4f,  0x63066cd9,
92                      -0x05f0c29d, -0x72f7f20b,  0x3b6e20c8,  0x4c69105e, -0x2a9fbe1c,
93                      -0x5d988e8e,  0x3c03e4d1,  0x4b04d447, -0x2df27a03, -0x5af54a95,
94                      0x35b5a8fa,  0x42b2986c, -0x2444362a, -0x534306c0,  0x32d86ce3,
95                      0x45df5c75, -0x2329f231, -0x542ec2a7,  0x26d930ac,  0x51de003a,
96                      -0x3728ae80, -0x402f9eea,  0x21b4f4b5,  0x56b3c423, -0x30456a67,
97                      -0x47425af1,  0x2802b89e,  0x5f058808, -0x39f3264e, -0x4ef416dc,
98                      0x2f6f7c87,  0x58684c11, -0x3e9ee255, -0x4999d2c3,  0x76dc4190,
99                      0x01db7106, -0x672ddf44, -0x102aefd6,  0x71b18589,  0x06b6b51f,
100                      -0x60401b5b, -0x17472bcd,  0x7807c9a2,  0x0f00f934, -0x69f65772,
101                      -0x1ef167e8,  0x7f6a0dbb,  0x086d3d2d, -0x6e9b9369, -0x199ca3ff,
102                      0x6b6b51f4,  0x1c6c6162, -0x7a9acf28, -0x0d9dffb2,  0x6c0695ed,
103                      0x1b01a57b, -0x7df70b3f, -0x0af03ba9,  0x65b0d9c6,  0x12b7e950,
104                      -0x74414716, -0x03467784,  0x62dd1ddf,  0x15da2d49, -0x732c830d,
105                      -0x042bb39b,  0x4db26158,  0x3ab551ce, -0x5c43ff8c, -0x2b44cf1e,
106                      0x4adfa541,  0x3dd895d7, -0x5b2e3b93, -0x2c290b05,  0x4369e96a,
107                      0x346ed9fc, -0x529877ba, -0x259f4730,  0x44042d73,  0x33031de5,
108                      -0x55f5b3a1, -0x22f28337,  0x5005713c,  0x270241aa, -0x41f4eff0,
109                      -0x36f3df7a,  0x5768b525,  0x206f85b3, -0x46992bf7, -0x319e1b61,
110                      0x5edef90e,  0x29d9c998, -0x4f2f67de, -0x3828574c,  0x59b33d17,
111                      0x2eb40d81, -0x4842a3c5, -0x3f459353, -0x12477ce0, -0x65404c4a,
112                      0x03b6e20c,  0x74b1d29a, -0x152ab8c7, -0x622d8851,  0x04db2615,
113                      0x73dc1683, -0x1c9cf4ee, -0x6b9bc47c,  0x0d6d6a3e,  0x7a6a5aa8,
114                      -0x1bf130f5, -0x6cf60063,  0x0a00ae27,  0x7d079eb1, -0x0ff06cbc,
115                      -0x78f75c2e,  0x1e01f268,  0x6906c2fe, -0x089da8a3, -0x7f9a9835,
116                      0x196c3671,  0x6e6b06e7, -0x012be48a, -0x762cd420,  0x10da7a5a,
117                      0x67dd4acc, -0x06462091, -0x71411007,  0x17b7be43,  0x60b08ed5,
118                      -0x29295c18, -0x5e2e6c82,  0x38d8c2c4,  0x4fdff252, -0x2e44980f,
119                      -0x5943a899,  0x3fb506dd,  0x48b2364b, -0x27f2d426, -0x50f5e4b4,
120                      0x36034af6,  0x41047a60, -0x209f103d, -0x579820ab,  0x316e8eef,
121                      0x4669be79, -0x349e4c74, -0x43997ce6,  0x256fd2a0,  0x5268e236,
122                      -0x33f3886b, -0x44f4b8fd,  0x220216b9,  0x5505262f, -0x3a45c442,
123                      -0x4d42f4d8,  0x2bb45a92,  0x5cb36a04, -0x3d280059, -0x4a2f30cf,
124                      0x2cd99e8b,  0x5bdeae1d, -0x649b3d50, -0x139c0dda,  0x756aa39c,
125                      0x026d930a, -0x63f6f957, -0x14f1c9c1,  0x72076785,  0x05005713,
126                      -0x6a40b57e, -0x1d4785ec,  0x7bb12bae,  0x0cb61b38, -0x6d2d7165,
127                      -0x1a2a41f3,  0x7cdcefb7,  0x0bdbdf21, -0x792c2d2c, -0x0e2b1dbe,
128                      0x68ddb3f8,  0x1fda836e, -0x7e41e933, -0x0946d9a5,  0x6fb077e1,
129                      0x18b74777, -0x77f7a51a, -0x00f09590,  0x66063bca,  0x11010b5c,
130                      -0x709a6101, -0x079d5197,  0x616bffd3,  0x166ccf45, -0x5ff51d88,
131                      -0x28f22d12,  0x4e048354,  0x3903b3c2, -0x5898d99f, -0x2f9fe909,
132                      0x4969474d,  0x3e6e77db, -0x512e95b6, -0x2629a524,  0x40df0b66,
133                      0x37d83bf0, -0x564351ad, -0x2144613b,  0x47b2cf7f,  0x30b5ffe9,
134                      -0x42420de4, -0x35453d76,  0x53b39330,  0x24b4a3a6, -0x452fc9fb,
135                      -0x3228f96d,  0x54de5729,  0x23d967bf, -0x4c9985d2, -0x3b9eb548,
136                      0x5d681b02,  0x2a6f2b94, -0x4bf441c9, -0x3cf3715f,  0x5a05df1b,
137                      0x2d02ef8d);
138     }
139     
140     $crc = ~$crc;
141     for ($i = 0; $i < strlen($str); $i++) {
142         $crc = ( $zip_crc_table[($crc ^ ord($str[$i])) & 0xff]
143                  ^ (($crc >> 8) & 0xffffff) );
144     }
145     return ~$crc;
146 }
147
148 define('GZIP_MAGIC', "\037\213");
149 define('GZIP_DEFLATE', 010);
150
151 function zip_deflate ($content)
152 {
153   // Compress content, and suck information from gzip header.
154   $z = gzip_compress($content);
155
156   // Suck OS type byte from gzip header. FIXME: this smells bad.
157
158   extract(unpack("a2magic/Ccomp_type/Cflags/@9/Cos_type", $z));
159   
160   if ($magic != GZIP_MAGIC)
161       ExitWiki("Bad gzip magic");
162   if ($comp_type != GZIP_DEFLATE)
163       ExitWiki("Bad gzip comp type");
164   if (($flags & 0x3e) != 0)
165       ExitWiki(sprintf("Bad flags (0x%02x)", $flags));
166
167   $gz_header_len = 10;
168   $gz_data_len = strlen($z) - $gz_header_len - 8;
169   if ($gz_data_len < 0)
170       ExitWiki("not enough gzip output?");
171
172   extract(unpack("Vcrc32", substr($z, $gz_header_len + $gz_data_len)));
173
174   return array(substr($z, $gz_header_len, $gz_data_len), // gzipped data
175                $crc32,          // crc
176                $os_type         // OS type
177       );
178 }
179
180 function zip_inflate ($data, $crc32, $uncomp_size)
181 {
182   if (!function_exists('gzopen'))
183       ExitWiki("Can't inflate data: zlib support not enabled in this PHP");
184
185   // Reconstruct gzip header and ungzip the data.
186   $mtime = time();              //(Bogus mtime)
187
188   return gzip_uncompress( pack("a2CxV@10", GZIP_MAGIC, GZIP_DEFLATE, $mtime)
189                            . $data
190                            . pack("VV", $crc32, $uncomp_size) );
191 }
192
193 function unixtime2dostime ($unix_time) {
194   if ($unix_time % 1)
195       $unix_time++;             // Round up to even seconds.
196
197   list ($year,$month,$mday,$hour,$min,$sec)
198       = explode(" ", date("Y n j G i s", $unix_time));
199
200   if ($year < 1980)
201       list ($year,$month,$mday,$hour,$min,$sec) = array(1980, 1, 1, 0, 0, 0);
202     
203   $dosdate = (($year - 1980) << 9) | ($month << 5) | $mday;
204   $dostime = ($hour << 11) | ($min << 5) | ($sec >> 1);
205
206   return array($dosdate, $dostime);
207 }
208
209 function dostime2unixtime ($dosdate, $dostime) {
210   $mday = $dosdate & 0x1f;
211   $month = ($dosdate >> 5) & 0x0f;
212   $year = 1980 + (($dosdate >> 9) & 0x7f);
213
214   $sec = ($dostime & 0x1f) * 2;
215   $min = ($dostime >> 5) & 0x3f;
216   $hour = ($dostime >> 11) & 0x1f;
217
218   return mktime($hour, $min, $sec, $month, $mday, $year);
219 }
220
221
222 /**
223  * Class for zipfile creation.
224  */
225 define('ZIP_DEFLATE', GZIP_DEFLATE);
226 define('ZIP_STORE', 0);
227 define('ZIP_CENTHEAD_MAGIC', "PK\001\002");
228 define('ZIP_LOCHEAD_MAGIC', "PK\003\004");
229 define('ZIP_ENDDIR_MAGIC', "PK\005\006");
230
231 class ZipWriter
232 {
233   function ZipWriter ($comment = "", $zipname = "archive.zip") {
234     $this->comment = $comment;
235     $this->nfiles = 0;
236     $this->dir = "";            // "Central directory block"
237     $this->offset = 0;          // Current file position.
238
239     $zipname = addslashes($zipname);
240     header("Content-Type: application/zip; name=\"$zipname\"");
241     header("Content-Disposition: save; filename=\"$zipname\"");
242   }
243
244   function addRegularFile ($filename, $content, $attrib = false) {
245     if (!$attrib)
246         $attrib = array();
247
248     $size = strlen($content);
249     if (function_exists('gzopen'))
250       {
251         list ($data, $crc32, $os_type) = zip_deflate($content);
252         if (strlen($data) < $size)
253           {
254             $content = $data;   // Use compressed data.
255             $comp_type = ZIP_DEFLATE;
256           }
257         else
258             unset($crc32);      // force plain store.
259       }
260     if (!isset($crc32))
261       {
262         $comp_type = ZIP_STORE;
263         $crc32 = zip_crc32($content);
264       }
265     
266     if (!empty($attrib['write_protected']))
267         $atx = (0100444 << 16) | 1; // S_IFREG + read permissions to everybody.
268     else
269         $atx = (0100644 << 16); // Add owner write perms.
270
271     $ati = $attrib['is_ascii'] ? 1 : 0;
272
273     if (empty($attrib['mtime']))
274         $attrib['mtime'] = time();
275     list ($mod_date, $mod_time) = unixtime2dostime($attrib['mtime']);
276
277     // Construct parts common to "Local file header" and "Central
278     // directory file header."
279     if (!isset($attrib['extra_field']))
280         $attrib['extra_field'] = '';
281     if (!isset($attrib['file_comment']))
282         $attrib['file_comment'] = '';
283     
284     $head = pack("vvvvvVVVvv",
285                  20,    // Version needed to extract (FIXME: is this right?)
286                  0,             // Gen purp bit flag
287                  $comp_type,
288                  $mod_time,
289                  $mod_date,
290                  $crc32,
291                  strlen($content),
292                  $size,
293                  strlen($filename),
294                  strlen($attrib['extra_field']));
295
296     // Construct the "Local file header"
297     $lheader = ZIP_LOCHEAD_MAGIC . $head . $filename . $attrib['extra_field'];
298
299     // Construct the "central directory file header"
300     $this->dir .= pack("a4CC",
301                        ZIP_CENTHEAD_MAGIC,
302                        23,      // Version made by (FIXME: is this right?)
303                        $os_type);
304     $this->dir .= $head;
305     $this->dir .= pack("vvvVV",
306                        strlen($attrib['file_comment']),
307                        0,       // Disk number start
308                        $ati,    // Internal file attributes
309                        $atx,    // External file attributes
310                        $this->offset); // Relative offset of local header
311     $this->dir .= $filename . $attrib['extra_field'] . $attrib['file_comment'];
312
313     // Output the "Local file header" and file contents.
314     echo $lheader;
315     echo $content;
316
317     $this->offset += strlen($lheader) + strlen($content);
318     $this->nfiles++;
319   }
320
321   function finish () {
322     // Output the central directory
323     echo $this->dir;
324
325     // Construct the "End of central directory record"
326     echo ZIP_ENDDIR_MAGIC;
327     echo pack("vvvvVVv",
328               0,                // Number of this disk.
329               0,                // Number of disk with start of c dir
330               $this->nfiles,    // Number entries on this disk
331               $this->nfiles,    // Number entries
332               strlen($this->dir), // Size of central directory
333               $this->offset,    // Offset of central directory
334               strlen($this->comment));
335     echo $this->comment;
336   }
337 }
338
339
340 /**
341  * Class for reading zip files.
342  *
343  * BUGS:
344  *
345  * Many of the ExitWiki()'s should probably be warn()'s (eg. CRC mismatch).
346  *
347  * Only a subset of zip formats is recognized.  (I think that unsupported
348  * formats will be recognized as such rather than silently munged.)
349  *
350  * We don't read the central directory.  This means we don't see the
351  * file attributes (text? read-only?), or file comments.
352  *
353  * Right now we ignore the file mod date and time, since we don't need it.
354  */
355 class ZipReader
356 {
357   function ZipReader ($zipfile) {
358     if (!is_string($zipfile))
359         $this->fp = $zipfile;   // File already open
360     else if (!($this->fp = fopen($zipfile, "rb")))
361         ExitWiki("Can't open zip file '$zipfile' for reading");
362   }
363
364   function _read ($nbytes) {
365     $chunk = fread($this->fp, $nbytes);
366     if (strlen($chunk) != $nbytes)
367         ExitWiki("Unexpected EOF in zip file");
368     return $chunk;
369   }
370
371   function done () {
372     fclose($this->fp);
373     return false;
374   }
375   
376   function readFile () {
377     $head = $this->_read(30);
378
379     extract(unpack("a4magic/vreq_version/vflags/vcomp_type"
380                    . "/vmod_time/vmod_date"
381                    . "/Vcrc32/Vcomp_size/Vuncomp_size"
382                    . "/vfilename_len/vextrafld_len",
383                    $head));
384     
385     //FIXME: we should probably check $req_version.
386     $attrib['mtime'] = dostime2unixtime($mod_date, $mod_time);
387     
388     if ($magic != ZIP_LOCHEAD_MAGIC)
389       {
390         if ($magic != ZIP_CENTHEAD_MAGIC)
391             ExitWiki("Bad header type: " . htmlspecialchars($magic)); // FIXME: better message?
392         return $this->done();
393       }
394     if (($flags & 0x21) != 0)
395         ExitWiki("Encryption and/or zip patches not supported.");
396     if (($flags & 0x08) != 0)
397         ExitWiki("Postponed CRC not yet supported."); // FIXME: ???
398
399     $filename = $this->_read($filename_len);
400     if ($extrafld_len != 0)
401         $attrib['extra_field'] = $this->_read($extrafld_len);
402
403     $data = $this->_read($comp_size);
404
405     if ($comp_type == ZIP_DEFLATE)
406       {
407         $data = zip_inflate($data, $crc32, $uncomp_size);
408       }
409     else if ($comp_type == ZIP_STORE)
410       {
411         $crc = zip_crc32($data);
412         if ($crc32 != $crc)
413             ExitWiki(sprintf("CRC mismatch %x != %x", $crc, $crc32));
414       }
415     else
416         ExitWiki("Compression method $comp_method unsupported");
417
418     if (strlen($data) != $uncomp_size)
419         ExitWiki(sprintf("Uncompressed size mismatch %d != %d",
420                     strlen($data), $uncomp_size));
421
422     return array($filename, $data, $attrib);
423   }
424 }
425
426 /**
427  * Routines for Mime mailification of pages.
428  */
429 //FIXME: these should go elsewhere (libmime?).
430
431 /**
432  * Routines for quoted-printable en/decoding.
433  */
434 function QuotedPrintableEncode ($string)
435 {
436   // Quote special characters in line.
437   $quoted = "";
438   while ($string)
439     {
440       // The complicated regexp is to force quoting of trailing spaces.
441       preg_match('/^([ !-<>-~]*)(?:([!-<>-~]$)|(.))/s', $string, $match);
442       $quoted .= $match[1] . $match[2];
443       if (!empty($match[3]))
444           $quoted .= sprintf("=%02X", ord($match[3]));
445       $string = substr($string, strlen($match[0]));
446     }
447   // Split line.
448   // This splits the line (preferably after white-space) into lines
449   // which are no longer than 76 chars (after adding trailing '=' for
450   // soft line break, but before adding \r\n.)
451   return preg_replace('/(?=.{77})(.{10,74}[ \t]|.{71,73}[^=][^=])/s',
452                       "\\1=\r\n", $quoted);
453 }
454
455 function QuotedPrintableDecode ($string)
456 {
457   // Eliminate soft line-breaks.
458   $string = preg_replace('/=[ \t\r]*\n/', '', $string);
459   return quoted_printable_decode($string);
460 }
461
462 define('MIME_TOKEN_REGEXP', "[-!#-'*+.0-9A-Z^-~]+");
463
464 function MimeContentTypeHeader ($type, $subtype, $params)
465 {
466   $header = "Content-Type: $type/$subtype";
467   reset($params);
468   while (list($key, $val) = each($params))
469     {
470       //FIXME:  what about non-ascii printables in $val?
471       if (!preg_match('/^' . MIME_TOKEN_REGEXP . '$/', $val))
472           $val = '"' . addslashes($val) . '"';
473       $header .= ";\r\n  $key=$val";
474     }
475   return "$header\r\n";
476 }
477
478 function MimeMultipart ($parts) 
479 {
480   global $mime_multipart_count;
481
482   // The string "=_" can not occur in quoted-printable encoded data.
483   $boundary = "=_multipart_boundary_" . ++$mime_multipart_count;
484   
485   $head = MimeContentTypeHeader('multipart', 'mixed',
486                                 array('boundary' => $boundary));
487
488   $sep = "\r\n--$boundary\r\n";
489
490   return $head . $sep . implode($sep, $parts) . "\r\n--${boundary}--\r\n";
491 }
492   
493 function MimeifyPageRevision ($revision) {
494     $page = $revision->getPage();
495     // FIXME: add 'hits' to $params 
496     $params = array('pagename' => rawurlencode($page->getName()),
497                     'author' => rawurlencode($revision->get('author')),
498                     'version' => $revision->getVersion(),
499                     'flags' =>"",
500                     'lastmodified' => $revision->get('mtime'));
501     if ($page->get('mtime'))
502         $params['created'] = $page->get('mtime');
503     if ($page->get('locked'))
504         $params['flags'] = 'PAGE_LOCKED';
505     if ($revision->get('author_id'))
506         $params['author_id'] = $revision->get('author_id');
507       
508
509     $out = MimeContentTypeHeader('application', 'x-phpwiki', $params);
510     $out .= "Content-Transfer-Encoding: quoted-printable\r\n";
511     $out .= "\r\n";
512   
513     foreach ($revision->getContent() as $line) {
514         $out .= QuotedPrintableEncode(chop($line)) . "\r\n";
515     }
516     return $out;
517 }
518
519 /**
520  * Routines for parsing Mime-ified phpwiki pages.
521  */
522 function ParseRFC822Headers (&$string)
523 {
524   if (preg_match("/^From (.*)\r?\n/", $string, $match))
525     {
526       $headers['from '] = preg_replace('/^\s+|\s+$/', '', $match[1]);
527       $string = substr($string, strlen($match[0]));
528     }
529
530   while (preg_match('/^([!-9;-~]+) [ \t]* : [ \t]* '
531                     . '( .* \r?\n (?: [ \t] .* \r?\n)* )/x',
532                     $string, $match))
533     {
534       $headers[strtolower($match[1])]
535            = preg_replace('/^\s+|\s+$/', '', $match[2]);
536       $string = substr($string, strlen($match[0]));
537     }
538
539   if (empty($headers))
540       return false;
541
542   if (! preg_match("/^\r?\n/", $string, $match))
543     {
544       // No blank line after headers.
545       return false;
546     }
547   
548   $string = substr($string, strlen($match[0]));
549   
550   return $headers;
551 }
552
553
554 function ParseMimeContentType ($string)
555 {
556   // FIXME: Remove (RFC822 style comments).
557
558   // Get type/subtype
559   if (!preg_match(':^\s*(' . MIME_TOKEN_REGEXP . ')\s*'
560                   . '/'
561                   . '\s*(' . MIME_TOKEN_REGEXP . ')\s*:x',
562                   $string, $match))
563       ExitWiki ("Bad MIME content-type");
564
565   $type = strtolower($match[1]);
566   $subtype = strtolower($match[2]);
567   $string = substr($string, strlen($match[0]));
568   
569   $param = array();
570
571   while (preg_match('/^;\s*(' . MIME_TOKEN_REGEXP . ')\s*=\s*'
572                     . '(?:(' . MIME_TOKEN_REGEXP . ')|"((?:[^"\\\\]|\\.)*)") \s*/sx',
573                     $string, $match))
574     {
575       if (strlen($match[2]))
576           $val = $match[2];
577       else
578           $val = preg_replace('/[\\\\](.)/s', '\\1', $match[3]);
579       
580       $param[strtolower($match[1])] = $val;
581            
582       $string = substr($string, strlen($match[0]));
583     }
584
585   return array($type, $subtype, $param);
586 }
587
588 function ParseMimeMultipart($data, $boundary)
589 {
590   if (!$boundary)
591       ExitWiki("No boundary?");
592
593   $boundary = preg_quote($boundary);
594
595   while (preg_match("/^(|.*?\n)--$boundary((?:--)?)[^\n]*\n/s",
596                      $data, $match))
597     {
598       $data = substr($data, strlen($match[0]));
599       if ( ! isset($parts) )
600           $parts = array();     // First time through: discard leading chaff
601       else
602         {
603           if ($content = ParseMimeifiedPages($match[1]))
604               for (reset($content); $p = current($content); next($content))
605                   $parts[] = $p;
606         }
607
608       if ($match[2])
609           return $parts;        // End boundary found.
610     }
611   ExitWiki("No end boundary?");
612 }
613
614 function GenerateFootnotesFromRefs($params)
615 {
616   $footnotes = array();
617   reset($params);
618   while (list($p, $reference) = each($params))
619     {
620       if (preg_match('/^ref([1-9][0-9]*)$/', $p, $m))
621           $footnotes[$m[1]] = sprintf(gettext ("[%d] See [%s]"),
622                                       $m[1], rawurldecode($reference));
623     }
624
625   if (sizeof($footnotes) > 0)
626     {
627       ksort($footnotes);
628       return "-----\n"
629           . gettext ("!References") . "\n"
630           . join("\n%%%\n", $footnotes) . "\n";
631     }
632   else
633       return "";
634 }
635
636
637 // Convert references in meta-data to footnotes.
638 // Only zip archives generated by phpwiki 1.2.x or earlier should have
639 // references.
640 function ParseMimeifiedPages ($data)
641 {
642     if (!($headers = ParseRFC822Headers($data))
643         || empty($headers['content-type'])) {
644         //trigger_error("Can't find content-type header", E_USER_WARNING);
645         return false;
646     }
647     $typeheader = $headers['content-type'];
648   
649     if (!(list ($type, $subtype, $params) = ParseMimeContentType($typeheader))) {
650         trigger_error("Can't parse content-type: ("
651              . htmlspecialchars($typeheader) . ")", E_USER_WARNING);
652         return false;
653     }
654     if ("$type/$subtype" == 'multipart/mixed') {
655         return ParseMimeMultipart($data, $params['boundary']);
656     }
657     else if ("$type/$subtype" != 'application/x-phpwiki') {
658         trigger_error("Bad content-type: $type/$subtype", E_USER_WARNING);
659         return false;
660     }
661
662     // FIXME: more sanity checking?
663     $page = array();
664     $pagedata = array();
665     $versiondata = array();
666
667     foreach ($params as $key => $value) {
668         if (empty($value))
669             continue;
670         $value = rawurldecode($value);
671         switch ($key) {
672         case 'pagename':
673         case 'version':
674             $page[$key] = $value;
675             break;
676         case 'flags':
677             if (preg_match('/PAGE_LOCKED/', $value))
678                 $pagedata['locked'] = 'yes';
679             break;
680         case 'created':
681             $pagedata[$key] = $value;
682             break;
683         case 'lastmodified':
684             $versiondata['mtime'] = $value;
685             break;
686         case 'author':
687         case 'author_id':
688             $versiondata[$key] = $value;
689             break;
690         }
691     }
692
693     // FIXME: do we need to try harder to find a pagename if we
694     //   haven't got one yet?
695     if (!isset($versiondata['author'])) {
696         $versiondata['author'] = $GLOBALS['user']->id();
697     }
698     
699     $encoding = strtolower($headers['content-transfer-encoding']);
700     if ($encoding == 'quoted-printable')
701         $data = QuotedPrintableDecode($data);
702     else if ($encoding && $encoding != 'binary')
703         ExitWiki("Unknown encoding type: $encoding");
704
705     $data .= GenerateFootnotesFromRefs($params);
706
707     $page['content'] = preg_replace('/[ \t\r]*\n/', "\n", chop($data));
708     $page['pagedata'] = $pagedata;
709     $page['versiondata'] = $versiondata;
710
711     return array($page);
712 }
713
714 // Local Variables:
715 // mode: php
716 // tab-width: 8
717 // c-basic-offset: 4
718 // c-hanging-comment-ender-p: nil
719 // indent-tabs-mode: nil
720 // End:   
721 ?>