/wp-content/plugins/duplicator/classes/package.archive.zip.php

https://github.com/Canuckaholic/Pop-Digital · PHP · 144 lines · 105 code · 19 blank · 20 comment · 15 complexity · 582c1156b2988160aa7a04ac7b9cc58a MD5 · raw file

  1. <?php
  2. if ( ! defined( 'DUPLICATOR_VERSION' ) ) exit; // Exit if accessed directly
  3. require_once (DUPLICATOR_PLUGIN_PATH . 'classes/package.archive.php');
  4. /**
  5. * DUP_ZIP
  6. * Creates a zip file using the built in PHP ZipArchive class
  7. */
  8. class DUP_Zip extends DUP_Archive {
  9. //PRIVATE
  10. private static $compressDir;
  11. private static $countDirs = 0;
  12. private static $countFiles = 0;
  13. private static $sqlPath;
  14. private static $zipPath;
  15. private static $zipFileSize;
  16. private static $zipArchive;
  17. private static $limitItems = 0;
  18. private static $networkFlush = false;
  19. private static $scanReport;
  20. /**
  21. * CREATE
  22. * Creates the zip file and adds the SQL file to the archive
  23. */
  24. static public function Create(DUP_Archive $archive) {
  25. try {
  26. $timerAllStart = DUP_Util::GetMicrotime();
  27. $package_zip_flush = DUP_Settings::Get('package_zip_flush');
  28. self::$compressDir = rtrim(DUP_Util::SafePath($archive->PackDir), '/');
  29. self::$sqlPath = DUP_Util::SafePath("{$archive->Package->StorePath}/{$archive->Package->Database->File}");
  30. self::$zipPath = DUP_Util::SafePath("{$archive->Package->StorePath}/{$archive->File}");
  31. self::$zipArchive = new ZipArchive();
  32. self::$networkFlush = empty($package_zip_flush) ? false : $package_zip_flush;
  33. $filterDirs = empty($archive->FilterDirs) ? 'not set' : $archive->FilterDirs;
  34. $filterExts = empty($archive->FilterExts) ? 'not set' : $archive->FilterExts;
  35. $filterOn = ($archive->FilterOn) ? 'ON' : 'OFF';
  36. //LOAD SCAN REPORT
  37. $json = file_get_contents(DUPLICATOR_SSDIR_PATH_TMP . "/{$archive->Package->NameHash}_scan.json");
  38. self::$scanReport = json_decode($json);
  39. DUP_Log::Info("\n********************************************************************************");
  40. DUP_Log::Info("ARCHIVE (ZIP):");
  41. DUP_Log::Info("********************************************************************************");
  42. $isZipOpen = (self::$zipArchive->open(self::$zipPath, ZIPARCHIVE::CREATE) === TRUE);
  43. if (! $isZipOpen){
  44. DUP_Log::Error("Cannot open zip file with PHP ZipArchive.", "Path location [" . self::$zipPath . "]");
  45. }
  46. DUP_Log::Info("ARCHIVE DIR: " . self::$compressDir);
  47. DUP_Log::Info("ARCHIVE FILE: " . basename(self::$zipPath));
  48. DUP_Log::Info("FILTERS: *{$filterOn}*");
  49. DUP_Log::Info("DIRS: {$filterDirs}");
  50. DUP_Log::Info("EXTS: {$filterExts}");
  51. DUP_Log::Info("----------------------------------------");
  52. DUP_Log::Info("COMPRESSING");
  53. DUP_Log::Info("SIZE:\t" . self::$scanReport->ARC->Size);
  54. DUP_Log::Info("STATS:\tDirs " . self::$scanReport->ARC->DirCount . " | Files " . self::$scanReport->ARC->FileCount . " | Links " . self::$scanReport->ARC->LinkCount);
  55. //ADD SQL
  56. $isSQLInZip = self::$zipArchive->addFile(self::$sqlPath, "database.sql");
  57. if ($isSQLInZip) {
  58. DUP_Log::Info("SQL ADDED: " . basename(self::$sqlPath));
  59. } else {
  60. DUP_Log::Error("Unable to add database.sql to archive.", "SQL File Path [" . self::$sqlath . "]");
  61. }
  62. self::$zipArchive->close();
  63. self::$zipArchive->open(self::$zipPath, ZipArchive::CREATE);
  64. //ZIP DIRECTORIES
  65. foreach(self::$scanReport->ARC->Dirs as $dir){
  66. if (self::$zipArchive->addEmptyDir(ltrim(str_replace(self::$compressDir, '', $dir), '/'))) {
  67. self::$countDirs++;
  68. } else {
  69. //Don't warn when dirtory is the root path
  70. if (strcmp($dir, rtrim(self::$compressDir, '/')) != 0)
  71. DUP_Log::Info("WARNING: Unable to zip directory: '{$dir}'" . rtrim(self::$compressDir, '/'));
  72. }
  73. }
  74. /* ZIP FILES: Network Flush
  75. * This allows the process to not timeout on fcgi
  76. * setups that need a response every X seconds */
  77. if (self::$networkFlush) {
  78. foreach(self::$scanReport->ARC->Files as $file) {
  79. if (self::$zipArchive->addFile($file, ltrim(str_replace(self::$compressDir, '', $file), '/'))) {
  80. self::$limitItems++;
  81. self::$countFiles++;
  82. } else {
  83. DUP_Log::Info("WARNING: Unable to zip file: {$file}");
  84. }
  85. //Trigger a flush to the web server after so many files have been loaded.
  86. if(self::$limitItems > DUPLICATOR_ZIP_FLUSH_TRIGGER) {
  87. $sumItems = (self::$countDirs + self::$countFiles);
  88. self::$zipArchive->close();
  89. self::$zipArchive->open(self::$zipPath);
  90. self::$limitItems = 0;
  91. DUP_Util::FcgiFlush();
  92. DUP_Log::Info("Items archived [{$sumItems}] flushing response.");
  93. }
  94. }
  95. //Normal
  96. } else {
  97. foreach(self::$scanReport->ARC->Files as $file) {
  98. if (self::$zipArchive->addFile($file, ltrim(str_replace(self::$compressDir, '', $file), '/'))) {
  99. self::$countFiles++;
  100. } else {
  101. DUP_Log::Info("WARNING: Unable to zip file: {$file}");
  102. }
  103. }
  104. }
  105. DUP_Log::Info(print_r(self::$zipArchive, true));
  106. //--------------------------------
  107. //LOG FINAL RESULTS
  108. DUP_Util::FcgiFlush();
  109. $zipCloseResult = self::$zipArchive->close();
  110. ($zipCloseResult)
  111. ? DUP_Log::Info("COMPRESSION RESULT: '{$zipCloseResult}'")
  112. : DUP_Log::Error("ZipArchive close failure.", "This hosted server may have a disk quota limit.\nCheck to make sure this archive file can be stored.");
  113. $timerAllEnd = DUP_Util::GetMicrotime();
  114. $timerAllSum = DUP_Util::ElapsedTime($timerAllEnd, $timerAllStart);
  115. self::$zipFileSize = @filesize(self::$zipPath);
  116. DUP_Log::Info("COMPRESSED SIZE: " . DUP_Util::ByteSize(self::$zipFileSize));
  117. DUP_Log::Info("ARCHIVE RUNTIME: {$timerAllSum}");
  118. DUP_Log::Info("MEMORY STACK: " . DUP_Server::GetPHPMemory());
  119. }
  120. catch (Exception $e) {
  121. DUP_Log::Error("Runtime error in package.archive.zip.php constructor.", "Exception: {$e}");
  122. }
  123. }
  124. }
  125. ?>