Index: branches/RC/core/units/admin/admin_events_handler.php =================================================================== diff -u -N -r10718 -r10721 --- branches/RC/core/units/admin/admin_events_handler.php (.../admin_events_handler.php) (revision 10718) +++ branches/RC/core/units/admin/admin_events_handler.php (.../admin_events_handler.php) (revision 10721) @@ -599,7 +599,7 @@ } /** - * Stops users import & redirect to import template + * Stops Backup & redirect to Backup template * * @param kEvent $event */ @@ -608,6 +608,16 @@ $event->redirect = 'tools/backup1'; } + /** + * Stops Restore & redirect to Restore template + * + * @param kEvent $event + */ + function OnRestoreCancel(&$event) + { + $event->redirect = 'tools/restore1'; + } + function performBackup() { $backupProgress = unserialize($this->Application->RecallVar('adm.backup_status')); @@ -622,6 +632,10 @@ if ($a_records['num'] < 50) { $backupProgress['table_num']++; + if ($backupProgress['table_names'][$backupProgress['table_num']] == TABLE_PREFIX.'Cache') { + $backupProgress['table_num']++; + } + $backupProgress['record_count'] = 0; } else { $backupProgress['record_count']+=50; } @@ -684,14 +698,15 @@ $values_sql .= $this->Conn->qstr($field_value).','; } $values_sql = preg_replace('/(.*),$/', '\\1', $values_sql); - $temp .= 'INSERT INTO '.$table.' ('.$fields_sql.') VALUES ('.$values_sql.')'; + $sql = 'INSERT INTO '.$table.' ('.$fields_sql.') VALUES ('.$values_sql.');'; + $sql=ereg_replace("\n","\\n", $sql); + $sql=ereg_replace("\r","\\r", $sql); + $temp .= $sql."\n"; } - $temp=ereg_replace("\n","\\n", $temp); - $temp=ereg_replace("\r","\\r", $temp); if(strlen(TABLE_PREFIX)) { - $temp = str_replace("INSERT INTO $prefix","INSERT INTO ",$temp); + $temp = str_replace("INSERT INTO ".TABLE_PREFIX, "INSERT INTO ", $temp); } return Array( @@ -758,6 +773,284 @@ return $schema_create; } + /** + * Deletes one backup file + * + * @param kEvent $event + */ + function OnDeleteBackup(&$event) + { + @unlink($this->get_backup_file()); + } + function get_backup_file() + { + return $this->Application->ConfigValue('Backup_Path').'/dump'.$this->Application->GetVar('backupdate').'.txt'; + } + /** + * Starts restore process + * + * @param kEvent $event + */ + function OnRestore(&$event) + { + $file = $this->get_backup_file(); + + $restoreProgress = Array ( + 'file_pos' => 0, + 'file_name' => $file, + 'file_size' => filesize($file), + ); + $this->Application->RemoveVar('adm.restore_success'); + $this->Application->StoreVar('adm.restore_status', serialize($restoreProgress)); + $event->redirect = 'tools/restore3'; + } + + function OnRestoreProgress(&$event) + { + $done_percent = $this->performRestore(); + + if ($done_percent == -3) { + $event->redirect = 'tools/restore4'; + return ; + } + + if ($done_percent < 0) { + $this->Application->StoreVar('adm.restore_error', 'File read error'); $event->redirect = 'tools/restore4'; + return ; + } + + if ($done_percent == 100) { + $this->replaceRestoredFiles(); + $this->Application->StoreVar('adm.restore_success', 1); + $event->redirect = 'tools/restore4'; + return ; + } + + echo $done_percent; + $event->status = erSTOP; + + } + + function replaceRestoredFiles() + { + // gather restored table names + $tables = $this->Conn->GetCol('SHOW TABLES'); + $mask_restore_table = '/^restore'.TABLE_PREFIX.'(.*)$/'; + foreach($tables as $table) + { + if( preg_match($mask_restore_table,$table,$rets) ) + { + $old_table = substr($table, 7); + $this->Conn->Query('DROP TABLE IF EXISTS '.$old_table); + $this->Conn->Query('CREATE TABLE '.$old_table.' LIKE '.$table); + $this->Conn->Query('INSERT INTO '.$old_table.' SELECT * FROM '.$table); + $this->Conn->Query('DROP TABLE '.$table); + } + } + + } + + function performRestore() + { + $restoreProgress = unserialize($this->Application->RecallVar('adm.restore_status')); + $filename = $restoreProgress['file_name']; + $FileOffset = $restoreProgress['file_pos']; + $MaxLines = 200; + $size = filesize($filename); + + if($FileOffset > $size) { + return -2; + } + + $fp = fopen($filename,"r"); + if(!$fp) { + return -1; + } + + + if($FileOffset>0) + { + fseek($fp,$FileOffset); + } + else + { + $EndOfSQL = FALSE; + $sql = ""; + while(!feof($fp) && !$EndOfSQL) + { + $l = fgets($fp,16384); + if(substr($l,0,11)=="INSERT INTO") + { + $EndOfSQL = TRUE; + } + else + { + $sql .= $l; + $FileOffset = ftell($fp) - strlen($l); + } + } + if(strlen($sql)) + { + $error = $this->runSchemaText($sql); + if ($error != '') { + + $this->Application->StoreVar('adm.restore_error', $error); + return -3; + } + } + fseek($fp,$FileOffset); + } + + $LinesRead = 0; + $sql = ""; + $AllSql = array(); + while($LinesRead < $MaxLines && !feof($fp)) + { + $sql = fgets($fp, 16384); + if(strlen($sql)) + { + $AllSql[] = $sql; + $LinesRead++; + } + } + if(!feof($fp)) + { + $FileOffset = ftell($fp); + } + else + { + $FileOffset = $size; + } + fclose($fp); + + if(count($AllSql)>0) { + $error = $this->runSQLText($AllSql); + if ($error != '') { + + $this->Application->StoreVar('adm.restore_error', $error); + return -3; + } + + } + $restoreProgress['file_pos'] = $FileOffset; + $this->Application->StoreVar('adm.restore_status', serialize($restoreProgress)); + + return round($FileOffset/$size * 100); +// $this->Application->StoreVar('adm.restore_error', 'lalalal'); +// $event->redirect = 'tools/restore4'; + } + + + function runSchemaText($sql) + { + $table_prefix = 'restore'.TABLE_PREFIX; +// $table_prefix = TABLE_PREFIX; + if(strlen($table_prefix)) + { + $what = "CREATE TABLE "; + $replace = "CREATE TABLE ".$table_prefix; + $sql = ereg_replace($what, $replace, $sql); + + $what = "DROP TABLE "; + $replace = "DROP TABLE IF EXISTS ".$table_prefix; + $sql = ereg_replace($what, $replace, $sql); + + $what = "INSERT INTO "; + $replace = "INSERT INTO ".$table_prefix; + $sql = ereg_replace($what, $replace, $sql); + + $what = "UPDATE "; + $replace = "UPDATE ".$table_prefix; + $sql = ereg_replace($what, $replace, $sql); + + $what = "ALTER TABLE "; + $replace = "ALTER TABLE ".$table_prefix; + $sql = ereg_replace($what, $replace, $sql); + } + + $commands = explode("# --------------------------------------------------------",$sql); + if(count($commands)>0) + { +// $query_func = getConnectionInterface('query',$dbo_type); +// $errorno_func = getConnectionInterface('errorno',$dbo_type); +// $errormsg_func = getConnectionInterface('errormsg',$dbo_type); + + for($i = 0; $i < count($commands); $i++) + { + $cmd = $commands[$i]; + $cmd = trim($cmd); + if(strlen($cmd)>0) + { + $this->Conn->Query($cmd); + if($this->Conn->errorCode != 0) + { + return $this->Conn->errorMessage." COMMAND:
$cmd
"; + } + } + } + } + } + + function runSQLText($allsql) + { + $line = 0; +// $query_func = getConnectionInterface('query',$dbo_type); +// $errorno_func = getConnectionInterface('errorno',$dbo_type); +// $errormsg_func = getConnectionInterface('errormsg',$dbo_type); + + while($line0 && substr($sql,0,1)!="#") + { + $table_prefix = 'restore'.TABLE_PREFIX; + if(strlen($table_prefix)) + { + $what = "CREATE TABLE "; + $replace = "CREATE TABLE ".$table_prefix; + $sql = ereg_replace($what, $replace, $sql); + + $what = "DELETE FROM "; + $replace = "DELETE FROM ".$table_prefix; + $sql = ereg_replace($what, $replace, $sql); + + $what = "DROP TABLE "; + $replace = "DROP TABLE IF EXISTS ".$table_prefix; + $sql = ereg_replace($what, $replace, $sql); + + $what = "INSERT INTO "; + $replace = "INSERT INTO ".$table_prefix; + $sql = ereg_replace($what, $replace, $sql); + + $what = "REPLACE INTO "; + $replace = "REPLACE INTO ".$table_prefix; + $sql = ereg_replace($what, $replace, $sql); + + $what = "UPDATE "; + $replace = "UPDATE ".$table_prefix; + $sql = ereg_replace($what, $replace, $sql); + + $what = "ALTER TABLE "; + $replace = "ALTER TABLE ".$table_prefix; + $sql = ereg_replace($what, $replace, $sql); + } + $sql = trim($sql); + if(strlen($sql)>0) + { + $this->Conn->Query($sql); + + if($this->Conn->errorCode != 0) + { + return $this->Conn->errorMessage." COMMAND:
$sql
"; + } + } + } + $line++; + } + } + + + } \ No newline at end of file