Blame
Date:
Tue May 17 04:00:37 2022 UTC
Message:
Daily backup
001
2021-12-17
jrmu
<?php if (!defined('PmWiki')) exit();
002
2021-12-17
jrmu
/* Copyright 2006-2019 Patrick R. Michaud (pmichaud@pobox.com)
003
2021-12-17
jrmu
This file is part of PmWiki; you can redistribute it and/or modify
004
2021-12-17
jrmu
it under the terms of the GNU General Public License as published
005
2021-12-17
jrmu
by the Free Software Foundation; either version 2 of the License, or
006
2021-12-17
jrmu
(at your option) any later version. See pmwiki.php for full details.
007
2021-12-17
jrmu
008
2021-12-17
jrmu
This script adds blocklisting capabilities to PmWiki, and can
009
2021-12-17
jrmu
be enabled by simply setting the following in local/config.php:
010
2021-12-17
jrmu
011
2021-12-17
jrmu
$EnableBlocklist = 1;
012
2021-12-17
jrmu
013
2021-12-17
jrmu
With $EnableBlocklist set to 1, this module will search through
014
2021-12-17
jrmu
the SiteAdmin.Blocklist page, as well as any other pages given by
015
2021-12-17
jrmu
the $Blocklist pages variable, looking for lines of the
016
2021-12-17
jrmu
form "block:some phrase" or "block:/regex/", with "some phrase"
017
2021-12-17
jrmu
and "/regex/" indicating things to be excluded from any
018
2021-12-17
jrmu
posting to the site.
019
2021-12-17
jrmu
020
2021-12-17
jrmu
In addition, if a page contains IP addresses of the form
021
2021-12-17
jrmu
"a.b.c.d" or "a.b.c.*", then any posts coming from hosts
022
2021-12-17
jrmu
matching the address will be blocked.
023
2021-12-17
jrmu
024
2021-12-17
jrmu
There is also an "unblock:..." form, which removes an entry
025
2021-12-17
jrmu
from the blocklist. This is useful for removing specific
026
2021-12-17
jrmu
block items in wikifarms and with automatically downloaded
027
2021-12-17
jrmu
blocklists (below).
028
2021-12-17
jrmu
029
2021-12-17
jrmu
The script also has the capability of automatically downloading
030
2021-12-17
jrmu
blocklists from other sources, such as chongqed.org and
031
2021-12-17
jrmu
and the MoinMaster blocklist. These are configured using
032
2021-12-17
jrmu
the $BlocklistDownload array. An $EnableBlocklist value
033
2021-12-17
jrmu
of at least 10 configures PmWiki to automatically download
034
2021-12-17
jrmu
these external blocklists and refresh them daily.
035
2021-12-17
jrmu
036
2021-12-17
jrmu
More information about blocklists is available in the
037
2021-12-17
jrmu
PmWiki.Blocklist page.
038
2021-12-17
jrmu
039
2021-12-17
jrmu
Script maintained by Petko YOTOV www.pmwiki.org/petko
040
2021-12-17
jrmu
*/
041
2021-12-17
jrmu
042
2021-12-17
jrmu
043
2021-12-17
jrmu
## Some recipes do page updates outside of the built-in posting
044
2021-12-17
jrmu
## cycle, so $EnableBlocklistImmediate is used to determine if
045
2021-12-17
jrmu
## we need to catch these. Currently this defaults to enabled,
046
2021-12-17
jrmu
## but at some point we may change the default to disabled.
047
2021-12-17
jrmu
if (IsEnabled($EnableBlocklistImmediate, 1)) {
048
2021-12-17
jrmu
SDVA($BlocklistActions, array('comment' => 1));
049
2021-12-17
jrmu
$ptext = implode(' ', @$_POST);
050
2021-12-17
jrmu
if ($ptext && @$BlocklistActions[$action]) {
051
2021-12-17
jrmu
Blocklist($pagename, $ptext);
052
2021-12-17
jrmu
if (!$EnablePost) {
053
2021-12-17
jrmu
unset($_POST['post']);
054
2021-12-17
jrmu
unset($_POST['postattr']);
055
2021-12-17
jrmu
unset($_POST['postedit']);
056
2021-12-17
jrmu
}
057
2021-12-17
jrmu
}
058
2021-12-17
jrmu
}
059
2021-12-17
jrmu
060
2021-12-17
jrmu
061
2021-12-17
jrmu
## If $EnableBlocklist is set to 10 or higher, then arrange to
062
2021-12-17
jrmu
## periodically download the "chongqed" and "moinmaster" blacklists.
063
2021-12-17
jrmu
if ($EnableBlocklist >= 10) {
064
2021-12-17
jrmu
# SDVA($BlocklistDownload['SiteAdmin.Blocklist-Chongqed'], array(
065
2021-12-17
jrmu
# 'url' => 'http://blacklist.chongqed.org/',
066
2021-12-17
jrmu
# 'format' => 'regex'));
067
2021-12-17
jrmu
SDVA($BlocklistDownload['SiteAdmin.Blocklist-MoinMaster'], array(
068
2021-12-17
jrmu
'url' => 'http://moinmo.in/BadContent?action=raw',
069
2021-12-17
jrmu
'format' => 'regex'));
070
2021-12-17
jrmu
}
071
2021-12-17
jrmu
072
2021-12-17
jrmu
073
2021-12-17
jrmu
## CheckBlocklist is inserted into $EditFunctions, to automatically
074
2021-12-17
jrmu
## check for blocks on anything being posted through the normal
075
2021-12-17
jrmu
## "update a page cycle"
076
2021-12-17
jrmu
array_unshift($EditFunctions, 'CheckBlocklist');
077
2021-12-17
jrmu
function CheckBlocklist($pagename, &$page, &$new) {
078
2021-12-17
jrmu
StopWatch("CheckBlocklist: begin $pagename");
079
2021-12-17
jrmu
$ptext = implode(' ', @$_POST);
080
2021-12-17
jrmu
if (@$ptext) Blocklist($pagename, $ptext);
081
2021-12-17
jrmu
StopWatch("CheckBlocklist: end $pagename");
082
2021-12-17
jrmu
}
083
2021-12-17
jrmu
084
2021-12-17
jrmu
085
2021-12-17
jrmu
## Blocklist is the function that does all of the work of
086
2021-12-17
jrmu
## checking for reasons to block a posting. It reads
087
2021-12-17
jrmu
## the available blocklist pages ($BlocklistPages) and
088
2021-12-17
jrmu
## builds an array of strings and regular expressiongs to
089
2021-12-17
jrmu
## be checked against the page; if any are found, then
090
2021-12-17
jrmu
## posting is blocked (via $EnablePost=0). The function
091
2021-12-17
jrmu
## also checks the REMOTE_ADDR against any blocked IP addresses.
092
2021-12-17
jrmu
function Blocklist($pagename, $text) {
093
2021-12-17
jrmu
global $BlocklistPages, $BlockedMessagesFmt, $BlocklistDownload,
094
2021-12-17
jrmu
$BlocklistDownloadRefresh, $Now, $EnablePost, $WhyBlockedFmt,
095
2021-12-17
jrmu
$MessagesFmt, $BlocklistMessageFmt, $EnableWhyBlocked, $IsBlocked;
096
2021-12-17
jrmu
097
2021-12-17
jrmu
StopWatch("Blocklist: begin $pagename");
098
2021-12-17
jrmu
099
2021-12-17
jrmu
$BlocklistDownload = (array)@$BlocklistDownload;
100
2021-12-17
jrmu
SDV($BlocklistPages,
101
2021-12-17
jrmu
array_merge(array('$SiteAdminGroup.Blocklist',
102
2021-12-17
jrmu
'$SiteAdminGroup.Blocklist-Farm'),
103
2021-12-17
jrmu
array_keys($BlocklistDownload)));
104
2021-12-17
jrmu
SDV($BlocklistMessageFmt, "<h3 class='wikimessage'>$[This post has been blocked by the administrator]</h3>");
105
2021-12-17
jrmu
SDVA($BlockedMessagesFmt, array(
106
2021-12-17
jrmu
'ip' => '$[Address blocked from posting]: ',
107
2021-12-17
jrmu
'text' => '$[Text blocked from posting]: '));
108
2021-12-17
jrmu
SDV($BlocklistDownloadRefresh, 86400);
109
2021-12-17
jrmu
110
2021-12-17
jrmu
## Loop over all blocklist pages
111
2021-12-17
jrmu
foreach((array)$BlocklistPages as $b) {
112
2021-12-17
jrmu
113
2021-12-17
jrmu
## load the current blocklist page
114
2021-12-17
jrmu
$pn = FmtPageName($b, $pagename);
115
2021-12-17
jrmu
$page = ReadPage($pn, READPAGE_CURRENT);
116
2021-12-17
jrmu
if (!$page) continue;
117
2021-12-17
jrmu
118
2021-12-17
jrmu
## if the page being checked is a blocklist page, stop blocking
119
2021-12-17
jrmu
if ($pagename == $pn) return;
120
2021-12-17
jrmu
121
2021-12-17
jrmu
## If the blocklist page is managed by automatic download,
122
2021-12-17
jrmu
## schedule any new downloads here
123
2021-12-17
jrmu
if (@$BlocklistDownload[$pn]) {
124
2021-12-17
jrmu
$bd = &$BlocklistDownload[$pn];
125
2021-12-17
jrmu
SDVA($bd, array(
126
2021-12-17
jrmu
'refresh' => $BlocklistDownloadRefresh,
127
2021-12-17
jrmu
'url' => "http://www.pmwiki.org/blocklists/$pn" ));
128
2021-12-17
jrmu
if (!@$page['text'] || $page['time'] < $Now - $bd['refresh'])
129
2021-12-17
jrmu
register_shutdown_function('BlocklistDownload', $pn, getcwd());
130
2021-12-17
jrmu
}
131
2021-12-17
jrmu
132
2021-12-17
jrmu
## If the blocklist is simply a list of regexes to be matched, load
133
2021-12-17
jrmu
## them into $terms['block'] and continue to the next blocklist page.
134
2021-12-17
jrmu
## Some regexes from remote sites aren't well-formed, so we have
135
2021-12-17
jrmu
## to escape any slashes that aren't already escaped.
136
2021-12-17
jrmu
if (strpos(@$page['text'], 'blocklist-format: regex') !==false) {
137
2021-12-17
jrmu
if (preg_match_all('/^([^\\s#].+)/m', $page['text'], $match))
138
2021-12-17
jrmu
foreach($match[0] as $m) {
139
2021-12-17
jrmu
$m = preg_replace('#(?<!\\\\)/#', '\\/', trim($m));
140
2021-12-17
jrmu
$terms['block'][] = "/$m/";
141
2021-12-17
jrmu
}
142
2021-12-17
jrmu
continue;
143
2021-12-17
jrmu
}
144
2021-12-17
jrmu
145
2021-12-17
jrmu
## Treat the page as a pmwiki-format blocklist page, with
146
2021-12-17
jrmu
## IP addresses and "block:"-style declarations. First, see
147
2021-12-17
jrmu
## if we need to block the author based on a.b.c.d or a.b.c.*
148
2021-12-17
jrmu
## IP addresses.
149
2021-12-17
jrmu
$ip = preg_quote($_SERVER['REMOTE_ADDR']);
150
2021-12-17
jrmu
$ip = preg_replace('/\\d+$/', '($0\\b|\\*)', $ip);
151
2021-12-17
jrmu
if (preg_match("/\\b$ip/", @$page['text'], $match)) {
152
2021-12-17
jrmu
$EnablePost = 0;
153
2021-12-17
jrmu
$IsBlocked = 1;
154
2021-12-17
jrmu
$WhyBlockedFmt[] = $BlockedMessagesFmt['ip'] . $match[0];
155
2021-12-17
jrmu
}
156
2021-12-17
jrmu
157
2021-12-17
jrmu
## Now we'll load any "block:" or "unblock:" specifications
158
2021-12-17
jrmu
## from the page text.
159
2021-12-17
jrmu
if (preg_match_all('/(un)?(?:block|regex):(.*)/', @$page['text'],
160
2021-12-17
jrmu
$match, PREG_SET_ORDER))
161
2021-12-17
jrmu
foreach($match as $m) $terms[$m[1].'block'][] = trim($m[2]);
162
2021-12-17
jrmu
}
163
2021-12-17
jrmu
164
2021-12-17
jrmu
## okay, we've loaded all of the terms, now subtract any 'unblock'
165
2021-12-17
jrmu
## terms from the block set.
166
2021-12-17
jrmu
StopWatch("Blocklist: diff unblock");
167
2021-12-17
jrmu
$blockterms = array_diff((array)@$terms['block'], (array)@$terms['unblock']);
168
2021-12-17
jrmu
169
2021-12-17
jrmu
## go through each of the remaining blockterms and see if it matches the
170
2021-12-17
jrmu
## text -- if so, disable posting and add a message to $WhyBlockedFmt.
171
2021-12-17
jrmu
StopWatch('Blocklist: blockterms (count='.count($blockterms).')');
172
2021-12-17
jrmu
$itext = strtolower($text);
173
2021-12-17
jrmu
foreach($blockterms as $b) {
174
2021-12-17
jrmu
if ($b[0] == '/') {
175
2021-12-17
jrmu
if (!preg_match($b, $text)) continue;
176
2021-12-17
jrmu
} else if (strpos($itext, strtolower($b)) === false) continue;
177
2021-12-17
jrmu
$EnablePost = 0;
178
2021-12-17
jrmu
$IsBlocked = 1;
179
2021-12-17
jrmu
$WhyBlockedFmt[] = $BlockedMessagesFmt['text'] . $b;
180
2021-12-17
jrmu
}
181
2021-12-17
jrmu
StopWatch('Blocklist: blockterms done');
182
2021-12-17
jrmu
183
2021-12-17
jrmu
## If we came across any reasons to block, let's provide a message
184
2021-12-17
jrmu
## to the author that it was blocked. If $EnableWhyBlocked is set,
185
2021-12-17
jrmu
## we'll even tell the author why. :-)
186
2021-12-17
jrmu
if (@$WhyBlockedFmt) {
187
2021-12-17
jrmu
$MessagesFmt[] = $BlocklistMessageFmt;
188
2021-12-17
jrmu
if (IsEnabled($EnableWhyBlocked, 0))
189
2021-12-17
jrmu
foreach((array)$WhyBlockedFmt as $why)
190
2021-12-17
jrmu
$MessagesFmt[] = "<pre class='blocklistmessage'>$why</pre>\n";
191
2021-12-17
jrmu
}
192
2021-12-17
jrmu
StopWatch("Blocklist: end $pagename");
193
2021-12-17
jrmu
}
194
2021-12-17
jrmu
195
2021-12-17
jrmu
196
2021-12-17
jrmu
## BlocklistDownload() handles retrieving blocklists from
197
2021-12-17
jrmu
## external sources into PmWiki pages. If it's able to
198
2021-12-17
jrmu
## download an updated list, it uses that; otherwise it leaves
199
2021-12-17
jrmu
## any existing list alone.
200
2021-12-17
jrmu
function BlocklistDownload($pagename, $dir = '') {
201
2021-12-17
jrmu
global $BlocklistDownloadFmt, $BlocklistDownload, $FmtV;
202
2021-12-17
jrmu
203
2021-12-17
jrmu
if ($dir) { flush(); chdir($dir); }
204
2021-12-17
jrmu
SDV($BlocklistDownloadFmt, "
205
2021-12-17
jrmu
[@
206
2021-12-17
jrmu
## blocklist-note: NOTE: This page is automatically generated by blocklist.php
207
2021-12-17
jrmu
## blocklist-note: NOTE: Any edits to this page may be lost!
208
2021-12-17
jrmu
## blocklist-url: \$BlocklistDownloadUrl
209
2021-12-17
jrmu
## blocklist-when: \$CurrentTimeISO
210
2021-12-17
jrmu
# blocklist-format: \$BlocklistFormat
211
2021-12-17
jrmu
\$BlocklistData
212
2021-12-17
jrmu
@]
213
2021-12-17
jrmu
");
214
2021-12-17
jrmu
215
2021-12-17
jrmu
## get the existing blocklist page
216
2021-12-17
jrmu
$bd = &$BlocklistDownload[$pagename];
217
2021-12-17
jrmu
$page = ReadPage($pagename, READPAGE_CURRENT);
218
2021-12-17
jrmu
219
2021-12-17
jrmu
## try to retrieve the remote data
220
2021-12-17
jrmu
$blocklistdata = @file($bd['url']);
221
2021-12-17
jrmu
222
2021-12-17
jrmu
## if we didn't get it, and we don't already have text, save a
223
2021-12-17
jrmu
## note in the page so we know what happened
224
2021-12-17
jrmu
if (!$blocklistdata && !@$page['text']) {
225
2021-12-17
jrmu
$auf = ini_get('allow_url_fopen');
226
2021-12-17
jrmu
$blocklistdata = "#### Unable to download blocklist (allow_url_fopen=$auf)";
227
2021-12-17
jrmu
}
228
2021-12-17
jrmu
229
2021-12-17
jrmu
## if we have some new text to save, let's format it and save it
230
2021-12-17
jrmu
if ($blocklistdata) {
231
2021-12-17
jrmu
$blocklistdata = implode('', (array)$blocklistdata);
232
2021-12-17
jrmu
$blocklistdata = preg_replace('/^##blocklist.*/m', '', $blocklistdata);
233
2021-12-17
jrmu
$FmtV['$BlocklistData'] = $blocklistdata;
234
2021-12-17
jrmu
$FmtV['$BlocklistDownloadUrl'] = $bd['url'];
235
2021-12-17
jrmu
$FmtV['$BlocklistFormat'] = $bd['format'];
236
2021-12-17
jrmu
$page['text'] = FmtPageName($BlocklistDownloadFmt, $pagename);
237
2021-12-17
jrmu
SDV($page['passwdread'], '@lock');
238
2021-12-17
jrmu
}
239
2021-12-17
jrmu
240
2021-12-17
jrmu
## save our updated(?) blocklist page
241
2021-12-17
jrmu
WritePage($pagename, $page);
242
2021-12-17
jrmu
}
IRCNow