PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.0.2
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.0.2
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 / modules / api-backups / classes / class-api-backups-utility.php

class-api-backups-utility.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 5.0.2, at modules/api-backups/classes/class-api-backups-utility.php

727 lines 19.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class Api_Backups_Utility
4 *
5 * @package MainWP\Dashboard
6 * @version 5.0
7 */
8
9 namespace MainWP\Dashboard\Module\ApiBackups;
10
11 use MainWP\Dashboard\MainWP_DB;
12 use MainWP\Dashboard\MainWP_Logger;
13 use MainWP\Dashboard\MainWP_System_Utility;
14
15 /**
16 * Class Api_Backups_Utility
17 */
18 class Api_Backups_Utility {
19
20 /**
21 * Instance value.
22 *
23 * @var static Default null
24 */
25 public static $instance = null;
26
27 /**
28 * Get Instance
29 *
30 * Creates public static instance.
31 *
32 * @static
33 *
34 * @return Api_Backups_Utility
35 */
36 public static function get_instance() {
37 if ( null === self::$instance ) {
38 self::$instance = new self();
39 }
40 return self::$instance;
41 }
42
43 /**
44 * Method __construct.
45 */
46 public function __construct() {
47 // contructor.
48 }
49
50 /**
51 * Method count_child_sites().
52 */
53 public function count_child_sites() {
54 $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_wp_for_current_user() );
55
56 if ( ! empty( $websites ) ) {
57 $sites_count = MainWP_DB::num_rows( $websites );
58 }
59 // Free result set.
60 Api_Backups_Helper::free_result( $websites );
61
62 return $sites_count;
63 }
64
65 /**
66 * Get timestamp.
67 *
68 * @param int $timestamp Holds Timestamp.
69 *
70 * @return float|int Return GMT offset.
71 */
72 public static function get_timestamp( $timestamp = false ) {
73 if ( empty( $timestamp ) ) {
74 $timestamp = time();
75 }
76 $gmtOffset = get_option( 'gmt_offset' );
77
78 return ( $gmtOffset ? ( $gmtOffset * HOUR_IN_SECONDS ) + $timestamp : $timestamp );
79 }
80
81 /**
82 * Format timestamp.
83 *
84 * @param int $timestamp Holds Timestamp.
85 * @param bool $gmt Whether to set as General mountain time. Default: FALSE.
86 *
87 * @return string Return Timestamp.
88 */
89 public static function format_timestamp( $timestamp, $gmt = false ) {
90 return date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $timestamp, $gmt );
91 }
92
93 /**
94 * Format datestamp.
95 *
96 * @param int $timestamp Holds Timestamp.
97 * @param bool $gmt Whether to set as General mountain time. Default: FALSE.
98 *
99 * @return string Return Timestamp.
100 */
101 public static function format_datestamp( $timestamp, $gmt = false ) {
102 return date_i18n( get_option( 'date_format' ), $timestamp, $gmt );
103 }
104
105 /**
106 * Method map_fields()
107 *
108 * Map Site.
109 *
110 * @param mixed $website Website to map.
111 * @param mixed $keys Keys to map.
112 * @param bool $object_output Output format array|object.
113 *
114 * @return object $outputSite Mapped site.
115 */
116 public static function map_fields( &$website, $keys, $object_output = false ) {
117 $outputSite = array();
118 if ( ! empty( $website ) ) {
119 if ( is_object( $website ) ) {
120 foreach ( $keys as $key ) {
121 if ( property_exists( $website, $key ) ) {
122 $outputSite[ $key ] = $website->$key;
123 }
124 }
125 } elseif ( is_array( $website ) ) {
126 foreach ( $keys as $key ) {
127 $outputSite[ $key ] = $website[ $key ];
128 }
129 }
130 }
131
132 if ( $object_output ) {
133 return (object) $outputSite;
134 } else {
135 return $outputSite;
136 }
137 }
138
139 /**
140 * Method array_sort()
141 *
142 * Sort given array by given flags.
143 *
144 * @param mixed $arr Array to sort.
145 * @param mixed $key Array key.
146 * @param string $sort_flag Flags to sort by. Default = SORT_STRING.
147 */
148 public static function array_sort( &$arr, $key, $sort_flag = SORT_STRING ) {
149 $sorter = array();
150 $ret = array();
151 reset( $arr );
152 foreach ( $arr as $ii => $val ) {
153 $sorter[ $ii ] = $val[ $key ];
154 }
155 asort( $sorter, $sort_flag );
156 foreach ( $sorter as $ii => $val ) {
157 $ret[ $ii ] = $arr[ $ii ];
158 }
159 $arr = $ret;
160 }
161
162 /**
163 * Debugging log info.
164 *
165 * Sets logging for debugging purpose
166 *
167 * @param string $message Log info message.
168 */
169 public static function log_info( $message ) {
170 self::log_debug( $message, 2 );
171 }
172
173 /**
174 * Debugging log.
175 *
176 * Sets logging for debugging purpose.
177 *
178 * @param string $message Log debug message.
179 * @param int $log_color Log color.
180 */
181 public static function log_debug( $message, $log_color = 3 ) {
182 if ( is_array( $message ) || is_object( $message ) ) {
183 $message = print_r( $message, true ); //phpcs:ignore
184 } elseif ( ! is_string( $message ) ) {
185 $message = 'undefined';
186 }
187
188 if ( ! in_array( $log_color, array( 0, 1, 2, 3 ) ) ) {
189 $log_color = 3;
190 }
191 do_action( 'mainwp_log_action', 'API Backups :: ' . $message, MainWP_Logger::API_BACKUPS_LOG_PRIORITY, $log_color );
192 }
193
194 /**
195 *
196 * Method save_lasttime_backup().
197 *
198 * @param int $site_id site id.
199 * @param int $available_backups Available backups.
200 * @param string $backup_api API backups provider.
201 *
202 * @return mixed
203 */
204 public static function save_lasttime_backup( $site_id, $available_backups, $backup_api ) { //phpcs:ignore -- complex method.
205
206 self::log_debug( 'save backup time :: [available backups=' . ( is_string( $available_backups ) ? $available_backups : 'is not string' ) . ']' );
207
208 if ( empty( $available_backups ) ) {
209 return;
210 }
211
212 if ( is_string( $available_backups ) ) {
213 $available_backups = json_decode( $available_backups );
214 }
215
216 $primaryBackup = MainWP_System_Utility::get_primary_backup();
217
218 if ( 'module-api-backups' !== $primaryBackup ) {
219 return;
220 }
221
222 $lasttime_backup = 0;
223 if ( 'plesk' === $backup_api ) {
224 $available_backups = is_object( $available_backups ) && ! empty( $available_backups->value ) ? $available_backups->value : array();
225 if ( is_array( $available_backups ) ) {
226 foreach ( $available_backups as $backup ) {
227 if ( is_object( $backup ) && ! empty( $backup->value->createdAt->value ) ) {
228 $backup_time = strtotime( $backup->value->createdAt->value );
229 if ( $backup_time > $lasttime_backup ) {
230 $lasttime_backup = $backup_time;
231 }
232 }
233 }
234 }
235 } elseif ( 'cpanel_auto' === $backup_api ) {
236 if ( is_object( $available_backups ) && isset( $available_backups->data ) ) {
237 $available_backups_automatic_list = $available_backups->data;
238 } else {
239 $available_backups_automatic_list = array();
240 }
241
242 if ( is_array( $available_backups_automatic_list ) ) {
243 foreach ( $available_backups_automatic_list as $backup ) {
244 if ( is_object( $backup ) && ! empty( $backup->backupID ) ) {
245 $backup_time = strtotime( $backup->backupID );
246 if ( $backup_time > $lasttime_backup ) {
247 $lasttime_backup = $backup_time;
248 }
249 }
250 }
251 }
252 } elseif ( 'cpanel_manual' === $backup_api ) {
253 $available_backups_manual_list = $available_backups;
254 if ( is_array( $available_backups_manual_list ) ) {
255 foreach ( $available_backups_manual_list as $backup ) {
256 if ( is_object( $backup ) && ! empty( $backup->timestamp ) ) {
257 $backup_time = $backup->timestamp;
258 if ( $backup_time > $lasttime_backup ) {
259 $lasttime_backup = $backup_time;
260 }
261 }
262 }
263 }
264 } elseif ( 'cpanel_wp_toolkit' === $backup_api ) {
265 $available_backups_manual_list = $available_backups;
266 if ( is_array( $available_backups_manual_list ) ) {
267 foreach ( $available_backups_manual_list as $backup ) {
268 if ( is_object( $backup ) && ! empty( $backup->value->createdAt->value ) ) {
269 $backup_time = strtotime( $backup->value->createdAt->value );
270 if ( $backup_time > $lasttime_backup ) {
271 $lasttime_backup = $backup_time;
272 }
273 }
274 }
275 }
276 } elseif ( 'gridpane' === $backup_api ) {
277 if ( isset( $available_backups->automatic ) ) {
278 $available_backups_automatic_list = explode( ',', $available_backups->automatic );
279 } else {
280 $available_backups_automatic_list = array();
281 }
282 if ( isset( $available_backups->manual ) ) {
283 $available_backups_manual_list = explode( ',', $available_backups->manual );
284 } else {
285 $available_backups_manual_list = array();
286 }
287 // to do check date created: $api_response->backups->local.
288
289 if ( is_array( $available_backups_automatic_list ) ) {
290 foreach ( $available_backups_automatic_list as $backup_name ) {
291 $backup_time = substr( $backup_name, 0, 16 ); // get date time string in name.
292 $backup_time = strtotime( $backup_time );
293 if ( $backup_time > $lasttime_backup ) {
294 $lasttime_backup = $backup_time;
295 }
296 }
297 }
298
299 if ( is_array( $available_backups_manual_list ) ) {
300 foreach ( $available_backups_manual_list as $backup_name ) {
301 $backup_time = substr( $backup_name, 0, 16 ); // get date time string in name.
302 $backup_time = strtotime( $backup_time );
303 if ( $backup_time > $lasttime_backup ) {
304 $lasttime_backup = $backup_time;
305 }
306 }
307 }
308 } elseif ( 'cloudways' === $backup_api ) {
309 if ( isset( $available_backups->application_backup_exists ) && true === $available_backups->application_backup_exists ) {
310 $available_backups = $available_backups->backup_dates;
311 } else {
312 $available_backups = array();
313 }
314
315 if ( is_array( $available_backups ) ) {
316 foreach ( $available_backups as $backup ) {
317 $backup_time = strtotime( $backup );
318 if ( $backup_time > $lasttime_backup ) {
319 $lasttime_backup = $backup_time;
320 }
321 }
322 }
323 } elseif ( 'vultr' === $backup_api ) {
324 if ( isset( $available_backups->snapshots ) ) {
325 $available_snapshots_list = $available_backups->snapshots;
326 } else {
327 $available_snapshots_list = array();
328 }
329 if ( isset( $available_backups->backups ) ) {
330 $available_backups_list = $available_backups->backups;
331 } else {
332 $available_backups_list = array();
333 }
334 if ( is_array( $available_snapshots_list ) ) {
335 foreach ( $available_snapshots_list as $backup ) {
336 if ( is_object( $backup ) && ! empty( $backup->date_created ) ) {
337 $backup_time = strtotime( $backup->date_created );
338 if ( $backup_time > $lasttime_backup ) {
339 $lasttime_backup = $backup_time;
340 }
341 }
342 }
343 }
344 if ( is_array( $available_backups_list ) ) {
345 foreach ( $available_backups_list as $backup ) {
346 if ( is_object( $backup ) && ! empty( $backup->date_created ) ) {
347 $backup_time = strtotime( $backup->date_created );
348 if ( $backup_time > $lasttime_backup ) {
349 $lasttime_backup = $backup_time;
350 }
351 }
352 }
353 }
354 } elseif ( 'linode' === $backup_api ) {
355 // Automatic Backups.
356 if ( isset( $available_backups->automatic ) ) {
357 $available_backups_automatic_list = $available_backups->automatic;
358 } else {
359 $available_backups_automatic_list = array();
360 }
361 // Manual Backups.
362 if ( isset( $available_backups->snapshot ) ) {
363 $available_backups_snapshot_list = $available_backups->snapshot;
364 } else {
365 $available_backups_snapshot_list = array();
366 }
367 if ( is_array( $available_backups_automatic_list ) ) {
368 foreach ( $available_backups_automatic_list as $backup ) {
369 if ( is_object( $backup ) && ! empty( $backup->created ) ) {
370 $backup_time = strtotime( $backup->created );
371 if ( $backup_time > $lasttime_backup ) {
372 $lasttime_backup = $backup_time;
373 }
374 }
375 }
376 }
377 if ( is_array( $available_backups_snapshot_list ) ) {
378 foreach ( $available_backups_snapshot_list as $backup ) {
379 if ( is_object( $backup ) && ! empty( $backup->created ) ) {
380 $backup_time = strtotime( $backup->created );
381 if ( $backup_time > $lasttime_backup ) {
382 $lasttime_backup = $backup_time;
383 }
384 }
385 }
386 }
387 } elseif ( 'digitalocean' === $backup_api ) {
388 if ( isset( $available_backups->snapshots ) ) {
389 $available_backups = $available_backups->snapshots;
390 }
391 if ( is_array( $available_backups ) ) {
392 foreach ( $available_backups as $backup ) {
393 if ( is_object( $backup ) && ! empty( $backup->created_at ) ) {
394 $backup_time = strtotime( $backup->created_at );
395 if ( $backup_time > $lasttime_backup ) {
396 $lasttime_backup = $backup_time;
397 }
398 }
399 }
400 }
401 }
402
403 if ( empty( $lasttime_backup ) ) {
404 return;
405 }
406 $lastBackup = MainWP_DB::instance()->get_website_option( $site_id, 'primary_lasttime_backup' );
407
408 if ( ! empty( $lastBackup ) && $lastBackup > $lasttime_backup ) {
409 return;
410 }
411 self::log_debug( 'save backup time :: [site-id=' . $site_id . '] :: [backup time=' . gmdate( 'Y-m-d H:i:s', $lasttime_backup ) . '] :: [api-backups=' . $backup_api . ']' );
412 $site = new \stdClass();
413 $site->id = $site_id;
414 MainWP_DB::instance()->update_website_option( $site, 'primary_lasttime_backup', $lasttime_backup );
415 }
416
417 /**
418 * Show Info Messages
419 *
420 * Check whenther or not to show the MainWP Message.
421 *
422 * @param string $notice_id Notice ID.
423 *
424 * @return bool False if hidden, true to show.
425 */
426 public static function show_mainwp_message( $notice_id ) {
427 $status = get_user_option( 'mainwp_notice_saved_status' );
428 if ( ! is_array( $status ) ) {
429 $status = array();
430 }
431 if ( isset( $status[ $notice_id ] ) ) {
432 return false;
433 }
434 return true;
435 }
436
437
438 /**
439 * Method human_filesize()
440 *
441 * Convert to human readable file size format,
442 * (B|kB|MB|GB|TB|PB|EB|ZB|YB).
443 *
444 * @param mixed $bytes File in bytes.
445 * @param integer $decimals Number of decimals to output.
446 *
447 * @return string Human readable file size.
448 */
449 public static function human_filesize( $bytes, $decimals = 2 ) {
450 $size = array( 'B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' );
451 $factor = floor( ( strlen( $bytes ) - 1 ) / 3 );
452
453 return sprintf( "%.{$decimals}f", $bytes / pow( 1024, $factor ) ) . @$size[ $factor ]; //phpcs:ignore
454 }
455
456
457 /**
458 * Get Color Code
459 *
460 * Returns CSS class to use correct color for the element.
461 *
462 * @param int $value value.
463 *
464 * @return string CSS class.
465 */
466 public static function color_code( $value ) {
467 $color = '';
468 if ( 5 < $value ) {
469 $color = 'red';
470 } elseif ( 0 < $value && 5 >= $value ) {
471 $color = 'yellow';
472 } else {
473 $color = 'green';
474 }
475 return $color;
476 }
477
478 /**
479 * Method update_option()
480 *
481 * Update option.
482 *
483 * @param mixed $option_name Option name.
484 * @param mixed $option_value Option value.
485 *
486 * @return (boolean) False if value was not updated and true if value was updated.
487 */
488 public static function update_option( $option_name, $option_value ) {
489 $success = add_option( $option_name, $option_value, '', 'no' );
490
491 if ( ! $success ) {
492 $success = update_option( $option_name, $option_value );
493 }
494
495 return $success;
496 }
497
498
499 /**
500 * Method encrypt_api_keys
501 *
502 * @param string $data data.
503 * @param int $siteid site id.
504 * @param string $file_key file key.
505 * @param string $service_name service name.
506 *
507 * Encrypt data.
508 */
509 public function encrypt_api_keys( $data, $siteid = false, $file_key = false, $service_name = '' ) {
510 if ( is_string( $data ) ) {
511
512 $prefix = 'apibackups_';
513
514 if ( ! empty( $siteid ) ) {
515 $prefix = $prefix . intval( $siteid ) . '_';
516 }
517
518 if ( ! empty( $service_name ) && is_string( $service_name ) ) {
519 $prefix = $prefix . $service_name . '_';
520 }
521
522 $result = apply_filters( 'mainwp_encrypt_key_value', false, $data, $prefix, $file_key );
523
524 if ( is_array( $result ) && ! empty( $result['encrypted_val'] ) ) {
525 return $result;
526 }
527 }
528 return $data;
529 }
530
531 /**
532 * Method decrypt_api_keys
533 *
534 * @param mixed $encrypted_data encrypted data.
535 * @param string $def_val default data.
536 */
537 public function decrypt_api_keys( $encrypted_data, $def_val = '' ) {
538 if ( ! empty( $encrypted_data ) && is_array( $encrypted_data ) && ! empty( $encrypted_data['encrypted_val'] ) ) { // old format.
539 $result = apply_filters( 'mainwp_decrypt_key_value', false, $encrypted_data, $def_val );
540 if ( ! empty( $result ) && is_string( $result ) ) {
541 return $result;
542 }
543 }
544 return $def_val;
545 }
546
547 /**
548 * Method get_compatible_site_api_key
549 *
550 * Encrypt data.
551 *
552 * @param string $name name data.
553 * @param string $def_val default data.
554 */
555 public function get_api_key( $name, $def_val = '' ) {
556
557 $names = array(
558 'vultr',
559 'gridpane',
560 'linode',
561 'digitalocean',
562 'cloudways',
563 'cpanel',
564 'plesk',
565 );
566
567 if ( ! in_array( $name, $names ) ) {
568 return $def_val;
569 }
570
571 $encrypted = get_option( 'mainwp_api_backups_' . $name . '_api_key' );
572 if ( is_array( $encrypted ) && ! empty( $encrypted['file_key'] ) ) {
573 $key = $this->decrypt_api_keys( $encrypted );
574 if ( ! empty( $key ) && is_string( $key ) ) {
575 return $key;
576 }
577 }
578 return $def_val;
579 }
580
581 /**
582 * Method update_compatible_site_api_key
583 *
584 * Encrypt data.
585 *
586 * @param string $name name data.
587 * @param string $value value data.
588 */
589 public function update_api_key( $name, $value ) {
590
591 $names = array(
592 'vultr',
593 'gridpane',
594 'linode',
595 'digitalocean',
596 'cloudways',
597 'cpanel',
598 'plesk',
599 );
600
601 if ( ! in_array( $name, $names ) ) {
602 return false;
603 }
604
605 $opt_name = 'mainwp_api_backups_' . $name . '_api_key';
606 $current = get_option( $opt_name );
607 $key_file = '';
608
609 if ( is_array( $current ) && ! empty( $current['file_key'] ) ) {
610 $key_file = $current['file_key'];
611 }
612
613 if ( empty( $value ) ) {
614 delete_option( $opt_name );
615 if ( ! empty( $key_file ) ) {
616 do_action( 'mainwp_delete_key_file', $key_file );
617 }
618 return true;
619 }
620
621 $encrypted = $this->encrypt_api_keys( $value, false, $key_file, $name );
622 if ( is_array( $encrypted ) && ! empty( $encrypted['file_key'] ) ) {
623 return update_option( $opt_name, $encrypted );
624 }
625
626 return false;
627 }
628
629 /**
630 * Method get child site key.
631 *
632 * Encrypt data.
633 *
634 * @param int $website_id name data.
635 * @param string $name name data.
636 * @param string $def_val default data.
637 */
638 public function get_child_api_key( $website_id, $name, $def_val = '' ) {
639
640 $names = array(
641 'vultr',
642 'gridpane',
643 'linode',
644 'digitalocean',
645 'cloudways',
646 'cpanel',
647 'plesk',
648 );
649
650 if ( ! in_array( $name, $names ) ) {
651 return $def_val;
652 }
653
654 $opt_name = 'mainwp_api_backups_' . $name . '_api_key';
655 $opt_encoded = Api_Backups_Helper::get_website_options( $website_id, array( $opt_name ) );
656 if ( array_key_exists( $opt_name, $opt_encoded ) ) {
657 $encrypted = json_decode( $opt_encoded[ $opt_name ], true );
658 } else {
659 $encrypted = '';
660 }
661
662 if ( is_array( $encrypted ) && ! empty( $encrypted['file_key'] ) ) {
663 $key = $this->decrypt_api_keys( $encrypted );
664
665 if ( ! empty( $key ) && is_string( $key ) ) {
666 return $key;
667 }
668 }
669 return $def_val;
670 }
671
672 /**
673 * Method update child api key.
674 *
675 * Encrypt data.
676 *
677 * @param int $website_id name data.
678 * @param string $name name data.
679 * @param string $value value data.
680 */
681 public function update_child_api_key( $website_id, $name, $value ) {
682
683 $names = array(
684 'vultr',
685 'gridpane',
686 'linode',
687 'digitalocean',
688 'cloudways',
689 'cpanel',
690 'plesk',
691 );
692
693 if ( ! in_array( $name, $names ) ) {
694 return false;
695 }
696
697 $opt_name = 'mainwp_api_backups_' . $name . '_api_key';
698 $opt_encoded = Api_Backups_Helper::get_website_options( $website_id, array( $opt_name ) );
699 if ( array_key_exists( $opt_name, $opt_encoded ) ) {
700 $current = json_decode( $opt_encoded[ $opt_name ], true );
701 } else {
702 $current = '';
703 }
704 $key_file = '';
705 if ( is_array( $current ) && ! empty( $current['file_key'] ) ) {
706 $key_file = $current['file_key'];
707 }
708
709 // If post $value is empty, delete option.
710 if ( empty( $value ) ) {
711 delete_option( $opt_name );
712 if ( ! empty( $key_file ) ) {
713 do_action( 'mainwp_delete_key_file', $key_file );
714 }
715 return true;
716 }
717
718 $encrypted = wp_json_encode( $this->encrypt_api_keys( $value, $website_id, $key_file, $name ) );
719
720 if ( ! empty( $encrypted ) ) {
721 return Api_Backups_Helper::update_website_option( $website_id, $opt_name, $encrypted );
722 }
723
724 return false;
725 }
726 }
727