view dump.php @ 1:f9775b1dc8f4

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