| 1 |
<?php |
| 2 |
/** |
| 3 |
* MainWP Legacy Backups Handler. |
| 4 |
* |
| 5 |
* @package MainWP/Dashboard |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace MainWP\Dashboard; |
| 9 |
|
| 10 |
// Exit if accessed directly. |
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Class MainWP_Manage_Backups_Handler |
| 17 |
* |
| 18 |
* @package MainWP\Dashboard |
| 19 |
*/ |
| 20 |
class MainWP_Manage_Backups_Handler { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR. |
| 21 |
|
| 22 |
/** |
| 23 |
* Instance variable. |
| 24 |
* |
| 25 |
* @var null $instance. |
| 26 |
*/ |
| 27 |
private static $instance = null; |
| 28 |
|
| 29 |
/** |
| 30 |
* Create instance. |
| 31 |
* |
| 32 |
* @return self $instance. |
| 33 |
*/ |
| 34 |
public static function instance() { |
| 35 |
if ( null === static::$instance ) { |
| 36 |
static::$instance = new self(); |
| 37 |
} |
| 38 |
|
| 39 |
return static::$instance; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Get Class Name. |
| 44 |
* |
| 45 |
* @return string __CLASS__ |
| 46 |
*/ |
| 47 |
public static function get_class_name() { |
| 48 |
return __CLASS__; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Validate Backup Tasks. |
| 53 |
* |
| 54 |
* @param array $pBackupTasks Backup tasks. |
| 55 |
* |
| 56 |
* @return bool true|false. |
| 57 |
* |
| 58 |
* @uses \MainWP\Dashboard\MainWP_DB_Backup::update_backup_task_with_values() |
| 59 |
* @uses \MainWP\Dashboard\MainWP_DB_Common::get_group_by_id() |
| 60 |
* @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id() |
| 61 |
*/ |
| 62 |
public static function validate_backup_tasks( $pBackupTasks ) { //phpcs:ignore -- NOSONAR - complex. |
| 63 |
if ( ! is_array( $pBackupTasks ) ) { |
| 64 |
return true; |
| 65 |
} |
| 66 |
|
| 67 |
$nothingChanged = true; |
| 68 |
foreach ( $pBackupTasks as $backupTask ) { |
| 69 |
if ( '' === $backupTask->groups ) { |
| 70 |
$newSiteIds = ''; |
| 71 |
$siteIds = ( '' === $backupTask->sites ? array() : explode( ',', $backupTask->sites ) ); |
| 72 |
foreach ( $siteIds as $siteId ) { |
| 73 |
$site = MainWP_DB::instance()->get_website_by_id( $siteId ); |
| 74 |
if ( ! empty( $site ) ) { |
| 75 |
$newSiteIds .= ',' . $siteId; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
$newSiteIds = trim( $newSiteIds, ',' ); |
| 80 |
|
| 81 |
if ( $newSiteIds !== $backupTask->sites ) { |
| 82 |
$nothingChanged = false; |
| 83 |
MainWP_DB_Backup::instance()->update_backup_task_with_values( $backupTask->id, array( 'sites' => $newSiteIds ) ); |
| 84 |
} |
| 85 |
} else { |
| 86 |
$newGroupIds = ''; |
| 87 |
$groupIds = explode( ',', $backupTask->groups ); |
| 88 |
foreach ( $groupIds as $groupId ) { |
| 89 |
$group = MainWP_DB_Common::instance()->get_group_by_id( $groupId ); |
| 90 |
if ( ! empty( $group ) ) { |
| 91 |
$newGroupIds .= ',' . $groupId; |
| 92 |
} |
| 93 |
} |
| 94 |
$newGroupIds = trim( $newGroupIds, ',' ); |
| 95 |
|
| 96 |
if ( $newGroupIds !== $backupTask->groups ) { |
| 97 |
$nothingChanged = false; |
| 98 |
MainWP_DB_Backup::instance()->update_backup_task_with_values( $backupTask->id, array( 'groups' => $newGroupIds ) ); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
return $nothingChanged; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Can Edit Backup Task. |
| 108 |
* |
| 109 |
* @param object $task Backup task. |
| 110 |
* |
| 111 |
* @return bool true|false. |
| 112 |
* |
| 113 |
* @uses \MainWP\Dashboard\MainWP_System::is_single_user() |
| 114 |
*/ |
| 115 |
public static function can_edit_backuptask( &$task ) { |
| 116 |
if ( null === $task ) { |
| 117 |
return false; |
| 118 |
} |
| 119 |
|
| 120 |
if ( MainWP_System::instance()->is_single_user() ) { |
| 121 |
return true; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Current user global. |
| 126 |
* |
| 127 |
* @global string |
| 128 |
*/ |
| 129 |
global $current_user; |
| 130 |
|
| 131 |
return $task->userid === $current_user->ID; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Update backup task. |
| 136 |
* |
| 137 |
* @uses \MainWP\Dashboard\MainWP_DB_Backup::add_backup_task() |
| 138 |
* @uses \MainWP\Dashboard\MainWP_DB_Backup::get_backup_task_by_id() |
| 139 |
* @uses \MainWP\Dashboard\MainWP_DB_Backup::update_backup_task() |
| 140 |
*/ |
| 141 |
public static function update_backup() { //phpcs:ignore -- NOSONAR - complex. |
| 142 |
|
| 143 |
/** |
| 144 |
* Current user global. |
| 145 |
* |
| 146 |
* @global string |
| 147 |
*/ |
| 148 |
global $current_user; |
| 149 |
|
| 150 |
//phpcs:disable WordPress.Security.NonceVerification.Missing -- nonce verified. |
| 151 |
$name = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : ''; |
| 152 |
|
| 153 |
if ( '' === $name ) { |
| 154 |
die( wp_json_encode( array( 'error' => esc_html__( 'Invalid backup task name. Please, enter a new name and try again.', 'mainwp' ) ) ) ); |
| 155 |
} |
| 156 |
|
| 157 |
$backupId = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : 0; |
| 158 |
$task = MainWP_DB_Backup::instance()->get_backup_task_by_id( $backupId ); |
| 159 |
|
| 160 |
if ( ! static::can_edit_backuptask( $task ) ) { |
| 161 |
die( wp_json_encode( array( 'error' => esc_html__( 'Insufficient permissions. Is this task set by you?', 'mainwp' ) ) ) ); |
| 162 |
} |
| 163 |
|
| 164 |
$schedule = isset( $_POST['schedule'] ) ? sanitize_text_field( wp_unslash( $_POST['schedule'] ) ) : ''; |
| 165 |
$type = isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : ''; |
| 166 |
$excludedFolder = isset( $_POST['exclude'] ) ? trim( wp_unslash( $_POST['exclude'] ), "\n" ) : ''; //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- ok. |
| 167 |
$excludedFolder = explode( "\n", $excludedFolder ); |
| 168 |
$excludedFolder = array_map( array( MainWP_Utility::get_class_name(), 'trim_slashes' ), $excludedFolder ); |
| 169 |
$excludedFolder = array_map( 'htmlentities', $excludedFolder ); |
| 170 |
$excludedFolder = implode( ',', $excludedFolder ); |
| 171 |
$sites = ''; |
| 172 |
$groups = ''; |
| 173 |
|
| 174 |
if ( isset( $_POST['sites'] ) && is_array( $_POST['sites'] ) ) { |
| 175 |
foreach ( array_map( 'sanitize_text_field', wp_unslash( $_POST['sites'] ) ) as $site ) { |
| 176 |
if ( '' !== $sites ) { |
| 177 |
$sites .= ','; |
| 178 |
} |
| 179 |
$sites .= $site; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
if ( isset( $_POST['groups'] ) && is_array( $_POST['groups'] ) ) { |
| 184 |
foreach ( array_map( 'sanitize_text_field', wp_unslash( $_POST['groups'] ) ) as $group ) { |
| 185 |
if ( '' !== $groups ) { |
| 186 |
$groups .= ','; |
| 187 |
} |
| 188 |
$groups .= $group; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
do_action( 'mainwp_update_backuptask', $task->id ); |
| 193 |
|
| 194 |
$archiveFormat = isset( $_POST['archiveFormat'] ) ? sanitize_text_field( wp_unslash( $_POST['archiveFormat'] ) ) : 'site'; |
| 195 |
$maximumFileDescriptorsOverride = isset( $_POST['maximumFileDescriptorsOverride'] ) && 1 === (int) $_POST['maximumFileDescriptorsOverride']; |
| 196 |
$maximumFileDescriptorsAuto = isset( $_POST['maximumFileDescriptorsAuto'] ) && 1 === (int) $_POST['maximumFileDescriptorsAuto']; |
| 197 |
$maximumFileDescriptors = isset( $_POST['maximumFileDescriptors'] ) ? intval( $_POST['maximumFileDescriptors'] ) : 150; |
| 198 |
$loadFilesBeforeZip = isset( $_POST['loadFilesBeforeZip'] ) ? 1 : 0; |
| 199 |
|
| 200 |
$subfolder = isset( $_POST['subfolder'] ) ? sanitize_text_field( wp_unslash( $_POST['subfolder'] ) ) : ''; |
| 201 |
$filename = isset( $_POST['filename'] ) ? sanitize_text_field( wp_unslash( $_POST['filename'] ) ) : ''; |
| 202 |
$excludebackup = isset( $_POST['excludebackup'] ) ? sanitize_text_field( wp_unslash( $_POST['excludebackup'] ) ) : ''; |
| 203 |
$excludecache = isset( $_POST['excludecache'] ) ? sanitize_text_field( wp_unslash( $_POST['excludecache'] ) ) : ''; |
| 204 |
$excludenonwp = isset( $_POST['excludenonwp'] ) ? sanitize_text_field( wp_unslash( $_POST['excludenonwp'] ) ) : ''; |
| 205 |
$excludezip = isset( $_POST['excludezip'] ) ? sanitize_text_field( wp_unslash( $_POST['excludezip'] ) ) : ''; |
| 206 |
|
| 207 |
//phpcs:enable.Missing |
| 208 |
|
| 209 |
if ( MainWP_DB_Backup::instance()->update_backup_task( $task->id, $current_user->ID, htmlentities( $name ), $schedule, $type, $excludedFolder, $sites, $groups, $subfolder, $filename, $excludebackup, $excludecache, $excludenonwp, $excludezip, $archiveFormat, $maximumFileDescriptorsOverride, $maximumFileDescriptorsAuto, $maximumFileDescriptors, $loadFilesBeforeZip ) === false ) { |
| 210 |
die( wp_json_encode( array( 'error' => esc_html__( 'Undefined error occurred. Please try again.', 'mainwp' ) ) ) ); |
| 211 |
} else { |
| 212 |
die( wp_json_encode( array( 'result' => esc_html__( 'Task updated successfully.', 'mainwp' ) ) ) ); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
/** Add backup task. */ |
| 217 |
public static function add_backup() { //phpcs:ignore -- NOSONAR - complex. |
| 218 |
|
| 219 |
/** |
| 220 |
* Current user global. |
| 221 |
* |
| 222 |
* @global string |
| 223 |
*/ |
| 224 |
global $current_user; |
| 225 |
|
| 226 |
//phpcs:disable WordPress.Security.NonceVerification -- ok. |
| 227 |
$name = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : ''; |
| 228 |
|
| 229 |
if ( empty( $name ) ) { |
| 230 |
die( wp_json_encode( array( 'error' => esc_html__( 'Invalid backup task name. Please, enter a new name and try again.', 'mainwp' ) ) ) ); |
| 231 |
} |
| 232 |
|
| 233 |
$schedule = isset( $_POST['schedule'] ) ? sanitize_text_field( wp_unslash( $_POST['schedule'] ) ) : ''; |
| 234 |
$type = isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : ''; |
| 235 |
$excludedFolder = isset( $_POST['exclude'] ) ? trim( wp_unslash( $_POST['exclude'] ), "\n" ) : ''; //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- ok. |
| 236 |
$excludedFolder = explode( "\n", $excludedFolder ); |
| 237 |
$excludedFolder = array_map( array( MainWP_Utility::get_class_name(), 'trim_slashes' ), $excludedFolder ); |
| 238 |
$excludedFolder = array_map( 'htmlentities', $excludedFolder ); |
| 239 |
$excludedFolder = implode( ',', $excludedFolder ); |
| 240 |
|
| 241 |
$sites = ''; |
| 242 |
$groups = ''; |
| 243 |
|
| 244 |
if ( isset( $_POST['sites'] ) ) { |
| 245 |
foreach ( array_map( 'sanitize_text_field', wp_unslash( $_POST['sites'] ) ) as $site ) { |
| 246 |
if ( '' !== $sites ) { |
| 247 |
$sites .= ','; |
| 248 |
} |
| 249 |
$sites .= $site; |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
if ( isset( $_POST['groups'] ) ) { |
| 254 |
foreach ( array_map( 'sanitize_text_field', wp_unslash( $_POST['groups'] ) ) as $group ) { |
| 255 |
if ( '' !== $groups ) { |
| 256 |
$groups .= ','; |
| 257 |
} |
| 258 |
$groups .= $group; |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
$archiveFormat = isset( $_POST['archiveFormat'] ) ? sanitize_text_field( wp_unslash( $_POST['archiveFormat'] ) ) : 'site'; |
| 263 |
$maximumFileDescriptorsOverride = isset( $_POST['maximumFileDescriptorsOverride'] ) && 1 === (int) $_POST['maximumFileDescriptorsOverride']; |
| 264 |
$maximumFileDescriptorsAuto = isset( $_POST['maximumFileDescriptorsAuto'] ) && 1 === (int) $_POST['maximumFileDescriptorsAuto']; |
| 265 |
$maximumFileDescriptors = isset( $_POST['maximumFileDescriptors'] ) ? intval( $_POST['maximumFileDescriptors'] ) : 150; |
| 266 |
$loadFilesBeforeZip = isset( $_POST['loadFilesBeforeZip'] ) ? 1 : 0; |
| 267 |
|
| 268 |
$subfolder = isset( $_POST['subfolder'] ) ? sanitize_text_field( wp_unslash( $_POST['subfolder'] ) ) : ''; |
| 269 |
$filename = isset( $_POST['filename'] ) ? sanitize_text_field( wp_unslash( $_POST['filename'] ) ) : ''; |
| 270 |
$excludebackup = isset( $_POST['excludebackup'] ) ? sanitize_text_field( wp_unslash( $_POST['excludebackup'] ) ) : ''; |
| 271 |
$excludecache = isset( $_POST['excludecache'] ) ? sanitize_text_field( wp_unslash( $_POST['excludecache'] ) ) : ''; |
| 272 |
$excludenonwp = isset( $_POST['excludenonwp'] ) ? sanitize_text_field( wp_unslash( $_POST['excludenonwp'] ) ) : ''; |
| 273 |
$excludezip = isset( $_POST['excludezip'] ) ? sanitize_text_field( wp_unslash( $_POST['excludezip'] ) ) : ''; |
| 274 |
//phpcs:enable |
| 275 |
|
| 276 |
$task = MainWP_DB_Backup::instance()->add_backup_task( $current_user->ID, htmlentities( $name ), $schedule, $type, $excludedFolder, $sites, $groups, $subfolder, $filename, 0, $excludebackup, $excludecache, $excludenonwp, $excludezip, $archiveFormat, $maximumFileDescriptorsOverride, $maximumFileDescriptorsAuto, $maximumFileDescriptors, $loadFilesBeforeZip ); |
| 277 |
|
| 278 |
if ( ! $task ) { |
| 279 |
die( wp_json_encode( array( 'error' => esc_html__( 'Undefined error occurred. Please try again.', 'mainwp' ) ) ) ); |
| 280 |
} else { |
| 281 |
do_action( 'mainwp_add_backuptask', $task->id ); |
| 282 |
|
| 283 |
die( wp_json_encode( array( 'result' => esc_html__( 'Task created successfully.', 'mainwp' ) ) ) ); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* Execute the backup task. |
| 289 |
* |
| 290 |
* @param mixed $task Task to perform. |
| 291 |
* @param integer $nrOfSites Number of Child Sites to perform the task on. |
| 292 |
* @param bool $updateRun ture|false. |
| 293 |
* |
| 294 |
* @return mixed $errorOutput. |
| 295 |
* |
| 296 |
* @uses \MainWP\Dashboard\MainWP_Backup_Handler::backup_site() |
| 297 |
* @uses \MainWP\Dashboard\MainWP_DB_Backup::update_backup_run() |
| 298 |
* @uses \MainWP\Dashboard\MainWP_DB_Backup::get_backup_task_by_id() |
| 299 |
* @uses \MainWP\Dashboard\MainWP_DB_Backup::update_backup_task_with_values() |
| 300 |
* @uses \MainWP\Dashboard\MainWP_DB_Backup::update_completed_sites() |
| 301 |
* @uses \MainWP\Dashboard\MainWP_DB_Backup::update_backup_errors() |
| 302 |
* @uses \MainWP\Dashboard\MainWP_DB_Backup::update_backup_completed() |
| 303 |
* @uses \MainWP\Dashboard\MainWP_DB_Common::get_user_notification_email() |
| 304 |
* @uses \MainWP\Dashboard\MainWP_Format::format_email() |
| 305 |
* @uses \MainWP\Dashboard\MainWP_Error_Helper::get_error_message() |
| 306 |
* @uses \MainWP\Dashboard\MainWP_DB::get_website_by_group_id() |
| 307 |
* @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id() |
| 308 |
* @uses \MainWP\Dashboard\MainWP_Utility::format_timestamp() |
| 309 |
* @uses \MainWP\Dashboard\MainWP_Utility::get_nice_url() |
| 310 |
* @uses \MainWP\Dashboard\MainWP_Utility::sanitize() |
| 311 |
*/ |
| 312 |
public static function execute_backup_task( $task, $nrOfSites = 0, $updateRun = true ) { // phpcs:ignore -- NOSONAR - not quite complex function. |
| 313 |
|
| 314 |
if ( $updateRun ) { |
| 315 |
MainWP_DB_Backup::instance()->update_backup_run( $task->id ); |
| 316 |
} |
| 317 |
|
| 318 |
$task = MainWP_DB_Backup::instance()->get_backup_task_by_id( $task->id ); |
| 319 |
|
| 320 |
$completed_sites = $task->completed_sites; |
| 321 |
|
| 322 |
if ( '' !== $completed_sites ) { |
| 323 |
$completed_sites = json_decode( $completed_sites, true ); |
| 324 |
} |
| 325 |
|
| 326 |
if ( ! is_array( $completed_sites ) ) { |
| 327 |
$completed_sites = array(); |
| 328 |
} |
| 329 |
|
| 330 |
$sites = array(); |
| 331 |
|
| 332 |
if ( empty( $task->groups ) ) { |
| 333 |
if ( '' !== $task->sites ) { |
| 334 |
$sites = explode( ',', $task->sites ); |
| 335 |
} |
| 336 |
} else { |
| 337 |
$groups = explode( ',', $task->groups ); |
| 338 |
foreach ( $groups as $groupid ) { |
| 339 |
$group_sites = MainWP_DB::instance()->get_websites_by_group_id( $groupid ); |
| 340 |
foreach ( $group_sites as $group_site ) { |
| 341 |
if ( in_array( $group_site->id, $sites ) ) { |
| 342 |
continue; |
| 343 |
} |
| 344 |
$sites[] = $group_site->id; |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
$errorOutput = null; |
| 349 |
|
| 350 |
$lastStartNotification = $task->lastStartNotificationSent; |
| 351 |
if ( $updateRun && ( 1 === get_option( 'mainwp_notificationOnBackupStart' ) ) && ( $lastStartNotification < $task->last_run ) ) { |
| 352 |
$email = MainWP_DB_Common::instance()->get_user_notification_email( $task->userid ); |
| 353 |
if ( '' !== $email ) { |
| 354 |
$output = 'A scheduled backup has started with MainWP on ' . MainWP_Utility::format_timestamp( MainWP_Utility::get_timestamp() ) . ' for the following ' . count( $sites ) . ' sites:<br />'; |
| 355 |
|
| 356 |
foreach ( $sites as $siteid ) { |
| 357 |
$website = MainWP_DB::instance()->get_website_by_id( $siteid ); |
| 358 |
$output .= ' • <a href="' . $website->url . '">' . MainWP_Utility::get_nice_url( $website->url ) . '</a><br />'; |
| 359 |
} |
| 360 |
|
| 361 |
$output .= '<br />Backup details:<br /><br />'; |
| 362 |
$output .= '<strong>Backup task</strong> - ' . $task->name . '<br />'; |
| 363 |
$output .= '<strong>Backup type</strong> - ' . ( 'db' === $task->type ? 'DATABASE BACKUP' : 'FULL BACKUP' ) . '<br />'; |
| 364 |
$output .= '<strong>Backup schedule</strong> - ' . strtoupper( $task->schedule ) . '<br />'; |
| 365 |
|
| 366 |
$mail_title = 'A Scheduled Backup has been Started - MainWP'; |
| 367 |
$formated_content = MainWP_Format::format_email( $email, $output, $mail_title ); |
| 368 |
wp_mail( $email, $mail_title, $formated_content, 'content-type: text/html' ); |
| 369 |
MainWP_DB_Backup::instance()->update_backup_task_with_values( $task->id, array( 'lastStartNotificationSent' => time() ) ); |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
$currentCount = 0; |
| 374 |
|
| 375 |
foreach ( $sites as $siteid ) { |
| 376 |
if ( isset( $completed_sites[ $siteid ] ) && ( true === $completed_sites[ $siteid ] ) ) { |
| 377 |
continue; |
| 378 |
} |
| 379 |
$website = MainWP_DB::instance()->get_website_by_id( $siteid ); |
| 380 |
|
| 381 |
try { |
| 382 |
$subfolder = str_replace( '%task%', MainWP_Utility::sanitize( $task->name ), $task->subfolder ); |
| 383 |
|
| 384 |
$backupResult = MainWP_Backup_Handler::backup_site( $siteid, $task, $subfolder ); |
| 385 |
|
| 386 |
if ( false === $backupResult ) { |
| 387 |
continue; |
| 388 |
} |
| 389 |
|
| 390 |
if ( null === $errorOutput ) { |
| 391 |
$errorOutput = ''; |
| 392 |
} |
| 393 |
$error = false; |
| 394 |
$tmpErrorOutput = ''; |
| 395 |
if ( isset( $backupResult['error'] ) ) { |
| 396 |
$tmpErrorOutput .= $backupResult['error'] . '<br />'; |
| 397 |
$error = true; |
| 398 |
} |
| 399 |
if ( isset( $backupResult['ftp'] ) && 'success' !== $backupResult['ftp'] ) { |
| 400 |
$tmpErrorOutput .= 'FTP: ' . $backupResult['ftp'] . '<br />'; |
| 401 |
$error = true; |
| 402 |
} |
| 403 |
if ( isset( $backupResult['dropbox'] ) && 'success' !== $backupResult['dropbox'] ) { |
| 404 |
$tmpErrorOutput .= 'Dropbox: ' . $backupResult['dropbox'] . '<br />'; |
| 405 |
$error = true; |
| 406 |
} |
| 407 |
|
| 408 |
if ( isset( $backupResult['amazon'] ) && 'success' !== $backupResult['amazon'] ) { |
| 409 |
$tmpErrorOutput .= 'Amazon: ' . $backupResult['amazon'] . '<br />'; |
| 410 |
$error = true; |
| 411 |
} |
| 412 |
|
| 413 |
if ( $error ) { |
| 414 |
$errorOutput .= 'Site: <strong>' . MainWP_Utility::get_nice_url( $website->url ) . '</strong><br />'; |
| 415 |
$errorOutput .= $tmpErrorOutput . '<br />'; |
| 416 |
} |
| 417 |
} catch ( \Exception $e ) { |
| 418 |
if ( null === $errorOutput ) { |
| 419 |
$errorOutput = ''; |
| 420 |
} |
| 421 |
$errorOutput .= 'Site: <strong>' . MainWP_Utility::get_nice_url( $website->url ) . '</strong><br />'; |
| 422 |
$errorOutput .= MainWP_Error_Helper::get_error_message( $e ) . '<br />'; |
| 423 |
$_error_output = MainWP_Error_Helper::get_error_message( $e ); |
| 424 |
} |
| 425 |
|
| 426 |
$_backup_result = isset( $backupResult ) ? $backupResult : ( isset( $_error_output ) ? $_error_output : '' ); |
| 427 |
do_action( 'mainwp_managesite_schedule_backup', $website, array( 'type' => $task->type ), $_backup_result ); |
| 428 |
|
| 429 |
++$currentCount; |
| 430 |
|
| 431 |
$task = MainWP_DB_Backup::instance()->get_backup_task_by_id( $task->id ); |
| 432 |
|
| 433 |
$completed_sites = $task->completed_sites; |
| 434 |
|
| 435 |
if ( '' !== $completed_sites ) { |
| 436 |
$completed_sites = json_decode( $completed_sites, true ); |
| 437 |
} |
| 438 |
if ( ! is_array( $completed_sites ) ) { |
| 439 |
$completed_sites = array(); |
| 440 |
} |
| 441 |
|
| 442 |
$completed_sites[ $siteid ] = true; |
| 443 |
MainWP_DB_Backup::instance()->update_completed_sites( $task->id, $completed_sites ); |
| 444 |
|
| 445 |
if ( ( 0 !== $nrOfSites ) && ( $nrOfSites <= $currentCount ) ) { |
| 446 |
break; |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
if ( null !== $errorOutput ) { |
| 451 |
MainWP_DB_Backup::instance()->update_backup_errors( $task->id, $errorOutput ); |
| 452 |
} |
| 453 |
|
| 454 |
if ( count( $completed_sites ) === count( $sites ) ) { |
| 455 |
MainWP_DB_Backup::instance()->update_backup_completed( $task->id ); |
| 456 |
|
| 457 |
if ( 1 === get_option( 'mainwp_notificationOnBackupFail' ) ) { |
| 458 |
$email = MainWP_DB_Common::instance()->get_user_notification_email( $task->userid ); |
| 459 |
if ( '' !== $email ) { |
| 460 |
$task = MainWP_DB_Backup::instance()->get_backup_task_by_id( $task->id ); |
| 461 |
if ( '' !== $task->backup_errors ) { |
| 462 |
$errorOutput = 'Errors occurred while executing task: <strong>' . $task->name . '</strong><br /><br />' . $task->backup_errors; |
| 463 |
$mail_title = 'A scheduled backup had an Error - MainWP'; |
| 464 |
$formated_content = MainWP_Format::format_email( $email, $errorOutput, $mail_title ); |
| 465 |
wp_mail( $email, $mail_title, $formated_content, 'content-type: text/html' ); |
| 466 |
|
| 467 |
MainWP_DB_Backup::instance()->update_backup_errors( $task->id, '' ); |
| 468 |
} |
| 469 |
} |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
return empty( $errorOutput ); |
| 474 |
} |
| 475 |
|
| 476 |
/** |
| 477 |
* Prepare Child Site to be backed up. |
| 478 |
* |
| 479 |
* @param mixed $pTaskId Task ID. |
| 480 |
* @param mixed $pSiteId Child Site ID. |
| 481 |
* @param mixed $pFileNameUID Filename Unique ID. |
| 482 |
* |
| 483 |
* @return self MainWP_Manage_Sites_Handler() |
| 484 |
* |
| 485 |
* @throws MainWP_Exception Error message. |
| 486 |
* |
| 487 |
* @uses \MainWP\Dashboard\MainWP_DB_Backup::get_backup_task_by_id() |
| 488 |
* @uses \MainWP\Dashboard\MainWP_Backup_Handler::backup() |
| 489 |
* @uses \MainWP\Dashboard\MainWP_Utility::sanitize() |
| 490 |
*/ |
| 491 |
public static function backup( $pTaskId, $pSiteId, $pFileNameUID ) { |
| 492 |
$backupTask = MainWP_DB_Backup::instance()->get_backup_task_by_id( $pTaskId ); |
| 493 |
|
| 494 |
$subfolder = str_replace( '%task%', MainWP_Utility::sanitize( $backupTask->name ), $backupTask->subfolder ); |
| 495 |
|
| 496 |
if ( 'site' === $backupTask->archiveFormat ) { |
| 497 |
$loadFilesBeforeZip = false; |
| 498 |
$maximumFileDescriptorsOverride = false; |
| 499 |
$maximumFileDescriptorsAuto = false; |
| 500 |
$maximumFileDescriptors = 150; |
| 501 |
$archiveFormat = false; |
| 502 |
} elseif ( 'global' === $backupTask->archiveFormat ) { |
| 503 |
$loadFilesBeforeZip = false; |
| 504 |
$maximumFileDescriptorsOverride = false; |
| 505 |
$maximumFileDescriptorsAuto = false; |
| 506 |
$maximumFileDescriptors = 150; |
| 507 |
$archiveFormat = 'global'; |
| 508 |
} else { |
| 509 |
$loadFilesBeforeZip = $backupTask->loadFilesBeforeZip; |
| 510 |
$maximumFileDescriptorsOverride = ( 'zip' === $backupTask->archiveFormat ) && ( 1 === (int) $backupTask->maximumFileDescriptorsOverride ); |
| 511 |
$maximumFileDescriptorsAuto = ( 'zip' === $backupTask->archiveFormat ) && ( 1 === (int) $backupTask->maximumFileDescriptorsAuto ); |
| 512 |
$maximumFileDescriptors = $backupTask->maximumFileDescriptors; |
| 513 |
$archiveFormat = $backupTask->archiveFormat; |
| 514 |
} |
| 515 |
|
| 516 |
return MainWP_Backup_Handler::backup( $pSiteId, $backupTask->type, $subfolder, $backupTask->exclude, $backupTask->excludebackup, $backupTask->excludecache, $backupTask->excludenonwp, $backupTask->excludezip, $backupTask->filename, $pFileNameUID, $archiveFormat, $maximumFileDescriptorsOverride, $maximumFileDescriptorsAuto, $maximumFileDescriptors, $loadFilesBeforeZip ); |
| 517 |
} |
| 518 |
|
| 519 |
/** |
| 520 |
* Get backup tasks and site ID. |
| 521 |
* |
| 522 |
* @param mixed $pTaskId Task ID. |
| 523 |
* |
| 524 |
* @return array $allSites All Sites array. |
| 525 |
* $remoteDestinations Remote destinations array. |
| 526 |
* |
| 527 |
* @uses \MainWP\Dashboard\MainWP_DB_Backup::get_backup_task_by_id() |
| 528 |
* @uses \MainWP\Dashboard\MainWP_DB_Backup::update_backup_run_manually() |
| 529 |
* @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id() |
| 530 |
* @uses \MainWP\Dashboard\MainWP_DB::get_website_by_group_id() |
| 531 |
*/ |
| 532 |
public static function get_backup_task_sites( $pTaskId ) { |
| 533 |
$sites = array(); |
| 534 |
$backupTask = MainWP_DB_Backup::instance()->get_backup_task_by_id( $pTaskId ); |
| 535 |
if ( empty( $backupTask->groups ) ) { |
| 536 |
if ( '' !== $backupTask->sites ) { |
| 537 |
$sites = explode( ',', $backupTask->sites ); |
| 538 |
} |
| 539 |
} else { |
| 540 |
$groups = explode( ',', $backupTask->groups ); |
| 541 |
foreach ( $groups as $groupid ) { |
| 542 |
$group_sites = MainWP_DB::instance()->get_websites_by_group_id( $groupid ); |
| 543 |
foreach ( $group_sites as $group_site ) { |
| 544 |
if ( in_array( $group_site->id, $sites ) ) { |
| 545 |
continue; |
| 546 |
} |
| 547 |
$sites[] = $group_site->id; |
| 548 |
} |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
$allSites = array(); |
| 553 |
foreach ( $sites as $site ) { |
| 554 |
$website = MainWP_DB::instance()->get_website_by_id( $site ); |
| 555 |
$allSites[] = array( |
| 556 |
'id' => $website->id, |
| 557 |
'name' => $website->name, |
| 558 |
'fullsize' => $website->totalsize * 1024, |
| 559 |
'dbsize' => $website->dbsize, |
| 560 |
); |
| 561 |
} |
| 562 |
|
| 563 |
$remoteDestinations = apply_filters( 'mainwp_backuptask_remotedestinations', array(), $backupTask ); |
| 564 |
MainWP_DB_Backup::instance()->update_backup_run_manually( $pTaskId ); |
| 565 |
|
| 566 |
return array( |
| 567 |
'sites' => $allSites, |
| 568 |
'remoteDestinations' => $remoteDestinations, |
| 569 |
); |
| 570 |
} |
| 571 |
|
| 572 |
/** |
| 573 |
* Remove Backup. |
| 574 |
* |
| 575 |
* @uses \MainWP\Dashboard\MainWP_DB_Backup::get_backup_task_by_id() |
| 576 |
* @uses \MainWP\Dashboard\MainWP_DB_Backup::remove_backup_task() |
| 577 |
*/ |
| 578 |
public static function remove_backup() { |
| 579 |
if ( isset( $_POST['id'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 580 |
$task = MainWP_DB_Backup::instance()->get_backup_task_by_id( intval( $_POST['id'] ) ); //phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 581 |
if ( static::can_edit_backuptask( $task ) ) { |
| 582 |
MainWP_DB_Backup::instance()->remove_backup_task( $task->id ); |
| 583 |
die( wp_json_encode( array( 'result' => 'SUCCESS' ) ) ); |
| 584 |
} |
| 585 |
} |
| 586 |
die( wp_json_encode( array( 'notask' => true ) ) ); |
| 587 |
} |
| 588 |
|
| 589 |
/** |
| 590 |
* Resume Backup. |
| 591 |
* |
| 592 |
* @uses \MainWP\Dashboard\MainWP_DB_Backup::get_backup_task_by_id() |
| 593 |
* @uses \MainWP\Dashboard\MainWP_DB_Backup::update_backup_task_with_values() |
| 594 |
*/ |
| 595 |
public static function resume_backup() { |
| 596 |
$id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : false; //phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 597 |
if ( $id ) { |
| 598 |
$task = MainWP_DB_Backup::instance()->get_backup_task_by_id( $id ); |
| 599 |
if ( static::can_edit_backuptask( $task ) ) { |
| 600 |
MainWP_DB_Backup::instance()->update_backup_task_with_values( $task->id, array( 'paused' => 0 ) ); |
| 601 |
die( wp_json_encode( array( 'result' => 'SUCCESS' ) ) ); |
| 602 |
} |
| 603 |
} |
| 604 |
die( wp_json_encode( array( 'notask' => true ) ) ); |
| 605 |
} |
| 606 |
|
| 607 |
/** |
| 608 |
* Pause Backup. |
| 609 |
* |
| 610 |
* @uses \MainWP\Dashboard\MainWP_DB_Backup::get_backup_task_by_id() |
| 611 |
* @uses \MainWP\Dashboard\MainWP_DB_Backup::update_backup_task_with_values() |
| 612 |
*/ |
| 613 |
public static function pause_backup() { |
| 614 |
$id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : false; //phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 615 |
if ( $id ) { |
| 616 |
$task = MainWP_DB_Backup::instance()->get_backup_task_by_id( $id ); |
| 617 |
if ( static::can_edit_backuptask( $task ) ) { |
| 618 |
MainWP_DB_Backup::instance()->update_backup_task_with_values( $task->id, array( 'paused' => 1 ) ); |
| 619 |
die( wp_json_encode( array( 'result' => 'SUCCESS' ) ) ); |
| 620 |
} |
| 621 |
} |
| 622 |
die( wp_json_encode( array( 'notask' => true ) ) ); |
| 623 |
} |
| 624 |
} |
| 625 |
|