view dump.php @ 0:45012f7a69aa

Initial commit
author Eris Caffee <discordia@eldalin.com>
date Thu, 29 Sep 2011 13:28:04 -0500
parents
children f9775b1dc8f4
line source
1 <?php
2 //
3 // TODO
4 //
5 // Object oriented design. A class for the mysql connection and dumping.
6 // A class for each supported script.
7 //
8 // A method to post the form.
9 //
10 // A way to drive from command line via wget. (Might need to switch to GET form
11 //
12 //
13 //
15 set_time_limit(0);
17 $print_form = 1;
19 //test mysql connection
20 if( isset($_REQUEST['action']) )
21 {
22 $db_host=$_REQUEST['db_host'];
23 $db_name=$_REQUEST['db_name'];
24 $db_user=$_REQUEST['db_user'];
25 $db_password=$_REQUEST['db_password'];
26 if ('Test Connection' == $_REQUEST['action'])
27 {
28 $retval = mysql_test($db_host,$db_name, $db_user, $db_password);
29 if ($retval)
30 {
31 echo "Success!"."<br>";
32 }
33 else
34 {
35 echo "Unable to connect."."<br />";
36 }
37 }
38 else if ('Export Using MySQLDump' == $_REQUEST['action'])
39 {
40 $retval = mysql_test($db_host, $db_name, $db_user, $db_password);
41 if ($retval)
42 {
43 $print_form=0;
44 send_dump($db_host, $db_name, $db_user, $db_password);
45 }
46 else
47 {
48 echo "Unable to connect."."<br />";
49 }
50 }
51 else if ('Export Using MySQLDump, Save File On Server' == $_REQUEST['action'])
52 {
53 $retval = mysql_test($db_host, $db_name, $db_user, $db_password);
54 if ($retval)
55 {
56 $print_form=0;
57 save_dump($db_host, $db_name, $db_user, $db_password);
58 }
59 else
60 {
61 echo "Unable to connect."."<br />";
62 }
63 }
64 else if ('Export Using SQL' == $_REQUEST['action'])
65 {
66 $link = mysql_login($db_host, $db_name, $db_user, $db_password);
67 if ($link)
68 {
69 $print_form=0;
70 send_dump_old($link, $db_host, $db_name);
71 }
72 else
73 {
74 echo "Unable to connect."."<br />";
75 }
76 }
77 else if ('View Dump' == $_REQUEST['action'])
78 {
79 $link = mysql_login($db_host, $db_name, $db_user, $db_password);
80 if ($link)
81 {
82 $print_form=0;
83 view_dump($link, $db_host, $db_name);
84 }
85 else
86 {
87 echo "Unable to connect."."<br />";
88 }
89 }
90 else if ('Try Wordpress' == $_REQUEST['action'])
91 {
92 $found = wp_get_logins($db_host, $db_name, $db_user, $db_password, $table_prefix, $multi);
93 if ($found)
94 {
95 $retval = mysql_test($db_host, $db_name, $db_user, $db_password);
96 if ($retval)
97 {
98 echo "Success!"."<br>";
99 $_REQUEST['db_host']=$db_host;
100 $_REQUEST['db_name']=$db_name;
101 $_REQUEST['db_user']=$db_user;
102 $_REQUEST['db_password']=$db_password;
103 }
104 }
105 else
106 {
107 echo "Wordpress not found"."<br />";
108 }
109 }
110 else if ('Try Joomla' == $_REQUEST['action'])
111 {
112 $found = get_joomla_info($db_host, $db_name, $db_user, $db_password);
113 if ($found)
114 {
115 $retval = mysql_test($db_host,$db_name, $db_user, $db_password);
116 if ($retval)
117 {
118 echo "Success!"."<br>";
119 $_REQUEST['db_host']=$db_host;
120 $_REQUEST['db_name']=$db_name;
121 $_REQUEST['db_user']=$db_user;
122 $_REQUEST['db_password']=$db_password;
123 }
124 else
125 {
126 printf("Unable to connect using<br />host: %s<br />database: %s<br />user:%s<br />password: %s<br />", $db_host, $db_name, $db_user, $db_password);
127 }
128 }
129 else
130 {
131 echo "Joomla not found"."<br />";
132 }
133 }
134 else if ('Try Simple Machines Forum' == $_REQUEST['action'])
135 {
136 $found = get_smf_info($db_host, $db_name, $db_user, $db_password);
137 if ($found)
138 {
139 $retval = mysql_test($db_host,$db_name, $db_user, $db_password);
140 if ($retval)
141 {
142 echo "Success!"."<br>";
143 $_REQUEST['db_host']=$db_host;
144 $_REQUEST['db_name']=$db_name;
145 $_REQUEST['db_user']=$db_user;
146 $_REQUEST['db_password']=$db_password;
147 }
148 else
149 {
150 printf("Unable to connect using<br />host: %s<br />database: %s<br />user:%s<br />password: %s<br />", $db_host, $db_name, $db_user, $db_password);
151 }
152 }
153 else
154 {
155 echo "Simple Machines Forum not found"."<br />";
156 }
157 }
158 }
160 ////////////////////////////////////////////////////////////////////////////////
161 function send_dump($db_host, $db_name, $db_user, $db_password)
162 {
163 $dumpname = $db_host."_".$db_name."-".date("Y\.m\.d\_H\.i\.s").".sql";
164 header('Content-type: text/plain;charset=UTF-8');
165 header('Content-Disposition: attachment; filename="'.$dumpname.'"');
166 passthru("mysqldump --opt -u'".$db_user."' -p'".$db_password."' -h'".$db_host."' '".$db_name."'");
167 }
169 ////////////////////////////////////////////////////////////////////////////////
170 function save_dump($db_host, $db_name, $db_user, $db_password)
171 {
172 $dumpname = $db_host."_".$db_name."-".date("Y\.m\.d\_H\.i\.s").".sql";
173 echo "<p>Saving dump to ".$dumpname."</p>";
174 passthru("mysqldump --opt -u'".$db_user."' -p'".$db_password."' -h'".$db_host."' '".$db_name."' > ".$dumpname);
175 }
177 ////////////////////////////////////////////////////////////////////////////////
178 function send_dump_old($link, $db_host, $db_name)
179 {
180 $dumpname = $db_host."_".$db_name."-".date("Y\.m\.d\_H\.i\.s").".sql";
181 header('Content-type: text/plain;charset=UTF-8');
182 header('Content-Disposition: attachment; filename="'.$dumpname.'"');
183 mysqldump($link, $db_host, $db_name);
184 }
186 ////////////////////////////////////////////////////////////////////////////////
187 function view_dump($link, $db_host, $db_name)
188 {
189 header('Content-type: text/plain;charset=UTF-8');
190 mysqldump($link, $db_host, $db_name);
191 }
193 ////////////////////////////////////////////////////////////////////////////////
194 function mysql_login($db_host, $db_name, $db_user, $db_password)
195 {
196 $link = mysql_connect($db_host, $db_user, $db_password);
197 if ($link)
198 {
199 $db_selected = mysql_select_db($db_name, $link);
200 if ($db_selected)
201 {
202 mysql_query("SET NAMES utf8;", $link);
203 }
204 else
205 {
206 printf("Connected, but unable to select database using<br />host: %s<br />database: %s<br />user:%s<br />password: %s<br />", $db_host, $db_name, $db_user, $db_password);
207 mysql_close($link);
208 $link = FALSE;
209 }
210 }
211 else
212 {
213 printf("Unable to connect using<br />host: %s<br />database: %s<br />user:%s<br />password: %s<br />", $db_host, $db_name, $db_user, $db_password);
214 mysql_close($link);
215 $link = FALSE;
216 }
217 return $link;
218 }
220 ////////////////////////////////////////////////////////////////////////////////
221 function mysql_test($db_host, $db_name, $db_user, $db_password)
222 {
223 $retval = 0;
224 $link = mysql_login($db_host, $db_name, $db_user, $db_password);
225 if ($link)
226 {
227 mysql_close($link);
228 $retval = 1;
229 }
230 return $retval;
231 }
233 ////////////////////////////////////////////////////////////////////////////////
234 // Miscellaneous functions
235 ////////////////////////////////////////////////////////////////////////////////
236 function find_files(&$filelist, $regex, $dir='.', $maxdepth=PHP_INT_MAX)
237 {
238 static $depth = -1;
239 $depth++;
240 if ($handle = opendir($dir) )
241 {
242 while (false !== ($file = readdir($handle)))
243 {
244 if (($file == ".") || ($file == ".."))
245 {
246 continue;
247 }
248 if (preg_match($regex, $file) > 0)
249 {
250 array_push($filelist, $dir."/".$file);
251 }
252 else if (is_dir($file) && $depth < $maxdepth)
253 {
254 find_files($filelist, $regex, $dir."/".$file, $maxdepth);
255 }
256 }
257 closedir($handle);
258 }
259 $depth = -1;
260 }
262 ////////////////////////////////////////////////////////////////////////////////
263 // Wordpress functions
264 ////////////////////////////////////////////////////////////////////////////////
265 function wp_find_configs(&$configs, $dir=".", $maxdepth=2)
266 {
267 $configs = array();
268 find_files($configs, "/^wp-config.php$/", $dir, $maxdepth);
269 }
271 function wp_get_logins(&$db_host, &$db_name, &$db_user, &$db_password, &$table_prefix, &$multi)
272 {
273 $found = 0;
274 $conf = fopen("wp-config.php", "r");
275 if ($conf)
276 {
277 while (($line = fgets($conf, 4096)) != false)
278 {
279 if (preg_match('/([\'\"])DB_NAME\1\s*,\s*([\'\"])(.*?)\2/', $line, $matches))
280 {
281 $db_name = $matches[3];
282 ++$found;
283 }
284 if (preg_match('/([\'\"])DB_USER\1\s*,\s*([\'\"])(.*?)\2/', $line, $matches))
285 {
286 $db_user = $matches[3];
287 ++$found;
288 }
289 if (preg_match('/([\'\"])DB_PASSWORD\1\s*,\s*([\'\"])(.*?)\2/', $line, $matches))
290 {
291 $db_password = $matches[3];
292 ++$found;
293 }
294 if (preg_match('/([\'\"])DB_HOST\1\s*,\s*([\'\"])(.*?)\2/', $line, $matches))
295 {
296 $db_host = $matches[3];
297 ++$found;
298 }
299 }
300 }
301 fclose($conf);
302 if ($found != 4)
303 {
304 return 0;
305 }
306 return 1;
307 }
309 function wp_list_sites()
310 {
312 }
314 ////////////////////////////////////////////////////////////////////////////////
315 // Joomla functions
316 ////////////////////////////////////////////////////////////////////////////////
317 function get_joomla_info(&$host, &$database, &$username, &$password)
318 {
319 $found = 0;
320 $conf = fopen("configuration.php", "r");
321 if ($conf)
322 {
323 while (($line = fgets($conf, 4096)) != false)
324 {
325 if (preg_match('/\Q$\Edb\s*=\s*([\'\"])(.*)\1/', $line, $matches))
326 {
327 printf("%s\n", $line);
328 print_r($matches); echo "<br />";
329 $database = $matches[2];
330 ++$found;
331 }
332 if (preg_match('/\Q$\Euser\s*=\s*([\'\"])(.*)\1/', $line, $matches))
333 {
334 print_r($matches); echo "<br />";
335 $username = $matches[2];
336 ++$found;
337 }
338 if (preg_match('/\Q$\Epassword\s*=\s*([\'\"])(.*)\1/', $line, $matches))
339 {
340 print_r($matches); echo "<br />";
341 $password = $matches[2];
342 ++$found;
343 }
344 if (preg_match('/\Q$\Ehost\s*=\s*([\'\"])(.*)\1/', $line, $matches))
345 {
346 print_r($matches); echo "<br />";
347 $host = $matches[2];
348 ++$found;
349 }
350 }
351 }
352 fclose($conf);
353 if ($found != 4)
354 {
355 return 0;
356 }
357 return 1;
358 }
360 ////////////////////////////////////////////////////////////////////////////////
361 // Simple Machines Forum functions
362 ////////////////////////////////////////////////////////////////////////////////
363 function get_smf_info(&$host, &$database, &$username, &$password)
364 {
365 $found = 0;
366 $conf = fopen("Settings.php", "r");
367 if ($conf)
368 {
369 while (($line = fgets($conf, 4096)) != false)
370 {
371 if (preg_match('/\$db_name\s*=\s*([\'\"])(.*)\1/', $line, $matches))
372 {
373 $database = $matches[2];
374 ++$found;
375 }
376 if (preg_match('/\$db_user\s*=\s*([\'\"])(.*)\1/', $line, $matches))
377 {
378 $username = $matches[2];
379 ++$found;
380 }
381 if (preg_match('/\$db_passwd\s*=\s*([\'\"])(.*)\1/', $line, $matches))
382 {
383 $password = $matches[2];
384 ++$found;
385 }
386 if (preg_match('/\$db_server\s*=\s*([\'\"])(.*)\1/', $line, $matches))
387 {
388 $host = $matches[2];
389 ++$found;
390 }
391 }
392 }
393 fclose($conf);
394 if ($found != 4)
395 {
396 return 0;
397 }
398 return 1;
399 }
403 ////////////////////////////////////////////////////////////////////////////////
404 // Mostly original mysqldump.php functions
405 ////////////////////////////////////////////////////////////////////////////////
406 function mysqldump($link, $db_host, $db_name)
407 {
408 printf("-- Host: %s\n".
409 "-- Database: %s\n".
410 "\n".
411 "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;\n".
412 "/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;\n".
413 "/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;\n".
414 "/*!40101 SET NAMES utf8 */;\n".
415 "/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;\n".
416 "/*!40103 SET TIME_ZONE='+00:00' */;\n".
417 "/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;\n".
418 "/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;\n".
419 "/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;\n".
420 "/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;".
421 "\n\n\n", $db_host, $db_name);
423 $sql = "SHOW TABLES;";
424 $result = mysql_query($sql, $link);
425 if ($result)
426 {
427 while ($row = mysql_fetch_row($result))
428 {
429 mysqldump_table_structure($link, $row[0]);
430 mysqldump_table_data($link, $row[0]);
431 }
432 }
433 else
434 {
435 printf("-- no tables in database \n");
436 }
437 mysql_free_result($result);
439 printf("\n\n\n".
440 "/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;\n".
441 "/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;\n".
442 "/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;\n".
443 "/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;\n".
444 "/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;\n".
445 "/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;\n".
446 "/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;\n".
447 "/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;\n");
448 }
450 ////////////////////////////////////////////////////////////////////////////////
451 function mysqldump_table_structure($link, $table)
452 {
453 echo "-- Table structure for table ".$table." \n";
454 echo "DROP TABLE IF EXISTS `".$table."`;\n\n";
455 $sql = "SHOW CREATE TABLE `".$table."`; ";
456 $result=mysql_query($sql, $link);
457 if ($result)
458 {
459 if ($row= mysql_fetch_assoc($result))
460 {
461 echo $row['Create Table'].";\n\n";
462 }
463 }
464 mysql_free_result($result);
465 }
467 ////////////////////////////////////////////////////////////////////////////////
468 function mysqldump_table_data($link, $table)
469 {
470 $sql = "SELECT * FROM `".$table."`;";
471 $result = mysql_query($sql, $link);
472 if ($result)
473 {
474 $num_rows = mysql_num_rows($result);
475 $num_fields = mysql_num_fields($result);
477 if ($num_rows > 0)
478 {
479 printf("-- dumping data for table %s\n".
480 "LOCK TABLE `%s`;\n".
481 "/*!40000 ALTER TABLE `%s` DISABLE KEYS */;\n",
482 $table, $table, $table);;
484 $field_type = array();
485 $i = 0;
486 while ($i < $num_fields)
487 {
488 $meta = mysql_fetch_field($result, $i);
489 array_push($field_type, $meta->type);
490 $i++;
491 }
493 printf("INSERT INTO `%s` VALUES\n", $table);;
494 $index = 0;
495 while ($row = mysql_fetch_row($result))
496 {
497 echo "(";
498 for ($i = 0; $i < $num_fields; $i++)
499 {
500 if (is_null ($row[$i]))
501 echo "null";
502 else
503 {
504 switch ($field_type[$i])
505 {
506 case 'int':
507 echo $row[$i];
508 break;
509 case 'string':
510 case 'blob' :
511 default:
512 printf("'%s'", mysql_real_escape_string($row[$i]));
513 }
514 }
515 if ($i < $num_fields-1)
516 echo ",";
517 }
518 echo ")";
520 if ($index < $num_rows-1)
521 echo ",";
522 else
523 echo ";";
524 echo "\n";
526 $index++;
527 }
528 printf("/*!40000 ALTER TABLE `%s` ENABLE KEYS */;\n".
529 "UNLOCK TABLE `%s`;\n",
530 $table, $table);
531 }
532 }
533 mysql_free_result($result);
534 echo "\n";
535 }
538 ////////////////////////////////////////////////////////////////////////////////
539 // The actual web page.
540 ////////////////////////////////////////////////////////////////////////////////
542 if ($print_form > 0)
543 {
544 ?>
545 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
547 <html>
548 <head>
549 <title>dump.php</title>
550 </head>
552 <body>
553 <hr />
554 <form action="" method="post">
555 MySQL connection parameters:
556 <table border="0">
557 <tr>
558 <td>Host:</td>
559 <td><input name="db_host" value="<?php if(isset($_REQUEST['db_host']))echo $_REQUEST['db_host']; else echo 'localhost';?>" /></td>
560 </tr>
561 <tr>
562 <td>Database:</td>
563 <td><input name="db_name" value="<?php echo $_REQUEST['db_name']; ?>" /></td>
564 </tr>
565 <tr>
566 <td>Username:</td>
567 <td><input name="db_user" value="<?php echo $_REQUEST['db_user']; ?>" /></td>
568 </tr>
569 <tr>
570 <td>Password:</td>
571 <td><input type="password" name="db_password" value="<?php echo $_REQUEST['db_password']; ?>" /></td>
572 </tr>
573 </table>
574 <input type="submit" name="action" value="Test Connection"> &nbsp;
575 <input type="submit" name="action" value="Export Using MySQLDump"> &nbsp;
576 <input type="submit" name="action" value="Export Using MySQLDump, Save File On Server"> &nbsp;
577 <input type="submit" name="action" value="Export Using SQL"> &nbsp;
578 <input type="submit" name="action" value="View Dump"><br />
579 <hr />
580 <input type="submit" name="action" value="Try Wordpress"><br />
581 <input type="submit" name="action" value="Try Joomla"><br />
582 <input type="submit" name="action" value="Try Simple Machines Forum"><br />
583 </form>
584 </body>
585 </html>
587 <?php
588 /*
589 $configs = array();
590 wp_find_configs($configs);
591 if (count($configs) > 0)
592 {
593 echo "<table>\n<th>Wordpress Configs</th>\n";
594 foreach ($configs as $conf)
595 {
596 printf("<tr><td>%s</td></tr>\n", $conf);
597 }
598 echo "</table>";
599 }
601 $configs = array();
602 find_files($configs, "/^configuration.php$/");
603 if (count($configs) > 0)
604 {
605 echo "<table>\n<th>Joomla Configs</th>\n";
606 foreach ($configs as $conf)
607 {
608 printf("<tr><td>%s</td></tr>\n", $conf);
609 }
610 echo "</table>";
611 }
612 */
613 }
614 ?>