Tweak to r28390:
[lhc/web/wiklou.git] / includes / SpecialBlockip.php
index 1df8a71..e93f6f6 100644 (file)
@@ -43,6 +43,7 @@ function wfSpecialBlockip( $par ) {
  */
 class IPBlockForm {
        var $BlockAddress, $BlockExpiry, $BlockReason;
+#      var $BlockEmail;
 
        function IPBlockForm( $par ) {
                global $wgRequest, $wgUser;
@@ -60,8 +61,9 @@ class IPBlockForm {
                $this->BlockAnonOnly = $wgRequest->getBool( 'wpAnonOnly', $byDefault );
                $this->BlockCreateAccount = $wgRequest->getBool( 'wpCreateAccount', $byDefault );
                $this->BlockEnableAutoblock = $wgRequest->getBool( 'wpEnableAutoblock', $byDefault );
+               $this->BlockEmail = $wgRequest->getBool( 'wpEmailBan', false );
                # Re-check user's rights to hide names, very serious, defaults to 0
-               $this->BlockHideName = $wgRequest->getBool( 'wpHideName', 0 ) && $wgUser->isAllowed( 'hideuser' );
+               $this->BlockHideName = ( $wgRequest->getBool( 'wpHideName', 0 ) && $wgUser->isAllowed( 'hideuser' ) ) ? 1 : 0;
        }
 
        function showForm( $err ) {
@@ -142,7 +144,7 @@ class IPBlockForm {
                        $blockReasonList .= $optgroup;
                }
 
-               $token = htmlspecialchars( $wgUser->editToken() );
+               $token = $wgUser->editToken();
 
                global $wgStylePath, $wgStyleVersion;
                $wgOut->addHTML( "
@@ -196,7 +198,8 @@ class IPBlockForm {
                        <td align=\"$alignRight\">{$mIpbreason}</td>
                        <td>
                                " . Xml::input( 'wpBlockReason', 45, $this->BlockReason,
-                                       array( 'tabindex' => '5', 'id' => 'mw-bi-reason' ) ) . "
+                                       array( 'tabindex' => '5', 'id' => 'mw-bi-reason',
+                                              'maxlength'=> '200' ) ) . "
                        </td>
                </tr>
                <tr id='wpAnonOnlyRow'>
@@ -224,10 +227,25 @@ class IPBlockForm {
                        </td>
                </tr>
                ");
+               
+               global $wgSysopEmailBans;
+               if ( $wgSysopEmailBans && $wgUser->isAllowed( 'blockemail' ) ) {
+                       $wgOut->addHTML("
+                       <tr id='wpEnableEmailBan'>
+                       <td>&nbsp;</td>
+                               <td>
+                                       " . wfCheckLabel( wfMsgHtml( 'ipbemailban' ),
+                                                       'wpEmailBan', 'wpEmailBan', $this->BlockEmail,
+                                                               array( 'tabindex' => '10' )) . "
+                               </td>
+                       </tr>
+                       ");
+               }
+
                // Allow some users to hide name from block log, blocklist and listusers
                if ( $wgUser->isAllowed( 'hideuser' ) ) {
                        $wgOut->addHTML("
-                       <tr>
+                       <tr id='wpEnableHideUser'>
                        <td>&nbsp;</td>
                                <td>
                                        " . wfCheckLabel( wfMsgHtml( 'ipbhidename' ),
@@ -237,12 +255,13 @@ class IPBlockForm {
                        </tr>
                        ");
                }
+               
                $wgOut->addHTML("
                <tr>
                        <td style='padding-top: 1em'>&nbsp;</td>
                        <td style='padding-top: 1em'>
                                " . Xml::submitButton( wfMsg( 'ipbsubmit' ),
-                                                       array( 'name' => 'wpBlock', 'tabindex' => '10' ) ) . "
+                                                       array( 'name' => 'wpBlock', 'tabindex' => '11' ) ) . "
                        </td>
                </tr>
        </table>" .
@@ -263,8 +282,21 @@ class IPBlockForm {
                }
        }
 
-       function doSubmit() {
-               global $wgOut, $wgUser, $wgSysopUserBans, $wgSysopRangeBans;
+       const BLOCK_SUCCESS = 0; // Success
+       const BLOCK_RANGE_INVALID = 1; // Invalid IP range
+       const BLOCK_RANGE_DISABLED = 2; // Sysops can't block ranges
+       const BLOCK_NONEXISTENT_USER = 3; // No such user
+       const BLOCK_IP_INVALID = 4; // Invalid IP address
+       const BLOCK_EXPIRY_INVALID = 5; // Invalid expiry time
+       const BLOCK_ALREADY_BLOCKED = 6; // User is already blocked
+       /**
+        * Backend block code.
+        * $userID and $expiry will be filled accordingly
+        * Returns one of the BLOCK_* constants
+        */
+       function doBlock(&$userId = null, &$expiry = null)
+       {
+               global $wgUser, $wgSysopUserBans, $wgSysopRangeBans;
 
                $userId = 0;
                # Expand valid IPv6 addresses, usernames are left as is
@@ -281,27 +313,23 @@ class IPBlockForm {
                                # IPv4
                                if ( $wgSysopRangeBans ) {
                                        if ( !IP::isIPv4( $this->BlockAddress ) || $matches[2] < 16 || $matches[2] > 32 ) {
-                                               $this->showForm( wfMsg( 'ip_range_invalid' ) );
-                                               return;
+                                               return self::BLOCK_RANGE_INVALID;
                                        }
                                        $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
                                } else {
                                        # Range block illegal
-                                       $this->showForm( wfMsg( 'range_block_disabled' ) );
-                                       return;
+                                       return self::BLOCK_RANGE_DISABLED;
                                }
                        } else if ( preg_match( "/^($rxIP6)\\/(\\d{1,3})$/", $this->BlockAddress, $matches ) ) {
                                # IPv6
                                if ( $wgSysopRangeBans ) {
                                        if ( !IP::isIPv6( $this->BlockAddress ) || $matches[2] < 64 || $matches[2] > 128 ) {
-                                               $this->showForm( wfMsg( 'ip_range_invalid' ) );
-                                               return;
+                                               return self::BLOCK_RANGE_INVALID;
                                        }
                                        $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
                                } else {
                                        # Range block illegal
-                                       $this->showForm( wfMsg( 'range_block_disabled' ) );
-                                       return;
+                                       return self::BLOCK_RANGE_DISABLED;
                                }
                        } else {
                                # Username block
@@ -312,12 +340,10 @@ class IPBlockForm {
                                                $this->BlockAddress = $user->getName();
                                                $userId = $user->getID();
                                        } else {
-                                               $this->showForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->BlockAddress ) ) );
-                                               return;
+                                               return self::BLOCK_NONEXISTENT_USER;
                                        }
                                } else {
-                                       $this->showForm( wfMsg( 'badipaddress' ) );
-                                       return;
+                                       return self::BLOCK_IP_INVALID;
                                }
                        }
                }
@@ -335,8 +361,7 @@ class IPBlockForm {
                        $expirestr = $this->BlockOther;
 
                if (strlen($expirestr) == 0) {
-                       $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
-                       return;
+                       return self::BLOCK_EXPIRY_INVALID;
                }
 
                if ( $expirestr == 'infinite' || $expirestr == 'indefinite' ) {
@@ -346,8 +371,7 @@ class IPBlockForm {
                        $expiry = strtotime( $expirestr );
 
                        if ( $expiry < 0 || $expiry === false ) {
-                               $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
-                               return;
+                               return self::BLOCK_EXPIRY_INVALID;
                        }
 
                        $expiry = wfTimestamp( TS_MW, $expiry );
@@ -355,17 +379,15 @@ class IPBlockForm {
 
                # Create block
                # Note: for a user block, ipb_address is only for display purposes
-
                $block = new Block( $this->BlockAddress, $userId, $wgUser->getID(),
                        $reasonstr, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly,
-                       $this->BlockCreateAccount, $this->BlockEnableAutoblock, $this->BlockHideName);
+                       $this->BlockCreateAccount, $this->BlockEnableAutoblock, $this->BlockHideName,
+                       $this->BlockEmail);
 
                if (wfRunHooks('BlockIp', array(&$block, &$wgUser))) {
 
                        if ( !$block->insert() ) {
-                               $this->showForm( wfMsg( 'ipb_already_blocked',
-                                       htmlspecialchars( $this->BlockAddress ) ) );
-                               return;
+                               return self::BLOCK_ALREADY_BLOCKED;
                        }
 
                        wfRunHooks('BlockIpComplete', array($block, $wgUser));
@@ -382,9 +404,45 @@ class IPBlockForm {
                          $reasonstr, $logParams );
 
                        # Report to the user
-                       $titleObj = SpecialPage::getTitleFor( 'Blockip' );
-                       $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
-                               urlencode( $this->BlockAddress ) ) );
+                       return self::BLOCK_SUCCESS;
+               }
+       }
+
+       /**
+        * UI entry point for blocking
+        * Wraps around doBlock()
+        */
+       function doSubmit()
+       {
+               global $wgOut;
+               $retval = $this->doBlock();
+               switch($retval)
+               {
+                       case self::BLOCK_RANGE_INVALID:
+                               $this->showForm( wfMsg( 'ip_range_invalid' ) );
+                               return;
+                       case self::BLOCK_RANGE_DISABLED:
+                               $this->showForm( wfMsg( 'range_block_disabled' ) );
+                               return;
+                       case self::BLOCK_NONEXISTENT_USER:
+                               $this->showForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->BlockAddress ) ) );
+                               return;
+                       case self::BLOCK_IP_INVALID:
+                               $this->showForm( wfMsg( 'badipaddress' ) );
+                               return;
+                       case self::BLOCK_EXPIRY_INVALID:
+                               $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
+                               return;
+                       case self::BLOCK_ALREADY_BLOCKED:
+                               $this->showForm( wfMsg( 'ipb_already_blocked', htmlspecialchars( $this->BlockAddress ) ) );
+                               return;
+                       case self::BLOCK_SUCCESS:
+                               $titleObj = SpecialPage::getTitleFor( 'Blockip' );
+                               $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
+                                       urlencode( $this->BlockAddress ) ) );
+                               return;
+                       default:
+                               throw new MWException( __METHOD__ . ": Unknown return value ``{$retval}''" );
                }
        }
 
@@ -419,6 +477,8 @@ class IPBlockForm {
                        $flags[] = 'nocreate';
                if( !$this->BlockEnableAutoblock )
                        $flags[] = 'noautoblock';
+               if ( $this->BlockEmail )
+                       $flags[] = 'noemail';
                return implode( ',', $flags );
        }
 
@@ -472,4 +532,4 @@ class IPBlockForm {
                }
        }
 }
-?>
+