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
7 error_reporting(E_ALL); // Debug purpose
9 // TODO: Use 'm' multi-line regex option
11 // Return an array of normalized/parsed URIs in the $string
12 // [OK] http://nasty.example.org#nasty_string
13 function spam_pickup($string = '')
15 // Picup external URIs: scheme:+//+fqdn(/path)
16 // Not available for IPv6 host, user@password, port
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);
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)
33 strtolower(urldecode($array[$uri][$part][0]));
35 // example.org => example.org/
36 if (! isset($array[$uri][$_path])) $array[$uri][$_path] = '/';
37 $array[$uri]['offset'] = $offset;
38 $array[$uri]['area'] = 0;
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_
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>)
55 area_measure($areas, $array);
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/]
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]
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])
78 area_measure($areas, $array);
80 // Remove 'offset's for area_measure()
81 foreach(array_keys($array) as $key)
82 unset($array[$key]['offset']);
88 // If in doubt, it's a little doubtful
89 function area_measure($areas, &$array, $belief = -1, $a_key = 'area', $o_key = 'offset')
91 if (! is_array($areas) || ! is_array($array)) return;
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;
108 function show_form($string)
110 $base = basename(__FILE__);
111 $string = htmlspecialchars($string);
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" />
122 // ---- Show form and result
123 echo basename(__FILE__) . '<br />';
124 $msg = isset($_POST['msg']) ? $_POST['msg'] : '';
128 $results = spam_pickup($msg);
131 $count = count($results);
133 foreach($results as $result)
134 if (isset($result['area']))
135 $area += $result['area'];
137 echo "TOTAL = $count URIs, AREA_TOTAL = $area, AREA_AVERAGE = " . ($area / $count) . "</br >" . "</br >";