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
6 error_reporting(E_ALL); // Debug purpose
8 // TODO: Use 'm' multi-line regex option
10 // Return an array of normalized/parsed URIs in the $string
11 // [OK] http://nasty.example.org#nasty_string
12 function spam_pickup($string = '')
14 // Picup external URIs: scheme:+//+fqdn(/path)
15 // Not available for IPv6 host, user@password, port
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);
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)
32 strtolower(urldecode($array[$uri][$part][0]));
34 // example.org => example.org/
35 if (! isset($array[$uri][$_path])) $array[$uri][$_path] = '/';
36 $array[$uri]['offset'] = $offset;
37 $array[$uri]['area'] = 0;
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_
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>)
54 area_measure($areas, $array);
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/]
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]
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])
77 area_measure($areas, $array);
79 // Remove 'offset's for area_measure()
80 foreach(array_keys($array) as $key)
81 unset($array[$key]['offset']);
87 // If in doubt, it's a little doubtful
88 function area_measure($areas, &$array, $belief = -1, $a_key = 'area', $o_key = 'offset')
90 if (! is_array($areas) || ! is_array($array)) return;
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;
107 function show_form($string)
109 $base = basename(__FILE__);
110 $string = htmlspecialchars($string);
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" />
121 // ---- Show form and result
122 echo basename(__FILE__) . '<br />';
123 $msg = isset($_POST['msg']) ? $_POST['msg'] : '';
127 $results = spam_pickup($msg);
130 $count = count($results);
132 foreach($results as $result)
133 if (isset($result['area']))
134 $area += $result['area'];
136 echo "TOTAL = $count URIs, AREA_TOTAL = $area, AREA_AVERAGE = " . ($area / $count) . "</br >" . "</br >";