Skip to content

Commit e1d275f

Browse files
author
ShingChi
committed
更新支持备份到本地
1 parent ec5143c commit e1d275f

File tree

2 files changed

+64
-32
lines changed

2 files changed

+64
-32
lines changed

Export/Action.php

+41-23
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,49 @@ class Export_Action extends Typecho_Widget implements Widget_Interface_Do
1010
*/
1111
public function doExport()
1212
{
13-
// 获取POST值
13+
// 需要备份的数据表
1414
$tableSelect = $this->request->get('tableSelect');
15-
16-
// 获取SQL语句
15+
// 备份的数据
1716
$content = $this->getSql($tableSelect);
18-
19-
// 获取备份目录并设置文件
20-
$config = Typecho_Widget::widget('Widget_Options')->plugin('Export');
21-
$path = __TYPECHO_ROOT_DIR__ . '/' . trim($config->path, '/') . '/';
17+
// 备份文件名
2218
$fileName = $this->request->get('fileName');
23-
$file = $path . $fileName;
24-
25-
if (!empty($fileName)) {
26-
if ((is_dir($path) || @mkdir($path, 0777)) && is_writable($path)) {
27-
$handle = fopen($file, 'wb');
28-
if ($handle && fwrite($handle, $content)) {
29-
fclose($handle);
30-
$this->widget('Widget_Notice')->set(_t('备份文件 ' . $fileName . ' 已创建'), 'success');
19+
20+
if (0 == $this->request->get('bakplace')) {
21+
header('Content-Type: text/x-sql');
22+
header('Content-Disposition: attachment; filename=' . $fileName);
23+
if (preg_match("/MSIE ([0-9].[0-9]{1,2})/", $_SERVER['HTTP_USER_AGENT'])) {
24+
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
25+
header('Pragma: public');
26+
} else {
27+
header('Pragma: no-cache');
28+
header('Last-Modified: '. gmdate('D, d M Y H:i:s',
29+
Typecho_Date::gmtTime() + (Typecho_Date::$timezoneOffset - Typecho_Date::$serverTimezoneOffset)) . ' GMT');
30+
}
31+
header('Expires: ' . gmdate('D, d M Y H:i:s',
32+
Typecho_Date::gmtTime() + (Typecho_Date::$timezoneOffset - Typecho_Date::$serverTimezoneOffset)) . ' GMT');
33+
echo $content;
34+
} else {
35+
// 备份目录及路径
36+
$config = Typecho_Widget::widget('Widget_Options')->plugin('Export');
37+
$path = __TYPECHO_ROOT_DIR__ . '/' . trim($config->path, '/') . '/';
38+
$file = $path . $fileName;
39+
40+
if (!empty($fileName)) {
41+
if ((is_dir($path) || @mkdir($path, 0777)) && is_writable($path)) {
42+
$handle = fopen($file, 'wb');
43+
if ($handle && fwrite($handle, $content)) {
44+
fclose($handle);
45+
$this->widget('Widget_Notice')->set(_t('备份文件 ' . $fileName . ' 已创建'), 'success');
46+
} else {
47+
$this->widget('Widget_Notice')->set(_t('备份文件创建失败,请检查目录权限'), 'error');
48+
}
3149
} else {
32-
$this->widget('Widget_Notice')->set(_t('备份文件创建失败,请检查目录权限'), 'error');
50+
$this->widget('Widget_Notice')->set(_t('文件夹创建失败或目录权限限制'), 'error');
3351
}
3452
} else {
35-
$this->widget('Widget_Notice')->set(_t('文件夹创建失败或目录权限限制'), 'error');
53+
$this->widget('Widget_Notice')->set(_t('备份文件名不能为空'), 'error');
3654
}
37-
} else {
38-
$this->widget('Widget_Notice')->set(_t('备份文件名不能为空'), 'error');
55+
$this->response->goBack();
3956
}
4057
}
4158

@@ -77,6 +94,7 @@ public function doImport()
7794

7895
$this->widget('Widget_Notice')->set($deleteCount > 0 ? _t('备份已经被导入') : _t('没有备份被导入'),
7996
$deleteCount > 0 ? 'success' : 'notice');
97+
$this->response->goBack();
8098
}
8199

82100
/**
@@ -94,15 +112,16 @@ public function doDelete()
94112
$bid = $this->request->get('bid');
95113
$deleteCount = 0;
96114
if ($bid) {
97-
$backups = is_array($bid) ? $bid : array($bid);
98-
foreach ($backups as $backup) {
99-
@unlink($path . $backup);
115+
$fileNames = is_array($bid) ? $bid : array($bid);
116+
foreach ($fileNames as $fileName) {
117+
@unlink($path . $fileName);
100118
$deleteCount ++;
101119
}
102120
}
103121

104122
$this->widget('Widget_Notice')->set($deleteCount > 0 ? _t('备份已经被删除') : _t('没有备份被删除'),
105123
$deleteCount > 0 ? 'success' : 'notice');
124+
$this->response->goBack();
106125
}
107126

108127
/**
@@ -201,6 +220,5 @@ public function action()
201220
$this->on($this->request->is('export'))->doExport();
202221
$this->on($this->request->is('import'))->doImport();
203222
$this->on($this->request->is('delete'))->doDelete();
204-
$this->response->goBack();
205223
}
206224
}

Export/panel.php

+23-9
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,21 @@
6262
<p class="description"><?php _e('备份文件默认生成在插件的 backup 文件夹下'); ?></p>
6363
</li>
6464
</ul>
65-
<ul class="typecho-option typecho-option-submit" id="typecho-option-item-submit-2">
65+
<ul class="typecho-option" id="typecho-option-item-bakplace-2">
66+
<li>
67+
<label class="typecho-label"><?php _e('备份保存'); ?></label>
68+
<span>
69+
<input name="bakplace" type="radio" value="0" id="bakplace-0" checked="true">
70+
<label for="bakplace-0"><?php _e('本地'); ?></label>
71+
</span>
72+
<span>
73+
<input name="bakplace" type="radio" value="1" id="bakplace-1">
74+
<label for="bakplace-1"><?php _e('服务器'); ?></label>
75+
</span>
76+
<p class="description"></p>
77+
</li>
78+
</ul>
79+
<ul class="typecho-option typecho-option-submit" id="typecho-option-item-submit-3">
6680
<li>
6781
<button type="submit" class="primary"><?php _e('开始备份'); ?></button>
6882
</li>
@@ -74,12 +88,12 @@
7488
<div class="typecho-list-operate clearfix">
7589
<form method="get">
7690
<div class="operate">
77-
<label><i class="sr-only">全选</i><input type="checkbox" class="typecho-table-select-all"></label>
91+
<label><i class="sr-only"><?php _e('全选'); ?></i><input type="checkbox" class="typecho-table-select-all"></label>
7892
<div class="btn-group btn-drop">
79-
<button class="dropdown-toggle btn-s" type="button"><i class="sr-only">操作</i>选中项 <i class="i-caret-down"></i></button>
93+
<button class="dropdown-toggle btn-s" type="button"><i class="sr-only"><?php _e('操作'); ?></i><?php _e('选中项'); ?> <i class="i-caret-down"></i></button>
8094
<ul class="dropdown-menu">
81-
<li><a lang="你确认要导入这些备份吗?" href="<?php $options->index('/action/export?import'); ?>">导入</a></li>
82-
<li><a lang="你确认要删除这些备份吗?" href="<?php $options->index('/action/export?delete'); ?>">删除</a></li>
95+
<li><a lang="<?php _e('你确认要导入这些备份吗?'); ?>" href="<?php $options->index('/action/export?import'); ?>"><?php _e('导入'); ?></a></li>
96+
<li><a lang="<?php _e('你确认要删除这些备份吗?'); ?>" href="<?php $options->index('/action/export?delete'); ?>"><?php _e('删除'); ?></a></li>
8397
</ul>
8498
</div>
8599
</div>
@@ -97,9 +111,9 @@
97111
<thead>
98112
<tr>
99113
<th> </th>
100-
<th>备份文件</th>
101-
<th>备份时间</th>
102-
<th>文件大小</th>
114+
<th><?php _e('备份文件'); ?></th>
115+
<th><?php _e('备份时间'); ?></th>
116+
<th><?php _e('文件大小'); ?></th>
103117
</tr>
104118
</thead>
105119
<tbody>
@@ -115,7 +129,7 @@
115129
<?php else: ?>
116130
<tr id="bid-no">
117131
<td></td>
118-
<td colspan="4">暂无备份文件</td>
132+
<td colspan="4"><?php _e('暂无备份文件'); ?></td>
119133
<td></td>
120134
<td></td>
121135
</tr>

0 commit comments

Comments
 (0)