Alrighty, now we properly remove old blocks before inserting the new one. (Bug 10080...
[lhc/web/wiklou.git] / includes / SpecialIpblocklist.php
index 293059f..696c7ef 100644 (file)
@@ -1,8 +1,7 @@
 <?php
 /**
- *
- * @package MediaWiki
- * @subpackage SpecialPage
+ * @file
+ * @ingroup SpecialPage
  */
 
 /**
@@ -19,142 +18,209 @@ function wfSpecialIpblocklist() {
 
        $ipu = new IPUnblockForm( $ip, $id, $reason );
 
-       if ( "success" == $action ) {
-               $ipu->showList( $wgOut->parse( wfMsg( 'unblocked', $successip ) ) );
-       } else if ( "submit" == $action && $wgRequest->wasPosted() &&
-               $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
-               if ( ! $wgUser->isAllowed('block') ) {
+       if( $action == 'unblock' ) {
+               # Check permissions
+               if( !$wgUser->isAllowed( 'block' ) ) {
                        $wgOut->permissionRequired( 'block' );
                        return;
                }
+               # Check for database lock
+               if( wfReadOnly() ) {
+                       $wgOut->readOnlyPage();
+                       return;
+               }
+               # Show unblock form
+               $ipu->showForm( '' );
+       } elseif( $action == 'submit' && $wgRequest->wasPosted()
+               && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
+               # Check permissions
+               if( !$wgUser->isAllowed( 'block' ) ) {
+                       $wgOut->permissionRequired( 'block' );
+                       return;
+               }
+               # Check for database lock
+               if( wfReadOnly() ) {
+                       $wgOut->readOnlyPage();
+                       return;
+               }
+               # Remove blocks and redirect user to success page
                $ipu->doSubmit();
-       } else if ( "unblock" == $action ) {
-               $ipu->showForm( "" );
+       } elseif( $action == 'success' ) {
+               # Inform the user of a successful unblock
+               # (No need to check permissions or locks here,
+               # if something was done, then it's too late!)
+               if ( substr( $successip, 0, 1) == '#' ) {
+                       // A block ID was unblocked
+                       $ipu->showList( $wgOut->parse( wfMsg( 'unblocked-id', $successip ) ) );
+               } else {
+                       // A username/IP was unblocked
+                       $ipu->showList( $wgOut->parse( wfMsg( 'unblocked', $successip ) ) );
+               }
        } else {
-               $ipu->showList( "" );
+               # Just show the block list
+               $ipu->showList( '' );
        }
+
 }
 
 /**
- *
- * @package MediaWiki
- * @subpackage SpecialPage
+ * implements Special:ipblocklist GUI
+ * @ingroup SpecialPage
  */
 class IPUnblockForm {
        var $ip, $reason, $id;
 
        function IPUnblockForm( $ip, $id, $reason ) {
-               $this->ip = $ip;
+               $this->ip = strtr( $ip, '_', ' ' );
                $this->id = $id;
                $this->reason = $reason;
        }
 
+       /**
+        * Generates the unblock form
+        * @param $err string: error message
+        * @return $out string: HTML form
+        */
        function showForm( $err ) {
                global $wgOut, $wgUser, $wgSysopUserBans;
 
                $wgOut->setPagetitle( wfMsg( 'unblockip' ) );
-               $wgOut->addWikiText( wfMsg( 'unblockiptext' ) );
+               $wgOut->addWikiMsg( 'unblockiptext' );
 
-               $ipa = wfMsgHtml( $wgSysopUserBans ? 'ipadressorusername' : 'ipaddress' );
-               $ipr = wfMsgHtml( 'ipbreason' );
-               $ipus = wfMsgHtml( 'ipusubmit' );
                $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
-               $action = $titleObj->escapeLocalURL( "action=submit" );
+               $action = $titleObj->getLocalURL( "action=submit" );
 
                if ( "" != $err ) {
                        $wgOut->setSubtitle( wfMsg( "formerror" ) );
-                       $wgOut->addWikitext( "<span class='error'>{$err}</span>\n" );
+                       $wgOut->addWikiText( Xml::tags( 'span', array( 'class' => 'error' ), $err ) . "\n" );
                }
-               $token = htmlspecialchars( $wgUser->editToken() );
 
                $addressPart = false;
                if ( $this->id ) {
                        $block = Block::newFromID( $this->id );
                        if ( $block ) {
                                $encName = htmlspecialchars( $block->getRedactedName() );
-                               $encId = htmlspecialchars( $this->id );
-                               $addressPart = $encName . "<input type='hidden' name=\"id\" value=\"$encId\" />";
+                               $encId = $this->id;
+                               $addressPart = $encName . Xml::hidden( 'id', $encId );
+                               $ipa = wfMsgHtml( $wgSysopUserBans ? 'ipadressorusername' : 'ipaddress' );
                        }
                }
                if ( !$addressPart ) {
-                       $addressPart = "<input tabindex='1' type='text' size='20' " .
-                               "name=\"wpUnblockAddress\" value=\"" . htmlspecialchars( $this->ip ) . "\" />";
+                       $addressPart = Xml::input( 'wpUnblockAddress', 40, $this->ip, array( 'type' => 'text', 'tabindex' => '1' ) );
+                       $ipa = Xml::label( wfMsg( $wgSysopUserBans ? 'ipadressorusername' : 'ipaddress' ), 'wpUnblockAddress' );
                }
 
-               $wgOut->addHTML( "
-<form id=\"unblockip\" method=\"post\" action=\"{$action}\">
-       <table border='0'>
-               <tr>
-                       <td align='right'>{$ipa}:</td>
-                       <td align='left'>
-                               {$addressPart}
-                       </td>
-               </tr>
-               <tr>
-                       <td align='right'>{$ipr}:</td>
-                       <td align='left'>
-                               <input tabindex='1' type='text' size='40' name=\"wpUnblockReason\" value=\"" . htmlspecialchars( $this->reason ) . "\" />
-                       </td>
-               </tr>
-               <tr>
-                       <td>&nbsp;</td>
-                       <td align='left'>
-                               <input tabindex='2' type='submit' name=\"wpBlock\" value=\"{$ipus}\" />
-                       </td>
-               </tr>
-       </table>
-       <input type='hidden' name='wpEditToken' value=\"{$token}\" />
-</form>\n" );
+               $wgOut->addHTML(
+                       Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'unblockip' ) ) .
+                       Xml::openElement( 'fieldset' ) .
+                       Xml::element( 'legend', null, wfMsg( 'ipb-unblock' ) ) .
+                       Xml::openElement( 'table', array( 'id' => 'mw-unblock-table' ) ).
+                       "<tr>
+                               <td class='mw-label'>
+                                       {$ipa}
+                               </td>
+                               <td class='mw-input'>
+                                       {$addressPart}
+                               </td>
+                       </tr>
+                       <tr>
+                               <td class='mw-label'>" .
+                                       Xml::label( wfMsg( 'ipbreason' ), 'wpUnblockReason' ) .
+                               "</td>
+                               <td class='mw-input'>" .
+                                       Xml::input( 'wpUnblockReason', 40, $this->reason, array( 'type' => 'text', 'tabindex' => '2' ) ) .
+                               "</td>
+                       </tr>
+                       <tr>
+                               <td>&nbsp;</td>
+                               <td class='mw-submit'>" .
+                                       Xml::submitButton( wfMsg( 'ipusubmit' ), array( 'name' => 'wpBlock', 'tabindex' => '3' ) ) .
+                               "</td>
+                       </tr>" .
+                       Xml::closeElement( 'table' ) .
+                       Xml::closeElement( 'fieldset' ) .
+                       Xml::hidden( 'wpEditToken', $wgUser->editToken() ) .
+                       Xml::closeElement( 'form' ) . "\n"
+               );
 
        }
 
-       function doSubmit() {
-               global $wgOut;
+       const UNBLOCK_SUCCESS = 0; // Success
+       const UNBLOCK_NO_SUCH_ID = 1; // No such block ID
+       const UNBLOCK_USER_NOT_BLOCKED = 2; // IP wasn't blocked
+       const UNBLOCK_BLOCKED_AS_RANGE = 3; // IP is part of a range block
+       const UNBLOCK_UNKNOWNERR = 4; // Unknown error
 
-               if ( $this->id ) {
-                       $block = Block::newFromID( $this->id );
-                       if ( $block ) {
-                               $this->ip = $block->getRedactedName();
+       /**
+        * Backend code for unblocking. doSubmit() wraps around this.
+        * $range is only used when UNBLOCK_BLOCKED_AS_RANGE is returned, in which
+        * case it contains the range $ip is part of.
+        * @return array array(message key, parameters) on failure, empty array on success
+        */
+
+       static function doUnblock(&$id, &$ip, &$reason, &$range = null)
+       {
+               if ( $id ) {
+                       $block = Block::newFromID( $id );
+                       if ( !$block ) {
+                               return array('ipb_cant_unblock', htmlspecialchars($id));
                        }
+                       $ip = $block->getRedactedName();
                } else {
                        $block = new Block();
-                       $this->ip = trim( $this->ip );
-                       if ( substr( $this->ip, 0, 1 ) == "#" ) {
-                               $id = substr( $this->ip, 1 );
+                       $ip = trim( $ip );
+                       if ( substr( $ip, 0, 1 ) == "#" ) {
+                               $id = substr( $ip, 1 );
                                $block = Block::newFromID( $id );
+                               if( !$block ) {
+                                       return array('ipb_cant_unblock', htmlspecialchars($id));
+                               }
+                               $ip = $block->getRedactedName();
                        } else {
-                               $block = Block::newFromDB( $this->ip );
-                               if ( !$block ) { 
-                                       $block = null;
+                               $block = Block::newFromDB( $ip );
+                               if ( !$block ) {
+                                       return array('ipb_cant_unblock', htmlspecialchars($id));
+                               }
+                               if( $block->mRangeStart != $block->mRangeEnd
+                                               && !strstr( $ip, "/" ) ) {
+                                       /* If the specified IP is a single address, and the block is
+                                        * a range block, don't unblock the range. */
+                                        $range = $block->mAddress;
+                                        return array('ipb_blocked_as_range', $ip, $range);
                                }
                        }
                }
-               $success = false;
-               if ( $block ) {
-                       # Delete block
-                       if ( $block->delete() ) {
-                               # Make log entry
-                               $log = new LogPage( 'block' );
-                               $log->addEntry( 'unblock', Title::makeTitle( NS_USER, $this->ip ), $this->reason );
-                               $success = true;
-                       }
+               // Yes, this is really necessary
+               $id = $block->mId;
+
+               # Delete block
+               if ( !$block->delete() ) {
+                       return array('ipb_cant_unblock', htmlspecialchars($id));
                }
 
-               if ( $success ) {
-                       # Report to the user
-                       $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
-                       $success = $titleObj->getFullURL( "action=success&successip=" . urlencode( $this->ip ) );
-                       $wgOut->redirect( $success );
-               } else {
-                       if ( !$this->ip && $this->id ) {
-                               $this->ip = '#' . $this->id;
-                       }
-                       $this->showForm( wfMsg( 'ipb_cant_unblock', htmlspecialchars( $this->id ) ) );
+               # Make log entry
+               $log = new LogPage( 'block' );
+               $log->addEntry( 'unblock', Title::makeTitle( NS_USER, $ip ), $reason );
+               return array();
+       }
+
+       function doSubmit() {
+               global $wgOut;
+               $retval = self::doUnblock($this->id, $this->ip, $this->reason, $range);
+               if(!empty($retval))
+               {
+                       $key = array_shift($retval);
+                       $this->showForm(wfMsgReal($key, $retval));
+                       return;
                }
+               # Report to the user
+               $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
+               $success = $titleObj->getFullURL( "action=success&successip=" . urlencode( $this->ip ) );
+               $wgOut->redirect( $success );
        }
 
        function showList( $msg ) {
-               global $wgOut;
+               global $wgOut, $wgUser;
 
                $wgOut->setPagetitle( wfMsg( "ipblocklist" ) );
                if ( "" != $msg ) {
@@ -168,6 +234,9 @@ class IPUnblockForm {
 
                $conds = array();
                $matches = array();
+               // Is user allowed to see all the blocks?
+               if ( !$wgUser->isAllowed( 'suppress' ) )
+                       $conds['ipb_deleted'] = 0;
                if ( $this->ip == '' ) {
                        // No extra conditions
                } elseif ( substr( $this->ip, 0, 1 ) == '#' ) {
@@ -180,7 +249,7 @@ class IPUnblockForm {
                        $conds['ipb_auto'] = 0;
                } else {
                        $user = User::newFromName( $this->ip );
-                       if ( $user && ( $id = $user->getID() ) != 0 ) {
+                       if ( $user && ( $id = $user->getId() ) != 0 ) {
                                $conds['ipb_user'] = $id;
                        } else {
                                // Uh...?
@@ -190,41 +259,33 @@ class IPUnblockForm {
                }
 
                $pager = new IPBlocklistPager( $this, $conds );
-               $s = $pager->getNavigationBar() .
-                       $this->searchForm();
                if ( $pager->getNumRows() ) {
-                       $s .= "<ul>" . 
-                               $pager->getBody() .
-                               "</ul>";
+                       $wgOut->addHTML(
+                               $this->searchForm() .
+                               $pager->getNavigationBar() .
+                               Xml::tags( 'ul', null, $pager->getBody() ) .
+                               $pager->getNavigationBar()
+                       );
+               } elseif ( $this->ip != '') {
+                       $wgOut->addHTML( $this->searchForm() );
+                       $wgOut->addWikiMsg( 'ipblocklist-no-results' );
                } else {
-                       $s .= '<p>' . wfMsgHTML( 'ipblocklistempty' ) . '</p>';
+                       $wgOut->addWikiMsg( 'ipblocklist-empty' );
                }
-               $s .= $pager->getNavigationBar();
-               $wgOut->addHTML( $s );
        }
 
        function searchForm() {
                global $wgTitle, $wgScript, $wgRequest;
                return
-                       wfElement( 'form', array(
-                               'action' => $wgScript ),
-                               null ) .
-                       wfHidden( 'title', $wgTitle->getPrefixedDbKey() ) .
-                       wfElement( 'input', array(
-                               'type' => 'hidden',
-                               'name' => 'action',
-                               'value' => 'search' ) ).
-                       wfElement( 'input', array(
-                               'type' => 'hidden',
-                               'name' => 'limit',
-                               'value' => $wgRequest->getText( 'limit' ) ) ) .
-                       wfElement( 'input', array(
-                               'name' => 'ip',
-                               'value' => $this->ip ) ) .
-                       wfElement( 'input', array(
-                               'type' => 'submit',
-                               'value' => wfMsg( 'searchbutton' ) ) ) .
-                       '</form>';
+                       Xml::tags( 'form', array( 'action' => $wgScript ),
+                               Xml::hidden( 'title', $wgTitle->getPrefixedDbKey() ) .
+                               Xml::openElement( 'fieldset' ) .
+                               Xml::element( 'legend', null, wfMsg( 'ipblocklist-legend' ) ) .
+                               Xml::inputLabel( wfMsg( 'ipblocklist-username' ), 'ip', 'ip', /* size */ false, $this->ip ) .
+                               '&nbsp;' .
+                               Xml::submitButton( wfMsg( 'ipblocklist-submit' ) ) .
+                               Xml::closeElement( 'fieldset' )
+                       );
        }
 
        /**
@@ -241,13 +302,12 @@ class IPUnblockForm {
                        $sk = $wgUser->getSkin();
                if( is_null( $msg ) ) {
                        $msg = array();
-                       $keys = array( 'infiniteblock', 'expiringblock', 'contribslink', 'unblocklink', 
-                               'anononlyblock', 'createaccountblock', 'noautoblockblock' );
+                       $keys = array( 'infiniteblock', 'expiringblock', 'unblocklink',
+                               'anononlyblock', 'createaccountblock', 'noautoblockblock', 'emailblock' );
                        foreach( $keys as $key ) {
                                $msg[$key] = wfMsgHtml( $key );
                        }
                        $msg['blocklistline'] = wfMsg( 'blocklistline' );
-                       $msg['contribslink'] = wfMsg( 'contribslink' );
                }
 
                # Prepare links to the blocker's user and talk pages
@@ -260,19 +320,14 @@ class IPUnblockForm {
                if( $block->mAuto ) {
                        $target = $block->getRedactedName(); # Hide the IP addresses of auto-blocks; privacy
                } else {
-                       $target = $sk->makeLinkObj( Title::makeTitle( NS_USER, $block->mAddress ), $block->mAddress );
-                       $target .= ' (' . $sk->makeKnownLinkObj( SpecialPage::getSafeTitleFor( 'Contributions', $block->mAddress ), $msg['contribslink'] ) . ')';
+                       $target = $sk->userLink( $block->mUser, $block->mAddress )
+                               . $sk->userToolLinks( $block->mUser, $block->mAddress, false, Linker::TOOL_LINKS_NOBLOCK );
                }
-               
+
                $formattedTime = $wgLang->timeanddate( $block->mTimestamp, true );
 
                $properties = array();
-               if ( $block->mExpiry === "" || $block->mExpiry === Block::infinity() ) {
-                       $properties[] = $msg['infiniteblock'];
-               } else {
-                       $properties[] = wfMsgReplaceArgs( $msg['expiringblock'],
-                               array( $wgLang->timeanddate( $block->mExpiry, true ) ) );
-               }
+               $properties[] = Block::formatExpiry( $block->mExpiry );
                if ( $block->mAnonOnly ) {
                        $properties[] = $msg['anononlyblock'];
                }
@@ -283,23 +338,35 @@ class IPUnblockForm {
                        $properties[] = $msg['noautoblockblock'];
                }
 
+               if ( $block->mBlockEmail && $block->mUser ) {
+                       $properties[] = $msg['emailblock'];
+               }
+
                $properties = implode( ', ', $properties );
 
                $line = wfMsgReplaceArgs( $msg['blocklistline'], array( $formattedTime, $blocker, $target, $properties ) );
 
-               $s = "<li>{$line}";
-
+               $unblocklink = '';
                if ( $wgUser->isAllowed('block') ) {
                        $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
-                       $s .= ' (' . $sk->makeKnownLinkObj($titleObj, $msg['unblocklink'], 'action=unblock&id=' . urlencode( $block->mId ) ) . ')';
+                       $unblocklink = ' (' . $sk->makeKnownLinkObj($titleObj, $msg['unblocklink'], 'action=unblock&id=' . urlencode( $block->mId ) ) . ')';
                }
-               $s .= $sk->commentBlock( $block->mReason );
-               $s .= "</li>\n";
+
+               $comment = $sk->commentBlock( $block->mReason );
+
+               $s = "{$line} $comment";
+               if ( $block->mHideName )
+                       $s = '<span class="history-deleted">' . $s . '</span>';
+
                wfProfileOut( __METHOD__ );
-               return $s;
+               return "<li>$s $unblocklink</li>\n";
        }
 }
 
+/**
+ * @todo document
+ * @ingroup Pager
+ */
 class IPBlocklistPager extends ReverseChronologicalPager {
        public $mForm, $mConds;
 
@@ -326,7 +393,7 @@ class IPBlocklistPager extends ReverseChronologicalPager {
                # Usernames and titles are in fact related by a simple substitution of space -> underscore
                # The last few lines of Title::secureAndSplit() tell the story.
                while ( $row = $this->mResult->fetchObject() ) {
-                       $name = str_replace( ' ', '_', $row->user_name );
+                       $name = str_replace( ' ', '_', $row->ipb_by_text );
                        $lb->add( NS_USER, $name );
                        $lb->add( NS_USER_TALK, $name );
                        $name = str_replace( ' ', '_', $row->ipb_address );
@@ -337,7 +404,7 @@ class IPBlocklistPager extends ReverseChronologicalPager {
                wfProfileOut( __METHOD__ );
                return '';
        }
-       
+
        function formatRow( $row ) {
                $block = new Block;
                $block->initFromRow( $row );
@@ -347,10 +414,9 @@ class IPBlocklistPager extends ReverseChronologicalPager {
        function getQueryInfo() {
                $conds = $this->mConds;
                $conds[] = 'ipb_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() );
-               $conds[] = 'ipb_by=user_id';
                return array(
-                       'tables' => array( 'ipblocks', 'user' ),
-                       'fields' => $this->mDb->tableName( 'ipblocks' ) . '.*,user_name',
+                       'tables' => 'ipblocks',
+                       'fields' => '*',
                        'conds' => $conds,
                );
        }
@@ -359,5 +425,3 @@ class IPBlocklistPager extends ReverseChronologicalPager {
                return 'ipb_timestamp';
        }
 }
-
-?>