OSDN Git Service

MERGE: リビジョン1873〜1893。skinnable-masterのマージ
[nucleus-jp/nucleus-next.git] / nucleus / libs / backup.php
index 64ae2da..dcc5d71 100644 (file)
-<?php\r
-/*\r
- * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
- * Copyright (C) 2002-2012 The Nucleus Group\r
- *\r
- * This program is free software; you can redistribute it and/or\r
- * modify it under the terms of the GNU General Public License\r
- * as published by the Free Software Foundation; either version 2\r
- * of the License, or (at your option) any later version.\r
- * (see nucleus/documentation/index.html#license for more info)\r
- */\r
-/**\r
- * Scripts to create/restore a backup of the Nucleus database\r
- *\r
- * @license http://nucleuscms.org/license.txt GNU General Public License\r
- * @copyright Copyright (C) 2002-2012 The Nucleus Group\r
- * @version $Id: backup.php 1624 2012-01-09 11:36:20Z sakamocchi $\r
- */\r
-\r
-class Backup\r
-{\r
-       /**\r
-        * Backup::Backup()\r
-        * Constructor, just for compatibility\r
-        *\r
-        * @deprecated\r
-        * @param       void\r
-        * @return      void\r
-        *\r
-        */\r
-       public function Backup()\r
-       {\r
-               return;\r
-       }\r
-\r
-       /**\r
-        * Backup::do_backup()\r
-        * This function creates an sql dump of the database and sends it to\r
-        * the user as a file (can be gzipped if they want)\r
-        *\r
-        * NOTE: this remains not-static for compatibility\r
-        *\r
-        * @param boolean       $gzip   1 = compress backup file, 0 = no compression (default)\r
-        * @return      void\r
-        *\r
-        */\r
-       public function do_backup($gzip = 0)\r
-       {\r
-               global $manager, $nucleus;\r
-\r
-               // tables of which backup is needed\r
-               $tables = array(\r
-                               sql_table('actionlog'),\r
-                               sql_table('ban'),\r
-                               sql_table('blog'),\r
-                               sql_table('comment'),\r
-                               sql_table('config'),\r
-                               sql_table('item'),\r
-                               sql_table('karma'),\r
-                               sql_table('member'),\r
-                               sql_table('skin'),\r
-                               sql_table('skin_desc'),\r
-                               sql_table('team'),\r
-                               sql_table('template'),\r
-                               sql_table('template_desc'),\r
-                               sql_table('plugin'),\r
-                               sql_table('plugin_event'),\r
-                               sql_table('plugin_option'),\r
-                               sql_table('plugin_option_desc'),\r
-                               sql_table('category'),\r
-                               sql_table('activation'),\r
-                               sql_table('tickets'),\r
-               );\r
-\r
-               // add tables that plugins want to backup to the list\r
-               // catch all output generated by plugins\r
-               ob_start();\r
-               $query = sprintf('SELECT pfile FROM %s', sql_table('plugin'));\r
-               $res = DB::getResult($query);\r
-               foreach ( $res as $row )\r
-               {\r
-                       $plug =& $manager->getPlugin($row['pfile']);\r
-                       if ( $plug )\r
-                       {\r
-                               $tables = array_merge($tables, (array) $plug->getTableList());\r
-                       }\r
-               }\r
-               ob_end_clean();\r
-\r
-               // remove duplicates\r
-               $tables = array_unique($tables);\r
-\r
-               // make sure browsers don't cache the backup\r
-               header("Pragma: no-cache");\r
-\r
-               // don't allow gzip compression when extension is not loaded\r
-               if ( ($gzip != 0) && !extension_loaded("zlib") )\r
-               {\r
-                       $gzip = 0;\r
-               }\r
-\r
-               if ( !$gzip )\r
-               {\r
-                       $filename = 'nucleus_db_backup_' . i18n::formatted_datetime('%Y-%m-%d-%H-%M-%S', time()) . ".sql";\r
-               }\r
-               else\r
-               {\r
-                       // use an output buffer\r
-                       @ob_start();\r
-                       @ob_implicit_flush(0);\r
-\r
-                       // set filename\r
-                       $filename = 'nucleus_db_backup_' . i18n::formatted_datetime('%Y-%m-%d-%H-%M-%S', time()) . ".sql.gz";\r
-               }\r
-\r
-               // send headers that tell the browser a file is coming\r
-               header("Content-Type: text/x-delimtext; name=\"$filename\"");\r
-               header("Content-disposition: attachment; filename=$filename");\r
-\r
-               // dump header\r
-               echo "/*\n";\r
-               echo " * This is a backup file generated by Nucleus \n";\r
-               echo " * http://www.nucleuscms.org/\n";\r
-               echo " * \n";\r
-               echo " * backup-date: " . i18n::formatted_datetime('rfc822GMT', time()) . "\n";\r
-               echo " * Nucleus CMS version: " . $nucleus['version'] . "\n";\r
-               echo " * \n";\r
-               echo " * WARNING: Only try to restore on servers running the exact same version of Nucleus\n";\r
-               echo " */\n";\r
-\r
-               // dump all tables\r
-               reset($tables);\r
-               array_walk($tables, array(__CLASS__, 'dump_table'));\r
-\r
-               if ( $gzip )\r
-               {\r
-                       $Size = ob_get_length();\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" . substr($contents, 0, strlen($contents) - 4)\r
-                       . self::gzip_print_four_characters($Crc) . self::gzip_print_four_characters($Size);\r
-               }\r
-               exit;\r
-       }\r
-\r
-       /**\r
-        * Backup::dump_table()\r
-        * Creates a dump for a single table\r
-        * ($tablename and $key are filled in by array_walk)\r
-        *\r
-        * @static\r
-        * @param       string  $tablename\r
-        * @param       string  $key\r
-        */\r
-       static private function dump_table($tablename, $key)\r
-       {\r
-               echo "/*\n";\r
-               echo " * TABLE: " . $tablename . "\n";\r
-               echo " */\n";\r
-\r
-               // dump table structure\r
-               self::dump_structure($tablename);\r
-\r
-               // dump table contents\r
-               self::dump_contents($tablename);\r
-               return;\r
-       }\r
-\r
-       /**\r
-        * Backup::dump_structure()\r
-        * Creates a dump of the table structure for one table\r
-        *\r
-        * @static\r
-        * @param       string  $tablename\r
-        * @return      void\r
-        *\r
-        */\r
-       static private function dump_structure($tablename)\r
-       {\r
-               // add command to drop table on restore\r
-               echo "DROP TABLE IF EXISTS {$tablename};\n\n";\r
-               $result = DB::getRow("SHOW CREATE TABLE {$tablename}");\r
-               echo $result['Create Table'];\r
-               echo ";\n\n";\r
-               return;\r
-       }\r
-\r
-       /**\r
-        * Backup::get_field_names()\r
-        * Returns the field named for the given table in the\r
-        * following format:\r
-        * (column1, column2, ..., columnn)\r
-        *\r
-        * @static\r
-        * @param       resource        $result\r
-        * @param       integer $num_fields\r
-        * @return      string\r
-        */\r
-       static private function get_field_names($result, $num_fields)\r
-       {\r
-               $fields = array();\r
-               for ( $j = 0; $j < $num_fields; $j++ )\r
-               {\r
-                       $col = $result->getColumnMeta($j);\r
-                       $fields[] = $col['name'];\r
-               }\r
-\r
-               return '(' . implode(', ', $fields) . ')';\r
-       }\r
-\r
-       /**\r
-        * Backup::dump_contents()\r
-        * Creates a dump of the table content for one table\r
-        *\r
-        * @static\r
-        * @param       string  $tablename\r
-        * @return      void\r
-        *\r
-        */\r
-       static private function dump_contents($tablename)\r
-       {\r
-               /*\r
-                * Grab the data from the table.\r
-               */\r
-               $result = DB::getResult("SELECT * FROM $tablename");\r
-\r
-               if ( $result->rowCount() > 0 )\r
-               {\r
-                       echo "\n";\r
-                       echo "/*\n";\r
-                       echo " * Table Data for {$tablename}\n";\r
-                       echo " */\n";\r
-               }\r
-\r
-               $num_fields = $result->columnCount();\r
-\r
-               /*\r
-                * Compose fieldname list\r
-               */\r
-               $tablename_list = self::get_field_names($result, $num_fields);\r
-\r
-               /*\r
-                * Loop through the resulting rows and build the sql statement.\r
-               */\r
-               foreach ( $result as $row )\r
-               {\r
-                       // Start building the SQL statement.\r
-                       echo 'INSERT INTO ' . $tablename . ' ' . $tablename_list . ' VALUES(';\r
-\r
-                       // Loop through the rows and fill in data for each column\r
-                       for ( $j = 0; $j < $num_fields; $j++ )\r
-                       {\r
-                               if ( !isset($row[$j]) )\r
-                               {\r
-                                       // no data for column\r
-                                       echo ' NULL';\r
-                               }\r
-                               elseif ( $row[$j] != '' )\r
-                               {\r
-                                       // data\r
-                                       echo ' ' . DB::quoteValue($row[$j]);\r
-                               }\r
-                               else\r
-                               {\r
-                                       // empty column (!= no data!)\r
-                                       echo "''";\r
-                               }\r
-\r
-                               // only add comma when not last column\r
-                               if ( $j != ($num_fields - 1) )\r
-                               {\r
-                                       echo ',';\r
-                               }\r
-                       }\r
-                       echo ");\n";\r
-               }\r
-               echo "\n";\r
-               return;\r
-       }\r
-\r
-       /**\r
-        * Backup::gzip_print_four_characters()\r
-        *\r
-        * @static\r
-        * @param       integer $val\r
-        * @return      integer\r
-        */\r
-       static private function gzip_print_four_characters($Val)\r
-       {\r
-               for ( $i = 0; $i < 4; $i ++ )\r
-               {\r
-                       $return .= chr($Val % 256);\r
-                       $Val = floor($Val / 256);\r
-               }\r
-               return $return;\r
-       }\r
-\r
-       /**\r
-        * Backup::do_restore()\r
-        * Restores a database backup\r
-        *\r
-        * NOTE: this remains not-static for compatibility\r
-        *\r
-        * @param       void\r
-        * @return      void\r
-        */\r
-       public function do_restore()\r
-       {\r
-               $uploadInfo = postFileInfo('backup_file');\r
-\r
-               // first of all: get uploaded file:\r
-               if ( array_key_exists('name', $uploadInfo) && empty($uploadInfo['name']) )\r
-               {\r
-                       return 'No file uploaded';\r
-               }\r
-               if ( !is_uploaded_file($uploadInfo['tmp_name']) )\r
-               {\r
-                       return 'No file uploaded';\r
-               }\r
-\r
-               $backup_file_name = $uploadInfo['name'];\r
-               $backup_file_tmpname = $uploadInfo['tmp_name'];\r
-               $backup_file_type = $uploadInfo['type'];\r
-\r
-               if ( !file_exists($backup_file_tmpname) )\r
-               {\r
-                       return 'File Upload Error';\r
-               }\r
-\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
-               $gzip = 0;\r
-               if ( preg_match("#\.gz#i", $backup_file_name) )\r
-               {\r
-                       $gzip = 1;\r
-               }\r
-\r
-               if ( !extension_loaded("zlib") && $gzip )\r
-               {\r
-                       return 'Cannot decompress gzipped backup (zlib package not installed)';\r
-               }\r
-\r
-               // get sql query according to gzip setting (either decompress, or not)\r
-               $contents = self::get_contents($backup_file_tmpname, $gzip);\r
-               if ( $contents == '' )\r
-               {\r
-                       return 'Cannot get contents from this file.';\r
-               }\r
-\r
-               /* detect lines */\r
-               $lines = preg_split('/[\r\n]/', $contents);\r
-               if( $lines === $contents )\r
-               {\r
-                       return 'Cannot parse contents from this file';\r
-               }\r
-\r
-               /* get sql statements from each lines */\r
-               $queries = self::get_queries($lines);\r
-               if ( $queries === array() )\r
-               {\r
-                       return "Cannot get SQL queries from this file.";\r
-               }\r
-\r
-               /* execute sql statements */\r
-               foreach ( $queries as $query )\r
-               {\r
-                       if ( DB::execute($query) === FALSE )\r
-                       {\r
-                               $error = DB::getError();\r
-                               debug('SQL Error: ' . $error[2]);\r
-                               break;\r
-                       }\r
-                       continue;\r
-               }\r
-               return;\r
-       }\r
-\r
-       static private function get_contents($temporary_name, $gzip = 0)\r
-       {\r
-               $contents = '';\r
-               if ( $gzip )\r
-               {\r
-                       // decompress and read\r
-                       $gz_ptr = gzopen($temporary_name, 'rb');\r
-                       while ( !gzeof($gz_ptr) )\r
-                       {\r
-                               $contents .= gzgets($gz_ptr, 100000);\r
-                       }\r
-               }\r
-               else\r
-               {\r
-                       // just read\r
-                       $fsize = filesize($temporary_name);\r
-                       if ( $fsize > 0 )\r
-                       {\r
-                               $contents = fread(fopen($temporary_name, 'r'), $fsize);\r
-                       }\r
-               }\r
-               return $contents;\r
-       }\r
-\r
-       static private function get_queries($lines)\r
-       {\r
-               $query = '';\r
-               $queries = array();\r
-               foreach ( $lines as $line )\r
-               {\r
-                       $line = trim($line);\r
-                       if ( !$line || $line[0] == '#' || preg_match('#^[\s|/]?\*#', $line) )\r
-                       {\r
-                               continue;\r
-                       }\r
-\r
-                       if ( preg_match('/^(.*);$/', $line, $matches) === 0 )\r
-                       {\r
-                               $query .= $line;\r
-                       }\r
-                       else\r
-                       {\r
-                               $query .= $matches[1];\r
-                               $queries[] = $query;\r
-                               $query = '';\r
-                       }\r
-                       continue;\r
-               }\r
-               return $queries;\r
-       }\r
+<?php
+/*
+ * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
+ * Copyright (C) 2002-2009 The Nucleus Group
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ * (see nucleus/documentation/index.html#license for more info)
+ */
+/**
+ * Scripts to create/restore a backup of the Nucleus database
+ *
+ * @license http://nucleuscms.org/license.txt GNU General Public License
+ * @copyright Copyright (C) 2002-2009 The Nucleus Group
+ * @version $Id: backup.php 1812 2012-05-01 14:59:07Z sakamocchi $
+ */
+
+class Backup
+{
+       /**
+        * Backup::Backup()
+        * Constructor, just for compatibility
+        *
+        * @deprecated
+        * @param       void
+        * @return      void
+        *
+        */
+       public function Backup()
+       {
+               return;
+       }
+
+       /**
+        * Backup::do_backup()
+        * This function creates an sql dump of the database and sends it to
+        * the user as a file (can be gzipped if they want)
+        *
+        * NOTE: this remains not-static for compatibility
+        *
+        * @param boolean       $gzip   1 = compress backup file, 0 = no compression (default)
+        * @return      void
+        *
+        */
+       public function do_backup($gzip = 0)
+       {
+               global $manager, $nucleus;
+
+               // tables of which backup is needed
+               $tables = array(
+                               sql_table('actionlog'),
+                               sql_table('ban'),
+                               sql_table('blog'),
+                               sql_table('comment'),
+                               sql_table('config'),
+                               sql_table('item'),
+                               sql_table('karma'),
+                               sql_table('member'),
+                               sql_table('skin'),
+                               sql_table('skin_desc'),
+                               sql_table('team'),
+                               sql_table('template'),
+                               sql_table('template_desc'),
+                               sql_table('plugin'),
+                               sql_table('plugin_event'),
+                               sql_table('plugin_option'),
+                               sql_table('plugin_option_desc'),
+                               sql_table('category'),
+                               sql_table('activation'),
+                               sql_table('tickets'),
+               );
+
+               // add tables that plugins want to backup to the list
+               // catch all output generated by plugins
+               ob_start();
+               $query = sprintf('SELECT pfile FROM %s', sql_table('plugin'));
+               $res = DB::getResult($query);
+               foreach ( $res as $row )
+               {
+                       $plug =& $manager->getPlugin($row['pfile']);
+                       if ( $plug )
+                       {
+                               $tables = array_merge($tables, (array) $plug->getTableList());
+                       }
+               }
+               ob_end_clean();
+
+               // remove duplicates
+               $tables = array_unique($tables);
+
+               // make sure browsers don't cache the backup
+               header("Pragma: no-cache");
+
+               // don't allow gzip compression when extension is not loaded
+               if ( ($gzip != 0) && !extension_loaded("zlib") )
+               {
+                       $gzip = 0;
+               }
+
+               if ( !$gzip )
+               {
+                       $filename = 'nucleus_db_backup_' . i18n::formatted_datetime('%Y-%m-%d-%H-%M-%S', time()) . ".sql";
+               }
+               else
+               {
+                       // use an output buffer
+                       @ob_start();
+                       @ob_implicit_flush(0);
+                               
+                       // set filename
+                       $filename = 'nucleus_db_backup_' . i18n::formatted_datetime('%Y-%m-%d-%H-%M-%S', time()) . ".sql.gz";
+               }
+
+               // send headers that tell the browser a file is coming
+               header("Content-Type: text/x-delimtext; name=\"$filename\"");
+               header("Content-disposition: attachment; filename=$filename");
+
+               // dump header
+               echo "/*\n";
+               echo " * This is a backup file generated by Nucleus \n";
+               echo " * http://www.nucleuscms.org/\n";
+               echo " * \n";
+               echo " * backup-date: " . i18n::formatted_datetime('rfc822GMT', time()) . "\n";
+               echo " * Nucleus CMS version: " . $nucleus['version'] . "\n";
+               echo " * \n";
+               echo " * WARNING: Only try to restore on servers running the exact same version of Nucleus\n";
+               echo " */\n";
+
+               // dump all tables
+               reset($tables);
+               /* NOTE: hope to use 'self' keyword here but works bad so here use __CLASS__ macro. */
+               array_walk($tables, array(__CLASS__, 'dump_table'));
+
+               if ( $gzip )
+               {
+                       $Size = ob_get_length();
+                       $Crc = crc32(ob_get_contents());
+                       $contents = gzcompress(ob_get_contents());
+                       ob_end_clean();
+                       echo "\x1f\x8b\x08\x00\x00\x00\x00\x00" . substr($contents, 0, strlen($contents) - 4)
+                       . self::gzip_print_four_characters($Crc) . self::gzip_print_four_characters($Size);
+               }
+               exit;
+       }
+
+       /**
+        * Backup::dump_table()
+        * Creates a dump for a single table
+        * ($tablename and $key are filled in by array_walk)
+        *
+        * @static
+        * @param       string  $tablename
+        * @param       string  $key
+        */
+       static private function dump_table($tablename, $key)
+       {
+               echo "/*\n";
+               echo " * TABLE: " . $tablename . "\n";
+               echo " */\n";
+
+               // dump table structure
+               self::dump_structure($tablename);
+
+               // dump table contents
+               self::dump_contents($tablename);
+               return;
+       }
+
+       /**
+        * Backup::dump_structure()
+        * Creates a dump of the table structure for one table
+        *
+        * @static
+        * @param       string  $tablename
+        * @return      void
+        *
+        */
+       static private function dump_structure($tablename)
+       {
+               // add command to drop table on restore
+               echo "DROP TABLE IF EXISTS {$tablename};\n\n";
+               $result = DB::getRow("SHOW CREATE TABLE {$tablename}");
+               echo $result['Create Table'];
+               echo ";\n\n";
+               return;
+       }
+
+       /**
+        * Backup::get_field_names()
+        * Returns the field named for the given table in the
+        * following format:
+        * (column1, column2, ..., columnn)
+        *
+        * @static
+        * @param       resource        $result
+        * @param       integer $num_fields
+        * @return      string
+        */
+       static private function get_field_names($result, $num_fields)
+       {
+               $fields = array();
+               for ( $j = 0; $j < $num_fields; $j++ )
+               {
+                       $col = $result->getColumnMeta($j);
+                       $fields[] = $col['name'];
+               }
+
+               return '(' . implode(', ', $fields) . ')';
+       }
+
+       /**
+        * Backup::dump_contents()
+        * Creates a dump of the table content for one table
+        *
+        * @static
+        * @param       string  $tablename
+        * @return      void
+        *
+        */
+       static private function dump_contents($tablename)
+       {
+               /*
+                * Grab the data from the table.
+               */
+               $result = DB::getResult("SELECT * FROM $tablename");
+
+               if ( $result->rowCount() > 0 )
+               {
+                       echo "\n";
+                       echo "/*\n";
+                       echo " * Table Data for {$tablename}\n";
+                       echo " */\n";
+               }
+
+               $num_fields = $result->columnCount();
+
+               /*
+                * Compose fieldname list
+               */
+               $tablename_list = self::get_field_names($result, $num_fields);
+
+               /*
+                * Loop through the resulting rows and build the sql statement.
+               */
+               foreach ( $result as $row )
+               {
+                       // Start building the SQL statement.
+                       echo 'INSERT INTO ' . $tablename . ' ' . $tablename_list . ' VALUES(';
+                               
+                       // Loop through the rows and fill in data for each column
+                       for ( $j = 0; $j < $num_fields; $j++ )
+                       {
+                               if ( !isset($row[$j]) )
+                               {
+                                       // no data for column
+                                       echo ' NULL';
+                               }
+                               elseif ( $row[$j] != '' )
+                               {
+                                       // data
+                                       echo ' ' . DB::quoteValue($row[$j]);
+                               }
+                               else
+                               {
+                                       // empty column (!= no data!)
+                                       echo "''";
+                               }
+
+                               // only add comma when not last column
+                               if ( $j != ($num_fields - 1) )
+                               {
+                                       echo ',';
+                               }
+                       }
+                       echo ");\n";
+               }
+               echo "\n";
+               return;
+       }
+
+       /**
+        * Backup::gzip_print_four_characters()
+        *
+        * @static
+        * @param       integer $val
+        * @return      integer
+        */
+       static private function gzip_print_four_characters($Val)
+       {
+               for ( $i = 0; $i < 4; $i ++ )
+               {
+                       $return .= chr($Val % 256);
+                       $Val = floor($Val / 256);
+               }
+               return $return;
+       }
+
+       /**
+        * Backup::do_restore()
+        * Restores a database backup
+        *
+        * NOTE: this remains not-static for compatibility
+        *
+        * @param       void
+        * @return      void
+        */
+       public function do_restore()
+       {
+               $uploadInfo = postFileInfo('backup_file');
+
+               // first of all: get uploaded file:
+               if ( array_key_exists('name', $uploadInfo) && empty($uploadInfo['name']) )
+               {
+                       return 'No file uploaded';
+               }
+               if ( !is_uploaded_file($uploadInfo['tmp_name']) )
+               {
+                       return 'No file uploaded';
+               }
+
+               $backup_file_name = $uploadInfo['name'];
+               $backup_file_tmpname = $uploadInfo['tmp_name'];
+               $backup_file_type = $uploadInfo['type'];
+
+               if ( !file_exists($backup_file_tmpname) )
+               {
+                       return 'File Upload Error';
+               }
+
+               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';
+               }
+
+               $gzip = 0;
+               if ( preg_match("#\.gz#i", $backup_file_name) )
+               {
+                       $gzip = 1;
+               }
+
+               if ( !extension_loaded("zlib") && $gzip )
+               {
+                       return 'Cannot decompress gzipped backup (zlib package not installed)';
+               }
+
+               // get sql query according to gzip setting (either decompress, or not)
+               $contents = self::get_contents($backup_file_tmpname, $gzip);
+               if ( $contents == '' )
+               {
+                       return 'Cannot get contents from this file.';
+               }
+
+               /* detect lines */
+               $lines = preg_split('/[\r\n]/', $contents);
+               if( $lines === $contents )
+               {
+                       return 'Cannot parse contents from this file';
+               }
+
+               /* get sql statements from each lines */
+               $queries = self::get_queries($lines);
+               if ( $queries === array() )
+               {
+                       return "Cannot get SQL queries from this file.";
+               }
+
+               /* execute sql statements */
+               foreach ( $queries as $query )
+               {
+                       if ( DB::execute($query) === FALSE )
+                       {
+                               $error = DB::getError();
+                               debug('SQL Error: ' . $error[2]);
+                               break;
+                       }
+                       continue;
+               }
+               return;
+       }
+
+       static private function get_contents($temporary_name, $gzip = 0)
+       {
+               $contents = '';
+               if ( $gzip )
+               {
+                       // decompress and read
+                       $gz_ptr = gzopen($temporary_name, 'rb');
+                       while ( !gzeof($gz_ptr) )
+                       {
+                               $contents .= gzgets($gz_ptr, 100000);
+                       }
+               }
+               else
+               {
+                       // just read
+                       $fsize = filesize($temporary_name);
+                       if ( $fsize > 0 )
+                       {
+                               $contents = fread(fopen($temporary_name, 'r'), $fsize);
+                       }
+               }
+               return $contents;
+       }
+
+       static private function get_queries($lines)
+       {
+               $query = '';
+               $queries = array();
+               foreach ( $lines as $line )
+               {
+                       $line = trim($line);
+                       if ( !$line || $line[0] == '#' || preg_match('#^[\s|/]?\*#', $line) )
+                       {
+                               continue;
+                       }
+
+                       if ( preg_match('/^(.*);$/', $line, $matches) === 0 )
+                       {
+                               $query .= $line;
+                       }
+                       else
+                       {
+                               $query .= $matches[1];
+                               $queries[] = $query;
+                               $query = '';
+                       }
+                       continue;
+               }
+               return $queries;
+       }
 }
\ No newline at end of file