PluginProbe ʕ •ᴥ•ʔ
Backup Migration / 2.1.7
Backup Migration v2.1.7
2.1.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 / progress / zip.php
backup-backup / includes / progress Last commit date
class-file-progress-storage.php 3 weeks ago interface-progress-storage.php 3 weeks ago logger-only.php 3 weeks ago migration.php 3 weeks ago staging.php 3 weeks ago zip.php 3 weeks ago
zip.php
219 lines
1 <?php
2
3 // Namespace
4 namespace BMI\Plugin\Progress;
5
6 // Use
7 use BMI\Plugin\BMI_Logger AS Logger;
8
9 // Exit on direct access
10 if (!defined('ABSPATH')) exit;
11
12 /**
13 * Main File Backup Logs
14 */
15 class BMI_ZipProgress {
16
17 public $name;
18 public $date;
19 public $millis;
20 public $cron;
21 public $logfilename;
22 public $latest;
23 public $latest_progress;
24 public $files;
25 public $bytes;
26 public $total_queries;
27 public $file;
28 public $progress;
29 public $muted = false;
30 public $clearEndCodes = false;
31
32 public function __construct($backup_name, $files = 0, $bytes = 0, $cron = false, $reset = true, $clearEndCodes = false) {
33
34 if (!file_exists(BMI_BACKUPS)) mkdir(BMI_BACKUPS, 0755, true);
35
36 $this->name = $backup_name;
37 $this->date = date('Y-m-d H:i:s');
38 $this->millis = microtime(true);
39 $this->cron = $cron;
40 $this->logfilename = substr($backup_name, 0, -4) . '.log';
41 $this->latest = BMI_BACKUPS . '/latest.' . BMI_LOGS_SUFFIX . '.log';
42 $this->latest_progress = BMI_BACKUPS . '/latest_progress.' . BMI_LOGS_SUFFIX . '.log';
43 $this->files = $files;
44 $this->bytes = $bytes;
45 $this->total_queries = 1;
46 $this->clearEndCodes = $clearEndCodes;
47
48 if ($reset == true) {
49 if (file_exists($this->latest)) @unlink($this->latest);
50 if (file_exists($this->latest_progress)) @unlink($this->latest_progress);
51 file_put_contents($this->latest_progress, '0/100');
52 }
53
54 if ($this->clearEndCodes) {
55 $this->removeAllEndCodes();
56 }
57
58 }
59
60 public function createManifest($dbBackupEngine = 'v4') {
61
62 global $table_prefix;
63
64 $isBackupStreamed = apply_filters('bmip_streaming_backup_is_enabled', false);
65 $isEncrypted = apply_filters('bmip_is_backup_encrypted', false);
66
67
68 $manifest = array(
69 'name' => $this->name,
70 'date' => $this->date,
71 'files' => $this->files,
72 'bytes' => $this->bytes,
73 'cron' => $this->cron,
74 'total_queries' => $this->total_queries,
75 'manifest' => date('Y-m-d H:i:s'),
76 'millis_start' => $this->millis,
77 'millis_end' => microtime(true),
78 'version' => (defined('BMI_VERSION') ? BMI_VERSION : ''),
79 'domain' => parse_url(home_url())['host'],
80 'dbdomain' => get_option('siteurl'),
81 'uid' => get_current_user_id(),
82 'source_query_output' => (defined('BMI_DB_MAX_ROWS_PER_QUERY') ? BMI_DB_MAX_ROWS_PER_QUERY : ''),
83 'db_backup_engine' => $dbBackupEngine,
84 'multisite' => (defined('MULTISITE') ? MULTISITE : 'false'),
85 'is_encrypted' => $isEncrypted,
86 'config' => array(
87 'ABSPATH' => ABSPATH,
88 'DB_NAME' => (defined('DB_NAME') ? DB_NAME : ''),
89 'DB_USER' => (defined('DB_USER') ? DB_USER : ''),
90 'DB_PASSWORD' => (defined('DB_PASSWORD') ? DB_PASSWORD : ''),
91 'DB_HOST' => (defined('DB_HOST') ? DB_HOST : ''),
92 'DB_CHARSET' => (defined('DB_CHARSET') ? DB_CHARSET : ''),
93 'DB_COLLATE' => (defined('DB_COLLATE') ? DB_COLLATE : ''),
94 'AUTH_KEY' => (defined('AUTH_KEY') ? AUTH_KEY : ''),
95 'SECURE_AUTH_KEY' => (defined('SECURE_AUTH_KEY') ? SECURE_AUTH_KEY : ''),
96 'LOGGED_IN_KEY' => (defined('LOGGED_IN_KEY') ? LOGGED_IN_KEY : ''),
97 'NONCE_KEY' => (defined('NONCE_KEY') ? NONCE_KEY : ''),
98 'AUTH_SALT' => (defined('AUTH_SALT') ? AUTH_SALT : ''),
99 'SECURE_AUTH_SALT' => (defined('SECURE_AUTH_SALT') ? SECURE_AUTH_SALT : ''),
100 'LOGGED_IN_SALT' => (defined('LOGGED_IN_SALT') ? LOGGED_IN_SALT : ''),
101 'NONCE_SALT' => (defined('NONCE_SALT') ? NONCE_SALT : ''),
102 'WP_DEBUG_LOG' => (defined('WP_DEBUG_LOG') ? WP_DEBUG_LOG : ''),
103 'WP_CONTENT_URL' => (defined('WP_CONTENT_URL') ? WP_CONTENT_URL : ''),
104 'WP_CONTENT_DIR' => (defined('WP_CONTENT_DIR') ? trailingslashit(WP_CONTENT_DIR) : ''),
105 'table_prefix' => $table_prefix
106 )
107 );
108
109 if ($isBackupStreamed) {
110 $manifest['is_streamed'] = true;
111 $manifest['chained_hash_chunk_size'] = (defined('CHAINED_HASH_CHUNK_SIZE') ? CHAINED_HASH_CHUNK_SIZE : 5 * 10485760); // 50MB by default
112 $manifest['is_locked'] = $this->cron ? 'unlocked' : 'locked';
113 }
114
115 return json_encode($manifest);
116
117 }
118
119 public function start($muted = false) {
120
121 $this->muted = $muted;
122
123 }
124
125 public function log($log = '', $level = 'INFO') {
126
127 if (!$this->muted) {
128 if (!is_resource($this->file)) {
129 $this->file = @fopen($this->latest, 'a');
130 }
131
132 if (defined('BMI_USING_CLI_FUNCTIONALITY') && BMI_USING_CLI_FUNCTIONALITY === true) {
133 $log_string = '[' . strtoupper($level) . '] [' . date('Y-m-d H:i:s') . '] [CLI] ' . $log . "\n";
134 } else {
135 $log_string = '[' . strtoupper($level) . '] [' . date('Y-m-d H:i:s') . '] ' . $log . "\n";
136 }
137
138 if (is_resource($this->file)) {
139 @fwrite($this->file, $log_string);
140 @fflush($this->file);
141 } else {
142 @file_put_contents($this->latest, $log_string, FILE_APPEND);
143 }
144
145 if (defined('BMI_USING_CLI_FUNCTIONALITY') && BMI_USING_CLI_FUNCTIONALITY === true) {
146 echo esc_html( $log_string );
147 }
148 }
149
150 }
151
152 public function progress($progress = '0') {
153
154 if (!is_resource($this->progress)) {
155 $this->progress = @fopen($this->latest_progress, 'c+');
156 }
157
158 if (is_resource($this->progress)) {
159 @rewind($this->progress);
160 @fwrite($this->progress, $progress);
161 @ftruncate($this->progress, strlen($progress));
162 @fflush($this->progress);
163 } else {
164 @file_put_contents($this->latest_progress, $progress);
165 }
166
167 }
168
169 public function removeAllEndCodes() {
170
171 if (!file_exists($this->latest)) return;
172 $logs = file_get_contents($this->latest);
173 if ($logs === false) return;
174 $logs = explode("\n", $logs);
175
176 foreach ($logs as $line => $string) {
177 if (strpos($string, '[END-CODE]') !== false) {
178 $code = substr($string, 10);
179 $logs[$line] = "[VERBOSE] Logs from there starts as new process, it was continued by smart solution system after applying new plugin settings. (" . $code . ")";
180 }
181 }
182
183 $logs = implode("\n", $logs);
184 file_put_contents($this->latest, $logs);
185 file_put_contents($this->latest_progress, '0/100');
186
187 }
188
189 /**
190 * Close open file handles safely.
191 */
192 public function closeFiles() {
193
194 if (is_resource($this->file)) {
195 @fflush($this->file);
196 @fclose($this->file);
197 $this->file = null;
198 }
199
200 if (is_resource($this->progress)) {
201 @fflush($this->progress);
202 @fclose($this->progress);
203 $this->progress = null;
204 }
205
206 }
207
208 public function end() {
209
210 // fclose($this->file);
211
212 }
213
214 public function __destruct() {
215 $this->closeFiles();
216 }
217
218 }
219