| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Restore Database and file class |
| 5 |
* |
| 6 |
* @version 1.0 |
| 7 |
*/ |
| 8 |
class Wpbp_Restore { |
| 9 |
/** |
| 10 |
* The path where the backup file is stored |
| 11 |
* |
| 12 |
* @string |
| 13 |
* @access private |
| 14 |
*/ |
| 15 |
private $path = ''; |
| 16 |
|
| 17 |
/** |
| 18 |
* The backup type, must be either complete, file or database |
| 19 |
* |
| 20 |
* @string |
| 21 |
* @access private |
| 22 |
*/ |
| 23 |
private $type = ''; |
| 24 |
|
| 25 |
function __construct() { |
| 26 |
|
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
public function start( $id ) { |
| 31 |
$options = get_option('wp_db_backup_backups'); |
| 32 |
$this->type = $options[$id]['type']; |
| 33 |
$this->path = $options[$id]['dir']; |
| 34 |
error_log("Restore Backup"); |
| 35 |
error_log($this->type); |
| 36 |
error_log($this->path); |
| 37 |
$this->restore(); |
| 38 |
} |
| 39 |
|
| 40 |
public function restore() { |
| 41 |
if ( ! class_exists( 'PclZip' ) ){ |
| 42 |
require ABSPATH . 'wp-admin/includes/class-pclzip.php'; |
| 43 |
} |
| 44 |
|
| 45 |
if ( $this->type == 'Complete' || $this->type == 'complete' ){ |
| 46 |
$this->restore_complete(); |
| 47 |
} |
| 48 |
|
| 49 |
if ( $this->type == 'Database' || $this->type == 'database' ){ |
| 50 |
$this->restore_database(); |
| 51 |
} |
| 52 |
|
| 53 |
if ( $this->type == 'File' || $this->type == 'file' ){ |
| 54 |
$this->restore_files(); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
public function restore_complete() { |
| 59 |
error_log("Inside restore_complete"); |
| 60 |
$filename = basename( $this->path, '.zip' ) . '.sql'; |
| 61 |
$file_path = ABSPATH . $filename; |
| 62 |
|
| 63 |
$this->restore_files(); |
| 64 |
|
| 65 |
$this->restore_database( $file_path ); |
| 66 |
} |
| 67 |
|
| 68 |
public function restore_database( $file = null ) { |
| 69 |
error_log("Inside restore_database"); |
| 70 |
global $wpdb; |
| 71 |
|
| 72 |
if ( ! $file ) { |
| 73 |
|
| 74 |
$archive = new PclZip( $this->path ); |
| 75 |
|
| 76 |
$filename = basename( $this->path, '.zip' ) . '.sql'; |
| 77 |
$path_info = wp_upload_dir(); |
| 78 |
$dir = $path_info['basedir'].'/'.WPDB_BACKUPS_DIR.'/'; |
| 79 |
$file_path = $dir .$filename; |
| 80 |
|
| 81 |
if ( ! $archive->extract( PCLZIP_OPT_PATH, $dir ) ){ |
| 82 |
wp_die( esc_html__('Unable to extract zip file. Please check that zlib php extension is enabled.','wpdbbkp').'<button onclick="history.go(-1);">'.esc_html__('Go Back','wpdbbkp').'</button>', esc_html__('ZIP Error','wpdbbkp') ); |
| 83 |
} |
| 84 |
} else { |
| 85 |
$file_path = $file; |
| 86 |
} |
| 87 |
|
| 88 |
$database_file = $file_path ; |
| 89 |
$database_name=$this->wp_backup_get_config_db_name(); |
| 90 |
$database_user=$this->wp_backup_get_config_data('DB_USER'); |
| 91 |
$datadase_password=$this->wp_backup_get_config_data('DB_PASSWORD'); |
| 92 |
$database_host=$this->wp_backup_get_config_data('DB_HOST'); |
| 93 |
|
| 94 |
ini_set("max_execution_time", "5000"); |
| 95 |
ini_set("max_input_time", "5000"); |
| 96 |
ini_set('memory_limit', '1000M'); |
| 97 |
set_time_limit(0); |
| 98 |
ignore_user_abort(true); |
| 99 |
|
| 100 |
if ( '' !== ( trim( (string) $database_name ) ) && '' !== ( trim( (string) $database_user ) ) && '' !== ( trim( (string) $database_host ) ) ) { |
| 101 |
$conn = mysqli_connect( (string) $database_host, (string) $database_user, (string) $datadase_password ); // phpcs:ignore |
| 102 |
if ( $conn ) { |
| 103 |
|
| 104 |
/*BEGIN: Select the Database*/ |
| 105 |
if(!mysqli_select_db($conn, (string)$database_name)) { |
| 106 |
$sql = "CREATE DATABASE IF NOT EXISTS `".(string)$database_name."`"; |
| 107 |
mysqli_query($conn, $sql); |
| 108 |
mysqli_select_db($conn, (string)$database_name); |
| 109 |
} |
| 110 |
/*END: Select the Database*/ |
| 111 |
|
| 112 |
/* BEGIN: Remove All Tables from the Database */ |
| 113 |
$found_tables = null; |
| 114 |
$result = mysqli_query( $conn, 'SHOW TABLES FROM `' . (string) $database_name . '`' ); // phpcs:ignore |
| 115 |
|
| 116 |
if ( $result ) { |
| 117 |
// $row = mysqli_fetch_row( $result ); // phpcs:ignore |
| 118 |
while ($row = mysqli_fetch_row($result)) { |
| 119 |
$found_tables[] = $row[0]; |
| 120 |
} |
| 121 |
|
| 122 |
if ( count( $found_tables ) > 0 ) { |
| 123 |
foreach ( $found_tables as $table_name ) { |
| 124 |
mysqli_query( $conn, "DROP TABLE " . (string) $database_name . ".".$table_name ); // phpcs:ignore |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
/* END: Remove All Tables from the Database */ |
| 130 |
|
| 131 |
/* BEGIN: Restore Database Content */ |
| 132 |
if ( isset( $database_file ) ) { |
| 133 |
$database_file = $database_file; |
| 134 |
if ( file_exists( $database_file ) ) { |
| 135 |
$sql_file = file_get_contents( $database_file, true ); |
| 136 |
if($sql_file){ |
| 137 |
$sql_queries = explode( ";\n", $sql_file ); |
| 138 |
$sql_queries_count = count( $sql_queries ); |
| 139 |
|
| 140 |
mysqli_query($conn, "SET sql_mode = ''"); |
| 141 |
|
| 142 |
for ( $i = 0; $i < $sql_queries_count; $i++ ) { |
| 143 |
$sql_query_=apply_filters( 'wpdbbkp_sql_query_restore', $sql_queries[ $i ] ); |
| 144 |
mysqli_query($conn, $sql_query_ ); // phpcs:ignore |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
/* END: Restore Database Content */ |
| 150 |
} |
| 151 |
} |
| 152 |
@unlink( $file_path ); |
| 153 |
} |
| 154 |
|
| 155 |
public function restore_files( $file = null ) { |
| 156 |
error_log("Inside restore_files"); |
| 157 |
if ( ! $file){ |
| 158 |
$archive = new PclZip( $this->path ); |
| 159 |
} |
| 160 |
else{ |
| 161 |
$archive = new PclZip( $file ); |
| 162 |
} |
| 163 |
|
| 164 |
if ( ! $archive->extract( PCLZIP_OPT_PATH, ABSPATH ) ){ |
| 165 |
wp_die( esc_html__('Unable to extract zip file. Please check that zlib php extension is enabled.','wpdbbkp').'<button onclick="history.go(-1);">'.esc_html__('Go Back','wpdbbkp').'</button>', esc_html__('ZIP Error','wpdbbkp') ); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
public function wp_backup_get_config_db_name() { |
| 170 |
$filepath=get_home_path().'/wp-config.php'; |
| 171 |
$config_file = @file_get_contents("$filepath", true); |
| 172 |
if($config_file){ |
| 173 |
preg_match("/'DB_NAME',\s*'(.*)?'/", $config_file, $matches); |
| 174 |
return $matches[1]; |
| 175 |
} |
| 176 |
|
| 177 |
return ''; |
| 178 |
} |
| 179 |
|
| 180 |
public function wp_backup_get_config_data($key) { |
| 181 |
$filepath=get_home_path().'/wp-config.php'; |
| 182 |
$config_file = @file_get_contents("$filepath", true); |
| 183 |
if($config_file){ |
| 184 |
switch($key) { |
| 185 |
case 'DB_NAME': |
| 186 |
preg_match("/'DB_NAME',\s*'(.*)?'/", $config_file, $matches); |
| 187 |
break; |
| 188 |
case 'DB_USER': |
| 189 |
preg_match("/'DB_USER',\s*'(.*)?'/", $config_file, $matches); |
| 190 |
break; |
| 191 |
case 'DB_PASSWORD': |
| 192 |
preg_match("/'DB_PASSWORD',\s*'(.*)?'/", $config_file, $matches); |
| 193 |
break; |
| 194 |
case 'DB_HOST': |
| 195 |
preg_match("/'DB_HOST',\s*'(.*)?'/", $config_file, $matches); |
| 196 |
break; |
| 197 |
} |
| 198 |
return $matches[1]; |
| 199 |
} |
| 200 |
|
| 201 |
return ''; |
| 202 |
|
| 203 |
} |
| 204 |
} |