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

745 lines 24.6 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 { //phpcs:ignore -- NOSONAR - multi methods.
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 === static::$instance ) {
38 static::$instance = new self();
39 }
40 return static::$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 ) { //phpcs:ignore -- NOSONAR - complex.
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 static::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 -- NOSONAR - complex method.
205
206 static::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 ( 'kinsta' === $backup_api ) {
236 $available_backups = is_array( $available_backups ) && ! empty( $available_backups ) ? $available_backups : array();
237 if ( is_array( $available_backups ) ) {
238 foreach ( $available_backups as $backup ) {
239 if ( is_object( $backup ) && ! empty( $backup->created_at ) ) {
240 $milliseconds = $backup->created_at;
241 $epoch = $milliseconds / 1000;
242 $backup_time = strtotime( $epoch );
243 if ( $backup_time > $lasttime_backup ) {
244 $lasttime_backup = $backup_time;
245 }
246 }
247 }
248 }
249 } elseif ( 'cpanel_auto' === $backup_api ) {
250 if ( is_object( $available_backups ) && isset( $available_backups->data ) ) {
251 $available_backups_automatic_list = $available_backups->data;
252 } else {
253 $available_backups_automatic_list = array();
254 }
255
256 if ( is_array( $available_backups_automatic_list ) ) {
257 foreach ( $available_backups_automatic_list as $backup ) {
258 if ( is_object( $backup ) && ! empty( $backup->backupID ) ) {
259 $backup_time = strtotime( $backup->backupID );
260 if ( $backup_time > $lasttime_backup ) {
261 $lasttime_backup = $backup_time;
262 }
263 }
264 }
265 }
266 } elseif ( 'cpanel_manual' === $backup_api ) {
267 $available_backups_manual_list = $available_backups;
268 if ( is_array( $available_backups_manual_list ) ) {
269 foreach ( $available_backups_manual_list as $backup ) {
270 if ( is_object( $backup ) && ! empty( $backup->timestamp ) ) {
271 $backup_time = $backup->timestamp;
272 if ( $backup_time > $lasttime_backup ) {
273 $lasttime_backup = $backup_time;
274 }
275 }
276 }
277 }
278 } elseif ( 'cpanel_wp_toolkit' === $backup_api ) {
279 $available_backups_manual_list = $available_backups;
280 if ( is_array( $available_backups_manual_list ) ) {
281 foreach ( $available_backups_manual_list as $backup ) {
282 if ( is_object( $backup ) && ! empty( $backup->value->createdAt->value ) ) {
283 $backup_time = strtotime( $backup->value->createdAt->value );
284 if ( $backup_time > $lasttime_backup ) {
285 $lasttime_backup = $backup_time;
286 }
287 }
288 }
289 }
290 } elseif ( 'gridpane' === $backup_api ) {
291 if ( isset( $available_backups->automatic ) ) {
292 $available_backups_automatic_list = explode( ',', $available_backups->automatic );
293 } else {
294 $available_backups_automatic_list = array();
295 }
296 if ( isset( $available_backups->manual ) ) {
297 $available_backups_manual_list = explode( ',', $available_backups->manual );
298 } else {
299 $available_backups_manual_list = array();
300 }
301 // to do check date created: $api_response->backups->local.
302
303 if ( is_array( $available_backups_automatic_list ) ) {
304 foreach ( $available_backups_automatic_list as $backup_name ) {
305 $backup_time = substr( $backup_name, 0, 16 ); // get date time string in name.
306 $backup_time = strtotime( $backup_time );
307 if ( $backup_time > $lasttime_backup ) {
308 $lasttime_backup = $backup_time;
309 }
310 }
311 }
312
313 if ( is_array( $available_backups_manual_list ) ) {
314 foreach ( $available_backups_manual_list as $backup_name ) {
315 $backup_time = substr( $backup_name, 0, 16 ); // get date time string in name.
316 $backup_time = strtotime( $backup_time );
317 if ( $backup_time > $lasttime_backup ) {
318 $lasttime_backup = $backup_time;
319 }
320 }
321 }
322 } elseif ( 'cloudways' === $backup_api ) {
323 if ( isset( $available_backups->application_backup_exists ) && true === $available_backups->application_backup_exists ) {
324 $available_backups = $available_backups->backup_dates;
325 } else {
326 $available_backups = array();
327 }
328
329 if ( is_array( $available_backups ) ) {
330 foreach ( $available_backups as $backup ) {
331 $backup_time = strtotime( $backup );
332 if ( $backup_time > $lasttime_backup ) {
333 $lasttime_backup = $backup_time;
334 }
335 }
336 }
337 } elseif ( 'vultr' === $backup_api ) {
338 if ( isset( $available_backups->snapshots ) ) {
339 $available_snapshots_list = $available_backups->snapshots;
340 } else {
341 $available_snapshots_list = array();
342 }
343 if ( isset( $available_backups->backups ) ) {
344 $available_backups_list = $available_backups->backups;
345 } else {
346 $available_backups_list = array();
347 }
348 if ( is_array( $available_snapshots_list ) ) {
349 foreach ( $available_snapshots_list as $backup ) {
350 if ( is_object( $backup ) && ! empty( $backup->date_created ) ) {
351 $backup_time = strtotime( $backup->date_created );
352 if ( $backup_time > $lasttime_backup ) {
353 $lasttime_backup = $backup_time;
354 }
355 }
356 }
357 }
358 if ( is_array( $available_backups_list ) ) {
359 foreach ( $available_backups_list as $backup ) {
360 if ( is_object( $backup ) && ! empty( $backup->date_created ) ) {
361 $backup_time = strtotime( $backup->date_created );
362 if ( $backup_time > $lasttime_backup ) {
363 $lasttime_backup = $backup_time;
364 }
365 }
366 }
367 }
368 } elseif ( 'linode' === $backup_api ) {
369 // Automatic Backups.
370 if ( isset( $available_backups->automatic ) ) {
371 $available_backups_automatic_list = $available_backups->automatic;
372 } else {
373 $available_backups_automatic_list = array();
374 }
375 // Manual Backups.
376 if ( isset( $available_backups->snapshot ) ) {
377 $available_backups_snapshot_list = $available_backups->snapshot;
378 } else {
379 $available_backups_snapshot_list = array();
380 }
381 if ( is_array( $available_backups_automatic_list ) ) {
382 foreach ( $available_backups_automatic_list as $backup ) {
383 if ( is_object( $backup ) && ! empty( $backup->created ) ) {
384 $backup_time = strtotime( $backup->created );
385 if ( $backup_time > $lasttime_backup ) {
386 $lasttime_backup = $backup_time;
387 }
388 }
389 }
390 }
391 if ( is_array( $available_backups_snapshot_list ) ) {
392 foreach ( $available_backups_snapshot_list as $backup ) {
393 if ( is_object( $backup ) && ! empty( $backup->created ) ) {
394 $backup_time = strtotime( $backup->created );
395 if ( $backup_time > $lasttime_backup ) {
396 $lasttime_backup = $backup_time;
397 }
398 }
399 }
400 }
401 } elseif ( 'digitalocean' === $backup_api ) {
402 if ( isset( $available_backups->snapshots ) ) {
403 $available_backups = $available_backups->snapshots;
404 }
405 if ( is_array( $available_backups ) ) {
406 foreach ( $available_backups as $backup ) {
407 if ( is_object( $backup ) && ! empty( $backup->created_at ) ) {
408 $backup_time = strtotime( $backup->created_at );
409 if ( $backup_time > $lasttime_backup ) {
410 $lasttime_backup = $backup_time;
411 }
412 }
413 }
414 }
415 }
416
417 if ( empty( $lasttime_backup ) ) {
418 return;
419 }
420 $lastBackup = MainWP_DB::instance()->get_website_option( $site_id, 'primary_lasttime_backup' );
421
422 if ( ! empty( $lastBackup ) && $lastBackup > $lasttime_backup ) {
423 return;
424 }
425 static::log_debug( 'save backup time :: [site-id=' . $site_id . '] :: [backup time=' . gmdate( 'Y-m-d H:i:s', $lasttime_backup ) . '] :: [api-backups=' . $backup_api . ']' );
426 $site = new \stdClass();
427 $site->id = $site_id;
428 MainWP_DB::instance()->update_website_option( $site, 'primary_lasttime_backup', $lasttime_backup );
429 }
430
431 /**
432 * Show Info Messages
433 *
434 * Check whenther or not to show the MainWP Message.
435 *
436 * @param string $notice_id Notice ID.
437 *
438 * @return bool False if hidden, true to show.
439 */
440 public static function show_mainwp_message( $notice_id ) {
441 $status = get_user_option( 'mainwp_notice_saved_status' );
442 if ( ! is_array( $status ) ) {
443 $status = array();
444 }
445 if ( isset( $status[ $notice_id ] ) ) {
446 return false;
447 }
448 return true;
449 }
450
451
452 /**
453 * Method human_filesize()
454 *
455 * Convert to human readable file size format,
456 * (B|kB|MB|GB|TB|PB|EB|ZB|YB).
457 *
458 * @param mixed $bytes File in bytes.
459 * @param integer $decimals Number of decimals to output.
460 *
461 * @return string Human readable file size.
462 */
463 public static function human_filesize( $bytes, $decimals = 2 ) {
464 $size = array( 'B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' );
465 $factor = floor( ( strlen( $bytes ) - 1 ) / 3 );
466
467 return sprintf( "%.{$decimals}f", $bytes / pow( 1024, $factor ) ) . @$size[ $factor ]; //phpcs:ignore
468 }
469
470
471 /**
472 * Get Color Code
473 *
474 * Returns CSS class to use correct color for the element.
475 *
476 * @param int $value value.
477 *
478 * @return string CSS class.
479 */
480 public static function color_code( $value ) {
481 $color = '';
482 if ( 5 < $value ) {
483 $color = 'red';
484 } elseif ( 0 < $value && 5 >= $value ) {
485 $color = 'yellow';
486 } else {
487 $color = 'green';
488 }
489 return $color;
490 }
491
492 /**
493 * Method update_option()
494 *
495 * Update option.
496 *
497 * @param mixed $option_name Option name.
498 * @param mixed $option_value Option value.
499 *
500 * @return (boolean) False if value was not updated and true if value was updated.
501 */
502 public static function update_option( $option_name, $option_value ) {
503 $success = add_option( $option_name, $option_value, '', 'no' );
504
505 if ( ! $success ) {
506 $success = update_option( $option_name, $option_value );
507 }
508
509 return $success;
510 }
511
512
513 /**
514 * Method encrypt_api_keys
515 *
516 * @param string $data data.
517 * @param int $siteid site id.
518 * @param string $file_key file key.
519 * @param string $service_name service name.
520 *
521 * Encrypt data.
522 */
523 public function encrypt_api_keys( $data, $siteid = false, $file_key = false, $service_name = '' ) {
524 if ( is_string( $data ) ) {
525
526 $prefix = 'apibackups_';
527
528 if ( ! empty( $siteid ) ) {
529 $prefix = $prefix . intval( $siteid ) . '_';
530 }
531
532 if ( ! empty( $service_name ) && is_string( $service_name ) ) {
533 $prefix = $prefix . $service_name . '_';
534 }
535
536 $result = apply_filters( 'mainwp_encrypt_key_value', false, $data, $prefix, $file_key );
537
538 if ( is_array( $result ) && ! empty( $result['encrypted_val'] ) ) {
539 return $result;
540 }
541 }
542 return $data;
543 }
544
545 /**
546 * Method decrypt_api_keys
547 *
548 * @param mixed $encrypted_data encrypted data.
549 * @param string $def_val default data.
550 */
551 public function decrypt_api_keys( $encrypted_data, $def_val = '' ) {
552 if ( ! empty( $encrypted_data ) && is_array( $encrypted_data ) && ! empty( $encrypted_data['encrypted_val'] ) ) { // old format.
553 $result = apply_filters( 'mainwp_decrypt_key_value', false, $encrypted_data, $def_val );
554 if ( ! empty( $result ) && is_string( $result ) ) {
555 return $result;
556 }
557 }
558 return $def_val;
559 }
560
561 /**
562 * Method get_compatible_site_api_key
563 *
564 * Encrypt data.
565 *
566 * @param string $name name data.
567 * @param string $def_val default data.
568 */
569 public function get_api_key( $name, $def_val = '' ) {
570
571 $names = array(
572 'vultr',
573 'gridpane',
574 'linode',
575 'digitalocean',
576 'cloudways',
577 'cpanel',
578 'plesk',
579 'kinsta',
580 );
581
582 if ( ! in_array( $name, $names ) ) {
583 return $def_val;
584 }
585
586 $encrypted = get_option( 'mainwp_api_backups_' . $name . '_api_key' );
587 if ( is_array( $encrypted ) && ! empty( $encrypted['file_key'] ) ) {
588 $key = $this->decrypt_api_keys( $encrypted );
589 if ( ! empty( $key ) && is_string( $key ) ) {
590 return $key;
591 }
592 }
593 return $def_val;
594 }
595
596 /**
597 * Method update_compatible_site_api_key
598 *
599 * Encrypt data.
600 *
601 * @param string $name name data.
602 * @param string $value value data.
603 */
604 public function update_api_key( $name, $value ) {
605
606 $names = array(
607 'vultr',
608 'gridpane',
609 'linode',
610 'digitalocean',
611 'cloudways',
612 'cpanel',
613 'plesk',
614 'kinsta',
615 );
616
617 if ( ! in_array( $name, $names ) ) {
618 return false;
619 }
620
621 $opt_name = 'mainwp_api_backups_' . $name . '_api_key';
622 $current = get_option( $opt_name );
623 $key_file = '';
624
625 if ( is_array( $current ) && ! empty( $current['file_key'] ) ) {
626 $key_file = $current['file_key'];
627 }
628
629 if ( empty( $value ) ) {
630 delete_option( $opt_name );
631 if ( ! empty( $key_file ) ) {
632 do_action( 'mainwp_delete_key_file', $key_file );
633 }
634 return true;
635 }
636
637 $encrypted = $this->encrypt_api_keys( $value, false, $key_file, $name );
638 if ( is_array( $encrypted ) && ! empty( $encrypted['file_key'] ) ) {
639 return update_option( $opt_name, $encrypted );
640 }
641
642 return false;
643 }
644
645 /**
646 * Method get child site key.
647 *
648 * Encrypt data.
649 *
650 * @param int $website_id name data.
651 * @param string $name name data.
652 * @param string $def_val default data.
653 */
654 public function get_child_api_key( $website_id, $name, $def_val = '' ) {
655
656 $names = array(
657 'vultr',
658 'gridpane',
659 'linode',
660 'digitalocean',
661 'cloudways',
662 'cpanel',
663 'plesk',
664 'kinsta',
665 );
666
667 if ( ! in_array( $name, $names ) ) {
668 return $def_val;
669 }
670
671 $opt_name = 'mainwp_api_backups_' . $name . '_api_key';
672 $opt_encoded = Api_Backups_Helper::get_website_options( $website_id, array( $opt_name ) );
673 if ( array_key_exists( $opt_name, $opt_encoded ) ) {
674 $encrypted = json_decode( $opt_encoded[ $opt_name ], true );
675 } else {
676 $encrypted = '';
677 }
678
679 if ( is_array( $encrypted ) && ! empty( $encrypted['file_key'] ) ) {
680 $key = $this->decrypt_api_keys( $encrypted );
681
682 if ( ! empty( $key ) && is_string( $key ) ) {
683 return $key;
684 }
685 }
686 return $def_val;
687 }
688
689 /**
690 * Method update child api key.
691 *
692 * Encrypt data.
693 *
694 * @param int $website_id name data.
695 * @param string $name name data.
696 * @param string $value value data.
697 */
698 public function update_child_api_key( $website_id, $name, $value ) {
699
700 $names = array(
701 'vultr',
702 'gridpane',
703 'linode',
704 'digitalocean',
705 'cloudways',
706 'cpanel',
707 'plesk',
708 'kinsta',
709 );
710
711 if ( ! in_array( $name, $names ) ) {
712 return false;
713 }
714
715 $opt_name = 'mainwp_api_backups_' . $name . '_api_key';
716 $opt_encoded = Api_Backups_Helper::get_website_options( $website_id, array( $opt_name ) );
717 if ( array_key_exists( $opt_name, $opt_encoded ) ) {
718 $current = json_decode( $opt_encoded[ $opt_name ], true );
719 } else {
720 $current = '';
721 }
722 $key_file = '';
723 if ( is_array( $current ) && ! empty( $current['file_key'] ) ) {
724 $key_file = $current['file_key'];
725 }
726
727 // If post $value is empty, delete option.
728 if ( empty( $value ) ) {
729 delete_option( $opt_name );
730 if ( ! empty( $key_file ) ) {
731 do_action( 'mainwp_delete_key_file', $key_file );
732 }
733 return true;
734 }
735
736 $encrypted = wp_json_encode( $this->encrypt_api_keys( $value, $website_id, $key_file, $name ) );
737
738 if ( ! empty( $encrypted ) ) {
739 return Api_Backups_Helper::update_website_option( $website_id, $opt_name, $encrypted );
740 }
741
742 return false;
743 }
744 }
745