2 // $Id: spam.php,v 1.2 2006/10/28 14:09:51 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 function spam_pickup($string = '')
12 // Not available for user@password, IDN
16 '#(\b[a-z][a-z0-9.+-]{1,8})://' . // 1: Scheme
19 '\[[0-9a-f:.]+\]' . '|' . // IPv6([colon-hex and dot]): RFC2732
20 '(?:[0-9]{1-3}\.){3}[0-9]{1-3}' . '|' . // IPv4(dot-decimal): 001.22.3.44
21 '[^\s<>"\'\[\]:/\#?]+' . // FQDN: foo.example.org
23 '(?::([a-z0-9]*))?' . // 3: Port
24 '((?:/+[^\s<>"\'\[\]/]+)*/+)?' .// 4: Directory path or path-info
25 '([^\s<>"\'\[\]]+)?' . // 5: File and query string
27 $string, $array, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
28 // Tests: ftp://dfshodfs:80/dfsdfs
29 //var_dump(recursive_map('htmlspecialchars', $array));
33 foreach(array_keys($array) as $uri) {
34 unset($array[$uri][0]); // Matched string itself
35 $offset = $array[$uri][1][1]; // [1][1] = scheme's offset
36 foreach(array_keys($array[$uri]) as $part) {
37 // Remove offsets (with normalization)
39 strtolower(urldecode($array[$uri][$part][0]));
42 if (! isset($array[$uri][$_port])) $array[$uri][$_port] = '';
43 if (! isset($array[$uri][$_path])) $array[$uri][$_path] = '';
44 $array[$uri][$_path] = path_normalize($array[$uri][$_path]);
45 $array[$uri]['offset'] = $offset;
46 $array[$uri]['area'] = 0;
49 // Area elevation for '(especially external)link' intension
50 if (! empty($array)) {
51 // Anchor tags by preg_match_all()
52 // [OK] <a href="http://nasty.example.com">visit http://nasty.example.com/</a>
53 // [NG] <a href="http://ng.example.com">visit http://ng.example.com _not_ended_
54 // [??] <a href= >Good site!</a> <a href= "#" >test</a>
56 preg_match_all('#<a\b[^>]*href[^>]*>.*?</a\b[^>]*(>)#i',
57 $string, $areas, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
58 //var_dump(recursive_map('htmlspecialchars', $areas));
59 foreach(array_keys($areas) as $area) {
60 $areas[$area] = array(
61 $areas[$area][0][1], // [0][1] = Area start (<a href>)
62 $areas[$area][1][1], // [1][1] = Area end (</a>)
65 area_measure($areas, $array);
67 // Various Wiki syntax
68 // [text_or_uri>text_or_uri]
69 // [text_or_uri:text_or_uri]
70 // [text_or_uri|text_or_uri]
71 // [text_or_uri->text_or_uri]
72 // [text_or_uri text_or_uri] // MediaWiki
73 // MediaWiki: [http://nasty.example.com/ visit http://nasty.example.com/]
75 // phpBB's "BBCode" by preg_match_all()
76 // [url]http://nasty.example.com/[/url]
77 // [link]http://nasty.example.com/[/link]
78 // [url=http://nasty.example.com]visit http://nasty.example.com/[/url]
79 // [link http://nasty.example.com/]buy something[/link]
82 preg_match_all('#\[(url|link)\b[^\]]*\].*?\[/\1\b[^\]]*(\])#i',
83 $string, $areas, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
84 //var_dump(recursive_map('htmlspecialchars', $areas));
85 foreach(array_keys($areas) as $area) {
86 $areas[$area] = array(
87 $areas[$area][0][1], // [0][1] = Area start ([url])
88 $areas[$area][2][1], // [4][1] = Area end ([/url])
91 area_measure($areas, $array);
93 // Remove 'offset's for area_measure()
94 foreach(array_keys($array) as $key)
95 unset($array[$key]['offset']);
101 // Path normalization
102 // example.org => example.org/
103 // example.org#hoge -> example.org/#hoge
104 // example.org/path/a/b/./c////./d -> example.org/path/a/b/c/d
105 // example.org/path/../../a/../back
106 function path_normalize($path = '')
108 if (! is_string($path) || $path == '') {
112 $last = ($path[strlen($path) - 1] == '/') ? '/' : '';
113 $array = explode('/', $path);
116 foreach(array_keys($array) as $key) {
117 if ($array[$key] == '' || $array[$key] == '.')
122 foreach($array as $value) {
123 if ($value == '..') {
126 array_push($tmp, $value);
134 $path = '/' . implode('/', $array) . $last;
140 // If in doubt, it's a little doubtful
141 function area_measure($areas, &$array, $belief = -1, $a_key = 'area', $o_key = 'offset')
143 if (! is_array($areas) || ! is_array($array)) return;
145 $areas_keys = array_keys($areas);
146 foreach(array_keys($array) as $u_index) {
147 $offset = isset($array[$u_index][$o_key]) ? intval($array[$u_index][$o_key]) : 0;
148 foreach($areas_keys as $a_index) {
149 if (isset($array[$u_index][$a_key])) {
150 $offset_s = intval($areas[$a_index][0]);
151 $offset_e = intval($areas[$a_index][1]);
152 if ($offset_s < $offset && $offset < $offset_e) {
153 $array[$u_index][$a_key] += $belief;