2023-05-24 20:09:59 +00:00
|
|
|
<?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 IDBAccessObject;
|
|
|
|
use MediaWiki\Extension\AbuseFilter\BlockedDomainStorage;
|
2023-12-10 19:03:19 +00:00
|
|
|
use MediaWiki\Html\Html;
|
2024-06-12 18:01:35 +00:00
|
|
|
use MediaWiki\HTMLForm\HTMLForm;
|
2023-12-10 19:03:19 +00:00
|
|
|
use MediaWiki\SpecialPage\SpecialPage;
|
2023-12-25 20:07:38 +00:00
|
|
|
use MediaWiki\Title\TitleValue;
|
2023-05-24 20:09:59 +00:00
|
|
|
use PermissionsError;
|
2023-06-01 17:10:07 +00:00
|
|
|
use WANObjectCache;
|
2023-05-24 20:09:59 +00:00
|
|
|
|
|
|
|
/**
|
2023-06-14 19:55:30 +00:00
|
|
|
* List and manage blocked external domains
|
2023-05-24 20:09:59 +00:00
|
|
|
*
|
|
|
|
* @ingroup SpecialPage
|
|
|
|
*/
|
|
|
|
class BlockedExternalDomains extends SpecialPage {
|
|
|
|
private BlockedDomainStorage $blockedDomainStorage;
|
2023-06-01 17:10:07 +00:00
|
|
|
private WANObjectCache $wanCache;
|
2023-05-24 20:09:59 +00:00
|
|
|
|
2023-06-01 17:10:07 +00:00
|
|
|
public function __construct(
|
|
|
|
BlockedDomainStorage $blockedDomainStorage,
|
|
|
|
WANObjectCache $wanCache
|
|
|
|
) {
|
2023-05-24 20:09:59 +00:00
|
|
|
parent::__construct( 'BlockedExternalDomains' );
|
|
|
|
$this->blockedDomainStorage = $blockedDomainStorage;
|
2023-06-01 17:10:07 +00:00
|
|
|
$this->wanCache = $wanCache;
|
2023-05-24 20:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @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();
|
2023-09-22 20:43:01 +00:00
|
|
|
$out->setPageTitleMsg( $this->msg( 'abusefilter-blocked-domains-title' ) );
|
2023-05-24 20:09:59 +00:00
|
|
|
$out->wrapWikiMsg( "$1", 'abusefilter-blocked-domains-intro' );
|
|
|
|
|
2023-06-01 13:01:14 +00:00
|
|
|
// Direct editing of this page is blocked via EditPermissionHandler
|
|
|
|
$userCanManage = $this->getAuthority()->isAllowed( 'abusefilter-modify-blocked-external-domains' );
|
2023-05-24 20:09:59 +00:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-14 19:55:30 +00:00
|
|
|
$res = $this->blockedDomainStorage->loadConfig( IDBAccessObject::READ_LATEST );
|
2023-05-24 20:09:59 +00:00
|
|
|
if ( !$res->isGood() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-14 19:55:30 +00:00
|
|
|
$content = Html::element( 'th', [], $this->msg( 'abusefilter-blocked-domains-domain-header' )->text() ) .
|
2023-12-25 20:07:38 +00:00
|
|
|
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()
|
|
|
|
);
|
|
|
|
}
|
2023-06-14 19:55:30 +00:00
|
|
|
$thead = Html::rawElement( 'tr', [], $content );
|
2023-05-24 20:09:59 +00:00
|
|
|
|
2023-06-01 17:10:07 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
);
|
2023-05-24 20:09:59 +00:00
|
|
|
|
|
|
|
$out->addModuleStyles( [ 'jquery.tablesorter.styles', 'mediawiki.pager.styles' ] );
|
|
|
|
$out->addModules( 'jquery.tablesorter' );
|
2023-06-14 19:55:30 +00:00
|
|
|
$out->addHTML( Html::rawElement(
|
2023-05-24 20:09:59 +00:00
|
|
|
'table',
|
|
|
|
[ 'class' => 'mw-datatable sortable' ],
|
2023-06-14 19:55:30 +00:00
|
|
|
Html::rawElement( 'thead', [], $thead ) .
|
|
|
|
Html::rawElement( 'tbody', [], $tbody )
|
2023-05-24 20:09:59 +00:00
|
|
|
) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 = '';
|
2023-06-14 19:55:30 +00:00
|
|
|
$newRow .= Html::rawElement( 'td', [], Html::element( 'code', [], $domain['domain'] ) );
|
2023-05-24 20:09:59 +00:00
|
|
|
|
2023-06-14 19:55:30 +00:00
|
|
|
$newRow .= Html::rawElement( 'td', [], $this->getOutput()->parseInlineAsInterface( $domain['notes'] ) );
|
2023-05-24 20:09:59 +00:00
|
|
|
|
|
|
|
if ( $showManageActions ) {
|
2023-12-25 20:07:38 +00:00
|
|
|
if ( isset( $domain['addedBy'] ) ) {
|
|
|
|
$addedBy = $this->getLinkRenderer()->makeLink(
|
|
|
|
new TitleValue( 3, $domain['addedBy'] ),
|
|
|
|
$domain['addedBy']
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$addedBy = '';
|
|
|
|
}
|
|
|
|
$newRow .= Html::rawElement( 'td', [], $addedBy );
|
|
|
|
|
2023-05-24 20:09:59 +00:00
|
|
|
$actionLink = $this->getLinkRenderer()->makeKnownLink(
|
|
|
|
$this->getPageTitle( 'remove' ),
|
|
|
|
$this->msg( 'abusefilter-blocked-domains-remove' )->text(),
|
|
|
|
[],
|
|
|
|
[ 'domain' => $domain['domain'] ] );
|
2023-06-14 19:55:30 +00:00
|
|
|
$newRow .= Html::rawElement( 'td', [], $actionLink );
|
2023-05-24 20:09:59 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 19:55:30 +00:00
|
|
|
return Html::rawElement( 'tr', [], $newRow ) . "\n";
|
2023-05-24 20:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
2023-09-22 20:43:01 +00:00
|
|
|
$out->setPageTitleMsg( $this->msg( 'abusefilter-blocked-domains-remove-title' ) );
|
2023-05-24 20:09:59 +00:00
|
|
|
$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();
|
2023-06-15 21:17:18 +00:00
|
|
|
$domain = $this->blockedDomainStorage->validateDomain( $data['Domain'] );
|
|
|
|
if ( $domain === false ) {
|
|
|
|
$out->wrapWikiTextAsInterface( 'error', 'Invalid URL' );
|
2023-05-24 20:09:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$rev = $this->blockedDomainStorage->removeDomain(
|
|
|
|
$domain,
|
|
|
|
$data['Notes'] ?? '',
|
|
|
|
$this->getUser()
|
|
|
|
);
|
|
|
|
|
2023-06-12 14:54:13 +00:00
|
|
|
if ( !$rev ) {
|
2023-05-24 20:09:59 +00:00
|
|
|
$out->wrapWikiTextAsInterface( 'error', 'Save failed' );
|
|
|
|
return false;
|
|
|
|
}
|
2023-06-12 14:24:40 +00:00
|
|
|
|
|
|
|
$out->redirect( $this->getPageTitle()->getLocalURL() );
|
|
|
|
return true;
|
2023-05-24 20:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
2023-09-22 20:43:01 +00:00
|
|
|
$out->setPageTitleMsg( $this->msg( "abusefilter-blocked-domains-add-heading" ) );
|
2023-05-24 20:09:59 +00:00
|
|
|
$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();
|
|
|
|
|
2023-06-15 21:17:18 +00:00
|
|
|
$domain = $this->blockedDomainStorage->validateDomain( $data['Domain'] );
|
|
|
|
if ( $domain === false ) {
|
|
|
|
$out->wrapWikiTextAsInterface( 'error', 'Invalid URL' );
|
2023-05-24 20:09:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$rev = $this->blockedDomainStorage->addDomain(
|
|
|
|
$domain,
|
|
|
|
$data['Notes'] ?? '',
|
|
|
|
$this->getUser()
|
|
|
|
);
|
|
|
|
|
2023-06-12 14:54:13 +00:00
|
|
|
if ( !$rev ) {
|
2023-05-24 20:09:59 +00:00
|
|
|
$out->wrapWikiTextAsInterface( 'error', 'Save failed' );
|
|
|
|
return false;
|
|
|
|
}
|
2023-06-12 14:24:40 +00:00
|
|
|
|
|
|
|
$out->redirect( $this->getPageTitle()->getLocalURL() );
|
|
|
|
return true;
|
2023-05-24 20:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
protected function getGroupName() {
|
|
|
|
return 'spam';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isListed() {
|
|
|
|
return $this->getConfig()->get( 'AbuseFilterEnableBlockedExternalDomain' );
|
|
|
|
}
|
|
|
|
}
|