PageRenderTime 64ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/184.168.182.1/wp-content/plugins/updraftplus/class-zip.php

https://gitlab.com/endomorphosis/falkenstein
PHP | 306 lines | 219 code | 66 blank | 21 comment | 73 complexity | f80dcd593b24929944c2f98151eb9cfc MD5 | raw file
  1. <?php
  2. if (!defined ('ABSPATH')) die('No direct access allowed');
  3. if (class_exists('ZipArchive')):
  4. # We just add a last_error variable for comaptibility with our UpdraftPlus_PclZip object
  5. class UpdraftPlus_ZipArchive extends ZipArchive {
  6. public $last_error = 'Unknown: ZipArchive does not return error messages';
  7. }
  8. endif;
  9. class UpdraftPlus_BinZip extends UpdraftPlus_PclZip {
  10. private $binzip;
  11. function __construct() {
  12. global $updraftplus_backup;
  13. $this->binzip = $updraftplus_backup->binzip;
  14. if (!is_string($this->binzip)) {
  15. $this->last_error = "No binary zip was found";
  16. return false;
  17. }
  18. return parent::__construct();
  19. }
  20. public function addFile($file, $add_as) {
  21. global $updraftplus;
  22. # Get the directory that $add_as is relative to
  23. $base = $updraftplus->str_lreplace($add_as, '', $file);
  24. if ($file == $base) {
  25. // Shouldn't happen
  26. } else {
  27. $rdirname = untrailingslashit($base);
  28. # Note: $file equals $rdirname/$add_as
  29. $this->addfiles[$rdirname][] = $add_as;
  30. }
  31. }
  32. # The standard zip binary cannot list; so we use PclZip for that
  33. # Do the actual write-out - it is assumed that close() is where this is done. Needs to return true/false
  34. public function close() {
  35. if (empty($this->pclzip)) {
  36. $this->last_error = 'Zip file was not opened';
  37. return false;
  38. }
  39. global $updraftplus, $updraftplus_backup;
  40. $updraft_dir = $updraftplus->backups_dir_location();
  41. $activity = false;
  42. # BinZip does not like zero-sized zip files
  43. if (file_exists($this->path) && 0 == filesize($this->path)) @unlink($this->path);
  44. $descriptorspec = array(
  45. 0 => array('pipe', 'r'),
  46. 1 => array('pipe', 'w'),
  47. 2 => array('pipe', 'w')
  48. );
  49. $exec = $this->binzip." -v -@ ".escapeshellarg($this->path);
  50. $last_recorded_alive = time();
  51. $something_useful_happened = $updraftplus->something_useful_happened;
  52. $orig_size = file_exists($this->path) ? filesize($this->path) : 0;
  53. $last_size = $orig_size;
  54. clearstatcache();
  55. $added_dirs_yet = false;
  56. # If there are no files to add, but there are empty directories, then we need to make sure the directories actually get added
  57. if (0 == count($this->addfiles) && 0 < count($this->adddirs)) {
  58. $dir = realpath($updraftplus_backup->make_zipfile_source);
  59. $this->addfiles[$dir] = '././.';
  60. }
  61. // Loop over each destination directory name
  62. foreach ($this->addfiles as $rdirname => $files) {
  63. $process = proc_open($exec, $descriptorspec, $pipes, $rdirname);
  64. if (!is_resource($process)) {
  65. $updraftplus->log('BinZip error: proc_open failed');
  66. $this->last_error = 'BinZip error: proc_open failed';
  67. return false;
  68. }
  69. if (!$added_dirs_yet) {
  70. # Add the directories - (in fact, with binzip, non-empty directories automatically have their entries added; but it doesn't hurt to add them explicitly)
  71. foreach ($this->adddirs as $dir) {
  72. fwrite($pipes[0], $dir."/\n");
  73. }
  74. $added_dirs_yet=true;
  75. }
  76. $read = array($pipes[1], $pipes[2]);
  77. $except = null;
  78. if (!is_array($files) || 0 == count($files)) {
  79. fclose($pipes[0]);
  80. $write = array();
  81. } else {
  82. $write = array($pipes[0]);
  83. }
  84. while ((!feof($pipes[1]) || !feof($pipes[2]) || (is_array($files) && count($files)>0)) && false !== ($changes = @stream_select($read, $write, $except, 0, 200000))) {
  85. if (is_array($write) && in_array($pipes[0], $write) && is_array($files) && count($files)>0) {
  86. $file = array_pop($files);
  87. // Send the list of files on stdin
  88. fwrite($pipes[0], $file."\n");
  89. if (0 == count($files)) fclose($pipes[0]);
  90. }
  91. if (is_array($read) && in_array($pipes[1], $read)) {
  92. $w = fgets($pipes[1]);
  93. // Logging all this really slows things down; use debug to mitigate
  94. if ($w && $updraftplus_backup->debug) $updraftplus->log("Output from zip: ".trim($w), 'debug');
  95. if (time() > $last_recorded_alive + 5) {
  96. $updraftplus->record_still_alive();
  97. $last_recorded_alive = time();
  98. }
  99. if (file_exists($this->path)) {
  100. $new_size = @filesize($this->path);
  101. if (!$something_useful_happened && $new_size > $orig_size + 20) {
  102. $updraftplus->something_useful_happened();
  103. $something_useful_happened = true;
  104. }
  105. clearstatcache();
  106. # Log when 20% bigger or at least every 50Mb
  107. if ($new_size > $last_size*1.2 || $new_size > $last_size + 52428800) {
  108. $updraftplus->log(basename($this->path).sprintf(": size is now: %.2f Mb", round($new_size/1048576,1)));
  109. $last_size = $new_size;
  110. }
  111. }
  112. }
  113. if (is_array($read) && in_array($pipes[2], $read)) {
  114. $last_error = fgets($pipes[2]);
  115. if (!empty($last_error)) $this->last_error = rtrim($last_error);
  116. }
  117. // Re-set
  118. $read = array($pipes[1], $pipes[2]);
  119. $write = (is_array($files) && count($files) >0) ? array($pipes[0]) : array();
  120. $except = null;
  121. }
  122. fclose($pipes[1]);
  123. fclose($pipes[2]);
  124. $ret = proc_close($process);
  125. if ($ret != 0 && $ret != 12) {
  126. $updraftplus->log("Binary zip: error (code: $ret - look it up in the Diagnostics section at http://www.info-zip.org/mans/zip.html for interpretation... and also check that your hosting account quota is not full)");
  127. if (!empty($w) && !$updraftplus_backup->debug) $updraftplus->log("Last output from zip: ".trim($w), 'debug');
  128. return false;
  129. }
  130. unset($this->addfiles[$rdirname]);
  131. }
  132. return true;
  133. }
  134. }
  135. # A ZipArchive compatibility layer, with behaviour sufficient for our usage of ZipArchive
  136. class UpdraftPlus_PclZip {
  137. protected $pclzip;
  138. protected $path;
  139. protected $addfiles;
  140. protected $adddirs;
  141. private $statindex;
  142. public $last_error;
  143. function __construct() {
  144. $this->addfiles = array();
  145. $this->adddirs = array();
  146. }
  147. public function __get($name) {
  148. if ($name != 'numFiles') return null;
  149. if (empty($this->pclzip)) return false;
  150. $statindex = $this->pclzip->listContent();
  151. if (empty($statindex)) {
  152. $this->statindex=array();
  153. return 0;
  154. }
  155. $result = array();
  156. foreach ($statindex as $i => $file) {
  157. if (!isset($statindex[$i]['folder']) || 0 == $statindex[$i]['folder']) {
  158. $result[] = $file;
  159. }
  160. unset($statindex[$i]);
  161. }
  162. $this->statindex=$result;
  163. return count($this->statindex);
  164. }
  165. public function statIndex($i) {
  166. if (empty($this->statindex[$i])) return array('name' => null, 'size' => 0);
  167. return array('name' => $this->statindex[$i]['filename'], 'size' => $this->statindex[$i]['size']);
  168. }
  169. public function open($path, $flags = 0) {
  170. if(!class_exists('PclZip')) include_once(ABSPATH.'/wp-admin/includes/class-pclzip.php');
  171. if(!class_exists('PclZip')) {
  172. $this->last_error = "No PclZip class was found";
  173. return false;
  174. }
  175. $ziparchive_create_match = (defined('ZIPARCHIVE::CREATE')) ? ZIPARCHIVE::CREATE : 1;
  176. if ($flags == $ziparchive_create_match && file_exists($path)) @unlink($path);
  177. $this->pclzip = new PclZip($path);
  178. if (empty($this->pclzip)) {
  179. $this->last_error = 'Could not get a PclZip object';
  180. return false;
  181. }
  182. # Make the empty directory we need to implement addEmptyDir()
  183. global $updraftplus;
  184. $updraft_dir = $updraftplus->backups_dir_location();
  185. if (!is_dir($updraft_dir.'/emptydir') && !mkdir($updraft_dir.'/emptydir')) {
  186. $this->last_error = "Could not create empty directory ($updraft_dir/emptydir)";
  187. return false;
  188. }
  189. $this->path = $path;
  190. return true;
  191. }
  192. # Do the actual write-out - it is assumed that close() is where this is done. Needs to return true/false
  193. public function close() {
  194. if (empty($this->pclzip)) {
  195. $this->last_error = 'Zip file was not opened';
  196. return false;
  197. }
  198. global $updraftplus;
  199. $updraft_dir = $updraftplus->backups_dir_location();
  200. $activity = false;
  201. # Add the empty directories
  202. foreach ($this->adddirs as $dir) {
  203. if (false == $this->pclzip->add($updraft_dir.'/emptydir', PCLZIP_OPT_REMOVE_PATH, $updraft_dir.'/emptydir', PCLZIP_OPT_ADD_PATH, $dir)) {
  204. $this->last_error = $this->pclzip->errorInfo(true);
  205. return false;
  206. }
  207. $activity = true;
  208. }
  209. foreach ($this->addfiles as $rdirname => $adirnames) {
  210. foreach ($adirnames as $adirname => $files) {
  211. if (false == $this->pclzip->add($files, PCLZIP_OPT_REMOVE_PATH, $rdirname, PCLZIP_OPT_ADD_PATH, $adirname)) {
  212. $this->last_error = $this->pclzip->errorInfo(true);
  213. return false;
  214. }
  215. $activity = true;
  216. }
  217. unset($this->addfiles[$rdirname]);
  218. }
  219. $this->pclzip = false;
  220. $this->addfiles = array();
  221. $this->adddirs = array();
  222. clearstatcache();
  223. if ($activity && filesize($this->path) < 50) {
  224. $this->last_error = "Write failed - unknown cause (check your file permissions)";
  225. return false;
  226. }
  227. return true;
  228. }
  229. # Note: basename($add_as) is irrelevant; that is, it is actually basename($file) that will be used. But these are always identical in our usage.
  230. public function addFile($file, $add_as) {
  231. # Add the files. PclZip appears to do the whole (copy zip to temporary file, add file, move file) cycle for each file - so batch them as much as possible. We have to batch by dirname(). On a test with 1000 files of 25Kb each in the same directory, this reduced the time needed on that directory from 120s to 15s (or 5s with primed caches).
  232. $rdirname = dirname($file);
  233. $adirname = dirname($add_as);
  234. $this->addfiles[$rdirname][$adirname][] = $file;
  235. }
  236. # PclZip doesn't have a direct way to do this
  237. public function addEmptyDir($dir) {
  238. $this->adddirs[] = $dir;
  239. }
  240. }