2 // $Id: spam.php,v 1.16 2006/11/12 11:21:57 henoheno Exp $
3 // Copyright (C) 2006 PukiWiki Developers Team
4 // License: GPL v2 or (at your option) any later version
6 // Functions for Concept-work of spam-uri metrics
8 // Return an array of URIs in the $string
9 // [OK] http://nasty.example.org#nasty_string
10 // [OK] http://nasty.example.org/foo/xxx#nasty_string/bar
11 // [OK] ftp://dfshodfs:80/dfsdfs
12 function uri_pickup($string = '', $normalize = TRUE)
14 // Not available for: user@password, IDN, Fragment(=ignored)
18 '#(\b[a-z][a-z0-9.+-]{1,8})://' . // 1: Scheme
21 '\[[0-9a-f:.]+\]' . '|' . // IPv6([colon-hex and dot]): RFC2732
22 '(?:[0-9]{1-3}\.){3}[0-9]{1-3}' . '|' . // IPv4(dot-decimal): 001.22.3.44
23 '[^\s<>"\'\[\]:/\#?]+' . // FQDN: foo.example.org
25 '(?::([a-z0-9]{2,}))?' . // 3: Port
26 '((?:/+[^\s<>"\'\[\]/\#]+)*/+)?' . // 4: Directory path or path-info
27 '([^\s<>"\'\[\]\#]+)?' . // 5: File and query string
28 // #: Fragment(ignored)
30 $string, $array, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
31 //var_dump(recursive_map('htmlspecialchars', $array));
34 $parts = array(1 => 'scheme', 2 => 'host', 3 => 'port',
35 4 => 'path', 5 => 'file');
37 foreach(array_keys($array) as $uri) {
38 unset($array[$uri][0]); // Matched string itself
39 array_rename_keys($array[$uri], $parts, TRUE, $default);
40 $offset = $array[$uri]['scheme'][1]; // Scheme's offset
42 // Remove offsets for each part
44 foreach(array_keys($array[$uri]) as $part) {
45 $array[$uri][$part] = strtolower($array[$uri][$part][0]);
47 $array[$uri]['path'] = path_normalize($array[$uri]['path']);
49 foreach(array_keys($array[$uri]) as $part) {
50 $array[$uri][$part] = & $array[$uri][$part][0];
53 $array[$uri]['offset'] = $offset;
54 $array[$uri]['area'] = 0;
60 // Preprocess: rawurldecode() and adding space(s) to detect/count some URIs if possible
61 // NOTE: It's maybe danger to var_dump() these results.
62 // [OK] http://victim.example.org/go?http%3A%2F%2Fnasty.example.org
63 // [OK] http://victim.example.org/http://nasty.example.org
64 function spam_uri_pickup_preprocess($string = '')
66 if (is_string($string)) {
70 '#\b[a-z][a-z0-9.+-]{1,8}://#i',
71 '#[a-z][a-z0-9.+-]{1,8}://#i'
81 // Main function of spam-uri pickup
82 function spam_uri_pickup($string = '')
84 $string = spam_uri_pickup_preprocess($string);
86 $array = uri_pickup($string);
88 // Area elevation for '(especially external)link' intension
89 if (! empty($array)) {
90 // Anchor tags by preg_match_all()
91 // [OK] <a href="http://nasty.example.com">visit http://nasty.example.com/</a>
92 // [OK] <a href=\'http://nasty.example.com/\' >discount foobar</a>
93 // [NG] <a href="http://ng.example.com">visit http://ng.example.com _not_ended_
94 // [NG] <a href= >Good site!</a> <a href= "#" >test</a>
96 preg_match_all('#<a\b[^>]*href[^>]*>.*?</a\b[^>]*(>)#i',
97 $string, $areas, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
98 //var_dump(recursive_map('htmlspecialchars', $areas));
99 foreach(array_keys($areas) as $area) {
100 $areas[$area] = array(
101 $areas[$area][0][1], // [0][1] = Area start (<a href>)
102 $areas[$area][1][1], // [1][1] = Area end (</a>)
105 area_measure($areas, $array);
107 // phpBB's "BBCode" by preg_match_all()
108 // [url]http://nasty.example.com/[/url]
109 // [link]http://nasty.example.com/[/link]
110 // [url=http://nasty.example.com]visit http://nasty.example.com/[/url]
111 // [link http://nasty.example.com/]buy something[/link]
114 preg_match_all('#\[(url|link)\b[^\]]*\].*?\[/\1\b[^\]]*(\])#i',
115 $string, $areas, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
116 //var_dump(recursive_map('htmlspecialchars', $areas));
117 foreach(array_keys($areas) as $area) {
118 $areas[$area] = array(
119 $areas[$area][0][1], // [0][1] = Area start ([url])
120 $areas[$area][2][1], // [4][1] = Area end ([/url])
123 area_measure($areas, $array);
125 // Various Wiki syntax
126 // [text_or_uri>text_or_uri]
127 // [text_or_uri:text_or_uri]
128 // [text_or_uri|text_or_uri]
129 // [text_or_uri->text_or_uri]
130 // [text_or_uri text_or_uri] // MediaWiki
131 // MediaWiki: [http://nasty.example.com/ visit http://nasty.example.com/]
133 // Remove 'offset's for area_measure()
134 //foreach(array_keys($array) as $key)
135 // unset($array[$key]['offset']);
141 // $array['something'] => $array['wanted']
142 function array_rename_keys(& $array, $keys = array('from' => 'to'), $force = FALSE, $default = '')
144 if (! is_array($array) || ! is_array($keys))
147 // Nondestructive test
149 foreach(array_keys($keys) as $from)
150 if (! isset($array[$from]))
153 foreach($keys as $from => $to) {
154 if ($from === $to) continue;
155 if (! $force || isset($array[$from])) {
156 $array[$to] = & $array[$from];
157 unset($array[$from]);
159 $array[$to] = $default;
166 // If in doubt, it's a little doubtful
167 function area_measure($areas, & $array, $belief = -1, $a_key = 'area', $o_key = 'offset')
169 if (! is_array($areas) || ! is_array($array)) return;
171 $areas_keys = array_keys($areas);
172 foreach(array_keys($array) as $u_index) {
173 $offset = isset($array[$u_index][$o_key]) ?
174 intval($array[$u_index][$o_key]) : 0;
175 foreach($areas_keys as $a_index) {
176 if (isset($array[$u_index][$a_key])) {
177 $offset_s = intval($areas[$a_index][0]);
178 $offset_e = intval($areas[$a_index][1]);
179 // [Area => inside <= Area]
180 if ($offset_s < $offset && $offset < $offset_e) {
181 $array[$u_index][$a_key] += $belief;
189 // ---------------------
192 // Path normalization
193 // example.org => example.org/
194 // example.org#hoge -> example.org/#hoge
195 // example.org/path/a/b/./c////./d -> example.org/path/a/b/c/d
196 // example.org/path/../../a/../back
197 function path_normalize($path = '', $divider = '/', $addroot = TRUE)
199 if (! is_string($path) || $path == '') {
200 $path = $addroot ? $divider : '';
203 $last = ($path[strlen($path) - 1] == $divider) ? $divider : '';
204 $array = explode($divider, $path);
207 foreach(array_keys($array) as $key) {
208 if ($array[$key] == '' || $array[$key] == '.')
213 foreach($array as $value) {
214 if ($value == '..') {
217 array_push($tmp, $value);
222 $path = $addroot ? $divider : '';
223 if (! empty($array)) $path .= implode($divider, $array) . $last;
230 // Output: array('' => array('a' => array('b' => NULL)))
231 function array_tree($string, $delimiter = '/', $reverse = FALSE)
235 $tmps = explode($delimiter, $string);
236 if (! $reverse) $tmps = array_reverse($tmps);
237 foreach ($tmps as $tmp) {
238 $tree = array($tmp => $tree);
244 // ---------------------
245 // Part One : Checker
247 // Simple/fast spam check
248 function is_uri_spam($target = '')
253 if (is_array($target)) {
254 foreach($target as $str) {
256 list($is_spam, $_urinum) = is_uri_spam($str);
261 $pickups = spam_uri_pickup($target);
262 $urinum += count($pickups);
263 if (! empty($pickups)) {
264 // Some users want to post some URLs, but ...
266 $is_spam = TRUE; // Too many!
268 foreach($pickups as $pickup) {
269 if ($pickup['area'] < 0) {
278 return array($is_spam, $urinum);
281 // ---------------------
284 function is_invalid_useragent($ua_name = '' /*, $ua_vars = ''*/ )
286 return $ua_name === '';
289 // ---------------------
291 // Mail to administrator with more measurement data?
292 // Simple/fast spam filter (for one text field)
293 function pkwk_spamfilter($action, $page, $target = array('title' => ''))
297 //$is_spam = is_invalid_useragent('NOTYET');
299 $action .= ' (Invalid User-Agent)';
301 list($is_spam) = is_uri_spam($target);
305 // Mail to administrator(s)
306 global $notify, $notify_subject;
308 $footer['ACTION'] = $action;
309 $footer['PAGE'] = '[blocked] ' . $page;
310 $footer['URI'] = get_script_uri() . '?' . rawurlencode($page);
311 $footer['USER_AGENT'] = TRUE;
312 $footer['REMOTE_ADDR'] = TRUE;
313 pkwk_mail_notify($notify_subject, var_export($target, TRUE), $footer);
318 if ($is_spam) spam_exit();
321 // ---------------------
323 // Common bahavior for blocking
324 // NOTE: Call this function from various blocking feature, to disgueise the reason 'why blocked'