Fix epic fail in r64860
[lhc/web/wiklou.git] / includes / specials / SpecialIpblocklist.php
index 9e29c23..ccb2899 100644 (file)
@@ -5,12 +5,13 @@
  */
 
 /**
+ * @param $ip part of title: Special:Ipblocklist/<ip>.
  * @todo document
  */
-function wfSpecialIpblocklist() {
+function wfSpecialIpblocklist( $ip = '' ) {
        global $wgUser, $wgOut, $wgRequest;
-
-       $ip = trim( $wgRequest->getVal( 'wpUnblockAddress', $wgRequest->getVal( 'ip' ) ) );
+       $ip = $wgRequest->getVal( 'ip', $ip );
+       $ip = trim( $wgRequest->getVal( 'wpUnblockAddress', $ip ) );
        $id = $wgRequest->getVal( 'id' );
        $reason = $wgRequest->getText( 'wpUnblockReason' );
        $action = $wgRequest->getText( 'action' );
@@ -18,7 +19,7 @@ function wfSpecialIpblocklist() {
 
        $ipu = new IPUnblockForm( $ip, $id, $reason );
 
-       if( $action == 'unblock' ) {
+       if( $action == 'unblock' || $action == 'submit' && $wgRequest->wasPosted() ) {
                # Check permissions
                if( !$wgUser->isAllowed( 'block' ) ) {
                        $wgOut->permissionRequired( 'block' );
@@ -29,22 +30,34 @@ function wfSpecialIpblocklist() {
                        $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;
+       
+               # bug 15810: blocked admins should have limited access here
+               if ( $wgUser->isBlocked() ) {
+                       if ( $id ) {
+                               # This doesn't pick up on autoblocks, but admins
+                               # should have the ipblock-exempt permission anyway
+                               $block = Block::newFromID( $id );
+                               $user = User::newFromName( $block->mAddress );
+                       } else {
+                               $user = User::newFromName( $ip );
+                       }
+                       $status = IPBlockForm::checkUnblockSelf( $user );
+                       if ( $status !== true ) {
+                               throw new ErrorPageError( 'badaccess', $status );
+                       }
                }
-               # Check for database lock
-               if( wfReadOnly() ) {
-                       $wgOut->readOnlyPage();
-                       return;
+               
+               if( $action == 'unblock' ){
+                       # Show unblock form
+                       $ipu->showForm( '' );
+               } elseif( $action == 'submit' 
+                       && $wgRequest->wasPosted()
+                       && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) 
+               {
+                       # Remove blocks and redirect user to success page
+                       $ipu->doSubmit();
                }
-               # Remove blocks and redirect user to success page
-               $ipu->doSubmit();
+               
        } elseif( $action == 'success' ) {
                # Inform the user of a successful unblock
                # (No need to check permissions or locks here,
@@ -78,7 +91,6 @@ class IPUnblockForm {
                $this->hideuserblocks = $wgRequest->getBool( 'hideuserblocks' );
                $this->hidetempblocks = $wgRequest->getBool( 'hidetempblocks' );
                $this->hideaddressblocks = $wgRequest->getBool( 'hideaddressblocks' );
-               $this->scanRange = $wgRequest->getBool( 'range' );
        }
 
        /**
@@ -95,7 +107,7 @@ class IPUnblockForm {
                $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
                $action = $titleObj->getLocalURL( "action=submit" );
 
-               if ( "" != $err ) {
+               if ( $err != "" ) {
                        $wgOut->setSubtitle( wfMsg( "formerror" ) );
                        $wgOut->addWikiText( Xml::tags( 'span', array( 'class' => 'error' ), $err ) . "\n" );
                }
@@ -163,7 +175,7 @@ class IPUnblockForm {
         * @return array array(message key, parameters) on failure, empty array on success
         */
 
-       static function doUnblock(&$id, &$ip, &$reason, &$range = null) {
+       static function doUnblock(&$id, &$ip, &$reason, &$range = null, $blocker=null) {
                if ( $id ) {
                        $block = Block::newFromID( $id );
                        if ( !$block ) {
@@ -185,8 +197,7 @@ class IPUnblockForm {
                                if ( !$block ) {
                                        return array('ipb_cant_unblock', htmlspecialchars($id));
                                }
-                               if( $block->mRangeStart != $block->mRangeEnd
-                                               && !strstr( $ip, "/" ) ) {
+                               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;
@@ -196,11 +207,22 @@ class IPUnblockForm {
                }
                // Yes, this is really necessary
                $id = $block->mId;
+               
+               # If the name was hidden and the blocking user cannot hide
+               # names, then don't allow any block removals...
+               if( $blocker && $block->mHideName && !$blocker->isAllowed('hideuser') ) {
+                       return array('ipb_cant_unblock', htmlspecialchars($id));
+               }
 
                # Delete block
                if ( !$block->delete() ) {
                        return array('ipb_cant_unblock', htmlspecialchars($id));
                }
+               
+               # Unset _deleted fields as needed
+               if( $block->mHideName ) {
+                       IPBlockForm::unsuppressUserName( $block->mAddress, $block->mUser );
+               }
 
                # Make log entry
                $log = new LogPage( 'block' );
@@ -209,10 +231,9 @@ class IPUnblockForm {
        }
 
        function doSubmit() {
-               global $wgOut;
-               $retval = self::doUnblock($this->id, $this->ip, $this->reason, $range);
-               if(!empty($retval))
-               {
+               global $wgOut, $wgUser;
+               $retval = self::doUnblock($this->id, $this->ip, $this->reason, $range, $wgUser);
+               if( !empty($retval) ) {
                        $key = array_shift($retval);
                        $this->showForm(wfMsgReal($key, $retval));
                        return;
@@ -227,7 +248,7 @@ class IPUnblockForm {
                global $wgOut, $wgUser;
 
                $wgOut->setPagetitle( wfMsg( "ipblocklist" ) );
-               if ( "" != $msg ) {
+               if ( $msg != "" ) {
                        $wgOut->setSubtitle( $msg );
                }
 
@@ -239,7 +260,7 @@ class IPUnblockForm {
                $conds = array();
                $matches = array();
                // Is user allowed to see all the blocks?
-               if ( !$wgUser->isAllowed( 'suppress' ) )
+               if ( !$wgUser->isAllowed( 'hideuser' ) )
                        $conds['ipb_deleted'] = 0;
                if ( $this->ip == '' ) {
                        // No extra conditions
@@ -247,12 +268,18 @@ class IPUnblockForm {
                        $conds['ipb_id'] = substr( $this->ip, 1 );
                // Single IPs
                } elseif ( IP::isIPAddress($this->ip) && strpos($this->ip,'/') === false ) {
-                       if( $this->scanRange && $iaddr = IP::toHex($this->ip) ) {
+                       if( $iaddr = IP::toHex($this->ip) ) {
                                # Only scan ranges which start in this /16, this improves search speed
                                # Blocks should not cross a /16 boundary.
                                $range = substr( $iaddr, 0, 4 );
-                               $conds[] = "(ipb_address = '" . IP::sanitizeIP($this->ip) . "') OR 
-                                       (ipb_range_start LIKE '$range%' AND ipb_range_start <= '$iaddr' AND ipb_range_end >= '$iaddr')";
+                               // Fixme -- encapsulate this sort of query-building.
+                               $dbr = wfGetDB( DB_SLAVE );
+                               $encIp = $dbr->addQuotes( IP::sanitizeIP($this->ip) );
+                               $encAddr = $dbr->addQuotes( $iaddr );
+                               $conds[] = "(ipb_address = $encIp) OR 
+                                       (ipb_range_start" . $dbr->buildLike( $range, $dbr->anyString() ) . " AND
+                                       ipb_range_start <= $encAddr
+                                       AND ipb_range_end >= $encAddr)";
                        } else {
                                $conds['ipb_address'] = IP::sanitizeIP($this->ip);
                        }
@@ -282,40 +309,49 @@ class IPUnblockForm {
                        $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
                }
 
+               // Search form
+               $wgOut->addHTML( $this->searchForm() );
+
+               // Check for other blocks, i.e. global/tor blocks
+               $otherBlockLink = array();
+               wfRunHooks( 'OtherBlockLogLink', array( &$otherBlockLink, $this->ip ) );
+
+               // Show additional header for the local block only when other blocks exists.
+               // Not necessary in a standard installation without such extensions enabled
+               if( count( $otherBlockLink ) ) {
+                       $wgOut->addHTML(
+                               Html::rawElement( 'h2', array(), wfMsg( 'ipblocklist-localblock' ) ) . "\n"
+                       );
+               }
                $pager = new IPBlocklistPager( $this, $conds );
                if ( $pager->getNumRows() ) {
                        $wgOut->addHTML(
-                               $this->searchForm() .
-                               $this->showhideLinks() .
                                $pager->getNavigationBar() .
                                Xml::tags( 'ul', null, $pager->getBody() ) .
                                $pager->getNavigationBar()
                        );
                } elseif ( $this->ip != '') {
-                       $wgOut->addHTML( $this->searchForm() );
                        $wgOut->addWikiMsg( 'ipblocklist-no-results' );
                } else {
-                       $wgOut->addHTML( $this->searchForm() . $this->showhideLinks() );
                        $wgOut->addWikiMsg( 'ipblocklist-empty' );
                }
-       }
 
-       function searchForm() {
-               global $wgTitle, $wgScript, $wgRequest;
-               return
-                       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 ) .
-                               '<br/>' . 
-                               Xml::checkLabel( wfMsg('ipblocklist-scanrange'), 'range', 'range', $this->scanRange ) .
-                               '&nbsp;' . Xml::submitButton( wfMsg( 'ipblocklist-submit' ) ) .
-                               Xml::closeElement( 'fieldset' )
+               if( count( $otherBlockLink ) ) {
+                       $wgOut->addHTML(
+                               Html::rawElement( 'h2', array(), wfMsgExt( 'ipblocklist-otherblocks', 'parseinline', count( $otherBlockLink ) ) ) . "\n"
                        );
+                       $list = '';
+                       foreach( $otherBlockLink as $link ) {
+                               $list .= Html::rawElement( 'li', array(), $link ) . "\n";
+                       }
+                       $wgOut->addHTML( Html::rawElement( 'ul', array( 'class' => 'mw-ipblocklist-otherblocks' ), $list ) . "\n" );
+               }
+
        }
-       
-       function showhideLinks() {
+
+       function searchForm() {
+               global $wgScript, $wgRequest, $wgLang;
+
                $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ) );
                $nondefaults = array();
                if( $this->hideuserblocks ) {
@@ -333,16 +369,26 @@ class IPUnblockForm {
                        array( 'hidetempblocks' => 1-$this->hidetempblocks ), $nondefaults);
                $sipbLink = $this->makeOptionsLink( $showhide[1-$this->hideaddressblocks],
                        array( 'hideaddressblocks' => 1-$this->hideaddressblocks ), $nondefaults);
-                       
+
                $links = array();
                $links[] = wfMsgHtml( 'ipblocklist-sh-userblocks', $ubLink );
                $links[] = wfMsgHtml( 'ipblocklist-sh-tempblocks', $tbLink );
                $links[] = wfMsgHtml( 'ipblocklist-sh-addressblocks', $sipbLink );
-               
-               $hl = '(' . implode( ' | ', $links ) . ')<hr/>';
-               return $hl;
+               $hl = $wgLang->pipeList( $links );
+
+               return
+                       Xml::tags( 'form', array( 'action' => $wgScript ),
+                               Xml::hidden( 'title', SpecialPage::getTitleFor( 'Ipblocklist' )->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' ) ) . '<br />' .
+                               $hl .
+                               Xml::closeElement( 'fieldset' )
+                       );
        }
-       
+
        /**
         * Makes change an option link which carries all the other options
         * @param $title see Title
@@ -352,8 +398,8 @@ class IPUnblockForm {
        function makeOptionsLink( $title, $override, $options, $active = false ) {
                global $wgUser;
                $sk = $wgUser->getSkin();
-               $params = wfArrayMerge( $options, $override );
-               $ipblocklist = SpecialPage::getTitleFor( 'IPBlockList' );
+               $params = $override + $options;
+               $ipblocklist = SpecialPage::getTitleFor( 'Ipblocklist' );
                return $sk->link( $ipblocklist, htmlspecialchars( $title ),
                        ( $active ? array( 'style'=>'font-weight: bold;' ) : array() ), $params, array( 'known' ) );
        }
@@ -372,12 +418,11 @@ class IPUnblockForm {
                        $sk = $wgUser->getSkin();
                if( is_null( $msg ) ) {
                        $msg = array();
-                       $keys = array( 'infiniteblock', 'expiringblock', 'unblocklink',
-                               'anononlyblock', 'createaccountblock', 'noautoblockblock', 'emailblock', 'blocklist-nousertalk' );
+                       $keys = array( 'infiniteblock', 'expiringblock', 'unblocklink', 'change-blocklink',
+                               'anononlyblock', 'createaccountblock', 'noautoblockblock', 'emailblock', 'blocklist-nousertalk', 'blocklistline' );
                        foreach( $keys as $key ) {
                                $msg[$key] = wfMsgHtml( $key );
                        }
-                       $msg['blocklistline'] = wfMsg( 'blocklistline' );
                }
 
                # Prepare links to the blocker's user and talk pages
@@ -394,7 +439,7 @@ class IPUnblockForm {
                                . $sk->userToolLinks( $block->mUser, $block->mAddress, false, Linker::TOOL_LINKS_NOBLOCK );
                }
 
-               $formattedTime = $wgLang->timeanddate( $block->mTimestamp, true );
+               $formattedTime = htmlspecialchars( $wgLang->timeanddate( $block->mTimestamp, true ) );
 
                $properties = array();
                $properties[] = Block::formatExpiry( $block->mExpiry );
@@ -416,24 +461,38 @@ class IPUnblockForm {
                        $properties[] = $msg['blocklist-nousertalk'];
                }
 
-               $properties = implode( ', ', $properties );
+               $properties = $wgLang->commaList( $properties );
 
                $line = wfMsgReplaceArgs( $msg['blocklistline'], array( $formattedTime, $blocker, $target, $properties ) );
 
                $unblocklink = '';
-               if ( $wgUser->isAllowed('block') ) {
-                       $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
-                       $unblocklink = ' (' . $sk->makeKnownLinkObj($titleObj, $msg['unblocklink'], 'action=unblock&id=' . urlencode( $block->mId ) ) . ')';
+               $changeblocklink = '';
+               $toolLinks = '';
+               if ( $wgUser->isAllowed( 'block' ) ) {
+                       $unblocklink = $sk->link( SpecialPage::getTitleFor( 'Ipblocklist' ),
+                                       $msg['unblocklink'],
+                                       array(),
+                                       array( 'action' => 'unblock', 'id' => $block->mId ),
+                                       'known' );
+
+                       # Create changeblocklink for all blocks with exception of autoblocks
+                       if( !$block->mAuto ) {
+                               $changeblocklink = wfMsgExt( 'pipe-separator', 'escapenoentities' ) .
+                                       $sk->link( SpecialPage::getTitleFor( 'Blockip', $block->mAddress ), 
+                                               $msg['change-blocklink'],
+                                               array(), array(), 'known' );
+                       }
+                       $toolLinks = "($unblocklink$changeblocklink)";
                }
 
-               $comment = $sk->commentBlock( $block->mReason );
+               $comment = $sk->commentBlock( htmlspecialchars($block->mReason) );
 
                $s = "{$line} $comment";
                if ( $block->mHideName )
                        $s = '<span class="history-deleted">' . $s . '</span>';
 
                wfProfileOut( __METHOD__ );
-               return "<li>$s $unblocklink</li>\n";
+               return "<li>$s $toolLinks</li>\n";
        }
 }