2 /***************************************************************************
3 * admin_db_utilities.php
5 * begin : Thu May 31, 2001
6 * copyright : (C) 2001 The phpBB Group
7 * email : support@phpbb.com
9 * $Id: admin_db_utilities.php,v 1.42.2.10 2003/03/04 21:02:19 acydburn Exp $
11 ****************************************************************************/
13 /***************************************************************************
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.
20 ***************************************************************************/
22 /***************************************************************************
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
27 * Some functions are adapted from the upgrade_20.php script and others
28 * adapted from the unoficial phpMyAdmin 2.2.0.
29 ***************************************************************************/
31 define('IN_PHPBB', 1);
33 if( !empty($setmodules) )
35 $filename = basename(__FILE__);
36 $module['General']['Backup_DB'] = $filename . "?perform=backup";
38 $file_uploads = (@phpversion() >= '4.0.0') ? @ini_get('file_uploads') : @get_cfg_var('file_uploads');
40 if( (empty($file_uploads) || $file_uploads != 0) && (strtolower($file_uploads) != 'off') && (@phpversion() != '4.0.4pl1') )
42 $module['General']['Restore_DB'] = $filename . "?perform=restore";
49 // Load default header
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);
58 // Set VERBOSE to 1 for debugging info..
63 // Increase maximum execution time, but don't complain about it if it isn't
66 @set_time_limit(1200);
68 // -----------------------
69 // The following functions are adapted from phpMyAdmin and upgrade_20.php
71 function gzip_PrintFourChars($Val)
73 for ($i = 0; $i < 4; $i ++)
75 $return .= chr($Val % 256);
76 $Val = floor($Val / 256);
84 // This function is used for grabbing the sequences for postgres...
86 function pg_get_sequences($crlf, $backup_type)
90 $get_seq_sql = "SELECT relname FROM pg_class WHERE NOT relname ~ 'pg_.*'
91 AND relkind = 'S' ORDER BY relname";
93 $seq = $db->sql_query($get_seq_sql);
95 if( !$num_seq = $db->sql_numrows($seq) )
98 $return_val = "# No Sequences Found $crlf";
103 $return_val = "# Sequences $crlf";
106 while($i_seq < $num_seq)
108 $row = $db->sql_fetchrow($seq);
109 $sequence = $row['relname'];
111 $get_props_sql = "SELECT * FROM $sequence";
112 $seq_props = $db->sql_query($get_props_sql);
114 if($db->sql_numrows($seq_props) > 0)
116 $row1 = $db->sql_fetchrow($seq_props);
118 if($backup_type == 'structure')
120 $row['last_value'] = 1;
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";
125 } // End if numrows > 0
127 if(($row['last_value'] > 1) && ($backup_type != 'structure'))
129 $return_val .= "SELECT NEXTVALE('$sequence'); $crlf";
130 unset($row['last_value']);
144 // The following functions will return the "CREATE TABLE syntax for the
147 // This function returns, will return the table def's for postgres...
149 function get_table_def_postgresql($table, $crlf)
155 // Get a listing of the fields, with their associated types, etc.
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'
162 AND a.attrelid = c.oid
163 AND a.atttypid = t.oid
165 $result = $db->sql_query($field_query);
169 message_die(GENERAL_ERROR, "Failed in get_table_def (show fields)", "", __LINE__, __FILE__, $field_query);
174 $schema_create .= "DROP TABLE $table;$crlf";
178 // Ok now we actually start building the SQL statements to restore the tables
181 $schema_create .= "CREATE TABLE $table($crlf";
183 while ($row = $db->sql_fetchrow($result))
186 // Get the data from the table
188 $sql_get_default = "SELECT d.adsrc AS rowdefault
189 FROM pg_attrdef d, pg_class c
190 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);
197 unset($row['rowdefault']);
201 $row['rowdefault'] = @pg_result($def_res, 0, 'rowdefault');
204 if ($row['type'] == 'bpchar')
206 // Internally stored as bpchar, but isn't accepted in a CREATE TABLE statement.
207 $row['type'] = 'char';
210 $schema_create .= ' ' . $row['field'] . ' ' . $row['type'];
212 if (eregi('char', $row['type']))
214 if ($row['lengthvar'] > 0)
216 $schema_create .= '(' . ($row['lengthvar'] -4) . ')';
220 if (eregi('numeric', $row['type']))
222 $schema_create .= '(';
223 $schema_create .= sprintf("%s,%s", (($row['lengthvar'] >> 16) & 0xffff), (($row['lengthvar'] - 4) & 0xffff));
224 $schema_create .= ')';
227 if (!empty($row['rowdefault']))
229 $schema_create .= ' DEFAULT ' . $row['rowdefault'];
232 if ($row['notnull'] == 't')
234 $schema_create .= ' NOT NULL';
237 $schema_create .= ",$crlf";
241 // Get the listing of primary keys.
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 ORDER BY index_name, tab_name, column_name ";
254 $result = $db->sql_query($sql_pri_keys);
258 message_die(GENERAL_ERROR, "Failed in get_table_def (show fields)", "", __LINE__, __FILE__, $sql_pri_keys);
261 while ( $row = $db->sql_fetchrow($result))
263 if ($row['primary_key'] == 't')
265 if (!empty($primary_key))
267 $primary_key .= ', ';
270 $primary_key .= $row['column_name'];
271 $primary_key_name = $row['index_name'];
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
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'] . ', ';
286 if (!empty($index_rows))
288 while(list($idx_name, $props) = each($index_rows))
290 $props['column_names'] = ereg_replace(", $", "" , $props['column_names']);
291 $index_create .= 'CREATE ' . $props['unique'] . " INDEX $idx_name ON $table (" . $props['column_names'] . ");$crlf";
295 if (!empty($primary_key))
297 $schema_create .= " CONSTRAINT $primary_key_name PRIMARY KEY ($primary_key),$crlf";
301 // Generate constraint clauses for CHECK constraints
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'
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
315 $result = $db->sql_query($sql_checks);
319 message_die(GENERAL_ERROR, "Failed in get_table_def (show fields)", "", __LINE__, __FILE__, $sql_checks);
323 // Add the constraints to the sql file.
325 while ($row = $db->sql_fetchrow($result))
327 $schema_create .= ' CONSTRAINT ' . $row['index_name'] . ' CHECK ' . $row['rcsrc'] . ",$crlf";
330 $schema_create = ereg_replace(',' . $crlf . '$', '', $schema_create);
331 $index_create = ereg_replace(',' . $crlf . '$', '', $index_create);
333 $schema_create .= "$crlf);$crlf";
335 if (!empty($index_create))
337 $schema_create .= $index_create;
341 // Ok now we've built all the sql return it to the calling function.
343 return (stripslashes($schema_create));
348 // This function returns the "CREATE TABLE" syntax for mysql dbms...
350 function get_table_def_mysql($table, $crlf)
355 $field_query = "SHOW FIELDS FROM $table";
356 $key_query = "SHOW KEYS FROM $table";
359 // If the user has selected to drop existing tables when doing a restore.
360 // Then we add the statement to drop the tables....
364 $schema_create .= "DROP TABLE IF EXISTS $table;$crlf";
367 $schema_create .= "CREATE TABLE $table($crlf";
370 // Ok lets grab the fields...
372 $result = $db->sql_query($field_query);
375 message_die(GENERAL_ERROR, "Failed in get_table_def (show fields)", "", __LINE__, __FILE__, $field_query);
378 while ($row = $db->sql_fetchrow($result))
380 $schema_create .= ' ' . $row['Field'] . ' ' . $row['Type'];
382 if(!empty($row['Default']))
384 $schema_create .= ' DEFAULT \'' . $row['Default'] . '\'';
387 if($row['Null'] != "YES")
389 $schema_create .= ' NOT NULL';
392 if($row['Extra'] != "")
394 $schema_create .= ' ' . $row['Extra'];
397 $schema_create .= ",$crlf";
400 // Drop the last ',$crlf' off ;)
402 $schema_create = ereg_replace(',' . $crlf . '$', "", $schema_create);
405 // Get any Indexed fields from the database...
407 $result = $db->sql_query($key_query);
410 message_die(GENERAL_ERROR, "FAILED IN get_table_def (show keys)", "", __LINE__, __FILE__, $key_query);
413 while($row = $db->sql_fetchrow($result))
415 $kname = $row['Key_name'];
417 if(($kname != 'PRIMARY') && ($row['Non_unique'] == 0))
419 $kname = "UNIQUE|$kname";
422 if(!is_array($index[$kname]))
424 $index[$kname] = array();
427 $index[$kname][] = $row['Column_name'];
430 while(list($x, $columns) = @each($index))
432 $schema_create .= ", $crlf";
436 $schema_create .= ' PRIMARY KEY (' . implode($columns, ', ') . ')';
438 elseif (substr($x,0,6) == 'UNIQUE')
440 $schema_create .= ' UNIQUE ' . substr($x,7) . ' (' . implode($columns, ', ') . ')';
444 $schema_create .= " KEY $x (" . implode($columns, ', ') . ')';
448 $schema_create .= "$crlf);";
450 if(get_magic_quotes_runtime())
452 return(stripslashes($schema_create));
456 return($schema_create);
459 } // End get_table_def_mysql
463 // This fuction will return a tables create definition to be used as an sql
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);
473 // Here is the function for postgres...
475 function get_table_content_postgresql($table, $handler)
480 // Grab all of the data from current table.
483 $result = $db->sql_query("SELECT * FROM $table");
487 message_die(GENERAL_ERROR, "Failed in get_table_content (select *)", "", __LINE__, __FILE__, "SELECT * FROM $table");
490 $i_num_fields = $db->sql_numfields($result);
492 for ($i = 0; $i < $i_num_fields; $i++)
494 $aryType[] = $db->sql_fieldtype($i, $result);
495 $aryName[] = $db->sql_fieldname($i, $result);
500 while($row = $db->sql_fetchrow($result))
503 unset($schema_fields);
504 unset($schema_insert);
506 // Build the SQL statement to recreate the data.
508 for($i = 0; $i < $i_num_fields; $i++)
510 $strVal = $row[$aryName[$i]];
511 if (eregi("char|text|bool", $aryType[$i]))
515 $strVal = addslashes($strVal);
517 elseif (eregi("date|timestamp", $aryType[$i]))
534 if (empty($strVal) && $strVal != "0")
539 $schema_vals .= " $strQuote$strVal$strQuote,";
540 $schema_fields .= " $aryName[$i],";
544 $schema_vals = ereg_replace(",$", "", $schema_vals);
545 $schema_vals = ereg_replace("^ ", "", $schema_vals);
546 $schema_fields = ereg_replace(",$", "", $schema_fields);
547 $schema_fields = ereg_replace("^ ", "", $schema_fields);
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.
553 $schema_insert = "INSERT INTO $table ($schema_fields) VALUES($schema_vals);";
555 $handler(trim($schema_insert));
560 }// end function get_table_content_postgres...
563 // This function is for getting the data from a mysql table.
566 function get_table_content_mysql($table, $handler)
570 // Grab the data from the table.
571 if (!($result = $db->sql_query("SELECT * FROM $table")))
573 message_die(GENERAL_ERROR, "Failed in get_table_content (select *)", "", __LINE__, __FILE__, "SELECT * FROM $table");
576 // Loop through the resulting rows and build the sql statement.
577 if ($row = $db->sql_fetchrow($result))
579 $handler("\n#\n# Table Data for $table\n#\n");
580 $field_names = array();
582 // Grab the list of field names.
583 $num_fields = $db->sql_numfields($result);
585 for ($j = 0; $j < $num_fields; $j++)
587 $field_names[$j] = $db->sql_fieldname($j, $result);
588 $table_list .= (($j > 0) ? ', ' : '') . $field_names[$j];
595 // Start building the SQL statement.
596 $schema_insert = "INSERT INTO $table $table_list VALUES(";
598 // Loop through the rows and fill in data for each column
599 for ($j = 0; $j < $num_fields; $j++)
601 $schema_insert .= ($j > 0) ? ', ' : '';
603 if(!isset($row[$field_names[$j]]))
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
611 $schema_insert .= 'NULL';
613 elseif ($row[$field_names[$j]] != '')
615 $schema_insert .= '\'' . addslashes($row[$field_names[$j]]) . '\'';
619 $schema_insert .= '\'\'';
623 $schema_insert .= ');';
625 // Go ahead and send the insert statement to the handler function.
626 $handler(trim($schema_insert));
629 while ($row = $db->sql_fetchrow($result));
635 function output_table_content($content)
639 //fwrite($tempfile, $content . "\n");
640 //$backup_sql .= $content . "\n";
650 // Begin program proper
652 if( isset($HTTP_GET_VARS['perform']) || isset($HTTP_POST_VARS['perform']) )
654 $perform = (isset($HTTP_POST_VARS['perform'])) ? $HTTP_POST_VARS['perform'] : $HTTP_GET_VARS['perform'];
680 include('./page_header_admin.'.$phpEx);
682 $template->set_filenames(array(
683 "body" => "admin/admin_message_body.tpl")
686 $template->assign_vars(array(
687 "MESSAGE_TITLE" => $lang['Information'],
688 "MESSAGE_TEXT" => $lang['Backups_not_supported'])
691 $template->pparse("body");
693 include('./page_footer_admin.'.$phpEx);
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');
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'] : "" );
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'] : "" );
702 $gzipcompress = (!empty($HTTP_POST_VARS['gzipcompress'])) ? $HTTP_POST_VARS['gzipcompress'] : ( (!empty($HTTP_GET_VARS['gzipcompress'])) ? $HTTP_GET_VARS['gzipcompress'] : 0 );
704 $drop = (!empty($HTTP_POST_VARS['drop'])) ? intval($HTTP_POST_VARS['drop']) : ( (!empty($HTTP_GET_VARS['drop'])) ? intval($HTTP_GET_VARS['drop']) : 0 );
706 if(!empty($additional_tables))
708 if(ereg(",", $additional_tables))
710 $additional_tables = split(",", $additional_tables);
712 for($i = 0; $i < count($additional_tables); $i++)
714 $tables[] = trim($additional_tables[$i]);
720 $tables[] = trim($additional_tables);
724 if( !isset($HTTP_POST_VARS['backupstart']) && !isset($HTTP_GET_VARS['backupstart']))
726 include('./page_header_admin.'.$phpEx);
728 $template->set_filenames(array(
729 "body" => "admin/db_utils_backup_body.tpl")
731 $s_hidden_fields = "<input type=\"hidden\" name=\"perform\" value=\"backup\" /><input type=\"hidden\" name=\"drop\" value=\"1\" /><input type=\"hidden\" name=\"perform\" value=\"$perform\" />";
733 $template->assign_vars(array(
734 "L_DATABASE_BACKUP" => $lang['Database_Utilities'] . " : " . $lang['Backup'],
735 "L_BACKUP_EXPLAIN" => $lang['Backup_explain'],
736 "L_FULL_BACKUP" => $lang['Full_backup'],
737 "L_STRUCTURE_BACKUP" => $lang['Structure_backup'],
738 "L_DATA_BACKUP" => $lang['Data_backup'],
739 "L_ADDITIONAL_TABLES" => $lang['Additional_tables'],
740 "L_START_BACKUP" => $lang['Start_backup'],
741 "L_BACKUP_OPTIONS" => $lang['Backup_options'],
742 "L_GZIP_COMPRESS" => $lang['Gzip_compress'],
743 "L_NO" => $lang['No'],
744 "L_YES" => $lang['Yes'],
746 "S_HIDDEN_FIELDS" => $s_hidden_fields,
747 "S_DBUTILS_ACTION" => append_sid("admin_db_utilities.$phpEx"))
749 $template->pparse("body");
754 else if( !isset($HTTP_POST_VARS['startdownload']) && !isset($HTTP_GET_VARS['startdownload']) )
756 if(is_array($additional_tables))
758 $additional_tables = implode(',', $additional_tables);
760 $template->set_filenames(array(
761 "body" => "admin/admin_message_body.tpl")
764 $template->assign_vars(array(
765 "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") . '">',
767 "MESSAGE_TITLE" => $lang['Database_Utilities'] . " : " . $lang['Backup'],
768 "MESSAGE_TEXT" => $lang['Backup_download'])
771 include('./page_header_admin.'.$phpEx);
773 $template->pparse("body");
775 include('./page_footer_admin.'.$phpEx);
778 header("Pragma: no-cache");
779 $do_gzip_compress = FALSE;
782 $phpver = phpversion();
786 if(extension_loaded("zlib"))
788 $do_gzip_compress = TRUE;
792 if($do_gzip_compress)
795 @ob_implicit_flush(0);
796 header("Content-Type: application/x-gzip; name=\"phpbb_db_backup.sql.gz\"");
797 header("Content-disposition: attachment; filename=phpbb_db_backup.sql.gz");
801 header("Content-Type: text/x-delimtext; name=\"phpbb_db_backup.sql\"");
802 header("Content-disposition: attachment; filename=phpbb_db_backup.sql");
806 // Build the sql script file...
809 echo "# phpBB Backup Script\n";
810 echo "# Dump of tables for $dbname\n";
811 echo "#\n# DATE : " . gmdate("d-m-Y H:i:s", time()) . " GMT\n";
814 if(SQL_LAYER == 'postgresql')
816 echo "\n" . pg_get_sequences("\n", $backup_type);
818 for($i = 0; $i < count($tables); $i++)
820 $table_name = $tables[$i];
825 $table_def_function = "get_table_def_postgresql";
826 $table_content_function = "get_table_content_postgresql";
831 $table_def_function = "get_table_def_mysql";
832 $table_content_function = "get_table_content_mysql";
836 if($backup_type != 'data')
838 echo "#\n# TABLE: " . $table_prefix . $table_name . "\n#\n";
839 echo $table_def_function($table_prefix . $table_name, "\n") . "\n";
842 if($backup_type != 'structure')
844 $table_content_function($table_prefix . $table_name, "output_table_content");
848 if($do_gzip_compress)
850 $Size = ob_get_length();
851 $Crc = crc32(ob_get_contents());
852 $contents = gzcompress(ob_get_contents());
854 echo "\x1f\x8b\x08\x00\x00\x00\x00\x00".substr($contents, 0, strlen($contents) - 4).gzip_PrintFourChars($Crc).gzip_PrintFourChars($Size);
861 if(!isset($HTTP_POST_VARS['restore_start']))
864 // Define Template files...
866 include('./page_header_admin.'.$phpEx);
868 $template->set_filenames(array(
869 "body" => "admin/db_utils_restore_body.tpl")
872 $s_hidden_fields = "<input type=\"hidden\" name=\"perform\" value=\"restore\" /><input type=\"hidden\" name=\"perform\" value=\"$perform\" />";
874 $template->assign_vars(array(
875 "L_DATABASE_RESTORE" => $lang['Database_Utilities'] . " : " . $lang['Restore'],
876 "L_RESTORE_EXPLAIN" => $lang['Restore_explain'],
877 "L_SELECT_FILE" => $lang['Select_file'],
878 "L_START_RESTORE" => $lang['Start_Restore'],
880 "S_DBUTILS_ACTION" => append_sid("admin_db_utilities.$phpEx"),
881 "S_HIDDEN_FIELDS" => $s_hidden_fields)
883 $template->pparse("body");
891 // Handle the file upload ....
892 // If no file was uploaded report an error...
894 $backup_file_name = (!empty($HTTP_POST_FILES['backup_file']['name'])) ? $HTTP_POST_FILES['backup_file']['name'] : "";
895 $backup_file_tmpname = ($HTTP_POST_FILES['backup_file']['tmp_name'] != "none") ? $HTTP_POST_FILES['backup_file']['tmp_name'] : "";
896 $backup_file_type = (!empty($HTTP_POST_FILES['backup_file']['type'])) ? $HTTP_POST_FILES['backup_file']['type'] : "";
898 if($backup_file_tmpname == "" || $backup_file_name == "")
900 message_die(GENERAL_MESSAGE, $lang['Restore_Error_no_file']);
903 // If I file was actually uploaded, check to make sure that we
904 // are actually passed the name of an uploaded file, and not
905 // a hackers attempt at getting us to process a local system
908 if( file_exists(phpbb_realpath($backup_file_tmpname)) )
910 if( preg_match("/^(text\/[a-zA-Z]+)|(application\/(x\-)?gzip(\-compressed)?)|(application\/octet-stream)$/is", $backup_file_type) )
912 if( preg_match("/\.gz$/is",$backup_file_name) )
914 $do_gzip_compress = FALSE;
915 $phpver = phpversion();
918 if(extension_loaded("zlib"))
920 $do_gzip_compress = TRUE;
924 if($do_gzip_compress)
926 $gz_ptr = gzopen($backup_file_tmpname, 'rb');
928 while( !gzeof($gz_ptr) )
930 $sql_query .= gzgets($gz_ptr, 100000);
935 message_die(GENERAL_ERROR, $lang['Restore_Error_decompress']);
940 $sql_query = fread(fopen($backup_file_tmpname, 'r'), filesize($backup_file_tmpname));
943 // Comment this line out to see if this fixes the stuff...
945 //$sql_query = stripslashes($sql_query);
949 message_die(GENERAL_ERROR, $lang['Restore_Error_filename'] ." $backup_file_type $backup_file_name");
954 message_die(GENERAL_ERROR, $lang['Restore_Error_uploading']);
959 // Strip out sql comments...
960 $sql_query = remove_remarks($sql_query);
961 $pieces = split_sql_file($sql_query, ";");
963 $sql_count = count($pieces);
964 for($i = 0; $i < $sql_count; $i++)
966 $sql = trim($pieces[$i]);
968 if(!empty($sql) and $sql[0] != "#")
972 echo "Executing: $sql\n<br>";
976 $result = $db->sql_query($sql);
978 if(!$result && ( !(SQL_LAYER == 'postgresql' && eregi("drop table", $sql) ) ) )
980 message_die(GENERAL_ERROR, "Error importing backup file", "", __LINE__, __FILE__, $sql);
986 include('./page_header_admin.'.$phpEx);
988 $template->set_filenames(array(
989 "body" => "admin/admin_message_body.tpl")
992 $message = $lang['Restore_success'];
994 $template->assign_vars(array(
995 "MESSAGE_TITLE" => $lang['Database_Utilities'] . " : " . $lang['Restore'],
996 "MESSAGE_TEXT" => $message)
999 $template->pparse("body");
1006 include('./page_footer_admin.'.$phpEx);