[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/extensions/SpamBlacklist/ -> SpamBlacklistHooks.php (source)

   1  <?php
   2  
   3  /**
   4   * Hooks for the spam blacklist extension
   5   */
   6  class SpamBlacklistHooks {
   7      /**
   8       * Hook function for EditFilterMergedContent
   9       *
  10       * @param IContextSource $context
  11       * @param Content        $content
  12       * @param Status         $status
  13       * @param string         $summary
  14       * @param User           $user
  15       * @param bool           $minoredit
  16       *
  17       * @return bool
  18       */
  19  	static function filterMergedContent( IContextSource $context, Content $content, Status $status, $summary, User $user, $minoredit ) {
  20          $title = $context->getTitle();
  21  
  22          if ( isset( $title->spamBlackListFiltered ) && $title->spamBlackListFiltered ) {
  23              // already filtered
  24              return true;
  25          }
  26  
  27          // get the link from the not-yet-saved page content.
  28          // no need to generate html to get external links
  29          $pout = $content->getParserOutput( $title, null, null, false );
  30          $links = array_keys( $pout->getExternalLinks() );
  31  
  32          // HACK: treat the edit summary as a link
  33          if ( $summary !== '' ) {
  34              $links[] = $summary;
  35          }
  36  
  37          $spamObj = BaseBlacklist::getInstance( 'spam' );
  38          $matches = $spamObj->filter( $links, $title );
  39  
  40          if ( $matches !== false ) {
  41              $status->fatal( 'spamprotectiontext' );
  42  
  43              foreach ( $matches as $match ) {
  44                  $status->fatal( 'spamprotectionmatch', $match );
  45              }
  46          }
  47  
  48          // Always return true, EditPage will look at $status->isOk().
  49          return true;
  50      }
  51  
  52      /**
  53       * Hook function for APIEditBeforeSave.
  54       * This allows blacklist matches to be reported directly in the result structure
  55       * of the API call.
  56       *
  57       * @param $editPage EditPage
  58       * @param $text string
  59       * @param $resultArr array
  60       * @return bool
  61       */
  62  	static function filterAPIEditBeforeSave( $editPage, $text, &$resultArr ) {
  63          $title = $editPage->mArticle->getTitle();
  64  
  65          // get the links from the not-yet-saved page content.
  66          $content = ContentHandler::makeContent(
  67              $text,
  68              $editPage->getTitle(),
  69              $editPage->contentModel,
  70              $editPage->contentFormat
  71          );
  72          $editInfo = $editPage->mArticle->prepareContentForEdit( $content, null, null, $editPage->contentFormat );
  73          $pout = $editInfo->output;
  74          $links = array_keys( $pout->getExternalLinks() );
  75  
  76          // HACK: treat the edit summary as a link
  77          $summary = $editPage->summary;
  78          if ( $summary !== '' ) {
  79              $links[] = $summary;
  80          }
  81  
  82          $spamObj = BaseBlacklist::getInstance( 'spam' );
  83          $matches = $spamObj->filter( $links, $title );
  84  
  85          if ( $matches !== false ) {
  86              $resultArr['spamblacklist'] = implode( '|', $matches );
  87          }
  88  
  89          // mark the title, so filterMergedContent can skip it.
  90          $title->spamBlackListFiltered = true;
  91  
  92          // return convention for hooks is the inverse of $wgFilterCallback
  93          return ( $matches === false );
  94      }
  95  
  96      /**
  97       * Verify that the user can send emails
  98       *
  99       * @param $user User
 100       * @param $hookErr array
 101       * @return bool
 102       */
 103  	public static function userCanSendEmail( &$user, &$hookErr ) {
 104          /** @var $blacklist EmailBlacklist */
 105          $blacklist = BaseBlacklist::getInstance( 'email' );
 106          if ( $blacklist->checkUser( $user ) ) {
 107              return true;
 108          }
 109  
 110          $hookErr = array( 'spam-blacklisted-email', 'spam-blacklisted-email-text', null );
 111  
 112          return false;
 113      }
 114  
 115      /**
 116       * Processes new accounts for valid email addresses
 117       *
 118       * @param $user User
 119       * @param $abortError
 120       * @return bool
 121       */
 122  	public static function abortNewAccount( $user, &$abortError ) {
 123          /** @var $blacklist EmailBlacklist */
 124          $blacklist = BaseBlacklist::getInstance( 'email' );
 125          if ( $blacklist->checkUser( $user ) ) {
 126              return true;
 127          }
 128  
 129          $abortError = wfMessage( 'spam-blacklisted-email-signup' )->escaped();
 130          return false;
 131      }
 132  
 133      /**
 134       * Hook function for EditFilter
 135       * Confirm that a local blacklist page being saved is valid,
 136       * and toss back a warning to the user if it isn't.
 137       *
 138       * @param $editPage EditPage
 139       * @param $text string
 140       * @param $section string
 141       * @param $hookError string
 142       * @return bool
 143       */
 144  	static function validate( $editPage, $text, $section, &$hookError ) {
 145          $thisPageName = $editPage->mTitle->getPrefixedDBkey();
 146  
 147          if( !BaseBlacklist::isLocalSource( $editPage->mTitle ) ) {
 148              wfDebugLog( 'SpamBlacklist', "Spam blacklist validator: [[$thisPageName]] not a local blacklist\n" );
 149              return true;
 150          }
 151  
 152          $type = BaseBlacklist::getTypeFromTitle( $editPage->mTitle );
 153          if ( $type === false ) {
 154              return true;
 155          }
 156  
 157          $lines = explode( "\n", $text );
 158  
 159          $badLines = SpamRegexBatch::getBadLines( $lines, BaseBlacklist::getInstance( $type ) );
 160          if( $badLines ) {
 161              wfDebugLog( 'SpamBlacklist', "Spam blacklist validator: [[$thisPageName]] given invalid input lines: " .
 162                  implode( ', ', $badLines ) . "\n" );
 163  
 164              $badList = "*<code>" .
 165                  implode( "</code>\n*<code>",
 166                      array_map( 'wfEscapeWikiText', $badLines ) ) .
 167                  "</code>\n";
 168              $hookError =
 169                  "<div class='errorbox'>" .
 170                      wfMessage( 'spam-invalid-lines' )->numParams( $badLines )->text() . "<br />" .
 171                      $badList .
 172                      "</div>\n" .
 173                      "<br clear='all' />\n";
 174          } else {
 175              wfDebugLog( 'SpamBlacklist', "Spam blacklist validator: [[$thisPageName]] ok or empty blacklist\n" );
 176          }
 177  
 178          return true;
 179      }
 180  
 181      /**
 182       * Hook function for PageContentSaveComplete
 183       * Clear local spam blacklist caches on page save.
 184       *
 185       * @param Page $wikiPage
 186       * @param User     $user
 187       * @param Content  $content
 188       * @param string   $summary
 189       * @param bool     $isMinor
 190       * @param bool     $isWatch
 191       * @param string   $section
 192       * @param int      $flags
 193       * @param int      $revision
 194       * @param Status   $status
 195       * @param int      $baseRevId
 196       *
 197       * @return bool
 198       */
 199  	static function pageSaveContent(
 200          Page $wikiPage,
 201          User $user,
 202          Content $content,
 203          $summary,
 204          $isMinor,
 205          $isWatch,
 206          $section,
 207          $flags,
 208          $revision,
 209          Status $status,
 210          $baseRevId
 211      ) {
 212          if( !BaseBlacklist::isLocalSource( $wikiPage->getTitle() ) ) {
 213              return true;
 214          }
 215          global $wgMemc, $wgDBname;
 216  
 217          // This sucks because every Blacklist needs to be cleared
 218          foreach ( BaseBlacklist::getBlacklistTypes() as $type => $class ) {
 219              $wgMemc->delete( "$wgDBname:{$type}_blacklist_regexes" );
 220          }
 221          return true;
 222      }
 223  }


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1