-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPlugin.php
107 lines (94 loc) · 3.27 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
<?php
/**
* 禁止讨厌鬼访问网站,眼不见心不烦~
*
* @package BlockIP
* @author Kokororin
* @version 1.0
* @update: 2015.9.10
* @link https://kotori.love
*/
class BlockIP_Plugin implements Typecho_Plugin_Interface
{
public static function activate()
{
Typecho_Plugin::factory('Widget_Archive')->beforeRender = array('BlockIP_Plugin', 'BlockIP');
return "启用BlockIP成功qwq";
}
public static function deactivate()
{
return "禁用BlockIP成功qwq";
}
public static function config(Typecho_Widget_Helper_Form $form)
{
$ips = new Typecho_Widget_Helper_Form_Element_Textarea('ips', null, null, _t('IP黑名单列表'), _t('一行一个,支持规则qwq<br>以下是例子qwq<br>192.168.1.1<br>210.10.2.1-20<br>222.34.4.*<br>218.192.104.*'));
$form->addInput($ips);
}
public static function personalConfig(Typecho_Widget_Helper_Form $form)
{}
public static function blockIP()
{
//debug
//print_r(BlockIP_Plugin::getAllBlockIP());
if (BlockIP_Plugin::checkIP()) {
$user = Typecho_Widget::widget('Widget_User');
throw new Typecho_Widget_Exception('抱歉,您的IP段无法访问,如有问题,请<a href="mailto:' . $user->mail . '">联系我</a>。');
}
}
private static function checkIP()
{
$flag = false;
$request = new Typecho_Request;
$ip = trim($request->getIp());
$iptable = BlockIP_Plugin::getAllBlockIP();
if ($iptable) {
foreach ($iptable as $value) {
if (preg_match("{$value}", $ip)) {
$flag = true;
break;
}
}
}
return $flag;
}
private static function makePregIP($str)
{
if (strpos($str, "-") !== false) {
$aIP = explode(".", $str);
foreach ($aIP as $key => $value) {
if (strpos($value, "-") === false) {
if ($key == 0) {
$preg_limit .= BlockIP_Plugin::makePregIP($value);
} else {
$preg_limit .= '.' . BlockIP_Plugin::makePregIP($value);
}
} else {
$aipNum = explode("-", $value);
for ($i = $aipNum[0]; $i <= $aipNum[1]; $i++) {
$preg .= $preg ? "|" . $i : "[" . $i;
}
$preg_limit .= strrpos($preg_limit, ".", 1) == (strlen($preg_limit) - 1) ? $preg . "]" : "." . $preg . "]";
}
}
} else {
$preg_limit .= $str;
}
return $preg_limit;
}
private static function getAllBlockIP()
{
$config = Typecho_Widget::widget('Widget_Options')->plugin('BlockIP');
$ips = $config->ips;
if ($ips) {
$ip_array = explode("\n", $ips);
foreach ($ip_array as $value) {
$ipaddress = BlockIP_Plugin::makePregIP($value);
$ip = str_ireplace(".", "\.", $ipaddress);
$ip = str_replace("*", "[0-9]{1,3}", $ip);
$ipaddress = "/" . trim($ip) . "/";
$ip_list[] = $ipaddress;
}
}
return $ip_list;
}
}