function downloadFile($file, $type = 'application/octet-stream') { // Make sure the files exists, otherwise we are wasting our time if (!file_exists($file)) { header("HTTP/1.1 404 Not Found"); exit; } $size = realFilesize($file); $name = basename($file); header('Pragma: public'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Cache-Control: private', false); header('Content-Transfer-Encoding: binary'); header('Content-Disposition: attachment; filename="' . $name . '";'); header('Content-Type: ' . $type); header('Content-Length: ' . $size); $chunkSize = 1024 * 1024 * 8; $handle = fopen($file, 'rb'); while (!feof($handle)) { $buffer = fread($handle, $chunkSize); echo $buffer; ob_flush(); flush(); } fclose($handle); exit; }
public static function getAllBackups() { $backups = array(); $path = SG_BACKUP_DIRECTORY; clearstatcache(); if ($handle = @opendir($path)) { $sgdb = SGDatabase::getInstance(); $data = $sgdb->query('SELECT id, name, type, subtype, status, progress, update_date FROM ' . SG_ACTION_TABLE_NAME); $allBackups = array(); foreach ($data as $row) { $allBackups[$row['name']][] = $row; } while (($file = readdir($handle)) !== false) { if ($file === '.') { continue; } if ($file === '..') { continue; } if (substr($file, 0, 10) == 'sg_backup_') { $backup = array(); $backup['name'] = $file; $backup['files'] = file_exists($path . $file . '/' . $file . '.sgbp') ? 1 : 0; $backup['backup_log'] = file_exists($path . $file . '/' . $file . '_backup.log') ? 1 : 0; $backup['restore_log'] = file_exists($path . $file . '/' . $file . '_restore.log') ? 1 : 0; if (!$backup['files'] && !$backup['backup_log'] && !$backup['restore_log']) { continue; } $backupRow = null; if (isset($allBackups[$file])) { $skip = false; foreach ($allBackups[$file] as $row) { if ($row['status'] == SG_ACTION_STATUS_IN_PROGRESS_FILES || $backupRow['status'] == SG_ACTION_STATUS_IN_PROGRESS_DB) { $backupRow = $row; break; } else { if (($row['status'] == SG_ACTION_STATUS_CANCELLING || $row['status'] == SG_ACTION_STATUS_CANCELLED) && $row['type'] != SG_ACTION_TYPE_UPLOAD) { $skip = true; break; } } $backupRow = $row; if ($row['status'] == SG_ACTION_STATUS_FINISHED_WARNINGS && $row['type'] != SG_ACTION_TYPE_UPLOAD) { $key = $row['type'] == SG_ACTION_TYPE_BACKUP ? 'backup_warning' : 'restore_warning'; $backupRow[$key] = 1; } else { if ($row['status'] == SG_ACTION_STATUS_ERROR && $row['type'] == SG_ACTION_TYPE_BACKUP) { $backupRow['backup_error'] = 1; break; } else { if ($row['status'] == SG_ACTION_STATUS_ERROR && $row['type'] == SG_ACTION_TYPE_RESTORE) { $backupRow['restore_error'] = 1; } } } } if ($skip === true) { continue; } } if ($backupRow) { $backup['active'] = $backupRow['status'] == SG_ACTION_STATUS_IN_PROGRESS_FILES || $backupRow['status'] == SG_ACTION_STATUS_IN_PROGRESS_DB || $backupRow['status'] == SG_ACTION_STATUS_CREATED ? 1 : 0; $backup['type'] = (int) $backupRow['type']; $backup['type'] = (int) $backupRow['type']; $backup['subtype'] = (int) $backupRow['subtype']; $backup['progress'] = (int) $backupRow['progress']; $backup['id'] = (int) $backupRow['id']; } else { $backup['active'] = 0; } if ($backup['active'] == 0) { $backup['backup_warning'] = (int) @$backupRow['backup_warning']; $backup['restore_warning'] = (int) @$backupRow['restore_warning']; $backup['backup_error'] = (int) @$backupRow['backup_error']; $backup['restore_error'] = (int) @$backupRow['restore_error']; } else { $backup['backup_warning'] = 0; $backup['restore_warning'] = 0; $backup['backup_error'] = 0; $backup['restore_error'] = 0; } $size = ''; if ($backup['files']) { $size = number_format(realFilesize($path . $file . '/' . $file . '.sgbp') / 1024.0 / 1024.0, 2, '.', '') . ' MB'; } $backup['size'] = $size; $modifiedTime = filemtime($path . $file . '/.'); $backup['date'] = @date('Y-m-d H:i', $modifiedTime); $backups[$modifiedTime] = $backup; } } closedir($handle); } krsort($backups); return array_values($backups); }