From e2d8c4f2867b8d672d546e4ffbf66eb096db28f7 Mon Sep 17 00:00:00 2001 From: reine Date: Thu, 15 Mar 2012 23:53:49 +0900 Subject: [PATCH] =?utf8?q?Backup=E3=82=AF=E3=83=A9=E3=82=B9=E3=81=AB?= =?utf8?q?=E5=AF=BE=E3=81=99=E3=82=8B=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * FIX:i18nクラスの関数を使うことでDBバックアップで出力されるgzファイルが不正なフォーマットになっていたのを修正 * CHANGE:DBリストア時のクエリ読み取り処理を簡素化 --- nucleus/libs/backup.php | 190 ++++++------------------------------------------ 1 file changed, 23 insertions(+), 167 deletions(-) diff --git a/nucleus/libs/backup.php b/nucleus/libs/backup.php index 77b3d2c..23b3ce7 100644 --- a/nucleus/libs/backup.php +++ b/nucleus/libs/backup.php @@ -23,14 +23,6 @@ class Backup { /** - * Backup::$debug - * for debugging - * - * @static - */ - static private $debug = FALSE; - - /** * Backup::Backup() * Constructor * @@ -145,8 +137,8 @@ class Backup $Crc = crc32(ob_get_contents()); $contents = gzcompress(ob_get_contents()); ob_end_clean(); - echo "\x1f\x8b\x08\x00\x00\x00\x00\x00" . i18n::substr($contents, 0, i18n::strlen($contents) - 4) - . $this->gzip_PrintFourChars($Crc) . $this->gzip_PrintFourChars($Size); + echo "\x1f\x8b\x08\x00\x00\x00\x00\x00" . substr($contents, 0, strlen($contents) - 4) + . $this->gzip_PrintFourChars($Crc) . $this->gzip_PrintFourChars($Size); } exit; } @@ -184,7 +176,7 @@ class Backup private function _backup_dump_structure($tablename) { // add command to drop table on restore - echo "DROP TABLE IF EXISTS $tablename;\n"; + echo "DROP TABLE IF EXISTS '$tablename';\n\n"; $result = sql_query("SHOW CREATE TABLE $tablename"); $create = sql_fetch_assoc($result); echo $create['Create Table']; @@ -308,7 +300,7 @@ class Backup $uploadInfo = postFileInfo('backup_file'); // first of all: get uploaded file: - if ( empty($uploadInfo['name']) ) + if ( array_key_exists('name', $uploadInfo) && empty($uploadInfo['name']) ) { return 'No file uploaded'; } @@ -326,12 +318,12 @@ class Backup return 'File Upload Error'; } - if ( !preg_match("/^(text\/[a-zA-Z]+)|(application\/(x\-)?gzip(\-compressed)?)|(application\/octet-stream)$/is", $backup_file_type) ) + if ( !preg_match("#^(text/[a-zA-Z]+)|(application/(x\-)?gzip(\-compressed)?)|(application/octet-stream)$#i", $backup_file_type) ) { return 'The uploaded file is not of the correct type'; } - if ( preg_match("/\.gz/is",$backup_file_name) ) + if ( preg_match("#\.gz#i",$backup_file_name) ) { $gzip = 1; } @@ -350,9 +342,11 @@ class Backup { // decompress and read $gz_ptr = gzopen($backup_file_tmpname, 'rb'); - $sql_query = ""; + $sql_query = ''; while ( !gzeof($gz_ptr) ) + { $sql_query .= gzgets($gz_ptr, 100000); + } } else { @@ -368,170 +362,32 @@ class Backup } } - // time to execute the query - $this->_execute_queries($sql_query); - return; - } - - /** - * Backup::_execute_queries() - * Executes a SQL query - * - * @param string $sql_query sql strings - * @return void - */ - private function _execute_queries($sql_query) - { - if ( !$sql_query ) - { - return; - } - - // Strip out sql comments... - $sql_query = $this->remove_remarks($sql_query); - $pieces = $this->split_sql_file($sql_query); - - $sql_count = count($pieces); - for ( $i = 0; $i < $sql_count; $i++ ) - { - $sql = trim($pieces[$i]); - - if( !empty($sql) and $sql[0] != "#" ) - { - if ( self::$debug ) - { - debug("Executing: " . i18n::hsc($sql) . "\n"); - } - - $result = sql_query($sql); - if ( !$result ) - { - debug('SQL Error: ' . sql_error()); - } - } - } - return; - } - - /** - * Backup::remove_remarks() - * remove_remarks will strip the sql comment lines - * out of an uploaded sql file - * - * @param string $sql sql string - * @return string sql string - * - */ - public function remove_remarks($sql) - { - $output = ""; - - $lines = preg_split("#\n#", $sql); - unset($sql); - + $lines = preg_split('/[\r\n]/', $sql_query); + $query = ''; foreach ( $lines as $line ) { - if ( !preg_match('#^\##', $line) ) - { - $output .= "{$line}\n"; - } - } - - return $output; - } - - /** - * Backup::split_sql_file() - * split_sql_file will split an uploaded sql file - * into single sql statements. - * - * @param string $sql sql string - * @return array sql statements - * - */ - public function split_sql_file($sql) - { - $output = array(); - - // Split up our string into "possible" SQL statements. - $tokens = preg_split( '#;#', $sql); - unset($sql); - - // this is faster than calling count($tokens) every time thru the loop. - $token_count = count($tokens); - for ( $i = 0; $i < $token_count; $i++ ) - { - // Don't wanna add an empty string as the last thing in the array. - if ( ($i == ($token_count - 1)) && (i18n::strlen($tokens[$i] <= 0)) ) + $line = trim($line); + if ( !$line || $line[0] == '#' ) { continue; } - // even number of quotes means a complete SQL statement - if ( $this->_evenNumberOfQuotes($tokens[$i]) ) + if ( preg_match('/^(.*);$/', $line, $matches) === 0 ) { - $output[] = $tokens[$i]; - unset($tokens[$i]); - continue; + $query .= $line; } - - // incomplete sql statement. keep adding tokens until we have a complete one. - // $temp will hold what we have so far. - $temp = $tokens[$i] . ";"; - unset($tokens[$i]); - - // Do we have a complete statement yet? - $complete_stmt = false; - - for ( $j = $i + 1; (!$complete_stmt && ($j < $token_count)); $j++ ) + else { - // odd number of quotes means a completed statement - // (in combination with the odd number we had already) - if ( !$this->_evenNumberOfQuotes($tokens[$j]) ) + $query .= $matches[1]; + $result = sql_query($query); + + if ( !$result ) { - $output[] = $temp . $tokens[$j]; - unset($tokens[$j]); - unset($temp); - - // exit the loop. - $complete_stmt = true; - // make sure the outer loop continues at the right point. - $i = $j; - continue; + debug('SQL Error: ' . sql_error()); } - - // even number of unescaped quotes. We still don't have a complete statement. - // (1 odd and 1 even always make an odd) - $temp .= $tokens[$j] . ";"; - // save memory. - $tokens[$j] = ""; + $query = ''; } } - return $output; - } - - /** - * BACKUP::_evenNumberOfQuotes() - * sub function of split_sql_file - * taken from phpBB - * - * @param string $text text string - * @return boolean - */ - private function _evenNumberOfQuotes($text) - { - // This is the total number of single quotes in the token. - $total_quotes = preg_match_all("#'#", $text, $matches); - // Counts single quotes that are preceded by an odd number of backslashes, - // which means they're escaped quotes. - $escaped_quotes = preg_match_all("#(?