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
6 // Functions for Concept-work of spam-uri metrics
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 = '')
13 // Not available for user@password, IDN
17 '#(\b[a-z][a-z0-9.+-]{1,8})://' . // 1: Scheme
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
24 '(?::([a-z0-9]*))?' . // 3: Port
25 '((?:/+[^\s<>"\'\[\]/]+)*/+)?' .// 4: Directory path or path-info
26 '([^\s<>"\'\[\]]+)?' . // 5: Rest of all (File and query string)
28 $string, $array, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
29 //var_dump(recursive_map('htmlspecialchars', $array));
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
43 // Remove offsets (with normalization)
44 foreach(array_keys($array[$uri]) as $part) {
46 strtolower(urldecode($array[$uri][$part][0]));
49 $array[$uri]['path'] = path_normalize($array[$uri]['path']);
50 $array[$uri]['offset'] = $offset;
51 $array[$uri]['area'] = 0;
53 //var_dump(recursive_map('htmlspecialchars', $array));
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>
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>)
71 area_measure($areas, $array);
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/]
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]
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])
97 area_measure($areas, $array);
99 // Remove 'offset's for area_measure()
100 foreach(array_keys($array) as $key)
101 unset($array[$key]['offset']);
107 // $array[0] => $array['name']
108 function array_rename_key(& $array, $from, $to, $force = FALSE, $default = '')
110 if (isset($array[$from])) {
111 $array[$to] = & $array[$from];
112 unset($array[$from]);
114 $array[$to] = $default;
121 function array_rename_keys(& $array, $rename = array(), $force = FALSE, $default = '')
124 foreach($rename as $from => $to) {
125 if (isset($array[$from])) {
126 $array[$to] = & $array[$from];
127 unset($array[$from]);
129 $array[$to] = $default;
133 foreach(array_keys($rename) as $from) {
134 if (! isset($array[$from])) {
138 foreach($rename as $from => $to) {
139 $array[$to] = & $array[$from];
140 unset($array[$from]);
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)
154 if (! is_string($path) || $path == '') {
155 $path = $addroot ? $divider : '';
158 $last = ($path[strlen($path) - 1] == $divider) ? $divider : '';
159 $array = explode($divider, $path);
162 foreach(array_keys($array) as $key) {
163 if ($array[$key] == '' || $array[$key] == '.')
168 foreach($array as $value) {
169 if ($value == '..') {
172 array_push($tmp, $value);
177 $path = $addroot ? $divider : '';
178 if (! empty($array)) $path .= implode($divider, $array) . $last;
184 // If in doubt, it's a little doubtful
185 function area_measure($areas, &$array, $belief = -1, $a_key = 'area', $o_key = 'offset')
187 if (! is_array($areas) || ! is_array($array)) return;
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;
204 // Simple spam filter (for one text field)
205 function pkwk_spamfilter($action, $page, $target = '')
208 $pickups = spam_pickup($target);
209 if (! empty($pickups)) {
210 foreach($pickups as $pickup) {
211 if ($pickup['area'] < 0) {
218 global $notify, $notify_subject;
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);