📄 acp_database.php
字号:
<?php/** ** @package acp* @version $Id: acp_database.php,v 1.47 2006/11/19 14:48:00 davidmj Exp $* @copyright (c) 2005 phpBB Group * @license http://opensource.org/licenses/gpl-license.php GNU Public License **//*** @package acp*/class acp_database{ var $u_action; function main($id, $mode) { global $db, $user, $auth, $template, $table_prefix; global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx; $user->add_lang('acp/database'); $this->tpl_name = 'acp_database'; $this->page_title = 'ACP_DATABASE'; $action = request_var('action', ''); $submit = (isset($_POST['submit'])) ? true : false; $template->assign_vars(array( 'MODE' => $mode )); switch ($mode) { case 'backup': switch ($action) { case 'download': $type = request_var('type', ''); $table = request_var('table', array('')); $format = request_var('method', ''); $where = request_var('where', ''); $store = $download = $structure = $schema_data = false; if ($where == 'store_and_download' || $where == 'store') { $store = true; } if ($where == 'store_and_download' || $where == 'download') { $download = true; } if ($type == 'full' || $type == 'structure') { $structure = true; } if ($type == 'full' || $type == 'data') { $schema_data = true; } @set_time_limit(1200); $time = time(); $filename = 'backup_' . $time; // We set up the info needed for our on-the-fly creation :D switch ($format) { case 'text': $ext = '.sql'; $open = 'fopen'; $write = 'fwrite'; $close = 'fclose'; $oper = ''; $mimetype = 'text/x-sql'; break; case 'bzip2': $ext = '.sql.bz2'; $open = 'bzopen'; $write = 'bzwrite'; $close = 'bzclose'; $oper = 'bzcompress'; $mimetype = 'application/x-bzip2'; break; case 'gzip': $ext = '.sql.gz'; $open = 'gzopen'; $write = 'gzwrite'; $close = 'gzclose'; $oper = 'gzencode'; $mimetype = 'application/x-gzip'; break; } // We write the file to "store" first (and then compress the file) to not use too much // memory. The server process can be easily killed by storing too much data at once. if ($store == true) { $file = $phpbb_root_path . 'store/' . $filename . $ext; $fp = $open($file, 'w'); if (!$fp) { trigger_error('Unable to write temporary file to storage folder', E_USER_ERROR); } } if ($download == true) { $name = $filename . $ext; header('Pragma: no-cache'); header("Content-Type: $mimetype; name=\"$name\""); header("Content-disposition: attachment; filename=$name"); } // All of the generated queries go here $sql_data = ''; $sql_data .= "#\n"; $sql_data .= "# phpBB Backup Script\n"; $sql_data .= "# Dump of tables for $table_prefix\n"; $sql_data .= "# DATE : " . gmdate("d-m-Y H:i:s", $time) . " GMT\n"; $sql_data .= "#\n"; switch ($db->sql_layer) { case 'sqlite': $sql_data .= "BEGIN TRANSACTION;\n"; $sqlite_version = sqlite_libversion(); break; case 'postgres': $sql_data .= "BEGIN;\n"; break; case 'mssql': case 'mssql_odbc': $sql_data .= "BEGIN TRANSACTION\n"; $sql_data .= "GO\n"; break; } if ($structure && $db->sql_layer == 'firebird') { $sql = 'SELECT RDB$FUNCTION_NAME, RDB$DESCRIPTION FROM RDB$FUNCTIONS ORDER BY RDB$FUNCTION_NAME'; $result = $db->sql_query($sql); $rows = array(); while ($row = $db->sql_fetchrow($result)) { $sql = 'SELECT F.RDB$FUNCTION_NAME, F.RDB$MODULE_NAME, F.RDB$ENTRYPOINT, F.RDB$RETURN_ARGUMENT, F.RDB$DESCRIPTION, FA.RDB$ARGUMENT_POSITION, FA.RDB$MECHANISM, FA.RDB$FIELD_TYPE, FA.RDB$FIELD_SCALE, FA.RDB$FIELD_LENGTH, FA.RDB$FIELD_SUB_TYPE, C.RDB$BYTES_PER_CHARACTER, C.RDB$CHARACTER_SET_NAME ,FA.RDB$FIELD_PRECISION FROM RDB$FUNCTIONS F LEFT JOIN RDB$FUNCTION_ARGUMENTS FA ON F.RDB$FUNCTION_NAME = FA.RDB$FUNCTION_NAME LEFT JOIN RDB$CHARACTER_SETS C ON FA.RDB$CHARACTER_SET_ID = C.RDB$CHARACTER_SET_ID WHERE (F.RDB$FUNCTION_NAME = ' . $row['FUNCTION_NAME'] . ') ORDER BY FA.RDB$ARGUMENT_POSITION'; $result2 = $db->sql_query($sql); while ($row2 = $db->sql_fetchrow($result2)) { } $db->sql_freeresult($result2); } $db->sql_freeresult($result); } foreach ($table as $table_name) { // Get the table structure if ($structure) { switch ($db->sql_layer) { case 'mysqli': case 'mysql4': case 'mysql': $sql_data .= '# Table: ' . $table_name . "\n"; $sql_data .= "DROP TABLE IF EXISTS $table_name;\n"; break; case 'oracle': $sql_data .= '# Table: ' . $table_name . "\n"; $sql_data .= "DROP TABLE $table_name;\n"; $sql_data .= '\\' . "\n"; break; case 'sqlite': $sql_data .= '# Table: ' . $table_name . "\n"; if (version_compare($sqlite_version, '3.0') == -1) { $sql_data .= "DROP TABLE $table_name;\n"; } else { $sql_data .= "DROP TABLE IF EXISTS $table_name;\n"; } break; case 'postgres': case 'firebird': $sql_data .= '# Table: ' . $table_name . "\n"; $sql_data .= "DROP TABLE $table_name;\n"; break; case 'mssql': case 'mssql_odbc': $sql_data .= '# Table: ' . $table_name . "\n"; $sql_data .= "IF OBJECT_ID(N'$table_name', N'U') IS NOT NULL\n"; $sql_data .= "DROP TABLE $table_name;\n"; $sql_data .= "GO\n"; break; } $sql_data .= $this->get_table_structure($table_name); } else { // We might wanna empty out all that junk :D $sql_data .= (($db->sql_layer == 'sqlite') ? 'DELETE FROM ' : 'TRUNCATE TABLE ') . $table_name . ";\n"; } // Now write the data for the first time. :) if ($store == true) { $write($fp, $sql_data); } if ($download == true) { if (!empty($oper)) { echo $oper($sql_data); } else { echo $sql_data; } } $sql_data = ''; // Data if ($schema_data) { $sql_data .= "\n"; switch ($db->sql_layer) { case 'mysqli': $sql = "SELECT * FROM $table_name"; $result = mysqli_query($db->db_connect_id, $sql, MYSQLI_USE_RESULT); if ($result != false) { $fields_cnt = mysqli_num_fields($result); // Get field information $field = mysqli_fetch_fields($result); $field_set = array(); for ($j = 0; $j < $fields_cnt; $j++) { $field_set[$j] = $field[$j]->name; } $search = array("\\", "'", "\x00", "\x0a", "\x0d", "\x1a", '"'); $replace = array("\\\\", "\\'", '\0', '\n', '\r', '\Z', '\\"'); $fields = implode(', ', $field_set); $values = array(); $schema_insert = 'INSERT INTO ' . $table_name . ' (' . $fields . ') VALUES ('; while ($row = mysqli_fetch_row($result)) { for ($j = 0; $j < $fields_cnt; $j++) { if (!isset($row[$j]) || is_null($row[$j])) { $values[$j] = 'NULL'; } else if (($field[$j]->flags & 32768) && !($field[$j]->flags & 1024)) { $values[$j] = $row[$j]; } else { $values[$j] = "'" . str_replace($search, $replace, $row[$j]) . "'"; } } $sql_data .= $schema_insert . implode(', ', $values) . ");\n"; if ($store == true) { $write($fp, $sql_data); } if ($download == true) { if (!empty($oper)) { echo $oper($sql_data); } else { echo $sql_data; } } $sql_data = ''; $values = array(); } mysqli_free_result($result); } break; case 'mysql4': case 'mysql': $sql = "SELECT * FROM $table_name"; $result = mysql_unbuffered_query($sql, $db->db_connect_id); if ($result != false) { $fields_cnt = mysql_num_fields($result); // Get field information $field = array(); for ($i = 0; $i < $fields_cnt; $i++) { $field[$i] = mysql_fetch_field($result, $i); } $field_set = array(); for ($j = 0; $j < $fields_cnt; $j++) { $field_set[$j] = $field[$j]->name; } $search = array("\\", "'", "\x00", "\x0a", "\x0d", "\x1a", '"'); $replace = array("\\\\", "\\'", '\0', '\n', '\r', '\Z', '\\"'); $fields = implode(', ', $field_set); $schema_insert = 'INSERT INTO ' . $table_name . ' (' . $fields . ') VALUES ('; while ($row = mysql_fetch_row($result)) { $values = array(); for ($j = 0; $j < $fields_cnt; $j++) { if (!isset($row[$j]) || is_null($row[$j])) { $values[$j] = 'NULL'; } else if ($field[$j]->numeric && ($field[$j]->type !== 'timestamp')) { $values[$j] = $row[$j]; } else { $values[$j] = "'" . str_replace($search, $replace, $row[$j]) . "'"; } } $sql_data .= $schema_insert . implode(', ', $values) . ");\n"; if ($store == true) { $write($fp, $sql_data); } if ($download == true) { if (!empty($oper)) { echo $oper($sql_data); } else { echo $sql_data; } } $sql_data = ''; } mysql_free_result($result); } break; case 'sqlite': // This is *not* my fault. The PHP guys forgot a call to finalize when they wrote this function. This forces all the tables to stay locked... // They finally fixed it in 5.1.3 but 5.1.2 and under still have this so instead, we go and grab the column types by smashing open the sqlite_master table // and grope around for things that remind us of datatypes... if (version_compare(phpversion(), '5.1.3', '>=')) { $col_types = sqlite_fetch_column_types($db->db_connect_id, $table_name); } else { $sql = "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = '" . $table_name . "'"; $table_data = sqlite_single_query($db->db_connect_id, $sql); $table_data = preg_replace('#CREATE\s+TABLE\s+"?' . $table_name . '"?#i', '', $table_data); $table_data = trim($table_data); preg_match('#\((.*)\)#s', $table_data, $matches); $column_list = array(); $table_cols = explode(',', trim($matches[1])); foreach ($table_cols as $declaration) { $entities = preg_split('#\s+#', trim($declaration)); $column_name = preg_replace('/"?([^"]+)"?/', '\1', $entities[0]); // Hit a primary key, those are not what we need :D if (empty($entities[1])) { continue; } $col_types[$column_name] = $entities[1]; } } // Unbueffered query and the foreach make this ultra fast, we wait for nothing. $sql = "SELECT * FROM $table_name"; $result = sqlite_unbuffered_query($db->db_connect_id, $sql); $rows = sqlite_fetch_all($result, SQLITE_ASSOC); foreach ($rows as $row) { $names = $data = array(); foreach ($row as $row_name => $row_data) { $names[] = $row_name; // Figure out what this data is, escape it properly if (is_null($row_data)) { $row_data = 'NULL'; } else if ($row_data == '') { $row_data = "''"; } else if (strpos($col_types[$row_name], 'text') !== false || strpos($col_types[$row_name], 'char') !== false || strpos($col_types[$row_name], 'blob') !== false) { $row_data = "'" . $row_data . "'"; } $data[] = $row_data; } $sql_data .= 'INSERT INTO ' . $table_name . ' (' . implode(', ', $names) . ') VALUES ('. implode(', ', $data) .");\n"; if ($store == true) { $write($fp, $sql_data); } if ($download == true) { if (!empty($oper)) { echo $oper($sql_data); } else { echo $sql_data; } } $sql_data = ''; } $db->sql_freeresult($result); break; case 'postgres': $ary_type = $ary_name = array(); // Grab all of the data from current table. $sql = "SELECT * FROM $table_name"; $result = $db->sql_query($sql); $i_num_fields = pg_num_fields($result); $seq = ''; for ($i = 0; $i < $i_num_fields; $i++) { $ary_type[$i] = pg_field_type($result, $i); $ary_name[$i] = pg_field_name($result, $i); $sql = "SELECT pg_get_expr(d.adbin, d.adrelid) as rowdefault FROM pg_attrdef d, pg_class c WHERE (c.relname = '{$table_name}') AND (c.oid = d.adrelid)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -