OSDN Git Service

f14058c63aad928c7017870b48bc97f8f7554837
[pukiwiki/pukiwiki_sandbox.git] / spam / spam.php
1 <?php
2 // $Id: spam.php,v 1.4 2006/11/01 14:39:12 henoheno Exp $
3 // Copyright (C) 2006 PukiWiki Developers Team
4 // License: GPL v2 or (at your option) any later version
5
6 // Functions for Concept-work of spam-uri metrics
7
8 // Return an array of normalized/parsed URIs in the $string
9 // [OK] http://nasty.example.org#nasty_string
10 // [OK] ftp://dfshodfs:80/dfsdfs
11 function spam_pickup($string = '')
12 {
13         // Not available for user@password, IDN
14         $array = array();
15         preg_match_all(
16                 // Refer RFC3986
17                 '#(\b[a-z][a-z0-9.+-]{1,8})://' .       // 1: Scheme
18                 '(' .
19                         // 2: Host
20                         '\[[0-9a-f:.]+\]' . '|' .                               // IPv6([colon-hex and dot]): RFC2732
21                         '(?:[0-9]{1-3}\.){3}[0-9]{1-3}' . '|' . // IPv4(dot-decimal): 001.22.3.44
22                         '[^\s<>"\'\[\]:/\#?]+' .                                // FQDN: foo.example.org
23                 ')' .
24                 '(?::([a-z0-9]*))?' .                   // 3: Port
25                 '((?:/+[^\s<>"\'\[\]/]+)*/+)?' .// 4: Directory path or path-info
26                 '([^\s<>"\'\[\]]+)?' .                  // 5: Rest of all (File and query string)
27                 '#i',
28                  $string, $array, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
29         //var_dump(recursive_map('htmlspecialchars', $array));
30         // Shrink $array
31         $parts = array(
32                 1 => 'scheme',
33                 2 => 'host',
34                 3 => 'port',
35                 4 => 'path',
36                 5 => 'rest',
37                 );
38         foreach(array_keys($array) as $uri) {
39                 unset($array[$uri][0]); // Matched string itself
40                 array_rename_keys($array[$uri], $parts, TRUE, array('', 0));
41                 $offset = $array[$uri]['scheme'][1]; // Scheme's offset
42
43                 // Remove offsets (with normalization)
44                 foreach(array_keys($array[$uri]) as $part) {
45                         $array[$uri][$part] =
46                                         strtolower(urldecode($array[$uri][$part][0]));
47                 }
48
49                 $array[$uri]['path'] = path_normalize($array[$uri]['path']);
50                 $array[$uri]['offset'] = $offset;
51                 $array[$uri]['area']  = 0;
52         }
53         //var_dump(recursive_map('htmlspecialchars', $array));
54
55         // Area elevation for '(especially external)link' intension
56         if (! empty($array)) {
57                 // Anchor tags by preg_match_all()
58                 // [OK] <a href="http://nasty.example.com">visit http://nasty.example.com/</a>
59                 // [NG] <a href="http://ng.example.com">visit http://ng.example.com _not_ended_
60                 // [NG] <a href=  >Good site!</a> <a href= "#" >test</a>
61                 $areas = array();
62                 preg_match_all('#<a\b[^>]*href[^>]*>.*?</a\b[^>]*(>)#i',
63                          $string, $areas, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
64                 //var_dump(recursive_map('htmlspecialchars', $areas));
65                 foreach(array_keys($areas) as $area) {
66                         $areas[$area] =  array(
67                                 $areas[$area][0][1], // [0][1] = Area start (<a href>)
68                                 $areas[$area][1][1], // [1][1] = Area end   (</a>)
69                         );
70                 }
71                 area_measure($areas, $array);
72
73                 // Various Wiki syntax
74                 // [text_or_uri>text_or_uri]
75                 // [text_or_uri:text_or_uri]
76                 // [text_or_uri|text_or_uri]
77                 // [text_or_uri->text_or_uri]
78                 // [text_or_uri text_or_uri] // MediaWiki
79                 // MediaWiki: [http://nasty.example.com/ visit http://nasty.example.com/]
80
81                 // phpBB's "BBCode" by preg_match_all()
82                 // [url]http://nasty.example.com/[/url]
83                 // [link]http://nasty.example.com/[/link]
84                 // [url=http://nasty.example.com]visit http://nasty.example.com/[/url]
85                 // [link http://nasty.example.com/]buy something[/link]
86                 // ?? [url=][/url]
87                 $areas = array();
88                 preg_match_all('#\[(url|link)\b[^\]]*\].*?\[/\1\b[^\]]*(\])#i',
89                          $string, $areas, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
90                 //var_dump(recursive_map('htmlspecialchars', $areas));
91                 foreach(array_keys($areas) as $area) {
92                         $areas[$area] = array(
93                                 $areas[$area][0][1], // [0][1] = Area start ([url])
94                                 $areas[$area][2][1], // [4][1] = Area end   ([/url])
95                         );
96                 }
97                 area_measure($areas, $array);
98
99                 // Remove 'offset's for area_measure()
100                 foreach(array_keys($array) as $key)
101                         unset($array[$key]['offset']);
102         }
103
104         return $array;
105 }
106
107 // $array[0] => $array['name']
108 function array_rename_key(& $array, $from, $to, $force = FALSE, $default = '')
109 {
110         if (isset($array[$from])) {
111                 $array[$to] = & $array[$from];
112                 unset($array[$from]);
113         } else if ($force) {
114                 $array[$to] = $default;
115         } else {
116                 return FALSE;
117         }
118         return TRUE;
119 }
120
121 function array_rename_keys(& $array, $rename = array(), $force = FALSE, $default = '')
122 {
123     if ($force) {
124                 foreach($rename as $from => $to) {
125                         if (isset($array[$from])) {
126                                 $array[$to] = & $array[$from];
127                                 unset($array[$from]);
128                         } else  {
129                                 $array[$to] = $default;
130                         }
131                 }
132         } else {
133                 foreach(array_keys($rename) as $from) {
134                         if (! isset($array[$from])) {
135                                 return FALSE;
136                         }
137                 }
138                 foreach($rename as $from => $to) {
139                         $array[$to] = & $array[$from];
140                         unset($array[$from]);
141                 }
142         }
143         return TRUE;
144 }
145
146
147 // Path normalization
148 // example.org => example.org/
149 // example.org#hoge -> example.org/#hoge
150 // example.org/path/a/b/./c////./d -> example.org/path/a/b/c/d
151 // example.org/path/../../a/../back
152 function path_normalize($path = '', $divider = '/', $addroot = TRUE)
153 {
154         if (! is_string($path) || $path == '') {
155                 $path = $addroot ? $divider : '';
156         } else {
157                 $path = trim($path);
158                 $last = ($path[strlen($path) - 1] == $divider) ? $divider : '';
159                 $array = explode($divider, $path);
160
161                 // Remove paddings
162                 foreach(array_keys($array) as $key) {
163                         if ($array[$key] == '' || $array[$key] == '.')
164                                  unset($array[$key]);
165                 }
166                 // Back-track
167                 $tmp = array();
168                 foreach($array as $value) {
169                         if ($value == '..') {
170                                 array_pop($tmp);
171                         } else {
172                                 array_push($tmp, $value);
173                         }
174                 }
175                 $array = & $tmp;
176
177                 $path = $addroot ? $divider : '';
178                 if (! empty($array)) $path .= implode($divider, $array) . $last;
179         }
180
181         return $path;
182 }
183
184 // If in doubt, it's a little doubtful
185 function area_measure($areas, &$array, $belief = -1, $a_key = 'area', $o_key = 'offset')
186 {
187         if (! is_array($areas) || ! is_array($array)) return;
188
189         $areas_keys = array_keys($areas);
190         foreach(array_keys($array) as $u_index) {
191                 $offset = isset($array[$u_index][$o_key]) ? intval($array[$u_index][$o_key]) : 0;
192                 foreach($areas_keys as $a_index) {
193                         if (isset($array[$u_index][$a_key])) {
194                                 $offset_s = intval($areas[$a_index][0]);
195                                 $offset_e = intval($areas[$a_index][1]);
196                                 if ($offset_s < $offset && $offset < $offset_e) {
197                                         $array[$u_index][$a_key] += $belief;
198                                 }
199                         }
200                 }
201         }
202 }
203
204 // Simple spam filter (for one text field)
205 function pkwk_spamfilter($action, $page, $target = '')
206 {
207         $is_spam = false;
208         $pickups = spam_pickup($target);
209         if (! empty($pickups)) {
210                 foreach($pickups as $pickup) {
211                         if ($pickup['area'] < 0) {
212                                 $is_spam = TRUE;
213                                 break;
214                         }
215                 }
216         }
217         if ($is_spam) {
218                 global $notify, $notify_subject;
219                 if ($notify) {
220                         $footer['ACTION'] = $action;
221                         $footer['PAGE']   = & $page;
222                         $footer['URI']    = get_script_uri() . '?' . rawurlencode($page);
223                         $footer['USER_AGENT']  = TRUE;
224                         $footer['REMOTE_ADDR'] = TRUE;
225                         pkwk_mail_notify($notify_subject . ' [blocked]', $target, $footer);
226                 }
227                 die("\n");
228         }
229 }
230
231 ?>