| @@ -1,12 +1,14 @@ | ||
| 1 | -<?php | |
| 1 | +<?php | |
| 2 | +//phpcs:ignoreFile | |
| 3 | +//this class is used to dump the complete database to a file when creating snapshots and uses multiple non-WordPress functions in order to imporove performance and compatibility with various servers even when dumping large databases | |
| 2 | 4 | /** |
| 3 | 5 | * Abstract dump file: provides common interface for writing |
| 4 | - * data to dump files. | |
| 6 | + * data to dump files. | |
| 5 | 7 | * (c) 2create Studio, Bulgaria |
| 6 | 8 | * http://2create.bg/ |
| 7 | 9 | */ |
| 8 | -abstract class Shuttle_Dump_File { | |
| 10 | +abstract class WPR_Shuttle_Dump_File { | |
| 9 | 11 | /** |
| 10 | 12 | * File Handle |
| 11 | 13 | */ |
| 12 | 14 | protected $fh; |
| @@ -20,11 +22,11 @@ | ||
| 20 | 22 | abstract function end(); |
| 21 | 23 | |
| 22 | 24 | static function create($filename) { |
| 23 | 25 | if (self::is_gzip($filename)) { |
| 24 | - return new Shuttle_Dump_File_Gzip($filename); | |
| 26 | + return new WPR_Shuttle_Dump_File_Gzip($filename); | |
| 25 | 27 | } |
| 26 | - return new Shuttle_Dump_File_Plaintext($filename); | |
| 28 | + return new WPR_Shuttle_Dump_File_Plaintext($filename); | |
| 27 | 29 | } |
| 28 | 30 | function __construct($file) { |
| 29 | 31 | $this->file_location = $file; |
| 30 | 32 | $this->fh = $this->open(); |
| @@ -29,21 +31,21 @@ | ||
| 29 | 31 | $this->file_location = $file; |
| 30 | 32 | $this->fh = $this->open(); |
| 31 | 33 | |
| 32 | 34 | if (!$this->fh) { |
| 33 | - throw new Shuttle_Exception("Couldn't create gz file"); | |
| 35 | + throw new WPR_Shuttle_Exception("Couldn't write SQL export file"); | |
| 34 | 36 | } |
| 35 | 37 | } |
| 36 | 38 | |
| 37 | 39 | public static function is_gzip($filename) { |
| 38 | 40 | return preg_match('~gz$~i', $filename); |
| 39 | - } | |
| 41 | + } | |
| 40 | 42 | } |
| 41 | 43 | |
| 42 | 44 | /** |
| 43 | - * Plain text implementation. Uses standard file functions in PHP. | |
| 45 | + * Plain text implementation. Uses standard file functions in PHP. | |
| 44 | 46 | */ |
| 45 | -class Shuttle_Dump_File_Plaintext extends Shuttle_Dump_File { | |
| 47 | +class WPR_Shuttle_Dump_File_Plaintext extends WPR_Shuttle_Dump_File { | |
| 46 | 48 | function open() { |
| 47 | 49 | return fopen($this->file_location, 'w'); |
| 48 | 50 | } |
| 49 | 51 | function write($string) { |
| @@ -54,11 +56,11 @@ | ||
| 54 | 56 | } |
| 55 | 57 | } |
| 56 | 58 | |
| 57 | 59 | /** |
| 58 | - * Gzip implementation. Uses gz* functions. | |
| 60 | + * Gzip implementation. Uses gz* functions. | |
| 59 | 61 | */ |
| 60 | -class Shuttle_Dump_File_Gzip extends Shuttle_Dump_File { | |
| 62 | +class WPR_Shuttle_Dump_File_Gzip extends WPR_Shuttle_Dump_File { | |
| 61 | 63 | function open() { |
| 62 | 64 | return gzopen($this->file_location, 'wb9'); |
| 63 | 65 | } |
| 64 | 66 | function write($string) { |
| @@ -69,11 +71,11 @@ | ||
| 69 | 71 | } |
| 70 | 72 | } |
| 71 | 73 | |
| 72 | 74 | /** |
| 73 | - * MySQL insert statement builder. | |
| 75 | + * MySQL insert statement builder. | |
| 74 | 76 | */ |
| 75 | -class Shuttle_Insert_Statement { | |
| 77 | +class WPR_Shuttle_Insert_Statement { | |
| 76 | 78 | private $rows = array(); |
| 77 | 79 | private $length = 0; |
| 78 | 80 | private $table; |
| 79 | 81 | |
| @@ -96,10 +98,10 @@ | ||
| 96 | 98 | if (empty($this->rows)) { |
| 97 | 99 | return false; |
| 98 | 100 | } |
| 99 | 101 | |
| 100 | - return 'INSERT INTO `' . $this->table . '` VALUES ' . | |
| 101 | - implode(",\n", $this->rows) . '; '; | |
| 102 | + return 'INSERT INTO `' . $this->table . '` VALUES ' . | |
| 103 | + implode(",\n", $this->rows) . ';'; | |
| 102 | 104 | } |
| 103 | 105 | |
| 104 | 106 | function get_length() { |
| 105 | 107 | return $this->length; |
| @@ -108,21 +110,21 @@ | ||
| 108 | 110 | |
| 109 | 111 | /** |
| 110 | 112 | * Main facade |
| 111 | 113 | */ |
| 112 | -abstract class Shuttle_Dumper { | |
| 114 | +abstract class WPR_Shuttle_Dumper { | |
| 113 | 115 | /** |
| 114 | 116 | * Maximum length of single insert statement |
| 115 | 117 | */ |
| 116 | 118 | const INSERT_THRESHOLD = 838860; |
| 117 | - | |
| 119 | + | |
| 118 | 120 | /** |
| 119 | - * @var Shuttle_DBConn | |
| 120 | - */ | |
| 121 | + * @var WPR_Shuttle_DBConn | |
| 122 | + */ | |
| 121 | 123 | public $db; |
| 122 | 124 | |
| 123 | 125 | /** |
| 124 | - * @var Shuttle_Dump_File | |
| 126 | + * @var WPR_Shuttle_Dump_File | |
| 125 | 127 | */ |
| 126 | 128 | public $dump_file; |
| 127 | 129 | |
| 128 | 130 | /** |
| @@ -140,22 +142,22 @@ | ||
| 140 | 142 | */ |
| 141 | 143 | public $exclude_tables = array(); |
| 142 | 144 | |
| 143 | 145 | /** |
| 144 | - * Factory method for dumper on current hosts's configuration. | |
| 146 | + * Factory method for dumper on current hosts's configuration. | |
| 145 | 147 | */ |
| 146 | 148 | static function create($db_options) { |
| 147 | - $db = Shuttle_DBConn::create($db_options); | |
| 149 | + $db = WPR_Shuttle_DBConn::create($db_options); | |
| 148 | 150 | |
| 149 | 151 | $db->connect(); |
| 150 | 152 | |
| 151 | - if (self::has_shell_access() | |
| 153 | + if (self::has_shell_access() | |
| 152 | 154 | && self::is_shell_command_available('mysqldump') |
| 153 | 155 | && self::is_shell_command_available('gzip') |
| 154 | 156 | ) { |
| 155 | - $dumper = new Shuttle_Dumper_ShellCommand($db); | |
| 157 | + $dumper = new WPR_Shuttle_Dumper_ShellCommand($db); | |
| 156 | 158 | } else { |
| 157 | - $dumper = new Shuttle_Dumper_Native($db); | |
| 159 | + $dumper = new WPR_Shuttle_Dumper_Native($db); | |
| 158 | 160 | } |
| 159 | 161 | |
| 160 | 162 | if (isset($db_options['include_tables'])) { |
| 161 | 163 | $dumper->include_tables = $db_options['include_tables']; |
| @@ -166,9 +168,9 @@ | ||
| 166 | 168 | |
| 167 | 169 | return $dumper; |
| 168 | 170 | } |
| 169 | 171 | |
| 170 | - function __construct(Shuttle_DBConn $db) { | |
| 172 | + function __construct(WPR_Shuttle_DBConn $db) { | |
| 171 | 173 | $this->db = $db; |
| 172 | 174 | } |
| 173 | 175 | |
| 174 | 176 | public static function has_shell_access() { |
| @@ -182,9 +184,9 @@ | ||
| 182 | 184 | public static function is_shell_command_available($command) { |
| 183 | 185 | if (preg_match('~win~i', PHP_OS)) { |
| 184 | 186 | /* |
| 185 | 187 | On Windows, the `where` command checks for availabilty in PATH. According |
| 186 | - to the manual(`where /?`), there is quiet mode: | |
| 188 | + to the manual(`where /?`), there is quiet mode: | |
| 187 | 189 | .... |
| 188 | 190 | /Q Returns only the exit code, without displaying the list |
| 189 | 191 | of matched files. (Quiet mode) |
| 190 | 192 | .... |
| @@ -201,9 +203,9 @@ | ||
| 201 | 203 | } else { |
| 202 | 204 | $last_line = exec('which ' . $command); |
| 203 | 205 | $last_line = trim($last_line); |
| 204 | 206 | |
| 205 | - // Whenever there is at least one line in the output, | |
| 207 | + // Whenever there is at least one line in the output, | |
| 206 | 208 | // it should be the path to the executable |
| 207 | 209 | if (empty($last_line)) { |
| 208 | 210 | return false; |
| 209 | 211 | } else { |
| @@ -209,9 +211,9 @@ | ||
| 209 | 211 | } else { |
| 210 | 212 | return true; |
| 211 | 213 | } |
| 212 | 214 | } |
| 213 | - | |
| 215 | + | |
| 214 | 216 | } |
| 215 | 217 | |
| 216 | 218 | /** |
| 217 | 219 | * Create an export file from the tables with that prefix. |
| @@ -225,9 +227,9 @@ | ||
| 225 | 227 | protected function get_tables($table_prefix) { |
| 226 | 228 | if (!empty($this->include_tables)) { |
| 227 | 229 | return $this->include_tables; |
| 228 | 230 | } |
| 229 | - | |
| 231 | + | |
| 230 | 232 | // $tables will only include the tables and not views. |
| 231 | 233 | // TODO - Handle views also, edits to be made in function 'get_create_table_sql' line 336 |
| 232 | 234 | $tables = $this->db->fetch_numeric(' |
| 233 | 235 | SHOW FULL TABLES WHERE Table_Type = "BASE TABLE" AND Tables_in_'.$this->db->name.' LIKE "' . $this->db->escape_like($table_prefix) . '%" |
| @@ -243,13 +245,13 @@ | ||
| 243 | 245 | return $tables_list; |
| 244 | 246 | } |
| 245 | 247 | } |
| 246 | 248 | |
| 247 | -class Shuttle_Dumper_ShellCommand extends Shuttle_Dumper { | |
| 249 | +class WPR_Shuttle_Dumper_ShellCommand extends WPR_Shuttle_Dumper { | |
| 248 | 250 | function dump($export_file_location, $table_prefix='') { |
| 249 | 251 | $command = 'mysqldump -h ' . escapeshellarg($this->db->host) . |
| 250 | - ' -u ' . escapeshellarg($this->db->username) . | |
| 251 | - ' --password=' . escapeshellarg($this->db->password) . | |
| 252 | + ' -u ' . escapeshellarg($this->db->username) . | |
| 253 | + ' --password=' . escapeshellarg($this->db->password) . | |
| 252 | 254 | ' ' . escapeshellarg($this->db->name); |
| 253 | 255 | |
| 254 | 256 | $include_all_tables = empty($table_prefix) && |
| 255 | 257 | empty($this->include_tables) && |
| @@ -263,9 +265,9 @@ | ||
| 263 | 265 | $error_file = tempnam(sys_get_temp_dir(), 'err'); |
| 264 | 266 | |
| 265 | 267 | $command .= ' 2> ' . escapeshellarg($error_file); |
| 266 | 268 | |
| 267 | - if (Shuttle_Dump_File::is_gzip($export_file_location)) { | |
| 269 | + if (WPR_Shuttle_Dump_File::is_gzip($export_file_location)) { | |
| 268 | 270 | $command .= ' | gzip'; |
| 269 | 271 | } |
| 270 | 272 | |
| 271 | 273 | $command .= ' > ' . escapeshellarg($export_file_location); |
| @@ -274,9 +276,9 @@ | ||
| 274 | 276 | |
| 275 | 277 | if ($return_val !== 0) { |
| 276 | 278 | $error_text = file_get_contents($error_file); |
| 277 | 279 | unlink($error_file); |
| 278 | - throw new Shuttle_Exception('Couldn\'t export database: ' . $error_text); | |
| 280 | + throw new WPR_Shuttle_Exception('Couldn\'t export database: ' . esc_html($error_text)); | |
| 279 | 281 | } |
| 280 | 282 | |
| 281 | 283 | unlink($error_file); |
| 282 | 284 | } |
| @@ -281,19 +283,22 @@ | ||
| 281 | 283 | unlink($error_file); |
| 282 | 284 | } |
| 283 | 285 | } |
| 284 | 286 | |
| 285 | -class Shuttle_Dumper_Native extends Shuttle_Dumper { | |
| 287 | +class WPR_Shuttle_Dumper_Native extends WPR_Shuttle_Dumper { | |
| 286 | 288 | public function dump($export_file_location, $table_prefix='') { |
| 289 | + global $wp_reset; | |
| 287 | 290 | $eol = $this->eol; |
| 288 | 291 | |
| 289 | - $this->dump_file = Shuttle_Dump_File::create($export_file_location); | |
| 292 | + $this->dump_file = WPR_Shuttle_Dump_File::create($export_file_location); | |
| 290 | 293 | |
| 291 | 294 | $this->dump_file->write("-- Generation time: " . date('r') . $eol); |
| 292 | 295 | $this->dump_file->write("-- Host: " . $this->db->host . $eol); |
| 293 | 296 | $this->dump_file->write("-- DB name: " . $this->db->name . $eol); |
| 297 | + $this->dump_file->write("-- WP Reset ver: " . $wp_reset->get_plugin_version() . $eol); | |
| 298 | + $this->dump_file->write("-- Table Prefix: " . $table_prefix . $eol); | |
| 294 | 299 | $this->dump_file->write("/*!40030 SET NAMES UTF8 */;$eol"); |
| 295 | - | |
| 300 | + | |
| 296 | 301 | $this->dump_file->write("/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;$eol"); |
| 297 | 302 | $this->dump_file->write("/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;$eol"); |
| 298 | 303 | $this->dump_file->write("/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;$eol"); |
| 299 | 304 | $this->dump_file->write("/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;$eol"); |
| @@ -307,9 +312,9 @@ | ||
| 307 | 312 | $tables = $this->get_tables($table_prefix); |
| 308 | 313 | foreach ($tables as $table) { |
| 309 | 314 | $this->dump_table($table); |
| 310 | 315 | } |
| 311 | - | |
| 316 | + | |
| 312 | 317 | $this->dump_file->write("$eol$eol"); |
| 313 | 318 | $this->dump_file->write("/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;$eol"); |
| 314 | 319 | $this->dump_file->write("/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;$eol"); |
| 315 | 320 | $this->dump_file->write("/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;$eol"); |
| @@ -330,9 +335,9 @@ | ||
| 330 | 335 | $this->dump_file->write($create_table_sql . $eol . $eol); |
| 331 | 336 | |
| 332 | 337 | $data = $this->db->query("SELECT * FROM `$table`"); |
| 333 | 338 | |
| 334 | - $insert = new Shuttle_Insert_Statement($table); | |
| 339 | + $insert = new WPR_Shuttle_Insert_Statement($table); | |
| 335 | 340 | |
| 336 | 341 | while ($row = $this->db->fetch_row($data)) { |
| 337 | 342 | $row_values = array(); |
| 338 | 343 | foreach ($row as $value) { |
| @@ -353,9 +358,9 @@ | ||
| 353 | 358 | $this->dump_file->write($insert->get_sql() . $eol); |
| 354 | 359 | } |
| 355 | 360 | $this->dump_file->write($eol . $eol); |
| 356 | 361 | } |
| 357 | - | |
| 362 | + | |
| 358 | 363 | public function get_create_table_sql($table) { |
| 359 | 364 | $create_table_sql = $this->db->fetch('SHOW CREATE TABLE `' . $table . '`'); |
| 360 | 365 | return $create_table_sql[0]['Create Table'] . ';'; |
| 361 | 366 | } |
| @@ -360,9 +365,9 @@ | ||
| 360 | 365 | return $create_table_sql[0]['Create Table'] . ';'; |
| 361 | 366 | } |
| 362 | 367 | } |
| 363 | 368 | |
| 364 | -class Shuttle_DBConn { | |
| 369 | +class WPR_Shuttle_DBConn { | |
| 365 | 370 | public $host; |
| 366 | 371 | public $username; |
| 367 | 372 | public $password; |
| 368 | 373 | public $name; |
| @@ -380,11 +385,11 @@ | ||
| 380 | 385 | } |
| 381 | 386 | |
| 382 | 387 | static function create($options) { |
| 383 | 388 | if (class_exists('mysqli')) { |
| 384 | - $class_name = "Shuttle_DBConn_Mysqli"; | |
| 389 | + $class_name = "WPR_Shuttle_DBConn_Mysqli"; | |
| 385 | 390 | } else { |
| 386 | - $class_name = "Shuttle_DBConn_Mysql"; | |
| 391 | + $class_name = "WPR_Shuttle_DBConn_Mysql"; | |
| 387 | 392 | } |
| 388 | 393 | |
| 389 | 394 | return new $class_name($options); |
| 390 | 395 | } |
| @@ -389,18 +394,18 @@ | ||
| 389 | 394 | return new $class_name($options); |
| 390 | 395 | } |
| 391 | 396 | } |
| 392 | 397 | |
| 393 | -class Shuttle_DBConn_Mysql extends Shuttle_DBConn { | |
| 398 | +class WPR_Shuttle_DBConn_Mysql extends WPR_Shuttle_DBConn { | |
| 394 | 399 | function connect() { |
| 395 | 400 | $this->connection = @mysql_connect($this->host, $this->username, $this->password); |
| 396 | 401 | if (!$this->connection) { |
| 397 | - throw new Shuttle_Exception("Couldn't connect to the database: " . mysql_error()); | |
| 402 | + throw new WPR_Shuttle_Exception("Couldn't connect to the database: " . esc_html(mysql_error())); | |
| 398 | 403 | } |
| 399 | 404 | |
| 400 | 405 | $select_db_res = mysql_select_db($this->name, $this->connection); |
| 401 | 406 | if (!$select_db_res) { |
| 402 | - throw new Shuttle_Exception("Couldn't select database: " . mysql_error($this->connection)); | |
| 407 | + throw new WPR_Shuttle_Exception("Couldn't select database: " . esc_html(mysql_error($this->connection))); | |
| 403 | 408 | } |
| 404 | 409 | |
| 405 | 410 | return true; |
| 406 | 411 | } |
| @@ -410,9 +415,9 @@ | ||
| 410 | 415 | $this->connect(); |
| 411 | 416 | } |
| 412 | 417 | $res = mysql_query($q); |
| 413 | 418 | if (!$res) { |
| 414 | - throw new Shuttle_Exception("SQL error: " . mysql_error($this->connection)); | |
| 419 | + throw new WPR_Shuttle_Exception("SQL error: " . esc_html(mysql_error($this->connection))); | |
| 415 | 420 | } |
| 416 | 421 | return $res; |
| 417 | 422 | } |
| 418 | 423 | |
| @@ -451,14 +456,14 @@ | ||
| 451 | 456 | } |
| 452 | 457 | } |
| 453 | 458 | |
| 454 | 459 | |
| 455 | -class Shuttle_DBConn_Mysqli extends Shuttle_DBConn { | |
| 460 | +class WPR_Shuttle_DBConn_Mysqli extends WPR_Shuttle_DBConn { | |
| 456 | 461 | function connect() { |
| 457 | 462 | $this->connection = @new MySQLi($this->host, $this->username, $this->password, $this->name); |
| 458 | 463 | |
| 459 | 464 | if ($this->connection->connect_error) { |
| 460 | - throw new Shuttle_Exception("Couldn't connect to the database: " . $this->connection->connect_error); | |
| 465 | + throw new WPR_Shuttle_Exception("Couldn't connect to the database: " . esc_html($this->connection->connect_error)); | |
| 461 | 466 | } |
| 462 | 467 | |
| 463 | 468 | return true; |
| 464 | 469 | } |
| @@ -467,13 +472,13 @@ | ||
| 467 | 472 | if (!$this->connection) { |
| 468 | 473 | $this->connect(); |
| 469 | 474 | } |
| 470 | 475 | $res = $this->connection->query($q); |
| 471 | - | |
| 476 | + | |
| 472 | 477 | if (!$res) { |
| 473 | - throw new Shuttle_Exception("SQL error: " . $this->connection->error); | |
| 478 | + throw new WPR_Shuttle_Exception("SQL error: " . esc_html($this->connection->error)); | |
| 474 | 479 | } |
| 475 | - | |
| 480 | + | |
| 476 | 481 | return $res; |
| 477 | 482 | } |
| 478 | 483 | |
| 479 | 484 | function fetch_numeric($query) { |
| @@ -510,5 +515,5 @@ | ||
| 510 | 515 | return $data->fetch_array(MYSQLI_ASSOC); |
| 511 | 516 | } |
| 512 | 517 | } |
| 513 | 518 | |
| 514 | -class Shuttle_Exception extends Exception {}; | |
| 519 | +class WPR_Shuttle_Exception extends Exception {}; | |