<commit_before><?php
/**
 * The import function to import the phpMyFAQ backups
 *
 * PHP Version 5.3
 *
 * This Source Code Form is subject to the terms of the Mozilla Public License,
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
 * obtain one at http://mozilla.org/MPL/2.0/.
 *
 * @category  phpMyFAQ 
 * @package   Administration
 * @author    <NAME> <<EMAIL>>
 * @copyright 2003-2014 phpMyFAQ Team
 * @license   http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
 * @link      http://www.phpmyfaq.de
 * @since     2003-02-24
 */

if (!defined('IS_VALID_PHPMYFAQ')) {
    $protocol = 'http';
    if (isset($_SERVER['HTTPS']) && strtoupper($_SERVER['HTTPS']) === 'ON'){
        $protocol = 'https';
    }
    header('Location: ' . $protocol . '://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['SCRIPT_NAME']));
    exit();
}

$csrfToken = PMF_Filter::filterInput(INPUT_POST, 'csrf', FILTER_SANITIZE_STRING);

if (!isset($_SESSION['phpmyfaq_csrf_token']) || $_SESSION['phpmyfaq_csrf_token'] !== $csrfToken) {
    $permission['restore'] = false; 
}
?>
    <header>
        <h2>
            <i class="icon-download-alt"></i> <?php echo $PMF_LANG['ad_csv_rest'] ?>
        </h2>
    </header>

<?php
if ($permission['restore']) {

    if (isset($_FILES['userfile']) && 0 == $_FILES['userfile']['error']) {
        
        $ok  = 1;
        $finfo = new finfo(FILEINFO_MIME_ENCODING);
        if ('utf-8' == $finfo->file($_FILES['userfile']['tmp_name'])) {
            print 'This file is not UTF_8 encoded.';
            $ok = 0;
        }
        $handle          = fopen($_FILES['userfile']['tmp_name'], 'r');
        $dat             = fgets($handle, 65536);
        $versionFound    = PMF_String::substr($dat, 0, 9);
        $versionExpected = '-- pmf' . substr($faqConfig->get('main.currentVersion'), 0, 3);

        if ($versionFound !== $versionExpected) {
            printf('%s (Version check failure: "%s" found, "%s" expected)',
                $PMF_LANG['ad_csv_no'],
                $versionFound,
                $versionExpected
            );
            $ok = 0;
        } else {
            // @todo: Start transaction for better recovery if something really bad happens
            $dat = trim(PMF_String::substr($dat, 11));
            $tbl = explode(' ', $dat);
            $num = count($tbl);
            for ($h = 0; $h < $num; $h++) {
                $mquery[] = 'DELETE FROM '.$tbl[$h];
            }
            $ok = 1;
        }

        if ($ok == 1) {
            $table_prefix = '';
            printf("<p>%s</p>\n", $PMF_LANG['ad_csv_prepare']);
            while ($dat = fgets($handle, 65536)) {
                $dat                       = trim($dat);
                $backup_prefix_pattern     = "-- pmftableprefix:";
                $backup_prefix_pattern_len = PMF_String::strlen($backup_prefix_pattern);
                if (PMF_String::substr($dat, 0, $backup_prefix_pattern_len) == $backup_prefix_pattern) {
                    $table_prefix = trim(PMF_String::substr($dat, $backup_prefix_pattern_len));
                }
                if ( (PMF_String::substr($dat, 0, 2) != '--') && ($dat != '') ) {
                    $mquery[] = trim(PMF_String::substr($dat, 0, -1));
                }
            }

            $k = 0;
            $g = 0;
            printf("<p>%s</p>\n", $PMF_LANG['ad_csv_process']);
            $num = count($mquery);
            $kg  = '';
            for ($i = 0; $i < $num; $i++) {
                $mquery[$i] = PMF_DB_Helper::alignTablePrefix($mquery[$i], $table_prefix, PMF_Db::getTablePrefix());
                $query      = $faqConfig->getDb()->escape($mquery[$i]);
                $kg         = $faqConfig->getDb()->query($query);
                if (!$kg) {
                    printf(
                        '<div class="alert alert-error"><strong>Query</strong>: "%s" failed (Reason: %s)</div>%s',
                        PMF_String::htmlspecialchars($query, ENT_QUOTES, 'utf-8'),
                        $faqConfig->getDb()->error(),
                        "\n"
                    );
                    $k++;
                } else {
                    printf(
                        '<!-- <div class="alert alert-success"><strong>Query</strong>: "%s" okay</div> -->%s',
                        PMF_String::htmlspecialchars($query, ENT_QUOTES, 'utf-8'),
                        "\n"
                    );
                    $g++;
                }
            }
            printf(
                '<p class="alert alert-success">%d %s %d %s</p>',
                $g,
                $PMF_LANG['ad_csv_of'],
                $num,<commit_msg>Fixed restore from backup<commit_after><?php
/**
 * The import function to import the phpMyFAQ backups
 *
 * PHP Version 5.3
 *
 * This Source Code Form is subject to the terms of the Mozilla Public License,
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
 * obtain one at http://mozilla.org/MPL/2.0/.
 *
 * @category  phpMyFAQ 
 * @package   Administration
 * @author    <NAME> <<EMAIL>>
 * @copyright 2003-2014 phpMyFAQ Team
 * @license   http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
 * @link      http://www.phpmyfaq.de
 * @since     2003-02-24
 */

if (!defined('IS_VALID_PHPMYFAQ')) {
    $protocol = 'http';
    if (isset($_SERVER['HTTPS']) && strtoupper($_SERVER['HTTPS']) === 'ON'){
        $protocol = 'https';
    }
    header('Location: ' . $protocol . '://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['SCRIPT_NAME']));
    exit();
}

$csrfToken = PMF_Filter::filterInput(INPUT_POST, 'csrf', FILTER_SANITIZE_STRING);

if (!isset($_SESSION['phpmyfaq_csrf_token']) || $_SESSION['phpmyfaq_csrf_token'] !== $csrfToken) {
    $permission['restore'] = false; 
}
?>
    <header>
        <h2>
            <i class="icon-download-alt"></i> <?php echo $PMF_LANG['ad_csv_rest'] ?>
        </h2>
    </header>

<?php
if ($permission['restore']) {

    if (isset($_FILES['userfile']) && 0 == $_FILES['userfile']['error']) {
        
        $ok  = 1;
        $finfo = new finfo(FILEINFO_MIME_ENCODING);
        if ('utf-8' == $finfo->file($_FILES['userfile']['tmp_name'])) {
            print 'This file is not UTF_8 encoded.';
            $ok = 0;
        }
        $handle          = fopen($_FILES['userfile']['tmp_name'], 'r');
        $dat             = fgets($handle, 65536);
        $versionFound    = PMF_String::substr($dat, 0, 9);
        $versionExpected = '-- pmf' . substr($faqConfig->get('main.currentVersion'), 0, 3);

        if ($versionFound !== $versionExpected) {
            printf('%s (Version check failure: "%s" found, "%s" expected)',
                $PMF_LANG['ad_csv_no'],
                $versionFound,
                $versionExpected
            );
            $ok = 0;
        } else {
            // @todo: Start transaction for better recovery if something really bad happens
            $dat = trim(PMF_String::substr($dat, 11));
            $tbl = explode(' ', $dat);
            $num = count($tbl);
            for ($h = 0; $h < $num; $h++) {
                $mquery[] = 'DELETE FROM '.$tbl[$h];
            }
            $ok = 1;
        }

        if ($ok == 1) {
            $table_prefix = '';
            printf("<p>%s</p>\n", $PMF_LANG['ad_csv_prepare']);
            while ($dat = fgets($handle, 65536)) {
                $dat                       = trim($dat);
                $backup_prefix_pattern     = "-- pmftableprefix:";
                $backup_prefix_pattern_len = PMF_String::strlen($backup_prefix_pattern);
                if (PMF_String::substr($dat, 0, $backup_prefix_pattern_len) == $backup_prefix_pattern) {
                    $table_prefix = trim(PMF_String::substr($dat, $backup_prefix_pattern_len));
                }
                if ( (PMF_String::substr($dat, 0, 2) != '--') && ($dat != '') ) {
                    $mquery[] = trim(PMF_String::substr($dat, 0, -1));
                }
            }

            $k = 0;
            $g = 0;
            printf("<p>%s</p>\n", $PMF_LANG['ad_csv_process']);
            $num = count($mquery);
            $kg  = '';
            for ($i = 0; $i < $num; $i++) {
                $mquery[$i] = PMF_DB_Helper::alignTablePrefix($mquery[$i], $table_prefix, PMF_Db::getTablePrefix());
                $kg         = $faqConfig->getDb()->query($mquery[$i]);
                if (!$kg) {
                    printf(
                        '<div class="alert alert-error"><strong>Query</strong>: "%s" failed (Reason: %s)</div>%s',
                        PMF_String::htmlspecialchars($query, ENT_QUOTES, 'utf-8'),
                        $faqConfig->getDb()->error(),
                        "\n"
                    );
                    $k++;
                } else {
                    printf(
                        '<!-- <div class="alert alert-success"><strong>Query</strong>: "%s" okay</div> -->%s',
                        PMF_String::htmlspecialchars($query, ENT_QUOTES, 'utf-8'),
                        "\n"
                    );
                    $g++;
                }
            }
            printf(
                '<p class="alert alert-success">%d %s %d %s</p>',
                $g,
                $PMF_LANG['ad_csv_of'],
                $num,
{'fraction_non_alphanumeric': 0.1738241308793456, 'fraction_numerical': 0.013561511139812722, 'mean_word_length': 1.7201405152224825, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6253019', 'n_tokens_mistral': 3005, 'n_tokens_neox': 2817, 'n_words': 568}