-
Notifications
You must be signed in to change notification settings - Fork 3
/
functions.php
210 lines (188 loc) · 7.08 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
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
function themeConfig($form) {
$thename = new Typecho_Widget_Helper_Form_Element_Text('thename', NULL, NULL, _t('首页用户名自定义'), _t('默认管理员昵称'));
$form->addInput($thename);
$avatar_url = new Typecho_Widget_Helper_Form_Element_Text('avatar_url', NULL, NULL, _t('头像地址'), NULL);
$form->addInput($avatar_url);
$beian = new Typecho_Widget_Helper_Form_Element_Text('beian', NULL, NULL, _t('备案号'), NULL);
$form->addInput($beian);
$theme_options = new Typecho_Widget_Helper_Form_Element_Checkbox('theme_options',
array('avatarCircular' => _t('头像圆形'),
'avatarCache' => _t('缓存头像'),
'indent' => _t('首行缩进')),
array('indent'), _t('其他功能')
);
$form->addInput($theme_options->multiMode());
}
function themeConfigHandle($settings, $isInit) {
$db = Typecho_Db::get();
$widget = Typecho_Widget::widget("Widget_Abstract_Options");
$theme = "AirCloud";
$options = Typecho_Widget::widget("Widget_Options");
if ($options->__get('theme:' . $theme)) {
$widget->update(array('value' => serialize($settings)), $db->sql()->where('name = ?', 'theme:' . $theme));
} else {
$widget->insert(array('name' => 'theme:' . $theme, 'value' => serialize($settings), 'user' => 0));
}
if ($settings['avatar_url']) {
if(!empty($settings['theme_options']) && in_array('avatarCache', $settings['theme_options'])){
$avatar_data = file_get_contents($settings['avatar_url']);
file_put_contents(dirname(__FILE__).'/avatar.png',$avatar_data);
}else{
unlink(dirname(__FILE__).'/avatar.png');
}
}
}
function themeInit($archive) {
if ($archive->is('single')) {
$archive->content = createCatalog($archive->content);
}
}
function createCatalog($obj) { //为文章标题添加锚点
global $catalog;
$catalog = array();
$obj = preg_replace_callback('/<h([1-6])(.*?)>(.*?)<\/h\1>/i', function($obj) {
global $catalog;
$catalog[] = array('text' => trim(strip_tags($obj[3])), 'depth' => $obj[1]);
return '<h'.$obj[1].$obj[2].' id="'.$obj[3].'"><a name="'.$obj[3].'"></a>'.$obj[3].'</h'.$obj[1].'>';
}, $obj);
return $obj;
}
function getCatalog() { //输出文章目录容器
global $catalog;
$index = '';
if ($catalog) {
$index = '<ol class="toc">'."\n";
$prev_depth = '';
$to_depth = 0;
foreach($catalog as $catalog_item) {
$catalog_depth = $catalog_item['depth'];
if ($prev_depth) {
if ($catalog_depth == $prev_depth) {
$index .= '</li>'."\n";
} elseif ($catalog_depth > $prev_depth) {
$to_depth++;
$index .= '<ol class="toc-child">'."\n";
} else {
$to_depth2 = ($to_depth > ($prev_depth - $catalog_depth)) ? ($prev_depth - $catalog_depth) : $to_depth;
if ($to_depth2) {
for ($i=0; $i<$to_depth2; $i++) {
$index .= '</li>'."\n".'</ol>'."\n";
$to_depth--;
}
}
$index .= '</li>';
}
}
$index .= '<li class="toc-item"><a class="toc-link" href="#'.$catalog_item['text'].'">'.'<span class="toc-text">'.$catalog_item['text'].'</span>'.'</a>';
$prev_depth = $catalog_item['depth'];
}
for ($i=0; $i<=$to_depth; $i++) {
$index .= '</li>'."\n".'</ol>'."\n";
}
$index = '<div id="toc" class="toc-article">'."\n".$index.'</div>'."\n";
}
echo $index;
}
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','status')
->from('table.comments')
->where('coid = ?', $parent));
$author = $arow['author'];
$status = $arow['status'];
if($author){
if($status=='approved'){
$href = ' <a class="at" uid="'.$parent.'" onclick="scrollt(\'comment-'.$parent.'\'); return false;">@'.$author.'</a>';
}else if($status=='waiting'){
$href = '<a href="javascript:void(0)">评论审核中···</a>';
}
}
echo $href;
} else {
echo "";
}
}
function timesince($older_date,$comment_date = false) {
$chunks = array(
array(86400 , '天'),
array(3600 , '小时'),
array(60 , '分钟'),
array(1 , '秒'),
);
$newer_date = time();
$since = abs($newer_date - $older_date);
for ($i = 0, $j = count($chunks); $i < $j; $i++){
$seconds = $chunks[$i][0];
$name = $chunks[$i][1];
if (($count = floor($since / $seconds)) != 0) break;
}
$output = $count.$name;
return $output;
}
/**
* 静态缓存类
*/
class cacheFile
{
private $_dir;
const EXT = '.json';
public function __construct() {
$this->_dir = dirname(__FILE__).'/caches/';
}
public function cacheData($key, $value = '', $path = '') {
$filePath = $this->_dir.$path.$key.self::EXT;
if ($value !== '') {
if (is_null($value)) {
return unlink($filePath);
}
$dir = dirname($filePath);
if (!is_dir($dir)) {
mkdir($dir, 0777);
}
return file_put_contents($filePath,$value);
}
if (!is_file($filePath)) {
return false;
} else {
echo $filePath;
return json_decode(file_get_contents($filePath), true);
}
}
}
$TheFile = dirname(__FILE__).'/caches/cache.json';
$cacheFile = new cacheFile();
$output = "";
$vowels = array("[", "{","]","}","<",">","\r\n", "\r", "\n","-","'",'"','`'," ",":",";",'\\'," "," ");
Typecho_Widget::widget('Widget_Contents_Post_Recent','pageSize=10000')->to($archives);
while($archives->next()):
$output .= '{"this":"post","link":"'.$archives->permalink.'","title":"'.$archives->title.'","text":"'.str_replace($vowels, "",$archives->text).'"},';
endwhile;
Typecho_Widget::widget('Widget_Contents_Page_List')->to($pages);
while($pages->next()):
$output .= '{"this":"page","link":"'.$pages->permalink.'","title":"'.$pages->title.'","text":"'.str_replace($vowels, "",$pages->text).'"},';
endwhile;
Typecho_Widget::widget('Widget_Metas_Tag_Cloud')->to($tags);
while ($tags->next()):
$output .= '{"this":"tag","link":"'.$tags->permalink.'","title":"'.$tags->name.'","text":"'.str_replace($vowels, "",$tags->description).'"},';
endwhile;
Typecho_Widget::widget('Widget_Metas_Category_List')->to($category);
while ($category->next()):
$output .= '{"this":"category","link":"'.$category->permalink.'","title":"'.$category->name.'","text":"'.str_replace($vowels, "",$category->description).'"},';
endwhile;
$output = substr($output,0,strlen($output)-1);
$data = '['.$output.']';
if (file_exists($TheFile)) {
if ( time() - filemtime( $TheFile) > 300){
$cacheFile->cacheData('cache', $data);
}; //5分钟300秒,判断文件时间可以自己调整
} else {
$cacheFile->cacheData('cache', $data);
};
?>