Skip to content

Commit 238bddd

Browse files
committed
* Added: Support for the new API Backups providers - cPanel and Plesk
* Updated: Updated the phpSecLib library to enhance security and performance.
1 parent ce41bc5 commit 238bddd

40 files changed

Lines changed: 663 additions & 225 deletions
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
/**
3+
* MainWP Child Site Api Backups
4+
*
5+
* Manages MainWP API Backups child site actions when needed.
6+
*
7+
* @package MainWP\Child
8+
*/
9+
10+
namespace MainWP\Child;
11+
12+
/**
13+
* Class MainWP_Child_Api_Backups
14+
*
15+
* This class handles all the MainWP API Backups child site actions when needed.
16+
*
17+
* @package MainWP\Child
18+
*/
19+
class MainWP_Child_Api_Backups {
20+
21+
/**
22+
* Public variable to state if supported plugin is installed on the child site.
23+
*
24+
* @var bool If supported plugin is installed, return true, if not, return false.
25+
*/
26+
public $is_plugin_installed = false;
27+
28+
/**
29+
* Public static variable to hold the single instance of the class.
30+
*
31+
* @var mixed Default null
32+
*/
33+
protected static $instance = null;
34+
35+
/**
36+
* Method instance()
37+
*
38+
* Create a public static instance.
39+
*
40+
* @return mixed Class instance.
41+
*/
42+
public static function instance() {
43+
if ( null === self::$instance ) {
44+
self::$instance = new self();
45+
}
46+
47+
return self::$instance;
48+
}
49+
50+
/**
51+
* MainWP_Child_Api_Backups constructor.
52+
*
53+
* Run any time class is called.
54+
*/
55+
public function __construct() {
56+
// Constructor.
57+
}
58+
59+
/**
60+
* Create a backup of the database for the given child site.
61+
*
62+
* @return void
63+
*/
64+
public function api_backups_mysqldump() {
65+
66+
// WordPress DB credentials.
67+
$database_name = DB_NAME;
68+
$user = DB_USER;
69+
$pass = DB_PASSWORD;
70+
71+
// Remove ":" & all numbers from "Localhost:3306".
72+
$host = str_replace( ':', '', preg_replace( '/[0-9]+/', '', DB_HOST ) );
73+
74+
// Get Site URL.
75+
$site_url = str_replace( '/', '.', preg_replace( '#^https?://#i', '', get_bloginfo( 'url' ) ) );
76+
77+
// Create a timestamp.
78+
$current_date_time = current_datetime();
79+
$current_date_time = $current_date_time->format( 'm-d-Y_H.i.s.A' );
80+
81+
// Build the uploads directory.
82+
$wp_get_upload_dir = wp_get_upload_dir();
83+
$wp_upload_dir = $wp_get_upload_dir['basedir'] . '/mainwp/api_db_backups/';
84+
85+
// Build the full path to the backup file.
86+
$gzip_full_path = $wp_upload_dir . $database_name . '_' . $site_url . '_' . $current_date_time . '.sql.gz';
87+
88+
// Create the directory if it doesn't exist.
89+
if ( ! file_exists( $wp_upload_dir ) ) { //phpcs:ignore
90+
mkdir( $wp_upload_dir, 0755, true ); //phpcs:ignore
91+
}
92+
93+
if ( function_exists( 'exec' ) ) {
94+
// Create the backup file. hide from logs ( password ).
95+
exec( "mysqldump --user={$user} --password='{$pass}' --host={$host} {$database_name} | gzip > {$gzip_full_path}", $output, $result ); //phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.system_calls_exec
96+
}
97+
98+
// Check if the backup was successful.
99+
if ( 0 === $result ) {
100+
// Success.
101+
MainWP_Helper::write(
102+
array(
103+
'result' => 'GOOD',
104+
'output' => $output,
105+
'res' => $result,
106+
)
107+
);
108+
} else {
109+
// Error.
110+
MainWP_Helper::write(
111+
array(
112+
'result' => 'ERROR',
113+
'output' => $output,
114+
'res' => $result,
115+
)
116+
);
117+
}
118+
}
119+
}

