-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathPlugin.php
631 lines (555 loc) · 22.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
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* Typecho缓存插件 , 例如:redis 缓存需要装 redis以及php里需要启用redis扩展
*
* @package TpCache
* @author Suroy,gogobody
* @version 1.0.6
* @link https://www.ijkxs.com
*/
class TpCache_Plugin implements Typecho_Plugin_Interface
{
public static $cache = null;
public static $html = null;
public static $path = null;
public static $sys_config = null;
public static $plugin_config = null;
public static $request = null;
public static $passed = false;
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
// 全局缓存
// 在index.php开始, 尝试使用缓存
Typecho_Plugin::factory('index.php')->begin = array(__CLASS__, 'C');
// 在index.php结束, 尝试写入缓存
Typecho_Plugin::factory('index.php')->end = array(__CLASS__, 'S');
// 编辑页面后更新缓存
Typecho_Plugin::factory('Widget_Contents_Post_Edit')->finishPublish = array(__CLASS__, 'post_update');
Typecho_Plugin::factory('Widget_Contents_Page_Edit')->finishPublish = array(__CLASS__, 'post_update');
// 删除页面后更新缓存
Typecho_Plugin::factory('Widget_Contents_Post_Edit')->delete = array(__CLASS__, 'post_del_update');
Typecho_Plugin::factory('Widget_Contents_Page_Edit')->delete = array(__CLASS__, 'post_del_update');
//评论
Typecho_Plugin::factory('Widget_Feedback')->finishComment = array(__CLASS__, 'comment_update');
//评论后台
Typecho_Plugin::factory('Widget_Comments_Edit')->finishDelete = array(__CLASS__, 'comment_update2');
Typecho_Plugin::factory('Widget_Comments_Edit')->finishEdit = array(__CLASS__, 'comment_update2');
Typecho_Plugin::factory('Widget_Comments_Edit')->finishComment = array(__CLASS__, 'comment_update2');
// 部分缓存
// 缓存MarkDown
Typecho_Plugin::factory('Widget_Abstract_Contents')->contentEx = array(__CLASS__, 'cache_contentEx');
// 插入写作标签
Typecho_Plugin::factory('admin/write-post.php')->bottom_100 = array(__CLASS__, 'forTpCacheToolbar');
Typecho_Plugin::factory('admin/write-page.php')->bottom_100 = array(__CLASS__, 'forTpCacheToolbar');
// 插件接口
Typecho_Plugin::factory('TpCache.Widget_Cache')->getCache = array(__CLASS__, 'TpCache_getCache');
Typecho_Plugin::factory('TpCache.Widget_Cache')->setCache = array(__CLASS__, 'TpCache_setCache');
return '插件安装成功,请设置需要缓存的页面';
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @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)
{
?>
<div class="j-setting-contain">
<link href="<?php echo Helper::options()->rootUrl ?>/usr/plugins/TpCache/assets/css/joe.setting.min.css" rel="stylesheet" type="text/css" />
<div>
<div class="j-aside">
<div class="logo">TpCache Modify</div>
<ul class="j-setting-tab">
<li data-current="j-setting-notice">插件公告</li>
<li data-current="j-setting-config">缓存设置</li>
<li data-current="j-setting-global">全局缓存</li>
<li data-current="j-setting-normal">部分缓存</li>
</ul>
<?php require_once('Backups.php'); ?>
</div>
</div>
<span id="j-version" style="display: none;">1.0.5</span>
<div class="j-setting-notice">请求数据中...</div>
<script src="<?php echo Helper::options()->rootUrl ?>/usr/plugins/TpCache/assets/js/joe.setting.min.js"></script>
<?php
$list = array('关闭', '开启');
$element = new Typecho_Widget_Helper_Form_Element_Radio('login', $list, 1, '是否对已登录用户失效', '已经登录用户不会触发缓存策略');
$element->setAttribute('class', 'j-setting-content j-setting-config');
$form->addInput($element);
$list = array('关闭', '开启');
$element = new Typecho_Widget_Helper_Form_Element_Radio('enable_ssl', $list, '0', '是否支持SSL');
$element->setAttribute('class', 'j-setting-content j-setting-config');
$form->addInput($element);
$list = array(
'0' => '不使用缓存',
'memcached' => 'Memcached',
'redis' => 'Redis'
);
$element = new Typecho_Widget_Helper_Form_Element_Radio('cache_driver', $list, '0', '缓存驱动');
$element->setAttribute('class', 'j-setting-content j-setting-config');
$form->addInput($element);
$element = new Typecho_Widget_Helper_Form_Element_Text('expire', null, '86400', '缓存过期时间', '86400 = 60s * 60m *24h,即一天的秒数');
$element->setAttribute('class', 'j-setting-content j-setting-config');
$form->addInput($element);
$element = new Typecho_Widget_Helper_Form_Element_Text('host', null, '127.0.0.1', '主机地址', '主机地址,一般为127.0.0.1');
$element->setAttribute('class', 'j-setting-content j-setting-config');
$form->addInput($element);
$element = new Typecho_Widget_Helper_Form_Element_Text('port', null, '11211', '端口号', 'memcache(d)默认为11211,redis默认为6379,其他类型随意填写');
$element->setAttribute('class', 'j-setting-content j-setting-config');
$form->addInput($element);
//$list = array('关闭', '开启');
//$element = new Typecho_Widget_Helper_Form_Element_Radio('is_debug', $list, 0, '是否开启debug');
//$form->addInput($element);
// 域名修正
$fixDomain = $_SERVER['SERVER_ADDR'].'|'.$_SERVER['SERVER_NAME'];
$element = new Typecho_Widget_Helper_Form_Element_Text('domain', null , (string)$fixDomain, '域名地址修正', '将直接服务器IP地址输出修正为域名输出');
$element->setAttribute('class', 'j-setting-content j-setting-config');
$form->addInput($element);
$list = array('关闭', '清除所有数据');
$element = new Typecho_Widget_Helper_Form_Element_Radio('is_clean', $list, 0, '清除所有数据');
$element->setAttribute('class', 'j-setting-content j-setting-config');
$form->addInput($element);
// 全局缓存
$list = array('0'=>'关闭', '1'=>'开启');
$element = new Typecho_Widget_Helper_Form_Element_Radio('enable_gcache', $list, '1', '是否开启全局缓存','在开启全局缓存的情况下,该页面缓存选项有效');
$element->setAttribute('class', 'j-setting-content j-setting-global');
$form->addInput($element);
$list = array(
'index' => '首页',
'archive' => '归档',
'post' => '文章',
'attachment' => '附件',
'category' => '分类',
'tag' => '标签',
'author' => '作者',
'search' => '搜索',
'feed' => 'feed',
'page' => '页面'
);
$element = new Typecho_Widget_Helper_Form_Element_Checkbox('cache_page', $list, array('index', 'post', 'search', 'page', 'author', 'tag'), '需要缓存的页面');
$element->setAttribute('class', 'j-setting-content j-setting-global');
$form->addInput($element);
$list = array('0'=>'关闭', '1'=>'开启');
$element = new Typecho_Widget_Helper_Form_Element_Radio('enable_markcache', $list, '1', '是否开启markdown缓存','在全局缓存命中失效的时候是否开启 markdown 部分缓存,
选项关闭,则未命中均不缓存。选项开启后,在文章编辑界面通过 NOCACHE 标签来决定是否缓存当前文章,按钮已经集成了,插入标签就表示不缓存<br>');
$element->setAttribute('class', 'j-setting-content j-setting-normal');
$form->addInput($element);
}
public static function personalConfig(Typecho_Widget_Helper_Form $form) {}
public static function configHandle($config, $is_init)
{
if ($config['cache_driver'] != '0') {
self::initBackend($config['cache_driver']);
if ($config['is_clean'] == '1') {
try {
self::$cache->flush();
} catch (Exception $e) {
print $e->getMessage();
}
$config['is_clean'] = '0';
}
}else{
// 关闭其他插件选项
if ($config['enable_gcache'] == '1'){
$config['enable_gcache'] = 0;
}
if ($config['enable_markcache'] == '1'){
$config['enable_markcache'] = 0;
}
}
Helper::configPlugin('TpCache', $config);
}
/**
* 尝试使用缓存
*/
public static function C()
{
self::initEnv();
if (!self::preCheck()) return;
if (!self::initPath()) return;
// 全局缓存关闭则直接返回
if (self::$plugin_config->enable_gcache == '0')
return ;
try {
// 获取当前url的缓存
$data = self::getCache();
if (!empty($data)) {
//缓存未过期, 跳过之后的缓存重写入
if ($data['time'] + self::$plugin_config->expire < time())
self::$passed = false;
// 缓存命中 // 这里是我个人用来控制 主题黑夜模式的... | 加入判断逻辑以适配不同主题
$html = str_replace('{colorMode}',(isset($_COOKIE['night']) && ($_COOKIE['night']) =='1')?'dark':'light',$data['html']);
$html = str_replace('<!--cache tag-->','<!--cache open-->',$data['html']);
$domain = explode('|', self::$plugin_config->domain);
$html = str_replace($domain[0], $domain[1], $html); //修正全站域名错误
// if(isset($_GET['debug'])){print_r(Helper::options()->siteurl());print_r($data);}
header("TpCache: HIT");
echo $html;
die;
} else {
header("TpCache: MISS");
}
} catch (Exception $e) {
header("TpCache: ERROR");
echo $e->getMessage();
}
// 先进行一次刷新
ob_flush();
}
/**
* 写入缓存页面
*/
public static function S($html = '')
{
if (is_null(self::$path) || !self::$passed)
return;
if (empty($html))
$html = ob_get_contents();
$data = array();
$data['time'] = time();
$data['html'] = $html;
self::setCache($data);
}
public static function getCache($name = null)
{
if ($name) return unserialize(self::$cache->get($name));
return unserialize(self::$cache->get(self::$path));
}
public static function setCache($data, $name = null)
{
if ($name) return self::$cache->set($name, serialize($data));
return self::$cache->set(self::$path, serialize($data));
}
public static function delCache($path, $rmHome = True)
{
self::$cache->delete($path);
if ($rmHome)
self::$cache->delete('/');
}
public static function preCheck($checkPost = True)
{
if ($checkPost && self::$request->isPost()) return false;
if (self::$plugin_config->login && Typecho_Widget::widget('Widget_User')->hasLogin())
return false;
if (self::$plugin_config->enable_ssl == '0' && self::$request->isSecure() == true)
return false;
if (self::$plugin_config->cache_driver == '0')
return false;
self::$passed = true;
return true;
}
public static function initEnv()
{
if (is_null(self::$sys_config))
self::$sys_config = Helper::options();
if (is_null(self::$plugin_config))
self::$plugin_config = self::$sys_config->plugin('TpCache');
if (is_null(self::$request))
self::$request = new Typecho_Request();
}
public static function initPath($pathInfo='')
{
if(empty($pathInfo))
$pathInfo = self::$request->getInstance()->getPathInfo();
if (!self::needCache($pathInfo)) return false;
self::$path = $pathInfo;
return self::initBackend(self::$plugin_config->cache_driver);
}
public static function initBackend($backend){
$class_name = "typecho_$backend";
require_once 'driver/cache.interface.php';
require_once "driver/$class_name.class.php";
self::$cache = call_user_func(array($class_name, 'getInstance'), self::$plugin_config);
if (is_null(self::$cache))
return false;
return true;
}
public static function needCache($path)
{
if(isset($_REQUEST['api'])) return false;
// 后台数据不缓存
$pattern = '#^' . __TYPECHO_ADMIN_DIR__ . '#i';
if (preg_match($pattern, $path)) return false;
// action动作不缓存
$pattern = '#^/action#i';
if (preg_match($pattern, $path)) return false;
// fix:pjax search 失效
$requestUrl = self::$request->getRequestUri();
// search 请求第一次不缓存
$pattern = '/.*?s=.*/i';
if (preg_match($pattern, $requestUrl)) return false;
// search 重定向后可以缓存
$pattern = '#^/search#i';
if (preg_match($pattern, $path) and in_array('search', self::$plugin_config->cache_page)) return true;
$_routingTable = self::$sys_config->routingTable;
// 针对文章页做特殊处理,付费文章不缓存
$post_regx = $_routingTable[0]['post']['regx'];
if (preg_match($post_regx,$path,$arr)){
/**
* 付费文章不缓存
* [0] => /archives/377.html
* [1] => 377
*/
if ($arr[1] and !empty($arr[1])){
// 查看文章是否是 tepass 付费文章
$db = Typecho_Db::get();
try {
// fix: 版本不同导致读取数据库名错误 @zsuroy
$database = isset($db->getConfig($db::READ)['database']) ? $db->getConfig($db::READ)['database'] : $db->getConfig($db::READ)[0]->database;
$tepass_exist = $db->fetchRow($db->select()->from('information_schema.TABLES')->where('TABLE_NAME = ?',$db->getPrefix().'tepass_posts')->where('TABLE_SCHEMA = ?',$database));
if (isset($tepass_exist) and count($tepass_exist) > 0){
$p_id = @$db->fetchObject($db->select('id')->from('table.tepass_posts')->where('post_id = ?',$arr[1]))->id;
if ($p_id) return false;
}
}catch (Typecho_Db_Query_Exception $e){
// 没有tepass
}
}
}
foreach ($_routingTable[0] as $page => $route) {
if ($route['widget'] != 'Widget_Archive' && $route['widget'] != '\Widget\Archive') continue;
if (preg_match($route['regx'], $path)) {
$exclude = array('_year', '_month', '_day', '_page');
$page = str_replace($exclude, '', $page);
if (in_array($page, self::$plugin_config->cache_page))
return true;
}
}
return false;
}
public static function post_update($contents, $class)
{
if ('publish' != $contents['visibility'] || $contents['created'] > time())
return;
self::initEnv();
if (self::$plugin_config->cache_driver == '0')
return;
self::$passed = true;
$type = $contents['type'];
$routeExists = (NULL != Typecho_Router::get($type));
if (!$routeExists) {
self::initPath('#');
self::delCache(self::$path);
return;
}
$db = Typecho_Db::get();
$contents['cid'] = $class->cid;
$contents['categories'] = $db->fetchAll($db->select()->from('table.metas')
->join('table.relationships', 'table.relationships.mid = table.metas.mid')
->where('table.relationships.cid = ?', $contents['cid'])
->where('table.metas.type = ?', 'category')
->order('table.metas.order', Typecho_Db::SORT_ASC));
$contents['category'] = urlencode(current(Typecho_Common::arrayFlatten($contents['categories'], 'slug')));
$contents['slug'] = urlencode(empty($contents['slug'])?$class->slug:$contents['slug']);
$contents['date'] = new Typecho_Date($contents['created']);
$contents['year'] = $contents['date']->year;
$contents['month'] = $contents['date']->month;
$contents['day'] = $contents['date']->day;
if (!self::initPath(Typecho_Router::url($type, $contents))){
return;
// throw new Typecho_Exception('初始化失败。url info:'.Typecho_Router::url($type, $contents));
}
self::delCache(self::$path);
// 同时,删除 markdown 的部分缓存
if ($class->cid)
self::delCache(self::getPostMarkCacheName($class->cid));
}
public static function post_del_update($cid, $obj)
{
$db = Typecho_Db::get();
$postObject = $db->fetchObject($db->select('cid','slug', 'type')
->from('table.contents')->where('cid = ?', $cid));
if (!$postObject->cid){
return;
}
// 生成path
$value = [];
$value['cid'] = $cid;
$value['type'] = $postObject->type;
$value['slug'] = urlencode($postObject->slug);
$pathInfo = Typecho_Router::url($value['type'], $value);
self::initEnv();
self::initBackend(self::$plugin_config->cache_driver);
self::delCache($pathInfo);
if ($cid){
self::delCache(self::getPostMarkCacheName($cid));
}
}
public static function comment_update($comment)
{
self::initEnv();
if (!self::preCheck(false)) return;
if (!self::initBackend(self::$plugin_config->cache_driver))
return;
// 获取评论的PATH_INFO
$path_info = self::$request->getInstance()->getPathInfo();
// 删除最后的 /comment就是需删除的path
$article_url = preg_replace('/\/comment$/i','',$path_info);
self::delCache($article_url);
// 同时,删除 markdown 的部分缓存
if ($comment->cid)
self::delCache(self::getPostMarkCacheName($comment->cid));
}
public static function comment_update2($comment, $edit)
{
self::initEnv();
self::preCheck(false);
self::initBackend(self::$plugin_config->cache_driver);
$perm = stripslashes($edit->parentContent->permalink);
$perm = preg_replace('/(https?):\/\//', '', $perm);
$perm = preg_replace('/'.$_SERVER['HTTP_HOST'].'/', '', $perm);
self::delCache($perm);
// 同时,删除 markdown 的部分缓存
if ($edit->cid)
self::delCache(self::getPostMarkCacheName($edit->cid));
if ($comment['cid'])
self::delCache(self::getPostMarkCacheName($comment['cid']));
}
// 缓存content
public static function getPostMarkCacheName($cid){
if (!self::$path)
self::$path = self::$request->getInstance()->getPathInfo();
return self::$path.'_'.$cid.'_markdown';
}
public static function cache_contentEx($content, $obj, $lastResult){
$content = empty( $lastResult ) ? $content : $lastResult;
if (self::$plugin_config->enable_markcache == '0'){
return $content;
}
// 文章页面设置了标记的就不缓存
if (substr_count($content,'<!--no-cache-->'))
return $content;
// 为 content 设置特殊的 cache name
self::$path = self::$request->getInstance()->getPathInfo();
$cacheName = self::getPostMarkCacheName($obj->cid);
self::initEnv();
if (!self::preCheck(false)) {
return $content;
}
if (!self::initBackend(self::$plugin_config->cache_driver)){
return $content;
}
// 获取评论的PATH_INFO
// $requestUrl = self::$request->getRequestUri();
try {
// 获取当前url的缓存
$data = self::getCache($cacheName);
if (!empty($data)) {
//缓存未过期, 跳过之后的缓存重写入
if ($data['time'] + self::$plugin_config->expire < time())
self::$passed = false;
return $data['html'];
}
} catch (Exception $e) {
// echo $e->getMessage();
return $content;
}
// 没有缓存就缓存起来
if (is_null(self::$path) || !self::$passed)
return $content;
$data = array();
$data['time'] = time();
$data['html'] = $content;
self::setCache($data,$cacheName);
return $content;
}
public static function forTpCacheToolbar(){
?>
<script>
$(document).ready(function(){
$('#wmd-button-row').append('<li class="wmd-button" id="wmd-TePass-button" title="插入no-cache"><span style="background: none;font-size: 16px;border: 1px solid #dedede;padding: 2px;color: red;width: auto;height: auto;">NOCACHE</span></li>');
if($('#wmd-button-row').length !== 0){
$('#wmd-TePass-button').click(function(){
var rs = "\r\n<!--no-cache-->\r\n";
var myField = $('#text')[0];
insertAtCursor(myField,rs);
})
}
function insertAtCursor(myField, myValue) {
//IE 浏览器
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
sel.select();
}
//FireFox、Chrome等
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
// 保存滚动条
var restoreTop = myField.scrollTop;
myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
if (restoreTop > 0) {myField.scrollTop = restoreTop;}
myField.selectionStart = startPos + myValue.length;
myField.selectionEnd = startPos + myValue.length;
myField.focus();
} else {
myField.value += myValue;
myField.focus();
}
}
});
</script>
<?php
}
/* 确保key值唯一 */
public static function TpCache_setCache($cacheKey,$val){
self::initEnv();
if (!self::preCheck(false)) {
return false;
}
if (!self::initBackend(self::$plugin_config->cache_driver)){
return false;
}
$data = array();
$data['time'] = time();
$data['html'] = $val;
self::setCache($data,$cacheKey);
return true;
}
// 插件接口,获取唯一cache值对应val
public static function TpCache_getCache($cacheKey){
self::initEnv();
if (!self::preCheck(false)) {
return false;
}
if (!self::initBackend(self::$plugin_config->cache_driver)){
return false;
}
try{
// 获取当前缓存
$data = self::getCache($cacheKey);
if (!empty($data)) {
//缓存未过期, 跳过之后的缓存重写入
if ($data['time'] + self::$plugin_config->expire < time())
self::$passed = false;
return $data['html'];
}
} catch (Exception $e) {
echo $e->getMessage();
return false;
}
return false;
}
}