execute_backup(); $this->locale = fusion_get_locale('', LOCALE.LOCALESET.'admin/db-backup.php'); \PHPFusion\BreadCrumbs::getInstance()->addBreadCrumb(['link' => ADMIN.'db_backup.php'.fusion_get_aidlink(), 'title' => $this->locale['450']]); $tab['title'][] = $this->locale['450']; $tab['id'][] = 'backup_db'; $tab['title'][] = $this->locale['480']; $tab['id'][] = 'restore_db'; $_GET['section'] = isset($_GET['section']) && in_array($_GET['section'], $tab['id']) ? $_GET['section'] : $tab['id'][0]; opentable($this->locale['450']); echo opentab($tab, $_GET['section'], 'database_tab', TRUE, 'nav-tabs m-b-20', 'section', ['action', 'section']); switch ($_GET['section']) { case 'backup_db': $this->backup_form(); break; case 'restore_db': $this->restore_form(); break; default: redirect(clean_request('', ['section'], FALSE)); } echo closetab(); closetable(); } private function execute_backup() { global $db_name, $db_prefix, $db_driver, $pdo_enabled; if (isset($_POST['btn_create_backup'])) { ini_set('max_execution_time', 0); set_time_limit(600); if (!check_admin_pass(isset($_POST['user_admin_password']) ? form_sanitizer($_POST['user_admin_password'], '', 'user_admin_password') : "")) { defender::stop(); } $db_tables = $_POST['db_tables']; if (count($db_tables) && defender::safe()) { $crlf = "\n"; ob_start(); @ob_implicit_flush(0); echo "#----------------------------------------------------------".$crlf; echo "# PHP-Fusion SQL Data Dump".$crlf; echo "# Database Name: `".$db_name."`".$crlf; echo "# Table Prefix: `".$db_prefix."`".$crlf; echo "# Date: `".date("d/m/Y H:i")."`".$crlf; echo "#----------------------------------------------------------".$crlf; dbquery('SET SQL_QUOTE_SHOW_CREATE=1'); foreach ($db_tables as $table) { if (!ini_get('safe_mode')) { @set_time_limit(1200); } dbquery("OPTIMIZE TABLE $table"); echo $crlf."#".$crlf."# Structure for Table `".$table."`".$crlf."#".$crlf; echo "DROP TABLE IF EXISTS `$table`;$crlf"; $row = dbarraynum(dbquery("SHOW CREATE TABLE $table")); echo $row[1].";".$crlf; $db = DatabaseFactory::getConnection(); $result = $db->query("SELECT * FROM $table"); $column_list = ''; $num_fields = ''; if ($result && dbrows($result)) { echo $crlf."#".$crlf."# Table Data for `".$table."`".$crlf."#".$crlf; $num_fields = $db->countColumns($result); for ($i = 0; $i < $num_fields; $i++) { if ((!empty($db_driver) && $db_driver === 'pdo' || !empty($pdo_enabled) && $pdo_enabled === 1)) { $column_meta = $result->getColumnMeta($i); $column_list .= (($column_list != "") ? ", " : "")."`".$column_meta['name']."`"; } else { $column_meta = $result->fetch_field(); $column_list .= (($column_list != "") ? ", " : "")."`".$column_meta->name."`"; } unset($column_meta); } } while ($row = dbarraynum($result)) { $dump = "INSERT INTO `$table` ($column_list) VALUES ("; for ($i = 0; $i < $num_fields; $i++) { $dump .= ($i > 0) ? ", " : ""; if (!isset($row[$i])) { $dump .= "NULL"; } else if ($row[$i] == "0" || $row[$i] != "") { $type = $this->GetSqlFieldType($table, $i); if (substr($type, 0, 7) == "tinyint" || substr($type, 0, 8) == "smallint" || substr($type, 0, 9) == "mediumint" || substr($type, 0, 3) == "int" || substr($type, 0, 6) == "bigint" || substr($type, 0, 9) == "timestamp" ) { $dump .= $row[$i]; } else { $search_array = ['\\', '\'', "\x00", "\x0a", "\x0d", "\x1a"]; $replace_array = ['\\\\', '\\\'', '\0', '\n', '\r', '\Z']; $row[$i] = str_replace($search_array, $replace_array, $row[$i]); $dump .= "'$row[$i]'"; } } else { $dump .= "''"; } } $dump .= ");"; echo $dump.$crlf; } } $contents = ob_get_contents(); ob_end_clean(); $file = form_sanitizer($_POST['backup_filename'], '', 'backup_filename'); $ext = form_sanitizer($_POST['backup_type'], '.sql', 'backup_type'); require_once INCLUDES."class.httpdownload.php"; $object = new \PHPFusion\httpdownload; $object->use_resume = FALSE; if ($ext == ".gz") { $object->set_mime("application/x-gzip gz tgz"); $object->set_bydata(gzencode($contents, 9)); $object->set_filename($file.'.sql'.$ext); } else { $object->set_mime("text/plain"); $object->set_bydata($contents); $object->set_filename($file.$ext); } $object->download(); exit; } } } private function GetSqlFieldType($table, $i) { $new_data = []; $result = dbquery("SHOW COLUMNS FROM ".$table); while ($data = dbarray($result)) { $new_data[] = $data; } return $new_data[$i]['Type']; } private function backup_form() { global $db_name, $db_prefix; $table_opt_list = ""; $result = dbquery("SHOW tables"); while ($row = dbarraynum($result)) { $table_opt_list .= "