OSDN Git Service

*** empty log message ***
[pukiwiki/pukiwiki_sandbox.git] / spam_pickup.php
1 <?php
2 // $Id: spam_pickup.php,v 1.1 2006/10/23 12:57:06 henoheno Exp $
3 // Concept-work of spam-uri metrics
4 // Copyright (C) 2006 PukiWiki Developer Team
5
6 error_reporting(E_ALL); // Debug purpose
7
8 // TODO: Use 'm' multi-line regex option
9
10 // Return an array of normalized/parsed URIs in the $string
11 // [OK] http://nasty.example.org#nasty_string
12 function spam_pickup($string = '')
13 {
14         // Picup external URIs: scheme:+//+fqdn(/path)
15         // Not available for IPv6 host, user@password, port
16         $array = array();
17         preg_match_all(
18                 '#(https?|\b[a-z0-9]{3,6})' .   // 1:Scheme
19                 ':?//' .                                                // "//" or "://"
20                 '([^\s<>"\'\[\]\#/]+)' .                // 2:Host (FQDN or IPv4 address)
21                 '(?::[a-z0-9]*)?' .                             // Port
22                 '([^\s<>"\'\[\]]+)?' .                  // 3:Path and Query string
23                 '#i', $string, $array, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
24         // Shrink $array
25         $_path = 3;
26         foreach(array_keys($array) as $uri) {
27                 unset($array[$uri][0]); // Matched string itself
28                 $offset = $array[$uri][1][1]; // [1][1] = scheme's offset
29                 foreach(array_keys($array[$uri]) as $part) {
30                         // Remove offsets (with normalization)
31                         $array[$uri][$part] =
32                                 strtolower(urldecode($array[$uri][$part][0]));
33                 }
34                 // example.org => example.org/
35                 if (! isset($array[$uri][$_path])) $array[$uri][$_path] = '/';
36                 $array[$uri]['offset'] = $offset;
37                 $array[$uri]['area']  = 0;
38         }
39
40         // Area elevation for '(especially external)link' intension
41         if (! empty($array)) {
42                 // Anchor tags by preg_match_all()
43                 // [OK] <a href="http://nasty.example.com">visit http://nasty.example.com/</a>
44                 // [NG] <a href="http://ng.example.com">visit http://ng.example.com _not_ended_
45                 $areas = array();
46                 preg_match_all('#<a\b[^>]*href[^>]*>.*?</a\b[^>]*(>)#i',
47                          $string, $areas, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
48                 foreach(array_keys($areas) as $area) {
49                         $areas[$area] =  array(
50                                 $areas[$area][0][1], // [0][1] = Area start (<a href>)
51                                 $areas[$area][1][1], // [1][1] = Area end   (</a>)
52                         );
53                 }
54                 area_measure($areas, $array);
55
56                 // Various Wiki syntax
57                 // [text_or_uri>text_or_uri]
58                 // [text_or_uri:text_or_uri]
59                 // [text_or_uri|text_or_uri]
60                 // [text_or_uri->text_or_uri]
61                 // [text_or_uri text_or_uri] // MediaWiki
62                 // MediaWiki: [http://nasty.example.com/ visit http://nasty.example.com/]
63
64                 // phpBB's "BBCode" by preg_match_all()
65                 // [url]http://nasty.example.com/[/url]
66                 // [link]http://nasty.example.com/[/link]
67                 // [url=http://nasty.example.com]visit http://nasty.example.com/[/url]
68                 $areas = array();
69                 preg_match_all('#\[(url|link)\b[^\]]*\].*?\[/\1\b[^\]]*(\])#i',
70                          $string, $areas, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
71                 foreach(array_keys($areas) as $area) {
72                         $areas[$area] = array(
73                                 $areas[$area][0][1], // [0][1] = Area start ([url])
74                                 $areas[$area][2][1], // [4][1] = Area end   ([/url])
75                         );
76                 }
77                 area_measure($areas, $array);
78
79                 // Remove 'offset's for area_measure()
80                 foreach(array_keys($array) as $key)
81                         unset($array[$key]['offset']);
82         }
83
84         return $array;
85 }
86
87 // If in doubt, it's a little doubtful
88 function area_measure($areas, &$array, $belief = -1, $a_key = 'area', $o_key = 'offset')
89 {
90         if (! is_array($areas) || ! is_array($array)) return;
91
92         $areas_keys = array_keys($areas);
93         foreach(array_keys($array) as $u_index) {
94                 $offset = isset($array[$u_index][$o_key]) ? intval($array[$u_index][$o_key]) : 0;
95                 foreach($areas_keys as $a_index) {
96                         if (isset($array[$u_index][$a_key])) {
97                                 $offset_s = intval($areas[$a_index][0]);
98                                 $offset_e = intval($areas[$a_index][1]);
99                                 if ($offset_s < $offset && $offset < $offset_e) {
100                                         $array[$u_index][$a_key] += $belief;
101                                 }
102                         }
103                 }
104         }
105 }
106
107 function show_form($string)
108 {
109         $base = basename(__FILE__);
110         $string = htmlspecialchars($string);
111         print <<< EOF
112 <form action="$base" method="post">
113         <textarea name="msg" rows="8" cols="80">$string</textarea><br />
114         <input type="submit" name="write" value="Submit" />
115 </form>
116 <br/>
117 EOF;
118 }
119
120
121 // ---- Show form and result
122 echo basename(__FILE__) . '<br />';
123 $msg = isset($_POST['msg']) ? $_POST['msg'] : '';
124 show_form($msg);
125
126 echo '<pre>';
127 $results = spam_pickup($msg);
128
129 // Measure
130 $count = count($results);
131 $area = 0;
132 foreach($results as $result)
133         if (isset($result['area']))
134                 $area += $result['area'];
135
136 echo "TOTAL = $count URIs, AREA_TOTAL = $area, AREA_AVERAGE = " . ($area / $count) . "</br >" . "</br >";
137 var_dump($results);
138 echo '</pre>';
139
140
141 ?>