This repository has been archived by the owner on Jun 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlugin.php
208 lines (191 loc) · 6.2 KB
/
Plugin.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
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
*
* 一言挂件widget
*
* @package Typecho-Hitokoto
* @author LittleJake
* @version 2.0.0
* @link https://blog.littlejake.net
*/
class Hitokoto_Plugin implements Typecho_Plugin_Interface
{
private static $api = "https://v1.hitokoto.cn/?";
private static $api_inter = "https://international.v1.hitokoto.cn/?";
/**
* 激活插件方法
*
* @return void
*/
public static function activate(){}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @access public
* @return void
*/
public static function deactivate(){}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form){
$display = new Typecho_Widget_Helper_Form_Element_Radio(
'display',
[
'1' => _t('是'),
'0' => _t('否')
],
'1',
_t('是否显示一言')
);
$api_type = new Typecho_Widget_Helper_Form_Element_Radio(
'api_type',
[
'intl' => _t('海外'),
'cn' => _t('中国')
],
'cn',
_t('选择一言服务器')
);
$category = new Typecho_Widget_Helper_Form_Element_Checkbox(
'category',
[
'a' => _t('动画'),
'b' => _t('漫画'),
'c' => _t('游戏'),
'd' => _t('文学'),
'e' => _t('原创'),
'f' => _t('来自网络'),
'g' => _t('其他'),
'h' => _t('影视'),
'i' => _t('诗词'),
'j' => _t('网易云'),
'k' => _t('哲学'),
'l' => _t('抖机灵'),
],
'a',
_t('选择显示的一言类型')
);
$time = new Typecho_Widget_Helper_Form_Element_Text(
'time',
NULL,
120,
_t('本地一言缓存过期时间'),
_t('默认为:120秒,尽量不要设置过快(参考QPS限制:国内3.5,国际:10)')
);
$template = new Typecho_Widget_Helper_Form_Element_Textarea(
'template',
NULL,
<<<EOF
<div>
<strong>{hitokoto}</strong>
<p>{from}</p>
</div>
EOF
,
_t('一言显示自定义模板'),
_t('可用变量参考:<a href="https://developer.hitokoto.cn/sentence/#返回格式" target="_blank">https://developer.hitokoto.cn/sentence/#返回格式</a>')
);
$form->addInput($display);
$form->addInput($category);
$form->addInput($api_type);
$form->addInput($time);
$form->addInput($template);
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
/**
*
* 获取一言
*
* @access public
* @return string
* @throws
*/
public static function getHitokoto()
{
//TODO 添加类型参数、自定义class、tag参数
$display = Typecho_Widget::widget('Widget_Options')
->plugin('Hitokoto')->display == 0?false:true;
//处理类型参数
$category = empty(Typecho_Widget::widget('Widget_Options')
->plugin('Hitokoto')->category)?'':"c=".implode("&c=", Typecho_Widget::widget('Widget_Options')
->plugin('Hitokoto')->category);
$url = Typecho_Widget::widget('Widget_Options')
->plugin('Hitokoto')->api_type == 'cn'?self::$api:self::$api_inter;
$expire = Typecho_Widget::widget('Widget_Options')
->plugin('Hitokoto')->time;
if(!$display)
return null;
$time = time();
if(is_readable("./hitokoto.json")) {
$json = @file_get_contents("./hitokoto.json");
$json = json_decode($json, true);
if($time - $json['time'] < intval($expire))
return self::format($json);
}
//curl获取json
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url.$category);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$json = curl_exec($ch);
curl_close($ch);
$json = json_decode($json, true);
$json['time'] = $time;
if(!@file_put_contents("./hitokoto.json", json_encode($json)))
return "<p>请检查目录插件权限</p>";
return self::format($json);
}
/**
* 用于处理、格式化json不同类型的数据
*
* @param $array
* @return string|string[]
* @throws Typecho_Exception
*/
public static function format($array){
$category = [
'a' => _t('动画'),
'b' => _t('漫画'),
'c' => _t('游戏'),
'd' => _t('文学'),
'e' => _t('原创'),
'f' => _t('来自网络'),
'g' => _t('其他'),
'h' => _t('影视'),
'i' => _t('诗词'),
'j' => _t('网易云'),
'k' => _t('哲学'),
'l' => _t('抖机灵'),
];
$template = Typecho_Widget::widget('Widget_Options')
->plugin('Hitokoto')->template;
foreach ($array as $k => $v)
switch ($k){
case 'created_at':
case 'time':
$template = str_replace("{{$k}}", htmlspecialchars(date('Y-m-d H:i:s',$v)), $template);
break;
case 'type':
$template = str_replace("{{$k}}", htmlspecialchars($category[$v]), $template);
break;
default:
$template = str_replace("{{$k}}", htmlspecialchars($v), $template);
}
return $template;
}
}