# HG changeset patch # User Eris Caffee # Date 1317320884 18000 # Node ID 45012f7a69aa734d4f17da8a5f9797e743c1ea1e Initial commit diff -r 000000000000 -r 45012f7a69aa dump.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dump.php Thu Sep 29 13:28:04 2011 -0500 @@ -0,0 +1,615 @@ +"; + } + else + { + echo "Unable to connect."."
"; + } + } + else if ('Export Using MySQLDump' == $_REQUEST['action']) + { + $retval = mysql_test($db_host, $db_name, $db_user, $db_password); + if ($retval) + { + $print_form=0; + send_dump($db_host, $db_name, $db_user, $db_password); + } + else + { + echo "Unable to connect."."
"; + } + } + else if ('Export Using MySQLDump, Save File On Server' == $_REQUEST['action']) + { + $retval = mysql_test($db_host, $db_name, $db_user, $db_password); + if ($retval) + { + $print_form=0; + save_dump($db_host, $db_name, $db_user, $db_password); + } + else + { + echo "Unable to connect."."
"; + } + } + else if ('Export Using SQL' == $_REQUEST['action']) + { + $link = mysql_login($db_host, $db_name, $db_user, $db_password); + if ($link) + { + $print_form=0; + send_dump_old($link, $db_host, $db_name); + } + else + { + echo "Unable to connect."."
"; + } + } + else if ('View Dump' == $_REQUEST['action']) + { + $link = mysql_login($db_host, $db_name, $db_user, $db_password); + if ($link) + { + $print_form=0; + view_dump($link, $db_host, $db_name); + } + else + { + echo "Unable to connect."."
"; + } + } + else if ('Try Wordpress' == $_REQUEST['action']) + { + $found = wp_get_logins($db_host, $db_name, $db_user, $db_password, $table_prefix, $multi); + if ($found) + { + $retval = mysql_test($db_host, $db_name, $db_user, $db_password); + if ($retval) + { + echo "Success!"."
"; + $_REQUEST['db_host']=$db_host; + $_REQUEST['db_name']=$db_name; + $_REQUEST['db_user']=$db_user; + $_REQUEST['db_password']=$db_password; + } + } + else + { + echo "Wordpress not found"."
"; + } + } + else if ('Try Joomla' == $_REQUEST['action']) + { + $found = get_joomla_info($db_host, $db_name, $db_user, $db_password); + if ($found) + { + $retval = mysql_test($db_host,$db_name, $db_user, $db_password); + if ($retval) + { + echo "Success!"."
"; + $_REQUEST['db_host']=$db_host; + $_REQUEST['db_name']=$db_name; + $_REQUEST['db_user']=$db_user; + $_REQUEST['db_password']=$db_password; + } + else + { + printf("Unable to connect using
host: %s
database: %s
user:%s
password: %s
", $db_host, $db_name, $db_user, $db_password); + } + } + else + { + echo "Joomla not found"."
"; + } + } + else if ('Try Simple Machines Forum' == $_REQUEST['action']) + { + $found = get_smf_info($db_host, $db_name, $db_user, $db_password); + if ($found) + { + $retval = mysql_test($db_host,$db_name, $db_user, $db_password); + if ($retval) + { + echo "Success!"."
"; + $_REQUEST['db_host']=$db_host; + $_REQUEST['db_name']=$db_name; + $_REQUEST['db_user']=$db_user; + $_REQUEST['db_password']=$db_password; + } + else + { + printf("Unable to connect using
host: %s
database: %s
user:%s
password: %s
", $db_host, $db_name, $db_user, $db_password); + } + } + else + { + echo "Simple Machines Forum not found"."
"; + } + } + } + +//////////////////////////////////////////////////////////////////////////////// +function send_dump($db_host, $db_name, $db_user, $db_password) + { + $dumpname = $db_host."_".$db_name."-".date("Y\.m\.d\_H\.i\.s").".sql"; + header('Content-type: text/plain;charset=UTF-8'); + header('Content-Disposition: attachment; filename="'.$dumpname.'"'); + passthru("mysqldump --opt -u'".$db_user."' -p'".$db_password."' -h'".$db_host."' '".$db_name."'"); + } + +//////////////////////////////////////////////////////////////////////////////// +function save_dump($db_host, $db_name, $db_user, $db_password) + { + $dumpname = $db_host."_".$db_name."-".date("Y\.m\.d\_H\.i\.s").".sql"; + echo "

Saving dump to ".$dumpname."

