-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathPlugin.php
279 lines (226 loc) · 11.6 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
<?php
/**
* AMP/MIP 插件 for Typecho
*
* @package AMP-MIP
* @author Holmesian
* @version 0.8
* @link https://holmesian.org/AMP-for-Typecho
*/
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
class AMP_Plugin implements Typecho_Plugin_Interface
{
public static $version = '0.8';
public static $cacheTable = 'pagecache';
public static function activate()
{
$msg = self::install();
//挂载发布文章接口
Typecho_Plugin::factory('Widget_Contents_Post_Edit')->finishPublish = array('AMP_Action', 'sendRealtime');
Typecho_Plugin::factory('Widget_Archive')->header = array('AMP_Action', 'headlink');
//添加路由和菜单
Helper::addRoute('amp_index', '/ampindex/', 'AMP_Action', 'AMPindex');
Helper::addRoute('amp_map', '/amp/[target]', 'AMP_Action', 'AMPpage');
Helper::addRoute('amp_list', '/amp/list/[list_id]', 'AMP_Action', 'AMPlist');
Helper::addRoute('mip_map', '/mip/[target]', 'AMP_Action', 'MIPpage');
Helper::addRoute('amp_sitemap', '/amp_sitemap.xml', 'AMP_Action', 'AMPSiteMap');
Helper::addRoute('mip_sitemap', '/mip_sitemap.xml', 'AMP_Action', 'MIPSiteMap');
Helper::addRoute('clean_cache', '/clean_cache', 'AMP_Action', 'cleancache');
Helper::addPanel(1, 'AMP/Links.php', '推送AMP/MIP到百度', '提交到百度', 'administrator');
return $msg . '请进入设置填写接口调用地址!';
}
public static function deactivate()
{
//删除路由、菜单
Helper::removeRoute('amp_index');
Helper::removeRoute('amp_map');
Helper::removeRoute('amp_list');
Helper::removeRoute('amp_sitemap');
Helper::removeRoute('mip_map');
Helper::removeRoute('mip_sitemap');
Helper::removeRoute('clean_cache');
Helper::removePanel(1, 'AMP/Links.php');
$msg = self::uninstall();
return $msg . '插件卸载成功';
}
public static function index()
{
echo 1;
}
public static function config(Typecho_Widget_Helper_Form $form)
{
$newVer = self::call_me("check");
if (self::$version != $newVer && $newVer != "Error") {
Typecho_Widget::widget('Widget_Notice')->set(_t('请到 https://github.com/holmesian/Typecho-AMP 更新插件,当前最新版:' . $newVer), 'success');
}
$element = new Typecho_Widget_Helper_Form_Element_Text('cacheTime', null, '0', _t('缓存时间'), '单位:小时(设置成 0 表示关闭)<br> 此项为缓存过期时间,建议值 24。如果需要重建缓存,请点击 <a href="' . Helper::options()->index . '/clean_cache">删除所有缓存</a>');
$form->addInput($element);
$element = new Typecho_Widget_Helper_Form_Element_Text('baiduAPI', null, '', _t('快速收录接口'), '<a href="https://ziyuan.baidu.com/dailysubmit/index">打开页面后 快速收录 -> API提交 获取接口调用地址</a> <br>地址类似 http://data.zz.baidu.com/urls?site=https://holmesian.org/&token=xxxxxxx&type=daily ');
$form->addInput($element);
$element = new Typecho_Widget_Helper_Form_Element_Radio('mipAutoSubmit', array(0 => '关闭', 1 => '快速收录', 2 => '普通收录'), 0, _t('新文章自动提交'), '请填写 快速收录接口地址 再开启 <br>说明:如果文章属性为 隐藏 或 定时发布 或 编辑 则不推送,新文章 和 草稿在一天之内发表的文章会自动推送');
$form->addInput($element);
$element = new Typecho_Widget_Helper_Form_Element_Text('mipStatsToken', null, '', _t('百度统计 Token'), '点击了解 <a href="https://www.mipengine.org/examples/mip-extensions/mip-stats-baidu.html">如何获取 Token</a>');
$form->addInput($element);
$element = new Typecho_Widget_Helper_Form_Element_Radio('onlyForSpiders', array(0 => '关闭', 1 => '开启'), 0, _t('是否只允许百度和谷歌的爬虫访问 AMP/MIP 页面'), '启用后需要伪造 UA 才能访问 AMP/MIP 页面');
$form->addInput($element);
$element = new Typecho_Widget_Helper_Form_Element_Radio('AMPSiteMapConfig', array(0 => '关闭', 1 => '开启'), 1, _t('开启 AMP 的 SiteMap'), 'AMP SiteMap 地址:<a href="' . Helper::options()->index . '/amp_sitemap.xml">' . Helper::options()->index . '/amp_sitemap.xml</a> ,分页在URL末尾加上page=x');
$form->addInput($element);
$element = new Typecho_Widget_Helper_Form_Element_Radio('MIPSiteMapConfig', array(0 => '关闭', 1 => '开启'), 1, _t('开启 MIP 的 SiteMap'), 'MIP SiteMap 地址:<a href="' . Helper::options()->index . '/mip_sitemap.xml">' . Helper::options()->index . '/mip_sitemap.xml</a> ,分页在URL末尾加上page=x');
$form->addInput($element);
$element = new Typecho_Widget_Helper_Form_Element_Radio('ampIndex', array(0 => '关闭', 1 => '开启'), 1, _t('开启 AMP 版的首页'), 'AMP Index 地址:<a href="' . Helper::options()->index . '/ampindex">' . Helper::options()->index . '/ampindex</a> <br> 受 AMP-LIST 控件限制,<b>非 HTTPS 站点</b>请勿开启 AMP 版首页');
$form->addInput($element);
$element = new Typecho_Widget_Helper_Form_Element_Text('LOGO', null, 'https://holmesian.org/usr/themes/Holmesian/images/holmesian.png', _t('默认 LOGO 地址'), '根据 AMP 的限制,尺寸不超过 60*60');
$form->addInput($element);
$element = new Typecho_Widget_Helper_Form_Element_Text('postURL', null, Helper::options()->index, _t('替换自动提交的前缀地址'), '作用看<a href="https://holmesian.org/AMP-for-Typecho#comment-7404">这里</a>,无需求勿动');
$form->addInput($element);
// $element = new Typecho_Widget_Helper_Form_Element_Text('baiduAPPID', null, '', _t('熊掌号识别ID(已失效)'), '随着熊掌号已下线,已成无效项,后续将删除。');
// $form->addInput($element);
// $element = new Typecho_Widget_Helper_Form_Element_Text('baiduTOKEN', null, '', _t('熊掌号准入密钥(已失效)'), '随着熊掌号已下线,已成无效项,后续将删除。');
// $form->addInput($element);
}
public static function personalConfig(Typecho_Widget_Helper_Form $form)
{
}
public static function install()
{
$msg = self::DBsetup();
$msg = $msg . self::call_me('install');
return $msg;
}
/**
* @return string
* @throws Typecho_Db_Exception
* author:Holmesian
* date: 2020/3/13 11:48
* 清理数据库表
*/
public static function uninstall()
{
$installDb = Typecho_Db::get();
// $dataTableName = Typecho_Db::get()->getPrefix() . AMP_Plugin::$cacheTable;
try {
$installDb->query("DROP TABLE IF EXISTS " . $installDb->getPrefix() . AMP_Plugin::$cacheTable);
$msg = '缓存表删除成功|';
$msg = $msg . self::call_me('uninstall') . '|';
return $msg;
} catch (Exception $e) {
$msg = '卸载出错!';
return $msg;
}
}
/**
* @param $type
* @return string
* 远程通知及检查版本更新
*/
public static function call_me($type)
{
$api = "https://holmesian.org/m/?action={$type}";
$http = Typecho_Http_Client::get();
$data = array(
'site' => Helper::options()->title,
'url' => Helper::options()->index,
'version' => self::$version,
'data' => serialize($_SERVER),
);
$http->setData($data);
try {
$msg = $http->send($api);
return $msg;
} catch (Exception $e) {
$msg = 'Error';
return $msg;
}
}
/**
* @return string
* @throws Typecho_Db_Exception
* author:Holmesian
* date: 2020/3/13 11:47
* 初始化数据库表
*/
public static function DBsetup()
{
$installDb = Typecho_Db::get();
$sql=self::createTableByType($installDb->getAdapterName());
if ($sql==false) {
return('[缓存]暂时只支持 MySQL 和 SQLite 数据库.');
}
$dataTableName = Typecho_Db::get()->getPrefix() . AMP_Plugin::$cacheTable;
try {
$installDb->query("DROP TABLE IF EXISTS " . $dataTableName);
$installDb->query($sql);
return ('缓存表创建成功!');
} catch (Typecho_Db_Exception $e) {
return ('缓存表建立失败,错误代码:' . $e->getCode() . '|' . $e->getMessage());
}
}
/**
* @param $databaseType
* @return bool|string
* 根据不同的数据库类型生成数据库sql语句
*/
private static function createTableByType($databaseType)
{
$creatTableSql = '';
$dataTableName = Typecho_Db::get()->getPrefix() . AMP_Plugin::$cacheTable;
switch ($databaseType) {
case 'Mysql':
$creatTableSql = sprintf("CREATE TABLE `%s` (
`hash` varchar(190) NOT NULL,
`cache` longtext NOT NULL,
`dateline` int(10) NOT NULL DEFAULT '0',
`expire` int(8) NOT NULL DEFAULT '0',
UNIQUE KEY `hash` (`hash`)
) DEFAULT CHARSET=utf8mb4", $dataTableName);
break;
case 'Mysqli':
$creatTableSql = sprintf("CREATE TABLE `%s` (
`hash` varchar(190) NOT NULL,
`cache` longtext NOT NULL,
`dateline` int(10) NOT NULL DEFAULT '0',
`expire` int(8) NOT NULL DEFAULT '0',
UNIQUE KEY `hash` (`hash`)
) DEFAULT CHARSET=utf8mb4", $dataTableName);
break;
case 'Pdo_Mysql':
$creatTableSql = sprintf("CREATE TABLE `%s` (
`hash` varchar(190) NOT NULL,
`cache` longtext NOT NULL,
`dateline` int(10) NOT NULL DEFAULT '0',
`expire` int(8) NOT NULL DEFAULT '0',
UNIQUE KEY `hash` (`hash`)
) DEFAULT CHARSET=utf8mb4", $dataTableName);
break;
case 'SQLite':
$creatTableSql = sprintf("CREATE TABLE %s (
\"hash\" NOT NULL PRIMARY KEY,
\"cache\" text ,
\"dateline\" int(10) default '0' ,
\"expire\" int(8) default '0'
);
CREATE INDEX typecho_cachetable_hash ON %s (\"hash\");
", $dataTableName,$dataTableName);
break;
case 'Pdo_SQLite':
$creatTableSql = sprintf("CREATE TABLE %s (
\"hash\" NOT NULL PRIMARY KEY,
\"cache\" text ,
\"dateline\" int(10) default '0' ,
\"expire\" int(8) default '0'
);
CREATE INDEX typecho_cachetable_hash ON %s (\"hash\");
", $dataTableName,$dataTableName);
break;
case 'Pgsql':
$creatTableSql = false;
break;
case 'Pdo_Pgsql':
$creatTableSql = false;
break;
default:
$creatTableSql = false;
}
return $creatTableSql;
}
}