phar_test.inc 1.6 KB

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