OSDN Git Service

Library spam.php
[pukiwiki/pukiwiki_sandbox.git] / spam / spam.php
1 <?php
2 // $Id: spam.php,v 1.1 2006/10/28 14:08:58 henoheno Exp $
3 // Concept-work of spam-uri metrics
4 // Copyright (C) 2006 PukiWiki Developers Team
5 // License: GPL v2 or (at your option) any later version
6
7 // Concept-work of spam-uri metrics
8
9 // Return an array of normalized/parsed URIs in the $string
10 // [OK] http://nasty.example.org#nasty_string
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: File and query string
27                 '#i',
28                  $string, $array, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
29                  // Tests: ftp://dfshodfs:80/dfsdfs
30         //var_dump(recursive_map('htmlspecialchars', $array));
31         // Shrink $array
32         $_port = 3;
33         $_path = 4;
34         foreach(array_keys($array) as $uri) {
35                 unset($array[$uri][0]); // Matched string itself
36                 $offset = $array[$uri][1][1]; // [1][1] = scheme's offset
37                 foreach(array_keys($array[$uri]) as $part) {
38                         // Remove offsets (with normalization)
39                         $array[$uri][$part] =
40                                         strtolower(urldecode($array[$uri][$part][0]));
41                 }
42                 
43                 if (! isset($array[$uri][$_port])) $array[$uri][$_port] = '';
44                 if (! isset($array[$uri][$_path])) $array[$uri][$_path] = '';
45                 $array[$uri][$_path] = path_normalize($array[$uri][$_path]);
46                 $array[$uri]['offset'] = $offset;
47                 $array[$uri]['area']  = 0;
48         }
49
50         // Area elevation for '(especially external)link' intension
51         if (! empty($array)) {
52                 // Anchor tags by preg_match_all()
53                 // [OK] <a href="http://nasty.example.com">visit http://nasty.example.com/</a>
54                 // [NG] <a href="http://ng.example.com">visit http://ng.example.com _not_ended_
55                 // [??] <a href=  >Good site!</a> <a href= "#" >test</a>
56                 $areas = array();
57                 preg_match_all('#<a\b[^>]*href[^>]*>.*?</a\b[^>]*(>)#i',
58                          $string, $areas, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
59                 //var_dump(recursive_map('htmlspecialchars', $areas));
60                 foreach(array_keys($areas) as $area) {
61                         $areas[$area] =  array(
62                                 $areas[$area][0][1], // [0][1] = Area start (<a href>)
63                                 $areas[$area][1][1], // [1][1] = Area end   (</a>)
64                         );
65                 }
66                 area_measure($areas, $array);
67
68                 // Various Wiki syntax
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]
73                 // [text_or_uri text_or_uri] // MediaWiki
74                 // MediaWiki: [http://nasty.example.com/ visit http://nasty.example.com/]
75
76                 // phpBB's "BBCode" by preg_match_all()
77                 // [url]http://nasty.example.com/[/url]
78                 // [link]http://nasty.example.com/[/link]
79                 // [url=http://nasty.example.com]visit http://nasty.example.com/[/url]
80                 // [link http://nasty.example.com/]buy something[/link]
81                 // ?? [url=][/url]
82                 $areas = array();
83                 preg_match_all('#\[(url|link)\b[^\]]*\].*?\[/\1\b[^\]]*(\])#i',
84                          $string, $areas, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
85                 //var_dump(recursive_map('htmlspecialchars', $areas));
86                 foreach(array_keys($areas) as $area) {
87                         $areas[$area] = array(
88                                 $areas[$area][0][1], // [0][1] = Area start ([url])
89                                 $areas[$area][2][1], // [4][1] = Area end   ([/url])
90                         );
91                 }
92                 area_measure($areas, $array);
93
94                 // Remove 'offset's for area_measure()
95                 foreach(array_keys($array) as $key)
96                         unset($array[$key]['offset']);
97         }
98
99         return $array;
100 }
101
102 // Path normalization
103 // example.org => example.org/
104 // example.org#hoge -> example.org/#hoge
105 // example.org/path/a/b/./c////./d -> example.org/path/a/b/c/d
106 // example.org/path/../../a/../back
107 function path_normalize($path = '')
108 {
109         if (! is_string($path) || $path == '') {
110                 $path = '/';
111         } else {
112                 $path = trim($path);
113                 $last = ($path[strlen($path) - 1] == '/') ? '/' : '';
114                 $array = explode('/', $path);
115
116                 // Remove paddings
117                 foreach(array_keys($array) as $key) {
118                         if ($array[$key] == '' || $array[$key] == '.')
119                                  unset($array[$key]);
120                 }
121                 // Back-track
122                 $tmp = array();
123                 foreach($array as $value) {
124                         if ($value == '..') {
125                                 array_pop($tmp);
126                         } else {
127                                 array_push($tmp, $value);
128                         }
129                 }
130                 $array = & $tmp;
131
132                 if (empty($array)) {
133                         $path = '/';
134                 } else {
135                         $path = '/' . implode('/', $array) . $last;
136                 } 
137         }
138         return $path;
139 }
140
141 // If in doubt, it's a little doubtful
142 function area_measure($areas, &$array, $belief = -1, $a_key = 'area', $o_key = 'offset')
143 {
144         if (! is_array($areas) || ! is_array($array)) return;
145
146         $areas_keys = array_keys($areas);
147         foreach(array_keys($array) as $u_index) {
148                 $offset = isset($array[$u_index][$o_key]) ? intval($array[$u_index][$o_key]) : 0;
149                 foreach($areas_keys as $a_index) {
150                         if (isset($array[$u_index][$a_key])) {
151                                 $offset_s = intval($areas[$a_index][0]);
152                                 $offset_e = intval($areas[$a_index][1]);
153                                 if ($offset_s < $offset && $offset < $offset_e) {
154                                         $array[$u_index][$a_key] += $belief;
155                                 }
156                         }
157                 }
158         }
159 }
160
161
162 ?>