OSDN Git Service

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