-
Notifications
You must be signed in to change notification settings - Fork 589
/
Plugin.php
120 lines (109 loc) · 3.58 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
<?php
/**
* 在显示404之前使用规则重写
*
* @package RewriteRule
* @author laobubu
* @version 1.0.0
* @link http://laobubu.net
*/
class RewriteRule_Plugin implements Typecho_Plugin_Interface
{
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
Typecho_Plugin::factory('Widget_Archive')->error404Handle = array('RewriteRule_Plugin', 'error404Handle');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @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){
$introduce = new Typecho_Widget_Helper_Layout("div");
$introduce->html(<<<EOI
<p>在 Typecho 正式显示 404 之前,此插件会遍历列表并作出跳转。</p>
<p>规则列表一行一个,格式为: Pattern Rewrite结果 [flags]</p>
<p>比如【 <code class="success">^\/(\d+)\/?$ /archives/$1 T</code> 】可以把 /12 以302重定向跳转到 /archivers/12 </p>
<p><a href="https://github.com/laobubu/Typecho_rewriterule">详细说明看这里</a></p>
EOI
);
$rules = new Typecho_Widget_Helper_Form_Element_Textarea('rules', NULL, '不符合书写规范的行可以当注释哦', _t('规则列表'));
$form->addInput($rules);
$form->addItem($introduce);
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
/**
* 插件实现方法
*
* @access public
* @return void
*/
public static function error404Handle($that,$select) {
$rules = Typecho_Widget::widget('Widget_Options')->plugin('RewriteRule')->rules;
$rules = str_replace("\r","",$rules);
$rules = explode("\n",$rules);
$uri = $_SERVER['REQUEST_URI'];
$func_decorate_key = function($value) {return '$'.$value;};
$matcher_list = array('cid','slug','category');
foreach($rules as $i) {
$i = preg_replace("/\s+/","\t",trim($i));
$l = explode("\t",$i,3);
if (count($l)>=2) {
$flags = $l[2]?$l[2]:"";
$pattern = "/".$l[0]."/";
if (stristr($flags,'i')) $pattern.="i";
if (!preg_match($pattern,$uri,$match_out))
continue;
if (stristr($flags,'c')) {
$count_of_matcher = 0;
foreach ($matcher_list as $mli) {
if (isset($match_out[$mli])) {
$select = $select->where('typecho_contents.`'.$mli.'` = ?', $match_out[$mli]);
$count_of_matcher++;
}
}
if ($count_of_matcher==0) continue;
$that->slug = NULL;
$that->query($select);
$match_out['cid'] = $that->cid;
$match_out['slug'] = $that->slug;
$match_out['category'] = $that->category;
if (!$that->slug) continue;
}
$r1 = array_map($func_decorate_key,array_keys($match_out));
$r2 = array_values($match_out);
$newuri = str_replace($r1,$r2,$l[1]);
header(stristr($flags,'t') ? "HTTP/1.1 302 Found" : "HTTP/1.1 301 Moved Permanently");
header("Location: ".$newuri);
echo '<meta http-equiv="refresh" content="0; url='.$newuri.'" />';
echo "<a href=\"{$newuri}\">Click to continue</a>";
exit;
}
}
}
}