ZipExtension.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Interface for the zip extension
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. namespace PhpMyAdmin;
  9. use ZipArchive;
  10. /**
  11. * Transformations class
  12. *
  13. * @package PhpMyAdmin
  14. */
  15. class ZipExtension
  16. {
  17. /**
  18. * @var ZipArchive
  19. */
  20. private $zip;
  21. /**
  22. * Constructor
  23. */
  24. public function __construct()
  25. {
  26. $this->zip = new ZipArchive();
  27. }
  28. /**
  29. * Gets zip file contents
  30. *
  31. * @param string $file path to zip file
  32. * @param string $specific_entry regular expression to match a file
  33. *
  34. * @return array ($error_message, $file_data); $error_message
  35. * is empty if no error
  36. */
  37. public function getContents($file, $specific_entry = null)
  38. {
  39. /**
  40. * This function is used to "import" a SQL file which has been exported earlier
  41. * That means that this function works on the assumption that the zip file contains only a single SQL file
  42. * It might also be an ODS file, look below
  43. */
  44. $error_message = '';
  45. $file_data = '';
  46. $res = $this->zip->open($file);
  47. if ($res === true) {
  48. if ($this->zip->numFiles === 0) {
  49. $error_message = __('No files found inside ZIP archive!');
  50. $this->zip->close();
  51. return (['error' => $error_message, 'data' => $file_data]);
  52. }
  53. /* Is the the zip really an ODS file? */
  54. $ods_mime = 'application/vnd.oasis.opendocument.spreadsheet';
  55. $first_zip_entry = $this->zip->getFromIndex(0);
  56. if (!strcmp($ods_mime, $first_zip_entry)) {
  57. $specific_entry = '/^content\.xml$/';
  58. }
  59. if (!isset($specific_entry)) {
  60. $file_data = $first_zip_entry;
  61. $this->zip->close();
  62. return (['error' => $error_message, 'data' => $file_data]);
  63. }
  64. /* Return the correct contents, not just the first entry */
  65. for ($i = 0; $i < $this->zip->numFiles; $i++) {
  66. if (@preg_match($specific_entry, $this->zip->getNameIndex($i))) {
  67. $file_data = $this->zip->getFromIndex($i);
  68. break;
  69. }
  70. }
  71. /* Couldn't find any files that matched $specific_entry */
  72. if (empty($file_data)) {
  73. $error_message = __('Error in ZIP archive:')
  74. . ' Could not find "' . $specific_entry . '"';
  75. }
  76. $this->zip->close();
  77. return (['error' => $error_message, 'data' => $file_data]);
  78. } else {
  79. $error_message = __('Error in ZIP archive:') . ' ' . $this->zip->getStatusString();
  80. $this->zip->close();
  81. return (['error' => $error_message, 'data' => $file_data]);
  82. }
  83. }
  84. /**
  85. * Returns the filename of the first file that matches the given $file_regexp.
  86. *
  87. * @param string $file path to zip file
  88. * @param string $regex regular expression for the file name to match
  89. *
  90. * @return string the file name of the first file that matches the given regular expression
  91. */
  92. public function findFile($file, $regex)
  93. {
  94. $res = $this->zip->open($file);
  95. if ($res === true) {
  96. for ($i = 0; $i < $this->zip->numFiles; $i++) {
  97. if (preg_match($regex, $this->zip->getNameIndex($i))) {
  98. $filename = $this->zip->getNameIndex($i);
  99. $this->zip->close();
  100. return $filename;
  101. }
  102. }
  103. }
  104. return false;
  105. }
  106. /**
  107. * Returns the number of files in the zip archive.
  108. *
  109. * @param string $file path to zip file
  110. *
  111. * @return int the number of files in the zip archive or 0, either if there wern't any files or an error occured.
  112. */
  113. public function getNumberOfFiles($file)
  114. {
  115. $num = 0;
  116. $res = $this->zip->open($file);
  117. if ($res === true) {
  118. $num = $this->zip->numFiles;
  119. }
  120. return $num;
  121. }
  122. /**
  123. * Extracts the content of $entry.
  124. *
  125. * @param string $file path to zip file
  126. * @param string $entry file in the archive that should be extracted
  127. *
  128. * @return string|bool data on sucess, false otherwise
  129. */
  130. public function extract($file, $entry)
  131. {
  132. if ($this->zip->open($file) === true) {
  133. $result = $this->zip->getFromName($entry);
  134. $this->zip->close();
  135. return $result;
  136. }
  137. return false;
  138. }
  139. /**
  140. * Creates a zip file.
  141. * If $data is an array and $name is a string, the filenames will be indexed.
  142. * The function will return false if $data is a string but $name is an array
  143. * or if $data is an array and $name is an array, but they don't have the
  144. * same amount of elements.
  145. *
  146. * @param array|string $data contents of the file/files
  147. * @param array|string $name name of the file/files in the archive
  148. * @param integer $time the current timestamp
  149. *
  150. * @return string|bool the ZIP file contents, or false if there was an error.
  151. */
  152. public function createFile($data, $name, $time = 0)
  153. {
  154. $datasec = []; // Array to store compressed data
  155. $ctrl_dir = []; // Central directory
  156. $old_offset = 0; // Last offset position
  157. $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00"; // End of central directory record
  158. if (is_string($data) && is_string($name)) {
  159. $data = [$name => $data];
  160. } elseif (is_array($data) && is_string($name)) {
  161. $ext_pos = strpos($name, '.');
  162. $extension = substr($name, $ext_pos);
  163. $newData = [];
  164. foreach ($data as $key => $value) {
  165. $newName = str_replace(
  166. $extension,
  167. '_' . $key . $extension,
  168. $name
  169. );
  170. $newData[$newName] = $value;
  171. }
  172. $data = $newData;
  173. } elseif (is_array($data) && is_array($name) && count($data) === count($name)) {
  174. $data = array_combine($name, $data);
  175. } else {
  176. return false;
  177. }
  178. foreach ($data as $table => $dump) {
  179. $temp_name = str_replace('\\', '/', $table);
  180. /* Get Local Time */
  181. $timearray = getdate();
  182. if ($timearray['year'] < 1980) {
  183. $timearray['year'] = 1980;
  184. $timearray['mon'] = 1;
  185. $timearray['mday'] = 1;
  186. $timearray['hours'] = 0;
  187. $timearray['minutes'] = 0;
  188. $timearray['seconds'] = 0;
  189. }
  190. $time = (($timearray['year'] - 1980) << 25)
  191. | ($timearray['mon'] << 21)
  192. | ($timearray['mday'] << 16)
  193. | ($timearray['hours'] << 11)
  194. | ($timearray['minutes'] << 5)
  195. | ($timearray['seconds'] >> 1);
  196. $hexdtime = pack('V', $time);
  197. $unc_len = strlen($dump);
  198. $crc = crc32($dump);
  199. $zdata = gzcompress($dump);
  200. $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
  201. $c_len = strlen($zdata);
  202. $fr = "\x50\x4b\x03\x04"
  203. . "\x14\x00" // ver needed to extract
  204. . "\x00\x00" // gen purpose bit flag
  205. . "\x08\x00" // compression method
  206. . $hexdtime // last mod time and date
  207. // "local file header" segment
  208. . pack('V', $crc) // crc32
  209. . pack('V', $c_len) // compressed filesize
  210. . pack('V', $unc_len) // uncompressed filesize
  211. . pack('v', strlen($temp_name)) // length of filename
  212. . pack('v', 0) // extra field length
  213. . $temp_name
  214. // "file data" segment
  215. . $zdata;
  216. $datasec[] = $fr;
  217. // now add to central directory record
  218. $cdrec = "\x50\x4b\x01\x02"
  219. . "\x00\x00" // version made by
  220. . "\x14\x00" // version needed to extract
  221. . "\x00\x00" // gen purpose bit flag
  222. . "\x08\x00" // compression method
  223. . $hexdtime // last mod time & date
  224. . pack('V', $crc) // crc32
  225. . pack('V', $c_len) // compressed filesize
  226. . pack('V', $unc_len) // uncompressed filesize
  227. . pack('v', strlen($temp_name)) // length of filename
  228. . pack('v', 0) // extra field length
  229. . pack('v', 0) // file comment length
  230. . pack('v', 0) // disk number start
  231. . pack('v', 0) // internal file attributes
  232. . pack('V', 32) // external file attributes
  233. // - 'archive' bit set
  234. . pack('V', $old_offset) // relative offset of local header
  235. . $temp_name; // filename
  236. $old_offset += strlen($fr);
  237. // optional extra field, file comment goes here
  238. // save to central directory
  239. $ctrl_dir[] = $cdrec;
  240. }
  241. /* Build string to return */
  242. $temp_ctrldir = implode('', $ctrl_dir);
  243. $header = $temp_ctrldir .
  244. $eof_ctrl_dir .
  245. pack('v', sizeof($ctrl_dir)) . //total #of entries "on this disk"
  246. pack('v', sizeof($ctrl_dir)) . //total #of entries overall
  247. pack('V', strlen($temp_ctrldir)) . //size of central dir
  248. pack('V', $old_offset) . //offset to start of central dir
  249. "\x00\x00"; //.zip file comment length
  250. $data = implode('', $datasec);
  251. return $data . $header;
  252. }
  253. }