-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace MediaWiki\Extension\UnifiedExtensionForFemiwiki\HookHandlers; | ||
|
||
use Config; | ||
use MediaWiki\Block\DatabaseBlockStore; | ||
use MediaWiki\Extension\SpamBlacklist\BaseBlacklist; | ||
use MediaWiki\User\User; | ||
use Wikimedia\AtEase\AtEase; | ||
use Wikimedia\Rdbms\ILoadBalancer; | ||
|
||
class SpamEmail implements | ||
\MediaWiki\Hook\IsValidEmailAddrHook | ||
{ | ||
|
||
/** @var Config */ | ||
private $config; | ||
|
||
/** @var ILoadBalancer */ | ||
private $loadBalancer; | ||
|
||
/** @var DatabaseBlockStore */ | ||
private $databaseBlockStore; | ||
|
||
/** | ||
* @param Config $config | ||
* @param ILoadBalancer $loadBalancer | ||
* @param DatabaseBlockStore $databaseBlockStore | ||
*/ | ||
public function __construct( | ||
Config $config, | ||
ILoadBalancer $loadBalancer, | ||
DatabaseBlockStore $databaseBlockStore | ||
) { | ||
$this->config = $config; | ||
$this->loadBalancer = $loadBalancer; | ||
$this->databaseBlockStore = $databaseBlockStore; | ||
} | ||
|
||
/** @inheritDoc */ | ||
public function onIsValidEmailAddr( $addr, &$result ) { | ||
if ( !$this->config->get( 'UnifiedExtensionForFemiwikiBlockByEmail' ) ) { | ||
return true; | ||
} | ||
|
||
// Check againt MediaWiki:Email-blacklist | ||
$denylist = BaseBlacklist::getEmailBlacklist()->getBlacklists(); | ||
foreach ( $denylist as $regex ) { | ||
AtEase::suppressWarnings(); | ||
$match = preg_match( $regex, $addr ); | ||
AtEase::restoreWarnings(); | ||
if ( $match ) { | ||
return false; | ||
} | ||
} | ||
|
||
// Check email addresses of block users | ||
if ( version_compare( '1.42', MW_VERSION, '<=' ) ) { | ||
$emails = array_filter( array_unique( array_map( | ||
$this->databaseBlockStore->newListFromConds( [ 'bt_user IS NOT NULL' ] ), | ||
static fn( $block ) => User::newFromIdentity( $block->getBlocker() )->getEmail() | ||
) ) ); | ||
} else { | ||
$dbr = $this->loadBalancer->getConnectionRef( ILoadBalancer::DB_REPLICA ); | ||
$emails = $dbr->newSelectQueryBuilder() | ||
->fields( [ 'user_email' ] ) | ||
->tables( [ 'ipblocks' ] ) | ||
->join( 'user', 'user_id = ipb_user' ) | ||
->conds( [ | ||
'ipb_user IS NOT NULL', | ||
"ipb_expiry > " . $dbr->addQuotes( $dbr->timestamp() ), | ||
] ) | ||
->fetchFieldValues(); | ||
$emails = array_filter(array_unique($emails)); | ||
} | ||
if ( in_array( $addr, $emails ) ) { | ||
return false; | ||
} | ||
} | ||
} |