Index: branches/5.2.x/core/units/admin/admin_events_handler.php =================================================================== diff -u -N -r14699 -r14707 --- branches/5.2.x/core/units/admin/admin_events_handler.php (.../admin_events_handler.php) (revision 14699) +++ branches/5.2.x/core/units/admin/admin_events_handler.php (.../admin_events_handler.php) (revision 14707) @@ -1,6 +1,6 @@ Application->ConfigValue('Backup_Path'); + $backup_helper =& $this->Application->recallObject('BackupHelper'); + /* @var $backup_helper BackupHelper */ - $file_helper =& $this->Application->recallObject('FileHelper'); - /* @var $file_helper FileHelper */ - - if (!$file_helper->CheckFolder($backup_path) || !is_writable($backup_path)) { + if ( !$backup_helper->initBackup() ) { $event->status = kEvent::erFAIL; - - $this->Application->SetVar('error_msg', $this->Application->Phrase('la_Text_backup_access')); - return ; } - $a_tables = $this->Conn->GetCol('SHOW TABLES'); // array_keys($tables); - $TableNames = Array(); - for($x=0;$x 0, - 'table_names' => $TableNames, - 'table_count' => count($TableNames), - 'record_count' => 0, - 'file_name' => $backup_path."/dump".adodb_mktime().".txt", - ); - $this->Application->RemoveVar('adm.backupcomplete_filename'); - $this->Application->RemoveVar('adm.backupcomplete_filesize'); - - $out = array(); - - for($x=0;$xGetTableCreate($TableNames[$x]); - } - } - - $fp = fopen($backupProgress['file_name'], 'a'); - - $sql = "SELECT Name, Version FROM ".TABLE_PREFIX."Modules"; - $r = $this->Conn->Query($sql); - foreach ($r AS $a_module) { - $version = $a_module['Version']; - fwrite($fp, "# ".$a_module['Name']." Version: $version;\n"); - } - fwrite($fp, "#------------------------------------------\n\n"); - - fwrite($fp,implode("\n",$out)); - fwrite($fp,"\n"); - - fclose($fp); - - $this->Application->StoreVar('adm.backup_status', serialize($backupProgress)); $event->redirect = 'tools/backup2'; } @@ -756,15 +705,18 @@ */ function OnBackupProgress(&$event) { - $done_percent = $this->performBackup(); + $backup_helper =& $this->Application->recallObject('BackupHelper'); + /* @var $backup_helper BackupHelper */ - if ($done_percent == 100) { + $done_percent = $backup_helper->performBackup(); + + if ( $done_percent == 100 ) { $event->redirect = 'tools/backup3'; - return ; + return; } - echo $done_percent; $event->status = kEvent::erSTOP; + echo $done_percent; } /** @@ -778,422 +730,65 @@ } /** - * 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')); -// echo "
"; print_r($backupProgress); echo "
"; -// exit; - $CurrentTable = $backupProgress['table_names'][$backupProgress['table_num']]; - - // get records - $a_records = $this->insert_data($CurrentTable,$backupProgress['record_count'],50,""); -// echo "
"; print_r($a_records); echo "
"; -// exit; - - 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; - } - - if ($a_records['sql']) { - $fp = fopen($backupProgress['file_name'], 'a'); - fwrite($fp, $a_records['sql']); - fclose($fp); - } - $percent = ($backupProgress['table_num'] / $backupProgress['table_count']) * 100; - if ($percent >= 100) { - $percent = 100; - $this->Application->StoreVar('adm.backupcomplete_filename', $backupProgress['file_name']); - $this->Application->StoreVar('adm.backupcomplete_filesize', round(filesize($backupProgress['file_name'])/1024/1024, 2)); // Mbytes - } else { - $this->Application->StoreVar('adm.backup_status', serialize($backupProgress)); - } - - return round($percent); - - } - - //extracts the rows of data from tables using limits - function insert_data($table, $start, $limit, $mywhere) - { -// global $out; - - if ($mywhere !="") - { - $whereclause= " WHERE ".$mywhere." "; - } - else - { - $whereclause = ""; - } - - $a_data = $this->Conn->Query("SELECT * from $table $whereclause LIMIT $start, $limit"); -// echo "SELECT * from $table $whereclause LIMIT $start, $limit"; -// echo "
"; print_r($a_records); echo "
"; -// exit; - if (!$a_data) { - return Array( - 'num' => 0, - 'sql' => '', - ); - } -// $prefix = GetTablePrefix(); - $rowcount = 0; - $a_fields = array_keys($a_data[0]); - $fields_sql = ''; - foreach ($a_fields AS $field_name) { - $fields_sql .= '`'.$field_name.'`,'; - } - $fields_sql = substr($fields_sql, 0, -1); - $temp = ''; - foreach ($a_data AS $a_row) - { - $values_sql = ''; - foreach ($a_row as $field_name => $field_value) { - $values_sql .= $this->Conn->qstr($field_value).','; - } - $values_sql = substr($values_sql, 0, -1); - $sql = 'INSERT INTO '.$table.' ('.$fields_sql.') VALUES ('.$values_sql.');'; - $sql = str_replace("\n", "\\n", $sql); - $sql = str_replace("\r", "\\r", $sql); - $temp .= $sql."\n"; - } - - if(strlen(TABLE_PREFIX)) - { - $temp = str_replace("INSERT INTO ".TABLE_PREFIX, "INSERT INTO ", $temp); - } - - return Array( - 'num' => count($a_data), - 'sql' => $temp, - ); - } - - - - function GetTableCreate($table, $crlf="\n") - { - $schema_create = 'DROP TABLE IF EXISTS ' . $table . ';' . $crlf; - $schema_create .="# --------------------------------------------------------".$crlf; - $this->Conn->Query("SET SQL_QUOTE_SHOW_CREATE = 0"); - $tmpres = $this->Conn->Query("SHOW CREATE TABLE $table"); -// echo "
"; print_r($tmpres); echo "
"; -// exit; - if(is_array($tmpres) && isset($tmpres[0])) - { - $tmpres = $tmpres[0]; - $pos = strpos($tmpres["Create Table"], ' ('); - $pos2 = strpos($tmpres["Create Table"], '('); - if ($pos2 != $pos + 1) - { - $pos = $pos2; - $tmpres["Create Table"] = str_replace(",", ",\n ", $tmpres["Create Table"]); - } - - $tmpres["Create Table"] = substr($tmpres["Create Table"], 0, 13) - . (($use_backquotes) ? $tmpres["Table"] : $tmpres["Table"]) - . substr($tmpres["Create Table"], $pos); - $tmpres["Create Table"] = str_replace("\n", $crlf, $tmpres["Create Table"]); - if (preg_match_all('((,\r?\n[\s]*(CONSTRAINT|FOREIGN[\s]*KEY)[^\r\n,]+)+)', $tmpres["Create Table"], $regs)) { - if (!isset($sql_constraints)) { - if (isset($GLOBALS['no_constraints_comments'])) { - $sql_constraints = ''; - } else { - $sql_constraints = $crlf . '#' . $crlf - . '# ' . $GLOBALS['strConstraintsForDumped'] . $crlf - . '#' . $crlf; - } - } - if (!isset($GLOBALS['no_constraints_comments'])) { - $sql_constraints .= $crlf .'#' . $crlf .'# ' . $GLOBALS['strConstraintsForTable'] . ' ' . $table . $crlf . '#' . $crlf; - } - $sql_constraints .= 'ALTER TABLE $table $crlf ' - . preg_replace('/(,\r?\n|^)([\s]*)(CONSTRAINT|FOREIGN[\s]*KEY)/', '\1\2ADD \3', substr($regs[0][0], 2)) - . ";\n"; - $tmpres["Create Table"] = preg_replace('((,\r?\n[\s]*(CONSTRAINT|FOREIGN[\s]*KEY)[^\r\n,]+)+)', '', $tmpres["Create Table"]); - } - $schema_create .= $tmpres["Create Table"]; - } - if(strlen($schema_create)) - { - $schema_create = str_replace("DROP TABLE IF EXISTS ".TABLE_PREFIX,"DROP TABLE ",$schema_create); - $schema_create = str_replace("CREATE TABLE ".TABLE_PREFIX,"CREATE TABLE ",$schema_create); - while(strlen($schema_create && substr($schema_create,-1)!=")")) - { - $schema_create = substr($schema_create,0,-1); - } - } - $schema_create .= "\n# --------------------------------------------------------\n"; - 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(); + $backup_helper =& $this->Application->recallObject('BackupHelper'); + /* @var $backup_helper BackupHelper */ - $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)); + $backup_helper->initRestore(); $event->redirect = 'tools/restore3'; } function OnRestoreProgress(&$event) { - $done_percent = $this->performRestore(); + $backup_helper =& $this->Application->recallObject('BackupHelper'); + /* @var $backup_helper BackupHelper */ - if ($done_percent == -3) { + $done_percent = $backup_helper->performRestore(); + + if ( $done_percent == BackupHelper::SQL_ERROR_DURING_RESTORE ) { $event->redirect = 'tools/restore4'; - return ; } - - if ($done_percent < 0) { - $this->Application->StoreVar('adm.restore_error', 'File read error'); $event->redirect = 'tools/restore4'; - return ; + elseif ( $done_percent == BackupHelper::FAILED_READING_BACKUP_FILE ) { + $this->Application->StoreVar('adm.restore_error', 'File read error'); + $event->redirect = 'tools/restore4'; } - - if ($done_percent == 100) { - $this->replaceRestoredFiles(); + elseif ( $done_percent == 100 ) { + $backup_helper->replaceRestoredFiles(); $this->Application->StoreVar('adm.restore_success', 1); $event->redirect = 'tools/restore4'; - return ; } - - echo $done_percent; - $event->status = kEvent::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); - } + else { + $event->status = kEvent::erSTOP; + echo $done_percent; } - } - 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); - 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); - 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'; - } - - /** - * Run given schema sqls and return error, if any + * Stops Restore & redirect to Restore template * - * @param $sql - * @return string - * @access protected + * @param kEvent $event */ - protected function runSchemaText($sql) + function OnRestoreCancel(&$event) { - $table_prefix = 'restore' . TABLE_PREFIX; - - if ( strlen($table_prefix) > 0 ) { - $replacements = Array ('INSERT INTO ', 'UPDATE ', 'ALTER TABLE ', 'DELETE FROM ', 'REPLACE INTO '); - - foreach ($replacements as $replacement) { - $sql = str_replace($replacement, $replacement . $table_prefix, $sql); - } - } - - $sql = str_replace('CREATE TABLE ', 'CREATE TABLE IF NOT EXISTS ' . $table_prefix, $sql); - $sql = str_replace('DROP TABLE ', 'DROP TABLE IF EXISTS ' . $table_prefix, $sql); - - $commands = explode("# --------------------------------------------------------", $sql); - - if ( count($commands) > 0 ) { - for ($i = 0; $i < count($commands); $i++) { - $cmd = trim( $commands[$i] ); - - if ( strlen($cmd) > 0 ) { - $this->Conn->Query($cmd); - - if ( $this->Conn->hasError() ) { - return $this->Conn->getErrorMsg() . " COMMAND:
$cmd
"; - } - } - } - } - - return ''; + $event->redirect = 'tools/restore1'; } /** - * Runs given sqls and return error message, if any + * Deletes one backup file * - * @param $all_sqls - * @return string - * @access protected + * @param kEvent $event */ - protected function runSQLText($all_sqls) + function OnDeleteBackup(&$event) { - $line = 0; + $backup_helper =& $this->Application->recallObject('BackupHelper'); + /* @var $backup_helper BackupHelper */ - while ( $line < count($all_sqls) ) { - $sql = $all_sqls[$line]; - if ( strlen(trim($sql)) > 0 && substr($sql, 0, 1) != "#" ) { - $table_prefix = 'restore' . TABLE_PREFIX; - - if ( strlen($table_prefix) > 0 ) { - $replacements = Array ('INSERT INTO ', 'UPDATE ', 'ALTER TABLE ', 'DELETE FROM ', 'REPLACE INTO '); - - foreach ($replacements as $replacement) { - $sql = str_replace($replacement, $replacement . $table_prefix, $sql); - } - } - - $sql = str_replace('CREATE TABLE ', 'CREATE TABLE IF NOT EXISTS ' . $table_prefix, $sql); - $sql = str_replace('DROP TABLE ', 'DROP TABLE IF EXISTS ' . $table_prefix, $sql); - $sql = trim($sql); - - if ( strlen($sql) > 0 ) { - $this->Conn->Query($sql); - - if ( $this->Conn->hasError() ) { - return $this->Conn->getErrorMsg() . " COMMAND:
$sql
"; - } - } - } - - $line++; - } - - return ''; + $backup_helper->delete(); } /** @@ -1205,9 +800,9 @@ { $sql = $this->Application->GetVar('sql'); if ($sql) { - $start = $this->getMoment(); + $start = microtime(true); $result = $this->Conn->Query($sql); - $this->Application->SetVar('sql_time', round($this->getMoment() - $start, 7)); + $this->Application->SetVar('sql_time', round(microtime(true) - $start, 7)); if ($result) @@ -1235,12 +830,6 @@ $event->status = kEvent::erFAIL; } - function getMoment() - { - list($usec, $sec) = explode(' ', microtime()); - return ((float)$usec + (float)$sec); - } - /** * Occurs after unit config cache was successfully rebuilt *