@@ -10,32 +10,49 @@ class Export_Action extends Typecho_Widget implements Widget_Interface_Do
10
10
*/
11
11
public function doExport ()
12
12
{
13
- // 获取POST值
13
+ // 需要备份的数据表
14
14
$ tableSelect = $ this ->request ->get ('tableSelect ' );
15
-
16
- // 获取SQL语句
15
+ // 备份的数据
17
16
$ content = $ this ->getSql ($ tableSelect );
18
-
19
- // 获取备份目录并设置文件
20
- $ config = Typecho_Widget::widget ('Widget_Options ' )->plugin ('Export ' );
21
- $ path = __TYPECHO_ROOT_DIR__ . '/ ' . trim ($ config ->path , '/ ' ) . '/ ' ;
17
+ // 备份文件名
22
18
$ 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
+ }
31
49
} else {
32
- $ this ->widget ('Widget_Notice ' )->set (_t ('备份文件创建失败,请检查目录权限 ' ), 'error ' );
50
+ $ this ->widget ('Widget_Notice ' )->set (_t ('文件夹创建失败或目录权限限制 ' ), 'error ' );
33
51
}
34
52
} else {
35
- $ this ->widget ('Widget_Notice ' )->set (_t ('文件夹创建失败或目录权限限制 ' ), 'error ' );
53
+ $ this ->widget ('Widget_Notice ' )->set (_t ('备份文件名不能为空 ' ), 'error ' );
36
54
}
37
- } else {
38
- $ this ->widget ('Widget_Notice ' )->set (_t ('备份文件名不能为空 ' ), 'error ' );
55
+ $ this ->response ->goBack ();
39
56
}
40
57
}
41
58
@@ -77,6 +94,7 @@ public function doImport()
77
94
78
95
$ this ->widget ('Widget_Notice ' )->set ($ deleteCount > 0 ? _t ('备份已经被导入 ' ) : _t ('没有备份被导入 ' ),
79
96
$ deleteCount > 0 ? 'success ' : 'notice ' );
97
+ $ this ->response ->goBack ();
80
98
}
81
99
82
100
/**
@@ -94,15 +112,16 @@ public function doDelete()
94
112
$ bid = $ this ->request ->get ('bid ' );
95
113
$ deleteCount = 0 ;
96
114
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 );
100
118
$ deleteCount ++;
101
119
}
102
120
}
103
121
104
122
$ this ->widget ('Widget_Notice ' )->set ($ deleteCount > 0 ? _t ('备份已经被删除 ' ) : _t ('没有备份被删除 ' ),
105
123
$ deleteCount > 0 ? 'success ' : 'notice ' );
124
+ $ this ->response ->goBack ();
106
125
}
107
126
108
127
/**
@@ -201,6 +220,5 @@ public function action()
201
220
$ this ->on ($ this ->request ->is ('export ' ))->doExport ();
202
221
$ this ->on ($ this ->request ->is ('import ' ))->doImport ();
203
222
$ this ->on ($ this ->request ->is ('delete ' ))->doDelete ();
204
- $ this ->response ->goBack ();
205
223
}
206
224
}
0 commit comments