2012-01-17 06:13:46 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hooks for the spam blacklist extension
|
|
|
|
*/
|
|
|
|
class SpamBlacklistHooks {
|
|
|
|
/**
|
2013-07-25 14:05:13 +00:00
|
|
|
* Hook function for EditFilterMergedContent
|
|
|
|
*
|
|
|
|
* @param IContextSource $context
|
|
|
|
* @param Content $content
|
|
|
|
* @param Status $status
|
|
|
|
* @param string $summary
|
|
|
|
* @param User $user
|
|
|
|
* @param bool $minoredit
|
2012-01-17 06:13:46 +00:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2013-07-25 14:05:13 +00:00
|
|
|
static function filterMergedContent( IContextSource $context, Content $content, Status $status, $summary, User $user, $minoredit ) {
|
|
|
|
$title = $context->getTitle();
|
|
|
|
|
|
|
|
if ( isset( $title->spamBlackListFiltered ) && $title->spamBlackListFiltered ) {
|
|
|
|
// already filtered
|
2012-01-17 06:13:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-25 14:05:13 +00:00
|
|
|
// get the link from the not-yet-saved page content.
|
2014-01-07 21:56:49 +00:00
|
|
|
$pout = $content->getParserOutput( $title );
|
2013-07-25 14:05:13 +00:00
|
|
|
$links = array_keys( $pout->getExternalLinks() );
|
|
|
|
|
|
|
|
// HACK: treat the edit summary as a link
|
|
|
|
if ( $summary !== '' ) {
|
|
|
|
$links[] = $summary;
|
|
|
|
}
|
|
|
|
|
2012-01-17 06:13:46 +00:00
|
|
|
$spamObj = BaseBlacklist::getInstance( 'spam' );
|
2013-07-25 14:05:13 +00:00
|
|
|
$matches = $spamObj->filter( $links, $title );
|
|
|
|
|
|
|
|
if ( $matches !== false ) {
|
|
|
|
$status->fatal( 'spamprotectiontext' );
|
|
|
|
|
|
|
|
foreach ( $matches as $match ) {
|
|
|
|
$status->fatal( 'spamprotectionmatch', $match );
|
|
|
|
}
|
2012-01-17 06:13:46 +00:00
|
|
|
}
|
2013-07-25 14:05:13 +00:00
|
|
|
|
|
|
|
// Always return true, EditPage will look at $status->isOk().
|
|
|
|
return true;
|
2012-01-17 06:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-07-25 14:05:13 +00:00
|
|
|
* Hook function for APIEditBeforeSave.
|
|
|
|
* This allows blacklist matches to be reported directly in the result structure
|
|
|
|
* of the API call.
|
2012-01-17 06:13:46 +00:00
|
|
|
*
|
|
|
|
* @param $editPage EditPage
|
|
|
|
* @param $text string
|
|
|
|
* @param $resultArr array
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
static function filterAPIEditBeforeSave( $editPage, $text, &$resultArr ) {
|
|
|
|
$title = $editPage->mArticle->getTitle();
|
2013-07-25 14:05:13 +00:00
|
|
|
|
|
|
|
// get the links from the not-yet-saved page content.
|
|
|
|
$content = ContentHandler::makeContent(
|
|
|
|
$text,
|
|
|
|
$editPage->getTitle(),
|
|
|
|
$editPage->contentModel,
|
|
|
|
$editPage->contentFormat
|
|
|
|
);
|
|
|
|
$editInfo = $editPage->mArticle->prepareContentForEdit( $content, null, null, $editPage->contentFormat );
|
|
|
|
$pout = $editInfo->output;
|
|
|
|
$links = array_keys( $pout->getExternalLinks() );
|
|
|
|
|
|
|
|
// HACK: treat the edit summary as a link
|
|
|
|
$summary = $editPage->summary;
|
|
|
|
if ( $summary !== '' ) {
|
|
|
|
$links[] = $summary;
|
|
|
|
}
|
|
|
|
|
|
|
|
$spamObj = BaseBlacklist::getInstance( 'spam' );
|
|
|
|
$matches = $spamObj->filter( $links, $title );
|
|
|
|
|
|
|
|
if ( $matches !== false ) {
|
|
|
|
$resultArr['spamblacklist'] = implode( '|', $matches );
|
2012-01-17 06:13:46 +00:00
|
|
|
}
|
2013-07-25 14:05:13 +00:00
|
|
|
|
|
|
|
// mark the title, so filterMergedContent can skip it.
|
|
|
|
$title->spamBlackListFiltered = true;
|
|
|
|
|
|
|
|
// return convention for hooks is the inverse of $wgFilterCallback
|
|
|
|
return ( $matches === false );
|
2012-01-17 06:13:46 +00:00
|
|
|
}
|
|
|
|
|
2012-01-18 23:29:37 +00:00
|
|
|
/**
|
|
|
|
* Verify that the user can send emails
|
|
|
|
*
|
|
|
|
* @param $user User
|
|
|
|
* @param $hookErr array
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function userCanSendEmail( &$user, &$hookErr ) {
|
|
|
|
/** @var $blacklist EmailBlacklist */
|
|
|
|
$blacklist = BaseBlacklist::getInstance( 'email' );
|
|
|
|
if ( $blacklist->checkUser( $user ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$hookErr = array( 'spam-blacklisted-email', 'spam-blacklisted-email-text', null );
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-02-27 11:50:47 +00:00
|
|
|
* Processes new accounts for valid email addresses
|
2012-01-18 23:29:37 +00:00
|
|
|
*
|
|
|
|
* @param $user User
|
|
|
|
* @param $abortError
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function abortNewAccount( $user, &$abortError ) {
|
|
|
|
/** @var $blacklist EmailBlacklist */
|
|
|
|
$blacklist = BaseBlacklist::getInstance( 'email' );
|
|
|
|
if ( $blacklist->checkUser( $user ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$abortError = wfMessage( 'spam-blacklisted-email-signup' )->escaped();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-17 06:13:46 +00:00
|
|
|
/**
|
|
|
|
* Hook function for EditFilter
|
|
|
|
* Confirm that a local blacklist page being saved is valid,
|
|
|
|
* and toss back a warning to the user if it isn't.
|
|
|
|
*
|
|
|
|
* @param $editPage EditPage
|
|
|
|
* @param $text string
|
|
|
|
* @param $section string
|
|
|
|
* @param $hookError string
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
static function validate( $editPage, $text, $section, &$hookError ) {
|
|
|
|
$thisPageName = $editPage->mTitle->getPrefixedDBkey();
|
|
|
|
|
|
|
|
if( !BaseBlacklist::isLocalSource( $editPage->mTitle ) ) {
|
|
|
|
wfDebugLog( 'SpamBlacklist', "Spam blacklist validator: [[$thisPageName]] not a local blacklist\n" );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-18 23:29:37 +00:00
|
|
|
$type = BaseBlacklist::getTypeFromTitle( $editPage->mTitle );
|
|
|
|
if ( $type === false ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-17 06:13:46 +00:00
|
|
|
$lines = explode( "\n", $text );
|
|
|
|
|
2012-01-18 23:29:37 +00:00
|
|
|
$badLines = SpamRegexBatch::getBadLines( $lines, BaseBlacklist::getInstance( $type ) );
|
2012-01-17 06:13:46 +00:00
|
|
|
if( $badLines ) {
|
|
|
|
wfDebugLog( 'SpamBlacklist', "Spam blacklist validator: [[$thisPageName]] given invalid input lines: " .
|
|
|
|
implode( ', ', $badLines ) . "\n" );
|
|
|
|
|
2012-09-02 15:41:39 +00:00
|
|
|
$badList = "*<code>" .
|
|
|
|
implode( "</code>\n*<code>",
|
2012-01-17 06:13:46 +00:00
|
|
|
array_map( 'wfEscapeWikiText', $badLines ) ) .
|
2012-09-02 15:41:39 +00:00
|
|
|
"</code>\n";
|
2012-01-17 06:13:46 +00:00
|
|
|
$hookError =
|
|
|
|
"<div class='errorbox'>" .
|
2012-09-02 15:41:39 +00:00
|
|
|
wfMessage( 'spam-invalid-lines' )->numParams( $badLines )->text() . "<br />" .
|
2012-01-17 06:13:46 +00:00
|
|
|
$badList .
|
|
|
|
"</div>\n" .
|
|
|
|
"<br clear='all' />\n";
|
|
|
|
} else {
|
|
|
|
wfDebugLog( 'SpamBlacklist', "Spam blacklist validator: [[$thisPageName]] ok or empty blacklist\n" );
|
|
|
|
}
|
2012-01-18 23:29:37 +00:00
|
|
|
|
|
|
|
return true;
|
2012-01-17 06:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-07-25 14:05:13 +00:00
|
|
|
* Hook function for PageContentSaveComplete
|
2012-01-17 06:13:46 +00:00
|
|
|
* Clear local spam blacklist caches on page save.
|
|
|
|
*
|
2013-07-25 14:05:13 +00:00
|
|
|
* @param Page $wikiPage
|
|
|
|
* @param User $user
|
|
|
|
* @param Content $content
|
|
|
|
* @param string $summary
|
|
|
|
* @param bool $isMinor
|
|
|
|
* @param bool $isWatch
|
|
|
|
* @param string $section
|
|
|
|
* @param int $flags
|
|
|
|
* @param int $revision
|
|
|
|
* @param Status $status
|
|
|
|
* @param int $baseRevId
|
|
|
|
*
|
2012-01-17 06:13:46 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2013-07-25 14:05:13 +00:00
|
|
|
static function pageSaveContent(
|
|
|
|
Page $wikiPage,
|
|
|
|
User $user,
|
|
|
|
Content $content,
|
|
|
|
$summary,
|
|
|
|
$isMinor,
|
|
|
|
$isWatch,
|
|
|
|
$section,
|
|
|
|
$flags,
|
|
|
|
$revision,
|
|
|
|
Status $status,
|
|
|
|
$baseRevId
|
|
|
|
) {
|
|
|
|
if( !BaseBlacklist::isLocalSource( $wikiPage->getTitle() ) ) {
|
2012-02-15 14:58:26 +00:00
|
|
|
return true;
|
2012-01-17 06:13:46 +00:00
|
|
|
}
|
|
|
|
global $wgMemc, $wgDBname;
|
|
|
|
|
|
|
|
// This sucks because every Blacklist needs to be cleared
|
|
|
|
foreach ( BaseBlacklist::getBlacklistTypes() as $type => $class ) {
|
|
|
|
$wgMemc->delete( "$wgDBname:{$type}_blacklist_regexes" );
|
|
|
|
}
|
2012-02-02 22:12:43 +00:00
|
|
|
return true;
|
2012-01-17 06:13:46 +00:00
|
|
|
}
|
|
|
|
}
|