"; + passthru("mysqldump --opt -u'".$db_user."' -p'".$db_password."' -h'".$db_host."' '".$db_name."' > ".$dumpname); + } + +//////////////////////////////////////////////////////////////////////////////// +function send_dump_old($link, $db_host, $db_name) + { + $dumpname = $db_host."_".$db_name."-".date("Y\.m\.d\_H\.i\.s").".sql"; + header('Content-type: text/plain;charset=UTF-8'); + header('Content-Disposition: attachment; filename="'.$dumpname.'"'); + mysqldump($link, $db_host, $db_name); + } + +//////////////////////////////////////////////////////////////////////////////// +function view_dump($link, $db_host, $db_name) + { + header('Content-type: text/plain;charset=UTF-8'); + mysqldump($link, $db_host, $db_name); + } + +//////////////////////////////////////////////////////////////////////////////// +function mysql_login($db_host, $db_name, $db_user, $db_password) + { + $link = mysql_connect($db_host, $db_user, $db_password); + if ($link) + { + $db_selected = mysql_select_db($db_name, $link); + if ($db_selected) + { + mysql_query("SET NAMES utf8;", $link); + } + else + { + printf("Connected, but unable to select database using
host: %s
database: %s
user:%s
password: %s
", $db_host, $db_name, $db_user, $db_password); + mysql_close($link); + $link = FALSE; + } + } + else + { + printf("Unable to connect using
host: %s
database: %s
user:%s
password: %s
", $db_host, $db_name, $db_user, $db_password); + mysql_close($link); + $link = FALSE; + } + return $link; + } + +//////////////////////////////////////////////////////////////////////////////// +function mysql_test($db_host, $db_name, $db_user, $db_password) + { + $retval = 0; + $link = mysql_login($db_host, $db_name, $db_user, $db_password); + if ($link) + { + mysql_close($link); + $retval = 1; + } + return $retval; + } + +//////////////////////////////////////////////////////////////////////////////// +// Miscellaneous functions +//////////////////////////////////////////////////////////////////////////////// +function find_files(&$filelist, $regex, $dir='.', $maxdepth=PHP_INT_MAX) + { + static $depth = -1; + $depth++; + if ($handle = opendir($dir) ) + { + while (false !== ($file = readdir($handle))) + { + if (($file == ".") || ($file == "..")) + { + continue; + } + if (preg_match($regex, $file) > 0) + { + array_push($filelist, $dir."/".$file); + } + else if (is_dir($file) && $depth < $maxdepth) + { + find_files($filelist, $regex, $dir."/".$file, $maxdepth); + } + } + closedir($handle); + } + $depth = -1; + } + +//////////////////////////////////////////////////////////////////////////////// +// Wordpress functions +//////////////////////////////////////////////////////////////////////////////// +function wp_find_configs(&$configs, $dir=".", $maxdepth=2) + { + $configs = array(); + find_files($configs, "/^wp-config.php$/", $dir, $maxdepth); + } + +function wp_get_logins(&$db_host, &$db_name, &$db_user, &$db_password, &$table_prefix, &$multi) + { + $found = 0; + $conf = fopen("wp-config.php", "r"); + if ($conf) + { + while (($line = fgets($conf, 4096)) != false) + { + if (preg_match('/([\'\"])DB_NAME\1\s*,\s*([\'\"])(.*?)\2/', $line, $matches)) + { + $db_name = $matches[3]; + ++$found; + } + if (preg_match('/([\'\"])DB_USER\1\s*,\s*([\'\"])(.*?)\2/', $line, $matches)) + { + $db_user = $matches[3]; + ++$found; + } + if (preg_match('/([\'\"])DB_PASSWORD\1\s*,\s*([\'\"])(.*?)\2/', $line, $matches)) + { + $db_password = $matches[3]; + ++$found; + } + if (preg_match('/([\'\"])DB_HOST\1\s*,\s*([\'\"])(.*?)\2/', $line, $matches)) + { + $db_host = $matches[3]; + ++$found; + } + } + } + fclose($conf); + if ($found != 4) + { + return 0; + } + return 1; + } + +function wp_list_sites() + { + + } + +//////////////////////////////////////////////////////////////////////////////// +// Joomla functions +//////////////////////////////////////////////////////////////////////////////// +function get_joomla_info(&$host, &$database, &$username, &$password) + { + $found = 0; + $conf = fopen("configuration.php", "r"); + if ($conf) + { + while (($line = fgets($conf, 4096)) != false) + { + if (preg_match('/\Q$\Edb\s*=\s*([\'\"])(.*)\1/', $line, $matches)) + { + printf("%s\n", $line); + print_r($matches); echo "
"; + $database = $matches[2]; + ++$found; + } + if (preg_match('/\Q$\Euser\s*=\s*([\'\"])(.*)\1/', $line, $matches)) + { + print_r($matches); echo "
"; + $username = $matches[2]; + ++$found; + } + if (preg_match('/\Q$\Epassword\s*=\s*([\'\"])(.*)\1/', $line, $matches)) + { + print_r($matches); echo "
"; + $password = $matches[2]; + ++$found; + } + if (preg_match('/\Q$\Ehost\s*=\s*([\'\"])(.*)\1/', $line, $matches)) + { + print_r($matches); echo "
"; + $host = $matches[2]; + ++$found; + } + } + } + fclose($conf); + if ($found != 4) + { + return 0; + } + return 1; + } + +//////////////////////////////////////////////////////////////////////////////// +// Simple Machines Forum functions +//////////////////////////////////////////////////////////////////////////////// +function get_smf_info(&$host, &$database, &$username, &$password) + { + $found = 0; + $conf = fopen("Settings.php", "r"); + if ($conf) + { + while (($line = fgets($conf, 4096)) != false) + { + if (preg_match('/\$db_name\s*=\s*([\'\"])(.*)\1/', $line, $matches)) + { + $database = $matches[2]; + ++$found; + } + if (preg_match('/\$db_user\s*=\s*([\'\"])(.*)\1/', $line, $matches)) + { + $username = $matches[2]; + ++$found; + } + if (preg_match('/\$db_passwd\s*=\s*([\'\"])(.*)\1/', $line, $matches)) + { + $password = $matches[2]; + ++$found; + } + if (preg_match('/\$db_server\s*=\s*([\'\"])(.*)\1/', $line, $matches)) + { + $host = $matches[2]; + ++$found; + } + } + } + fclose($conf); + if ($found != 4) + { + return 0; + } + return 1; + } + + + +//////////////////////////////////////////////////////////////////////////////// +// Mostly original mysqldump.php functions +//////////////////////////////////////////////////////////////////////////////// +function mysqldump($link, $db_host, $db_name) + { + printf("-- Host: %s\n". + "-- Database: %s\n". + "\n". + "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;\n". + "/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;\n". + "/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;\n". + "/*!40101 SET NAMES utf8 */;\n". + "/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;\n". + "/*!40103 SET TIME_ZONE='+00:00' */;\n". + "/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;\n". + "/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;\n". + "/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;\n". + "/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;". + "\n\n\n", $db_host, $db_name); + + $sql = "SHOW TABLES;"; + $result = mysql_query($sql, $link); + if ($result) + { + while ($row = mysql_fetch_row($result)) + { + mysqldump_table_structure($link, $row[0]); + mysqldump_table_data($link, $row[0]); + } + } + else + { + printf("-- no tables in database \n"); + } + mysql_free_result($result); + + printf("\n\n\n". + "/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;\n". + "/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;\n". + "/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;\n". + "/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;\n". + "/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;\n". + "/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;\n". + "/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;\n". + "/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;\n"); + } + +//////////////////////////////////////////////////////////////////////////////// +function mysqldump_table_structure($link, $table) + { + echo "-- Table structure for table ".$table." \n"; + echo "DROP TABLE IF EXISTS `".$table."`;\n\n"; + $sql = "SHOW CREATE TABLE `".$table."`; "; + $result=mysql_query($sql, $link); + if ($result) + { + if ($row= mysql_fetch_assoc($result)) + { + echo $row['Create Table'].";\n\n"; + } + } + mysql_free_result($result); + } + +//////////////////////////////////////////////////////////////////////////////// +function mysqldump_table_data($link, $table) + { + $sql = "SELECT * FROM `".$table."`;"; + $result = mysql_query($sql, $link); + if ($result) + { + $num_rows = mysql_num_rows($result); + $num_fields = mysql_num_fields($result); + + if ($num_rows > 0) + { + printf("-- dumping data for table %s\n". + "LOCK TABLE `%s`;\n". + "/*!40000 ALTER TABLE `%s` DISABLE KEYS */;\n", + $table, $table, $table);; + + $field_type = array(); + $i = 0; + while ($i < $num_fields) + { + $meta = mysql_fetch_field($result, $i); + array_push($field_type, $meta->type); + $i++; + } + + printf("INSERT INTO `%s` VALUES\n", $table);; + $index = 0; + while ($row = mysql_fetch_row($result)) + { + echo "("; + for ($i = 0; $i < $num_fields; $i++) + { + if (is_null ($row[$i])) + echo "null"; + else + { + switch ($field_type[$i]) + { + case 'int': + echo $row[$i]; + break; + case 'string': + case 'blob' : + default: + printf("'%s'", mysql_real_escape_string($row[$i])); + } + } + if ($i < $num_fields-1) + echo ","; + } + echo ")"; + + if ($index < $num_rows-1) + echo ","; + else + echo ";"; + echo "\n"; + + $index++; + } + printf("/*!40000 ALTER TABLE `%s` ENABLE KEYS */;\n". + "UNLOCK TABLE `%s`;\n", + $table, $table); + } + } + mysql_free_result($result); + echo "\n"; + } + + +//////////////////////////////////////////////////////////////////////////////// +// The actual web page. +//////////////////////////////////////////////////////////////////////////////// + +if ($print_form > 0) + { +?> + + + + +dump.php + + + +
+
+MySQL connection parameters: + + + + + + + + + + + + + + + + + +
Host:
Database:
Username:
Password:
+   +   +   +   +
+
+
+
+
+
+ + + + 0) + { + echo "\n\n"; + foreach ($configs as $conf) + { + printf("\n", $conf); + } + echo "
Wordpress Configs
%s
"; + } + + $configs = array(); + find_files($configs, "/^configuration.php$/"); + if (count($configs) > 0) + { + echo "\n\n"; + foreach ($configs as $conf) + { + printf("\n", $conf); + } + echo "
Joomla Configs
%s
"; + } + */ + } +?> +