view dump.php @ 2:2a9d07f0f275

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