This repository has been archived by the owner on Oct 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathfunctions.php
221 lines (197 loc) · 7.67 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
<?php
/**
* Castle Functions
* Last Update: 2020/05/14
*/
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
define('CASTLE_ENV', 'dev');
//判断 PHP 版本是否达到要求
if (substr(PHP_VERSION, 0, 3) < '7.0') {
echo '<h2>PHP版本必须 >= 7.0</h2>';
die();
}
//不存在的错误
error_reporting(0);
//设置时区 [上海]
//如果时间显示有误请注解
date_default_timezone_set("Asia/Shanghai");
//引用核心文件
require_once __DIR__ . '/core/libs/libs.php';
//常量
define('CASTLE_VERSION', Castle_Libs::getThemeVersion());
//引用文件
Castle_Libs::requireFile(__DIR__ . '/core/libs/', 'php');
$GLOBALS['CastleLang'] = Castle_Lang::getLang();
//向编辑界面添加按钮
Typecho_Plugin::factory('admin/write-post.php')->bottom = array('Castle_Libs', 'addButtons');
Typecho_Plugin::factory('admin/write-page.php')->bottom = array('Castle_Libs', 'addButtons');
//内容解析
Typecho_Plugin::factory('Widget_Abstract_Contents')->markdown = array('Castle_Contents', 'markdown');
Typecho_Plugin::factory('Widget_Abstract_Contents')->contentEx = array('Castle_Contents', 'contentEx');
/**
* Theme Init
*
* @param $archive Widget_Archive
*/
function themeInit($archive)
{
//已经兼容反垃圾
//Helper::options()->commentsAntiSpam = false; //关闭评论反垃圾(否则与PJAX冲突)
Helper::options()->commentsMarkdown = true; //启用评论可使用MarkDown语法
Helper::options()->commentsCheckReferer = false; //关闭检查评论来源URL与文章链接是否一致判断(否则会无法评论)
Helper::options()->commentsPageBreak = true; //是否开启评论分页
Helper::options()->commentsPageSize = 5; //评论每页显示条数
Helper::options()->commentsPageDisplay = 'first'; //默认显示第一页
Helper::options()->commentsOrder = 'DESC'; //将较新的评论展示在第一页
Helper::options()->commentsMaxNestingLevels = 999; //最大回复层数
Helper::options()->commentsHTMLTagAllowed = '<a href=""> <img src=""> <img src="" class=""> <pre> <code> <code class=""> <del>'; //评论允许使用的标签
//站点配置文件
if ($_SERVER['REQUEST_METHOD'] == 'GET' && @$_GET["action"] == 'siteConfig') {
Castle_API::siteConfig();
die();
}
//登录状态
if ($_SERVER['REQUEST_METHOD'] == 'GET' && @$_GET["action"] == 'loginStatus') {
Castle_API::loginStatus();
die();
}
//表情配置文件
if ($_SERVER['REQUEST_METHOD'] == 'GET' && @$_GET["action"] == 'owoConfig') {
$arr = [
'setting' => [
'owoList' => (Castle_OwO::getOwOList()) ? Castle_OwO::getOwOList() : false
]
];
echo 'var CastleConfig = ' . json_encode($arr) . ';';
die();
}
//登陆
if ($_SERVER['REQUEST_METHOD'] == 'POST' && @$_GET["action"] == 'login' && @$_GET['_'] != NULL && Helper::options()->sidebarToolsBar && in_array('login', Helper::options()->sidebarToolsBar)) {
header('Content-type: application/json');
echo json_encode((new TypechoLogin())->Login());
die();
}
//退出登录
if ($_SERVER['REQUEST_METHOD'] == 'GET' && @$_GET["action"] == 'logout' && @$_GET['_'] != NULL && Helper::options()->sidebarToolsBar && in_array('login', Helper::options()->sidebarToolsBar)) {
header('Content-type: application/json');
echo json_encode((new TypechoLogin())->Logout());
die();
}
//获取登陆状态
if ($_SERVER['REQUEST_METHOD'] == 'GET' && @$_GET["action"] == 'hasLogin') {
Typecho_Widget::widget('Widget_User')->to($user);
if ($user->hasLogin()) {
$arr['status'] = true;
} else {
$arr['status'] = false;
}
header('Content-type: application/json');
echo json_encode($arr);
die();
}
//获取评论者头像
if ($_SERVER['REQUEST_METHOD'] == 'GET' && @$_GET["action"] == 'ajax_get_avatar') {
echo Castle_Avatar::getCommentAvatar($_GET['email'], 100);
die();
}
}
/**
* 自定义字段
*/
function themeFields($layout)
{
$excerpt = new Typecho_Widget_Helper_Form_Element_Textarea('excerpt', null, null, '文章摘要', '自定义简介摘要,如不填将自动摘取前 100 字。');
$layout->addItem($excerpt);
$cover = new Typecho_Widget_Helper_Form_Element_Text('cover', NULL, NULL, _t('文章封面图'), _t('自定义封面,如不填将显示随机封面图。'));
$layout->addItem($cover);
//对于魔改的后台模板不保证能正常判断
preg_match('/write-post.php/', $_SERVER['SCRIPT_NAME'], $post);
if (@$post[0] == 'write-post.php') {
$PostType = new Typecho_Widget_Helper_Form_Element_Select(
'PostType',
array(
'post' => '文章(默认)',
'nopic' => '无图',
//'dynamic'=>'日常'
),
'post',
'文章类型',
'设置发表的文章的类型(仅对文章有效)。'
);
$layout->addItem($PostType);
}
$showToc = new Typecho_Widget_Helper_Form_Element_Select(
'showToc',
[
'0' => '不显示目录',
'1' => '显示目录'
],
'0',
'目录树',
'是否显示文章/普通页面目录树'
);
$layout->addItem($showToc);
$advancedSettings = new Typecho_Widget_Helper_Form_Element_Textarea(
'advancedSettings',
NULL,
NULL,
'高级设置',
'文章/独立页高级设置,如果不懂此有何用请勿填写。'
);
$layout->addItem($advancedSettings);
}
/**
* HTML 压缩
*
* @author LiNPX
* @link https://www.linpx.com/p/pinghsu-subject-integration-code-compression.html
*/
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;
} elseif (strtolower(substr($c, 0, 4)) == '<pre' || strtolower(substr($c, 0, 9)) == '<textarea') {
$compress .= $c;
continue;
} elseif (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;
} elseif ($char == '\'' && $chars[$key - 1] != '\\' && !$is_quot) {
$is_apos = !$is_apos;
} elseif ($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;
}