forked from typecho-fans/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plugin.php
330 lines (289 loc) · 10.4 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* 为博客添加Gravatar头像墙功能, 支持镜像加速与缓存
*
* @category widget
* @package Avatars
* @author 羽中
* @version 1.2.4
* @dependence 14.10.10
* @link http://www.yzmb.me/archives/net/avatars-for-typecho
*/
class Avatars_Plugin implements Typecho_Plugin_Interface
{
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
Typecho_Plugin::factory('Widget_Abstract_Contents')->contentEx = array('Avatars_Plugin','walls');
Typecho_Plugin::factory('Widget_Abstract_Comments')->gravatar = array('Avatars_Plugin','avatars');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate(){}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
echo
'<div style="color:#999;font-size:13px;word-break:break-all;"><p>'
._t('在主题模版文件中的适当位置插入代码%s<br/>或编辑文章/页面写入%s即可显示读者墙(黑字标签与class名称可自定义)','<span style="color:#467B96;font-weight:bold;"><?php Avatars_Plugin::output("<span style="color:#444;">li</span>","<span style="color:#444;">mostactive</span>"); ?></span>','<span style="color:#467B96;font-weight:bold;">[AVATARS|<span style="color:#444;">li</span>|<span style="color:#444;">mostactive</span>]</span>').
'</p></div>';
$wsize = new Typecho_Widget_Helper_Form_Element_Text('wsize',
NULL,'32',_t('读者墙头像大小'),_t('读者墙上的头像尺寸(单位px无需填写)'));
$wsize->input->setAttribute('class','w-10');
$wsize->addRule('required',_t('头像尺寸不能为空'));
$form->addInput($wsize->addRule('isInteger',_t('请填写整数数字')));
$wdefault = new Typecho_Widget_Helper_Form_Element_Text('wdefault',
NULL,'',_t('读者墙缺省头像'),_t('支持gravatar随机头像后缀(identicon/monsterid/wavatar/retro等), 自定义图片url注意尺寸应一致'));
$wdefault->input->setAttribute('class','w-60');
$form->addInput($wdefault);
$listnumber = new Typecho_Widget_Helper_Form_Element_Text('listnumber',
NULL,'10',_t('读者墙头像数目'),_t('读者墙上最多展示的评论者头像个数'));
$listnumber->input->setAttribute('class','w-10');
$listnumber->addRule('required',_t('头像个数不能为空'));
$form->addInput($listnumber->addRule('isInteger',_t('请填写整数数字')));
$since = new Typecho_Widget_Helper_Form_Element_Text('since',
NULL,'30',_t('读者墙收录时间'),_t('读者墙将展示该天数以内的评论排行'));
$since->input->setAttribute('class','w-10');
$form->addInput($since->addRule('isInteger',_t('请填写整数数字')));
$altword = new Typecho_Widget_Helper_Form_Element_Text('altword',
NULL,'条评论',_t('读者墙提示文字'),_t('个性化读者墙头像的评论数提示文字'));
$altword->input->setAttribute('class','mini');
$form->addInput($altword);
$proxy = new Typecho_Widget_Helper_Form_Element_Radio('proxy',
array(''=>_t('否'),'https://cdn.v2ex.com/gravatar/'=>_t('v2ex镜像'),'moecdn'=>_t('MoeCDN镜像')),'',_t('用代理加速头像'),_t('国内直连gravatar服务器不流畅时可选'));
$form->addInput($proxy);
$cache = new Typecho_Widget_Helper_Form_Element_Checkbox('cache',
array(1=>_t('是')),NULL,_t('在本地缓存头像'),_t('将头像下载到插件的cache目录中调用'));
$form->addInput($cache);
$comment = new Typecho_Widget_Helper_Form_Element_Checkbox('comment',
array(1=>_t('是')),NULL,_t('作用评论区头像'),_t('评论区头像也用上两项设置(加速/缓存)'));
$form->addInput($comment);
if (Typecho_Request::getInstance()->is('action=deletefiles')) {
self::deletefiles();
}
//清空动作按钮
$deletefiles = new Typecho_Widget_Helper_Form_Element_Submit();
$deletefiles->value(_t('清空缓存'));
$deletefiles->setAttribute('style','position:relative');
$deletefiles->input->setAttribute('style','position:absolute;bottom:127.5px;left:110px;padding-bottom:1px');
$deletefiles->input->setAttribute('class','btn btn-xs btn-warn');
$deletefiles->input->setAttribute('formaction',Helper::options()->adminUrl.'options-plugin.php?config=Avatars&action=deletefiles');
$form->addItem($deletefiles);
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
/**
* 读者墙标签解析
*
* @access public
* @param string $content
* @return string
*/
public static function walls($content,$widget,$lastResult)
{
$content = empty($lastResult) ? $content : $lastResult;
if ($widget->is('page') && false!==stripos($content,'[AVATARS')) {
$content = preg_replace_callback('/\[AVATARS(\w*[^>]*)\]/i',array('Avatars_Plugin','callback'),$content);
}
return $content;
}
/**
* 标签参数回调
*
* @access public
* @param array $matche
* @return string
*/
public static function callback($matche)
{
$listtag = 'li';
$class = 'mostactive';
if ($matche['1']) {
if (preg_match('/\|([\w-]*)\|([\w-]*)/i',$matche['1'],$out)) {
$listtag = trim($out['1']);
$class = trim($out['2']);
}
}
return self::output($listtag,$class,true);
}
/**
* 读者墙实例输出
*
* @access public
* @param string $listtag 标签名称
* @param string $class class名称
* @param boolean $iscall 是否回调
* @return void
*/
public static function output($listtag='li',$class='mostactive',$iscall=false)
{
$options = Helper::options();
$settings = $options->plugin('Avatars');
$listtag = $listtag ? $listtag : 'li';
$wurl = '';
$mostactive = '';
//获取评论计数
$db = Typecho_Db::get();
$select = $db->select(array('COUNT(author)'=>'cnt'),'author','url','mail')->from('table.comments')
->where('status = ?','approved')
->where('authorId = ?','0') //排除博主
->where('type = ?','comment')
->where('created > ?',$options->gmtTime + $options->timezone - $settings->since*24*3600) //收录时间
->limit($settings->listnumber)
->group('author')
->order('cnt',Typecho_Db::SORT_DESC); //降序排列
$counts = $db->fetchAll($select);
foreach ($counts as $count) {
//获取优化地址
$wurl = self::avurl($count['mail'],$settings->wsize,$options->commentsAvatarRating //同步评级设置
,$settings->wdefault,Typecho_Widget::widget('Widget_Archive')->request->isSecure() //获取安全请求
);
$mostactive .= '
<'.$listtag;
$mostactive .= $class ? ' class="'.$class.'"' : '';
$mostactive .= '><a href="';
$mostactive .= $count['url'] ? $count['url'] : '###'; //静默空白链接
$mostactive .= '"';
$mostactive .= $options->commentsUrlNofollow ? ' rel="external nofollow"' : ''; //同步nofollow设置
$mostactive .= ' title="'.$count['author'].' - '.$count['cnt'].$settings->altword.'">';
$mostactive .= $wurl ? '<img src="'.$wurl.'" alt="'.$count['author'].'" class="avatar" />' : _t('<span style="font-weight:bold;color:#467B96;">%s</span>','缓存出错!');
$mostactive .= '</a></'.$listtag.'>';
}
//修正替换输出
if ($iscall) {
return $mostactive;
} else {
echo $mostactive;
}
}
/**
* 输出头像地址
*
* @access private
* @param string $mail 邮箱地址
* @param integer $size 头像尺寸
* @param string $rate 头像评级
* @param string $default 默认头像
* @param boolean $secure https请求
* @return string
*/
private static function avurl($mail,$size,$rating,$default,$secure=false)
{
$options = Helper::options();
$settings = $options->plugin('Avatars');
$hash = $mail ? md5(strtolower(trim($mail))) : '';
$proxy = $settings->proxy;
$server = $proxy ?
($proxy=='moecdn' ? ($secure ? 'https://gravatar.moefont.com/avatar/' : 'http://gravatar.moefont.com/avatar/') : $proxy)
: ($secure ? 'https://secure.gravatar.com/avatar/' : 'http://'.rand(0,2).'.gravatar.com/avatar/');
$url = $server.$hash;
$url .= '?s='.$size;
$url .= '&r='.$rating;
$url .= '&d='.$default;
if ($settings->cache) {
$path = __TYPECHO_ROOT_DIR__.__TYPECHO_PLUGIN_DIR__.'/Avatars/cache/';
//生成缓存目录
if (!is_dir($path)) {
if (!self::makedir($path)) {
return false;
}
}
//默认缓存15日
$cachetime = 14*24*3600;
$cachedir = $path.$hash.$size;
//生成缓存头像
if (!is_file($cachedir) || (time()-filemtime($cachedir))>$cachetime) {
if (!@copy($url,$cachedir)) {
return false;
}
}
$url = $options->pluginUrl.'/Avatars/cache/'.$hash.$size;
}
return $url;
}
/**
* 缓存头像清空
*
* @access private
* @return void
*/
private function deletefiles()
{
$path = glob(__TYPECHO_ROOT_DIR__.'/usr/plugins/Avatars/cache/*');
if ($path) {
foreach ($path as $filename) {
@unlink($filename);
}
}
Typecho_Widget::widget('Widget_Notice')->set(_t('本地头像缓存已清空!'),'notice');
Typecho_Response::getInstance()->goBack();
}
/**
* 本地目录创建
*
* @access private
* @param string $path 路径
* @return boolean
*/
private static function makedir($path)
{
$path = preg_replace("/\\\+/", '/', $path);
$current = rtrim($path, '/');
$last = $current;
while (!is_dir($current) && false !== strpos($path, '/')) {
$last = $current;
$current = dirname($current);
}
if ($last == $current) {
return true;
}
if (!@mkdir($last)) {
return false;
}
$stat = @stat($last);
$perms = $stat['mode'] & 0007777;
@chmod($last, $perms);
return self::makedir($path);
}
/**
* 兼容评论区头像
*
* @access public
* @param integer $size 头像尺寸
* @param string $default 默认头像
* @return void
*/
public static function avatars($size,$rating,$default,$comments)
{
$mail = $comments->mail;
$issecure = $comments->request->isSecure();
$url = Helper::options()->plugin('Avatars')->comment ? self::avurl($mail,$size,$rating,$default,$issecure)
: Typecho_Common::gravatarUrl($mail,$size,$rating,$default,$issecure);
echo $url ? '<img class="avatar" src="'.$url.'" alt="'.$comments->author.'" />' : _t('<span style="font-weight:bold;color:#467B96;">%s</span>','缓存出错!');
}
}