forked from muruoxi2018/Check_Font_Copyright
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
214 lines (187 loc) · 6.27 KB
/
api.php
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
/*
* Copyright (C) www.muruoxi.com
*/
!defined('DEBUG') and define('DEBUG', 1); // 0: 线上模式; 1: 调试模式;
define('APP_PATH', dirname(__FILE__) . '/'); // __DIR__
!defined('XIUNOPHP_PATH') and define('XIUNOPHP_PATH', APP_PATH . 'xiunophp/');
$conf = (@include APP_PATH . 'conf.php') or exit('<script>window.location="https://www.muruoxi.com/"</script>');
if (DEBUG > 0) {
include XIUNOPHP_PATH . 'xiunophp.php';
} else {
include XIUNOPHP_PATH . 'xiunophp.min.php';
}
// 转换为绝对路径,防止被包含时出错。
substr($conf['log_path'], 0, 2) == './' and $conf['log_path'] = APP_PATH . $conf['log_path'];
substr($conf['tmp_path'], 0, 2) == './' and $conf['tmp_path'] = APP_PATH . $conf['tmp_path'];
substr($conf['upload_path'], 0, 2) == './' and $conf['upload_path'] = APP_PATH . $conf['upload_path'];
$_SERVER['conf'] = $conf;
//测试数据库连接 / try to connect database
//db_connect() or exit($errstr);
// $_SERVER['db'] = $db = db_new($conf['db']);
// 此处可能报错
// $r = db_connect($db);
// print_r($r);
// 下面开始处理事物
// -1 意料外的错误
// 0 一切正常
// 1+ 各种识别中的状态
//设置返回类型为json
header('content-type:application/json;charset=utf-8');
$action = param('action');
switch ($action) {
case 'uploadFont':
uploadFont();
break;
case 'addFont':
addFont();
break;
case 'getAllFontsList':
getAllFontsList();
break;
case 'getFreeFontsList':
getFreeFontsList();
break;
case 'getStatistics':
getStatistics();
break;
case 'searchFonts':
searchFonts();
break;
default:
xn_message(-1, '未定义的操作');
}
/**
* 入库一个字体,需要审核
* @return void
* @Description
* @Author MuRuoxi
* @DateTime 2020-08-08
*/
function addFont()
{
$fonts = [];
is_null(param('cn_name')) ? xn_message(0, '请输入字体的中文名') : $fonts['cn_name'] = param('cn_name');
is_null(param('en_name')) ? xn_message(0, '请输入字体的英文名') : $fonts['en_name'] = param('en_name');
is_null(param('is_vip')) ? xn_message(0, '请选择字体的类型') : $fonts['is_vip'] = param('is_vip');
is_null(param('attachment')) ? xn_message(0, '请上传字体') : $fonts['attachment'] = str_replace($_SERVER['conf']['downurl'], '', param('attachment'));
$result = db_find_one('fonts', array('en_name' => $fonts['en_name']));
$result ? xn_message(-1, '这个字体已经入库了,请不要重复提交') : db_insert('fonts', $fonts);
xn_message(0, '提交成功');
}
/**
* 拉取所有字体列表(只有审核过的会被拉取)
* 至少要有一个审核通过的数据哦
* @return void
* @Description
* @Author MuRuoxi
* @DateTime 2020-08-08
*/
function getAllFontsList()
{
$result = db_find('fonts', array('verify' => 1), null, 1, 10000, null, array('cn_name', 'en_name', 'is_vip', 'attachment'));
foreach ($result as &$arr) {
$arr['attachment'] = atod($arr['attachment']);
}
$result ? xn_message(0, array('fontslist' => $result)) : xn_message(-1, '数据库读取失败');
}
/**
* 拉取统计数据,返回两个数据
* count 入库的字体总数
* verify 审核通过的字体总数
* @return void
* @Description
* @Author MuRuoxi
* @DateTime 2020-08-08
*/
function getStatistics()
{
$result = [];
$result['count'] = db_count('fonts');
$result['verify'] = db_count('fonts', array('verify' => 1));
xn_message(0, $result);
}
/**
* 拉取免费字体列表
* @return void
* @Description
* @Author MuRuoxi
* @DateTime 2020-08-08
*/
function getFreeFontsList()
{
$result = db_find('fonts', array('is_vip' => 0, 'verify' => 1));
$result = arrlist_keep_keys($result, array('cn_name', 'en_name', 'is_vip', 'attachment'));
foreach ($result as &$val) {
$val['attachment'] = atod($val['attachment']);
}
xn_message(0, array('fontslist' => $result));
}
/**
* 上传字体
* @return void
* @Description
* @Author MuRuoxi
* @DateTime 2020-08-08
*/
function uploadFont()
{
$fileInfo = $_FILES['file']; //获取提交过来的文件
$filePath = $_FILES['file']['tmp_name']; //获取文件临时目录
$name = iconv('utf-8', 'gb2312', $fileInfo['name']); //避免中文乱码
if (file_ext(file_name($name)) == 'zip') {
global $time;
$newpath = $_SERVER['conf']['upload_path'] . date('Y-n-j', $time) . '/';
$newname = $newpath . xn_rand(8) . '.zip';
xn_mkdir($newpath, null, true);
move_uploaded_file($filePath, $newname);
xn_message(1, array(
'download' => str_replace($_SERVER['conf']['upload_path'], $_SERVER['conf']['downurl'], $newname)
));
} else {
xn_message(-1, '文件类型错误');
}
}
/**
* 搜索字体
* @return void
* @Description
* @Author MuRuoxi
* @DateTime 2020-08-08
*/
function searchFonts()
{
$font = param('font');
$page = param('page');
$limit = param('limit');
if (!$font) {
xn_message(-1, '请输入字体的名称');
}
$en_result = db_find('fonts', array('en_name' => array('LIKE' => $font), 'verify' => 1), null, $page, $limit);
$cn_result = db_find('fonts', array('cn_name' => array('LIKE' => $font), 'verify' => 1), null, $page, $limit);
$result = $en_result + $cn_result;
$result = arrlist_keep_keys($result, array('cn_name', 'en_name', 'is_vip', 'attachment'));
foreach ($result as &$arr) {
$arr['attachment'] = $arr['is_vip'] == 2 ? '#' : atod($arr['attachment']);
}
$en_count = db_count('fonts', array('en_name' => array('LIKE' => $font), 'verify' => 1));
$cn_count = db_count('fonts', array('cn_name' => array('LIKE' => $font), 'verify' => 1));
if (!count($result)) {
xn_message(-1, '尚未收录该字体信息');
} else {
xn_message(0, array('fonts' => $result, 'count' => $cn_count + $en_count));
}
}
/**
* 将本地附件的地址转为可下载的地址,网络地址不会变动
* @param [string] $attachment
* @return void
* @Description
* @Author MuRuoxi
* @DateTime 2020-08-08
*/
function atod($attachment)
{
$downurl = substr($attachment, 0, 8) == 'https://' || substr($attachment, 0, 7) == 'http://' ? $attachment : $_SERVER['conf']['downurl'] . $attachment;
return $downurl;
}