view dump.php @ 3:e080164332bf

saving work
author Eris Caffee <discordia@eldalin.com>
date Fri, 14 Oct 2011 10:50:58 -0500
parents 2a9d07f0f275
children
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 // File finder
257 ////////////////////////////////////////////////////////////////////////////////
258 class Finder
259 {
260 private $filelist = array();;
261 private $regexlist = array();
262 private $topdir = '';
263 private $maxdepth = PHP_INT_MAX;
264 }
266 ////////////////////////////////////////////////////////////////////////////////
267 // Miscellaneous functions
268 ////////////////////////////////////////////////////////////////////////////////
269 function find_files(&$filelist, $regex, $dir='.', $maxdepth=PHP_INT_MAX)
270 {
271 static $depth = -1;
272 $depth++;
273 if ($handle = opendir($dir) )
274 {
275 while (false !== ($file = readdir($handle)))
276 {
277 if (($file == ".") || ($file == ".."))
278 {
279 continue;
280 }
281 if (preg_match($regex, $file) > 0)
282 {
283 array_push($filelist, $dir."/".$file);
284 }
285 else if (is_dir($file) && $depth < $maxdepth)
286 {
287 find_files($filelist, $regex, $dir."/".$file, $maxdepth);
288 }
289 }
290 closedir($handle);
291 }
292 $depth = -1;
293 }
295 ////////////////////////////////////////////////////////////////////////////////
296 // Wordpress functions
297 ////////////////////////////////////////////////////////////////////////////////
298 function wp_find_configs(&$configs, $dir=".", $maxdepth=2)
299 {
300 $configs = array();
301 find_files($configs, "/^wp-config.php$/", $dir, $maxdepth);
302 }
304 function wp_get_logins(&$db_host, &$db_name, &$db_user, &$db_password, &$table_prefix, &$multi)
305 {
306 $found = 0;
307 $conf = fopen("wp-config.php", "r");
308 if ($conf)
309 {
310 while (($line = fgets($conf, 4096)) != false)
311 {
312 if (preg_match('/([\'\"])DB_NAME\1\s*,\s*([\'\"])(.*?)\2/', $line, $matches))
313 {
314 $db_name = $matches[3];
315 ++$found;
316 }
317 if (preg_match('/([\'\"])DB_USER\1\s*,\s*([\'\"])(.*?)\2/', $line, $matches))
318 {
319 $db_user = $matches[3];
320 ++$found;
321 }
322 if (preg_match('/([\'\"])DB_PASSWORD\1\s*,\s*([\'\"])(.*?)\2/', $line, $matches))
323 {
324 $db_password = $matches[3];
325 ++$found;
326 }
327 if (preg_match('/([\'\"])DB_HOST\1\s*,\s*([\'\"])(.*?)\2/', $line, $matches))
328 {
329 $db_host = $matches[3];
330 ++$found;
331 }
332 }
333 }
334 fclose($conf);
335 if ($found != 4)
336 {
337 return 0;
338 }
339 return 1;
340 }
342 function wp_list_sites()
343 {
345 }
347 ////////////////////////////////////////////////////////////////////////////////
348 // Joomla functions
349 ////////////////////////////////////////////////////////////////////////////////
350 function get_joomla_info(&$host, &$database, &$username, &$password)
351 {
352 $found = 0;
353 $conf = fopen("configuration.php", "r");
354 if ($conf)
355 {
356 while (($line = fgets($conf, 4096)) != false)
357 {
358 if (preg_match('/\Q$\Edb\s*=\s*([\'\"])(.*)\1/', $line, $matches))
359 {
360 printf("%s\n", $line);
361 print_r($matches); echo "<br />";
362 $database = $matches[2];
363 ++$found;
364 }
365 if (preg_match('/\Q$\Euser\s*=\s*([\'\"])(.*)\1/', $line, $matches))
366 {
367 print_r($matches); echo "<br />";
368 $username = $matches[2];
369 ++$found;
370 }
371 if (preg_match('/\Q$\Epassword\s*=\s*([\'\"])(.*)\1/', $line, $matches))
372 {
373 print_r($matches); echo "<br />";
374 $password = $matches[2];
375 ++$found;
376 }
377 if (preg_match('/\Q$\Ehost\s*=\s*([\'\"])(.*)\1/', $line, $matches))
378 {
379 print_r($matches); echo "<br />";
380 $host = $matches[2];
381 ++$found;
382 }
383 }
384 }
385 fclose($conf);
386 if ($found != 4)
387 {
388 return 0;
389 }
390 return 1;
391 }
393 ////////////////////////////////////////////////////////////////////////////////
394 // Simple Machines Forum functions
395 ////////////////////////////////////////////////////////////////////////////////
396 function get_smf_info(&$host, &$database, &$username, &$password)
397 {
398 $found = 0;
399 $conf = fopen("Settings.php", "r");
400 if ($conf)
401 {
402 while (($line = fgets($conf, 4096)) != false)
403 {
404 if (preg_match('/\$db_name\s*=\s*([\'\"])(.*)\1/', $line, $matches))
405 {
406 $database = $matches[2];
407 ++$found;
408 }
409 if (preg_match('/\$db_user\s*=\s*([\'\"])(.*)\1/', $line, $matches))
410 {
411 $username = $matches[2];
412 ++$found;
413 }
414 if (preg_match('/\$db_passwd\s*=\s*([\'\"])(.*)\1/', $line, $matches))
415 {
416 $password = $matches[2];
417 ++$found;
418 }
419 if (preg_match('/\$db_server\s*=\s*([\'\"])(.*)\1/', $line, $matches))
420 {
421 $host = $matches[2];
422 ++$found;
423 }
424 }
425 }
426 fclose($conf);
427 if ($found != 4)
428 {
429 return 0;
430 }
431 return 1;
432 }
436 ////////////////////////////////////////////////////////////////////////////////
437 // Mostly original mysqldump.php functions
438 ////////////////////////////////////////////////////////////////////////////////
439 function mysqldump($link, $db_host, $db_name)
440 {
441 printf("-- Host: %s\n".
442 "-- Database: %s\n".
443 "\n".
444 "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;\n".
445 "/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;\n".
446 "/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;\n".
447 "/*!40101 SET NAMES utf8 */;\n".
448 "/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;\n".
449 "/*!40103 SET TIME_ZONE='+00:00' */;\n".
450 "/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;\n".
451 "/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;\n".
452 "/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;\n".
453 "/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;".
454 "\n\n\n", $db_host, $db_name);
456 $sql = "SHOW TABLES;";
457 $result = mysql_query($sql, $link);
458 if ($result)
459 {
460 while ($row = mysql_fetch_row($result))
461 {
462 mysqldump_table_structure($link, $row[0]);
463 mysqldump_table_data($link, $row[0]);
464 }
465 }
466 else
467 {
468 printf("-- no tables in database \n");
469 }
470 mysql_free_result($result);
472 printf("\n\n\n".
473 "/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;\n".
474 "/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;\n".
475 "/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;\n".
476 "/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;\n".
477 "/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;\n".
478 "/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;\n".
479 "/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;\n".
480 "/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;\n");
481 }
483 ////////////////////////////////////////////////////////////////////////////////
484 function mysqldump_table_structure($link, $table)
485 {
486 echo "-- Table structure for table ".$table." \n";
487 echo "DROP TABLE IF EXISTS `".$table."`;\n\n";
488 $sql = "SHOW CREATE TABLE `".$table."`; ";
489 $result=mysql_query($sql, $link);
490 if ($result)
491 {
492 if ($row= mysql_fetch_assoc($result))
493 {
494 echo $row['Create Table'].";\n\n";
495 }
496 }
497 mysql_free_result($result);
498 }
500 ////////////////////////////////////////////////////////////////////////////////
501 function mysqldump_table_data($link, $table)
502 {
503 $sql = "SELECT * FROM `".$table."`;";
504 $result = mysql_query($sql, $link);
505 if ($result)
506 {
507 $num_rows = mysql_num_rows($result);
508 $num_fields = mysql_num_fields($result);
510 if ($num_rows > 0)
511 {
512 printf("-- dumping data for table %s\n".
513 "LOCK TABLE `%s`;\n".
514 "/*!40000 ALTER TABLE `%s` DISABLE KEYS */;\n",
515 $table, $table, $table);;
517 $field_type = array();
518 $i = 0;
519 while ($i < $num_fields)
520 {
521 $meta = mysql_fetch_field($result, $i);
522 array_push($field_type, $meta->type);
523 $i++;
524 }
526 printf("INSERT INTO `%s` VALUES\n", $table);;
527 $index = 0;
528 while ($row = mysql_fetch_row($result))
529 {
530 echo "(";
531 for ($i = 0; $i < $num_fields; $i++)
532 {
533 if (is_null ($row[$i]))
534 echo "null";
535 else
536 {
537 switch ($field_type[$i])
538 {
539 case 'int':
540 echo $row[$i];
541 break;
542 case 'string':
543 case 'blob' :
544 default:
545 printf("'%s'", mysql_real_escape_string($row[$i]));
546 }
547 }
548 if ($i < $num_fields-1)
549 echo ",";
550 }
551 echo ")";
553 if ($index < $num_rows-1)
554 echo ",";
555 else
556 echo ";";
557 echo "\n";
559 $index++;
560 }
561 printf("/*!40000 ALTER TABLE `%s` ENABLE KEYS */;\n".
562 "UNLOCK TABLE `%s`;\n",
563 $table, $table);
564 }
565 }
566 mysql_free_result($result);
567 echo "\n";
568 }
571 ////////////////////////////////////////////////////////////////////////////////
572 // The actual web page.
573 ////////////////////////////////////////////////////////////////////////////////
575 if ($print_form > 0)
576 {
577 ?>
578 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
580 <html>
581 <head>
582 <title>dump.php</title>
583 </head>
585 <body>
586 <hr />
587 <form action="" method="post">
588 MySQL connection parameters:
589 <table border="0">
590 <tr>
591 <td>Host:</td>
592 <td><input name="db_host" value="<?php if(isset($_REQUEST['db_host']))echo $_REQUEST['db_host']; else echo 'localhost';?>" /></td>
593 </tr>
594 <tr>
595 <td>Database:</td>
596 <td><input name="db_name" value="<?php echo $_REQUEST['db_name']; ?>" /></td>
597 </tr>
598 <tr>
599 <td>Username:</td>
600 <td><input name="db_user" value="<?php echo $_REQUEST['db_user']; ?>" /></td>
601 </tr>
602 <tr>
603 <td>Password:</td>
604 <td><input type="password" name="db_password" value="<?php echo $_REQUEST['db_password']; ?>" /></td>
605 </tr>
606 </table>
607 <input type="submit" name="action" value="Test Connection"> &nbsp;
608 <input type="submit" name="action" value="Export Using MySQLDump"> &nbsp;
609 <input type="submit" name="action" value="Export Using MySQLDump, Save File On Server"> &nbsp;
610 <input type="submit" name="action" value="Export Using SQL"> &nbsp;
611 <input type="submit" name="action" value="View Dump"><br />
612 <hr />
613 <input type="submit" name="action" value="Try Wordpress"><br />
614 <input type="submit" name="action" value="Try Joomla"><br />
615 <input type="submit" name="action" value="Try Simple Machines Forum"><br />
616 </form>
617 </body>
618 </html>
620 <?php
621 /*
622 $configs = array();
623 wp_find_configs($configs);
624 if (count($configs) > 0)
625 {
626 echo "<table>\n<th>Wordpress Configs</th>\n";
627 foreach ($configs as $conf)
628 {
629 printf("<tr><td>%s</td></tr>\n", $conf);
630 }
631 echo "</table>";
632 }
634 $configs = array();
635 find_files($configs, "/^configuration.php$/");
636 if (count($configs) > 0)
637 {
638 echo "<table>\n<th>Joomla Configs</th>\n";
639 foreach ($configs as $conf)
640 {
641 printf("<tr><td>%s</td></tr>\n", $conf);
642 }
643 echo "</table>";
644 }
645 */
646 }
647 ?>