Author: NUKEVIET commercial version 1.0
Exemple #1
0
/**
 * nv_dump_save()
 *
 * @param mixed $params
 * @return
 */
function nv_dump_save($params)
{
    global $db, $sys_info, $global_config, $db_config;
    if ($sys_info['allowed_set_time_limit']) {
        set_time_limit(1200);
    }
    if (!isset($params['tables']) or !is_array($params['tables']) or $params['tables'] == array()) {
        return false;
    }
    $params['tables'] = array_map('trim', $params['tables']);
    $tables = array();
    $dbsize = 0;
    $result = $db->query('SHOW TABLE STATUS');
    $a = 0;
    while ($item = $result->fetch()) {
        unset($m);
        if (in_array($item['name'], $params['tables'])) {
            $tables[$a]['name'] = $item['name'];
            $tables[$a]['size'] = intval($item['data_length']) + intval($item['index_length']);
            $tables[$a]['limit'] = 1 + round(1048576 / ($item['avg_row_length'] + 1));
            $tables[$a]['numrow'] = $item['rows'];
            $tables[$a]['charset'] = preg_match('/^([a-z0-9]+)_/i', $item['collation'], $m) ? $m[1] : '';
            $tables[$a]['type'] = isset($item['engine']) ? $item['engine'] : $item['t'];
            ++$a;
            $dbsize += intval($item['data_length']) + intval($item['index_length']);
        }
    }
    $result->closeCursor();
    if (empty($a)) {
        return false;
    }
    $dumpsave = new dumpsave($params['savetype'], $params['filename']);
    if (!$dumpsave->open()) {
        return false;
    }
    $path_dump = '';
    if (file_exists(NV_ROOTDIR . '/themes/' . $global_config['site_theme'] . '/system/dump.tpl')) {
        $path_dump = NV_ROOTDIR . '/themes/' . $global_config['site_theme'] . '/system/dump.tpl';
    } else {
        $path_dump = NV_ROOTDIR . '/themes/default/system/dump.tpl';
    }
    $template = explode('@@@', file_get_contents($path_dump));
    $patterns = array("/\\{\\|SERVER_NAME\\|\\}/", "/\\{\\|GENERATION_TIME\\|\\}/", "/\\{\\|SQL_VERSION\\|\\}/", "/\\{\\|PHP_VERSION\\|\\}/", "/\\{\\|DB_NAME\\|\\}/", "/\\{\\|DB_CHARACTER\\|\\}/", "/\\{\\|DB_COLLATION\\|\\}/");
    $replacements = array($db->server, gmdate("F j, Y, h:i A", NV_CURRENTTIME) . " GMT", $db->getAttribute(PDO::ATTR_SERVER_VERSION), PHP_VERSION, $db->dbname, $db_config['charset'], $db_config['collation']);
    if (!$dumpsave->write(preg_replace($patterns, $replacements, $template[0]))) {
        return false;
    }
    $db->query('SET SQL_QUOTE_SHOW_CREATE = 1');
    $a = 0;
    foreach ($tables as $table) {
        $content = $db->query('SHOW CREATE TABLE ' . $table['name'])->fetchColumn(1);
        $content = preg_replace('/(KEY[^\\(]+)(\\([^\\)]+\\))[\\s\\r\\n\\t]+(USING BTREE)/i', '\\1\\3 \\2', $content);
        $content = preg_replace('/(default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP|DEFAULT CHARSET=\\w+|COLLATE=\\w+|character set \\w+|collate \\w+|AUTO_INCREMENT=\\w+)/i', ' \\1', $content);
        $patterns = array("/\\{\\|TABLE_NAME\\|\\}/", "/\\{\\|TABLE_STR\\|\\}/");
        $replacements = array($table['name'], $content);
        if (!$dumpsave->write(preg_replace($patterns, $replacements, $template[1]))) {
            return false;
        }
        if ($params['type'] == 'str') {
            continue;
        }
        if (!empty($table['numrow'])) {
            $patterns = array("/\\{\\|TABLE_NAME\\|\\}/");
            $replacements = array($table['name']);
            if (!$dumpsave->write(preg_replace($patterns, $replacements, $template[2]))) {
                return false;
            }
            $columns = array();
            $columns_array = $db->columns_array($table['name']);
            foreach ($columns_array as $col) {
                $columns[$col['field']] = preg_match('/^(\\w*int|year)/', $col['type']) ? 'int' : 'txt';
            }
            $maxi = ceil($table['numrow'] / $table['limit']);
            $from = 0;
            $a = 0;
            for ($i = 0; $i < $maxi; ++$i) {
                $db->sqlreset()->select('*')->from($table['name'])->limit($table['limit'])->offset($from);
                $result = $db->query($db->sql());
                while ($row = $result->fetch()) {
                    if (isset($row['bodyhtml'])) {
                        $row['bodyhtml'] = strtr($row['bodyhtml'], array("\r\n" => '', "\r" => '', "\n" => ''));
                    } elseif (isset($row['bodytext'])) {
                        $row['bodytext'] = strtr($row['bodytext'], array("\r\n" => ' ', "\r" => ' ', "\n" => ' '));
                    }
                    $row2 = array();
                    foreach ($columns as $key => $kt) {
                        $row2[] = isset($row[$key]) ? $kt == 'int' ? $row[$key] : "'" . addslashes($row[$key]) . "'" : 'NULL';
                    }
                    $row2 = NV_EOL . '(' . implode(', ', $row2) . ')';
                    ++$a;
                    if ($a < $table['numrow']) {
                        if (!$dumpsave->write($row2 . ', ')) {
                            return false;
                        }
                    } else {
                        if (!$dumpsave->write($row2 . ';')) {
                            return false;
                        }
                        break;
                    }
                }
                $result->closeCursor();
                $from += $table['limit'];
            }
        }
    }
    if (!$dumpsave->close()) {
        return false;
    }
    return array($params['filename'], $dbsize);
}
Exemple #2
0
/**
 * nv_dump_save()
 * 
 * @param mixed $params
 * @return
 */
