zipping.php
309 lines
| 1 | <?php |
| 2 | |
| 3 | // Namespace |
| 4 | namespace BMI\Plugin\Zipper; |
| 5 | |
| 6 | // Use |
| 7 | use BMI\Plugin\Backup_Migration_Plugin as BMP; |
| 8 | use BMI\Plugin\BMI_Logger as Logger; |
| 9 | use BMI\Plugin\Progress\BMI_ZipProgress as Progress; |
| 10 | |
| 11 | // Exit on direct access |
| 12 | if (!defined('ABSPATH')) { |
| 13 | exit; |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * BMI_Zipper |
| 18 | */ |
| 19 | class BMI_Zipper { |
| 20 | public function makeZIP($files, $output, $name, &$zip_progress, $cron = false) { |
| 21 | |
| 22 | // Verbose |
| 23 | Logger::log(__("Creating backup ", 'backup-backup')); |
| 24 | Logger::log(__("Found ", 'backup-backup') . sizeof($files) . __(" files to backup.", 'backup-backup')); |
| 25 | |
| 26 | // Require Universal Zip Library |
| 27 | require_once BMI_INCLUDES . '/zipper/src/zip.php'; |
| 28 | |
| 29 | // Start microtime for ZIP Process |
| 30 | $start = microtime(true); |
| 31 | |
| 32 | // Logs |
| 33 | $zip_progress->log(__("Preparing map of files...", 'backup-backup'), 'step'); |
| 34 | |
| 35 | // Try to catch error |
| 36 | try { |
| 37 | |
| 38 | // Create new ZIP |
| 39 | $zip = new Zip(); |
| 40 | $zip->zip_start($output, $files, $name, $zip_progress, $start); |
| 41 | |
| 42 | // Logs |
| 43 | $zip_progress->log(__("Files prepared.", 'backup-backup'), 'success'); |
| 44 | $zip_progress->log(__("Starting compression process...", 'backup-backup'), 'info'); |
| 45 | |
| 46 | // Close ZIP and Save |
| 47 | $lala = $zip->zip_end(2, $cron); |
| 48 | if (!$lala) { |
| 49 | $zip_progress->log(__("Something went wrong (pclzip) – removing backup files...", 'backup-backup'), 'error'); |
| 50 | |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | return true; |
| 55 | } catch (\Throwable $e) { |
| 56 | |
| 57 | // Error print |
| 58 | $zip_progress->log(__("Reverting backup, removing file...", 'backup-backup'), 'step'); |
| 59 | $zip_progress->log(__("There was an error during backup...", 'backup-backup'), 'error'); |
| 60 | $zip_progress->log($e->getMessage(), 'error'); |
| 61 | |
| 62 | return false; |
| 63 | } catch (\Exception $e) { |
| 64 | |
| 65 | // Error print |
| 66 | $zip_progress->log(__("Reverting backup, removing file...", 'backup-backup'), 'step'); |
| 67 | $zip_progress->log(__("There was an error during backup...", 'backup-backup'), 'error'); |
| 68 | $zip_progress->log($e->getMessage(), 'error'); |
| 69 | |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | public function getZipFileContent($zipname, $filename) { |
| 77 | if (class_exists('ZipArchive') || class_exists('\ZipArchive')) { |
| 78 | $zip = new \ZipArchive(); |
| 79 | |
| 80 | if ($zip->open($zipname) === true) { |
| 81 | if ($content = $zip->getFromName($filename)) { |
| 82 | return json_decode($content); |
| 83 | } else { |
| 84 | return false; |
| 85 | } |
| 86 | } else { |
| 87 | return false; |
| 88 | } |
| 89 | } else { |
| 90 | if (!class_exists('PclZip')) { |
| 91 | if (!defined('PCLZIP_TEMPORARY_DIR')) { |
| 92 | $bmi_tmp_dir = BMI_ROOT_DIR . '/tmp'; |
| 93 | if (!file_exists($bmi_tmp_dir)) { |
| 94 | @mkdir($bmi_tmp_dir, 0775, true); |
| 95 | } |
| 96 | define('PCLZIP_TEMPORARY_DIR', $bmi_tmp_dir . '/bmi-'); |
| 97 | } |
| 98 | if (defined('BMI_PRO_PCLZIP') && file_exists(BMI_PRO_PCLZIP)) { |
| 99 | require_once BMI_PRO_PCLZIP; |
| 100 | } else { |
| 101 | require_once trailingslashit(ABSPATH) . 'wp-admin/includes/class-pclzip.php'; |
| 102 | } |
| 103 | } |
| 104 | $lib = new \PclZip($zipname); |
| 105 | $content = $lib->extract(PCLZIP_OPT_BY_NAME, $filename, PCLZIP_OPT_EXTRACT_AS_STRING); |
| 106 | if (isset($content[0]) && isset($content[0]['content'])) { |
| 107 | return json_decode($content[0]['content']); |
| 108 | } else { |
| 109 | return false; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | public function getZipContentList($zippath, $savepath) { |
| 115 | |
| 116 | if (class_exists('ZipArchive') || class_exists('\ZipArchive')) { |
| 117 | |
| 118 | $zip = new \ZipArchive(); |
| 119 | $zip->open($zippath); |
| 120 | |
| 121 | if (!isset($zip->numFiles) || $zip->numFiles == 0 || $zip->numFiles === false) { |
| 122 | $zip->close(); |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | $tmpf = fopen($savepath, 'a+'); |
| 127 | $totalAmount = $zip->numFiles; |
| 128 | |
| 129 | for ($i = 0; $i < $zip->numFiles; ++$i) { |
| 130 | |
| 131 | $stat = $zip->statIndex($i); |
| 132 | fwrite($tmpf, $stat['name'] . "\n"); |
| 133 | unset($stat); |
| 134 | |
| 135 | } |
| 136 | |
| 137 | fclose($tmpf); |
| 138 | $zip->close(); |
| 139 | |
| 140 | return $totalAmount; |
| 141 | |
| 142 | } else { |
| 143 | |
| 144 | if (!class_exists('PclZip')) { |
| 145 | if (!defined('PCLZIP_TEMPORARY_DIR')) { |
| 146 | $bmi_tmp_dir = BMI_ROOT_DIR . '/tmp'; |
| 147 | if (!file_exists($bmi_tmp_dir)) { |
| 148 | @mkdir($bmi_tmp_dir, 0775, true); |
| 149 | } |
| 150 | define('PCLZIP_TEMPORARY_DIR', $bmi_tmp_dir . '/bmi-'); |
| 151 | } |
| 152 | if (defined('BMI_PRO_PCLZIP') && file_exists(BMI_PRO_PCLZIP)) { |
| 153 | require_once BMI_PRO_PCLZIP; |
| 154 | } else { |
| 155 | require_once trailingslashit(ABSPATH) . 'wp-admin/includes/class-pclzip.php'; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | $zip = new \PclZip($zippath); |
| 160 | $list = $zip->listContent(); |
| 161 | if ($list == 0) { |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | $tmpf = fopen($savepath, 'a+'); |
| 166 | |
| 167 | $totalAmount = sizeof($list); |
| 168 | for ($i = 0; $i < $totalAmount; ++$i) { |
| 169 | |
| 170 | fwrite($tmpf, $list[$i]['filename'] . "\n"); |
| 171 | |
| 172 | } |
| 173 | |
| 174 | fclose($tmpf); |
| 175 | |
| 176 | return $totalAmount; |
| 177 | |
| 178 | } |
| 179 | |
| 180 | } |
| 181 | |
| 182 | public function getZipFileContentPlain($zipname, $filename) { |
| 183 | if (class_exists('ZipArchive')) { |
| 184 | $zip = new \ZipArchive(); |
| 185 | |
| 186 | if ($zip->open($zipname) === true) { |
| 187 | if ($content = $zip->getFromName($filename)) { |
| 188 | return $content; |
| 189 | } else { |
| 190 | return false; |
| 191 | } |
| 192 | } else { |
| 193 | return false; |
| 194 | } |
| 195 | } else { |
| 196 | if (!class_exists('PclZip')) { |
| 197 | if (!defined('PCLZIP_TEMPORARY_DIR')) { |
| 198 | $bmi_tmp_dir = BMI_ROOT_DIR . '/tmp'; |
| 199 | if (!file_exists($bmi_tmp_dir)) { |
| 200 | @mkdir($bmi_tmp_dir, 0775, true); |
| 201 | } |
| 202 | define('PCLZIP_TEMPORARY_DIR', $bmi_tmp_dir . '/bmi-'); |
| 203 | } |
| 204 | if (defined('BMI_PRO_PCLZIP') && file_exists(BMI_PRO_PCLZIP)) { |
| 205 | require_once BMI_PRO_PCLZIP; |
| 206 | } else { |
| 207 | require_once trailingslashit(ABSPATH) . 'wp-admin/includes/class-pclzip.php'; |
| 208 | } |
| 209 | } |
| 210 | $lib = new \PclZip($zipname); |
| 211 | |
| 212 | $content = $lib->extract(PCLZIP_OPT_BY_NAME, $filename, PCLZIP_OPT_EXTRACT_AS_STRING); |
| 213 | if (sizeof($content) > 0) { |
| 214 | return $content[0]['content']; |
| 215 | } else { |
| 216 | return false; |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | public function lock_zip($zippath, $unlock = false) { |
| 222 | |
| 223 | // Require Universal Zip Library |
| 224 | require_once BMI_INCLUDES . '/zipper/src/zip.php'; |
| 225 | |
| 226 | try { |
| 227 | |
| 228 | // Path to lock file |
| 229 | $filename = '.lock'; |
| 230 | |
| 231 | // Load lib |
| 232 | if (!class_exists('PclZip')) { |
| 233 | if (!defined('PCLZIP_TEMPORARY_DIR')) { |
| 234 | $bmi_tmp_dir = BMI_ROOT_DIR . '/tmp'; |
| 235 | if (!file_exists($bmi_tmp_dir)) { |
| 236 | @mkdir($bmi_tmp_dir, 0775, true); |
| 237 | } |
| 238 | define('PCLZIP_TEMPORARY_DIR', $bmi_tmp_dir . '/bmi-'); |
| 239 | } |
| 240 | if (defined('BMI_PRO_PCLZIP') && file_exists(BMI_PRO_PCLZIP)) { |
| 241 | require_once BMI_PRO_PCLZIP; |
| 242 | } else { |
| 243 | require_once trailingslashit(ABSPATH) . 'wp-admin/includes/class-pclzip.php'; |
| 244 | } |
| 245 | } |
| 246 | $lib = new \PclZip($zippath); |
| 247 | |
| 248 | $md5_file_summary_path = BMI_BACKUPS . DIRECTORY_SEPARATOR. 'md5summary.php'; |
| 249 | if (file_exists($md5_file_summary_path)) { |
| 250 | $zip_name = basename($zippath); |
| 251 | $md5summary = file_get_contents($md5_file_summary_path); |
| 252 | $md5summary = substr($md5summary, 18, -2); |
| 253 | if (is_serialized($md5summary)) { |
| 254 | $md5summary = maybe_unserialize($md5summary); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | if (isset($md5summary[$zip_name])) { |
| 259 | $md5s = $md5summary[$zip_name]; |
| 260 | for ($i = 0; $i < sizeof($md5s); ++$i) { |
| 261 | $md5_file_path = BMI_BACKUPS . DIRECTORY_SEPARATOR. $md5s[$i] . '.json'; |
| 262 | if (file_exists($md5_file_path)) { |
| 263 | @unlink($md5_file_path); |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | // Unlocking case |
| 269 | if ($unlock) { |
| 270 | if ($this->is_locked_zip($zippath)) { |
| 271 | $lib->delete(PCLZIP_OPT_BY_NAME, $filename); |
| 272 | } else { |
| 273 | return true; |
| 274 | } |
| 275 | } else { |
| 276 | if (!$this->is_locked_zip($zippath)) { |
| 277 | |
| 278 | // Locking case |
| 279 | $content = json_encode(['locked' => 'true']); |
| 280 | $lib->add([[PCLZIP_ATT_FILE_NAME => $filename, PCLZIP_ATT_FILE_CONTENT => $content]]); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | return true; |
| 285 | } catch (\Exception $e) { |
| 286 | Logger::error($e); |
| 287 | |
| 288 | return false; |
| 289 | } catch (\Throwable $e) { |
| 290 | Logger::error($e); |
| 291 | |
| 292 | return false; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | public function is_locked_zip($zippath) { |
| 297 | $lock = $this->getZipFileContent($zippath, '.lock'); |
| 298 | if ($lock) { |
| 299 | if ($lock->locked == 'true') { |
| 300 | return true; |
| 301 | } else { |
| 302 | return false; |
| 303 | } |
| 304 | } else { |
| 305 | return false; |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 |