ba9664a2c28e390c2f39e65d368edf16ffbe84f6
[atutor.git] / mods / wiki / plugins / edit / spam_deface.php
1 <?php
2
3 /*
4    The page "BannedLinks" can hold URLs (or domain name / link patterns,
5    if put into square brackets) which will be filtered from the Wiki
6    everytime someone tries to put them on a page.
7    Filtering uses Google or a special redirection page to decrease
8    search engine ranking.
9    
10    - use this plugin if you encounter consecutive spam injection; it
11      is the preferred/recommended method
12    - plugins/linking/zero_pagerank.php is however more accurate and
13      up-to-date with banned URLs, as it operates at rendering time
14      (but therefore was also slower)
15 */
16
17 // define("ZERO_PAGERANK", "http://www.google.com/url?sa=D&q=");
18 define("ZERO_PAGERANK", "http://erfurtwiki.sourceforge.net/fragments/zero_pagerank.php?url=");
19 define("EWIKI_PAGE_BANNED", "BannedLinks");
20 $ewiki_config["info_refs_once"] = 1;  // disable {refs} info/ for old versions
21
22
23 $ewiki_plugins["edit_save"][] = "ewiki_edit_save_antispam_urldeface";
24 function ewiki_edit_save_antispam_urldeface(&$save, &$old) {
25
26    preg_match_all('°(http://[^\s*<>"\'\[\]\#]+)°', $old["content"], $old_urls);
27    preg_match_all('°(http://[^\s*<>"\'\[\]\#]+)°', $save["content"], $save_urls);
28
29    $added_urls = array_diff($save_urls[1], $old_urls[1]);
30    if ($added_urls) {
31       foreach ($added_urls as $url) {
32       
33          #-- test against BannedLinks, then deface (filter page) URL
34          if (ewiki_banned_link($url)) {
35             $save["content"] = str_replace($url, ZERO_PAGERANK.urlencode($url), $save["content"]);
36          }
37       }
38    }
39 }
40
41
42 function ewiki_banned_link($href) {
43    global $ewiki_config, $ewiki_plugins;
44    
45    #-- buffer list of banned urls
46    if (!isset($ewiki_config["banned"])) {
47       $data = ewiki_db::GET(EWIKI_PAGE_BANNED);
48       $ewiki_config["banned"] = trim(strtolower($data["refs"]));
49    }
50
51    #-- check for entry
52    if ($b = &$ewiki_config["banned"]) {
53       $href = strtolower(urldecode($href));
54       if (strpos($b, $href) !== false) {            // quick string check
55          return(true);
56       }
57       foreach (explode("\n", $b) as $bad) {         // use as patterns
58          if (strpos($href, $bad) !== false) {
59             return(true);
60          }
61       }
62    }
63    
64    #-- advanced
65    if ($pf_a = $ewiki_plugins["ban_lookup"]) {
66       foreach ($pf_a as $pf) {
67          if ($pf($href)) {
68             return(true);
69          }
70       }
71    }
72
73    return(false);
74 }
75
76
77 ?>