1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace MediaWiki\Extension\AbuseFilter\Special;
use ErrorPageError;
use MediaWiki\Extension\AbuseFilter\BlockedDomainStorage;
use MediaWiki\Html\Html;
use MediaWiki\HTMLForm\HTMLForm;
use MediaWiki\SpecialPage\SpecialPage;
use MediaWiki\Title\TitleValue;
use PermissionsError;
use Wikimedia\ObjectCache\WANObjectCache;
use Wikimedia\Rdbms\IDBAccessObject;
/**
* List and manage blocked external domains
*
* @ingroup SpecialPage
*/
class BlockedExternalDomains extends SpecialPage {
private BlockedDomainStorage $blockedDomainStorage;
private WANObjectCache $wanCache;
public function __construct(
BlockedDomainStorage $blockedDomainStorage,
WANObjectCache $wanCache
) {
parent::__construct( 'BlockedExternalDomains' );
$this->blockedDomainStorage = $blockedDomainStorage;
$this->wanCache = $wanCache;
}
/** @inheritDoc */
public function execute( $par ) {
if ( !$this->getConfig()->get( 'AbuseFilterEnableBlockedExternalDomain' ) ) {
throw new ErrorPageError( 'abusefilter-disabled', 'disabledspecialpage-disabled' );
}
$this->setHeaders();
$this->outputHeader();
$this->addHelpLink( 'Manual:BlockedExternalDomains' );
$request = $this->getRequest();
switch ( $par ) {
case 'remove':
$this->showRemoveForm( $request->getVal( 'domain' ) );
break;
case 'add':
$this->showAddForm( $request->getVal( 'domain' ) );
break;
default:
$this->showList();
break;
}
}
private function showList() {
$out = $this->getOutput();
$out->setPageTitleMsg( $this->msg( 'abusefilter-blocked-domains-title' ) );
$out->wrapWikiMsg( "$1", 'abusefilter-blocked-domains-intro' );
// Direct editing of this page is blocked via EditPermissionHandler
$userCanManage = $this->getAuthority()->isAllowed( 'abusefilter-modify-blocked-external-domains' );
// Show form to add a blocked domain
if ( $userCanManage ) {
$fields = [
'Domain' => [
'type' => 'text',
'label' => $this->msg( 'abusefilter-blocked-domains-domain' )->plain(),
'required' => true,
],
'Notes' => [
'type' => 'text',
'maxlength' => 255,
'label' => $this->msg( 'abusefilter-blocked-domains-notes' )->plain(),
'size' => 250,
],
];
HTMLForm::factory( 'ooui', $fields, $this->getContext() )
->setAction( $this->getPageTitle( 'add' )->getLocalURL() )
->setWrapperLegendMsg( 'abusefilter-blocked-domains-add-heading' )
->setHeaderHtml( $this->msg( 'abusefilter-blocked-domains-add-explanation' )->parseAsBlock() )
->setSubmitCallback( [ $this, 'processAddForm' ] )
->setSubmitTextMsg( 'abusefilter-blocked-domains-add-submit' )
->show();
if ( $out->getRedirect() !== '' ) {
return;
}
}
$res = $this->blockedDomainStorage->loadConfig( IDBAccessObject::READ_LATEST );
if ( !$res->isGood() ) {
return;
}
$content = Html::element( 'th', [], $this->msg( 'abusefilter-blocked-domains-domain-header' )->text() ) .
Html::element( 'th', [], $this->msg( 'abusefilter-blocked-domains-notes-header' )->text() );
if ( $userCanManage ) {
$content .= Html::element(
'th',
[],
$this->msg( 'abusefilter-blocked-domains-addedby-header' )->text()
);
$content .= Html::element(
'th',
[ 'class' => 'unsortable' ],
$this->msg( 'abusefilter-blocked-domains-actions-header' )->text()
);
}
$thead = Html::rawElement( 'tr', [], $content );
// Parsing each row is expensive, put it behind WAN cache
// with md5 checksum, we make sure changes to the domain list
// invalidate the cache
$cacheKey = $this->wanCache->makeKey(
'abuse-filter-special-blocked-external-domains-rows',
md5( json_encode( $res->getValue() ) ),
(int)$userCanManage
);
$tbody = $this->wanCache->getWithSetCallback(
$cacheKey,
WANObjectCache::TTL_DAY,
function () use ( $res, $userCanManage ) {
$tbody = '';
foreach ( $res->getValue() as $domain ) {
$tbody .= $this->doDomainRow( $domain, $userCanManage );
}
return $tbody;
}
);
$out->addModuleStyles( [ 'jquery.tablesorter.styles', 'mediawiki.pager.styles' ] );
$out->addModules( 'jquery.tablesorter' );
$out->addHTML( Html::rawElement(
'table',
[ 'class' => 'mw-datatable sortable' ],
Html::rawElement( 'thead', [], $thead ) .
Html::rawElement( 'tbody', [], $tbody )
) );
}
/**
* Show the row in the table
*
* @param array $domain domain data
* @param bool $showManageActions whether to add manage actions
* @return string HTML for the row
*/
private function doDomainRow( $domain, $showManageActions ) {
$newRow = Html::rawElement( 'td', [], Html::element( 'code', [], $domain['domain'] ) );
$newRow .= Html::rawElement( 'td', [], $this->getOutput()->parseInlineAsInterface( $domain['notes'] ) );
if ( $showManageActions ) {
if ( isset( $domain['addedBy'] ) ) {
$addedBy = $this->getLinkRenderer()->makeLink(
new TitleValue( 3, $domain['addedBy'] ),
$domain['addedBy']
);
} else {
$addedBy = '';
}
$newRow .= Html::rawElement( 'td', [], $addedBy );
$actionLink = $this->getLinkRenderer()->makeKnownLink(
$this->getPageTitle( 'remove' ),
$this->msg( 'abusefilter-blocked-domains-remove' )->text(),
[],
[ 'domain' => $domain['domain'] ]
);
$newRow .= Html::rawElement( 'td', [], $actionLink );
}
return Html::rawElement( 'tr', [], $newRow ) . "\n";
}
/**
* Show form for removing a domain from the blocked list
*
* @param string $domain
* @return void
*/
private function showRemoveForm( $domain ) {
if ( !$this->getAuthority()->isAllowed( 'editsitejson' ) ) {
throw new PermissionsError( 'editsitejson' );
}
$out = $this->getOutput();
$out->setPageTitleMsg( $this->msg( 'abusefilter-blocked-domains-remove-title' ) );
$out->addBacklinkSubtitle( $this->getPageTitle() );
$preText = $this->msg( 'abusefilter-blocked-domains-remove-explanation-initial', $domain )->parseAsBlock();
$fields = [
'Domain' => [
'type' => 'text',
'label' => $this->msg( 'abusefilter-blocked-domains-domain' )->plain(),
'required' => true,
'default' => $domain,
],
'Notes' => [
'type' => 'text',
'maxlength' => 255,
'label' => $this->msg( 'abusefilter-blocked-domains-notes' )->plain(),
'size' => 250,
],
];
HTMLForm::factory( 'ooui', $fields, $this->getContext() )
->setAction( $this->getPageTitle( 'remove' )->getLocalURL() )
->setSubmitCallback( function ( $data, $form ) {
return $this->processRemoveForm( $data, $form );
} )
->setSubmitTextMsg( 'abusefilter-blocked-domains-remove-submit' )
->setSubmitDestructive()
->addPreHtml( $preText )
->show();
}
/**
* Process the form for removing a domain from the blocked list
*
* @param array $data request data
* @param HTMLForm $form
* @return bool whether the action was successful or not
*/
public function processRemoveForm( array $data, HTMLForm $form ) {
$out = $form->getContext()->getOutput();
$domain = $this->blockedDomainStorage->validateDomain( $data['Domain'] );
if ( $domain === false ) {
$out->wrapWikiTextAsInterface( 'error', 'Invalid URL' );
return false;
}
$rev = $this->blockedDomainStorage->removeDomain(
$domain,
$data['Notes'] ?? '',
$this->getUser()
);
if ( !$rev ) {
$out->wrapWikiTextAsInterface( 'error', 'Save failed' );
return false;
}
$out->redirect( $this->getPageTitle()->getLocalURL() );
return true;
}
/**
* Show form for adding a domain to the blocked list
*
* @param string $domain
* @return void
*/
private function showAddForm( $domain ) {
if ( !$this->getAuthority()->isAllowed( 'editsitejson' ) ) {
throw new PermissionsError( 'editsitejson' );
}
$out = $this->getOutput();
$out->setPageTitleMsg( $this->msg( "abusefilter-blocked-domains-add-heading" ) );
$out->addBacklinkSubtitle( $this->getPageTitle() );
$preText = $this->msg( "abusefilter-blocked-domains-add-explanation", $domain )->parseAsBlock();
$fields = [
'Domain' => [
'type' => 'text',
'label' => $this->msg( 'abusefilter-blocked-domains-domain' )->plain(),
'required' => true,
'default' => $domain,
],
'Notes' => [
'type' => 'text',
'maxlength' => 255,
'label' => $this->msg( 'abusefilter-blocked-domains-notes' )->plain(),
'size' => 250,
],
];
HTMLForm::factory( 'ooui', $fields, $this->getContext() )
->setAction( $this->getPageTitle( 'add' )->getLocalURL() )
->setSubmitCallback( function ( $data, $form ) {
return $this->processAddForm( $data, $form );
} )
->setSubmitTextMsg( "abusefilter-blocked-domains-add-submit" )
->addPreHtml( $preText )
->show();
}
/**
* Process the form for adding a domain to the blocked list
*
* @param array $data request data
* @param HTMLForm $form
* @return bool whether the action was successful or not
*/
private function processAddForm( array $data, HTMLForm $form ) {
$out = $form->getContext()->getOutput();
$domain = $this->blockedDomainStorage->validateDomain( $data['Domain'] );
if ( $domain === false ) {
$out->wrapWikiTextAsInterface( 'error', 'Invalid URL' );
return false;
}
$rev = $this->blockedDomainStorage->addDomain(
$domain,
$data['Notes'] ?? '',
$this->getUser()
);
if ( !$rev ) {
$out->wrapWikiTextAsInterface( 'error', 'Save failed' );
return false;
}
$out->redirect( $this->getPageTitle()->getLocalURL() );
return true;
}
/** @inheritDoc */
protected function getGroupName() {
return 'spam';
}
/** @inheritDoc */
public function isListed() {
return $this->getConfig()->get( 'AbuseFilterEnableBlockedExternalDomain' );
}
}
|