Exemplo n.º 1
0
/* ---  vystup  --- */
$output .= '<br />';
if (isset($_POST['do_backup']) && _loginright_adminbackup) {
    /* ----- tvorba zalohy ----- */
    // typ zalohy (0 = db, 1 = partial, 2 = full)
    if (isset($_POST['type_full'])) {
        $type = _backup_full;
        $type_name = 'full';
    } elseif (isset($_POST['type_partial'])) {
        $type = _backup_partial;
        $type_name = 'partial';
    } else {
        $type = _backup_db;
        $type_name = 'db';
    }
    $type_ext = _backupExt($type);
    // nazev souboru
    $fname = @str_replace('.', '_', _getDomain());
    if ($fname == '') {
        $fname = _mysql_db;
    }
    $fname .= '_' . date("Y_m_d");
    // komprese
    $can_compress = extension_loaded('zlib');
    $memlimit = _phpIniLimit('memory_limit');
    $should_compress = !isset($memlimit) || $memlimit > 10485760;
    // velikosti
    $sizes = array();
    // velikost databaze
    $sizes['db'] = 10240;
    // +- 10kB struktura databaze
/**
 * [ADMIN] Backup API - vytvorit zalohu
 * @param int $type typ zalohy (0 = jen databaze, 1 = castecna, 2 = kompletni)
 * @param int $compress nastaveni komprese (0 = bez, 1 = rychla, 2 = nejlepsi)
 * @param string|resource|null $fname nazev souboru pro odeslani na vystup, resource j*z otevreneho souboru nebo null (= neodesilat, vratit)
 * @param string|null $note textova poznamka nebo null
 * @param array|null $extra_dirs extra adresare pro pridani (pouze typ >= 1) nebo null
 * @return string|null pokud je $fname null, vrati instanci KZip archivu, v pripade kompletni zalohy jako pole array(kzip_inst, script_php_code)), jinak null
 */
function _backupCreate($type, $compress, $fname = null, $note = null, $extra_dirs = null)
{
    // nastaveni komprese
    if ($compress === 1) {
        $compress = 3;
    } elseif ($compress === 2) {
        $compress = 9;
    } else {
        $compress = null;
    }
    // bez komprese
    // vychozi konfigurace
    $installer = false;
    $dirs_root = false;
    $dirs_ensure = false;
    $dirs_merge = null;
    $add_voidfile = false;
    $add_cronlock = false;
    $version = _systemversion;
    $void_fname = '___void';
    $ext = _backupExt($type);
    // konfigurace dle typu
    switch ($type) {
        // jen databaze
        case _backup_db:
            $dirs = array();
            $version = _checkVersion('database', null, true);
            $version = $version[0];
            break;
            // castecna zaloha
        // castecna zaloha
        case _backup_partial:
            $dirs = array('pictures', 'plugins');
            $dirs_ensure = true;
            if (isset($extra_dirs)) {
                $dirs = array_merge($dirs, $extra_dirs);
                $dirs_merge = $extra_dirs;
            }
            break;
            // uplna zaloha
        // uplna zaloha
        case _backup_full:
            $installer = true;
            $dirs_root = true;
            $dirs = array('admin', 'pictures', 'plugins', 'remote', 'require', 'data/installer');
            $dirs_ensure = true;
            $add_voidfile = true;
            $add_cronlock = true;
            if (isset($extra_dirs)) {
                $dirs = array_merge($dirs, $extra_dirs);
                $dirs_merge = $extra_dirs;
            }
            break;
            // spatny typ
        // spatny typ
        default:
            trigger_error('Neplatny typ zalohy', E_USER_WARNING);
            return;
    }
    /* ---  tvorba zalohy  --- */
    // hlavicky
    if (is_string($fname)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/force-download');
        header('Content-Disposition: attachment; filename="' . $fname . '.' . $ext . '"');
        header('Cache-Control: no-cache, must-revalidate');
        header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
    }
    // tvorba a konfigurace archivu
    $kzip = new KZip();
    if (!$kzip->has_zlib) {
        $compress = null;
    } elseif (isset($compress)) {
        $kzip->setComprLevel($compress);
    }
    $kzip->vars = array('idt' => _backup_idt, 'version' => $version, 'system_version' => _systemversion, 'system_state' => _systemstate, 'system_revision' => _systemstate_revision, 'type' => $type, 'void' => $void_fname, 'time' => time(), 'url' => _url, 'size' => 0, 'note' => isset($note) ? strval($note) : null, 'merge' => $dirs_merge);
    // databaze - struktura a data
    $dbdump = new DBDump();
    $struct = $dbdump->exportTables();
    $kzip->addFile($struct[1], '/database/struct');
    $data = $dbdump->exportData();
    $kzip->addFile($data[1], '/database/data');
    // adresare
    if (!empty($dirs)) {
        for ($i = 0; isset($dirs[$i]); ++$i) {
            $localDirPath = _indexroot . $dirs[$i] . '/';
            $archiveDirPath = '/files/' . $dirs[$i] . '/';
            $kzip->addDir($localDirPath, $archiveDirPath, true, 'pictures' === $dirs[$i] ? '_backupFilterThumbnails' : null);
        }
    }
    // soubory z korenoveho adresare
    if ($dirs_root) {
        $kzip->addDir(_indexroot, '/files/');
        $kzip->removeFile('/files/.htaccess');
        // soubor .htaccess se nezalohuje
        $kzip->removeFile('/files/config.php');
        // soubor config.php se nezalohuje
    }
    // voidfile
    if ($add_voidfile) {
        $kzip->addFile(_void_file, '/files/data/');
    }
    // cron lock file
    if ($add_cronlock) {
        $kzip->addfile(_indexroot . 'data/cron.lock', '/files/data/');
    }
    // spocitat velikost vstupnich souboru
    $files = $kzip->listAll();
    for ($i = 0; isset($files[$i]); ++$i) {
        $kzip->vars['size'] += $kzip->getFileSize($files[$i]);
    }
    // vynutit existenci adresaru
    if ($dirs_ensure) {
        _backupEnsureDirs($kzip, $void_fname, $type, !in_array('upload', $dirs));
    }
    // vlozeni instalatoru
    if ($installer) {
        global $_lang;
        require _indexroot . 'config.php';
        if (!isset($locale)) {
            $locale = array('czech', 'utf8', 'cz_CZ');
        }
        if (!isset($timezone)) {
            $timezone = 'Europe/Prague';
        }
        $inst = file_get_contents(_indexroot . 'data/installer/script.php');
        $inst = '<?php ' . substr($inst, strpos($inst, '/* @@@begin@@@ */') + 17);
        $inst = str_replace(array('$__locale', '$__timezone', '$__time_format', '/* @@@core@@@ */', '/* @@@kzip@@@ */', '/* @@@dbdump@@@ */', '/* @@@lang@@@ */', '/* @@@s_states@@@ */'), array(var_export($locale, true), var_export($timezone, true), var_export(_time_format, true), substr(file_get_contents(_indexroot . 'require/load.php'), 5), substr(file_get_contents(_indexroot . 'require/class/kzip.php'), 5), substr(file_get_contents(_indexroot . 'require/class/dbdump.php'), 5), '$_lang = ' . var_export($_lang['installer'] + array('type.full' => $_lang['admin.other.backup.backup.full'], 'dbdump' => $_lang['dbdump'], 'global.continue' => $_lang['global.continue'], 'global.cancel' => $_lang['global.cancel'], 'global.note' => $_lang['global.note']), true) . ';', '$_sys_states = ' . var_export(SL::$states, true) . ';'), $inst);
        if (is_string($fname)) {
            echo $inst;
        }
    }
    // sestaveni archivu
    if (isset($fname)) {
        // do vystupu ci souboru
        if (is_string($fname)) {
            $kzip->packToOutput(isset($compress));
        } else {
            if ($installer) {
                fwrite($fname, $inst);
            }
            $kzip->packToFile(null, false, isset($compress), $fname);
        }
        $kzip->free();
    } elseif ($installer) {
        // vratit kzip + kod instalatoru
        return array($kzip, $inst);
    } else {
        return $kzip;
    }
}