-
Notifications
You must be signed in to change notification settings - Fork 11
/
functions.php
461 lines (420 loc) · 20.7 KB
/
functions.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
function themeConfig($form) {
$logoUrl = new Typecho_Widget_Helper_Form_Element_Text('logoUrl', NULL, NULL, _t('页头logo地址'), _t('一般为http://www.yourblog.com/image.png,支持 https:// 或 //,留空则使用站点名称'));
$form->addInput($logoUrl->addRule('xssCheck', _t('请不要在图片链接中使用特殊字符')));
$footerLogoUrl = new Typecho_Widget_Helper_Form_Element_Text('footerLogoUrl', NULL, NULL, _t('页尾logo地址'), _t('一般为http://www.yourblog.com/image.png,支持 https:// 或 //,留空则使用站点名称'));
$form->addInput($footerLogoUrl->addRule('xssCheck', _t('请不要在图片链接中使用特殊字符')));
$favicon = new Typecho_Widget_Helper_Form_Element_Text('favicon', NULL, NULL, _t('favicon地址'), _t('一般为http://www.yourblog.com/image.png,支持 https:// 或 //,留空则不设置favicon'));
$form->addInput($favicon->addRule('xssCheck', _t('请不要在图片链接中使用特殊字符')));
$iosicon = new Typecho_Widget_Helper_Form_Element_Text('iosicon', NULL, NULL, _t('apple touch icon地址'), _t('一般为http://www.yourblog.com/image.png,支持 https:// 或 //,留空则不设置Apple Touch Icon'));
$form->addInput($iosicon->addRule('xssCheck', _t('请不要在图片链接中使用特殊字符')));
$searchPage = new Typecho_Widget_Helper_Form_Element_Text('searchPage', NULL, NULL, _t('搜索页地址'), _t('输入你的 Template Page of Search 的页面地址,记得带上 http:// 或 https://'));
$form->addInput($searchPage->addRule('xssCheck', _t('请不要在链接中使用特殊字符')));
$beian = new Typecho_Widget_Helper_Form_Element_Text('beian', NULL, NULL, _t('ICP备案号'), _t('例如:渝ICP备15004857号-1,留空则不设置ICP备案号'));
$form->addInput($beian->addRule('xssCheck', _t('请不要在ICP备案号中使用特殊字符')));
$emoji = new Typecho_Widget_Helper_Form_Element_Radio('emoji',
array('able' => _t('启用'),
'disable' => _t('禁止'),
),
'able', _t('启用 Emoji 表情'), _t('启用后可在编辑器里插入 Emoji 表情符号'));
$form->addInput($emoji);
$links = new Typecho_Widget_Helper_Form_Element_Radio('links',
array('able' => _t('个站官方'),
'disable' => _t('Rinvay代理'),
),
'able', _t('个站友链项目'), _t('选择个站友链API接口,若个站HTTPS接口不能使用可选择Rinvay代理接口'));
$form->addInput($links);
$pjaxSet = new Typecho_Widget_Helper_Form_Element_Radio('pjaxSet',
array('able' => _t('InstantClick'),
'disable' => _t('Pjax'),
),
'disable', _t('PJAX加速设置'), _t('默认Pjax,若启用InstantClick则需提前到关闭‘开启反垃圾保护’,开关在‘设置-评论’,同时回复表情暂时无法使用。'));
$form->addInput($pjaxSet);
$DnsPrefetch = new Typecho_Widget_Helper_Form_Element_Radio('DnsPrefetch',
array('able' => _t('启用'),
'disable' => _t('禁止'),
),
'disable', _t('DNS预解析加速'), _t('默认禁止,启用则会对CDN资源和Gravatar进行加速'));
$form->addInput($DnsPrefetch);
$htmlCompress = new Typecho_Widget_Helper_Form_Element_Radio('htmlCompress',
array('able' => _t('启用'),
'disable' => _t('禁止'),
),
'disable', _t('代码压缩设置'), _t('默认禁止,启用则会对HTML代码进行压缩,可能会跟部分插件存在兼容问题,请自行测试'));
$form->addInput($htmlCompress);
$fastClickSet = new Typecho_Widget_Helper_Form_Element_Radio('fastClickSet',
array('able' => _t('启用'),
'disable' => _t('禁止'),
),
'disable', _t('移动端点击延迟消除设置'), _t('默认禁止,好多安卓原生浏览器有点击延迟,想开启就开启吧'));
$form->addInput($fastClickSet);
$postListSwitch = new Typecho_Widget_Helper_Form_Element_Radio('postListSwitch',
array('threeList' => _t('三栏'),
'oneList' => _t('单栏'),
),
'oneList', _t('首页文章列表设置'), _t('默认单栏,根据自己的喜好去做切换吧'));
$form->addInput($postListSwitch);
$colorBgPosts = new Typecho_Widget_Helper_Form_Element_Radio('colorBgPosts',
array('customColor' => _t('启用'),
'defaultColor' => _t('禁用'),
),
'defaultColor', _t('文章色块设置'), _t('默认禁止,启用则可以通过文章字段控制色块颜色,仅支持blue、purple、green、yellow、red'));
$form->addInput($colorBgPosts);
$postshowthumb = new Typecho_Widget_Helper_Form_Element_Radio('postshowthumb',
array('able' => _t('启用'),
'disable' => _t('禁用'),
),
'disable', _t('文章题图设置'), _t('默认禁止,启用则在文章页内显示缩略图'));
$form->addInput($postshowthumb);
$relatedPosts = new Typecho_Widget_Helper_Form_Element_Radio('relatedPosts',
array('able' => _t('启用'),
'disable' => _t('禁止'),
),
'disable', _t('相关文章设置'), _t('默认禁止,仅在文章页中生效,最多显示六条,文章是根据标签进行相关的'));
$form->addInput($relatedPosts);
$tableOfContents = new Typecho_Widget_Helper_Form_Element_Radio('tableOfContents',
array('able' => _t('启用'),
'disable' => _t('禁止'),
),
'disable', _t('文章目录设置'), _t('默认禁止,文章页右边目录生成,仅在网页宽度大于1000px时显示'));
$form->addInput($tableOfContents);
$useHighline = new Typecho_Widget_Helper_Form_Element_Radio('useHighline',
array('able' => _t('启用'),
'disable' => _t('禁止'),
),
'disable', _t('代码高亮设置'), _t('默认禁止,启用则会对 ``` 进行代码高亮,支持22种编程语言的高亮'));
$form->addInput($useHighline);
$useMathjax = new Typecho_Widget_Helper_Form_Element_Radio('useMathjax',
array('able' => _t('启用'),
'disable' => _t('禁止'),
),
'disable', _t('文章Mathjax设置'), _t('默认禁止,启用则会对内容页进行数学公式渲染,仅支持 $公式$ 和 $$公式$$ '));
$form->addInput($useMathjax);
$GoogleAnalytics = new Typecho_Widget_Helper_Form_Element_Textarea('GoogleAnalytics', NULL, NULL, _t('Google Analytics代码'), _t('填写你从Google Analytics获取到的Universal Analytics跟踪代码,不需要script标签'));
$form->addInput($GoogleAnalytics);
$socialweibo = new Typecho_Widget_Helper_Form_Element_Text('socialweibo', NULL, NULL, _t('输入微博链接'), _t('在这里输入微博链接,支持 http:// 或 https:// 或 //'));
$form->addInput($socialweibo->addRule('xssCheck', _t('请不要在链接中使用特殊字符')));
$socialzhihu = new Typecho_Widget_Helper_Form_Element_Text('socialzhihu', NULL, NULL, _t('输入知乎链接'), _t('在这里输入知乎链接,支持 http:// 或 https:// 或 //'));
$form->addInput($socialzhihu->addRule('xssCheck', _t('请不要在链接中使用特殊字符')));
$socialgithub = new Typecho_Widget_Helper_Form_Element_Text('socialgithub', NULL, NULL, _t('输入GitHub链接'), _t('在这里输入GitHub链接,支持 http:// 或 https://或 //'));
$form->addInput($socialgithub->addRule('xssCheck', _t('请不要在链接中使用特殊字符')));
$socialtwitter = new Typecho_Widget_Helper_Form_Element_Text('socialtwitter', NULL, NULL, _t('输入Twitter链接'), _t('在这里输入twitter链接,支持 http:// 或 https:// 或 //'));
$form->addInput($socialtwitter->addRule('xssCheck', _t('请不要在链接中使用特殊字符')));
$srcAddress = new Typecho_Widget_Helper_Form_Element_Text('src_add', NULL, NULL, _t('图片CDN替换前地址'), _t('即你的附件存放链接,一般为http://www.yourblog.com/usr/uploads/'));
$form->addInput($srcAddress->addRule('xssCheck', _t('请不要在链接中使用特殊字符')));
$cdnAddress = new Typecho_Widget_Helper_Form_Element_Text('cdn_add', NULL, NULL, _t('图片CDN替换后地址'), _t('即你的七牛云存储域名,一般为http://yourblog.qiniudn.com/,可能也支持其他有镜像功能的CDN服务'));
$form->addInput($cdnAddress->addRule('xssCheck', _t('请不要在链接中使用特殊字符')));
$default_thumb = new Typecho_Widget_Helper_Form_Element_Text('default_thumb', NULL, '', _t('默认缩略图'),_t('文章没有图片时的默认缩略图,留空则无,一般为http://www.yourblog.com/image.png'));
$form->addInput($default_thumb->addRule('xssCheck', _t('请不要在链接中使用特殊字符')));
}
function themeInit($archive){
Helper::options()->commentsMaxNestingLevels = 999;
if ($archive->is('index')) {
$archive->parameter->pageSize = 12;
}
}
function showThumb($obj,$size=null,$link=false){
preg_match_all( "/<[img|IMG].*?src=[\'|\"](.*?)[\'|\"].*?[\/]?>/", $obj->content, $matches );
$thumb = '';
$options = Typecho_Widget::widget('Widget_Options');
$attach = $obj->attachments(1)->attachment;
if (isset($attach->isImage) && $attach->isImage == 1){
$thumb = $attach->url;
if(!empty($options->src_add) && !empty($options->cdn_add)){
$thumb = str_ireplace($options->src_add,$options->cdn_add,$thumb);
}
}elseif(isset($matches[1][0])){
$thumb = $matches[1][0];
if(!empty($options->src_add) && !empty($options->cdn_add)){
$thumb = str_ireplace($options->src_add,$options->cdn_add,$thumb);
}
}
if(empty($thumb) && empty($options->default_thumb)){
return '';
}else{
$thumb = empty($thumb) ? $options->default_thumb : $thumb;
}
if($link){
return $thumb;
}
}
function parseFieldsThumb($obj){
$options = Typecho_Widget::widget('Widget_Options');
if(!empty($options->src_add) && !empty($options->cdn_add)){
$fieldsThumb = str_ireplace($options->src_add,$options->cdn_add,$obj->fields->thumb);
echo trim($fieldsThumb);
}else{
return $obj->fields->thumb();
}
}
function parseContent($obj){
$options = Typecho_Widget::widget('Widget_Options');
if(!empty($options->src_add) && !empty($options->cdn_add)){
$obj->content = str_ireplace($options->src_add,$options->cdn_add,$obj->content);
}
$obj->content = preg_replace("/<a href=\"([^\"]*)\">/i", "<a href=\"\\1\" target=\"_blank\">", $obj->content);
$ms = $obj->content;
$urlreg = '/[A-Za-z0-9_\-\x{4e00}-\x{9fa5}]+/u';
$arureg = '/#\([A-Za-z0-9_\-\x{4e00}-\x{9fa5}]+\)/u';
$paopaoreg = '/@\([A-Za-z0-9_\-\x{4e00}-\x{9fa5}]+\)/u';
$paopao = gethosturl().'/usr/themes/Rinvay/images/biaoqing/paopao/';
$aru = gethosturl().'/usr/themes/Rinvay/images/biaoqing/aru/';
$paopaoid = preg_match_all ( $paopaoreg , $ms , $namep );
$aruid = preg_match_all ( $arureg , $ms , $namea );
$aa = $namep[0];
$bb = $namea[0];
for ($i=0; $i < sizeof($aa); $i++) {
$names = preg_match ( $urlreg , $aa[$i] , $hex );
$nameid = preg_match ( $paopaoreg , $aa[$i] , $hexs );
$namehex = preg_replace('/%/u','',urlencode($hex[0]));
$imgurl = '<img class="rinvay" src="'.$paopao.$namehex.'_2x.png'.'" >';
$ms = str_replace($hexs[0],$imgurl,$ms);
$content = preg_replace($paopaoreg,$imgurl,$ms);
}
for ($l=0; $l < sizeof($bb); $l++) {
$names = preg_match ( $urlreg , $bb[$l] , $hex );
$nameid = preg_match ( $arureg , $bb[$l] , $hexs );
$namehex = preg_replace('/%/u','',urlencode($hex[0]));
$imgurl = '<img class="rinvay" src="'.$aru.$namehex.'_2x.png'.'" >';
$ms = str_replace($hexs[0],$imgurl,$ms);
$content = preg_replace($arureg,$imgurl,$ms);
}
echo $ms;
}
function getCommentAt($coid){
$db = Typecho_Db::get();
$prow = $db->fetchRow($db->select('parent')
->from('table.comments')
->where('coid = ? AND status = ?', $coid, 'approved'));
$parent = $prow['parent'];
if ($parent != "0") {
$arow = $db->fetchRow($db->select('author')
->from('table.comments')
->where('coid = ? AND status = ?', $parent, 'approved'));
$author = $arow['author'];
$href = '<a href="#comment-'.$parent.'">@'.$author.'</a>';
echo $href;
} else {
echo '';
}
}
function getRecentPosts($obj,$pageSize){
$db = Typecho_Db::get();
$rows = $db->fetchAll($db->select('cid')
->from('table.contents')
->where('type = ? AND status = ?', 'post', 'publish')
->order('created', Typecho_Db::SORT_DESC)
->limit($pageSize));
foreach($rows as $row){
$cid = $row['cid'];
$apost = $obj->widget('Widget_Archive@post_'.$cid, 'type=post', 'cid='.$cid);
$output = '<li><a href="'.$apost->permalink .'">'. $apost->title .'</a></li>';
echo $output;
}
}
function randBgIco(){
$bgIco=array('book','game','note','chat','code','image','web','link','design','lock');
return $bgIco[mt_rand(0,9)];
}
function randBgColor(){
$bgColor=array('blue','purple','green','yellow','red','orange');
return $bgColor[mt_rand(0,5)];
}
function theNext($widget, $default = NULL){
$db = Typecho_Db::get();
$sql = $db->select()->from('table.contents')
->where('table.contents.created > ?', $widget->created)
->where('table.contents.status = ?', 'publish')
->where('table.contents.type = ?', $widget->type)
->where('table.contents.password IS NULL')
->order('table.contents.created', Typecho_Db::SORT_ASC)
->limit(1);
$content = $db->fetchRow($sql);
if ($content) {
$content = $widget->filter($content);
$link = '<a href="' . $content['permalink'] . '" title="' . $content['title'] . '">←</a>';
echo $link;
} else {
echo $default;
}
}
function thePrev($widget, $default = NULL){
$db = Typecho_Db::get();
$sql = $db->select()->from('table.contents')
->where('table.contents.created < ?', $widget->created)
->where('table.contents.status = ?', 'publish')
->where('table.contents.type = ?', $widget->type)
->where('table.contents.password IS NULL')
->order('table.contents.created', Typecho_Db::SORT_DESC)
->limit(1);
$content = $db->fetchRow($sql);
if ($content) {
$content = $widget->filter($content);
$link = '<a href="' . $content['permalink'] . '" title="' . $content['title'] . '">→</a>';
echo $link;
} else {
echo $default;
}
}
function compressHtml($html_source) {
$chunks = preg_split('/(<!--<nocompress>-->.*?<!--<\/nocompress>-->|<nocompress>.*?<\/nocompress>|<pre.*?\/pre>|<textarea.*?\/textarea>|<script.*?\/script>)/msi', $html_source, -1, PREG_SPLIT_DELIM_CAPTURE);
$compress = '';
foreach ($chunks as $c) {
if (strtolower(substr($c, 0, 19)) == '<!--<nocompress>-->') {
$c = substr($c, 19, strlen($c) - 19 - 20);
$compress .= $c;
continue;
} else if (strtolower(substr($c, 0, 12)) == '<nocompress>') {
$c = substr($c, 12, strlen($c) - 12 - 13);
$compress .= $c;
continue;
} else if (strtolower(substr($c, 0, 4)) == '<pre' || strtolower(substr($c, 0, 9)) == '<textarea') {
$compress .= $c;
continue;
} else if (strtolower(substr($c, 0, 7)) == '<script' && strpos($c, '//') != false && (strpos($c, "\r") !== false || strpos($c, "\n") !== false)) {
$tmps = preg_split('/(\r|\n)/ms', $c, -1, PREG_SPLIT_NO_EMPTY);
$c = '';
foreach ($tmps as $tmp) {
if (strpos($tmp, '//') !== false) {
if (substr(trim($tmp), 0, 2) == '//') {
continue;
}
$chars = preg_split('//', $tmp, -1, PREG_SPLIT_NO_EMPTY);
$is_quot = $is_apos = false;
foreach ($chars as $key => $char) {
if ($char == '"' && $chars[$key - 1] != '\\' && !$is_apos) {
$is_quot = !$is_quot;
} else if ($char == '\'' && $chars[$key - 1] != '\\' && !$is_quot) {
$is_apos = !$is_apos;
} else if ($char == '/' && $chars[$key + 1] == '/' && !$is_quot && !$is_apos) {
$tmp = substr($tmp, 0, $key);
break;
}
}
}
$c .= $tmp;
}
}
$c = preg_replace('/[\\n\\r\\t]+/', ' ', $c);
$c = preg_replace('/\\s{2,}/', ' ', $c);
$c = preg_replace('/>\\s</', '> <', $c);
$c = preg_replace('/\\/\\*.*?\\*\\//i', '', $c);
$c = preg_replace('/<!--[^!]*-->/', '', $c);
$compress .= $c;
}
return $compress;
}
function seoSetting($obj){
}
// 设置时区
date_default_timezone_set('Asia/Shanghai');
/**
* 秒转时间,格式 年 月 日 时 分 秒
*
* @author Roogle
* @return html
*/
function getBuildTime(){
// 在下面按格式输入本站创建的时间
$site_create_time = strtotime('2018-06-10 00:00:00');
$time = time() - $site_create_time;
if(is_numeric($time)){
$value = array(
"years" => 0, "days" => 0, "hours" => 0,
"minutes" => 0, "seconds" => 0,
);
// if($time >= 31556926){
// $value["years"] = floor($time/31556926);
// $time = ($time%31556926);
// }
if($time >= 86400){
$value["days"] = floor($time/86400);
$time = ($time%86400);
}
if($time >= 3600){
$value["hours"] = floor($time/3600);
$time = ($time%3600);
}
if($time >= 60){
$value["minutes"] = floor($time/60);
$time = ($time%60);
}
$value["seconds"] = floor($time);
// echo '已运行'.$value['years'].'年'.$value['days'].'天'.$value['hours'].'小时'.$value['minutes'].'分';
echo 'Running '.$value['days'].'Day'.$value['hours'].'Hor'.$value['minutes'].'Min';
}else{
echo '';
}
}
function get_post_view($archive)
{
$cid = $archive->cid;
$db = Typecho_Db::get();
$prefix = $db->getPrefix();
if (!array_key_exists('views', $db->fetchRow($db->select()->from('table.contents')))) {
$db->query('ALTER TABLE `' . $prefix . 'contents` ADD `views` INT(10) DEFAULT 0;');
echo 0;
return;
}
$row = $db->fetchRow($db->select('views')->from('table.contents')->where('cid = ?', $cid));
if ($archive->is('single')) {
$views = Typecho_Cookie::get('extend_contents_views');
if(empty($views)){
$views = array();
}else{
$views = explode(',', $views);
}
if(!in_array($cid,$views)){
$db->query($db->update('table.contents')->rows(array('views' => (int) $row['views'] + 1))->where('cid = ?', $cid));
array_push($views, $cid);
$views = implode(',', $views);
Typecho_Cookie::set('extend_contents_views', $views); //记录查看cookie
}
}
echo $row['views'];
}
//判断页面加载速度
function timer_start() {
global $timestart;
$mtime = explode( ' ', microtime() );
$timestart = $mtime[1] + $mtime[0];
return true;
}
timer_start();
function timer_stop( $display = 0, $precision = 3 ) {
global $timestart, $timeend;
$mtime = explode( ' ', microtime() );
$timeend = $mtime[1] + $mtime[0];
$timetotal = $timeend - $timestart;
$r = number_format( $timetotal, $precision );
if ( $display )
echo $r;
return $r;
}
//判断内容页是否百度收录
function baidu_record() {
$url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
if(checkBaidu($url)==1)
{echo "百度已收录";
}
else
{echo "<a style=\"color:red;\" rel=\"external nofollow\" title=\"点击提交收录!\" target=\"_blank\" href=\"http://zhanzhang.baidu.com/sitesubmit/index?sitename=$url\">百度未收录</a>";}
}
function checkBaidu($url) {
$url = 'http://www.baidu.com/s?wd=' . urlencode($url);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$rs = curl_exec($curl);
curl_close($curl);
if (!strpos($rs, '没有找到')) { //没有找到说明已被百度收录
return 1;
} else {
return -1;
}
}
function gethosturl(){
$http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
return $http_type . $_SERVER['HTTP_HOST'];
}