OSDN Git Service

.bh, .sc
[pukiwiki/pukiwiki_sandbox.git] / spam / spam_pickup.php
index 42d4f3c..8905f9e 100644 (file)
 <?php
-// $Id: spam_pickup.php,v 1.3 2006/10/25 17:58:48 teanan Exp $
+// $Id: spam_pickup.php,v 1.49 2007/05/12 09:19:49 henoheno Exp $
 // Concept-work of spam-uri metrics
-// Copyright (C) 2006 PukiWiki Developer Team
+// Copyright (C) 2006-2007 PukiWiki Developers Team
 // License: GPL v2 or (at your option) any later version
 
 error_reporting(E_ALL); // Debug purpose
 
-// TODO: Use 'm' multi-line regex option
+require('spam.php');
 
-// Return an array of normalized/parsed URIs in the $string
-// [OK] http://nasty.example.org#nasty_string
-function spam_pickup($string = '')
+// Recursive array_map()
+// e.g. Sanitilze ALL values (Debug purpose): var_dump(recursive_map('htmlspecialchars', $array));
+function recursive_map($func, $array)
 {
-       // Picup external URIs: scheme:+//+fqdn(/path)
-       // Not available for IPv6 host, user@password, port
-       $array = array();
-       preg_match_all(
-               '#(https?|\b[a-z0-9]{3,6})' .   // 1:Scheme
-               ':?//' .                                                // "//" or "://"
-               '([^\s<>"\'\[\]\#/]+)' .                // 2:Host (FQDN or IPv4 address)
-               '(?::[a-z0-9]*)?' .                             // Port
-               '((?:/[^\s<>"\'\[\]/]+)*/)?' .  // 3:Directory
-               '([^\s<>"\'\[\]]+)?' .                  // 4:Path and Query string
-               '#i', $string, $array, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
-       // Shrink $array
-       $_path = 3;
-       foreach(array_keys($array) as $uri) {
-               unset($array[$uri][0]); // Matched string itself
-               $offset = $array[$uri][1][1]; // [1][1] = scheme's offset
-               foreach(array_keys($array[$uri]) as $part) {
-                       // Remove offsets (with normalization)
-                       $array[$uri][$part] =
-                               strtolower(urldecode($array[$uri][$part][0]));
+       if (is_array($array)) {
+               if (! empty($array)) {
+                       $array = array_map('recursive_map',
+                                array_fill(0, count($array), $func), $array);
                }
-               // example.org => example.org/
-               if (! isset($array[$uri][$_path])) $array[$uri][$_path] = '/';
-               $array[$uri]['offset'] = $offset;
-               $array[$uri]['area']  = 0;
+       } else {
+               $array = $func($array);
        }
-
-       // Area elevation for '(especially external)link' intension
-       if (! empty($array)) {
-               // Anchor tags by preg_match_all()
-               // [OK] <a href="http://nasty.example.com">visit http://nasty.example.com/</a>
-               // [NG] <a href="http://ng.example.com">visit http://ng.example.com _not_ended_
-               $areas = array();
-               preg_match_all('#<a\b[^>]*href[^>]*>.*?</a\b[^>]*(>)#i',
-                        $string, $areas, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
-               foreach(array_keys($areas) as $area) {
-                       $areas[$area] =  array(
-                               $areas[$area][0][1], // [0][1] = Area start (<a href>)
-                               $areas[$area][1][1], // [1][1] = Area end   (</a>)
-                       );
-               }
-               area_measure($areas, $array);
-
-               // Various Wiki syntax
-               // [text_or_uri>text_or_uri]
-               // [text_or_uri:text_or_uri]
-               // [text_or_uri|text_or_uri]
-               // [text_or_uri->text_or_uri]
-               // [text_or_uri text_or_uri] // MediaWiki
-               // MediaWiki: [http://nasty.example.com/ visit http://nasty.example.com/]
-
-               // phpBB's "BBCode" by preg_match_all()
-               // [url]http://nasty.example.com/[/url]
-               // [link]http://nasty.example.com/[/link]
-               // [url=http://nasty.example.com]visit http://nasty.example.com/[/url]
-               $areas = array();
-               preg_match_all('#\[(url|link)\b[^\]]*\].*?\[/\1\b[^\]]*(\])#i',
-                        $string, $areas, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
-               foreach(array_keys($areas) as $area) {
-                       $areas[$area] = array(
-                               $areas[$area][0][1], // [0][1] = Area start ([url])
-                               $areas[$area][2][1], // [4][1] = Area end   ([/url])
-                       );
-               }
-               area_measure($areas, $array);
-
-               // Remove 'offset's for area_measure()
-               foreach(array_keys($array) as $key)
-                       unset($array[$key]['offset']);
-       }
-
        return $array;
 }
 
-// If in doubt, it's a little doubtful
-function area_measure($areas, &$array, $belief = -1, $a_key = 'area', $o_key = 'offset')
+function show_form($string, $asap = FALSE, $progress = TRUE, $pickup = TRUE)
 {
-       if (! is_array($areas) || ! is_array($array)) return;
-
-       $areas_keys = array_keys($areas);
-       foreach(array_keys($array) as $u_index) {
-               $offset = isset($array[$u_index][$o_key]) ? intval($array[$u_index][$o_key]) : 0;
-               foreach($areas_keys as $a_index) {
-                       if (isset($array[$u_index][$a_key])) {
-                               $offset_s = intval($areas[$a_index][0]);
-                               $offset_e = intval($areas[$a_index][1]);
-                               if ($offset_s < $offset && $offset < $offset_e) {
-                                       $array[$u_index][$a_key] += $belief;
-                               }
-                       }
-               }
-       }
-}
+       $string   = htmlspecialchars($string);
+       $asap     = $asap     ? ' checked' : '';
+       $progress = $progress ? ' checked' : '';
+       $pickup   = $pickup   ? ' checked' : '';
+       $base     = basename(__FILE__);
 
-function show_form($string)
-{
-       $base = basename(__FILE__);
-       $string = htmlspecialchars($string);
-       print <<< EOF
+       return <<< EOF
 <form action="$base" method="post">
+
        <textarea name="msg" rows="8" cols="80">$string</textarea><br />
+
+       <input type="checkbox" name="asap"   id="asap"   value="on"$asap>
+       <label for="asap">asap</label><br />
+
+       <input type="checkbox" name="progress" id="progress" value="on"$progress>
+       <label for="progress">Show \$progress</label><br />
+
+       <input type="checkbox" name="pickup" id="pickup" value="on"$pickup>
+       <label for="pickup">Show pickuped URIs</label><br />
+
        <input type="submit" name="write" value="Submit" />
+
 </form>
-<br/>
 EOF;
+
 }
 
 
 // ---- Show form and result
 echo basename(__FILE__) . '<br />';
-$msg = isset($_POST['msg']) ? $_POST['msg'] : '';
-show_form($msg);
 
-echo '<pre>';
-$results = spam_pickup($msg);
+$msg    = isset($_POST['msg'])      ? $_POST['msg'] : '';
+$asap   = isset($_POST['asap'])     ? TRUE : FALSE;
+$prog   = isset($_POST['progress']) ? TRUE : FALSE;
+$pickup = isset($_POST['pickup'])   ? TRUE : FALSE;
+
+echo show_form($msg, $asap, $prog, $pickup);
+echo '<br/>';
+
+
+// -----------------------------------------------------
+       $spam = array();
+
+       // Threshold and rules for insertion (default)
+       $spam['method']['_default'] = array(
+               '_comment'     => '_default',
+               'quantity'     =>  8,
+               //'non_uniquri'  =>  3,
+               'non_uniqhost' =>  3,
+               'area_anchor'  =>  0,
+               'area_bbcode'  =>  0,
+               'uniqhost'     => TRUE,
+               'badhost'      => TRUE,
+               //'asap'         => TRUE, // Stop as soon as possible (quick but less-info)
+       );
+       
+       // For editing
+       // NOTE:
+       // Any thresholds may LOCK your contents by
+       // "posting one URL" many times.
+       // Any rules will lock contents that have NG things already.
+       $spam['method']['edit'] = array(
+               // Supposed_by_you(n) * Edit_form_spec(2) * Margin(1.5)
+               '_comment'     => 'edit',
+               //'quantity'     => 60 * 3,
+               //'non_uniquri'  =>  5 * 3,
+               //'non_uniqhost' => 50 * 3,
+               //'area_anchor'  => 30 * 3,
+               //'area_bbcode'  => 15 * 3,
+               'uniqhost'     => TRUE,
+               'badhost'      => TRUE,
+               //'asap'         => TRUE,
+       );
+       
+       
+$method = & $spam['method']['_default'];
+//$method = & $spam['method']['edit'];
+//$method = check_uri_spam_method();
+//var_dump($method);
+// -----------------------------------------------------
+
+if ($asap) $method['asap'] = TRUE;
+
+$progress = check_uri_spam(
+       array(
+               'a http://foobaA.example.com',
+               $msg,
+               'b http://foobarB.example.com'
+       ), $method);
+
+if (! empty($progress)) {
+
+
+       if (empty($progress['is_spam'])) {
+               echo 'ACTION: Seems not a spam';
+               echo '<br />';
+       } else {
+               echo 'ACTION: Blocked by ' . summarize_spam_progress($progress, TRUE);
+               echo '<br />';
+
+               if (! $asap) {
+                       echo 'METRICS: ' . summarize_spam_progress($progress) . '<br />' . "\n";
+               }
 
-// Measure
-$count = count($results);
-$area = 0;
-foreach($results as $result)
-       if (isset($result['area']))
-               $area += $result['area'];
+               $action = 'Blocked by: ' . summarize_spam_progress($progress, TRUE);
 
-echo "TOTAL = $count URIs, AREA_TOTAL = $area, AREA_AVERAGE = " . ($area / $count) . "</br >" . "</br >";
-var_dump($results);
-echo '</pre>';
+               $tmp = summarize_detail_badhost($progress);
+               if ($tmp != '') {
+                       echo 'DETAIL_BADHOST: ' . 
+                               str_replace('  ', '&nbsp; ', nl2br(htmlspecialchars($tmp). "\n"));
+               }
+       }
 
+       $tmp = summarize_detail_newtral($progress);
+       if (! $asap && $tmp != '') {
+               echo 'DETAIL_NEUTRAL_HOST: ' . 
+                               str_replace('  ', '&nbsp; ', nl2br(htmlspecialchars($tmp). "\n"));
+       }
+       
+       if ($prog) {
+               echo '<pre>';
+               echo '$progress:' . "\n";
+               echo htmlspecialchars(var_export($progress, TRUE));
+               echo '</pre>';
+       }
+}
 
+if ($pickup) {
+       echo '<pre>';
+       $results = uri_pickup_normalize(spam_uri_pickup($msg));
+       echo '$results:' . "\n";
+       echo htmlspecialchars(var_export($results, TRUE));
+       echo '</pre>';
+}
 ?>