OSDN Git Service

require() subroutines
[pukiwiki/pukiwiki_sandbox.git] / spam / checker.php
1 <?php
2 // $Id: checker.php,v 1.6 2009/01/02 11:55:45 henoheno Exp $
3 // Concept-work of spam-uri metrics
4 // Copyright (C) 2006-2007 PukiWiki Developers Team
5 // License: GPL v2 or (at your option) any later version
6
7 error_reporting(E_ALL); // Debug purpose
8
9 require('spam.php');
10
11 // Recursive array_map()
12 // e.g. Sanitilze ALL values (Debug purpose): var_dump(recursive_map('htmlspecialchars', $array));
13 function recursive_map($func, $array)
14 {
15         if (is_array($array)) {
16                 if (! empty($array)) {
17                         $array = array_map('recursive_map',
18                                  array_fill(0, count($array), $func), $array);
19                 }
20         } else {
21                 $array = $func($array);
22         }
23         return $array;
24 }
25
26 function show_form($string, $asap = FALSE, $progress = TRUE, $pickup = TRUE)
27 {
28         $string   = htmlspecialchars($string);
29         $asap     = $asap     ? ' checked' : '';
30         $progress = $progress ? ' checked' : '';
31         $pickup   = $pickup   ? ' checked' : '';
32         $base     = basename(__FILE__);
33
34         return <<< EOF
35 <form action="$base" method="post">
36
37         <textarea name="msg" rows="8" cols="80">$string</textarea><br />
38
39         <input type="checkbox" name="asap"   id="asap"   value="on"$asap>
40         <label for="asap">asap</label><br />
41
42         <input type="checkbox" name="progress" id="progress" value="on"$progress>
43         <label for="progress">Show \$progress</label><br />
44
45         <input type="checkbox" name="pickup" id="pickup" value="on"$pickup>
46         <label for="pickup">Show pickuped URIs</label><br />
47
48         <input type="submit" name="write" value="Submit" />
49
50 </form>
51 EOF;
52
53 }
54
55
56 // ---- Show form and result
57 echo basename(__FILE__) . '<br />';
58
59 $msg    = isset($_POST['msg'])      ? $_POST['msg'] : '';
60 $asap   = isset($_POST['asap'])     ? TRUE : FALSE;
61 $prog   = isset($_POST['progress']) ? TRUE : FALSE;
62 $pickup = isset($_POST['pickup'])   ? TRUE : FALSE;
63
64 echo show_form(stripslashes($msg), $asap, $prog, $pickup);
65 echo '<br/>';
66
67
68 // -----------------------------------------------------
69         $spam = array();
70
71         // Threshold and rules for insertion (default)
72         $spam['method']['_default'] = array(
73                 '_comment'     => '_default',
74                 'quantity'     =>  8,
75                 //'non_uniquri'  =>  3,
76                 'non_uniqhost' =>  3,
77                 'area_anchor'  =>  0,
78                 'area_bbcode'  =>  0,
79                 'uniqhost'     => TRUE,
80                 'badhost'      => TRUE,
81                 //'asap'         => TRUE, // Stop as soon as possible (quick but less-info)
82         );
83         
84         // For editing
85         // NOTE:
86         // Any thresholds may LOCK your contents by
87         // "posting one URL" many times.
88         // Any rules will lock contents that have NG things already.
89         $spam['method']['edit'] = array(
90                 // Supposed_by_you(n) * Edit_form_spec(2) * Margin(1.5)
91                 '_comment'     => 'edit',
92                 //'quantity'     => 60 * 3,
93                 //'non_uniquri'  =>  5 * 3,
94                 //'non_uniqhost' => 50 * 3,
95                 //'area_anchor'  => 30 * 3,
96                 //'area_bbcode'  => 15 * 3,
97                 'uniqhost'     => TRUE,
98                 'badhost'      => TRUE,
99                 //'asap'         => TRUE,
100         );
101         
102         
103 $method = & $spam['method']['_default'];
104 //$method = & $spam['method']['edit'];
105 //$method = check_uri_spam_method();
106 //var_dump($method);
107 // -----------------------------------------------------
108
109 if ($asap) $method['asap'] = TRUE;
110
111 $progress = check_uri_spam(
112         array(
113                 'a http://foobaA.example.com',
114                 $msg,
115                 'b http://foobarB.example.com'
116         ), $method);
117
118 if (! empty($progress)) {
119
120
121         if (empty($progress['is_spam'])) {
122                 echo 'ACTION: Seems not a spam';
123                 echo '<br />';
124         } else {
125                 echo 'ACTION: Blocked by ' . summarize_spam_progress($progress, TRUE);
126                 echo '<br />';
127
128                 if (! $asap) {
129                         echo 'METRICS: ' . summarize_spam_progress($progress) . '<br />' . "\n";
130                 }
131
132                 $action = 'Blocked by: ' . summarize_spam_progress($progress, TRUE);
133
134                 $tmp = summarize_detail_badhost($progress);
135                 if ($tmp != '') {
136                         echo 'DETAIL_BADHOST: ' . 
137                                 str_replace('  ', '&nbsp; ', nl2br(htmlspecialchars($tmp). "\n"));
138                 }
139         }
140
141         $tmp = summarize_detail_newtral($progress);
142         if (! $asap && $tmp != '') {
143                 echo 'DETAIL_NEUTRAL_HOST: ' . 
144                                 str_replace('  ', '&nbsp; ', nl2br(htmlspecialchars($tmp). "\n"));
145         }
146         
147         if ($prog) {
148                 echo '<pre>';
149                 echo '$progress:' . "\n";
150                 echo htmlspecialchars(var_export($progress, TRUE));
151                 echo '</pre>';
152         }
153 }
154
155 if ($pickup) {
156         echo '<pre>';
157         $results = spam_uri_pickup($msg);
158         $results = uri_pickup_normalize($results);
159         $results = uri_pickup_normalize_pathfile($results);
160         echo '$results:' . "\n";
161         echo htmlspecialchars(var_export($results, TRUE));
162         echo '</pre>';
163 }
164 ?>