demo.zip.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /////////////////////////////////////////////////////////////////
  3. /// getID3() by James Heinrich <info@getid3.org> //
  4. // available at https://github.com/JamesHeinrich/getID3 //
  5. // or https://www.getid3.org //
  6. // or http://getid3.sourceforge.net //
  7. // //
  8. // /demo/demo.zip.php - part of getID3() //
  9. // Sample script how to use getID3() to decompress zip files //
  10. // see readme.txt for more details //
  11. // ///
  12. /////////////////////////////////////////////////////////////////
  13. die('For security reasons, this demo has been disabled. It can be enabled by removing line '.__LINE__.' in demos/'.basename(__FILE__));
  14. function UnzipFileContents($filename, &$errors) {
  15. $errors = array();
  16. $DecompressedFileContents = array();
  17. if (!class_exists('getID3')) {
  18. $errors[] = 'class getID3 not defined, please include getid3.php';
  19. } elseif (include_once('module.archive.zip.php')) {
  20. $getid3 = new getID3();
  21. $getid3->info['filesize'] = filesize($filename);
  22. ob_start();
  23. if ($getid3->fp = fopen($filename, 'rb')) {
  24. ob_end_clean();
  25. $getid3_zip = new getid3_zip($getid3);
  26. $getid3_zip->analyze();
  27. if (($getid3->info['fileformat'] == 'zip') && !empty($getid3->info['zip']['files'])) {
  28. if (!empty($getid3->info['zip']['central_directory'])) {
  29. $ZipDirectoryToWalk = $getid3->info['zip']['central_directory'];
  30. } elseif (!empty($getid3->info['zip']['entries'])) {
  31. $ZipDirectoryToWalk = $getid3->info['zip']['entries'];
  32. } else {
  33. $errors[] = 'failed to parse ZIP attachment "'.$filename.'" (no central directory)<br>';
  34. fclose($getid3->fp);
  35. return false;
  36. }
  37. foreach ($ZipDirectoryToWalk as $key => $valuearray) {
  38. fseek($getid3->fp, $valuearray['entry_offset'], SEEK_SET);
  39. $LocalFileHeader = $getid3_zip->ZIPparseLocalFileHeader();
  40. if ($LocalFileHeader['flags']['encrypted']) {
  41. // password-protected
  42. $DecompressedFileContents[$valuearray['filename']] = '';
  43. } else {
  44. fseek($getid3->fp, $LocalFileHeader['data_offset'], SEEK_SET);
  45. $compressedFileData = '';
  46. while ((strlen($compressedFileData) < $LocalFileHeader['compressed_size']) && !feof($getid3->fp)) {
  47. $compressedFileData .= fread($getid3->fp, 32768);
  48. }
  49. switch ($LocalFileHeader['raw']['compression_method']) {
  50. case 0: // store - great, just copy data unchanged
  51. $uncompressedFileData = $compressedFileData;
  52. break;
  53. case 8: // deflate
  54. ob_start();
  55. $uncompressedFileData = gzinflate($compressedFileData);
  56. $gzinflate_errors = trim(strip_tags(ob_get_contents()));
  57. ob_end_clean();
  58. if ($gzinflate_errors) {
  59. $errors[] = 'gzinflate() failed for file ['.$LocalFileHeader['filename'].']: "'.$gzinflate_errors.'"';
  60. continue 2;
  61. }
  62. break;
  63. case 1: // shrink
  64. case 2: // reduce-1
  65. case 3: // reduce-2
  66. case 4: // reduce-3
  67. case 5: // reduce-4
  68. case 6: // implode
  69. case 7: // tokenize
  70. case 9: // deflate64
  71. case 10: // PKWARE Date Compression Library Imploding
  72. $DecompressedFileContents[$valuearray['filename']] = '';
  73. $errors[] = 'unsupported ZIP compression method ('.$LocalFileHeader['raw']['compression_method'].' = '.$getid3_zip->ZIPcompressionMethodLookup($LocalFileHeader['raw']['compression_method']).')';
  74. continue 2;
  75. default:
  76. $DecompressedFileContents[$valuearray['filename']] = '';
  77. $errors[] = 'unknown ZIP compression method ('.$LocalFileHeader['raw']['compression_method'].')';
  78. continue 2;
  79. }
  80. $DecompressedFileContents[$valuearray['filename']] = $uncompressedFileData;
  81. unset($compressedFileData);
  82. }
  83. }
  84. } else {
  85. $errors[] = $filename.' does not appear to be a zip file';
  86. }
  87. } else {
  88. $error_message = ob_get_contents();
  89. ob_end_clean();
  90. $errors[] = 'failed to fopen('.$filename.', rb): '.$error_message;
  91. }
  92. } else {
  93. $errors[] = 'failed to include_once(module.archive.zip.php)';
  94. }
  95. return $DecompressedFileContents;
  96. }