PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.0
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.0
6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 5.2.2 5.3 All 153 releases
mainwp / class / class-mainwp-backup-handler.php

class-mainwp-backup-handler.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 5.0, at class/class-mainwp-backup-handler.php

1,206 lines 42.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP Backup Handler.
4 *
5 * @package MainWP/Dashboard
6 */
7
8 namespace MainWP\Dashboard;
9
10 /**
11 * Class MainWP_Backup_Handler
12 *
13 * @package MainWP\Dashboard
14 */
15 class MainWP_Backup_Handler {
16
17 /**
18 * Method get_class_name()
19 *
20 * Get Class Name.
21 *
22 * @return string __CLASS__
23 */
24 public static function get_class_name() {
25 return __CLASS__;
26 }
27
28 /**
29 * Method backup_site()
30 *
31 * Backup Child Site.
32 *
33 * @param mixed $siteid Child Site ID.
34 * @param mixed $pTask Task to perform.
35 * @param mixed $subfolder Sub folder to place backup.
36 *
37 * @gobal object $wp_filesystem WordPress filesystem instance.
38 *
39 * @return mixed $backup_result
40 * @throws MainWP_Exception Error message.
41 *
42 * @uses \MainWP\Dashboard\MainWP_Connect::fetch_url_authed()
43 * @uses \MainWP\Dashboard\MainWP_Connect::download_to_file()
44 * @uses \MainWP\Dashboard\MainWP_Connect::get_get_data_authed()
45 * @uses \MainWP\Dashboard\MainWP_DB_Backup::get_backup_task_progress()
46 * @uses \MainWP\Dashboard\MainWP_DB_Backup::add_backup_task_progress()
47 * @uses \MainWP\Dashboard\MainWP_DB_Backup::update_backup_task_progress()
48 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
49 * @uses \MainWP\Dashboard\MainWP_Exception
50 * @uses \MainWP\Dashboard\MainWP_Logger::debug_for_website()
51 * @uses \MainWP\Dashboard\MainWP_Logger::warning_for_website()
52 * @uses \MainWP\Dashboard\MainWP_System::is_single_user()
53 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_wp_file_system()
54 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_mainwp_specific_dir()
55 * @uses \MainWP\Dashboard\MainWP_Utility::sanitize()
56 * @uses \MainWP\Dashboard\MainWP_Utility::date()
57 * @uses \MainWP\Dashboard\MainWP_Utility::remove_preslash_spaces()
58 * @uses \MainWP\Dashboard\MainWP_Utility::normalize_filename()
59 * @uses \MainWP\Dashboard\MainWP_Utility::value_to_string()
60 * @uses \MainWP\Dashboard\self::get_real_extension()
61 */
62 public static function backup_site( $siteid, $pTask, $subfolder ) {
63 // phpcs: ignore -- Current complexity is the only way to achieve desired results, pull request solutions appreciated.
64 if ( ! get_option( 'mainwp_enableLegacyBackupFeature' ) ) {
65 return false;
66 }
67
68 $userid = $pTask->userid;
69 $type = $pTask->type;
70 $exclude = $pTask->exclude;
71 $taskId = $pTask->id;
72 $excludebackup = $pTask->excludebackup;
73 $excludecache = $pTask->excludecache;
74 $excludenonwp = $pTask->excludenonwp;
75 $excludezip = $pTask->excludezip;
76 $pFilename = $pTask->filename;
77
78 if ( '' === trim( $pFilename ) ) {
79 $pFilename = null;
80 }
81
82 $backup_result = array();
83
84 // Creating a backup.
85 $website = MainWP_DB::instance()->get_website_by_id( $siteid );
86 $subfolder = str_replace( '%sitename%', MainWP_Utility::sanitize( $website->name ), $subfolder );
87 $subfolder = str_replace( '%url%', MainWP_Utility::sanitize( MainWP_Utility::get_nice_url( $website->url ) ), $subfolder );
88 $subfolder = str_replace( '%type%', $type, $subfolder );
89 $subfolder = str_replace( '%date%', MainWP_Utility::date( 'Ymd' ), $subfolder );
90 $subfolder = str_replace( '%task%', '', $subfolder );
91 $subfolder = str_replace( '%', '', $subfolder );
92 $subfolder = MainWP_Utility::remove_preslash_spaces( $subfolder );
93 $subfolder = MainWP_Utility::normalize_filename( $subfolder );
94
95 if ( ! MainWP_System::instance()->is_single_user() && ( $userid !== $website->userid ) ) {
96 throw new MainWP_Exception( 'Undefined error.' );
97 }
98
99 $websiteCleanUrl = $website->url;
100 if ( '/' === substr( $websiteCleanUrl, - 1 ) ) {
101 $websiteCleanUrl = substr( $websiteCleanUrl, 0, - 1 );
102 }
103 $websiteCleanUrl = str_replace(
104 array( 'http://', 'https://', '/' ),
105 array( '', '', '-' ),
106 $websiteCleanUrl
107 );
108
109 if ( 'db' === $type ) {
110 $ext = '.sql.' . self::get_current_archive_extension( $website, $pTask );
111 } else {
112 $ext = '.' . self::get_current_archive_extension( $website, $pTask );
113 }
114
115 $file = str_replace(
116 array(
117 '%sitename%',
118 '%url%',
119 '%date%',
120 '%time%',
121 '%type%',
122 ),
123 array(
124 MainWP_Utility::sanitize( $website->name ),
125 $websiteCleanUrl,
126 MainWP_Utility::date( 'm-d-Y' ),
127 MainWP_Utility::date( 'G\hi\ms\s' ),
128 $type,
129 ),
130 $pFilename
131 );
132 $file = str_replace( '%', '', $file );
133 $file = MainWP_Utility::normalize_filename( $file );
134
135 if ( ! empty( $file ) ) {
136 $file .= $ext;
137 }
138
139 if ( 'zip' === $pTask->archiveFormat ) {
140 $loadFilesBeforeZip = (int) $pTask->loadFilesBeforeZip;
141 } elseif ( '' === $pTask->archiveFormat || 'site' === $pTask->archiveFormat ) {
142 $loadFilesBeforeZip = (int) $website->loadFilesBeforeZip;
143 } else {
144 $loadFilesBeforeZip = 1;
145 }
146
147 if ( 1 === $loadFilesBeforeZip ) {
148 $loadFilesBeforeZip = get_option( 'mainwp_options_loadFilesBeforeZip' );
149 $loadFilesBeforeZip = ( 1 === (int) $loadFilesBeforeZip || false === $loadFilesBeforeZip );
150 } else {
151 $loadFilesBeforeZip = ( 2 === $loadFilesBeforeZip );
152 }
153
154 if ( ( 'zip' === $pTask->archiveFormat ) && ( 1 === (int) $pTask->maximumFileDescriptorsOverride ) ) {
155 $maximumFileDescriptorsAuto = ( 1 === (int) $pTask->maximumFileDescriptorsAuto );
156 $maximumFileDescriptors = $pTask->maximumFileDescriptors;
157 } elseif ( ( '' === $pTask->archiveFormat || 'site' === $pTask->archiveFormat ) && ( 1 === (int) $website->maximumFileDescriptorsOverride ) ) {
158 $maximumFileDescriptorsAuto = ( 1 === (int) $website->maximumFileDescriptorsAuto );
159 $maximumFileDescriptors = $website->maximumFileDescriptors;
160 } else {
161 $maximumFileDescriptorsAuto = get_option( 'mainwp_maximumFileDescriptorsAuto' );
162 $maximumFileDescriptors = get_option( 'mainwp_maximumFileDescriptors' );
163 $maximumFileDescriptors = ( false === $maximumFileDescriptors ? 150 : $maximumFileDescriptors );
164 }
165
166 $information = false;
167 $backupTaskProgress = MainWP_DB_Backup::instance()->get_backup_task_progress( $taskId, $website->id );
168 if ( empty( $backupTaskProgress ) || ( $backupTaskProgress->dtsFetched < $pTask->last_run ) ) {
169 $start = microtime( true );
170 try {
171 $pid = time();
172
173 if ( empty( $backupTaskProgress ) ) {
174 MainWP_DB_Backup::instance()->add_backup_task_progress( $taskId, $website->id, array() );
175 }
176
177 MainWP_DB_Backup::instance()->update_backup_task_progress(
178 $taskId,
179 $website->id,
180 array(
181 'dtsFetched' => time(),
182 'fetchResult' => wp_json_encode( array() ),
183 'downloadedDB' => '',
184 'downloadedDBComplete' => 0,
185 'downloadedFULL' => '',
186 'downloadedFULLComplete' => 0,
187 'removedFiles' => 0,
188 'attempts' => 0,
189 'last_error' => '',
190 'pid' => $pid,
191 )
192 );
193
194 $params = array(
195 'type' => $type,
196 'exclude' => $exclude,
197 'excludebackup' => $excludebackup,
198 'excludecache' => $excludecache,
199 'excludenonwp' => $excludenonwp,
200 'excludezip' => $excludezip,
201 'ext' => self::get_current_archive_extension( $website, $pTask ),
202 'file_descriptors_auto' => $maximumFileDescriptorsAuto,
203 'file_descriptors' => $maximumFileDescriptors,
204 'loadFilesBeforeZip' => $loadFilesBeforeZip,
205 'pid' => $pid,
206 'f' => $file,
207 );
208
209 MainWP_Logger::instance()->debug_for_website( $website, 'backup', 'Requesting backup: ' . MainWP_Utility::value_to_string( $params, 1 ) );
210
211 $information = MainWP_Connect::fetch_url_authed( $website, 'backup', $params, false, false, false );
212 } catch ( MainWP_Exception $e ) {
213 MainWP_Logger::instance()->warning_for_website( $website, 'backup', 'ERROR: ' . $e->getMessage() . ' (' . $e->get_message_extra() . ')' );
214 $stop = microtime( true );
215 // Bigger then 30 seconds means a timeout.
216 if ( 30 < ( $stop - $start ) ) {
217 MainWP_DB_Backup::instance()->update_backup_task_progress(
218 $taskId,
219 $website->id,
220 array(
221 'last_error' => wp_json_encode(
222 array(
223 'message' => $e->getMessage(),
224 'extra' => $e->get_message_extra(),
225 )
226 ),
227 )
228 );
229
230 return false;
231 }
232
233 throw $e;
234 }
235
236 if ( isset( $information['error'] ) && stristr( $information['error'], 'Another backup process is running' ) ) {
237 return false;
238 }
239
240 $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'fetchResult' => wp_json_encode( $information ) ) );
241 } elseif ( empty( $backupTaskProgress->fetchResult ) ) {
242 try {
243 $temp = MainWP_Connect::fetch_url_authed( $website, 'backup_checkpid', array( 'pid' => $backupTaskProgress->pid ) );
244 } catch ( \Exception $e ) {
245 // ok!
246 }
247
248 if ( ! empty( $temp ) ) {
249 if ( 'stalled' === $temp['status'] ) {
250 if ( 5 > $backupTaskProgress->attempts ) {
251 $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'attempts' => $backupTaskProgress->attempts ++ ) );
252
253 try {
254 $information = MainWP_Connect::fetch_url_authed(
255 $website,
256 'backup',
257 array(
258 'type' => $type,
259 'exclude' => $exclude,
260 'excludebackup' => $excludebackup,
261 'excludecache' => $excludecache,
262 'excludenonwp' => $excludenonwp,
263 'excludezip' => $excludezip,
264 'ext' => self::get_current_archive_extension( $website, $pTask ),
265 'file_descriptors_auto' => $maximumFileDescriptorsAuto,
266 'file_descriptors' => $maximumFileDescriptors,
267 'loadFilesBeforeZip' => $loadFilesBeforeZip,
268 'pid' => $backupTaskProgress->pid,
269 'append' => '1',
270 'f' => $temp['file'],
271 ),
272 false,
273 false,
274 false
275 );
276
277 if ( isset( $information['error'] ) && stristr( $information['error'], 'Another backup process is running' ) ) {
278 MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'attempts' => ( $backupTaskProgress->attempts - 1 ) ) );
279
280 return false;
281 }
282 } catch ( MainWP_Exception $e ) {
283 return false;
284 }
285
286 $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'fetchResult' => wp_json_encode( $information ) ) );
287 } else {
288 throw new MainWP_Exception( 'Backup failed after 5 retries.' );
289 }
290 } elseif ( 'invalid' === $temp['status'] ) {
291 $error = json_decode( $backupTaskProgress->last_error );
292
293 if ( ! is_array( $error ) ) {
294 throw new MainWP_Exception( 'Backup failed.' );
295 } else {
296 throw new MainWP_Exception( $error['message'], $error['extra'] ); //phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
297 }
298 } elseif ( 'busy' === $temp['status'] ) {
299 return false;
300 } elseif ( 'done' === $temp['status'] ) {
301 if ( 'full' === $type ) {
302 $information['full'] = $temp['file'];
303 $information['db'] = false;
304 } else {
305 $information['full'] = false;
306 $information['db'] = $temp['file'];
307 }
308
309 $information['size'] = $temp['size'];
310
311 $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'fetchResult' => wp_json_encode( $information ) ) );
312 }
313 } elseif ( 5 > $backupTaskProgress->attempts ) {
314 $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'attempts' => $backupTaskProgress->attempts ++ ) );
315 } else {
316 throw new MainWP_Exception( 'Backup failed after 5 retries.' );
317 }
318 }
319
320 if ( false === $information ) {
321 $information = $backupTaskProgress->fetchResult;
322 }
323
324 if ( isset( $information['error'] ) ) {
325 throw new MainWP_Exception( esc_html( $information['error'] ) );
326 } elseif ( 'db' === $type && ! $information['db'] ) {
327 throw new MainWP_Exception( 'Database backup failed.' );
328 } elseif ( 'full' === $type && ! $information['full'] ) {
329 throw new MainWP_Exception( 'Full backup failed.' );
330 } elseif ( isset( $information['db'] ) ) {
331 $hasWPFileSystem = MainWP_System_Utility::get_wp_file_system();
332
333 /**
334 * WordPress filesystem instance.
335 *
336 * @global object
337 */
338 global $wp_filesystem;
339
340 $dir = MainWP_System_Utility::get_mainwp_specific_dir( $website->id );
341
342 $wp_filesystem->mkdir( $dir, 0777 );
343
344 if ( ! $wp_filesystem->exists( $dir . 'index.php' ) ) {
345 $wp_filesystem->touch( $dir . 'index.php' );
346 }
347
348 // Clean old backups from our system.
349 $maxBackups = get_option( 'mainwp_backupsOnServer' );
350 if ( false === $maxBackups ) {
351 $maxBackups = 1;
352 }
353
354 if ( 1 !== $backupTaskProgress->removedFiles ) {
355 $dbBackups = array();
356 $fullBackups = array();
357 if ( $wp_filesystem->exists( $dir ) ) {
358 $dh = opendir( $dir );
359 if ( $dh ) {
360 while ( false !== ( $file = readdir( $dh ) ) ) {
361 if ( '.' !== $file && '..' !== $file ) {
362 $theFile = $dir . $file;
363 if ( $information['db'] && self::is_sql_file( $file ) ) {
364 $dbBackups[ filemtime( $theFile ) . $file ] = $theFile;
365 }
366
367 if ( $information['full'] && self::is_archive( $file ) && ! self::is_sql_archive( $file ) ) {
368 $fullBackups[ filemtime( $theFile ) . $file ] = $theFile;
369 }
370 }
371 }
372 closedir( $dh );
373 }
374 }
375 krsort( $dbBackups );
376 krsort( $fullBackups );
377
378 $cnt = 0;
379 foreach ( $dbBackups as $key => $dbBackup ) {
380 ++$cnt;
381 if ( $cnt >= $maxBackups ) {
382 $wp_filesystem->delete( $dbBackup );
383 }
384 }
385
386 $cnt = 0;
387 foreach ( $fullBackups as $key => $fullBackup ) {
388 ++$cnt;
389 if ( $cnt >= $maxBackups ) {
390 $wp_filesystem->delete( $fullBackup );
391 }
392 }
393 $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'removedFiles' => 1 ) );
394 }
395
396 $localBackupFile = null;
397
398 $fm_date = MainWP_Utility::sanitize_file_name( MainWP_Utility::date( get_option( 'date_format' ) ) );
399 $fm_time = MainWP_Utility::sanitize_file_name( MainWP_Utility::date( get_option( 'time_format' ) ) );
400
401 $what = null;
402 $regexBackupFile = null;
403
404 if ( $information['db'] ) {
405 $what = 'db';
406 $regexBackupFile = 'db-' . $websiteCleanUrl . '-(.*)-(.*).sql(\.zip|\.tar|\.tar\.gz|\.tar\.bz2)?';
407 if ( '' === $backupTaskProgress->downloadedDB ) {
408 $localBackupFile = $dir . 'db-' . $websiteCleanUrl . '-' . $fm_date . '-' . $fm_time;
409
410 if ( null !== $pFilename ) {
411 $filename = str_replace(
412 array(
413 '%sitename%',
414 '%url%',
415 '%date%',
416 '%time%',
417 '%type%',
418 ),
419 array(
420 MainWP_Utility::sanitize( $website->name ),
421 $websiteCleanUrl,
422 $fm_date,
423 $fm_time,
424 $what,
425 ),
426 $pFilename
427 );
428 $filename = str_replace( '%', '', $filename );
429 $localBackupFile = $dir . $filename;
430 }
431 $localBackupFile .= self::get_real_extension( $information['db'] );
432
433 $localBackupFile = MainWP_Utility::normalize_filename( $localBackupFile );
434
435 $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'downloadedDB' => $localBackupFile ) );
436 } else {
437 $localBackupFile = $backupTaskProgress->downloadedDB;
438 }
439
440 if ( 0 === (int) $backupTaskProgress->downloadedDBComplete ) {
441 MainWP_Connect::download_to_file( MainWP_Connect::get_get_data_authed( $website, $information['db'], 'fdl' ), $localBackupFile, $information['size'], $website->http_user, $website->http_pass );
442 $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'downloadedDBComplete' => 1 ) );
443 }
444 }
445
446 if ( $information['full'] ) {
447 $realExt = self::get_real_extension( $information['full'] );
448 $what = 'full';
449 $regexBackupFile = 'full-' . $websiteCleanUrl . '-(.*)-(.*).(zip|tar|tar.gz|tar.bz2)';
450 if ( '' === $backupTaskProgress->downloadedFULL ) {
451 $localBackupFile = $dir . 'full-' . $websiteCleanUrl . '-' . $fm_date . '-' . $fm_time . $realExt;
452
453 if ( null !== $pFilename ) {
454 $filename = str_replace(
455 array(
456 '%sitename%',
457 '%url%',
458 '%date%',
459 '%time%',
460 '%type%',
461 ),
462 array(
463 MainWP_Utility::sanitize( $website->name ),
464 $websiteCleanUrl,
465 $fm_date,
466 $fm_time,
467 $what,
468 ),
469 $pFilename
470 );
471 $filename = str_replace( '%', '', $filename );
472 $localBackupFile = $dir . $filename . $realExt;
473 }
474
475 $localBackupFile = MainWP_Utility::normalize_filename( $localBackupFile );
476
477 $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'downloadedFULL' => $localBackupFile ) );
478 } else {
479 $localBackupFile = $backupTaskProgress->downloadedFULL;
480 }
481
482 if ( 0 === (int) $backupTaskProgress->downloadedFULLComplete ) {
483 if ( file_exists( $localBackupFile ) ) {
484 $time = filemtime( $localBackupFile );
485
486 $minutes = gmdate( 'i', time() );
487 $seconds = gmdate( 's', time() );
488
489 $file_minutes = gmdate( 'i', $time );
490 $file_seconds = gmdate( 's', $time );
491
492 $minuteDiff = $minutes - $file_minutes;
493 if ( 59 === $minuteDiff ) {
494 $minuteDiff = 1;
495 }
496 $secondsdiff = ( $minuteDiff * 60 ) + $seconds - $file_seconds;
497
498 if ( 60 > $secondsdiff ) {
499 // still downloading.
500 return false;
501 }
502 }
503
504 MainWP_Connect::download_to_file( MainWP_Connect::get_get_data_authed( $website, $information['full'], 'fdl' ), $localBackupFile, $information['size'], $website->http_user, $website->http_pass );
505 MainWP_Connect::fetch_url_authed( $website, 'delete_backup', array( 'del' => $information['full'] ) );
506 $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'downloadedFULLComplete' => 1 ) );
507 }
508 }
509
510 $unique = $pTask->last_run;
511
512 do_action( 'mainwp_postprocess_backup_site', $localBackupFile, $what, $subfolder, $regexBackupFile, $website, $taskId, $unique );
513 $extra_result = apply_filters( 'mainwp_postprocess_backup_sites_feedback', array(), $unique );
514 if ( is_array( $extra_result ) ) {
515 foreach ( $extra_result as $key => $value ) {
516 $backup_result[ $key ] = $value;
517 }
518 }
519 } else {
520 throw new MainWP_Exception( 'Database backup failed due to an undefined error.' );
521 }
522
523 return $backup_result;
524 }
525
526 /**
527 * Method backup_download_file()
528 *
529 * Download backup file.
530 *
531 * @param mixed $pSiteId Child Site ID.
532 * @param mixed $pType full|db Type of backup.
533 * @param mixed $pUrl Backup location.
534 * @param mixed $pFile Backup File.
535 *
536 * @return bool true|false
537 * @throws MainWP_Exception Error message.
538 *
539 * @uses \MainWP\Dashboard\MainWP_Connect::get_get_data_authed()
540 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
541 * @uses \MainWP\Dashboard\MainWP_Connect::download_to_file()
542 * @uses \MainWP\Dashboard\MainWP_Utility::end_session()
543 */
544 public static function backup_download_file( $pSiteId, $pType, $pUrl, $pFile ) {
545 $hasWPFileSystem = MainWP_System_Utility::get_wp_file_system();
546
547 /**
548 * WordPress filesystem instance.
549 *
550 * @global object
551 */
552 global $wp_filesystem;
553
554 $dir = dirname( $pFile ) . '/';
555 $wp_filesystem->mkdir( $dir, 0777 );
556 if ( ! $wp_filesystem->exists( $dir . 'index.php' ) ) {
557 $wp_filesystem->touch( $dir . 'index.php' );
558 }
559 // Clean old backups from our system.
560 $maxBackups = get_option( 'mainwp_backupsOnServer' );
561 if ( false === $maxBackups ) {
562 $maxBackups = 1;
563 }
564
565 $dbBackups = array();
566 $fullBackups = array();
567
568 if ( file_exists( $dir ) ) {
569 $dh = opendir( $dir );
570 if ( $dh ) {
571 while ( false !== ( $file = readdir( $dh ) ) ) {
572 if ( '.' !== $file && '..' !== $file ) {
573 $theFile = $dir . $file;
574 if ( 'db' === $pType && self::is_sql_file( $file ) ) {
575 $dbBackups[ filemtime( $theFile ) . $file ] = $theFile;
576 }
577
578 if ( 'full' === $pType && self::is_archive( $file ) && ! self::is_sql_archive( $file ) ) {
579 $fullBackups[ filemtime( $theFile ) . $file ] = $theFile;
580 }
581 }
582 }
583 closedir( $dh );
584 }
585 }
586 krsort( $dbBackups );
587 krsort( $fullBackups );
588
589 $cnt = 0;
590 foreach ( $dbBackups as $key => $dbBackup ) {
591 ++$cnt;
592 if ( $cnt >= $maxBackups ) {
593 $wp_filesystem->delete( $dbBackup );
594 }
595 }
596
597 $cnt = 0;
598 foreach ( $fullBackups as $key => $fullBackup ) {
599 ++$cnt;
600 if ( $cnt >= $maxBackups ) {
601 $wp_filesystem->delete( $fullBackup );
602 }
603 }
604
605 $website = MainWP_DB::instance()->get_website_by_id( $pSiteId );
606 MainWP_Utility::end_session();
607
608 $what = null;
609 if ( 'db' === $pType ) {
610 MainWP_Connect::download_to_file( MainWP_Connect::get_get_data_authed( $website, $pUrl, 'fdl' ), $pFile, false, $website->http_user, $website->http_pass );
611 }
612
613 if ( 'full' === $pType ) {
614 MainWP_Connect::download_to_file( MainWP_Connect::get_get_data_authed( $website, $pUrl, 'fdl' ), $pFile, false, $website->http_user, $website->http_pass );
615 }
616
617 return true;
618 }
619
620 /**
621 * Method backup_delete_file().
622 *
623 * Delete backup file.
624 *
625 * @param mixed $pSiteId Child Site ID.
626 * @param mixed $pFile File to delete.
627 *
628 * @return bool true
629 *
630 * @uses \MainWP\Dashboard\MainWP_Connect::fetch_url_authed()
631 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
632 */
633 public static function backup_delete_file( $pSiteId, $pFile ) {
634 $website = MainWP_DB::instance()->get_website_by_id( $pSiteId );
635 MainWP_Connect::fetch_url_authed( $website, 'delete_backup', array( 'del' => $pFile ) );
636
637 return true;
638 }
639
640 /**
641 * Method backup_check_pid()
642 *
643 * Check backup pid.
644 *
645 * @param mixed $pSiteId Child Site id.
646 * @param mixed $pid Backup pid.
647 * @param mixed $type full|db Type of backup.
648 * @param mixed $subfolder Sub folder for backup.
649 * @param mixed $pFilename Backups filename.
650 *
651 * @return array $status, $result.
652 * @throws \Exception Error message.
653 *
654 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_mainwp_specific_dir()
655 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
656 * @uses \MainWP\Dashboard\MainWP_Utility::end_session()
657 * @uses \MainWP\Dashboard\MainWP_Utility::sanitize()
658 * @uses \MainWP\Dashboard\MainWP_Utility::get_nice_url()
659 * @uses \MainWP\Dashboard\MainWP_Utility::date()
660 * @uses \MainWP\Dashboard\MainWP_Utility::remove_preslash_spaces()
661 * @uses \MainWP\Dashboard\MainWP_Utility::normalize_filename()
662 * @uses \MainWP\Dashboard\MainWP_Utility::sanitize_file_name()
663 * @uses \MainWP\Dashboard\self::get_real_extension()
664 */
665 public static function backup_check_pid( $pSiteId, $pid, $type, $subfolder, $pFilename ) {
666 $website = MainWP_DB::instance()->get_website_by_id( $pSiteId );
667
668 MainWP_Utility::end_session();
669 $information = MainWP_Connect::fetch_url_authed( $website, 'backup_checkpid', array( 'pid' => $pid ) );
670
671 $status = $information['status'];
672
673 $result = isset( $information['file'] ) ? array( 'file' => $information['file'] ) : array();
674 if ( 'done' === $status ) {
675 $result['file'] = $information['file'];
676 $result['size'] = $information['size'];
677
678 $subfolder = str_replace( '%sitename%', MainWP_Utility::sanitize( $website->name ), $subfolder );
679 $subfolder = str_replace( '%url%', MainWP_Utility::sanitize( MainWP_Utility::get_nice_url( $website->url ) ), $subfolder );
680 $subfolder = str_replace( '%type%', $type, $subfolder );
681 $subfolder = str_replace( '%date%', MainWP_Utility::date( 'Ymd' ), $subfolder );
682 $subfolder = str_replace( '%task%', '', $subfolder );
683 $subfolder = str_replace( '%', '', $subfolder );
684 $subfolder = MainWP_Utility::remove_preslash_spaces( $subfolder );
685 $subfolder = MainWP_Utility::normalize_filename( $subfolder );
686
687 $result['subfolder'] = $subfolder;
688
689 $websiteCleanUrl = $website->url;
690 if ( '/' === substr( $websiteCleanUrl, - 1 ) ) {
691 $websiteCleanUrl = substr( $websiteCleanUrl, 0, - 1 );
692 }
693 $websiteCleanUrl = str_replace(
694 array( 'http://', 'https://', '/' ),
695 array(
696 '',
697 '',
698 '-',
699 ),
700 $websiteCleanUrl
701 );
702
703 $dir = MainWP_System_Utility::get_mainwp_specific_dir( $pSiteId );
704
705 $fm_date = MainWP_Utility::sanitize_file_name( MainWP_Utility::date( get_option( 'date_format' ) ) );
706 $fm_time = MainWP_Utility::sanitize_file_name( MainWP_Utility::date( get_option( 'time_format' ) ) );
707
708 if ( 'db' === $type ) {
709 $localBackupFile = $dir . 'db-' . $websiteCleanUrl . '-' . $fm_date . '-' . $fm_time . self::get_real_extension( $information['file'] );
710 $localRegexFile = 'db-' . $websiteCleanUrl . '-(.*)-(.*).sql(\.zip|\.tar|\.tar\.gz|\.tar\.bz2)?';
711 } else {
712 $localBackupFile = $dir . 'full-' . $websiteCleanUrl . '-' . $fm_date . '-' . $fm_time . self::get_real_extension( $information['file'] );
713 $localRegexFile = 'full-' . $websiteCleanUrl . '-(.*)-(.*).(zip|tar|tar.gz|tar.bz2)';
714 }
715
716 if ( null !== $pFilename ) {
717 $filename = str_replace(
718 array(
719 '%sitename%',
720 '%url%',
721 '%date%',
722 '%time%',
723 '%type%',
724 ),
725 array(
726 MainWP_Utility::sanitize( $website->name ),
727 $websiteCleanUrl,
728 $fm_date,
729 $fm_time,
730 $type,
731 ),
732 $pFilename
733 );
734 $filename = str_replace( '%', '', $filename );
735 $localBackupFile = $dir . $filename;
736 $localBackupFile = MainWP_Utility::normalize_filename( $localBackupFile );
737
738 if ( 'db' === $type ) {
739 $localBackupFile .= self::get_real_extension( $information['file'] );
740 } else {
741 $localBackupFile .= self::get_real_extension( $information['file'] );
742 }
743 }
744
745 $result['local'] = $localBackupFile;
746 $result['regexfile'] = $localRegexFile;
747 }
748
749 return array(
750 'status' => $status,
751 'result' => $result,
752 );
753 }
754
755 /**
756 * Method backup()
757 *
758 * Backup Child Site.
759 *
760 * @param mixed $pSiteId Child Site ID.
761 * @param mixed $pType full|db Typ of backup.
762 * @param mixed $pSubfolder Sub folder to store backup.
763 * @param mixed $pExclude Exclusion list.
764 * @param mixed $excludebackup Exclude backup files.
765 * @param mixed $excludecache Exclude cache files.
766 * @param mixed $excludenonwp Exclude no WP Files.
767 * @param mixed $excludezip Exclude Zip files.
768 * @param null $pFilename Name of backup file.
769 * @param string $pFileNameUID File name unique ID.
770 * @param bool $pArchiveFormat Archive format.
771 * @param bool $pMaximumFileDescriptorsOverride Override maximum file descriptors.
772 * @param bool $pMaximumFileDescriptorsAuto Auto maximum file descriptors.
773 * @param bool $pMaximumFileDescriptors Maximum file descriptors.
774 * @param bool $pLoadFilesBeforeZip Load files before zip.
775 * @param bool $pid Backup pid.
776 * @param bool $append Append to backup.
777 *
778 * @return mixed $backup_result
779 * @throws MainWP_Exception Error message.
780 *
781 * @uses \MainWP\Dashboard\MainWP_Connect::fetch_url_authed()
782 * @uses \MainWP\Dashboard\MainWP_Exception
783 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
784 * @uses \MainWP\Dashboard\MainWP_Logger::debug_for_website()
785 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
786 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_mainwp_specific_dir()
787 * @uses \MainWP\Dashboard\MainWP_Utility::sanitize()
788 * @uses \MainWP\Dashboard\MainWP_Utility::date()
789 * @uses \MainWP\Dashboard\MainWP_Utility::remove_preslash_spaces()
790 * @uses \MainWP\Dashboard\MainWP_Utility::normalize_filename()
791 * @uses \MainWP\Dashboard\MainWP_Utility::sanitize_file_name()
792 * @uses \MainWP\Dashboard\MainWP_Utility::end_session()
793 * @uses \MainWP\Dashboard\MainWP_Utility::value_to_string()
794 * @uses \MainWP\Dashboard\self::get_real_extension()
795 */
796 public static function backup(
797 $pSiteId,
798 $pType,
799 $pSubfolder,
800 $pExclude,
801 $excludebackup,
802 $excludecache,
803 $excludenonwp,
804 $excludezip,
805 $pFilename = null,
806 $pFileNameUID = '',
807 $pArchiveFormat = false,
808 $pMaximumFileDescriptorsOverride = false,
809 $pMaximumFileDescriptorsAuto = false,
810 $pMaximumFileDescriptors = false,
811 $pLoadFilesBeforeZip = false,
812 $pid = false,
813 $append = false
814 ) {
815
816 if ( ! get_option( 'mainwp_enableLegacyBackupFeature' ) ) {
817 return false;
818 }
819
820 if ( '' === trim( $pFilename ) ) {
821 $pFilename = null;
822 }
823
824 $backup_result = array();
825
826 $website = MainWP_DB::instance()->get_website_by_id( $pSiteId );
827 $subfolder = str_replace( '%sitename%', MainWP_Utility::sanitize( $website->name ), $pSubfolder );
828 $subfolder = str_replace( '%url%', MainWP_Utility::sanitize( MainWP_Utility::get_nice_url( $website->url ) ), $subfolder );
829 $subfolder = str_replace( '%type%', $pType, $subfolder );
830 $subfolder = str_replace( '%date%', MainWP_Utility::date( 'Ymd' ), $subfolder );
831 $subfolder = str_replace( '%task%', '', $subfolder );
832 $subfolder = str_replace( '%', '', $subfolder );
833 $subfolder = MainWP_Utility::remove_preslash_spaces( $subfolder );
834 $subfolder = MainWP_Utility::normalize_filename( $subfolder );
835
836 if ( MainWP_System_Utility::is_suspended_site( $website ) ) {
837 throw new MainWP_Exception( 'Suspended site.', '', 'SUSPENDED_SITE' );
838 }
839
840 if ( ! MainWP_System_Utility::can_edit_website( $website ) ) {
841 throw new MainWP_Exception( 'You are not allowed to backup this site' );
842 }
843
844 $websiteCleanUrl = $website->url;
845 if ( '/' === substr( $websiteCleanUrl, - 1 ) ) {
846 $websiteCleanUrl = substr( $websiteCleanUrl, 0, - 1 );
847 }
848 $websiteCleanUrl = str_replace(
849 array( 'http://', 'https://', '/' ),
850 array( '', '', '-' ),
851 $websiteCleanUrl
852 );
853
854 if ( false === $pMaximumFileDescriptorsOverride ) {
855 if ( 1 === (int) $website->maximumFileDescriptorsOverride ) {
856 $maximumFileDescriptorsAuto = ( 1 === (int) $website->maximumFileDescriptorsAuto );
857 $maximumFileDescriptors = $website->maximumFileDescriptors;
858 } else {
859 $maximumFileDescriptorsAuto = get_option( 'mainwp_maximumFileDescriptorsAuto' );
860 $maximumFileDescriptors = get_option( 'mainwp_maximumFileDescriptors' );
861 $maximumFileDescriptors = ( false === $maximumFileDescriptors ? 150 : $maximumFileDescriptors );
862 }
863 } elseif ( ( 'global' !== $pArchiveFormat ) && ( 1 === (int) $pMaximumFileDescriptorsOverride ) ) {
864 $maximumFileDescriptorsAuto = ( 1 === $pMaximumFileDescriptorsAuto );
865 $maximumFileDescriptors = $pMaximumFileDescriptors;
866 } else {
867 $maximumFileDescriptorsAuto = get_option( 'mainwp_maximumFileDescriptorsAuto' );
868 $maximumFileDescriptors = get_option( 'mainwp_maximumFileDescriptors' );
869 $maximumFileDescriptors = ( false === $maximumFileDescriptors ? 150 : $maximumFileDescriptors );
870 }
871
872 $file = str_replace(
873 array(
874 '%sitename%',
875 '%url%',
876 '%date%',
877 '%time%',
878 '%type%',
879 ),
880 array(
881 MainWP_Utility::sanitize( $website->name ),
882 $websiteCleanUrl,
883 MainWP_Utility::date( 'm-d-Y' ),
884 MainWP_Utility::date( 'G\hi\ms\s' ),
885 $pType,
886 ),
887 $pFilename
888 );
889 $file = str_replace( '%', '', $file );
890 $file = MainWP_Utility::normalize_filename( $file );
891
892 // Normal flow: check site settings & fallback to global.
893 if ( false === $pLoadFilesBeforeZip ) {
894 $loadFilesBeforeZip = $website->loadFilesBeforeZip;
895 if ( 1 === $loadFilesBeforeZip ) {
896 $loadFilesBeforeZip = get_option( 'mainwp_options_loadFilesBeforeZip' );
897 $loadFilesBeforeZip = ( 1 === $loadFilesBeforeZip || false === $loadFilesBeforeZip );
898 } else {
899 $loadFilesBeforeZip = ( 2 === $loadFilesBeforeZip );
900 }
901 } elseif ( 'global' === $pArchiveFormat || 1 === $pLoadFilesBeforeZip ) { // Overridden flow: only fallback to global.
902 $loadFilesBeforeZip = get_option( 'mainwp_options_loadFilesBeforeZip' );
903 $loadFilesBeforeZip = ( 1 === $loadFilesBeforeZip || false === $loadFilesBeforeZip );
904 } else {
905 $loadFilesBeforeZip = ( 2 === $pLoadFilesBeforeZip );
906 }
907
908 // Normal flow: check site settings & fallback to global.
909 if ( false === $pArchiveFormat ) {
910 $archiveFormat = self::get_current_archive_extension( $website );
911 } elseif ( 'global' === $pArchiveFormat ) {
912 $archiveFormat = self::get_current_archive_extension();
913 } else {
914 $archiveFormat = $pArchiveFormat;
915 }
916
917 MainWP_Utility::end_session();
918
919 $params = array(
920 'type' => $pType,
921 'exclude' => $pExclude,
922 'excludebackup' => $excludebackup,
923 'excludecache' => $excludecache,
924 'excludenonwp' => $excludenonwp,
925 'excludezip' => $excludezip,
926 'ext' => $archiveFormat,
927 'file_descriptors_auto' => $maximumFileDescriptorsAuto,
928 'file_descriptors' => $maximumFileDescriptors,
929 'loadFilesBeforeZip' => $loadFilesBeforeZip,
930 'f' => $file,
931 'fileUID' => $pFileNameUID,
932 'pid' => $pid,
933 'append' => ( $append ? 1 : 0 ),
934 );
935
936 MainWP_Logger::instance()->debug_for_website( $website, 'backup', 'Requesting backup: ' . MainWP_Utility::value_to_string( $params, 1 ) );
937
938 $information = MainWP_Connect::fetch_url_authed( $website, 'backup', $params, false, false, false );
939 do_action( 'mainwp_managesite_backup', $website, array( 'type' => $pType ), $information );
940
941 if ( isset( $information['error'] ) ) {
942 throw new MainWP_Exception( esc_html( $information['error'] ) ); //phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
943 } elseif ( 'db' === $pType && ! $information['db'] ) {
944 throw new MainWP_Exception( 'Database backup failed.' );
945 } elseif ( 'full' === $pType && ! $information['full'] ) {
946 throw new MainWP_Exception( 'Full backup failed.' );
947 } elseif ( isset( $information['db'] ) ) {
948 if ( false !== $information['db'] ) {
949 $backup_result['url'] = $information['db'];
950 $backup_result['type'] = 'db';
951 } elseif ( false !== $information['full'] ) {
952 $backup_result['url'] = $information['full'];
953 $backup_result['type'] = 'full';
954 }
955
956 if ( isset( $information['size'] ) ) {
957 $backup_result['size'] = $information['size'];
958 }
959 $backup_result['subfolder'] = $subfolder;
960
961 $dir = MainWP_System_Utility::get_mainwp_specific_dir( $pSiteId );
962
963 $fm_date = MainWP_Utility::sanitize_file_name( MainWP_Utility::date( get_option( 'date_format' ) ) );
964 $fm_time = MainWP_Utility::sanitize_file_name( MainWP_Utility::date( get_option( 'time_format' ) ) );
965
966 if ( 'db' === $pType ) {
967 $localBackupFile = $dir . 'db-' . $websiteCleanUrl . '-' . $fm_date . '-' . $fm_time . self::get_real_extension( $information['db'] );
968 $localRegexFile = 'db-' . $websiteCleanUrl . '-(.*)-(.*).sql(\.zip|\.tar|\.tar\.gz|\.tar\.bz2)?';
969 } else {
970 $localBackupFile = $dir . 'full-' . $websiteCleanUrl . '-' . $fm_date . '-' . $fm_time . self::get_real_extension( $information['full'] );
971 $localRegexFile = 'full-' . $websiteCleanUrl . '-(.*)-(.*).(zip|tar|tar.gz|tar.bz2)';
972 }
973
974 if ( null !== $pFilename ) {
975 $filename = str_replace(
976 array(
977 '%sitename%',
978 '%url%',
979 '%date%',
980 '%time%',
981 '%type%',
982 ),
983 array(
984 MainWP_Utility::sanitize( $website->name ),
985 $websiteCleanUrl,
986 $fm_date,
987 $fm_time,
988 $pType,
989 ),
990 $pFilename
991 );
992 $filename = str_replace( '%', '', $filename );
993 $localBackupFile = $dir . $filename;
994 $localBackupFile = MainWP_Utility::normalize_filename( $localBackupFile );
995
996 if ( 'db' === $pType ) {
997 $localBackupFile .= self::get_real_extension( $information['db'] );
998 } else {
999 $localBackupFile .= self::get_real_extension( $information['full'] );
1000 }
1001 }
1002
1003 $backup_result['local'] = $localBackupFile;
1004 $backup_result['regexfile'] = $localRegexFile;
1005
1006 return $backup_result;
1007 } else {
1008 throw new MainWP_Exception( 'Database backup failed due to an undefined error' );
1009 }
1010 }
1011
1012 /**
1013 * Method is_archive()
1014 *
1015 * Check if Archive.
1016 *
1017 * @param mixed $pFileName File to check.
1018 * @param string $pPrefix File prefix.
1019 * @param string $pSuffix File suffix.
1020 *
1021 * @return mixed Files that are archives.
1022 */
1023 public static function is_archive( $pFileName, $pPrefix = '', $pSuffix = '' ) {
1024 return preg_match( '/' . $pPrefix . '(.*).(zip|tar|tar.gz|tar.bz2)' . $pSuffix . '$/', $pFileName );
1025 }
1026
1027 /**
1028 * Method is_sql_file()
1029 *
1030 * Check if file is SQL.
1031 *
1032 * @param mixed $pFileName File to check.
1033 *
1034 * @return mixed SQL Files.
1035 */
1036 public static function is_sql_file( $pFileName ) {
1037 return preg_match( '/(.*).sql$/', $pFileName ) || self::is_sql_archive( $pFileName );
1038 }
1039
1040 /**
1041 * Method is_sql_archive()
1042 *
1043 * Check if is SQL Archive.
1044 *
1045 * @param mixed $pFileName File to check.
1046 *
1047 * @return mixed SQL archive.
1048 */
1049 public static function is_sql_archive( $pFileName ) {
1050 return preg_match( '/(.*).sql.(zip|tar|tar.gz|tar.bz2)$/', $pFileName );
1051 }
1052
1053 /**
1054 * Method get_current_archive_extension()
1055 *
1056 * Get extension of current Archive.
1057 *
1058 * @param bool $website true|false.
1059 * @param bool|string $task true|false|global|site.
1060 *
1061 * @return mixed $archiveFormat Format of Archive.
1062 *
1063 * @uses \MainWP\Dashboard\MainWP_DB_Backup::get_website_backup_settings()
1064 */
1065 public static function get_current_archive_extension( $website = false, $task = false ) {
1066 $useSite = true;
1067 if ( false !== $task ) {
1068 if ( 'global' === $task->archiveFormat ) {
1069 $useGlobal = true;
1070 $useSite = false;
1071 } elseif ( '' === $task->archiveFormat || 'site' === $task->archiveFormat ) {
1072 $useGlobal = false;
1073 $useSite = true;
1074 } else {
1075 $archiveFormat = $task->archiveFormat;
1076 $useGlobal = false;
1077 $useSite = false;
1078 }
1079 }
1080
1081 if ( $useSite ) {
1082 if ( false === $website ) {
1083 $useGlobal = true;
1084 } else {
1085 $backupSettings = MainWP_DB_Backup::instance()->get_website_backup_settings( $website->id );
1086 $archiveFormat = $backupSettings->archiveFormat;
1087 $useGlobal = ( 'global' === $archiveFormat );
1088 }
1089 }
1090
1091 if ( $useGlobal ) {
1092 $archiveFormat = get_option( 'mainwp_archiveFormat' );
1093 if ( false === $archiveFormat ) {
1094 $archiveFormat = 'tar.gz';
1095 }
1096 }
1097
1098 return $archiveFormat;
1099 }
1100
1101 /**
1102 * Method get_real_extension()
1103 *
1104 * Get full file extension.
1105 *
1106 * @param mixed $path Path to file.
1107 *
1108 * @return mixed $check|pathinfo()
1109 */
1110 public static function get_real_extension( $path ) {
1111 $checks = array( '.sql.zip', '.sql.tar', '.sql.tar.gz', '.sql.tar.bz2', '.tar.gz', '.tar.bz2' );
1112 foreach ( $checks as $check ) {
1113 if ( MainWP_Utility::ends_with( $path, $check ) ) {
1114 return $check;
1115 }
1116 }
1117
1118 return '.' . pathinfo( $path, PATHINFO_EXTENSION );
1119 }
1120
1121
1122 /**
1123 * Method handle_settings_post()
1124 *
1125 * Update Child Site Settings.
1126 *
1127 * @return bool true|false
1128 *
1129 * @uses \MainWP\Dashboard\MainWP_System_Utility::is_admin()
1130 * @uses \MainWP\Dashboard\MainWP_Utility::update_option()
1131 */
1132 public static function handle_settings_post() {
1133 if ( MainWP_System_Utility::is_admin() ) {
1134 if ( isset( $_POST['submit'] ) && isset( $_POST['wp_nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['wp_nonce'] ), 'Settings' ) ) {
1135 if ( isset( $_POST['mainwp_options_backupOnServer'] ) && 0 < $_POST['mainwp_options_backupOnServer'] ) {
1136 MainWP_Utility::update_option( 'mainwp_backupsOnServer', ( isset( $_POST['mainwp_options_backupOnServer'] ) ? intval( $_POST['mainwp_options_backupOnServer'] ) : 1 ) );
1137 }
1138 if ( isset( $_POST['mainwp_options_maximumFileDescriptors'] ) && - 1 < $_POST['mainwp_options_maximumFileDescriptors'] ) {
1139 MainWP_Utility::update_option( 'mainwp_maximumFileDescriptors', ( isset( $_POST['mainwp_options_maximumFileDescriptors'] ) ? intval( $_POST['mainwp_options_maximumFileDescriptors'] ) : 1 ) );
1140 }
1141 MainWP_Utility::update_option( 'mainwp_maximumFileDescriptorsAuto', ( ! isset( $_POST['mainwp_maximumFileDescriptorsAuto'] ) ? 0 : 1 ) );
1142 if ( ! empty( $_POST['mainwp_options_backupOnExternalSources'] ) && 0 <= $_POST['mainwp_options_backupOnExternalSources'] ) {
1143 MainWP_Utility::update_option( 'mainwp_backupOnExternalSources', ( isset( $_POST['mainwp_options_backupOnExternalSources'] ) ? intval( $_POST['mainwp_options_backupOnExternalSources'] ) : 1 ) );
1144 }
1145 MainWP_Utility::update_option( 'mainwp_archiveFormat', ( isset( $_POST['mainwp_archiveFormat'] ) ? sanitize_text_field( wp_unslash( $_POST['mainwp_archiveFormat'] ) ) : '' ) );
1146
1147 $old_primaryBackup = get_option( 'mainwp_primaryBackup' );
1148 $old_enableLegacyBackup = get_option( 'mainwp_enableLegacyBackupFeature' );
1149 $updated_enableLegacyBackup = false;
1150
1151 if ( isset( $_POST['mainwp_primaryBackup'] ) ) {
1152 if ( ! empty( $_POST['mainwp_primaryBackup'] ) ) { // not default backup method.
1153 MainWP_Utility::update_option( 'mainwp_notificationOnBackupFail', 0 );
1154 MainWP_Utility::update_option( 'mainwp_notificationOnBackupStart', 0 );
1155 MainWP_Utility::update_option( 'mainwp_chunkedBackupTasks', 0 );
1156 if ( empty( $old_primaryBackup ) ) {
1157 MainWP_Utility::update_option( 'mainwp_enableLegacyBackupFeature', 0 );
1158 $updated_enableLegacyBackup = true;
1159 }
1160 }
1161 MainWP_Utility::update_option( 'mainwp_primaryBackup', sanitize_text_field( wp_unslash( $_POST['mainwp_primaryBackup'] ) ) );
1162 }
1163
1164 if ( ! isset( $_POST['mainwp_primaryBackup'] ) || empty( $_POST['mainwp_primaryBackup'] ) ) {
1165 MainWP_Utility::update_option( 'mainwp_options_loadFilesBeforeZip', ( ! isset( $_POST['mainwp_options_loadFilesBeforeZip'] ) ? 0 : 1 ) );
1166 MainWP_Utility::update_option( 'mainwp_notificationOnBackupFail', ( ! isset( $_POST['mainwp_options_notificationOnBackupFail'] ) ? 0 : 1 ) );
1167 MainWP_Utility::update_option( 'mainwp_notificationOnBackupStart', ( ! isset( $_POST['mainwp_options_notificationOnBackupStart'] ) ? 0 : 1 ) );
1168 MainWP_Utility::update_option( 'mainwp_chunkedBackupTasks', ( ! isset( $_POST['mainwp_options_chunkedBackupTasks'] ) ? 0 : 1 ) );
1169 }
1170
1171 $enableLegacyBackup = ( isset( $_POST['mainwp_options_enableLegacyBackupFeature'] ) && ! empty( $_POST['mainwp_options_enableLegacyBackupFeature'] ) ) ? 1 : 0;
1172 if ( $enableLegacyBackup && empty( $old_enableLegacyBackup ) ) {
1173 MainWP_Utility::update_option( 'mainwp_primaryBackup', '' );
1174 }
1175
1176 if ( ! $updated_enableLegacyBackup ) {
1177 MainWP_Utility::update_option( 'mainwp_enableLegacyBackupFeature', $enableLegacyBackup );
1178 }
1179 return true;
1180 }
1181 }
1182 return false;
1183 }
1184
1185 /**
1186 * Method backup_get_file_size()
1187 *
1188 * Get file size.
1189 *
1190 * @param mixed $pFile Backup file.
1191 *
1192 * @return false|int
1193 *
1194 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_mainwp_specific_dir()
1195 */
1196 public static function backup_get_file_size( $pFile ) {
1197 $dir = MainWP_System_Utility::get_mainwp_specific_dir();
1198
1199 if ( stristr( $pFile, $dir ) && file_exists( $pFile ) ) {
1200 return filesize( $pFile );
1201 }
1202
1203 return 0;
1204 }
1205 }
1206