class Backup\r
{\r
/**\r
- * Backup::$debug\r
- * for debugging\r
- * \r
- * @static\r
- */\r
- static private $debug = FALSE;\r
- \r
- /**\r
* Backup::Backup()\r
* Constructor\r
* \r
$Crc = crc32(ob_get_contents());\r
$contents = gzcompress(ob_get_contents());\r
ob_end_clean();\r
- echo "\x1f\x8b\x08\x00\x00\x00\x00\x00" . i18n::substr($contents, 0, i18n::strlen($contents) - 4)\r
- . $this->gzip_PrintFourChars($Crc) . $this->gzip_PrintFourChars($Size);\r
+ echo "\x1f\x8b\x08\x00\x00\x00\x00\x00" . substr($contents, 0, strlen($contents) - 4)\r
+ . $this->gzip_PrintFourChars($Crc) . $this->gzip_PrintFourChars($Size);\r
}\r
exit;\r
}\r
private function _backup_dump_structure($tablename)\r
{\r
// add command to drop table on restore\r
- echo "DROP TABLE IF EXISTS $tablename;\n";\r
+ echo "DROP TABLE IF EXISTS '$tablename';\n\n";\r
$result = sql_query("SHOW CREATE TABLE $tablename");\r
$create = sql_fetch_assoc($result);\r
echo $create['Create Table'];\r
$uploadInfo = postFileInfo('backup_file');\r
\r
// first of all: get uploaded file:\r
- if ( empty($uploadInfo['name']) )\r
+ if ( array_key_exists('name', $uploadInfo) && empty($uploadInfo['name']) )\r
{\r
return 'No file uploaded';\r
}\r
return 'File Upload Error';\r
}\r
\r
- if ( !preg_match("/^(text\/[a-zA-Z]+)|(application\/(x\-)?gzip(\-compressed)?)|(application\/octet-stream)$/is", $backup_file_type) )\r
+ if ( !preg_match("#^(text/[a-zA-Z]+)|(application/(x\-)?gzip(\-compressed)?)|(application/octet-stream)$#i", $backup_file_type) )\r
{\r
return 'The uploaded file is not of the correct type';\r
}\r
\r
- if ( preg_match("/\.gz/is",$backup_file_name) )\r
+ if ( preg_match("#\.gz#i",$backup_file_name) )\r
{\r
$gzip = 1;\r
}\r
{\r
// decompress and read\r
$gz_ptr = gzopen($backup_file_tmpname, 'rb');\r
- $sql_query = "";\r
+ $sql_query = '';\r
while ( !gzeof($gz_ptr) )\r
+ {\r
$sql_query .= gzgets($gz_ptr, 100000);\r
+ }\r
}\r
else\r
{\r
}\r
}\r
\r
- // time to execute the query\r
- $this->_execute_queries($sql_query);\r
- return;\r
- }\r
- \r
- /**\r
- * Backup::_execute_queries()\r
- * Executes a SQL query\r
- * \r
- * @param string $sql_query sql strings\r
- * @return void\r
- */\r
- private function _execute_queries($sql_query)\r
- {\r
- if ( !$sql_query )\r
- {\r
- return;\r
- }\r
- \r
- // Strip out sql comments...\r
- $sql_query = $this->remove_remarks($sql_query);\r
- $pieces = $this->split_sql_file($sql_query);\r
- \r
- $sql_count = count($pieces);\r
- for ( $i = 0; $i < $sql_count; $i++ )\r
- {\r
- $sql = trim($pieces[$i]);\r
- \r
- if( !empty($sql) and $sql[0] != "#" )\r
- {\r
- if ( self::$debug )\r
- {\r
- debug("Executing: " . i18n::hsc($sql) . "\n");\r
- }\r
- \r
- $result = sql_query($sql);\r
- if ( !$result )\r
- {\r
- debug('SQL Error: ' . sql_error());\r
- }\r
- }\r
- }\r
- return;\r
- }\r
- \r
- /**\r
- * Backup::remove_remarks()\r
- * remove_remarks will strip the sql comment lines\r
- * out of an uploaded sql file\r
- * \r
- * @param string $sql sql string\r
- * @return string sql string\r
- * \r
- */\r
- public function remove_remarks($sql)\r
- {\r
- $output = "";\r
- \r
- $lines = preg_split("#\n#", $sql);\r
- unset($sql);\r
- \r
+ $lines = preg_split('/[\r\n]/', $sql_query);\r
+ $query = '';\r
foreach ( $lines as $line )\r
{\r
- if ( !preg_match('#^\##', $line) )\r
- {\r
- $output .= "{$line}\n";\r
- }\r
- }\r
- \r
- return $output;\r
- }\r
- \r
- /**\r
- * Backup::split_sql_file()\r
- * split_sql_file will split an uploaded sql file\r
- * into single sql statements.\r
- * \r
- * @param string $sql sql string\r
- * @return array sql statements\r
- * \r
- */\r
- public function split_sql_file($sql)\r
- {\r
- $output = array();\r
- \r
- // Split up our string into "possible" SQL statements.\r
- $tokens = preg_split( '#;#', $sql);\r
- unset($sql);\r
- \r
- // this is faster than calling count($tokens) every time thru the loop.\r
- $token_count = count($tokens);\r
- for ( $i = 0; $i < $token_count; $i++ )\r
- {\r
- // Don't wanna add an empty string as the last thing in the array.\r
- if ( ($i == ($token_count - 1)) && (i18n::strlen($tokens[$i] <= 0)) )\r
+ $line = trim($line);\r
+ if ( !$line || $line[0] == '#' )\r
{\r
continue;\r
}\r
\r
- // even number of quotes means a complete SQL statement\r
- if ( $this->_evenNumberOfQuotes($tokens[$i]) )\r
+ if ( preg_match('/^(.*);$/', $line, $matches) === 0 )\r
{\r
- $output[] = $tokens[$i];\r
- unset($tokens[$i]);\r
- continue;\r
+ $query .= $line;\r
}\r
- \r
- // incomplete sql statement. keep adding tokens until we have a complete one.\r
- // $temp will hold what we have so far.\r
- $temp = $tokens[$i] . ";";\r
- unset($tokens[$i]);\r
- \r
- // Do we have a complete statement yet?\r
- $complete_stmt = false;\r
- \r
- for ( $j = $i + 1; (!$complete_stmt && ($j < $token_count)); $j++ )\r
+ else\r
{\r
- // odd number of quotes means a completed statement\r
- // (in combination with the odd number we had already)\r
- if ( !$this->_evenNumberOfQuotes($tokens[$j]) )\r
+ $query .= $matches[1];\r
+ $result = sql_query($query);\r
+ \r
+ if ( !$result )\r
{\r
- $output[] = $temp . $tokens[$j];\r
- unset($tokens[$j]);\r
- unset($temp);\r
- \r
- // exit the loop.\r
- $complete_stmt = true;\r
- // make sure the outer loop continues at the right point.\r
- $i = $j;\r
- continue;\r
+ debug('SQL Error: ' . sql_error());\r
}\r
- \r
- // even number of unescaped quotes. We still don't have a complete statement.\r
- // (1 odd and 1 even always make an odd)\r
- $temp .= $tokens[$j] . ";";\r
- // save memory.\r
- $tokens[$j] = "";\r
+ $query = '';\r
}\r
}\r
- return $output;\r
- }\r
- \r
- /**\r
- * BACKUP::_evenNumberOfQuotes()\r
- * sub function of split_sql_file\r
- * taken from phpBB\r
- * \r
- * @param string $text text string\r
- * @return boolean \r
- */ \r
- private function _evenNumberOfQuotes($text)\r
- {\r
- // This is the total number of single quotes in the token.\r
- $total_quotes = preg_match_all("#'#", $text, $matches);\r
- // Counts single quotes that are preceded by an odd number of backslashes,\r
- // which means they're escaped quotes.\r
- $escaped_quotes = preg_match_all("#(?<!\\\\)(\\\\\\\\)*\\\\'#", $text, $matches);\r
- $unescaped_quotes = $total_quotes - $escaped_quotes;\r
- \r
- if ( self::$debug )\r
- {\r
- debug($total_quotes . "-" . $escaped_quotes . "-" . $unescaped_quotes);\r
- }\r
- \r
- return (boolean) ( ($unescaped_quotes % 2) == 0 );\r
+ return;\r
}\r
}\r