Skip to content

Commit

Permalink
Add error correcting mode for backup recovery
Browse files Browse the repository at this point in the history
  • Loading branch information
joyqi committed Mar 14, 2018
1 parent 76c7eb7 commit 13c1986
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
42 changes: 30 additions & 12 deletions admin/backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,50 @@
<input tabindex="2" id="backup-upload-file" name="file" type="file" class="file">
</li>
</ul>
<ul class="typecho-option">
<li>
<input tabindex="3" name="fix" value="1" type="checkbox" id="backup-fix">
<label for="backup-fix"><?php _e('纠错模式'); ?></label>
<p class="description"><?php _e('当你在恢复备份的时候出现错误, 请尝试使用此模式'); ?></p>
</li>
</ul>
<ul class="typecho-option typecho-option-submit">
<li>
<button tabindex="3" type="submit" class="btn primary"><?php _e('上传并恢复 &raquo;'); ?></button>
<button tabindex="4" type="submit" class="btn primary"><?php _e('上传并恢复 &raquo;'); ?></button>
<input type="hidden" name="do" value="import">
</li>
</ul>
</form>

<form action="<?php echo $actionUrl; ?>" id="from-server" class="tab-content hidden" method="post">
<?php if (empty($backupFiles)): ?>
<ul class="typecho-option">
<li>
<p class="description"><?php _e('将备份文件手动上传至服务器的 %s 目录下后, 这里会出现文件选项', __TYPECHO_BACKUP_DIR__); ?></p>
</li>
</ul>
<?php else: ?>
<ul class="typecho-option">
<li>
<label class="typecho-label" for="backup-select-file"><?php _e('选择一个备份文件恢复数据'); ?></label>
<select tabindex="5" name="file" id="backup-select-file">
<?php foreach ($backupFiles as $file): ?>
<option value="<?php echo $file; ?>"><?php echo $file; ?></option>
<?php endforeach; ?>
</select>
</li>
</ul>
<ul class="typecho-option">
<li>
<?php if (empty($backupFiles)): ?>
<p class="description"><?php _e('将备份文件手动上传至服务器的 %s 目录下后, 这里会出现文件选项', __TYPECHO_BACKUP_DIR__); ?></p>
<?php else: ?>
<label class="typecho-label" for="backup-select-file"><?php _e('选择一个备份文件恢复数据'); ?></label>
<select name="file" id="backup-select-file">
<?php foreach ($backupFiles as $file): ?>
<option value="<?php echo $file; ?>"><?php echo $file; ?></option>
<?php endforeach; ?>
</select>
<?php endif; ?>
<input tabindex="6" name="fix" value="1" type="checkbox" id="backup-fix">
<label for="backup-fix"><?php _e('纠错模式'); ?></label>
<p class="description"><?php _e('当你在恢复备份的时候出现错误, 请尝试使用此模式'); ?></p>
</li>
</ul>
<?php endif; ?>
<ul class="typecho-option typecho-option-submit">
<li>
<button tabindex="5" type="submit" class="btn primary"><?php _e('选择并恢复 &raquo;'); ?></button>
<button tabindex="7" type="submit" class="btn primary"><?php _e('选择并恢复 &raquo;'); ?></button>
<input type="hidden" name="do" value="import">
</li>
</ul>
Expand Down
8 changes: 7 additions & 1 deletion var/Typecho/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,7 @@ public static function buildBackupBuffer($type, $header, $body)
* @param bool $offset
* @return array|bool
*/
public static function extractBackupBuffer($fp, &$offset)
public static function extractBackupBuffer($fp, &$offset, $isFix = false)
{
$meta = fread($fp, 6);
$offset += 6;
Expand All @@ -1165,6 +1165,12 @@ public static function extractBackupBuffer($fp, &$offset)
return false;
}

if ($isFix) {
$bodyLen = array_reduce(json_decode($header, true), function ($carry, $len) {
return NULL === $len ? $carry : $carry + $len;
}, 0);
}

$body = @fread($fp, $bodyLen);
$offset += $bodyLen;

Expand Down
3 changes: 2 additions & 1 deletion var/Widget/Backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ private function buildBuffer($type, $data)
*/
private function extractData($file)
{
$isFix = $this->request->is('fix=1');
$fp = @fopen($file, 'rb');

if (!$fp) {
Expand Down Expand Up @@ -157,7 +158,7 @@ private function extractData($file)
$offset = $headerSize;

while (!feof($fp) && $offset + $headerSize < $fileSize) {
$data = Typecho_Common::extractBackupBuffer($fp, $offset);
$data = Typecho_Common::extractBackupBuffer($fp, $offset, $isFix);

if (!$data) {
@fclose($fp);
Expand Down

0 comments on commit 13c1986

Please sign in to comment.