function nv_dump_save($params)
{
    global $db, $sys_info, $global_config;
    if ($sys_info['allowed_set_time_limit']) {
        set_time_limit(1200);
    }
    if (!isset($params['tables']) or !is_array($params['tables']) or $params['tables'] == array()) {
        return false;
    }
    $params['tables'] = array_map("trim", $params['tables']);
    $tables = array();
    $dbsize = 0;
    $result = $db->sql_query("SHOW TABLE STATUS");
    $a = 0;
    while ($item = $db->sql_fetch_assoc($result)) {
        unset($m);
        if (in_array($item['Name'], $params['tables'])) {
            $tables[$a]['name'] = $item['Name'];
            $tables[$a]['size'] = intval($item['Data_length']) + intval($item['Index_length']);
            $tables[$a]['limit'] = 1 + round(1048576 / ($item['Avg_row_length'] + 1));
            $tables[$a]['numrow'] = $item['Rows'];
            $tables[$a]['charset'] = preg_match("/^([a-z0-9]+)_/i", $item['Collation'], $m) ? $m[1] : "";
            $tables[$a]['type'] = isset($item['Engine']) ? $item['Engine'] : $item['Type'];
            $a++;
            $dbsize += intval($item['Data_length']) + intval($item['Index_length']);
        }
    }
    $db->sql_freeresult($result);
    if (empty($a)) {
        return false;
    }
    $dumpsave = new dumpsave($params['savetype'], $params['filename']);
    if (!$dumpsave->open()) {
        return false;
    }
    $path_dump = "";
    if (file_exists(NV_ROOTDIR . "/themes/" . $global_config['site_theme'] . "/system/dump.tpl")) {
        $path_dump = NV_ROOTDIR . "/themes/" . $global_config['site_theme'] . "/system/dump.tpl";
    } else {
        $path_dump = NV_ROOTDIR . "/themes/default/system/dump.tpl";
    }
    $template = explode("@@@", file_get_contents($path_dump));
    $patterns = array("/\\{\\|SERVER_NAME\\|\\}/", "/\\{\\|GENERATION_TIME\\|\\}/", "/\\{\\|SQL_VERSION\\|\\}/", "/\\{\\|PHP_VERSION\\|\\}/", "/\\{\\|DB_NAME\\|\\}/");
    $replacements = array($db->server, gmdate("F j, Y, h:i A", NV_CURRENTTIME) . " GMT", $db->sql_version, PHP_VERSION, $db->dbname);
    if (!$dumpsave->write(preg_replace($patterns, $replacements, $template[0]))) {
        return false;
    }
    $db->sql_query("SET NAMES 'utf8'");
    $db->sql_query("SET SQL_QUOTE_SHOW_CREATE = 1");
    $a = 0;
    foreach ($tables as $table) {
        $result = $db->sql_query("SHOW CREATE TABLE `" . $table['name'] . "`");
        $content = $db->sql_fetchrow($result);
        $db->sql_freeresult($result);
        $content = preg_replace('/(KEY[^\\(]+)(\\([^\\)]+\\))[\\s\\r\\n\\t]+(USING BTREE)/i', '\\1\\3 \\2', $content[1]);
        $content = preg_replace('/(default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP|DEFAULT CHARSET=\\w+|COLLATE=\\w+|character set \\w+|collate \\w+|AUTO_INCREMENT=\\w+)/i', ' \\1', $content);
        $patterns = array("/\\{\\|TABLE_NAME\\|\\}/", "/\\{\\|TABLE_STR\\|\\}/");
        $replacements = array($table['name'], $content);
        if (!$dumpsave->write(preg_replace($patterns, $replacements, $template[1]))) {
            return false;
        }
        if ($params['type'] == 'str') {
            continue;
        }
        if (!empty($table['numrow'])) {
            $patterns = array("/\\{\\|TABLE_NAME\\|\\}/");
            $replacements = array($table['name']);
            if (!$dumpsave->write(preg_replace($patterns, $replacements, $template[2]))) {
                return false;
            }
            $columns = array();
            $result = $db->sql_query("SHOW COLUMNS FROM `" . $table['name'] . "`");
            while ($col = $db->sql_fetchrow($result)) {
                $columns[$col['Field']] = preg_match("/^(\\w*int|year)/", $col[1]) ? 'int' : 'txt';
            }
            $db->sql_freeresult($result);
            $maxi = ceil($table['numrow'] / $table['limit']);
            $from = 0;
            $a = 0;
            for ($i = 0; $i < $maxi; $i++) {
                $result = $db->sql_query("SELECT * FROM `" . $table['name'] . "` LIMIT " . $from . ", " . $table['limit'] . "");
                while ($row = $db->sql_fetchrow($result)) {
                    $row2 = array();
                    foreach ($columns as $key => $kt) {
                        $row2[] = isset($row[$key]) ? $kt == 'int' ? $row[$key] : "'" . mysql_real_escape_string($row[$key]) . "'" : "NULL";
                    }
                    $row2 = NV_EOL . "(" . implode(", ", $row2) . ")";
                    $a++;
                    if ($a < $table['numrow']) {
                        if (!$dumpsave->write($row2 . ", ")) {
                            return false;
                        }
                    } else {
                        if (!$dumpsave->write($row2 . ";")) {
                            return false;
                        }
                        break;
                    }
                }
                $db->sql_freeresult($result);
                $from += $table['limit'];
            }
        }
    }
    if (!$dumpsave->close()) {
        return false;
    }
    return array($params['filename'], $dbsize);
}