Legoktm has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/101473


Change subject: Initial commit
......................................................................

Initial commit

Change-Id: If2f9383b679041726f4ab97308bffbd9935e5cd8
---
A StopForumSpam.body.php
A StopForumSpam.hooks.php
A StopForumSpam.i18n.php
A StopForumSpam.php
A ext.SFS.formhack.js
5 files changed, 292 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/StopForumSpam 
refs/changes/73/101473/1

diff --git a/StopForumSpam.body.php b/StopForumSpam.body.php
new file mode 100644
index 0000000..6c08bcd
--- /dev/null
+++ b/StopForumSpam.body.php
@@ -0,0 +1,127 @@
+<?php
+
+class StopForumSpam {
+
+       /**
+        * Actual submission process to stopforumspam.com
+        * @param User $user our bad spammer to report
+        * @return bool indicating success of operation
+        */
+       public static function submit( User $user ) {
+               global $wgSFSAPIKey;
+
+               if ( !$user->isLoggedIn() ) {
+                       return false;
+               }
+
+               $ips = self::getUserIPs( $user );
+               if ( $ips === false ) {
+                       wfDebugLog( 'StopForumSpam', 'IP info is not stored in 
recentchanges nor CheckUser extension, bailing' );
+                       return false;
+               } elseif( !$ips ) {
+                       wfDebugLog( 'StopForumSpam', "Couldn't find any IPs for 
\"{$user->getName()}\"");
+                       return false;
+               }
+
+               $email = $user->getEmail();
+               if ( !$email ) {
+                       wfDebugLog( 'StopForumSpam', "User:{$user->getName()} 
does not have an email associated with the account.");
+                       return false;
+               }
+
+
+               $params = array(
+                       'username' => $user->getName(),
+                       'email' => $email,
+                       'evidence' => self::getUserEvidenceLink( $user ),
+                       'api_key' => $wgSFSAPIKey,
+               );
+               // Each IP must be submitted separately I guess.
+               // @todo find a way to batch submit.
+               foreach ( $ips as $ip ) {
+                       $params['ip_addr'] = $ip;
+                       $url = wfAppendQuery( 
'http://www.stopforumspam.com/add.php', $params );
+                       $req = MWHttpRequest::factory( $url );
+                       $req->execute();
+                       $json = $req->getContent();
+                       $decoded = FormatJson::decode( $json, true );
+                       wfDebugLog( 'StopForumSpam', "Sent data for 
{$user->getName()}/{$ip}" );
+               }
+
+               return true;
+       }
+
+       /**
+        * Our evidence is the user's contributions. Though they've
+        * probably been deleted by now...
+        * @param User $user
+        * @return String
+        */
+       public static function getUserEvidenceLink( User $user ) {
+               return SpecialPage::getTitleFor( 'Contributions', 
$user->getName() )
+                       ->getFullURL( '', false, PROTO_HTTPS );
+       }
+
+       /**
+        * @param User $user
+        * @return array|bool
+        */
+       public static function getUserIPs( User $user ) {
+               global $wgPutIPinRC;
+               if ( $wgPutIPinRC ) {
+                       return self::getUserIPsFromRC( $user );
+               } elseif ( class_exists( 'CheckUser' ) ) {
+                       return self::getUserIPsFromCU( $user );
+               } else {
+                       return false;
+               }
+       }
+
+       /**
+        * Uses the rc_ip field in recentchanges
+        * @param User $user
+        * @return array
+        */
+       public static function getUserIPsFromRC( User $user ) {
+               $dbr = wfGetDB( DB_SLAVE );
+               $rows = $dbr->select(
+                       'recentchanges',
+                       array( 'DISTINCT(rc_ip)' ),
+                       array( 'rc_user' => $user->getId() ),
+                       __METHOD__
+               );
+
+               $ips = array();
+               foreach ( $rows as $row ) {
+                       if ( !in_array( $row->rc_ip, $ips ) ) {
+                               $ips[] = $row->rc_ip;
+                       }
+               }
+
+               return $ips;
+       }
+
+       /**
+        * Uses the cuc_ip field in Checkuser
+        * @param User $user
+        * @return array|bool
+        */
+       public static function getUserIPsFromCU( User $user ) {
+               $dbr = wfGetDB( DB_SLAVE );
+               $rows = $dbr->select(
+                       'cu_changes',
+                       array( 'DISTINCT(cuc_ip)' ),
+                       array( 'cuc_user' => $user->getId() ),
+                       __METHOD__
+               );
+
+               $ips = array();
+               foreach ( $rows as $row ) {
+                       if ( !in_array( $row->cuc_ip, $ips ) ) {
+                               $ips[] = $row->cuc_ip;
+                       }
+               }
+
+               return $ips;
+       }
+}
diff --git a/StopForumSpam.hooks.php b/StopForumSpam.hooks.php
new file mode 100644
index 0000000..7c7b5e0
--- /dev/null
+++ b/StopForumSpam.hooks.php
@@ -0,0 +1,48 @@
+<?php
+
+
+class SFSHooks {
+
+       /**
+        * Some JS to add our checkbox
+        * @param HTMLForm $form
+        * @return bool
+        */
+       public static function onSpecialBlockBeforeFormDisplay( HTMLForm $form 
) {
+               if ( $form->getUser()->isAllowed( 'stopforumspam' ) ) {
+                       $form->getOutput()->addModules( 'ext.SFS.formhack' );
+               }
+
+               return true;
+       }
+
+       /**
+        * Triggers the data submission process
+        * @param Block $block
+        * @param User $user who made the block
+        * @return bool
+        */
+       public static function onBlockIpComplete( Block $block, User $user ) {
+               $context = RequestContext::getMain();
+               $title = $context->getTitle();
+               $request = $context->getRequest();
+               if ( $title && $title->isSpecial( 'Block' ) ) {
+                       if ( $user->isAllowed( 'stopforumspam' ) && 
$request->getBool( 'wpSFS' ) ) {
+                               $target = $block->getTarget();
+                               if ( $target instanceof User ) {
+                                       StopForumSpam::submit( $target );
+                               } else {
+                                       $target = User::newFromName( $target );
+                                       if ( $target && $target->exists() ) {
+                                               StopForumSpam::submit( $target 
);
+                                       } else {
+                                               wfDebug( "Could not detect 
valid user from \"{$block->getTarget()}\"" );
+                                       }
+                               }
+
+                       }
+               }
+
+               return true;
+       }
+}
\ No newline at end of file
diff --git a/StopForumSpam.i18n.php b/StopForumSpam.i18n.php
new file mode 100644
index 0000000..82c7035
--- /dev/null
+++ b/StopForumSpam.i18n.php
@@ -0,0 +1,19 @@
+<?php
+
+$messages = array();
+
+/** English
+ * @author Kunal Mehta
+ */
+$messages['en'] = array(
+       'stopforumspam-desc' => 'Allows administrators to send data to 
[http://stopforumspam.com/ stopforumspam.com]',
+       'stopforumspam-checkbox' => 'Send user information to 
stopforumspam.com',
+);
+
+/** Message documentation (Message documentation)
+ * @author Kunal Mehta
+ */
+$messages['qqq'] = array(
+       'stopforumspam-desc' => 
'{{desc|name=StopForumSpam|url=http://www.mediawiki.org/wiki/Extension:StopForumSpam}}',
+       'stopforumspam-checkbox' => 'Checkbox on Special:Block for 
administrators to submit a user\'s information',
+);
diff --git a/StopForumSpam.php b/StopForumSpam.php
new file mode 100644
index 0000000..ab2664b
--- /dev/null
+++ b/StopForumSpam.php
@@ -0,0 +1,51 @@
+<?php
+
+if ( !defined( 'MEDIAWIKI' ) ) {
+       die;
+}
+
+/**
+ * StopForumSpam.com is a website dedicated to
+ * stopping spam. This extension helps
+ * by contributing data when an administrator
+ * blocks a user.
+ *
+ * @see http://stopforumspam.com/faq
+ * @author Kunal Mehta <lego...@gmail.com>
+ * @license GPL v2 or higher
+ */
+
+/**
+ * Your API key for stopforumspam.com
+ * @see http://stopforumspam.com/signup
+ */
+$wgSFSAPIKey = '';
+
+$wgExtensionCredits['antispam'][] = array(
+       'path' => __FILE__,
+       'name' => 'StopForumSpam',
+       'author' => 'Kunal Mehta',
+       'url' => 'https://www.mediawiki.org/wiki/Extension:StopForumSpam',
+       'descriptionmsg' => 'stopforumspam-desc',
+       'version' => '0.1',
+);
+
+$wgAutoloadClasses['SFSHooks'] = __DIR__ . '/StopForumSpam.hooks.php';
+$wgAutoloadClasses['StopForumSpam'] = __DIR__ . '/StopForumSpam.body.php';
+
+$wgHooks['SpecialBlockBeforeFormDisplay'][] = 
'SFSHooks::onSpecialBlockBeforeFormDisplay';
+$wgHooks['BlockIpComplete'][] = 'SFSHooks::onBlockIpComplete';
+
+$wgResourceModules['ext.SFS.formhack'] = array(
+       'scripts' => array(
+               'ext.SFS.formhack.js',
+       ),
+       'dependencies' => array(
+               'mediawiki.jqueryMsg',
+               'mediawiki.special.block', // not sure if this is actually 
required
+       ),
+       'localBasePath' => __DIR__,
+       'remoteExtPath' => 'StopForumSpam',
+);
+
+$wgGroupPermissions['sysop']['stopforumspam'] = true;
diff --git a/ext.SFS.formhack.js b/ext.SFS.formhack.js
new file mode 100644
index 0000000..d31c77c
--- /dev/null
+++ b/ext.SFS.formhack.js
@@ -0,0 +1,47 @@
+/**
+ * Stolen from mediawiki.special.block.js
+ */
+( function ( mw, $ ) {
+       $( function () {
+               var $blockTarget = $( '#mw-bi-target' ),
+                       $SFSbox;
+
+               // this will be needed until gerrit change #101063 is merged
+               $SFSbox = $('<tr>')
+                       .addClass( 'mw-htmlform-field-HTMLCheckField')
+                       .append( '<td 
class="mw-label"><label>&#160;</label></td>' )
+                       .append( $('<td>')
+                               .addClass('mw-input')
+                               .append('<input name="wpSFS" type="checkbox" 
value="1" id="mw-input-wpSFS" />')
+                               .append('&#160;')
+                               .append( $('<label>')
+                                       .attr('for', 'mw-input-wpSFS')
+                                       .text( mw.message( 
'stopforumspam-checkbox').text() )
+                               )
+                       );
+
+               $( '#mw-input-wpHardBlock' ).closest( 'tr').after( $SFSbox );
+
+               function updateBlockOptions( instant ) {
+                       var blocktarget = $.trim( $blockTarget.val() ),
+                               isEmpty = blocktarget === '',
+                               isIp = mw.util.isIPv4Address( blocktarget, true 
) || mw.util.isIPv6Address( blocktarget, true ),
+                               isIpRange = isIp && blocktarget.match( /\/\d+$/ 
);
+
+                       if ( !isIp && !isEmpty ) {
+                               $SFSbox.goIn( instant );
+                       } else {
+                               $SFSbox.goOut( instant );
+                       }
+               }
+
+               if ( $blockTarget.length ) {
+                       // Bind functions so they're checked whenever stuff 
changes
+                       $blockTarget.keyup( updateBlockOptions );
+
+                       // Call them now to set initial state (ie. 
Special:Block/Foobar?wpBlockExpiry=2+hours)
+                       updateBlockOptions( /* instant= */ true );
+               }
+       } );
+}( mediaWiki, jQuery ) );
+

-- 
To view, visit https://gerrit.wikimedia.org/r/101473
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If2f9383b679041726f4ab97308bffbd9935e5cd8
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/StopForumSpam
Gerrit-Branch: master
Gerrit-Owner: Legoktm <legoktm.wikipe...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to