$restore['filename'] = urldecode($_GET['filename']);
 }
 $restore['compressed'] = 0;
 if (substr(strtolower($restore['filename']), -2) == 'gz') {
     $restore['compressed'] = 1;
 }
 $restore['dump_encoding'] = 'utf8';
 if (isset($_POST['sel_dump_encoding'])) {
     $restore['dump_encoding'] = $_POST['sel_dump_encoding'];
 }
 if (isset($_GET['dump_encoding'])) {
     $restore['dump_encoding'] = $_GET['dump_encoding'];
 }
 $restore['part'] = 0;
 // Read Statusline of file
 $sline = readStatusline($restore['filename']);
 // if it is a backup done by MSD we get the charset from the statusline
 if (isset($sline['charset']) && $sline['charset'] != '?') {
     $restore['dump_encoding'] = $sline['charset'];
 }
 // init some values
 $restore['tables_total'] = $sline['tables'];
 $restore['records_total'] = $sline['records'];
 if ($sline['part'] != 'MP_0') {
     $restore['part'] = 1;
 }
 if ($config['empty_db_before_restore'] == 1) {
     truncateDb($config['db_actual']);
 }
 $restore['max_zeit'] = intval($config['max_execution_time'] * $config['time_buffer']);
 $restore['restore_start_time'] = time();
 * http://www.mysqldumper.net
 *
 * @package         MySQLDumper
 * @version         SVN: $rev: 1207 $
 * @author          $Author$
 * @lastmodified    $Date$
 */
if (!defined('MSD_VERSION')) {
    die('No direct access.');
}
$fileName = isset($_GET['filename']) ? urldecode($_GET['filename']) : '';
if (isset($_POST['file'][0])) {
    $fileName = $_POST['file'][0];
}
$tplRestoreSelectTables = new MSDTemplate();
$tplRestoreSelectTables->set_filenames(array('tplRestoreSelectTables' => 'tpl/restore/selectTables.tpl'));
//Get Header-Infos from file
$sline = readStatusline($fileName);
if ($sline['records'] == -1) {
    // not a backup of MySQLDumper
    $tplRestoreSelectTables->assign_block_vars('NO_MSD_BACKUP', array());
} else {
    // Get Tableinfo from file header
    $tabledata = getTableHeaderInfoFromBackup($fileName);
    for ($i = 0; $i < sizeof($tabledata); $i++) {
        $klasse = $i % 2 ? 1 : '';
        $tplRestoreSelectTables->assign_block_vars('ROW', array('CLASS' => 'dbrow' . $klasse, 'ID' => $i, 'NR' => $i + 1, 'TABLENAME' => $tabledata[$i]['name'], 'RECORDS' => String::formatNumber($tabledata[$i]['records']), 'SIZE' => byteOutput($tabledata[$i]['size']), 'LAST_UPDATE' => $tabledata[$i]['update'], 'TABLETYPE' => $tabledata[$i]['engine']));
    }
}
$confirmRestore = $lang['L_FM_ALERTRESTORE1'] . ' `' . $config['db_actual'] . '`  ' . $lang['L_FM_ALERTRESTORE2'] . ' ' . $fileName . ' ' . $lang['L_FM_ALERTRESTORE3'];
$tplRestoreSelectTables->assign_vars(array('PAGETITLE' => $lang['L_RESTORE'] . ' - ' . $lang['L_TABLESELECTION'], 'DATABASE' => $config['db_actual'], 'FILENAME' => $fileName, 'ICON_OK' => $icon['ok'], 'ICON_DELETE' => $icon['delete'], 'ICON_RESTORE' => $icon['restore'], 'L_NO_MSD_BACKUP' => $lang['L_NOT_SUPPORTED'], 'CONFIRM_RESTORE' => $confirmRestore));
/**
 * Delete old backups from folder work/backup according to configuration
 *
 * @return string Outputstring with messages about deleted files
 */
