edddf667daa471a37a33dc7d4485101b08522f67
[atutor.git] / mods / wiki / plugins / edit / spam_block.php
1 <?php
2
3 /*
4    Unlike with ../edit/spam_deface, this plugin allows to really block
5    people from adding links. However, you must put the forbidden domain
6    names and URL search patterns on the page named "BlockedLinks"
7    instead. But this allows you to separate between banned and blocked
8    URLs then.
9    
10    - useful for aggressive link spammers
11    - can be used in conjunction with one of the _deface plugins, but
12      then must be loaded _before_ any of the other ones
13 */
14
15 define("EWIKI_PAGE_BLOCKED", "BlockedLinks");
16 $ewiki_config["info_refs_once"] = 1;  // disable {refs} info/ for old versions
17
18 $ewiki_t["en"]["BLOCKED_URL"] = "Sorry boy, but we couldn't accept your submission, because one or more of the contained links is on our blacklist. All newly submitted have been added, ha ha - and depending on the admins preferences a Google complaint has been sent. Now go away.";
19
20
21 $ewiki_plugins["edit_save"][] = "ewiki_edit_save_antispam_urlblock";
22 function ewiki_edit_save_antispam_urlblock(&$save, &$old_data) {
23
24    global $ewiki_errmsg, $ewiki_id;
25    $BLOCK = EWIKI_PAGE_BLOCKED;
26
27    preg_match_all('°(http://[^\s*<>"\'\[\]\#]+)°', $save["content"], $save_urls);
28    preg_match_all('°(http://[^\s*<>"\'\[\]\#]+)°', $old_data["content"], $old_urls);
29
30    $added_urls = array_diff($save_urls[1], $old_urls[1]);
31    if ($added_urls) {
32       foreach ($added_urls as $i=>$url) {
33       
34          #-- test against BannedLinks, then deface (filter page) URL
35          if (ewiki_blocked_link($url, $BLOCK)) {
36             $block = true;
37             unset($added_urls[$i]);
38          }
39       }
40       $old = $i + 1;
41    }
42
43    #-- if matched
44    if ($block) {
45       #-- add new URLs to our BannedLinks page
46       if ($new = count($added_urls)) {
47          $content = "";
48          foreach ($added_urls as $d) { 
49             $d = preg_replace('#^.+//(?:www\.)?#', '', $d);
50             $d = preg_replace('#^([^/]+)(/.*)?$*', '$1', $d);
51             if ($d) {
52                $content .= "\n* [$d] (auto-added by spam attack on [$ewiki_id])";
53             }
54          }
55          if ($content) {
56             ewiki_db::APPEND($BLOCK, $content);
57          }
58          $date = strftime("%c", time());
59          ewiki_append_to_page("SpamLog", "\n* spam attack on [$ewiki_id] from $_SERVER[REMOTE_ADDRESS]:$_SERVER[REMOTE_PORT] ($_SERVER[HTTP_USER_AGENT]) happend at $date, around {$new} of the {$old} added URLs were already on BlockedLinks");
60       }
61
62       #-- error reporting method for ["edit_save"]
63       $save = array();
64       $ewiki_errmsg = ewiki_t("BLOCKED_URL");
65       return(false);
66    }
67 }
68
69
70
71 function ewiki_blocked_link($href, $LinkPage=EWIKI_PAGE_BLOCKED) {
72    global $ewiki_config, $ewiki_plugins;
73    
74    if (! ($href = trim(strtolower(urldecode($href)))) ) {
75       return;
76    }
77    
78    #-- buffer list of banned urls
79    if (!isset($ewiki_config[$LinkPage])) {
80       $data = ewiki_db::GET($LinkPage);
81       $ewiki_config[$LinkPage] = trim(strtolower($data["refs"]));
82    }
83
84    #-- check for entry
85    if ($b = &$ewiki_config[$LinkPage]) {
86       if (strpos($b, $href) !== false) {            // quick string check
87          return(true);
88       }
89       foreach (explode("\n", $b) as $bad) {         // use as patterns
90          if (strlen($bad) && (strpos($href, $bad) !== false)) {
91             return(true);
92          }
93       }
94    }
95    
96    #-- advanced
97    if ($pf_a = $ewiki_plugins["ban_lookup"]) {
98       foreach ($pf_a as $pf) {
99          if ($pf($href)) {
100             return(true);
101          }
102       }
103    }
104
105    return(false);
106 }
107
108
109 ?>