unzip.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. $zip = zip_open($zipFile);
  41. $remove_path = addcslashes($remove_path,"/");
  42. if (is_resource($zip))
  43. {
  44. $i=0;
  45. $i2=0;
  46. $extracted_files = array();
  47. $ignored_files = array();
  48. while ($zip_entry = zip_read($zip))
  49. {
  50. $filename = zip_entry_name( $zip_entry );
  51. $file_path = preg_replace( "/$remove_path/", "", $filename );
  52. $dir_path = preg_replace( "/$remove_path/", "", dirname( $filename ) );
  53. if( isset( $blacklist ) and is_array( $blacklist ) and in_array( $file_path , $blacklist ) )
  54. {
  55. if( isset( $whitelist ) and is_array( $whitelist ) and in_array( $filename , $whitelist ) )
  56. {
  57. $ignored_files[$i2] = $file_path;
  58. $i2++;
  59. }
  60. continue;
  61. }
  62. if( isset( $whitelist ) and is_array( $whitelist ) and !in_array( $filename , $whitelist ) )
  63. continue;
  64. $completePath = $extract_path . $dir_path;
  65. $completeName = $extract_path . $file_path;
  66. $escaped_temp_path = str_replace('\\', '\\\\', $temp_path);// For Windows paths backslashes
  67. $root = preg_match("#^$escaped_temp_path#", $completePath)?$temp_path:$base_path;
  68. $escaped_root = str_replace('\\', '\\\\', $root);
  69. $relative_path = preg_replace("#^$escaped_root(.*)$#","$1",$completePath);
  70. // Walk through path to create non existing directories
  71. // This won't apply to empty directories ! They are created further below
  72. if(!file_exists($completePath) && preg_match( '/^' . $remove_path .'/', dirname(zip_entry_name($zip_entry)) ) )
  73. {
  74. $tmp = $root;
  75. foreach(preg_split('/(\/|\\\\)/',$relative_path) AS $k)
  76. {
  77. if( $k != "" )
  78. {
  79. $tmp .= $k.DIRECTORY_SEPARATOR;
  80. if( !file_exists($tmp) )
  81. {
  82. if(!mkdir($tmp, 0777))
  83. {
  84. return "Unable to write folder ${tmp}.\n";
  85. }
  86. }
  87. }
  88. }
  89. }
  90. if (zip_entry_open($zip, $zip_entry, "r"))
  91. {
  92. if( preg_match( '/^' . $remove_path .'/', dirname(zip_entry_name($zip_entry)) ) )
  93. {
  94. if ( ! preg_match( "/\/$/", $completeName) )
  95. {
  96. if ( $fd = fopen($completeName, 'w+'))
  97. {
  98. fwrite($fd, zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)));
  99. fclose($fd);
  100. $extracted_files[$i]['filename'] = zip_entry_name($zip_entry);
  101. $i++;
  102. }
  103. else
  104. {
  105. return "Unable to write file ${completeName}.\n";
  106. }
  107. }
  108. }
  109. zip_entry_close($zip_entry);
  110. }
  111. }
  112. zip_close($zip);
  113. return array('ignored_files' => $ignored_files, 'extracted_files' => $extracted_files);
  114. }
  115. return "${zipFile} is corrupt.\n";
  116. }
  117. ?>