function doAutoDelete()
{
    global $config, $lang, $out;
    $out = '';
    if ($config['auto_delete']['max_backup_files'] > 0) {
        //Files einlesen
        $dh = opendir($config['paths']['backup']);
        $files = array();
        // Build assoc Array $db=>$timestamp=>$filenames
        if (!function_exists('ReadStatusline')) {
            include './inc/functions/functions_files.php';
        }
        while (false !== ($filename = readdir($dh))) {
            if ($filename != '.' && $filename != '..' && !is_dir($config['paths']['backup'] . $filename)) {
                $statusline = readStatusline($filename);
                if ($statusline['dbname'] != 'unknown') {
                    $dbName = $statusline['dbname'];
                    $datum = substr($filename, strlen($dbName) + 1);
                    $timestamp = substr($datum, 0, 16);
                    if (!isset($files[$dbName])) {
                        $files[$dbName] = array();
                    }
                    if (!isset($files[$dbName][$timestamp])) {
                        $files[$dbName][$timestamp] = array();
                    }
                    $files[$dbName][$timestamp][] = $filename;
                }
            }
        }
        $out = '';
        // stores output messages
        // Backups per DB and Timestamp
        foreach ($files as $db => $val) {
            if (sizeof($val) > $config['auto_delete']['max_backup_files']) {
                $db_files = $val;
                krsort($db_files, SORT_STRING);
                //now latest backupfiles are on top -> delete all files with greater index
                $i = 0;
                foreach ($db_files as $timestamp => $filenames) {
                    if ($i >= $config['auto_delete']['max_backup_files']) {
                        // Backup too old -> delete files
                        foreach ($filenames as $f) {
                            if ($out == '') {
                                $out .= $lang['L_FM_AUTODEL1'] . '<br />';
                            }
                            if (@unlink('./' . $config['paths']['backup'] . $f)) {
                                $out .= '<span class="success">' . sprintf($lang['L_DELETE_FILE_SUCCESS'], $f) . '</span><br />';
                            } else {
                                $out .= $lang['L_ERROR'] . ': <p class="error">' . sprintf($lang['L_DELETE_FILE_ERROR'], $f) . '</p><br />';
                            }
                        }
                    }
                    $i++;
                }
            }
        }
    }
    return $out;
}
// call after selecting tables to restore
//-> save selection and head over to restore.php
if (isset($_POST['restore_tbl'])) {
    echo '<script type="text/javascript">' . 'location.href="index.php?p=restore&filename=' . urlencode($_POST['filename']) . '";</script></body></html>';
    exit;
}
if (isset($_POST['restore'])) {
    if (isset($_POST['file'])) {
        if (isset($_POST['select_tables']) && $_POST['select_tables'] == 1) {
            // select tables to restore
            include './inc/restore/select_tables.php';
            $showFilelist = false;
        } else {
            $encodingFound = true;
            $file = $_POST['file'][0];
            $statusline = readStatusline($file);
            if (isset($_POST['sel_dump_encoding_restore'])) {
                // encoding of file was selected -> we can start
                $dumpEncoding = $_POST['sel_dump_encoding_restore'];
            } else {
                if (!isset($statusline['charset']) || trim($statusline['charset']) == '?') {
                    // unknown encoding of a file not created by MySQLDumper
                    // -> ask user what encoding should be used
                    $charsets = $dbo->getCharsets();
                    foreach ($charsets as $name => $val) {
                        $charsetsDescription[$name] = $name . ' - ' . $val['Description'];
                    }
                    $tplRestoreSelectEncoding = new MSDTemplate();
                    $tplRestoreSelectEncoding->set_filenames(array('tplRestoreSelectEncoding' => 'tpl/restore/file_select_encoding.tpl'));
                    $encSelect = Html::getOptionlist($charsetsDescription, 'utf8');
                    $tplRestoreSelectEncoding->assign_vars(array('PAGETITLE' => $lang['L_FM_RESTORE'] . ': ' . $file, 'DATABASE' => $config['db_actual'], 'ENCODING_SELECT' => $encSelect, 'FILE_NAME' => $file));