class/class-mainwp-child-branding.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,6 @@ public function admin_menu() {
583583
if ( 'T' === $opts['show_support'] ) {
584584
$title = $opts['contact_label'];
585585
if ( isset( $extra_setting['show_button_in'] ) && ( 2 === (int) $extra_setting['show_button_in'] || 3 === (int) $extra_setting['show_button_in'] ) ) {
586-
$title = $opts['contact_label'];
587586
add_menu_page(
588587
$title,
589588
$title,
@@ -600,9 +599,9 @@ public function admin_menu() {
600599

601600
if ( isset( $extra_setting['show_button_in'] ) && ( 1 === (int) $extra_setting['show_button_in'] || 3 === (int) $extra_setting['show_button_in'] ) ) {
602601
add_submenu_page(
603-
null,
602+
'admin.php',
603+
$title,
604604
$title,
605-
$opts['contact_label'],
606605
'read',
607606
'ContactSupport',
608607
array(

class/class-mainwp-child-cache-purge.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -834,15 +834,15 @@ public function sitegrounds_optimizer_auto_purge_cache() {
834834
public function cloudflair_auto_purge_cache() {
835835

836836
// Credentials for Cloudflare.
837-
$cust_email = get_option( 'mainwp_cloudflair_email' );
838-
$cust_xauth = get_option( 'mainwp_child_cloudflair_key' );
837+
$cust_email = get_option( 'mainwp_cloudflair_email', '' );
838+
$cust_xauth = get_option( 'mainwp_child_cloudflair_key', '' );
839839
if ( ! empty( $cust_xauth ) ) {
840840
$cust_xauth = MainWP_Child_Keys_Manager::instance()->decrypt_string( $cust_xauth );
841841
}
842842
$cust_domain = trim( str_replace( array( 'http://', 'https://', 'www.' ), '', get_option( 'siteurl' ) ), '/' );
843843

844844
// Check if we have all the required data.
845-
if ( '' === $cust_email || '' === $cust_xauth || '' === $cust_domain ) {
845+
if ( '' === $cust_email || '' === $cust_xauth ) {
846846
return;
847847
}
848848

@@ -1028,18 +1028,15 @@ public function record_results( $information ) {
10281028
* @return string The url without subdomains (if any).
10291029
*/
10301030
public function strip_subdomains( $url ) {
1031-
1032-
// credits to gavingmiller for maintaining this list.
1033-
$second_level_domains = wp_remote_get( 'https://raw.githubusercontent.com/gavingmiller/second-level-domains/master/SLDs.csv' );
1034-
1035-
// presume sld first ...
1036-
$possible_sld = implode( '.', array_slice( explode( '.', $url ), -2 ) );
1037-
1038-
// and then verify it.
1039-
if ( strpos( $second_level_domains, $possible_sld ) ) {
1040-
return implode( '.', array_slice( explode( '.', $url ), -3 ) );
1031+
$parts = explode( '/', $url );
1032+
$url_first = $parts[0]; // get first part.
1033+
$count = count( explode( '.', $url_first ) );
1034+
$domain = '';
1035+
if ( 4 <= $count ) {
1036+
$domain = implode( '.', array_slice( explode( '.', $url_first ), -3 ) );
10411037
} else {
1042-
return implode( '.', array_slice( explode( '.', $url ), -2 ) );
1038+
$domain = implode( '.', array_slice( explode( '.', $url_first ), -2 ) );
10431039
}
1040+
return $domain;
10441041
}
10451042
}

class/class-mainwp-child-callable.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class MainWP_Child_Callable {
9898
'jetpack_scan' => 'jetpack_scan',
9999
'delete_actions' => 'delete_actions',
100100
'verify_action' => 'verify_action',
101-
101+
'api_backups_mysqldump' => 'api_backups_mysqldump',
102102
);
103103

104104
/**
@@ -1090,4 +1090,16 @@ public function deactivate() {
10901090
$information['deactivated'] = true;
10911091
MainWP_Helper::write( $information );
10921092
}
1093+
1094+
/**
1095+
* Method api_backups_mysqldump()
1096+
*
1097+
* Fire off the action() function.
1098+
*
1099+
* @uses MainWP_Child_Api_Backups::action()
1100+
* @used-by \MainWP\Extensions\ApiBackups\MainWP_API_Backups_3rd_Party::api_backups_mysqldump()
1101+
*/
1102+
public function api_backups_mysqldump() {
1103+
MainWP_Child_Api_Backups::instance()->api_backups_mysqldump();
1104+
}
10931105
}

class/class-mainwp-child-ithemes-security.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,12 @@ public function save_settings() {
461461
foreach ( $settings as $key => $val ) {
462462
$current_settings[$key] = $val;
463463
}
464+
if('two-factor' === $module ){
465+
$active = \ITSEC_Modules::is_active( 'two-factor' );
466+
if(!$active){
467+
\ITSEC_Modules::activate( 'two-factor' );
468+
}
469+
}
464470
\ITSEC_Modules::set_settings( $module, $current_settings );
465471
$updated = true;
466472
}

class/class-mainwp-child-stats.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,11 @@ public function get_site_stats( $information = array(), $exit_done = true ) {
272272
}
273273

274274
if ( ! empty( $_POST['primaryBackup'] ) ) {
275-
$primary_bk = ! empty( $_POST['primaryBackup'] ) ? sanitize_text_field( wp_unslash( $_POST['primaryBackup'] ) ) : '';
276-
$information['primaryLasttimeBackup'] = MainWP_Utility::get_lasttime_backup( $primary_bk );
275+
$primary_bk = ! empty( $_POST['primaryBackup'] ) ? sanitize_text_field( wp_unslash( $_POST['primaryBackup'] ) ) : '';
276+
$last_time = MainWP_Utility::get_lasttime_backup( $primary_bk );
277+
if ( false !== $last_time ) {
278+
$information['primaryLasttimeBackup'] = $last_time; // to fix overwrite other last time primary backup.
279+
}
277280
}
278281

279282
$last_post = wp_get_recent_posts( array( 'numberposts' => absint( '1' ) ) );

class/class-mainwp-child.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class MainWP_Child {
3333
*
3434
* @var string MainWP Child plugin version.
3535
*/
36-
public static $version = '4.6';
36+
public static $version = '5.0';
3737

3838
/**
3939
* Private variable containing the latest MainWP Child update version.
@@ -96,8 +96,13 @@ public function __construct( $plugin_file ) {
9696
add_action( 'core_upgrade_preamble', array( MainWP_Child_Updates::get_instance(), 'detect_premium_themesplugins_updates' ) );
9797

9898
MainWP_Pages::get_instance()->init();
99+
100+
// Initiate MainWP Cache Control class.
99101
MainWP_Child_Cache_Purge::instance();
100102

103+
// Initiate MainWP Child API Backups class.
104+
MainWP_Child_Api_Backups::instance();
105+
101106
if ( is_admin() ) {
102107
MainWP_Helper::update_option( 'mainwp_child_plugin_version', self::$version, 'yes' );
103108
}

class/class-mainwp-clone.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@ public static function upload_mimes( $mime_types = array() ) {
215215
* @uses \MainWP\Child\MainWP_Connect::is_valid_auth()
216216
* @uses \MainWP\Child\MainWP_Helper::get_mainwp_dir()
217217
* @uses \MainWP\Child\MainWP_Helper::write()
218-
* @uses \MainWP\Child\MainWP_Utility::upload_file()
219218
*/
220219
public function request_clone_funct() { // phpcs:ignore -- Current complexity is the only way to achieve desired results, pull request solutions appreciated.
221220
// phpcs:disable WordPress.Security.NonceVerification
@@ -234,7 +233,7 @@ public function request_clone_funct() { // phpcs:ignore -- Current complexity is
234233
if ( 'dl' === $cloneFunc ) {
235234
$f = isset( $_REQUEST['f'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['f'] ) ) : '';
236235
if ( ! empty( $f ) ) {
237-
MainWP_Utility::instance()->upload_file( sanitize_text_field( wp_unslash( $_REQUEST['f'] ) ) );
236+
MainWP_Utility::instance()->upload_file_backup( sanitize_text_field( wp_unslash( $_REQUEST['f'] ) ) );
238237
}
239238
exit;
240239
} elseif ( 'deleteCloneBackup' === $cloneFunc ) {

0 commit comments

Comments
 (0)