unzip.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /*
  3. *
  4. * OGP - Open Game Panel
  5. * Copyright (C) 2008 - 2018 The OGP Development Team
  6. *
  7. * http://www.opengamepanel.org/
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. */
  24. function extractZip( $zipFile, $extract_path, $remove_path = '', $blacklist = '', $whitelist = '' )
  25. {
  26. $temp_path = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
  27. $base_path = rtrim(getcwd(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
  28. if(!file_exists($extract_path))
  29. {
  30. return "Destination path (" . $extract_path . ") does not exists.\n";
  31. }
  32. if(!is_writable($extract_path))
  33. {
  34. return "Can't extract to " . $extract_path . ", not writable.\n";
  35. }
  36. if($zipFile == '' or $extract_path == '')
  37. return "Invalid arguments.\n";
  38. if( ! file_exists( $zipFile ) )
  39. return "Unable to read " . $zipFile . ".\n";
  40. $remove_path = addcslashes($remove_path,"/");
  41. $zip = new ZipArchive;
  42. if ($zip)
  43. {
  44. $i=0;
  45. $i2=0;
  46. $extracted_files = array();
  47. $ignored_files = array();
  48. if ($zip->open($zipFile) === TRUE)
  49. {
  50. for($j = 0; $j < $zip->numFiles; $j++)
  51. {
  52. $filename = $zip->getNameIndex($j);
  53. $file_path = preg_replace( "/$remove_path/", "", $filename );
  54. $dir_path = preg_replace( "/$remove_path/", "", dirname( $filename ) );
  55. if( isset( $blacklist ) and is_array( $blacklist ) and in_array( $file_path , $blacklist ) )
  56. {
  57. if( isset( $whitelist ) and is_array( $whitelist ) and in_array( $filename , $whitelist ) )
  58. {
  59. $ignored_files[$i2] = $file_path;
  60. $i2++;
  61. }
  62. continue;
  63. }
  64. if( isset( $whitelist ) and is_array( $whitelist ) and !in_array( $filename , $whitelist ) )
  65. continue;
  66. $completePath = $extract_path . $dir_path;
  67. $completeName = $extract_path . $file_path;
  68. $escaped_temp_path = str_replace('\\', '\\\\', $temp_path);// For Windows paths backslashes
  69. $root = preg_match("#^$escaped_temp_path#", $completePath)?$temp_path:$base_path;
  70. $escaped_root = str_replace('\\', '\\\\', $root);
  71. $relative_path = preg_replace("#^$escaped_root(.*)$#","$1",$completePath);
  72. // Walk through path to create non existing directories
  73. // This won't apply to empty directories ! They are created further below
  74. if(!file_exists($completePath) && preg_match( '/^' . $remove_path .'/', dirname($filename) ) )
  75. {
  76. $tmp = $root;
  77. foreach(preg_split('/(\/|\\\\)/',$relative_path) AS $k)
  78. {
  79. if( $k != "" )
  80. {
  81. $tmp .= $k.DIRECTORY_SEPARATOR;
  82. if( !file_exists($tmp) )
  83. {
  84. if(!mkdir($tmp, 0777))
  85. {
  86. return "Unable to write folder " . $tmp . ".\n";
  87. }
  88. }
  89. }
  90. }
  91. }
  92. if( preg_match( '/^' . $remove_path .'/', dirname($filename) ) )
  93. {
  94. if ( ! preg_match( "/\/$/", $completeName) )
  95. {
  96. if ( $fd = fopen($completeName, 'w+'))
  97. {
  98. $fp = $zip->getStream($filename);
  99. if($fp){
  100. fwrite($fd, stream_get_contents($fp));
  101. $extracted_files[$i]['filename'] = $filename;
  102. $i++;
  103. }
  104. fclose($fd);
  105. }
  106. else
  107. {
  108. return "Unable to write file " . $completeName . ".\n";
  109. }
  110. }
  111. }
  112. }
  113. $zip->close();
  114. }
  115. return array('ignored_files' => $ignored_files, 'extracted_files' => $extracted_files);
  116. }
  117. return $zipFile . " is corrupt.\n";
  118. }
  119. function extractZipGitUpdateFile( $zipFile, $extract_path)
  120. {
  121. if(!file_exists($extract_path))
  122. {
  123. return "Destination path (" . $extract_path . ") does not exists.\n";
  124. }
  125. if(!is_writable($extract_path))
  126. {
  127. return "Can't extract to " . $extract_path . ", not writable.\n";
  128. }
  129. if($zipFile == '' or $extract_path == '')
  130. return "Invalid arguments.\n";
  131. if( ! file_exists( $zipFile ) )
  132. return "Unable to read " . $zipFile . ".\n";
  133. $zip = new ZipArchive;
  134. if ($zip)
  135. {
  136. if ($zip->open($zipFile) === TRUE)
  137. {
  138. $zip->extractTo($extract_path);
  139. $zip->close();
  140. }
  141. $extracted_files = array();
  142. $ignored_files = array();
  143. // Construct the iterator
  144. $it = new RecursiveDirectoryIterator($extract_path);
  145. // Loop through files
  146. $i = 0;
  147. foreach(new RecursiveIteratorIterator($it) as $file) {
  148. $filename = $file->getPathname();
  149. $filenameRelative = $file->getFilename();
  150. if($filenameRelative == ".." || $filenameRelative == "."){
  151. continue;
  152. }
  153. $file_path_no_extract = preg_replace( "#$extract_path/#", "", $filename );
  154. $extracted_files[$i]['filename'] = $file_path_no_extract;
  155. $i++;
  156. }
  157. return array('ignored_files' => $ignored_files, 'extracted_files' => $extracted_files);
  158. }
  159. return $zipFile . " is corrupt.\n";
  160. }
  161. ?>