C:\xampp\htdocs\landing\wp-content\updraft\plugins-old\wp-file-manager\classes\db-restore.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
/**
 * Define database parameters here
 */
$upload_dir wp_upload_dir();
$backup_dirname $upload_dir['basedir'].'/wp-file-manager-pro/fm_backup';
define("BACKUP_DIR"$backup_dirname);
define("CHARSET"'utf8');
define("DISABLE_FOREIGN_KEY_CHECKS"true);

/**
 * The Restore_Database class
 */
class Restore_Database {
    
/**
     * Host where the database is located
     */
    
var $host;

    
/**
     * Username used to connect to database
     */
    
var $username;

    
/**
     * Password used to connect to database
     */
    
var $passwd;

    
/**
     * Database to backup
     */
    
var $dbName;

    
/**
     * Database charset
     */
    
var $charset;

    
/**
     * Database connection
     */
    
var $conn;

    
/**
     * Disable foreign key checks
     */
    
var $disableForeignKeyChecks;

    
/**
     * Constructor initializes database
     */
    
function __construct($filename) {
        
$this->host                    DB_HOST;
        
$this->username                DB_USER;
        
$this->passwd                  DB_PASSWORD;
        
$this->dbName                  DB_NAME;
        
$this->charset                 DB_CHARSET;
        
$this->disableForeignKeyChecks defined('DISABLE_FOREIGN_KEY_CHECKS') ? DISABLE_FOREIGN_KEY_CHECKS true;
        
$this->conn                    $this->initializeDatabase();
        
$this->backupDir               defined('BACKUP_DIR') ? BACKUP_DIR '.';
        
$this->backupFile              $filename;
    }

    
/**
     * Destructor re-enables foreign key checks
     */
    
function __destructor() {
        
/**
         * Re-enable foreign key checks 
         */
        
if ($this->disableForeignKeyChecks === true) {
            
mysqli_query($this->conn'SET foreign_key_checks = 1');
        }
    }

    protected function 
initializeDatabase() {
        try {
            
$conn mysqli_connect($this->host$this->username$this->passwd$this->dbName);
            if (
mysqli_connect_errno()) {
                throw new 
Exception('ERROR connecting database: ' mysqli_connect_error());
                die();
            }
            if (!
mysqli_set_charset($conn$this->charset)) {
                
mysqli_query($conn'SET NAMES '.$this->charset);
            }

            
/**
             * Disable foreign key checks 
             */
            
if ($this->disableForeignKeyChecks === true) {
                
mysqli_query($conn'SET foreign_key_checks = 0');
            }

        } catch (
Exception $e) {
            
print_r($e->getMessage());
            die();
        }

        return 
$conn;
    }

    
/**
     * Backup the whole database or just some tables
     * Use '*' for whole database or 'table1 table2 table3...'
     * @param string $tables
     */
    
public function restoreDb() {
        try {
            
$sql '';
            
$multiLineComment false;

            
$backupDir $this->backupDir;
            
$backupFile $this->backupFile;

            
/**
             * Gunzip file if gzipped
             */
            
$backupFileIsGzipped substr($backupFile, -33) == '.gz' true false;
            if (
$backupFileIsGzipped) {
                if (!
$backupFile $this->gunzipBackupFile()) {
                    throw new 
Exception("ERROR: couldn't gunzip backup file " $backupDir '/' $backupFile);
                }
            }

            
/**
            * Read backup file line by line
            */
            
$handle fopen($backupDir '/' $backupFile"r");
            if (
$handle) {
                while ((
$line fgets($handle)) !== false) {
                    
$line ltrim(rtrim($line));
                    if (
strlen($line) > 1) { // avoid blank lines
                        
$lineIsComment false;
                        if (
preg_match('/^\/\*/'$line)) {
                            
$multiLineComment true;
                            
$lineIsComment true;
                        }
                        if (
$multiLineComment or preg_match('/^\/\//'$line)) {
                            
$lineIsComment true;
                        }
                        if (!
$lineIsComment) {
                            
$sql .= $line;
                            if (
preg_match('/;$/'$line)) {
                                
// execute query
                                
if(mysqli_query($this->conn$sql)) {
                                    if (
preg_match('/^CREATE TABLE `([^`]+)`/i'$sql$tableName)) {
                                        
$this->obfPrint("Table succesfully created: `" $tableName[1] . "`");
                                    }
                                    
$sql '';
                                } else {
                                    throw new 
Exception("ERROR: SQL execution error: " mysqli_error($this->conn));
                                }
                            }
                        } else if (
preg_match('/\*\/$/'$line)) {
                            
$multiLineComment false;
                        }
                    }
                }
                
fclose($handle);
            } else {
                throw new 
Exception("ERROR: couldn't open backup file " $backupDir '/' $backupFile);
            } 
        } catch (
Exception $e) {
            
print_r($e->getMessage());
            return 
false;
        }

        if (
$backupFileIsGzipped) {
            
unlink($backupDir '/' $backupFile);
        }

        return 
true;
    }

    
/*
     * Gunzip backup file
     *
     * @return string New filename (without .gz appended and without backup directory) if success, or false if operation fails
     */
    
protected function gunzipBackupFile() {
        
// Raising this value may increase performance
        
$bufferSize 4096// read 4kb at a time
        
$error false;

        
$source $this->backupDir '/' $this->backupFile;
        
$dest $this->backupDir '/' date("Ymd_His"time()) . '_' substr($this->backupFile0, -3);

        
$this->obfPrint('Gunzipping backup file ' $source '... '11);

        
// Remove $dest file if exists
        
if (file_exists($dest)) {
            if (!
unlink($dest)) {
                return 
false;
            }
        }
        
        
// Open gzipped and destination files in binary mode
        
if (!$srcFile gzopen($this->backupDir '/' $this->backupFile'rb')) {
            return 
false;
        }
        if (!
$dstFile fopen($dest'wb')) {
            return 
false;
        }

        while (!
gzeof($srcFile)) {
            
// Read buffer-size bytes
            // Both fwrite and gzread are binary-safe
            
if(!fwrite($dstFilegzread($srcFile$bufferSize))) {
                return 
false;
            }
        }

        
fclose($dstFile);
        
gzclose($srcFile);

        
// Return backup filename excluding backup directory
        
return str_replace($this->backupDir '/'''$dest);
    }

    
/**
     * Prints message forcing output buffer flush
     *
     */
    
public function obfPrint ($msg ''$lineBreaksBefore 0$lineBreaksAfter 1) {
        if (!
$msg) {
            return 
false;
        }

        
$msg date("Y-m-d H:i:s") . ' - ' $msg;
        
$output '';

        if (
php_sapi_name() != "cli") {
            
$lineBreak "<br />";
        } else {
            
$lineBreak "\n";
        }

        if (
$lineBreaksBefore 0) {
            for (
$i 1$i <= $lineBreaksBefore$i++) {
                
$output .= $lineBreak;
            }                
        }

        
$output .= $msg;

        if (
$lineBreaksAfter 0) {
            for (
$i 1$i <= $lineBreaksAfter$i++) {
                
$output .= $lineBreak;
            }                
        }

        if (
php_sapi_name() == "cli") {
            
$output .= "\n";
        }

        echo 
$output;

        if (
php_sapi_name() != "cli") {
            
ob_flush();
        }

        
flush();
    }
}
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586