Skip to content

Commit

Permalink
fix backup file version
Browse files Browse the repository at this point in the history
  • Loading branch information
joyqi committed Mar 15, 2018
1 parent 13c1986 commit 7900c1b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 29 deletions.
14 changes: 0 additions & 14 deletions admin/backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,6 @@
<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="4" type="submit" class="btn primary"><?php _e('上传并恢复 &raquo;'); ?></button>
Expand All @@ -75,13 +68,6 @@
</select>
</li>
</ul>
<ul class="typecho-option">
<li>
<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>
Expand Down
17 changes: 10 additions & 7 deletions var/Typecho/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,7 @@ public static function buildBackupBuffer($type, $header, $body)
{
$buffer = '';

$buffer .= pack('vvv', $type, strlen($header), strlen($body));
$buffer .= pack('vvV', $type, strlen($header), strlen($body));
$buffer .= $header . $body;
$buffer .= md5($buffer);

Expand All @@ -1144,19 +1144,22 @@ public static function buildBackupBuffer($type, $header, $body)
*
* @param $fp
* @param bool $offset
* @param string $version
* @return array|bool
*/
public static function extractBackupBuffer($fp, &$offset, $isFix = false)
public static function extractBackupBuffer($fp, &$offset, $version)
{
$meta = fread($fp, 6);
$offset += 6;
$realMetaLen = $version == 'FILE' ? 6 : 8;

$meta = fread($fp, $realMetaLen);
$offset += $realMetaLen;
$metaLen = strlen($meta);

if (false === $meta || $metaLen != 6) {
if (false === $meta || $metaLen != $realMetaLen) {
return false;
}

list ($type, $headerLen, $bodyLen) = array_values(unpack('v3', $meta));
list ($type, $headerLen, $bodyLen) = array_values(unpack($version == 'FILE' ? 'v3' : 'v1type/v1headerLen/V1bodyLen', $meta));

$header = @fread($fp, $headerLen);
$offset += $headerLen;
Expand All @@ -1165,7 +1168,7 @@ public static function extractBackupBuffer($fp, &$offset, $isFix = false)
return false;
}

if ($isFix) {
if ('FILE' == $version) {
$bodyLen = array_reduce(json_decode($header, true), function ($carry, $len) {
return NULL === $len ? $carry : $carry + $len;
}, 0);
Expand Down
35 changes: 27 additions & 8 deletions var/Widget/Backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
*/
class Widget_Backup extends Widget_Abstract_Options implements Widget_Interface_Do
{
const HEADER = '%TYPECHO_BACKUP_FILE%';
const HEADER = '%TYPECHO_BACKUP_XXXX%';
const HEADER_VERSION = '0001';

/**
* @var array
Expand Down Expand Up @@ -113,14 +114,31 @@ private function buildBuffer($type, $data)
return Typecho_Common::buildBackupBuffer($type, $header, $body);
}

/**
* @param $str
* @param $version
* @return bool
*/
private function parseHeader($str, &$version) {
if (!$str || strlen($str) != strlen(self::HEADER)) {
return false;
}

if (!preg_match("/%TYPECHO_BACKUP_[A-Z0-9]{4}%/", $str)) {
return false;
}

$version = substr($str, 16, -1);
return true;
}

/**
* 解析数据
*
* @param $file
*/
private function extractData($file)
{
$isFix = $this->request->is('fix=1');
$fp = @fopen($file, 'rb');

if (!$fp) {
Expand All @@ -139,7 +157,7 @@ private function extractData($file)

$fileHeader = @fread($fp, $headerSize);

if (!$fileHeader || $fileHeader != self::HEADER) {
if (!$this->parseHeader($fileHeader, $version)) {
@fclose($fp);
$this->widget('Widget_Notice')->set(_t('备份文件格式错误'), 'error');
$this->response->goBack();
Expand All @@ -148,7 +166,7 @@ private function extractData($file)
fseek($fp, $fileSize - $headerSize);
$fileFooter = @fread($fp, $headerSize);

if (!$fileFooter || $fileFooter != self::HEADER) {
if (!$this->parseHeader($fileFooter, $version)) {
@fclose($fp);
$this->widget('Widget_Notice')->set(_t('备份文件格式错误'), 'error');
$this->response->goBack();
Expand All @@ -158,7 +176,7 @@ private function extractData($file)
$offset = $headerSize;

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

if (!$data) {
@fclose($fp);
Expand Down Expand Up @@ -269,7 +287,8 @@ private function export()
$this->response->setHeader('Content-Disposition', 'attachment; filename="'
. date('Ymd') . '_' . $host . '_' . uniqid() . '.dat"');

$buffer = self::HEADER;
$header = str_replace('XXXX', self::HEADER_VERSION, self::HEADER);
$buffer = $header;
$db = $this->db;

foreach ($this->_types as $type => $val) {
Expand All @@ -281,7 +300,7 @@ private function export()
foreach ($rows as $row) {
$buffer .= $this->buildBuffer($val, $this->applyFields($type, $row));

if (sizeof($buffer) >= 1024 * 1024) {
if (strlen($buffer) >= 1024 * 1024) {
echo $buffer;
ob_flush();
$buffer = '';
Expand All @@ -296,7 +315,7 @@ private function export()
}

Typecho_Plugin::factory(__CLASS__)->export();
echo self::HEADER;
echo $header;
ob_end_flush();
}

Expand Down

0 comments on commit 7900c1b

Please sign in to comment.