-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugin.php
324 lines (280 loc) · 9.43 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
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* Typecho缓存插件
*
* @package TpCache
* @author 老高
* @version 0.7
* @link http://www.phpgao.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_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_update3');
Typecho_Plugin::factory('Widget_Comments_Edit')->finishComment = array(__CLASS__, 'comment_update2');
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)
{
$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'), '需要缓存的页面');
$form->addInput($element);
$list = array('关闭', '开启');
$element = new Typecho_Widget_Helper_Form_Element_Radio('login', $list, 1, '是否对已登录用户失效', '已经录用户不会触发缓存策略');
$form->addInput($element);
$list = array('关闭', '开启');
$element = new Typecho_Widget_Helper_Form_Element_Radio('enable_ssl', $list, '0', '是否支持SSL');
$form->addInput($element);
$list = array(
'0' => '不使用缓存',
'memcached' => 'Memcached',
'redis' => 'Redis'
);
$element = new Typecho_Widget_Helper_Form_Element_Radio('cache_driver', $list, '0', '缓存驱动');
$form->addInput($element);
$element = new Typecho_Widget_Helper_Form_Element_Text('expire', null, '86400', '缓存过期时间', '86400 = 60s * 60m *24h,即一天的秒数');
$form->addInput($element);
$element = new Typecho_Widget_Helper_Form_Element_Text('host', null, '127.0.0.1', '主机地址', '主机地址,一般为127.0.0.1');
$form->addInput($element);
$element = new Typecho_Widget_Helper_Form_Element_Text('port', null, '11211', '端口号', 'memcache(d)默认为11211,redis默认为6379,其他类型随意填写');
$form->addInput($element);
//$list = array('关闭', '开启');
//$element = new Typecho_Widget_Helper_Form_Element_Radio('is_debug', $list, 0, '是否开启debug');
//$form->addInput($element);
$list = array('关闭', '清除所有数据');
$element = new Typecho_Widget_Helper_Form_Element_Radio('is_clean', $list, 0, '清除所有数据');
$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';
}
}
Helper::configPlugin('TpCache', $config);
}
/**
* 尝试使用缓存
*/
public static function C()
{
self::initEnv();
if (!self::preCheck()) return;
if (!self::initPath()) return;
try {
// 获取当前url的缓存
$data = self::getCache();
if (!empty($data)) {
//缓存未过期, 跳过之后的缓存重写入
if ($data['time'] + self::$plugin_config->expire < time())
self::$passed = false;
echo $data['html'];
die;
}
} catch (Exception $e) {
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()
{
return unserialize(self::$cache->get(self::$path));
}
public static function setCache($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->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)
{
//后台数据不缓存
$pattern = '#^' . __TYPECHO_ADMIN_DIR__ . '#i';
if (preg_match($pattern, $path)) return false;
//action动作不缓存
$pattern = '#^/action#i';
if (preg_match($pattern, $path)) return false;
$_routingTable = self::$sys_config->routingTable;
foreach ($_routingTable[0] as $page => $route) {
if ($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::preCheck(false)) return;
$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($contents['slug']);
$contents['date'] = new Typecho_Date($contents['created']);
$contents['year'] = $contents['date']->year;
$contents['month'] = $contents['date']->month;
$contents['day'] = $contents['date']->day;
self::initPath(Typecho_Router::url($type, $contents));
self::delCache(self::$path);
}
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->getPathInfo();
// 删除最后的 /comment就是需删除的path
$article_url = preg_replace('/\/comment$/i','',$path_info);
self::delCache($article_url);
}
public static function comment_update2($comment = null, $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);
}
}