PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 4.4.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v4.4.1
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 4.4.1, at class/class-mainwp-backup-handler.php

1,208 lines 42.3 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
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 = $pTask->loadFilesBeforeZip;
141 } elseif ( '' == $pTask->archiveFormat || 'site' == $pTask->archiveFormat ) {
142 $loadFilesBeforeZip = $website->loadFilesBeforeZip;
143 } else {
144 $loadFilesBeforeZip = 1;
145 }
146
147 if ( 1 === $loadFilesBeforeZip ) {
148 $loadFilesBeforeZip = get_option( 'mainwp_options_loadFilesBeforeZip' );
149 $loadFilesBeforeZip = ( 1 === $loadFilesBeforeZip || false === $loadFilesBeforeZip );
150 } else {
151 $loadFilesBeforeZip = ( 2 === $loadFilesBeforeZip );
152 }
153
154 if ( ( 'zip' == $pTask->archiveFormat ) && ( 1 == $pTask->maximumFileDescriptorsOverride ) ) {
155 $maximumFileDescriptorsAuto = ( 1 == $pTask->maximumFileDescriptorsAuto );
156 $maximumFileDescriptors = $pTask->maximumFileDescriptors;
157 } elseif ( ( '' == $pTask->archiveFormat || 'site' == $pTask->archiveFormat ) && ( 1 == $website->maximumFileDescriptorsOverride ) ) {
158 $maximumFileDescriptorsAuto = ( 1 == $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'] );
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 } else {
314 if ( 5 > $backupTaskProgress->attempts ) {
315 $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'attempts' => $backupTaskProgress->attempts ++ ) );
316 } else {
317 throw new MainWP_Exception( 'Backup failed after 5 retries.' );
318 }
319 }
320 }
321
322 if ( false === $information ) {
323 $information = $backupTaskProgress->fetchResult;
324 }
325
326 if ( isset( $information['error'] ) ) {
327 throw new MainWP_Exception( esc_html( $information['error'] ) );
328 } elseif ( 'db' === $type && ! $information['db'] ) {
329 throw new MainWP_Exception( 'Database backup failed.' );
330 } elseif ( 'full' === $type && ! $information['full'] ) {
331 throw new MainWP_Exception( 'Full backup failed.' );
332 } elseif ( isset( $information['db'] ) ) {
333 $hasWPFileSystem = MainWP_System_Utility::get_wp_file_system();
334
335 /**
336 * WordPress filesystem instance.
337 *
338 * @global object
339 */
340 global $wp_filesystem;
341
342 $dir = MainWP_System_Utility::get_mainwp_specific_dir( $website->id );
343
344 $wp_filesystem->mkdir( $dir, 0777 );
345
346 if ( ! $wp_filesystem->exists( $dir . 'index.php' ) ) {
347 $wp_filesystem->touch( $dir . 'index.php' );
348 }
349
350 // Clean old backups from our system.
351 $maxBackups = get_option( 'mainwp_backupsOnServer' );
352 if ( false === $maxBackups ) {
353 $maxBackups = 1;
354 }
355
356 if ( 1 !== $backupTaskProgress->removedFiles ) {
357 $dbBackups = array();
358 $fullBackups = array();
359 if ( $wp_filesystem->exists( $dir ) ) {
360 $dh = opendir( $dir );
361 if ( $dh ) {
362 while ( false !== ( $file = readdir( $dh ) ) ) {
363 if ( '.' !== $file && '..' !== $file ) {
364 $theFile = $dir . $file;
365 if ( $information['db'] && self::is_sql_file( $file ) ) {
366 $dbBackups[ filemtime( $theFile ) . $file ] = $theFile;
367 }
368
369 if ( $information['full'] && self::is_archive( $file ) && ! self::is_sql_archive( $file ) ) {
370 $fullBackups[ filemtime( $theFile ) . $file ] = $theFile;
371 }
372 }
373 }
374 closedir( $dh );
375 }
376 }
377 krsort( $dbBackups );
378 krsort( $fullBackups );
379
380 $cnt = 0;
381 foreach ( $dbBackups as $key => $dbBackup ) {
382 $cnt ++;
383 if ( $cnt >= $maxBackups ) {
384 $wp_filesystem->delete( $dbBackup );
385 }
386 }
387
388 $cnt = 0;
389 foreach ( $fullBackups as $key => $fullBackup ) {
390 $cnt ++;
391 if ( $cnt >= $maxBackups ) {
392 $wp_filesystem->delete( $fullBackup );
393 }
394 }
395 $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'removedFiles' => 1 ) );
396 }
397
398 $localBackupFile = null;
399
400 $fm_date = MainWP_Utility::sanitize_file_name( MainWP_Utility::date( get_option( 'date_format' ) ) );
401 $fm_time = MainWP_Utility::sanitize_file_name( MainWP_Utility::date( get_option( 'time_format' ) ) );
402
403 $what = null;
404 $regexBackupFile = null;
405
406 if ( $information['db'] ) {
407 $what = 'db';
408 $regexBackupFile = 'db-' . $websiteCleanUrl . '-(.*)-(.*).sql(\.zip|\.tar|\.tar\.gz|\.tar\.bz2)?';
409 if ( '' == $backupTaskProgress->downloadedDB ) {
410 $localBackupFile = $dir . 'db-' . $websiteCleanUrl . '-' . $fm_date . '-' . $fm_time;
411
412 if ( null != $pFilename ) {
413 $filename = str_replace(
414 array(
415 '%sitename%',
416 '%url%',
417 '%date%',
418 '%time%',
419 '%type%',
420 ),
421 array(
422 MainWP_Utility::sanitize( $website->name ),
423 $websiteCleanUrl,
424 $fm_date,
425 $fm_time,
426 $what,
427 ),
428 $pFilename
429 );
430 $filename = str_replace( '%', '', $filename );
431 $localBackupFile = $dir . $filename;
432 }
433 $localBackupFile .= self::get_real_extension( $information['db'] );
434
435 $localBackupFile = MainWP_Utility::normalize_filename( $localBackupFile );
436
437 $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'downloadedDB' => $localBackupFile ) );
438 } else {
439 $localBackupFile = $backupTaskProgress->downloadedDB;
440 }
441
442 if ( 0 == $backupTaskProgress->downloadedDBComplete ) {
443 MainWP_Connect::download_to_file( MainWP_Connect::get_get_data_authed( $website, $information['db'], 'fdl' ), $localBackupFile, $information['size'], $website->http_user, $website->http_pass );
444 $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'downloadedDBComplete' => 1 ) );
445 }
446 }
447
448 if ( $information['full'] ) {
449 $realExt = self::get_real_extension( $information['full'] );
450 $what = 'full';
451 $regexBackupFile = 'full-' . $websiteCleanUrl . '-(.*)-(.*).(zip|tar|tar.gz|tar.bz2)';
452 if ( '' == $backupTaskProgress->downloadedFULL ) {
453 $localBackupFile = $dir . 'full-' . $websiteCleanUrl . '-' . $fm_date . '-' . $fm_time . $realExt;
454
455 if ( null != $pFilename ) {
456 $filename = str_replace(
457 array(
458 '%sitename%',
459 '%url%',
460 '%date%',
461 '%time%',
462 '%type%',
463 ),
464 array(
465 MainWP_Utility::sanitize( $website->name ),
466 $websiteCleanUrl,
467 $fm_date,
468 $fm_time,
469 $what,
470 ),
471 $pFilename
472 );
473 $filename = str_replace( '%', '', $filename );
474 $localBackupFile = $dir . $filename . $realExt;
475 }
476
477 $localBackupFile = MainWP_Utility::normalize_filename( $localBackupFile );
478
479 $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'downloadedFULL' => $localBackupFile ) );
480 } else {
481 $localBackupFile = $backupTaskProgress->downloadedFULL;
482 }
483
484 if ( 0 == $backupTaskProgress->downloadedFULLComplete ) {
485 if ( file_exists( $localBackupFile ) ) {
486 $time = filemtime( $localBackupFile );
487
488 $minutes = gmdate( 'i', time() );
489 $seconds = gmdate( 's', time() );
490
491 $file_minutes = gmdate( 'i', $time );
492 $file_seconds = gmdate( 's', $time );
493
494 $minuteDiff = $minutes - $file_minutes;
495 if ( 59 === $minuteDiff ) {
496 $minuteDiff = 1;
497 }
498 $secondsdiff = ( $minuteDiff * 60 ) + $seconds - $file_seconds;
499
500 if ( 60 > $secondsdiff ) {
501 // still downloading.
502 return false;
503 }
504 }
505
506 MainWP_Connect::download_to_file( MainWP_Connect::get_get_data_authed( $website, $information['full'], 'fdl' ), $localBackupFile, $information['size'], $website->http_user, $website->http_pass );
507 MainWP_Connect::fetch_url_authed( $website, 'delete_backup', array( 'del' => $information['full'] ) );
508 $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'downloadedFULLComplete' => 1 ) );
509 }
510 }
511
512 $unique = $pTask->last_run;
513
514 do_action( 'mainwp_postprocess_backup_site', $localBackupFile, $what, $subfolder, $regexBackupFile, $website, $taskId, $unique );
515 $extra_result = apply_filters( 'mainwp_postprocess_backup_sites_feedback', array(), $unique );
516 if ( is_array( $extra_result ) ) {
517 foreach ( $extra_result as $key => $value ) {
518 $backup_result[ $key ] = $value;
519 }
520 }
521 } else {
522 throw new MainWP_Exception( 'Database backup failed due to an undefined error.' );
523 }
524
525 return $backup_result;
526 }
527
528 /**
529 * Method backup_download_file()
530 *
531 * Download backup file.
532 *
533 * @param mixed $pSiteId Child Site ID.
534 * @param mixed $pType full|db Type of backup.
535 * @param mixed $pUrl Backup location.
536 * @param mixed $pFile Backup File.
537 *
538 * @return bool true|false
539 * @throws MainWP_Exception
540 *
541 * @uses \MainWP\Dashboard\MainWP_Connect::get_get_data_authed()
542 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
543 * @uses \MainWP\Dashboard\MainWP_Connect::download_to_file()
544 * @uses \MainWP\Dashboard\MainWP_Utility::end_session()
545 */
546 public static function backup_download_file( $pSiteId, $pType, $pUrl, $pFile ) {
547 $hasWPFileSystem = MainWP_System_Utility::get_wp_file_system();
548
549 /**
550 * WordPress filesystem instance.
551 *
552 * @global object
553 */
554 global $wp_filesystem;
555
556 $dir = dirname( $pFile ) . '/';
557 $wp_filesystem->mkdir( $dir, 0777 );
558 if ( ! $wp_filesystem->exists( $dir . 'index.php' ) ) {
559 $wp_filesystem->touch( $dir . 'index.php' );
560 }
561 // Clean old backups from our system.
562 $maxBackups = get_option( 'mainwp_backupsOnServer' );
563 if ( false === $maxBackups ) {
564 $maxBackups = 1;
565 }
566
567 $dbBackups = array();
568 $fullBackups = array();
569
570 if ( file_exists( $dir ) ) {
571 $dh = opendir( $dir );
572 if ( $dh ) {
573 while ( false !== ( $file = readdir( $dh ) ) ) {
574 if ( '.' !== $file && '..' !== $file ) {
575 $theFile = $dir . $file;
576 if ( 'db' === $pType && self::is_sql_file( $file ) ) {
577 $dbBackups[ filemtime( $theFile ) . $file ] = $theFile;
578 }
579
580 if ( 'full' === $pType && self::is_archive( $file ) && ! self::is_sql_archive( $file ) ) {
581 $fullBackups[ filemtime( $theFile ) . $file ] = $theFile;
582 }
583 }
584 }
585 closedir( $dh );
586 }
587 }
588 krsort( $dbBackups );
589 krsort( $fullBackups );
590
591 $cnt = 0;
592 foreach ( $dbBackups as $key => $dbBackup ) {
593 $cnt ++;
594 if ( $cnt >= $maxBackups ) {
595 $wp_filesystem->delete( $dbBackup );
596 }
597 }
598
599 $cnt = 0;
600 foreach ( $fullBackups as $key => $fullBackup ) {
601 $cnt ++;
602 if ( $cnt >= $maxBackups ) {
603 $wp_filesystem->delete( $fullBackup );
604 }
605 }
606
607 $website = MainWP_DB::instance()->get_website_by_id( $pSiteId );
608 MainWP_Utility::end_session();
609
610 $what = null;
611 if ( 'db' === $pType ) {
612 MainWP_Connect::download_to_file( MainWP_Connect::get_get_data_authed( $website, $pUrl, 'fdl' ), $pFile, false, $website->http_user, $website->http_pass );
613 }
614
615 if ( 'full' === $pType ) {
616 MainWP_Connect::download_to_file( MainWP_Connect::get_get_data_authed( $website, $pUrl, 'fdl' ), $pFile, false, $website->http_user, $website->http_pass );
617 }
618
619 return true;
620 }
621
622 /**
623 * Method backup_delete_file()
624 *
625 * Delete backup file.
626 *
627 * @param mixed $pSiteId Child Site ID
628 * @param mixed $pFile File to delete.
629 *
630 * @return bool true
631 *
632 * @uses \MainWP\Dashboard\MainWP_Connect::fetch_url_authed()
633 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
634 */
635 public static function backup_delete_file( $pSiteId, $pFile ) {
636 $website = MainWP_DB::instance()->get_website_by_id( $pSiteId );
637 MainWP_Connect::fetch_url_authed( $website, 'delete_backup', array( 'del' => $pFile ) );
638
639 return true;
640 }
641
642 /**
643 * Method backup_check_pid()
644 *
645 * Check backup pid.
646 *
647 * @param mixed $pSiteId Child Site id.
648 * @param mixed $pid Backup pid.
649 * @param mixed $type full|db Type of backup.
650 * @param mixed $subfolder Sub folder for backup.
651 * @param mixed $pFilename Backups filename
652 *
653 * @return array $status, $result.
654 * @throws \Exception Error message.
655 *
656 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_mainwp_specific_dir()
657 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
658 * @uses \MainWP\Dashboard\MainWP_Utility::end_session()
659 * @uses \MainWP\Dashboard\MainWP_Utility::sanitize()
660 * @uses \MainWP\Dashboard\MainWP_Utility::get_nice_url()
661 * @uses \MainWP\Dashboard\MainWP_Utility::date()
662 * @uses \MainWP\Dashboard\MainWP_Utility::remove_preslash_spaces()
663 * @uses \MainWP\Dashboard\MainWP_Utility::normalize_filename()
664 * @uses \MainWP\Dashboard\MainWP_Utility::sanitize_file_name()
665 * @uses \MainWP\Dashboard\self::get_real_extension()
666 */
667 public static function backup_check_pid( $pSiteId, $pid, $type, $subfolder, $pFilename ) {
668 $website = MainWP_DB::instance()->get_website_by_id( $pSiteId );
669
670 MainWP_Utility::end_session();
671 $information = MainWP_Connect::fetch_url_authed( $website, 'backup_checkpid', array( 'pid' => $pid ) );
672
673 $status = $information['status'];
674
675 $result = isset( $information['file'] ) ? array( 'file' => $information['file'] ) : array();
676 if ( 'done' === $status ) {
677 $result['file'] = $information['file'];
678 $result['size'] = $information['size'];
679
680 $subfolder = str_replace( '%sitename%', MainWP_Utility::sanitize( $website->name ), $subfolder );
681 $subfolder = str_replace( '%url%', MainWP_Utility::sanitize( MainWP_Utility::get_nice_url( $website->url ) ), $subfolder );
682 $subfolder = str_replace( '%type%', $type, $subfolder );
683 $subfolder = str_replace( '%date%', MainWP_Utility::date( 'Ymd' ), $subfolder );
684 $subfolder = str_replace( '%task%', '', $subfolder );
685 $subfolder = str_replace( '%', '', $subfolder );
686 $subfolder = MainWP_Utility::remove_preslash_spaces( $subfolder );
687 $subfolder = MainWP_Utility::normalize_filename( $subfolder );
688
689 $result['subfolder'] = $subfolder;
690
691 $websiteCleanUrl = $website->url;
692 if ( '/' === substr( $websiteCleanUrl, - 1 ) ) {
693 $websiteCleanUrl = substr( $websiteCleanUrl, 0, - 1 );
694 }
695 $websiteCleanUrl = str_replace(
696 array( 'http://', 'https://', '/' ),
697 array(
698 '',
699 '',
700 '-',
701 ),
702 $websiteCleanUrl
703 );
704
705 $dir = MainWP_System_Utility::get_mainwp_specific_dir( $pSiteId );
706
707 $fm_date = MainWP_Utility::sanitize_file_name( MainWP_Utility::date( get_option( 'date_format' ) ) );
708 $fm_time = MainWP_Utility::sanitize_file_name( MainWP_Utility::date( get_option( 'time_format' ) ) );
709
710 if ( 'db' === $type ) {
711 $localBackupFile = $dir . 'db-' . $websiteCleanUrl . '-' . $fm_date . '-' . $fm_time . self::get_real_extension( $information['file'] );
712 $localRegexFile = 'db-' . $websiteCleanUrl . '-(.*)-(.*).sql(\.zip|\.tar|\.tar\.gz|\.tar\.bz2)?';
713 } else {
714 $localBackupFile = $dir . 'full-' . $websiteCleanUrl . '-' . $fm_date . '-' . $fm_time . self::get_real_extension( $information['file'] );
715 $localRegexFile = 'full-' . $websiteCleanUrl . '-(.*)-(.*).(zip|tar|tar.gz|tar.bz2)';
716 }
717
718 if ( null != $pFilename ) {
719 $filename = str_replace(
720 array(
721 '%sitename%',
722 '%url%',
723 '%date%',
724 '%time%',
725 '%type%',
726 ),
727 array(
728 MainWP_Utility::sanitize( $website->name ),
729 $websiteCleanUrl,
730 $fm_date,
731 $fm_time,
732 $type,
733 ),
734 $pFilename
735 );
736 $filename = str_replace( '%', '', $filename );
737 $localBackupFile = $dir . $filename;
738 $localBackupFile = MainWP_Utility::normalize_filename( $localBackupFile );
739
740 if ( 'db' === $type ) {
741 $localBackupFile .= self::get_real_extension( $information['file'] );
742 } else {
743 $localBackupFile .= self::get_real_extension( $information['file'] );
744 }
745 }
746
747 $result['local'] = $localBackupFile;
748 $result['regexfile'] = $localRegexFile;
749 }
750
751 return array(
752 'status' => $status,
753 'result' => $result,
754 );
755 }
756
757 /**
758 * Method backup()
759 *
760 * Backup Child Site.
761 *
762 * @param mixed $pSiteId Child Site ID.
763 * @param mixed $pType full|db Typ of backup.
764 * @param mixed $pSubfolder Sub folder to store backup.
765 * @param mixed $pExclude Exclusion list.
766 * @param mixed $excludebackup Exclude backup files.
767 * @param mixed $excludecache Exclude cache files.
768 * @param mixed $excludenonwp Exclude no WP Files.
769 * @param mixed $excludezip Exclude Zip files
770 * @param null $pFilename Name of backup file.
771 * @param string $pFileNameUID File name unique ID.
772 * @param bool $pArchiveFormat Archive format.
773 * @param bool $pMaximumFileDescriptorsOverride Override maximum file descriptors.
774 * @param bool $pMaximumFileDescriptorsAuto Auto maximum file descriptors.
775 * @param bool $pMaximumFileDescriptors Maximum file descriptors.
776 * @param bool $pLoadFilesBeforeZip Load files before zip.
777 * @param bool $pid Backup pid.
778 * @param bool $append Append to backup.
779 *
780 * @return mixed $backup_result
781 * @throws MainWP_Exception
782 *
783 * @uses \MainWP\Dashboard\MainWP_Connect::fetch_url_authed()
784 * @uses \MainWP\Dashboard\MainWP_Exception
785 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
786 * @uses \MainWP\Dashboard\MainWP_Logger::debug_for_website()
787 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
788 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_mainwp_specific_dir()
789 * @uses \MainWP\Dashboard\MainWP_Utility::sanitize()
790 * @uses \MainWP\Dashboard\MainWP_Utility::date()
791 * @uses \MainWP\Dashboard\MainWP_Utility::remove_preslash_spaces()
792 * @uses \MainWP\Dashboard\MainWP_Utility::normalize_filename()
793 * @uses \MainWP\Dashboard\MainWP_Utility::sanitize_file_name()
794 * @uses \MainWP\Dashboard\MainWP_Utility::end_session()
795 * @uses \MainWP\Dashboard\MainWP_Utility::value_to_string()
796 * @uses \MainWP\Dashboard\self::get_real_extension()
797 */
798 public static function backup(
799 $pSiteId,
800 $pType,
801 $pSubfolder,
802 $pExclude,
803 $excludebackup,
804 $excludecache,
805 $excludenonwp,
806 $excludezip,
807 $pFilename = null,
808 $pFileNameUID = '',
809 $pArchiveFormat = false,
810 $pMaximumFileDescriptorsOverride = false,
811 $pMaximumFileDescriptorsAuto = false,
812 $pMaximumFileDescriptors = false,
813 $pLoadFilesBeforeZip = false,
814 $pid = false,
815 $append = false ) {
816
817 if ( ! get_option( 'mainwp_enableLegacyBackupFeature' ) ) {
818 return false;
819 }
820
821 if ( '' == trim( $pFilename ) ) {
822 $pFilename = null;
823 }
824
825 $backup_result = array();
826
827 $website = MainWP_DB::instance()->get_website_by_id( $pSiteId );
828 $subfolder = str_replace( '%sitename%', MainWP_Utility::sanitize( $website->name ), $pSubfolder );
829 $subfolder = str_replace( '%url%', MainWP_Utility::sanitize( MainWP_Utility::get_nice_url( $website->url ) ), $subfolder );
830 $subfolder = str_replace( '%type%', $pType, $subfolder );
831 $subfolder = str_replace( '%date%', MainWP_Utility::date( 'Ymd' ), $subfolder );
832 $subfolder = str_replace( '%task%', '', $subfolder );
833 $subfolder = str_replace( '%', '', $subfolder );
834 $subfolder = MainWP_Utility::remove_preslash_spaces( $subfolder );
835 $subfolder = MainWP_Utility::normalize_filename( $subfolder );
836
837 if ( MainWP_System_Utility::is_suspended_site( $website ) ) {
838 throw new MainWP_Exception( 'Suspended site.', '', 'SUSPENDED_SITE' );
839 }
840
841 if ( ! MainWP_System_Utility::can_edit_website( $website ) ) {
842 throw new MainWP_Exception( 'You are not allowed to backup this site' );
843 }
844
845 $websiteCleanUrl = $website->url;
846 if ( '/' === substr( $websiteCleanUrl, - 1 ) ) {
847 $websiteCleanUrl = substr( $websiteCleanUrl, 0, - 1 );
848 }
849 $websiteCleanUrl = str_replace(
850 array( 'http://', 'https://', '/' ),
851 array( '', '', '-' ),
852 $websiteCleanUrl
853 );
854
855 if ( false === $pMaximumFileDescriptorsOverride ) {
856 if ( 1 == $website->maximumFileDescriptorsOverride ) {
857 $maximumFileDescriptorsAuto = ( 1 == $website->maximumFileDescriptorsAuto );
858 $maximumFileDescriptors = $website->maximumFileDescriptors;
859 } else {
860 $maximumFileDescriptorsAuto = get_option( 'mainwp_maximumFileDescriptorsAuto' );
861 $maximumFileDescriptors = get_option( 'mainwp_maximumFileDescriptors' );
862 $maximumFileDescriptors = ( false === $maximumFileDescriptors ? 150 : $maximumFileDescriptors );
863 }
864 } elseif ( ( 'global' !== $pArchiveFormat ) && ( 1 === $pMaximumFileDescriptorsOverride ) ) {
865 $maximumFileDescriptorsAuto = ( 1 === $pMaximumFileDescriptorsAuto );
866 $maximumFileDescriptors = $pMaximumFileDescriptors;
867 } else {
868 $maximumFileDescriptorsAuto = get_option( 'mainwp_maximumFileDescriptorsAuto' );
869 $maximumFileDescriptors = get_option( 'mainwp_maximumFileDescriptors' );
870 $maximumFileDescriptors = ( false === $maximumFileDescriptors ? 150 : $maximumFileDescriptors );
871 }
872
873 $file = str_replace(
874 array(
875 '%sitename%',
876 '%url%',
877 '%date%',
878 '%time%',
879 '%type%',
880 ),
881 array(
882 MainWP_Utility::sanitize( $website->name ),
883 $websiteCleanUrl,
884 MainWP_Utility::date( 'm-d-Y' ),
885 MainWP_Utility::date( 'G\hi\ms\s' ),
886 $pType,
887 ),
888 $pFilename
889 );
890 $file = str_replace( '%', '', $file );
891 $file = MainWP_Utility::normalize_filename( $file );
892
893 // Normal flow: check site settings & fallback to global.
894 if ( false === $pLoadFilesBeforeZip ) {
895 $loadFilesBeforeZip = $website->loadFilesBeforeZip;
896 if ( 1 === $loadFilesBeforeZip ) {
897 $loadFilesBeforeZip = get_option( 'mainwp_options_loadFilesBeforeZip' );
898 $loadFilesBeforeZip = ( 1 === $loadFilesBeforeZip || false === $loadFilesBeforeZip );
899 } else {
900 $loadFilesBeforeZip = ( 2 === $loadFilesBeforeZip );
901 }
902 } elseif ( 'global' === $pArchiveFormat || 1 === $pLoadFilesBeforeZip ) { // Overridden flow: only fallback to global.
903 $loadFilesBeforeZip = get_option( 'mainwp_options_loadFilesBeforeZip' );
904 $loadFilesBeforeZip = ( 1 === $loadFilesBeforeZip || false === $loadFilesBeforeZip );
905 } else {
906 $loadFilesBeforeZip = ( 2 === $pLoadFilesBeforeZip );
907 }
908
909 // Normal flow: check site settings & fallback to global.
910 if ( false === $pArchiveFormat ) {
911 $archiveFormat = self::get_current_archive_extension( $website );
912 } elseif ( 'global' === $pArchiveFormat ) {
913 $archiveFormat = self::get_current_archive_extension();
914 } else {
915 $archiveFormat = $pArchiveFormat;
916 }
917
918 MainWP_Utility::end_session();
919
920 $params = array(
921 'type' => $pType,
922 'exclude' => $pExclude,
923 'excludebackup' => $excludebackup,
924 'excludecache' => $excludecache,
925 'excludenonwp' => $excludenonwp,
926 'excludezip' => $excludezip,
927 'ext' => $archiveFormat,
928 'file_descriptors_auto' => $maximumFileDescriptorsAuto,
929 'file_descriptors' => $maximumFileDescriptors,
930 'loadFilesBeforeZip' => $loadFilesBeforeZip,
931 'f' => $file,
932 'fileUID' => $pFileNameUID,
933 'pid' => $pid,
934 'append' => ( $append ? 1 : 0 ),
935 );
936
937 MainWP_Logger::instance()->debug_for_website( $website, 'backup', 'Requesting backup: ' . MainWP_Utility::value_to_string( $params, 1 ) );
938
939 $information = MainWP_Connect::fetch_url_authed( $website, 'backup', $params, false, false, false );
940 do_action( 'mainwp_managesite_backup', $website, array( 'type' => $pType ), $information );
941
942 if ( isset( $information['error'] ) ) {
943 throw new MainWP_Exception( esc_html( $information['error'] ) );
944 } elseif ( 'db' === $pType && ! $information['db'] ) {
945 throw new MainWP_Exception( 'Database backup failed.' );
946 } elseif ( 'full' === $pType && ! $information['full'] ) {
947 throw new MainWP_Exception( 'Full backup failed.' );
948 } elseif ( isset( $information['db'] ) ) {
949 if ( false !== $information['db'] ) {
950 $backup_result['url'] = $information['db'];
951 $backup_result['type'] = 'db';
952 } elseif ( false !== $information['full'] ) {
953 $backup_result['url'] = $information['full'];
954 $backup_result['type'] = 'full';
955 }
956
957 if ( isset( $information['size'] ) ) {
958 $backup_result['size'] = $information['size'];
959 }
960 $backup_result['subfolder'] = $subfolder;
961
962 $dir = MainWP_System_Utility::get_mainwp_specific_dir( $pSiteId );
963
964 $fm_date = MainWP_Utility::sanitize_file_name( MainWP_Utility::date( get_option( 'date_format' ) ) );
965 $fm_time = MainWP_Utility::sanitize_file_name( MainWP_Utility::date( get_option( 'time_format' ) ) );
966
967 if ( 'db' === $pType ) {
968 $localBackupFile = $dir . 'db-' . $websiteCleanUrl . '-' . $fm_date . '-' . $fm_time . self::get_real_extension( $information['db'] );
969 $localRegexFile = 'db-' . $websiteCleanUrl . '-(.*)-(.*).sql(\.zip|\.tar|\.tar\.gz|\.tar\.bz2)?';
970 } else {
971 $localBackupFile = $dir . 'full-' . $websiteCleanUrl . '-' . $fm_date . '-' . $fm_time . self::get_real_extension( $information['full'] );
972 $localRegexFile = 'full-' . $websiteCleanUrl . '-(.*)-(.*).(zip|tar|tar.gz|tar.bz2)';
973 }
974
975 if ( null != $pFilename ) {
976 $filename = str_replace(
977 array(
978 '%sitename%',
979 '%url%',
980 '%date%',
981 '%time%',
982 '%type%',
983 ),
984 array(
985 MainWP_Utility::sanitize( $website->name ),
986 $websiteCleanUrl,
987 $fm_date,
988 $fm_time,
989 $pType,
990 ),
991 $pFilename
992 );
993 $filename = str_replace( '%', '', $filename );
994 $localBackupFile = $dir . $filename;
995 $localBackupFile = MainWP_Utility::normalize_filename( $localBackupFile );
996
997 if ( 'db' === $pType ) {
998 $localBackupFile .= self::get_real_extension( $information['db'] );
999 } else {
1000 $localBackupFile .= self::get_real_extension( $information['full'] );
1001 }
1002 }
1003
1004 $backup_result['local'] = $localBackupFile;
1005 $backup_result['regexfile'] = $localRegexFile;
1006
1007 return $backup_result;
1008 } else {
1009 throw new MainWP_Exception( 'Database backup failed due to an undefined error' );
1010 }
1011 }
1012
1013 /**
1014 * Method is_archive()
1015 *
1016 * Check if Archive.
1017 *
1018 * @param mixed $pFileName File to check.
1019 * @param string $pPrefix File prefix.
1020 * @param string $pSuffix File suffix.
1021 *
1022 * @return mixed Files that are archives.
1023 */
1024 public static function is_archive( $pFileName, $pPrefix = '', $pSuffix = '' ) {
1025 return preg_match( '/' . $pPrefix . '(.*).(zip|tar|tar.gz|tar.bz2)' . $pSuffix . '$/', $pFileName );
1026 }
1027
1028 /**
1029 * Method is_sql_file()
1030 *
1031 * Check if file is SQL.
1032 *
1033 * @param mixed $pFileName File to check.
1034 *
1035 * @return mixed SQL Files.
1036 */
1037 public static function is_sql_file( $pFileName ) {
1038 return preg_match( '/(.*).sql$/', $pFileName ) || self::is_sql_archive( $pFileName );
1039 }
1040
1041 /**
1042 * Method is_sql_archive()
1043 *
1044 * Check if is SQL Archive.
1045 *
1046 * @param mixed $pFileName File to check.
1047 *
1048 * @return mixed SQL archive.
1049 */
1050 public static function is_sql_archive( $pFileName ) {
1051 return preg_match( '/(.*).sql.(zip|tar|tar.gz|tar.bz2)$/', $pFileName );
1052 }
1053
1054 /**
1055 * Method get_current_archive_extension()
1056 *
1057 * Get extension of current Archive.
1058 *
1059 * @param bool $website true|false
1060 * @param bool|string $task true|false|global|site
1061 *
1062 * @return mixed $archiveFormat Format of Archive.
1063 *
1064 * @uses \MainWP\Dashboard\MainWP_DB_Backup::get_website_backup_settings()
1065 */
1066 public static function get_current_archive_extension( $website = false, $task = false ) {
1067 $useSite = true;
1068 if ( false != $task ) {
1069 if ( 'global' === $task->archiveFormat ) {
1070 $useGlobal = true;
1071 $useSite = false;
1072 } elseif ( '' == $task->archiveFormat || 'site' == $task->archiveFormat ) {
1073 $useGlobal = false;
1074 $useSite = true;
1075 } else {
1076 $archiveFormat = $task->archiveFormat;
1077 $useGlobal = false;
1078 $useSite = false;
1079 }
1080 }
1081
1082 if ( $useSite ) {
1083 if ( false === $website ) {
1084 $useGlobal = true;
1085 } else {
1086 $backupSettings = MainWP_DB_Backup::instance()->get_website_backup_settings( $website->id );
1087 $archiveFormat = $backupSettings->archiveFormat;
1088 $useGlobal = ( 'global' === $archiveFormat );
1089 }
1090 }
1091
1092 if ( $useGlobal ) {
1093 $archiveFormat = get_option( 'mainwp_archiveFormat' );
1094 if ( false === $archiveFormat ) {
1095 $archiveFormat = 'tar.gz';
1096 }
1097 }
1098
1099 return $archiveFormat;
1100 }
1101
1102 /**
1103 * Method get_real_extension()
1104 *
1105 * Get full file extension.
1106 *
1107 * @param mixed $path Path to file.
1108 *
1109 * @return mixed $check|pathinfo()
1110 */
1111 public static function get_real_extension( $path ) {
1112 $checks = array( '.sql.zip', '.sql.tar', '.sql.tar.gz', '.sql.tar.bz2', '.tar.gz', '.tar.bz2' );
1113 foreach ( $checks as $check ) {
1114 if ( MainWP_Utility::ends_with( $path, $check ) ) {
1115 return $check;
1116 }
1117 }
1118
1119 return '.' . pathinfo( $path, PATHINFO_EXTENSION );
1120 }
1121
1122
1123 /**
1124 * Method handle_settings_post()
1125 *
1126 * Update Child Site Settings.
1127 *
1128 * @return bool true|false
1129 *
1130 * @uses \MainWP\Dashboard\MainWP_System_Utility::is_admin()
1131 * @uses \MainWP\Dashboard\MainWP_Utility::update_option()
1132 */
1133 public static function handle_settings_post() {
1134 if ( MainWP_System_Utility::is_admin() ) {
1135 if ( isset( $_POST['submit'] ) && isset( $_POST['wp_nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['wp_nonce'] ), 'Settings' ) ) {
1136 if ( isset( $_POST['mainwp_options_backupOnServer'] ) && 0 < $_POST['mainwp_options_backupOnServer'] ) {
1137 MainWP_Utility::update_option( 'mainwp_backupsOnServer', ( isset( $_POST['mainwp_options_backupOnServer'] ) ? intval( $_POST['mainwp_options_backupOnServer'] ) : 1 ) );
1138 }
1139 if ( isset( $_POST['mainwp_options_maximumFileDescriptors'] ) && - 1 < $_POST['mainwp_options_maximumFileDescriptors'] ) {
1140 MainWP_Utility::update_option( 'mainwp_maximumFileDescriptors', ( isset( $_POST['mainwp_options_maximumFileDescriptors'] ) ? intval( $_POST['mainwp_options_maximumFileDescriptors'] ) : 1 ) );
1141 }
1142 MainWP_Utility::update_option( 'mainwp_maximumFileDescriptorsAuto', ( ! isset( $_POST['mainwp_maximumFileDescriptorsAuto'] ) ? 0 : 1 ) );
1143 if ( ! empty( $_POST['mainwp_options_backupOnExternalSources'] ) && 0 <= $_POST['mainwp_options_backupOnExternalSources'] ) {
1144 MainWP_Utility::update_option( 'mainwp_backupOnExternalSources', ( isset( $_POST['mainwp_options_backupOnExternalSources'] ) ? intval( $_POST['mainwp_options_backupOnExternalSources'] ) : 1 ) );
1145 }
1146 MainWP_Utility::update_option( 'mainwp_archiveFormat', ( isset( $_POST['mainwp_archiveFormat'] ) ? sanitize_text_field( wp_unslash( $_POST['mainwp_archiveFormat'] ) ) : '' ) );
1147
1148 $old_primaryBackup = get_option( 'mainwp_primaryBackup' );
1149 $old_enableLegacyBackup = get_option( 'mainwp_enableLegacyBackupFeature' );
1150 $updated_enableLegacyBackup = false;
1151
1152 if ( isset( $_POST['mainwp_primaryBackup'] ) ) {
1153 if ( ! empty( $_POST['mainwp_primaryBackup'] ) ) { // not default backup method.
1154 MainWP_Utility::update_option( 'mainwp_notificationOnBackupFail', 0 );
1155 MainWP_Utility::update_option( 'mainwp_notificationOnBackupStart', 0 );
1156 MainWP_Utility::update_option( 'mainwp_chunkedBackupTasks', 0 );
1157 if ( empty( $old_primaryBackup ) ) {
1158 MainWP_Utility::update_option( 'mainwp_enableLegacyBackupFeature', 0 );
1159 $updated_enableLegacyBackup = true;
1160 }
1161 }
1162 MainWP_Utility::update_option( 'mainwp_primaryBackup', sanitize_text_field( wp_unslash( $_POST['mainwp_primaryBackup'] ) ) );
1163 }
1164
1165 if ( ! isset( $_POST['mainwp_primaryBackup'] ) || empty( $_POST['mainwp_primaryBackup'] ) ) {
1166 MainWP_Utility::update_option( 'mainwp_options_loadFilesBeforeZip', ( ! isset( $_POST['mainwp_options_loadFilesBeforeZip'] ) ? 0 : 1 ) );
1167 MainWP_Utility::update_option( 'mainwp_notificationOnBackupFail', ( ! isset( $_POST['mainwp_options_notificationOnBackupFail'] ) ? 0 : 1 ) );
1168 MainWP_Utility::update_option( 'mainwp_notificationOnBackupStart', ( ! isset( $_POST['mainwp_options_notificationOnBackupStart'] ) ? 0 : 1 ) );
1169 MainWP_Utility::update_option( 'mainwp_chunkedBackupTasks', ( ! isset( $_POST['mainwp_options_chunkedBackupTasks'] ) ? 0 : 1 ) );
1170 }
1171
1172 $enableLegacyBackup = ( isset( $_POST['mainwp_options_enableLegacyBackupFeature'] ) && ! empty( $_POST['mainwp_options_enableLegacyBackupFeature'] ) ) ? 1 : 0;
1173 if ( $enableLegacyBackup && empty( $old_enableLegacyBackup ) ) {
1174 MainWP_Utility::update_option( 'mainwp_primaryBackup', '' );
1175 }
1176
1177 if ( ! $updated_enableLegacyBackup ) {
1178 MainWP_Utility::update_option( 'mainwp_enableLegacyBackupFeature', $enableLegacyBackup );
1179 }
1180 return true;
1181 }
1182 }
1183 return false;
1184 }
1185
1186 /**
1187 * Method backup_get_file_size()
1188 *
1189 * Get file size.
1190 *
1191 * @param mixed $pFile Backup file.
1192 *
1193 * @return false|int
1194 *
1195 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_mainwp_specific_dir()
1196 */
1197 public static function backup_get_file_size( $pFile ) {
1198 $dir = MainWP_System_Utility::get_mainwp_specific_dir();
1199
1200 if ( stristr( $pFile, $dir ) && file_exists( $pFile ) ) {
1201 return @filesize( $pFile );
1202 }
1203
1204 return 0;
1205 }
1206
1207 }
1208