PluginProbe
WP Database Backup – Unlimited Database & Files Backup by Backup for WP / 6.1
WP Database Backup – Unlimited Database & Files Backup by Backup for WP v6.1
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.1, at includes/admin/class-restore.php

192 lines 8.4 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
99 if ( '' !== ( trim( (string) $database_name ) ) && '' !== ( trim( (string) $database_user ) ) && '' !== ( trim( (string) $database_host ) ) ) {
100 $conn = mysqli_connect( (string) $database_host, (string) $database_user, (string) $datadase_password ); // phpcs:ignore
101 if ( $conn ) {
102
103 /*BEGIN: Select the Database*/
104 if(!mysqli_select_db($conn, (string)$database_name)) {
105 $sql = "CREATE DATABASE IF NOT EXISTS `".(string)$database_name."`";
106 mysqli_query($conn, $sql);
107 mysqli_select_db($conn, (string)$database_name);
108 }
109 /*END: Select the Database*/
110
111 /* BEGIN: Remove All Tables from the Database */
112 $found_tables = null;
113 $result = mysqli_query( $conn, 'SHOW TABLES FROM `' . (string) $database_name . '`' ); // phpcs:ignore
114
115 if ( $result ) {
116 // $row = mysqli_fetch_row( $result ); // phpcs:ignore
117 while ($row = mysqli_fetch_row($result)) {
118 $found_tables[] = $row[0];
119 }
120
121 if ( count( $found_tables ) > 0 ) {
122 foreach ( $found_tables as $table_name ) {
123 mysqli_query( $conn, "DROP TABLE " . (string) $database_name . ".".$table_name ); // phpcs:ignore
124 }
125 }
126 }
127
128 /* END: Remove All Tables from the Database */
129
130 /* BEGIN: Restore Database Content */
131 if ( isset( $database_file ) ) {
132 $database_file = $database_file;
133 if ( file_exists( $database_file ) ) {
134 $sql_file = file_get_contents( $database_file, true );
135
136 $sql_queries = explode( ";\n", $sql_file );
137 $sql_queries_count = count( $sql_queries );
138
139 mysqli_query($conn, "SET sql_mode = ''");
140
141 for ( $i = 0; $i < $sql_queries_count; $i++ ) {
142 mysqli_query($conn, $sql_queries[ $i ] ); // phpcs:ignore
143 }
144 }
145 }
146 /* END: Restore Database Content */
147 }
148 }
149 @unlink( $file_path );
150 }
151
152 public function restore_files( $file = null ) {
153 error_log("Inside restore_files");
154 if ( ! $file){
155 $archive = new PclZip( $this->path );
156 }
157 else{
158 $archive = new PclZip( $file );
159 }
160
161 if ( ! $archive->extract( PCLZIP_OPT_PATH, ABSPATH ) ){
162 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' );
163 }
164 }
165
166 public function wp_backup_get_config_db_name() {
167 $filepath=get_home_path().'/wp-config.php';
168 $config_file = @file_get_contents("$filepath", true);
169 preg_match("/'DB_NAME',\s*'(.*)?'/", $config_file, $matches);
170 return $matches[1];
171 }
172
173 public function wp_backup_get_config_data($key) {
174 $filepath=get_home_path().'/wp-config.php';
175 $config_file = @file_get_contents("$filepath", true);
176 switch($key) {
177 case 'DB_NAME':
178 preg_match("/'DB_NAME',\s*'(.*)?'/", $config_file, $matches);
179 break;
180 case 'DB_USER':
181 preg_match("/'DB_USER',\s*'(.*)?'/", $config_file, $matches);
182 break;
183 case 'DB_PASSWORD':
184 preg_match("/'DB_PASSWORD',\s*'(.*)?'/", $config_file, $matches);
185 break;
186 case 'DB_HOST':
187 preg_match("/'DB_HOST',\s*'(.*)?'/", $config_file, $matches);
188 break;
189 }
190 return $matches[1];
191 }
192 }