1<?php
2// must be run within Dokuwiki
3if(!defined('DOKU_INC')) die();
4
5if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
6require_once(DOKU_PLUGIN.'admin.php');
7
8/**
9 * All DokuWiki plugins to extend the admin function
10 * need to inherit from this class
11 */
12class admin_plugin_whyspam extends DokuWiki_Admin_Plugin {
13
14    /**
15     * access for managers
16     */
17    function forAdminOnly(){
18        return false;
19    }
20
21    /**
22     * return sort order for position in admin menu
23     */
24    function getMenuSort() {
25        return 71;
26    }
27
28    /**
29     * handle user request
30     */
31    function handle() {
32    }
33
34    /**
35     * output appropriate html
36     */
37    function html() {
38        global $config_cascade;
39
40        echo $this->locale_xhtml('intro');
41        echo '<form method="post" action="" class="whyspam">';
42        echo '<fieldset>';
43        echo '<legend>'.$this->getLang('paste').'</legend>';
44        echo '<textarea name="whyspam" class="edit"></textarea><br />';
45        echo '<input type="submit" class="button" />';
46        echo '</fieldset>';
47        echo '</form>';
48
49        $found = array();
50        if($_REQUEST['whyspam']){
51            foreach($config_cascade['wordblock'] as $bla => $files){
52                foreach($files as $file){
53                    $found = array_merge($found,$this->_checkit($file,$_REQUEST['whyspam']));
54                }
55            }
56
57            echo '<div class="level2">';
58            if(count($found)){
59                echo '<p>'.$this->getLang('found').'</p>';
60                echo '<ul>';
61                foreach($found as $f){
62                    echo '<li><div class="li">'.hsc($f[0]).':'.$f[1].'<br /><code>'.hsc($f[2]).'</code><br /><i>'.hsc($f[3]).'</i></div></li>';
63                }
64                echo '</ul>';
65            }else{
66                echo '<p>'.$this->getLang('notfound').'</p>';
67            }
68            echo '</div>';
69        }
70
71    }
72
73
74    function _checkit($file,&$text){
75        $found = array();
76
77        $blockfile = (array) @file($file);
78        $i=0;
79        $text = preg_replace('!(\b)(www\.[\w.:?\-;,]+?\.[\w.:?\-;,]+?[\w/\#~:.?+=&%@\!\-.:?\-;,]+?)([.:?\-;,]*[^\w/\#~:.?+=&%@\!\-.:?\-;,])!i','\1http://\2 \2\3',$text);
80        foreach($blockfile as $block){
81            $i++;
82            $block = preg_replace('/#.*$/','',$block);
83            $block = trim($block);
84            if(empty($block)) continue;
85            if(preg_match('#('.$block.')#si',$text,$matches)){
86                $pos = strpos($text,$matches[0]);
87                $s = max(0,$pos - 15);
88                $l = strlen($matches[0])+30;
89                $snip = substr($text,$s,$l);
90
91                $found[] = array($file, $i, $block, $snip);
92            }
93        }
94        return $found;
95    }
96
97
98}
99//Setup VIM: ex: et ts=4 :
100