OSDN Git Service

Library spam.php
[pukiwiki/pukiwiki_sandbox.git] / spam.php
1 <?php
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
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 function spam_pickup($string = '')
11 {
12         // Not available for user@password, IDN
13         $array = array();
14         preg_match_all(
15                 // Refer RFC3986
16                 '#(\b[a-z][a-z0-9.+-]{1,8})://' .       // 1: Scheme
17                 '(' .
18                         // 2: Host
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
22                 ')' .
23                 '(?::([a-z0-9]*))?' .                   // 3: Port
24                 '((?:/+[^\s<>"\'\[\]/]+)*/+)?' .// 4: Directory path or path-info
25                 '([^\s<>"\'\[\]]+)?' .                  // 5: File and query string
26                 '#i',
27                  $string, $array, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
28                  // Tests: ftp://dfshodfs:80/dfsdfs
29         //var_dump(recursive_map('htmlspecialchars', $array));
30         // Shrink $array
31         $_port = 3;
32         $_path = 4;
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)
38                         $array[$uri][$part] =
39                                         strtolower(urldecode($array[$uri][$part][0]));
40                 }
41                 
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;
47         }
48
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>
55                 $areas = array();
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>)
63                         );
64                 }
65                 area_measure($areas, $array);
66
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/]
74
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]
80                 // ?? [url=][/url]
81                 $areas = array();
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])
89                         );
90                 }
91                 area_measure($areas, $array);
92
93                 // Remove 'offset's for area_measure()
94                 foreach(array_keys($array) as $key)
95                         unset($array[$key]['offset']);
96         }
97
98         return $array;
99 }
100
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 = '')
107 {
108         if (! is_string($path) || $path == '') {
109                 $path = '/';
110         } else {
111                 $path = trim($path);
112                 $last = ($path[strlen($path) - 1] == '/') ? '/' : '';
113                 $array = explode('/', $path);
114
115                 // Remove paddings
116                 foreach(array_keys($array) as $key) {
117                         if ($array[$key] == '' || $array[$key] == '.')
118                                  unset($array[$key]);
119                 }
120                 // Back-track
121                 $tmp = array();
122                 foreach($array as $value) {
123                         if ($value == '..') {
124                                 array_pop($tmp);
125                         } else {
126                                 array_push($tmp, $value);
127                         }
128                 }
129                 $array = & $tmp;
130
131                 if (empty($array)) {
132                         $path = '/';
133                 } else {
134                         $path = '/' . implode('/', $array) . $last;
135                 } 
136         }
137         return $path;
138 }
139
140 // If in doubt, it's a little doubtful
141 function area_measure($areas, &$array, $belief = -1, $a_key = 'area', $o_key = 'offset')
142 {
143         if (! is_array($areas) || ! is_array($array)) return;
144
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;
154                                 }
155                         }
156                 }
157         }
158 }
159
160
161 ?>