-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
executable file
·125 lines (117 loc) · 4.63 KB
/
api.php
File metadata and controls
executable file
·125 lines (117 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php // CODE BY HW
//接口入口
require_once dirname(__FILE__) . '/src/common.php';
$data = post('data');
if(empty($data)) {
header('HTTP/1.1 403 Forbidden');
exit;
}
$data = decrypt(urldecode($data));
if(empty($data)) {
header('HTTP/1.1 403 Forbidden');
exit;
}
$data = json_decode($data, true);
if(json_last_error() !== JSON_ERROR_NONE) {
header('HTTP/1.1 403 Forbidden');
exit;
}
$action = isset($data['action']) ? $data['action'] : null;
if($action === 'getTables') {
LogX('[ api ][ getTables ] Begin');
$tables = GlobalModel::getTables();
echo encrypt(json(array('code' => 1, 'msg' => '成功', 'data' => $tables)));
LogX('[ api ][ getTables ] End');
exit;
} elseif($action === 'pushTables') {
LogX('[ api ][ pushTables ] Begin');
if(!isset($_FILES['file']) || !is_uploaded_file($_FILES['file']['tmp_name'])) {
echo encrypt(json(array('code' => 0, 'msg' => '没有上传文件')));
exit;
}
$filename = dirname(__FILE__) . '/tmp/'. uniqid() . '.zip';
create_path(dirname($filename));
if(!move_uploaded_file($_FILES['file']['tmp_name'], $filename)) {
echo encrypt(json(array('code' => 0, 'msg' => '移动文件失败')));
exit;
}
LogX('[ api ][ pushTables ] Upload: ' . $filename);
$zip = new ZipArchive();
if($zip->open($filename) !== true) {
echo encrypt(json(array('code' => 0, 'msg' => '压缩包有误')));
exit;
}
$extractPath = dirname($filename) . '/' . pathinfo($filename, PATHINFO_FILENAME);
create_path($extractPath);
if($zip->extractTo($extractPath) !== true) {
echo encrypt(json(array('code' => 0, 'msg' => '解压失败')));
exit;
}
$zip->close();
$file = scandir($extractPath, 1);
if(!isset($file[0]) || !is_file($extractPath . '/' .$file[0])) {
echo encrypt(json(array('code' => 0, 'msg' => '压缩包有误')));
exit;
}
LogX('[ api ][ pushTables ] ZipArchive: ' . $extractPath . '/' . $file[0]);
$num = GlobalModel::importTables($extractPath . '/' . $file[0]);
unlink($filename);
unlink($extractPath . '/' . $file[0]);
rmdir($extractPath);
LogX('[ api ][ pushTables ] Unlink: ' . $filename);
LogX('[ api ][ pushTables ] Unlink: ' . $extractPath . '/' . $file[0]);
LogX('[ api ][ pushTables ] Rmdir: ' . $extractPath);
LogX('[ api ][ pushTables ] End');
echo encrypt(json(array('code' => 1, 'msg' => '成功', 'data' => array('num' => $num))));
exit;
} elseif($action === 'pullTables') {
LogX('[ api ][ pullTables ] Begin');
$tables = isset($data['tables']) ? trim($data['tables']) : '';
$tables = array_filter(explode(',', $tables));
if(empty($tables)) {
echo encrypt(json(array('code' => 0, 'msg' => '请选择数据表')));
exit;
}
try{
$file = GlobalModel::exportTables($tables);
LogX('[ api ][ pullTables ] exportTables: ' . $file);
if(!file_exists($file)) {
echo encrypt(json(array('code' => 0, 'msg' => '导出失败')));
exit;
}
$filename = dirname(__FILE__) . '/tmp/%s.zip';
$filename = sprintf($filename, pathinfo($file, PATHINFO_FILENAME));
create_path(dirname($filename));
$zip = new ZipArchive();
if($zip->open($filename, ZipArchive::CREATE) !== true) {
echo encrypt(json(array('code' => 0, 'msg' => '创建压缩包失败')));
exit;
}
$zip->addFile($file, pathinfo($file, PATHINFO_BASENAME));
$zip->close();
LogX('[ api ][ pullTables ] ZipArchive: ' . $filename);
//发送数据
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filename));
readfile($filename);
unlink($filename);
unlink($file);
LogX('[ api ][ pullTables ] Unlink: ' . $filename);
LogX('[ api ][ pullTables ] Unlink: ' . $file);
LogX('[ api ][ pullTables ] End');
}catch(\Exception $e) {
LogX('[ api ][ pullTables ] Exception: ' . $e->getMessage());
LogX('[ api ][ pullTables ] End');
echo encrypt(json(array('code' => 0, 'msg' => $e->getMessage())));
exit;
}
} else {
echo encrypt(json(array('code' => 0, 'msg' => 'Unknown Method')));
exit;
}
exit;