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

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

564 lines 17.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * This class handles the MainWP Post Backups.
4 *
5 * @package MainWP/Dashboard
6 */
7
8 namespace MainWP\Dashboard;
9
10 /**
11 * Class MainWP_Post_Backup_Handler
12 *
13 * Handle Backup Post.
14 *
15 * @package MainWP\Dashboard
16 */
17 class MainWP_Post_Backup_Handler extends MainWP_Post_Base_Handler {
18
19 /** @var $instance Singleton MainWP_Post_Backup_Handler. */
20 private static $instance = null;
21
22 /**
23 * Create public static instance.
24 *
25 * @static
26 * @return self $instance MainWP_Post_Backup_Handler.
27 */
28 public static function instance() {
29 if ( null == self::$instance ) {
30 self::$instance = new self();
31 }
32 return self::$instance;
33 }
34
35 /**
36 * Init backups actions
37 */
38 public function init() {
39 // Page: ManageBackups.
40 $this->add_action( 'mainwp_addbackup', array( &$this, 'mainwp_addbackup' ) );
41 if ( mainwp_current_user_have_right( 'dashboard', 'edit_backup_tasks' ) ) {
42 $this->add_action( 'mainwp_updatebackup', array( &$this, 'mainwp_updatebackup' ) );
43 }
44 if ( mainwp_current_user_have_right( 'dashboard', 'delete_backup_tasks' ) ) {
45 $this->add_action( 'mainwp_removebackup', array( &$this, 'mainwp_removebackup' ) );
46 }
47 $this->add_action( 'mainwp_pausebackup', array( &$this, 'mainwp_pausebackup' ) );
48 $this->add_action( 'mainwp_resumebackup', array( &$this, 'mainwp_resumebackup' ) );
49
50 $this->add_action( 'mainwp_backuptask_get_sites', array( &$this, 'mainwp_backuptask_get_sites' ) );
51
52 if ( mainwp_current_user_have_right( 'dashboard', 'run_backup_tasks' ) ) {
53 $this->add_action( 'mainwp_backuptask_run_site', array( &$this, 'mainwp_backuptask_run_site' ) );
54 }
55 $this->add_action( 'mainwp_backup_upload_file', array( &$this, 'mainwp_backup_upload_file' ) );
56
57 // Page: backup.
58 if ( mainwp_current_user_have_right( 'dashboard', 'run_backup_tasks' ) ) {
59 $this->add_action( 'mainwp_backup_run_site', array( &$this, 'mainwp_backup_run_site' ) );
60 }
61 if ( mainwp_current_user_have_right( 'dashboard', 'execute_backups' ) ) {
62 $this->add_action( 'mainwp_backup', array( &$this, 'mainwp_backup' ) );
63 }
64 $this->add_action( 'mainwp_checkbackups', array( &$this, 'mainwp_checkbackups' ) );
65 $this->add_action( 'mainwp_backup_checkpid', array( &$this, 'mainwp_backup_checkpid' ) );
66 $this->add_action( 'mainwp_createbackup_getfilesize', array( &$this, 'mainwp_createbackup_getfilesize' ) );
67 $this->add_action( 'mainwp_backup_download_file', array( &$this, 'mainwp_backup_download_file' ) );
68 $this->add_action( 'mainwp_backup_delete_file', array( &$this, 'mainwp_backup_delete_file' ) );
69 $this->add_action( 'mainwp_backup_getfilesize', array( &$this, 'mainwp_backup_getfilesize' ) );
70 $this->add_action( 'mainwp_backup_upload_getprogress', array( &$this, 'mainwp_backup_upload_getprogress' ) );
71 $this->add_action( 'mainwp_backup_upload_checkstatus', array( &$this, 'mainwp_backup_upload_checkstatus' ) );
72 }
73
74 /*
75 * Page: Backup
76 */
77
78 /**
79 * Method mainwp_backup_run_site()
80 *
81 * Run backup task of site
82 *
83 * @throws MainWP_Exception on errors.
84 */
85 public function mainwp_backup_run_site() {
86 $this->secure_request( 'mainwp_backup_run_site' );
87 $site_id = isset( $_POST['site_id'] ) ? intval( $_POST['site_id'] ) : false;
88 try {
89 if ( ! $site_id ) {
90 throw new MainWP_Exception( 'Invalid request' );
91 }
92
93 $ret = array( 'result' => MainWP_Backup_Handler::backup( $site_id, 'full', '', '', 1, 1, 1, 1 ) );
94 wp_send_json( $ret );
95 } catch ( MainWP_Exception $e ) {
96 die(
97 wp_json_encode(
98 array(
99 'error' => array(
100 'message' => $e->getMessage(),
101 'extra' => $e->get_message_extra(),
102 ),
103 )
104 )
105 );
106 }
107 }
108
109 /**
110 * Method mainwp_backup()
111 *
112 * Run backup task
113 *
114 * @throws MainWP_Exception on errors.
115 */
116 public function mainwp_backup() {
117 $this->secure_request( 'mainwp_backup' );
118 $site_id = isset( $_POST['site_id'] ) ? intval( $_POST['site_id'] ) : false;
119 try {
120 if ( ! $site_id ) {
121 throw new MainWP_Exception( 'Invalid request' );
122 }
123
124 $excludedFolder = isset( $_POST['exclude'] ) ? trim( $_POST['exclude'], "\n" ) : '';
125 $excludedFolder = explode( "\n", $excludedFolder );
126 $excludedFolder = array_map( array( 'MainWP_Utility', 'trim_slashes' ), $excludedFolder );
127 $excludedFolder = array_map( 'htmlentities', $excludedFolder );
128 $excludedFolder = implode( ',', $excludedFolder );
129
130 $type = isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : '';
131 $subfolder = isset( $_POST['subfolder'] ) ? sanitize_text_field( wp_unslash( $_POST['subfolder'] ) ) : '';
132 $excludebackup = isset( $_POST['excludebackup'] ) ? sanitize_text_field( wp_unslash( $_POST['excludebackup'] ) ) : '';
133 $excludecache = isset( $_POST['excludecache'] ) ? sanitize_text_field( wp_unslash( $_POST['excludecache'] ) ) : '';
134 $excludenonwp = isset( $_POST['excludenonwp'] ) ? sanitize_text_field( wp_unslash( $_POST['excludenonwp'] ) ) : '';
135 $excludezip = isset( $_POST['excludezip'] ) ? sanitize_text_field( wp_unslash( $_POST['excludezip'] ) ) : '';
136 $filename = isset( $_POST['filename'] ) ? sanitize_text_field( wp_unslash( $_POST['filename'] ) ) : '';
137 $fileNameUID = isset( $_POST['fileNameUID'] ) ? sanitize_text_field( wp_unslash( $_POST['fileNameUID'] ) ) : '';
138 $archiveFormat = isset( $_POST['archiveFormat'] ) ? sanitize_text_field( wp_unslash( $_POST['archiveFormat'] ) ) : '';
139 $maximumFileDescriptorsOverride = isset( $_POST['maximumFileDescriptorsOverride'] ) ? intval( $_POST['maximumFileDescriptorsOverride'] ) : false;
140 $maximumFileDescriptorsAuto = isset( $_POST['maximumFileDescriptorsAuto'] ) ? intval( $_POST['maximumFileDescriptorsAuto'] ) : false;
141 $maximumFileDescriptors = isset( $_POST['maximumFileDescriptors'] ) ? sanitize_text_field( wp_unslash( $_POST['maximumFileDescriptors'] ) ) : '';
142 $loadFilesBeforeZip = isset( $_POST['loadFilesBeforeZip'] ) ? sanitize_text_field( wp_unslash( $_POST['loadFilesBeforeZip'] ) ) : false;
143 $pid = isset( $_POST['pid'] ) ? intval( $_POST['pid'] ) : false;
144 $append = isset( $_POST['append'] ) ? intval( $_POST['append'] ) : false;
145
146 $result = MainWP_Backup_Handler::backup( $site_id, $type, $subfolder, $excludedFolder, $excludebackup, $excludecache, $excludenonwp, $excludezip, $filename, $fileNameUID, $archiveFormat, $maximumFileDescriptorsOverride, $maximumFileDescriptorsAuto, $maximumFileDescriptors, $loadFilesBeforeZip, $pid, $append );
147 wp_send_json( array( 'result' => $result ) );
148 } catch ( MainWP_Exception $e ) {
149 die(
150 wp_json_encode(
151 array(
152 'error' => array(
153 'message' => $e->getMessage(),
154 'extra' => $e->get_message_extra(),
155 ),
156 )
157 )
158 );
159 }
160 }
161
162 /**
163 * Method mainwp_backup_checkpid()
164 *
165 * Check backup task
166 *
167 * @throws MainWP_Exception on errors.
168 */
169 public function mainwp_backup_checkpid() {
170 $this->secure_request( 'mainwp_backup_checkpid' );
171 $site_id = isset( $_POST['site_id'] ) ? intval( $_POST['site_id'] ) : false;
172 try {
173 if ( ! $site_id ) {
174 throw new MainWP_Exception( 'Invalid request' );
175 }
176 $pid = isset( $_POST['pid'] ) ? intval( $_POST['pid'] ) : false;
177 $type = isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : '';
178 $subfolder = isset( $_POST['subfolder'] ) ? sanitize_text_field( wp_unslash( $_POST['subfolder'] ) ) : '';
179 $filename = isset( $_POST['filename'] ) ? sanitize_text_field( wp_unslash( $_POST['filename'] ) ) : '';
180 wp_send_json( MainWP_Backup_Handler::backup_check_pid( $site_id, $pid, $type, $subfolder, $filename ) );
181 } catch ( MainWP_Exception $e ) {
182 die(
183 wp_json_encode(
184 array(
185 'error' => array(
186 'message' => $e->getMessage(),
187 'extra' => $e->get_message_extra(),
188 ),
189 )
190 )
191 );
192 }
193 }
194
195 /**
196 * Method mainwp_backup_download_file()
197 *
198 * Download backup file
199 *
200 * @throws MainWP_Exception on errors.
201 */
202 public function mainwp_backup_download_file() {
203 $this->secure_request( 'mainwp_backup_download_file' );
204 $site_id = isset( $_POST['site_id'] ) ? intval( $_POST['site_id'] ) : false;
205 try {
206 if ( ! $site_id ) {
207 throw new MainWP_Exception( 'Invalid request' );
208 }
209 $type = isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : '';
210 $url = isset( $_POST['url'] ) ? sanitize_text_field( wp_unslash( $_POST['url'] ) ) : '';
211 $local = isset( $_POST['local'] ) ? sanitize_text_field( wp_unslash( $_POST['local'] ) ) : '';
212 die( wp_json_encode( array( 'result' => MainWP_Backup_Handler::backup_download_file( $site_id, $type, $url, $local ) ) ) );
213 } catch ( MainWP_Exception $e ) {
214 die(
215 wp_json_encode(
216 array(
217 'error' => array(
218 'message' => $e->getMessage(),
219 'extra' => $e->get_message_extra(),
220 ),
221 )
222 )
223 );
224 }
225 }
226
227 /**
228 * Method mainwp_backup_delete_file()
229 *
230 * Delete backup file
231 *
232 * @throws MainWP_Exception on errors.
233 */
234 public function mainwp_backup_delete_file() {
235 $this->secure_request( 'mainwp_backup_delete_file' );
236 $site_id = isset( $_POST['site_id'] ) ? intval( $_POST['site_id'] ) : false;
237 try {
238 if ( ! $site_id ) {
239 throw new MainWP_Exception( __( 'Invalid request!', 'mainwp' ) );
240 }
241
242 $site_id = isset( $_POST['site_id'] ) ? intval( $_POST['site_id'] ) : 0;
243 $file = isset( $_POST['file'] ) ? sanitize_text_field( wp_unslash( $_POST['file'] ) ) : '';
244
245 die( wp_json_encode( array( 'result' => MainWP_Backup_Handler::backup_delete_file( $site_id, $file ) ) ) );
246 } catch ( MainWP_Exception $e ) {
247 die(
248 wp_json_encode(
249 array(
250 'error' => array(
251 'message' => $e->getMessage(),
252 'extra' => $e->get_message_extra(),
253 ),
254 )
255 )
256 );
257 }
258 }
259
260 /**
261 * Method mainwp_createbackup_getfilesize()
262 *
263 * Get create backup file size.
264 *
265 * @throws \Exception on errors.
266 */
267 public function mainwp_createbackup_getfilesize() {
268 $this->secure_request( 'mainwp_createbackup_getfilesize' );
269
270 try {
271 if ( ! isset( $_POST['siteId'] ) ) {
272 throw new \Exception( __( 'No site selected!', 'mainwp' ) );
273 }
274 $siteId = isset( $_POST['siteId'] ) ? intval( $_POST['siteId'] ) : '';
275 $fileName = isset( $_POST['fileName'] ) ? sanitize_text_field( wp_unslash( $_POST['fileName'] ) ) : '';
276 $fileNameUID = isset( $_POST['fileNameUID'] ) ? sanitize_text_field( wp_unslash( $_POST['fileNameUID'] ) ) : '';
277 $type = isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : '';
278
279 $website = MainWP_DB::instance()->get_website_by_id( $siteId );
280 if ( ! $website ) {
281 throw new \Exception( __( 'No site selected!', 'mainwp' ) );
282 }
283
284 MainWP_Utility::end_session();
285 // Send request to the childsite!
286 $result = MainWP_Connect::fetch_url_authed(
287 $website,
288 'createBackupPoll',
289 array(
290 'fileName' => $fileName,
291 'fileNameUID' => $fileNameUID,
292 'type' => $type,
293 )
294 );
295
296 if ( ! isset( $result['size'] ) ) {
297 throw new \Exception( __( 'Invalid response!', 'mainwp' ) );
298 }
299
300 if ( MainWP_Utility::ctype_digit( $result['size'] ) ) {
301 $output = array( 'size' => $result['size'] );
302 } else {
303 $output = array();
304 }
305 } catch ( \Exception $e ) {
306 $output = array( 'error' => $e->getMessage() );
307 }
308
309 die( wp_json_encode( $output ) );
310 }
311
312 /**
313 * Method mainwp_backup_getfilesize()
314 *
315 * Get backup file size
316 *
317 * @throws MainWP_Exception on errors.
318 */
319 public function mainwp_backup_getfilesize() {
320 $this->secure_request( 'mainwp_backup_getfilesize' );
321
322 try {
323 $local = isset( $_POST['local'] ) ? sanitize_text_field( wp_unslash( $_POST['local'] ) ) : '';
324 die( wp_json_encode( array( 'result' => MainWP_Manage_Sites::backup_get_file_size( $local ) ) ) );
325 } catch ( MainWP_Exception $e ) {
326 die(
327 wp_json_encode(
328 array(
329 'error' => array(
330 'message' => $e->getMessage(),
331 'extra' => $e->get_message_extra(),
332 ),
333 )
334 )
335 );
336 }
337 }
338
339 /**
340 * Method mainwp_backup_upload_checkstatus()
341 *
342 * Check upload status
343 *
344 * @throws MainWP_Exception on errors.
345 */
346 public function mainwp_backup_upload_checkstatus() {
347 $this->secure_request( 'mainwp_backup_upload_checkstatus' );
348
349 try {
350 $unique = isset( $_POST['unique'] ) ? sanitize_text_field( wp_unslash( $_POST['unique'] ) ) : false;
351 $array = get_option( 'mainwp_upload_progress' );
352 $info = apply_filters( 'mainwp_remote_destination_info', array(), ( isset( $_POST['remote_destination'] ) ? sanitize_text_field( wp_unslash( $_POST['remote_destination'] ) ) : '' ) );
353
354 if ( ! is_array( $array ) || empty( $unique ) || empty( $array[ $unique ] ) || empty( $array[ $unique ]['dts'] ) ) {
355 die(
356 wp_json_encode(
357 array(
358 'status' => 'stalled',
359 'info' => $info,
360 )
361 )
362 );
363 } elseif ( isset( $array[ $unique ]['finished'] ) ) {
364 die(
365 wp_json_encode(
366 array(
367 'status' => 'done',
368 'info' => $info,
369 )
370 )
371 );
372 } else {
373
374 if ( isset( $array[ $unique ]['dts'] ) && intval( $array[ $unique ]['dts'] ) < ( time() - ( 2 * 60 ) ) ) { // 2minutes.
375 die(
376 wp_json_encode(
377 array(
378 'status' => 'stalled',
379 'info' => $info,
380 )
381 )
382 );
383 } else {
384 die(
385 wp_json_encode(
386 array(
387 'status' => 'busy',
388 'info' => $info,
389 )
390 )
391 );
392 }
393 }
394 } catch ( MainWP_Exception $e ) {
395 die(
396 wp_json_encode(
397 array(
398 'error' => array(
399 'message' => $e->getMessage(),
400 'extra' => $e->get_message_extra(),
401 ),
402 )
403 )
404 );
405 }
406 }
407
408 /**
409 * Method mainwp_backup_upload_getprogress()
410 *
411 * Get progress status
412 *
413 * @throws MainWP_Exception on finished or errors.
414 */
415 public function mainwp_backup_upload_getprogress() {
416 $this->secure_request( 'mainwp_backup_upload_getprogress' );
417
418 try {
419 $array = get_option( 'mainwp_upload_progress' );
420 $unique = isset( $_POST['unique'] ) ? sanitize_text_field( wp_unslash( $_POST['unique'] ) ) : false;
421
422 if ( ! is_array( $array ) || ! isset( $array[ $unique ] ) ) {
423 die( wp_json_encode( array( 'result' => 0 ) ) );
424 } elseif ( isset( $array[ $unique ]['finished'] ) ) {
425 throw new MainWP_Exception( __( 'finished...', 'maiwnp' ) );
426 } else {
427 wp_send_json( array( 'result' => ( isset( $array[ $unique ]['offset'] ) ? $array[ $unique ]['offset'] : $array[ $unique ] ) ) );
428 }
429 } catch ( MainWP_Exception $e ) {
430 die(
431 wp_json_encode(
432 array(
433 'error' => array(
434 'message' => $e->getMessage(),
435 'extra' => $e->get_message_extra(),
436 ),
437 )
438 )
439 );
440 }
441 }
442
443 /*
444 * Page: ManageBackups
445 */
446
447 /**
448 * Method mainwp_addbackup()
449 *
450 * Add task to the database.
451 */
452 public function mainwp_addbackup() {
453 $this->secure_request( 'mainwp_addbackup' );
454
455 MainWP_Manage_Backups_Handler::add_backup();
456 }
457
458 /**
459 * Method mainwp_updatebackup()
460 *
461 * Update task.
462 */
463 public function mainwp_updatebackup() {
464 $this->secure_request( 'mainwp_updatebackup' );
465
466 MainWP_Manage_Backups_Handler::update_backup();
467 }
468
469 /**
470 * Method mainwp_removebackup()
471 *
472 * Remove a task from MainWP.
473 */
474 public function mainwp_removebackup() {
475 $this->secure_request( 'mainwp_removebackup' );
476
477 MainWP_Manage_Backups_Handler::remove_backup();
478 }
479
480 /**
481 * Method mainwp_resumebackup()
482 *
483 * Resume backup task.
484 */
485 public function mainwp_resumebackup() {
486 $this->secure_request( 'mainwp_resumebackup' );
487
488 MainWP_Manage_Backups_Handler::resume_backup();
489 }
490
491 /**
492 * Method mainwp_pausebackup()
493 *
494 * Pause backup task.
495 */
496 public function mainwp_pausebackup() {
497 $this->secure_request( 'mainwp_pausebackup' );
498
499 MainWP_Manage_Backups_Handler::pause_backup();
500 }
501
502 /**
503 * Method mainwp_backuptask_get_sites()
504 *
505 * Get sites of backup task.
506 */
507 public function mainwp_backuptask_get_sites() {
508 $this->secure_request( 'mainwp_backuptask_get_sites' );
509
510 $taskID = isset( $_POST['task_id'] ) ? intval( $_POST['task_id'] ) : 0;
511
512 wp_send_json( array( 'result' => MainWP_Manage_Backups_Handler::get_backup_task_sites( $taskID ) ) );
513 }
514
515 /**
516 * Method mainwp_backuptask_run_site()
517 *
518 * Run backup task of site.
519 *
520 * @throws MainWP_Exception on errors.
521 */
522 public function mainwp_backuptask_run_site() {
523 try {
524 $this->secure_request( 'mainwp_backuptask_run_site' );
525 $site_id = isset( $_POST['site_id'] ) ? intval( $_POST['site_id'] ) : false;
526 $task_id = isset( $_POST['task_id'] ) ? intval( $_POST['task_id'] ) : false;
527 if ( ! $site_id || ! $task_id ) {
528 throw new MainWP_Exception( 'Invalid request' );
529 }
530
531 $fileNameUID = isset( $_POST['fileNameUID'] ) ? sanitize_text_field( wp_unslash( $_POST['fileNameUID'] ) ) : false;
532 wp_send_json( array( 'result' => MainWP_Manage_Backups_Handler::backup( $task_id, $site_id, $fileNameUID ) ) );
533 } catch ( MainWP_Exception $e ) {
534 die(
535 wp_json_encode(
536 array(
537 'error' => array(
538 'message' => $e->getMessage(),
539 'extra' => $e->get_message_extra(),
540 ),
541 )
542 )
543 );
544 }
545 }
546
547 /**
548 * Method mainwp_checkbackups()
549 *
550 * Check backup status.
551 *
552 * @throws \Exception on errors.
553 */
554 public function mainwp_checkbackups() {
555 $this->secure_request( 'mainwp_checkbackups' );
556
557 try {
558 wp_send_json( array( 'result' => MainWP_Updates_Overview::check_backups() ) );
559 } catch ( \Exception $e ) {
560 die( wp_json_encode( array( 'error' => $e->getMessage() ) ) );
561 }
562 }
563 }
564