PluginProbe
WP Database Backup – Unlimited Database & Files Backup by Backup for WP / 6.5
WP Database Backup – Unlimited Database & Files Backup by Backup for WP v6.5
7.13 7.12 trunk 1.1 2.1.1 5.9 6.0 6.1 6.10 6.11 6.12 6.12.1 6.2 6.3 6.4 6.5 6.5.1 6.6 6.7 6.8 6.9 7.0 7.0.1 7.1 7.10 All 34 releases
wp-database-backup / includes / admin / class-restore.php

class-restore.php in WP Database Backup – Unlimited Database & Files Backup by Backup for WP 6.5, at includes/admin/class-restore.php

194 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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( 'Unable to extract zip file. Please check that zlib php extension is enabled. <button onclick="history.go(-1);">Go Back</button>', 'ZIP Error' );
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
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 /* END: Restore Database Content */
149 }
150 }
151 @unlink( $file_path );
152 }
153
154 public function restore_files( $file = null ) {
155 error_log("Inside restore_files");
156 if ( ! $file){
157 $archive = new PclZip( $this->path );
158 }
159 else{
160 $archive = new PclZip( $file );
161 }
162
163 if ( ! $archive->extract( PCLZIP_OPT_PATH, ABSPATH ) ){
164 wp_die( 'Unable to extract zip file. Please check that zlib php extension is enabled. <button onclick="history.go(-1);">Go Back</button>', 'ZIP Error' );
165 }
166 }
167
168 public function wp_backup_get_config_db_name() {
169 $filepath=get_home_path().'/wp-config.php';
170 $config_file = @file_get_contents("$filepath", true);
171 preg_match("/'DB_NAME',\s*'(.*)?'/", $config_file, $matches);
172 return $matches[1];
173 }
174
175 public function wp_backup_get_config_data($key) {
176 $filepath=get_home_path().'/wp-config.php';
177 $config_file = @file_get_contents("$filepath", true);
178 switch($key) {
179 case 'DB_NAME':
180 preg_match("/'DB_NAME',\s*'(.*)?'/", $config_file, $matches);
181 break;
182 case 'DB_USER':
183 preg_match("/'DB_USER',\s*'(.*)?'/", $config_file, $matches);
184 break;
185 case 'DB_PASSWORD':
186 preg_match("/'DB_PASSWORD',\s*'(.*)?'/", $config_file, $matches);
187 break;
188 case 'DB_HOST':
189 preg_match("/'DB_HOST',\s*'(.*)?'/", $config_file, $matches);
190 break;
191 }
192 return $matches[1];
193 }
194 }