PluginProbe ʕ •ᴥ•ʔ
Backup Migration / 1.3.7
Backup Migration v1.3.7
2.1.6 2.1.5.2 trunk 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.6.1 1.4.7 1.4.8 1.4.9 1.4.9.1 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.5.1
backup-backup / includes / scanner / backups.php
backup-backup / includes / scanner Last commit date
backups.php 2 years ago files.php 2 years ago
backups.php
183 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 if (!file_exists($zip_path)) return false;
47
48 // Get manifest content
49 $md5_file_summary_path = BMI_BACKUPS . DIRECTORY_SEPARATOR. 'md5summary.php';
50 $zip_modification_time = filemtime($zip_path);
51 $zip_name = basename($zip_path);
52
53 $md5summary = [];
54 if (file_exists($md5_file_summary_path)) {
55 $md5summary = file_get_contents($md5_file_summary_path);
56 $md5summary = substr($md5summary, 18, -2);
57 if (is_serialized($md5summary)) {
58 $md5summary = maybe_unserialize($md5summary);
59 }
60 }
61
62 $md5s = [];
63 $cached_md5 = false;
64 if (isset($md5summary[$zip_name])) {
65
66 $md5s = $md5summary[$zip_name];
67 $latest = 0;
68 $latest_md5 = false;
69 for ($i = 0; $i < sizeof($md5s); ++$i) {
70 $md5_file_path = BMI_BACKUPS . DIRECTORY_SEPARATOR . $md5s[$i] . '.json';
71 if (file_exists($md5_file_path)) {
72 $ftime = filemtime($md5_file_path);
73 if ($ftime > $latest) {
74 $latest = $ftime;
75 $latest_md5 = $md5s[$i];
76 }
77 }
78 }
79
80 if ($latest_md5 != false) {
81 $cached_md5 = $latest_md5;
82 }
83
84 } else {
85
86 $md5summary[$zip_name] = [];
87
88 }
89
90 if ($cached_md5 != false) {
91 $zip_md5 = $cached_md5;
92 $md5_file_path = BMI_BACKUPS . DIRECTORY_SEPARATOR. $cached_md5 . '.json';
93 } else {
94 $zip_md5 = md5_file($zip_path);
95 $md5_file_path = BMI_BACKUPS . DIRECTORY_SEPARATOR. $zip_md5 . '.json';
96 if (!in_array($zip_md5, $md5summary[$zip_name])) {
97 $md5summary[$zip_name][] = $zip_md5;
98 }
99 }
100
101 $res = array();
102 if (file_exists($md5_file_path)) {
103 $manifest = json_decode(file_get_contents($md5_file_path));
104 } else {
105 $manifest = $zipper->getZipFileContent($zip_path, 'bmi_backup_manifest.json');
106 }
107
108 if ($manifest) {
109
110 $res[] = $manifest->name . '#%&' . $zip_name;
111 $res[] = $manifest->date;
112 $res[] = $manifest->files;
113 $res[] = $manifest->manifest;
114 $res[] = @filesize($zip_path);
115 if (!isset($manifest->is_locked)) {
116 $manifest->is_locked = $zipper->is_locked_zip($zip_path) ? 'locked' : 'unlocked';
117 $res[] = $manifest->is_locked;
118 } else {
119 $res[] = $manifest->is_locked;
120 }
121 $res[] = $manifest->cron;
122 $res[] = $zip_md5;
123 $res[] = sanitize_text_field($manifest->domain);
124
125 if (!file_exists($md5_file_path)) {
126 $manifest->abspath = $manifest->config->ABSPATH;
127 $manifest->table_prefix = $manifest->config->table_prefix;
128 unset($manifest->config);
129 file_put_contents($md5_file_path, json_encode($manifest));
130 } else touch($md5_file_path);
131
132 $cacheMd5String = "<?php exit; \$x = '" . serialize($md5summary) . "';";
133 file_put_contents($md5_file_summary_path, $cacheMd5String);
134
135 return $res;
136
137 } else return false;
138
139 }
140
141 public function getAvailableBackups() {
142
143 // Require Universal Zip Library
144 require_once BMI_INCLUDES . '/zipper/zipping.php';
145 $zipper = new Zipper();
146
147 // Scan for manifests
148 $manifests = array();
149 $backups = array();
150 $external = array();
151 $ongoing = get_option('bmip_to_be_uploaded', [
152 'current_upload' => [],
153 'queue' => [],
154 'failed' => []
155 ]);
156
157 if (file_exists(BMI_BACKUPS))
158 $backups = $this->scanBackupDir(BMI_BACKUPS);
159
160 for ($i = 0; $i < sizeof($backups); ++$i) {
161
162 $backup = $backups[$i];
163 if (!file_exists($backup['path'])) continue;
164 $manifest = $this->getManifestFromZip($backup['path'] . '/' . $backup['filename'], $zipper);
165 if ($manifest) $manifests[$backup['filename']] = $manifest;
166
167 }
168
169 if (defined('BMI_BACKUP_PRO') && defined('BMI_PRO_INC')) {
170 $proPath = BMI_PRO_INC . 'external/controller.php';
171 if (file_exists($proPath)) {
172 require_once $proPath;
173 $externalStorage = new ExternalStorage();
174 $external = $externalStorage->getExternalBackups();
175 }
176 }
177
178 return [ 'local' => $manifests, 'external' => $external, 'ongoing' => $ongoing ];
179
180 }
181
182 }
183