1 rizwank 1.1 <?php
2 /***************************************************************************
3 * admin_db_utilities.php
4 * -------------------
5 * begin : Thu May 31, 2001
6 * copyright : (C) 2001 The phpBB Group
7 * email : support@phpbb.com
8 *
9 * $Id: admin_db_utilities.php,v 1.42.2.9 2002/12/21 19:09:56 psotfx Exp $
10 *
11 ****************************************************************************/
12
13 /***************************************************************************
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 ***************************************************************************/
21
22 rizwank 1.1 /***************************************************************************
23 * We will attempt to create a file based backup of all of the data in the
24 * users phpBB database. The resulting file should be able to be imported by
25 * the db_restore.php function, or by using the mysql command_line
26 *
27 * Some functions are adapted from the upgrade_20.php script and others
28 * adapted from the unoficial phpMyAdmin 2.2.0.
29 ***************************************************************************/
30
31 define('IN_PHPBB', 1);
32
33 if( !empty($setmodules) )
34 {
35 $filename = basename(__FILE__);
36 $module['General']['Backup_DB'] = $filename . "?perform=backup";
37
38 $file_uploads = (@phpversion() >= '4.0.0') ? @ini_get('file_uploads') : @get_cfg_var('file_uploads');
39
40 if( (empty($file_uploads) || $file_uploads != 0) && (strtolower($file_uploads) != 'off') && (@phpversion() != '4.0.4pl1') )
41 {
42 $module['General']['Restore_DB'] = $filename . "?perform=restore";
43 rizwank 1.1 }
44
45 return;
46 }
47
48 //
49 // Load default header
50 //
51 $no_page_header = TRUE;
52 $phpbb_root_path = "./../";
53 require($phpbb_root_path . 'extension.inc');
54 require('./pagestart.' . $phpEx);
55 include($phpbb_root_path . 'includes/sql_parse.'.$phpEx);
56
57 //
58 // Set VERBOSE to 1 for debugging info..
59 //
60 define("VERBOSE", 0);
61
62 //
63 // Increase maximum execution time, but don't complain about it if it isn't
64 rizwank 1.1 // allowed.
65 //
66 @set_time_limit(1200);
67
68 // -----------------------
69 // The following functions are adapted from phpMyAdmin and upgrade_20.php
70 //
71 function gzip_PrintFourChars($Val)
72 {
73 for ($i = 0; $i < 4; $i ++)
74 {
75 $return .= chr($Val % 256);
76 $Val = floor($Val / 256);
77 }
78 return $return;
79 }
80
81
82
83 //
84 // This function is used for grabbing the sequences for postgres...
85 rizwank 1.1 //
86 function pg_get_sequences($crlf, $backup_type)
87 {
88 global $db;
89
90 $get_seq_sql = "SELECT relname FROM pg_class WHERE NOT relname ~ 'pg_.*'
91 AND relkind = 'S' ORDER BY relname";
92
93 $seq = $db->sql_query($get_seq_sql);
94
95 if( !$num_seq = $db->sql_numrows($seq) )
96 {
97
98 $return_val = "# No Sequences Found $crlf";
99
100 }
101 else
102 {
103 $return_val = "# Sequences $crlf";
104 $i_seq = 0;
105
106 rizwank 1.1 while($i_seq < $num_seq)
107 {
108 $row = $db->sql_fetchrow($seq);
109 $sequence = $row['relname'];
110
111 $get_props_sql = "SELECT * FROM $sequence";
112 $seq_props = $db->sql_query($get_props_sql);
113
114 if($db->sql_numrows($seq_props) > 0)
115 {
116 $row1 = $db->sql_fetchrow($seq_props);
117
118 if($backup_type == 'structure')
119 {
120 $row['last_value'] = 1;
121 }
122
123 $return_val .= "CREATE SEQUENCE $sequence start " . $row['last_value'] . ' increment ' . $row['increment_by'] . ' maxvalue ' . $row['max_value'] . ' minvalue ' . $row['min_value'] . ' cache ' . $row['cache_value'] . "; $crlf";
124
125 } // End if numrows > 0
126
127 rizwank 1.1 if(($row['last_value'] > 1) && ($backup_type != 'structure'))
128 {
129 $return_val .= "SELECT NEXTVALE('$sequence'); $crlf";
130 unset($row['last_value']);
131 }
132
133 $i_seq++;
134
135 } // End while..
136
137 } // End else...
138
139 return $returnval;
140
141 } // End function...
142
143 //
144 // The following functions will return the "CREATE TABLE syntax for the
145 // varying DBMS's
146 //
147 // This function returns, will return the table def's for postgres...
148 rizwank 1.1 //
149 function get_table_def_postgresql($table, $crlf)
150 {
151 global $drop, $db;
152
153 $schema_create = "";
154 //
155 // Get a listing of the fields, with their associated types, etc.
156 //
157
158 $field_query = "SELECT a.attnum, a.attname AS field, t.typname as type, a.attlen AS length, a.atttypmod as lengthvar, a.attnotnull as notnull
159 FROM pg_class c, pg_attribute a, pg_type t
160 WHERE c.relname = '$table'
161 AND a.attnum > 0
162 AND a.attrelid = c.oid
163 AND a.atttypid = t.oid
164 ORDER BY a.attnum";
165 $result = $db->sql_query($field_query);
166
167 if(!$result)
168 {
169 rizwank 1.1 message_die(GENERAL_ERROR, "Failed in get_table_def (show fields)", "", __LINE__, __FILE__, $field_query);
170 } // end if..
171
172 if ($drop == 1)
173 {
174 $schema_create .= "DROP TABLE $table;$crlf";
175 } // end if
176
177 //
178 // Ok now we actually start building the SQL statements to restore the tables
179 //
180
181 $schema_create .= "CREATE TABLE $table($crlf";
182
183 while ($row = $db->sql_fetchrow($result))
184 {
185 //
186 // Get the data from the table
187 //
188 $sql_get_default = "SELECT d.adsrc AS rowdefault
189 FROM pg_attrdef d, pg_class c
190 rizwank 1.1 WHERE (c.relname = '$table')
191 AND (c.oid = d.adrelid)
192 AND d.adnum = " . $row['attnum'];
193 $def_res = $db->sql_query($sql_get_default);
194
195 if (!$def_res)
196 {
197 unset($row['rowdefault']);
198 }
199 else
200 {
201 $row['rowdefault'] = @pg_result($def_res, 0, 'rowdefault');
202 }
203
204 if ($row['type'] == 'bpchar')
205 {
206 // Internally stored as bpchar, but isn't accepted in a CREATE TABLE statement.
207 $row['type'] = 'char';
208 }
209
210 $schema_create .= ' ' . $row['field'] . ' ' . $row['type'];
211 rizwank 1.1
212 if (eregi('char', $row['type']))
213 {
214 if ($row['lengthvar'] > 0)
215 {
216 $schema_create .= '(' . ($row['lengthvar'] -4) . ')';
217 }
218 }
219
220 if (eregi('numeric', $row['type']))
221 {
222 $schema_create .= '(';
223 $schema_create .= sprintf("%s,%s", (($row['lengthvar'] >> 16) & 0xffff), (($row['lengthvar'] - 4) & 0xffff));
224 $schema_create .= ')';
225 }
226
227 if (!empty($row['rowdefault']))
228 {
229 $schema_create .= ' DEFAULT ' . $row['rowdefault'];
230 }
231
232 rizwank 1.1 if ($row['notnull'] == 't')
233 {
234 $schema_create .= ' NOT NULL';
235 }
236
237 $schema_create .= ",$crlf";
238
239 }
240 //
241 // Get the listing of primary keys.
242 //
243
244 $sql_pri_keys = "SELECT ic.relname AS index_name, bc.relname AS tab_name, ta.attname AS column_name, i.indisunique AS unique_key, i.indisprimary AS primary_key
245 FROM pg_class bc, pg_class ic, pg_index i, pg_attribute ta, pg_attribute ia
246 WHERE (bc.oid = i.indrelid)
247 AND (ic.oid = i.indexrelid)
248 AND (ia.attrelid = i.indexrelid)
249 AND (ta.attrelid = bc.oid)
250 AND (bc.relname = '$table')
251 AND (ta.attrelid = i.indrelid)
252 AND (ta.attnum = i.indkey[ia.attnum-1])
253 rizwank 1.1 ORDER BY index_name, tab_name, column_name ";
254 $result = $db->sql_query($sql_pri_keys);
255
256 if(!$result)
257 {
258 message_die(GENERAL_ERROR, "Failed in get_table_def (show fields)", "", __LINE__, __FILE__, $sql_pri_keys);
259 }
260
261 while ( $row = $db->sql_fetchrow($result))
262 {
263 if ($row['primary_key'] == 't')
264 {
265 if (!empty($primary_key))
266 {
267 $primary_key .= ', ';
268 }
269
270 $primary_key .= $row['column_name'];
271 $primary_key_name = $row['index_name'];
272
273 }
274 rizwank 1.1 else
275 {
276 //
277 // We have to store this all this info because it is possible to have a multi-column key...
278 // we can loop through it again and build the statement
279 //
280 $index_rows[$row['index_name']]['table'] = $table;
281 $index_rows[$row['index_name']]['unique'] = ($row['unique_key'] == 't') ? ' UNIQUE ' : '';
282 $index_rows[$row['index_name']]['column_names'] .= $row['column_name'] . ', ';
283 }
284 }
285
286 if (!empty($index_rows))
287 {
288 while(list($idx_name, $props) = each($index_rows))
289 {
290 $props['column_names'] = ereg_replace(", $", "" , $props['column_names']);
291 $index_create .= 'CREATE ' . $props['unique'] . " INDEX $idx_name ON $table (" . $props['column_names'] . ");$crlf";
292 }
293 }
294
295 rizwank 1.1 if (!empty($primary_key))
296 {
297 $schema_create .= " CONSTRAINT $primary_key_name PRIMARY KEY ($primary_key),$crlf";
298 }
299
300 //
301 // Generate constraint clauses for CHECK constraints
302 //
303 $sql_checks = "SELECT rcname as index_name, rcsrc
304 FROM pg_relcheck, pg_class bc
305 WHERE rcrelid = bc.oid
306 AND bc.relname = '$table'
307 AND NOT EXISTS (
308 SELECT *
309 FROM pg_relcheck as c, pg_inherits as i
310 WHERE i.inhrelid = pg_relcheck.rcrelid
311 AND c.rcname = pg_relcheck.rcname
312 AND c.rcsrc = pg_relcheck.rcsrc
313 AND c.rcrelid = i.inhparent
314 )";
315 $result = $db->sql_query($sql_checks);
316 rizwank 1.1
317 if (!$result)
318 {
319 message_die(GENERAL_ERROR, "Failed in get_table_def (show fields)", "", __LINE__, __FILE__, $sql_checks);
320 }
321
322 //
323 // Add the constraints to the sql file.
324 //
325 while ($row = $db->sql_fetchrow($result))
326 {
327 $schema_create .= ' CONSTRAINT ' . $row['index_name'] . ' CHECK ' . $row['rcsrc'] . ",$crlf";
328 }
329
330 $schema_create = ereg_replace(',' . $crlf . '$', '', $schema_create);
331 $index_create = ereg_replace(',' . $crlf . '$', '', $index_create);
332
333 $schema_create .= "$crlf);$crlf";
334
335 if (!empty($index_create))
336 {
337 rizwank 1.1 $schema_create .= $index_create;
338 }
339
340 //
341 // Ok now we've built all the sql return it to the calling function.
342 //
343 return (stripslashes($schema_create));
344
345 }
346
347 //
348 // This function returns the "CREATE TABLE" syntax for mysql dbms...
349 //
350 function get_table_def_mysql($table, $crlf)
351 {
352 global $drop, $db;
353
354 $schema_create = "";
355 $field_query = "SHOW FIELDS FROM $table";
356 $key_query = "SHOW KEYS FROM $table";
357
358 rizwank 1.1 //
359 // If the user has selected to drop existing tables when doing a restore.
360 // Then we add the statement to drop the tables....
361 //
362 if ($drop == 1)
363 {
364 $schema_create .= "DROP TABLE IF EXISTS $table;$crlf";
365 }
366
367 $schema_create .= "CREATE TABLE $table($crlf";
368
369 //
370 // Ok lets grab the fields...
371 //
372 $result = $db->sql_query($field_query);
373 if(!$result)
374 {
375 message_die(GENERAL_ERROR, "Failed in get_table_def (show fields)", "", __LINE__, __FILE__, $field_query);
376 }
377
378 while ($row = $db->sql_fetchrow($result))
379 rizwank 1.1 {
380 $schema_create .= ' ' . $row['Field'] . ' ' . $row['Type'];
381
382 if(!empty($row['Default']))
383 {
384 $schema_create .= ' DEFAULT \'' . $row['Default'] . '\'';
385 }
386
387 if($row['Null'] != "YES")
388 {
389 $schema_create .= ' NOT NULL';
390 }
391
392 if($row['Extra'] != "")
393 {
394 $schema_create .= ' ' . $row['Extra'];
395 }
396
397 $schema_create .= ",$crlf";
398 }
399 //
400 rizwank 1.1 // Drop the last ',$crlf' off ;)
401 //
402 $schema_create = ereg_replace(',' . $crlf . '$', "", $schema_create);
403
404 //
405 // Get any Indexed fields from the database...
406 //
407 $result = $db->sql_query($key_query);
408 if(!$result)
409 {
410 message_die(GENERAL_ERROR, "FAILED IN get_table_def (show keys)", "", __LINE__, __FILE__, $key_query);
411 }
412
413 while($row = $db->sql_fetchrow($result))
414 {
415 $kname = $row['Key_name'];
416
417 if(($kname != 'PRIMARY') && ($row['Non_unique'] == 0))
418 {
419 $kname = "UNIQUE|$kname";
420 }
421 rizwank 1.1
422 if(!is_array($index[$kname]))
423 {
424 $index[$kname] = array();
425 }
426
427 $index[$kname][] = $row['Column_name'];
428 }
429
430 while(list($x, $columns) = @each($index))
431 {
432 $schema_create .= ", $crlf";
433
434 if($x == 'PRIMARY')
435 {
436 $schema_create .= ' PRIMARY KEY (' . implode($columns, ', ') . ')';
437 }
438 elseif (substr($x,0,6) == 'UNIQUE')
439 {
440 $schema_create .= ' UNIQUE ' . substr($x,7) . ' (' . implode($columns, ', ') . ')';
441 }
442 rizwank 1.1 else
443 {
444 $schema_create .= " KEY $x (" . implode($columns, ', ') . ')';
445 }
446 }
447
448 $schema_create .= "$crlf);";
449
450 if(get_magic_quotes_runtime())
451 {
452 return(stripslashes($schema_create));
453 }
454 else
455 {
456 return($schema_create);
457 }
458
459 } // End get_table_def_mysql
460
461
462 //
463 rizwank 1.1 // This fuction will return a tables create definition to be used as an sql
464 // statement.
465 //
466 //
467 // The following functions Get the data from the tables and format it as a
468 // series of INSERT statements, for each different DBMS...
469 // After every row a custom callback function $handler gets called.
470 // $handler must accept one parameter ($sql_insert);
471 //
472 //
473 // Here is the function for postgres...
474 //
475 function get_table_content_postgresql($table, $handler)
476 {
477 global $db;
478
479 //
480 // Grab all of the data from current table.
481 //
482
483 $result = $db->sql_query("SELECT * FROM $table");
484 rizwank 1.1
485 if (!$result)
486 {
487 message_die(GENERAL_ERROR, "Failed in get_table_content (select *)", "", __LINE__, __FILE__, "SELECT * FROM $table");
488 }
489
490 $i_num_fields = $db->sql_numfields($result);
491
492 for ($i = 0; $i < $i_num_fields; $i++)
493 {
494 $aryType[] = $db->sql_fieldtype($i, $result);
495 $aryName[] = $db->sql_fieldname($i, $result);
496 }
497
498 $iRec = 0;
499
500 while($row = $db->sql_fetchrow($result))
501 {
502 unset($schema_vals);
503 unset($schema_fields);
504 unset($schema_insert);
505 rizwank 1.1 //
506 // Build the SQL statement to recreate the data.
507 //
508 for($i = 0; $i < $i_num_fields; $i++)
509 {
510 $strVal = $row[$aryName[$i]];
511 if (eregi("char|text|bool", $aryType[$i]))
512 {
513 $strQuote = "'";
514 $strEmpty = "";
515 $strVal = addslashes($strVal);
516 }
517 elseif (eregi("date|timestamp", $aryType[$i]))
518 {
519 if ($empty($strVal))
520 {
521 $strQuote = "";
522 }
523 else
524 {
525 $strQuote = "'";
526 rizwank 1.1 }
527 }
528 else
529 {
530 $strQuote = "";
531 $strEmpty = "NULL";
532 }
533
534 if (empty($strVal) && $strVal != "0")
535 {
536 $strVal = $strEmpty;
537 }
538
539 $schema_vals .= " $strQuote$strVal$strQuote,";
540 $schema_fields .= " $aryName[$i],";
541
542 }
543
544 $schema_vals = ereg_replace(",$", "", $schema_vals);
545 $schema_vals = ereg_replace("^ ", "", $schema_vals);
546 $schema_fields = ereg_replace(",$", "", $schema_fields);
547 rizwank 1.1 $schema_fields = ereg_replace("^ ", "", $schema_fields);
548
549 //
550 // Take the ordered fields and their associated data and build it
551 // into a valid sql statement to recreate that field in the data.
552 //
553 $schema_insert = "INSERT INTO $table ($schema_fields) VALUES($schema_vals);";
554
555 $handler(trim($schema_insert));
556 }
557
558 return(true);
559
560 }// end function get_table_content_postgres...
561
562 //
563 // This function is for getting the data from a mysql table.
564 //
565
566 function get_table_content_mysql($table, $handler)
567 {
568 rizwank 1.1 global $db;
569
570 // Grab the data from the table.
571 if (!($result = $db->sql_query("SELECT * FROM $table")))
572 {
573 message_die(GENERAL_ERROR, "Failed in get_table_content (select *)", "", __LINE__, __FILE__, "SELECT * FROM $table");
574 }
575
576 // Loop through the resulting rows and build the sql statement.
577 if ($row = $db->sql_fetchrow($result))
578 {
579 $handler("\n#\n# Table Data for $table\n#\n");
580 $field_names = array();
581
582 // Grab the list of field names.
583 $num_fields = $db->sql_numfields($result);
584 $table_list = '(';
585 for ($j = 0; $j < $num_fields; $j++)
586 {
587 $field_names[$j] = $db->sql_fieldname($j, $result);
588 $table_list .= (($j > 0) ? ', ' : '') . $field_names[$j];
589 rizwank 1.1
590 }
591 $table_list .= ')';
592
593 do
594 {
595 // Start building the SQL statement.
596 $schema_insert = "INSERT INTO $table $table_list VALUES(";
597
598 // Loop through the rows and fill in data for each column
599 for ($j = 0; $j < $num_fields; $j++)
600 {
601 $schema_insert .= ($j > 0) ? ', ' : '';
602
603 if(!isset($row[$field_names[$j]]))
604 {
605 //
606 // If there is no data for the column set it to null.
607 // There was a problem here with an extra space causing the
608 // sql file not to reimport if the last column was null in
609 // any table. Should be fixed now :) JLH
610 rizwank 1.1 //
611 $schema_insert .= 'NULL';
612 }
613 elseif ($row[$field_names[$j]] != '')
614 {
615 $schema_insert .= '\'' . addslashes($row[$field_names[$j]]) . '\'';
616 }
617 else
618 {
619 $schema_insert .= '\'\'';
620 }
621 }
622
623 $schema_insert .= ');';
624
625 // Go ahead and send the insert statement to the handler function.
626 $handler(trim($schema_insert));
627
628 }
629 while ($row = $db->sql_fetchrow($result));
630 }
631 rizwank 1.1
632 return(true);
633 }
634
635 function output_table_content($content)
636 {
637 global $tempfile;
638
639 //fwrite($tempfile, $content . "\n");
640 //$backup_sql .= $content . "\n";
641 echo $content ."\n";
642 return;
643 }
644 //
645 // End Functions
646 // -------------
647
648
649 //
650 // Begin program proper
651 //
652 rizwank 1.1 if( isset($HTTP_GET_VARS['perform']) || isset($HTTP_POST_VARS['perform']) )
653 {
654 $perform = (isset($HTTP_POST_VARS['perform'])) ? $HTTP_POST_VARS['perform'] : $HTTP_GET_VARS['perform'];
655
656 switch($perform)
657 {
658 case 'backup':
659
660 $error = false;
661 switch(SQL_LAYER)
662 {
663 case 'oracle':
664 $error = true;
665 break;
666 case 'db2':
667 $error = true;
668 break;
669 case 'msaccess':
670 $error = true;
671 break;
672 case 'mssql':
673 rizwank 1.1 case 'mssql-odbc':
674 $error = true;
675 break;
676 }
677
678 if ($error)
679 {
680 include('./page_header_admin.'.$phpEx);
681
682 $template->set_filenames(array(
683 "body" => "admin/admin_message_body.tpl")
684 );
685
686 $template->assign_vars(array(
687 "MESSAGE_TITLE" => $lang['Information'],
688 "MESSAGE_TEXT" => $lang['Backups_not_supported'])
689 );
690
691 $template->pparse("body");
692
693 include('./page_footer_admin.'.$phpEx);
694 rizwank 1.1 }
695
696 $tables = array('auth_access', 'banlist', 'categories', 'config', 'disallow', 'forums', 'forum_prune', 'groups', 'posts', 'posts_text', 'privmsgs', 'privmsgs_text', 'ranks', 'search_results', 'search_wordlist', 'search_wordmatch', 'sessions', 'smilies', 'themes', 'themes_name', 'topics', 'topics_watch', 'user_group', 'users', 'vote_desc', 'vote_results', 'vote_voters', 'words');
697
698 $additional_tables = (isset($HTTP_POST_VARS['additional_tables'])) ? $HTTP_POST_VARS['additional_tables'] : ( (isset($HTTP_GET_VARS['additional_tables'])) ? $HTTP_GET_VARS['additional_tables'] : "" );
699
700 $backup_type = (isset($HTTP_POST_VARS['backup_type'])) ? $HTTP_POST_VARS['backup_type'] : ( (isset($HTTP_GET_VARS['backup_type'])) ? $HTTP_GET_VARS['backup_type'] : "" );
701
702 $gzipcompress = (!empty($HTTP_POST_VARS['gzipcompress'])) ? $HTTP_POST_VARS['gzipcompress'] : ( (!empty($HTTP_GET_VARS['gzipcompress'])) ? $HTTP_GET_VARS['gzipcompress'] : 0 );
703
704 if(!empty($additional_tables))
705 {
706 if(ereg(",", $additional_tables))
707 {
708 $additional_tables = split(",", $additional_tables);
709
710 for($i = 0; $i < count($additional_tables); $i++)
711 {
712 $tables[] = trim($additional_tables[$i]);
713 }
714
715 rizwank 1.1 }
716 else
717 {
718 $tables[] = trim($additional_tables);
719 }
720 }
721
722 if( !isset($HTTP_POST_VARS['backupstart']) && !isset($HTTP_GET_VARS['backupstart']))
723 {
724 include('./page_header_admin.'.$phpEx);
725
726 $template->set_filenames(array(
727 "body" => "admin/db_utils_backup_body.tpl")
728 );
729 $s_hidden_fields = "<input type=\"hidden\" name=\"perform\" value=\"backup\" /><input type=\"hidden\" name=\"drop\" value=\"1\" /><input type=\"hidden\" name=\"perform\" value=\"$perform\" />";
730
731 $template->assign_vars(array(
732 "L_DATABASE_BACKUP" => $lang['Database_Utilities'] . " : " . $lang['Backup'],
733 "L_BACKUP_EXPLAIN" => $lang['Backup_explain'],
734 "L_FULL_BACKUP" => $lang['Full_backup'],
735 "L_STRUCTURE_BACKUP" => $lang['Structure_backup'],
736 rizwank 1.1 "L_DATA_BACKUP" => $lang['Data_backup'],
737 "L_ADDITIONAL_TABLES" => $lang['Additional_tables'],
738 "L_START_BACKUP" => $lang['Start_backup'],
739 "L_BACKUP_OPTIONS" => $lang['Backup_options'],
740 "L_GZIP_COMPRESS" => $lang['Gzip_compress'],
741 "L_NO" => $lang['No'],
742 "L_YES" => $lang['Yes'],
743
744 "S_HIDDEN_FIELDS" => $s_hidden_fields,
745 "S_DBUTILS_ACTION" => append_sid("admin_db_utilities.$phpEx"))
746 );
747 $template->pparse("body");
748
749 break;
750
751 }
752 else if( !isset($HTTP_POST_VARS['startdownload']) && !isset($HTTP_GET_VARS['startdownload']) )
753 {
754 if(is_array($additional_tables))
755 {
756 $additional_tables = implode(',', $additional_tables);
757 rizwank 1.1 }
758 $template->set_filenames(array(
759 "body" => "admin/admin_message_body.tpl")
760 );
761
762 $template->assign_vars(array(
763 "META" => '<meta http-equiv="refresh" content="2;url=' . append_sid("admin_db_utilities.$phpEx?perform=backup&additional_tables=" . quotemeta($additional_tables) . "&backup_type=$backup_type&drop=1&backupstart=1&gzipcompress=$gzipcompress&startdownload=1") . '">',
764
765 "MESSAGE_TITLE" => $lang['Database_Utilities'] . " : " . $lang['Backup'],
766 "MESSAGE_TEXT" => $lang['Backup_download'])
767 );
768
769 include('./page_header_admin.'.$phpEx);
770
771 $template->pparse("body");
772
773 include('./page_footer_admin.'.$phpEx);
774
775 }
776 header("Pragma: no-cache");
777 $do_gzip_compress = FALSE;
778 rizwank 1.1 if( $gzipcompress )
779 {
780 $phpver = phpversion();
781
782 if($phpver >= "4.0")
783 {
784 if(extension_loaded("zlib"))
785 {
786 $do_gzip_compress = TRUE;
787 }
788 }
789 }
790 if($do_gzip_compress)
791 {
792 @ob_start();
793 @ob_implicit_flush(0);
794 header("Content-Type: application/x-gzip; name=\"phpbb_db_backup.sql.gz\"");
795 header("Content-disposition: attachment; filename=phpbb_db_backup.sql.gz");
796 }
797 else
798 {
799 rizwank 1.1 header("Content-Type: text/x-delimtext; name=\"phpbb_db_backup.sql\"");
800 header("Content-disposition: attachment; filename=phpbb_db_backup.sql");
801 }
802
803 //
804 // Build the sql script file...
805 //
806 echo "#\n";
807 echo "# phpBB Backup Script\n";
808 echo "# Dump of tables for $dbname\n";
809 echo "#\n# DATE : " . gmdate("d-m-Y H:i:s", time()) . " GMT\n";
810 echo "#\n";
811
812 if(SQL_LAYER == 'postgresql')
813 {
814 echo "\n" . pg_get_sequences("\n", $backup_type);
815 }
816 for($i = 0; $i < count($tables); $i++)
817 {
818 $table_name = $tables[$i];
819
820 rizwank 1.1 switch (SQL_LAYER)
821 {
822 case 'postgresql':
823 $table_def_function = "get_table_def_postgresql";
824 $table_content_function = "get_table_content_postgresql";
825 break;
826
827 case 'mysql':
828 case 'mysql4':
829 $table_def_function = "get_table_def_mysql";
830 $table_content_function = "get_table_content_mysql";
831 break;
832 }
833
834 if($backup_type != 'data')
835 {
836 echo "#\n# TABLE: " . $table_prefix . $table_name . "\n#\n";
837 echo $table_def_function($table_prefix . $table_name, "\n") . "\n";
838 }
839
840 if($backup_type != 'structure')
841 rizwank 1.1 {
842 $table_content_function($table_prefix . $table_name, "output_table_content");
843 }
844 }
845
846 if($do_gzip_compress)
847 {
848 $Size = ob_get_length();
849 $Crc = crc32(ob_get_contents());
850 $contents = gzcompress(ob_get_contents());
851 ob_end_clean();
852 echo "\x1f\x8b\x08\x00\x00\x00\x00\x00".substr($contents, 0, strlen($contents) - 4).gzip_PrintFourChars($Crc).gzip_PrintFourChars($Size);
853 }
854 exit;
855
856 break;
857
858 case 'restore':
859 if(!isset($restore_start))
860 {
861 //
862 rizwank 1.1 // Define Template files...
863 //
864 include('./page_header_admin.'.$phpEx);
865
866 $template->set_filenames(array(
867 "body" => "admin/db_utils_restore_body.tpl")
868 );
869
870 $s_hidden_fields = "<input type=\"hidden\" name=\"perform\" value=\"restore\" /><input type=\"hidden\" name=\"perform\" value=\"$perform\" />";
871
872 $template->assign_vars(array(
873 "L_DATABASE_RESTORE" => $lang['Database_Utilities'] . " : " . $lang['Restore'],
874 "L_RESTORE_EXPLAIN" => $lang['Restore_explain'],
875 "L_SELECT_FILE" => $lang['Select_file'],
876 "L_START_RESTORE" => $lang['Start_Restore'],
877
878 "S_DBUTILS_ACTION" => append_sid("admin_db_utilities.$phpEx"),
879 "S_HIDDEN_FIELDS" => $s_hidden_fields)
880 );
881 $template->pparse("body");
882
883 rizwank 1.1 break;
884
885 }
886 else
887 {
888 //
889 // Handle the file upload ....
890 // If no file was uploaded report an error...
891 //
892 $backup_file_name = (!empty($HTTP_POST_FILES['backup_file']['name'])) ? $HTTP_POST_FILES['backup_file']['name'] : "";
893 $backup_file_tmpname = ($HTTP_POST_FILES['backup_file']['tmp_name'] != "none") ? $HTTP_POST_FILES['backup_file']['tmp_name'] : "";
894 $backup_file_type = (!empty($HTTP_POST_FILES['backup_file']['type'])) ? $HTTP_POST_FILES['backup_file']['type'] : "";
895
896 if($backup_file_tmpname == "" || $backup_file_name == "")
897 {
898 message_die(GENERAL_MESSAGE, $lang['Restore_Error_no_file']);
899 }
900 //
901 // If I file was actually uploaded, check to make sure that we
902 // are actually passed the name of an uploaded file, and not
903 // a hackers attempt at getting us to process a local system
904 rizwank 1.1 // file.
905 //
906 if( file_exists(phpbb_realpath($backup_file_tmpname)) )
907 {
908 if( preg_match("/^(text\/[a-zA-Z]+)|(application\/(x\-)?gzip(\-compressed)?)|(application\/octet-stream)$/is", $backup_file_type) )
909 {
910 if( preg_match("/\.gz$/is",$backup_file_name) )
911 {
912 $do_gzip_compress = FALSE;
913 $phpver = phpversion();
914 if($phpver >= "4.0")
915 {
916 if(extension_loaded("zlib"))
917 {
918 $do_gzip_compress = TRUE;
919 }
920 }
921
922 if($do_gzip_compress)
923 {
924 $gz_ptr = gzopen($backup_file_tmpname, 'rb');
925 rizwank 1.1 $sql_query = "";
926 while( !gzeof($gz_ptr) )
927 {
928 $sql_query .= gzgets($gz_ptr, 100000);
929 }
930 }
931 else
932 {
933 message_die(GENERAL_ERROR, $lang['Restore_Error_decompress']);
934 }
935 }
936 else
937 {
938 $sql_query = fread(fopen($backup_file_tmpname, 'r'), filesize($backup_file_tmpname));
939 }
940 //
941 // Comment this line out to see if this fixes the stuff...
942 //
943 //$sql_query = stripslashes($sql_query);
944 }
945 else
946 rizwank 1.1 {
947 message_die(GENERAL_ERROR, $lang['Restore_Error_filename'] ." $backup_file_type $backup_file_name");
948 }
949 }
950 else
951 {
952 message_die(GENERAL_ERROR, $lang['Restore_Error_uploading']);
953 }
954
955 if($sql_query != "")
956 {
957 // Strip out sql comments...
958 $sql_query = remove_remarks($sql_query);
959 $pieces = split_sql_file($sql_query, ";");
960
961 $sql_count = count($pieces);
962 for($i = 0; $i < $sql_count; $i++)
963 {
964 $sql = trim($pieces[$i]);
965
966 if(!empty($sql) and $sql[0] != "#")
967 rizwank 1.1 {
968 if(VERBOSE == 1)
969 {
970 echo "Executing: $sql\n<br>";
971 flush();
972 }
973
974 $result = $db->sql_query($sql);
975
976 if(!$result && ( !(SQL_LAYER == 'postgresql' && eregi("drop table", $sql) ) ) )
977 {
978 message_die(GENERAL_ERROR, "Error importing backup file", "", __LINE__, __FILE__, $sql);
979 }
980 }
981 }
982 }
983
984 include('./page_header_admin.'.$phpEx);
985
986 $template->set_filenames(array(
987 "body" => "admin/admin_message_body.tpl")
988 rizwank 1.1 );
989
990 $message = $lang['Restore_success'];
991
992 $template->assign_vars(array(
993 "MESSAGE_TITLE" => $lang['Database_Utilities'] . " : " . $lang['Restore'],
994 "MESSAGE_TEXT" => $message)
995 );
996
997 $template->pparse("body");
998 break;
999 }
1000 break;
1001 }
1002 }
1003
1004 include('./page_footer_admin.'.$phpEx);
1005
1006 ?>
|