backups.php
180 lines
| 1 | <?php |
| 2 | |
| 3 | // Namespace |
| 4 | namespace BMI\Plugin\Scanner; |
| 5 | |
| 6 | // Use |
| 7 | use BMI\Plugin\BMI_Logger AS Logger; |
| 8 | use BMI\Plugin\Zipper\BMI_Zipper AS Zipper; |
| 9 | use BMI\Plugin\Zipper\Zip AS Zip; |
| 10 | use BMI\Plugin\External\BMI_External_Storage as ExternalStorage; |
| 11 | |
| 12 | // Exit on direct access |
| 13 | if (!defined('ABSPATH')) exit; |
| 14 | |
| 15 | /** |
| 16 | * Main Backup Scanner Logic |
| 17 | */ |
| 18 | class BMI_BackupsScanner { |
| 19 | |
| 20 | public function scanBackupDir($path) { |
| 21 | |
| 22 | $files = []; |
| 23 | foreach (new \DirectoryIterator($path) as $fileInfo) { |
| 24 | |
| 25 | if ($fileInfo->isDot()) continue; |
| 26 | if ($fileInfo->isFile()) { |
| 27 | if (in_array($fileInfo->getExtension(), ['zip', 'tar', 'tar.gz', 'gz', 'rar', '7zip', '7z'])) { |
| 28 | |
| 29 | $files[] = array( |
| 30 | 'filename' => $fileInfo->getFilename(), |
| 31 | 'path' => $path, |
| 32 | 'size' => $fileInfo->getSize() |
| 33 | ); |
| 34 | |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | } |
| 39 | |
| 40 | return $files; |
| 41 | |
| 42 | } |
| 43 | |
| 44 | public function getManifestFromZip($zip_path, &$zipper) { |
| 45 | |
| 46 | // Get manifest content |
| 47 | $md5_file_summary_path = BMI_BACKUPS . DIRECTORY_SEPARATOR. 'md5summary.php'; |
| 48 | $zip_modification_time = filemtime($zip_path); |
| 49 | $zip_name = basename($zip_path); |
| 50 | |
| 51 | $md5summary = []; |
| 52 | if (file_exists($md5_file_summary_path)) { |
| 53 | $md5summary = file_get_contents($md5_file_summary_path); |
| 54 | $md5summary = substr($md5summary, 18, -2); |
| 55 | if (is_serialized($md5summary)) { |
| 56 | $md5summary = maybe_unserialize($md5summary); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | $md5s = []; |
| 61 | $cached_md5 = false; |
| 62 | if (isset($md5summary[$zip_name])) { |
| 63 | |
| 64 | $md5s = $md5summary[$zip_name]; |
| 65 | $latest = 0; |
| 66 | $latest_md5 = false; |
| 67 | for ($i = 0; $i < sizeof($md5s); ++$i) { |
| 68 | $md5_file_path = BMI_BACKUPS . DIRECTORY_SEPARATOR . $md5s[$i] . '.json'; |
| 69 | if (file_exists($md5_file_path)) { |
| 70 | $ftime = filemtime($md5_file_path); |
| 71 | if ($ftime > $latest) { |
| 72 | $latest = $ftime; |
| 73 | $latest_md5 = $md5s[$i]; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | if ($latest_md5 != false) { |
| 79 | $cached_md5 = $latest_md5; |
| 80 | } |
| 81 | |
| 82 | } else { |
| 83 | |
| 84 | $md5summary[$zip_name] = []; |
| 85 | |
| 86 | } |
| 87 | |
| 88 | if ($cached_md5 != false) { |
| 89 | $zip_md5 = $cached_md5; |
| 90 | $md5_file_path = BMI_BACKUPS . DIRECTORY_SEPARATOR. $cached_md5 . '.json'; |
| 91 | } else { |
| 92 | $zip_md5 = md5_file($zip_path); |
| 93 | $md5_file_path = BMI_BACKUPS . DIRECTORY_SEPARATOR. $zip_md5 . '.json'; |
| 94 | if (!in_array($zip_md5, $md5summary[$zip_name])) { |
| 95 | $md5summary[$zip_name][] = $zip_md5; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | $res = array(); |
| 100 | if (file_exists($md5_file_path)) { |
| 101 | $manifest = json_decode(file_get_contents($md5_file_path)); |
| 102 | } else { |
| 103 | $manifest = $zipper->getZipFileContent($zip_path, 'bmi_backup_manifest.json'); |
| 104 | } |
| 105 | |
| 106 | if ($manifest) { |
| 107 | |
| 108 | $res[] = $manifest->name . '#%&' . $zip_name; |
| 109 | $res[] = $manifest->date; |
| 110 | $res[] = $manifest->files; |
| 111 | $res[] = $manifest->manifest; |
| 112 | $res[] = @filesize($zip_path); |
| 113 | if (!isset($manifest->is_locked)) { |
| 114 | $manifest->is_locked = $zipper->is_locked_zip($zip_path) ? 'locked' : 'unlocked'; |
| 115 | $res[] = $manifest->is_locked; |
| 116 | } else { |
| 117 | $res[] = $manifest->is_locked; |
| 118 | } |
| 119 | $res[] = $manifest->cron; |
| 120 | $res[] = $zip_md5; |
| 121 | $res[] = sanitize_text_field($manifest->domain); |
| 122 | |
| 123 | if (!file_exists($md5_file_path)) { |
| 124 | $manifest->abspath = $manifest->config->ABSPATH; |
| 125 | $manifest->table_prefix = $manifest->config->table_prefix; |
| 126 | unset($manifest->config); |
| 127 | file_put_contents($md5_file_path, json_encode($manifest)); |
| 128 | } else touch($md5_file_path); |
| 129 | |
| 130 | $cacheMd5String = "<?php exit; \$x = '" . serialize($md5summary) . "';"; |
| 131 | file_put_contents($md5_file_summary_path, $cacheMd5String); |
| 132 | |
| 133 | return $res; |
| 134 | |
| 135 | } else return false; |
| 136 | |
| 137 | } |
| 138 | |
| 139 | public function getAvailableBackups() { |
| 140 | |
| 141 | // Require Universal Zip Library |
| 142 | require_once BMI_INCLUDES . '/zipper/zipping.php'; |
| 143 | $zipper = new Zipper(); |
| 144 | |
| 145 | // Scan for manifests |
| 146 | $manifests = array(); |
| 147 | $backups = array(); |
| 148 | $external = array(); |
| 149 | $ongoing = get_option('bmip_to_be_uploaded', [ |
| 150 | 'current_upload' => [], |
| 151 | 'queue' => [], |
| 152 | 'failed' => [] |
| 153 | ]); |
| 154 | |
| 155 | if (file_exists(BMI_BACKUPS)) |
| 156 | $backups = $this->scanBackupDir(BMI_BACKUPS); |
| 157 | |
| 158 | for ($i = 0; $i < sizeof($backups); ++$i) { |
| 159 | |
| 160 | $backup = $backups[$i]; |
| 161 | $manifest = $this->getManifestFromZip($backup['path'] . '/' . $backup['filename'], $zipper); |
| 162 | if ($manifest) $manifests[$backup['filename']] = $manifest; |
| 163 | |
| 164 | } |
| 165 | |
| 166 | if (defined('BMI_BACKUP_PRO') && defined('BMI_PRO_INC')) { |
| 167 | $proPath = BMI_PRO_INC . 'external/controller.php'; |
| 168 | if (file_exists($proPath)) { |
| 169 | require_once $proPath; |
| 170 | $externalStorage = new ExternalStorage(); |
| 171 | $external = $externalStorage->getExternalBackups(); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | return [ 'local' => $manifests, 'external' => $external, 'ongoing' => $ongoing ]; |
| 176 | |
| 177 | } |
| 178 | |
| 179 | } |
| 180 |