5cfa34a04f125149d3efb90060ca507a2a150939
[lhc/web/wiklou.git] / includes / specials / SpecialIpblocklist.php
1 <?php
2 /**
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 */
19
20 /**
21 * Implements Special:ipblocklist
22 * @ingroup SpecialPage
23 */
24 class IPUnblockForm extends SpecialPage {
25 var $ip, $reason, $id;
26 var $hideuserblocks, $hidetempblocks, $hideaddressblocks;
27
28 function __construct() {
29 parent::__construct( 'Ipblocklist' );
30 }
31
32 /**
33 * Main execution point
34 *
35 * @param $ip part of title: Special:Ipblocklist/<ip>.
36 */
37 function execute( $ip ) {
38 global $wgUser, $wgOut, $wgRequest;
39
40 $this->setHeaders();
41 $this->outputHeader();
42
43 $ip = $wgRequest->getVal( 'ip', $ip );
44 $this->ip = trim( $wgRequest->getVal( 'wpUnblockAddress', $ip ) );
45 $this->id = $wgRequest->getVal( 'id' );
46 $this->reason = $wgRequest->getText( 'wpUnblockReason' );
47 $this->hideuserblocks = $wgRequest->getBool( 'hideuserblocks' );
48 $this->hidetempblocks = $wgRequest->getBool( 'hidetempblocks' );
49 $this->hideaddressblocks = $wgRequest->getBool( 'hideaddressblocks' );
50
51 $action = $wgRequest->getText( 'action' );
52 $successip = $wgRequest->getVal( 'successip' );
53
54 if( $action == 'unblock' || $action == 'submit' && $wgRequest->wasPosted() ) {
55 # Check permissions
56 if( !$wgUser->isAllowed( 'block' ) ) {
57 $wgOut->permissionRequired( 'block' );
58 return;
59 }
60 # Check for database lock
61 if( wfReadOnly() ) {
62 $wgOut->readOnlyPage();
63 return;
64 }
65
66 # bug 15810: blocked admins should have limited access here
67 if ( $wgUser->isBlocked() ) {
68 if ( $id ) {
69 # This doesn't pick up on autoblocks, but admins
70 # should have the ipblock-exempt permission anyway
71 $block = Block::newFromID( $id );
72 $user = User::newFromName( $block->mAddress );
73 } else {
74 $user = User::newFromName( $ip );
75 }
76 $status = IPBlockForm::checkUnblockSelf( $user );
77 if ( $status !== true ) {
78 throw new ErrorPageError( 'badaccess', $status );
79 }
80 }
81
82 if( $action == 'unblock' ){
83 # Show unblock form
84 $this->showForm( '' );
85 } elseif( $action == 'submit'
86 && $wgRequest->wasPosted()
87 && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) )
88 {
89 # Remove blocks and redirect user to success page
90 $this->doSubmit();
91 }
92
93 } elseif( $action == 'success' ) {
94 # Inform the user of a successful unblock
95 # (No need to check permissions or locks here,
96 # if something was done, then it's too late!)
97 if ( substr( $successip, 0, 1) == '#' ) {
98 // A block ID was unblocked
99 $this->showList( $wgOut->parse( wfMsg( 'unblocked-id', $successip ) ) );
100 } else {
101 // A username/IP was unblocked
102 $this->showList( $wgOut->parse( wfMsg( 'unblocked', $successip ) ) );
103 }
104 } else {
105 # Just show the block list
106 $this->showList( '' );
107 }
108 }
109
110 /**
111 * Generates the unblock form
112 *
113 * @param $err string: error message
114 * @return $out string: HTML form
115 */
116 function showForm( $err ) {
117 global $wgOut, $wgUser, $wgSysopUserBans;
118
119 $wgOut->addWikiMsg( 'unblockiptext' );
120
121 $action = $this->getTitle()->getLocalURL( 'action=submit' );
122
123 if ( $err != '' ) {
124 $wgOut->setSubtitle( wfMsg( 'formerror' ) );
125 $wgOut->addWikiText( Html::rawElement( 'span', array( 'class' => 'error' ), $err ) . "\n" );
126 }
127
128 $addressPart = false;
129 if ( $this->id ) {
130 $block = Block::newFromID( $this->id );
131 if ( $block ) {
132 $encName = htmlspecialchars( $block->getRedactedName() );
133 $encId = $this->id;
134 $addressPart = $encName . Html::hidden( 'id', $encId );
135 $ipa = wfMsgHtml( $wgSysopUserBans ? 'ipadressorusername' : 'ipaddress' );
136 }
137 }
138 if ( !$addressPart ) {
139 $addressPart = Xml::input( 'wpUnblockAddress', 40, $this->ip, array( 'type' => 'text', 'tabindex' => '1' ) );
140 $ipa = Xml::label( wfMsg( $wgSysopUserBans ? 'ipadressorusername' : 'ipaddress' ), 'wpUnblockAddress' );
141 }
142
143 $wgOut->addHTML(
144 Html::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'unblockip' ) ) .
145 Html::openElement( 'fieldset' ) .
146 Html::element( 'legend', null, wfMsg( 'ipb-unblock' ) ) .
147 Html::openElement( 'table', array( 'id' => 'mw-unblock-table' ) ).
148 "<tr>
149 <td class='mw-label'>
150 {$ipa}
151 </td>
152 <td class='mw-input'>
153 {$addressPart}
154 </td>
155 </tr>
156 <tr>
157 <td class='mw-label'>" .
158 Xml::label( wfMsg( 'ipbreason' ), 'wpUnblockReason' ) .
159 "</td>
160 <td class='mw-input'>" .
161 Xml::input( 'wpUnblockReason', 40, $this->reason, array( 'type' => 'text', 'tabindex' => '2' ) ) .
162 "</td>
163 </tr>
164 <tr>
165 <td>&#160;</td>
166 <td class='mw-submit'>" .
167 Xml::submitButton( wfMsg( 'ipusubmit' ), array( 'name' => 'wpBlock', 'tabindex' => '3' ) ) .
168 "</td>
169 </tr>" .
170 Html::closeElement( 'table' ) .
171 Html::closeElement( 'fieldset' ) .
172 Html::hidden( 'wpEditToken', $wgUser->editToken() ) .
173 Html::closeElement( 'form' ) . "\n"
174 );
175
176 }
177
178 const UNBLOCK_SUCCESS = 0; // Success
179 const UNBLOCK_NO_SUCH_ID = 1; // No such block ID
180 const UNBLOCK_USER_NOT_BLOCKED = 2; // IP wasn't blocked
181 const UNBLOCK_BLOCKED_AS_RANGE = 3; // IP is part of a range block
182 const UNBLOCK_UNKNOWNERR = 4; // Unknown error
183
184 /**
185 * Backend code for unblocking. doSubmit() wraps around this.
186 * $range is only used when UNBLOCK_BLOCKED_AS_RANGE is returned, in which
187 * case it contains the range $ip is part of.
188 * @return array array(message key, parameters) on failure, empty array on success
189 */
190 public static function doUnblock( &$id, &$ip, &$reason, &$range = null, $blocker = null ) {
191 if ( $id ) {
192 $block = Block::newFromID( $id );
193 if ( !$block ) {
194 return array( 'ipb_cant_unblock', htmlspecialchars( $id ) );
195 }
196 $ip = $block->getRedactedName();
197 } else {
198 $block = new Block();
199 $ip = trim( $ip );
200 if ( substr( $ip, 0, 1 ) == "#" ) {
201 $id = substr( $ip, 1 );
202 $block = Block::newFromID( $id );
203 if( !$block ) {
204 return array( 'ipb_cant_unblock', htmlspecialchars( $id ) );
205 }
206 $ip = $block->getRedactedName();
207 } else {
208 $block = Block::newFromDB( $ip );
209 if ( !$block ) {
210 return array( 'ipb_cant_unblock', htmlspecialchars( $id ) );
211 }
212 if( $block->mRangeStart != $block->mRangeEnd && !strstr( $ip, "/" ) ) {
213 /* If the specified IP is a single address, and the block is
214 * a range block, don't unblock the range. */
215 $range = $block->mAddress;
216 return array( 'ipb_blocked_as_range', $ip, $range );
217 }
218 }
219 }
220 // Yes, this is really necessary
221 $id = $block->mId;
222
223 # If the name was hidden and the blocking user cannot hide
224 # names, then don't allow any block removals...
225 if( $blocker && $block->mHideName && !$blocker->isAllowed( 'hideuser' ) ) {
226 return array( 'ipb_cant_unblock', htmlspecialchars( $id ) );
227 }
228
229 # Delete block
230 if ( !$block->delete() ) {
231 return array( 'ipb_cant_unblock', htmlspecialchars( $id ) );
232 }
233
234 # Unset _deleted fields as needed
235 if( $block->mHideName ) {
236 IPBlockForm::unsuppressUserName( $block->mAddress, $block->mUser );
237 }
238
239 # Make log entry
240 $log = new LogPage( 'block' );
241 $log->addEntry( 'unblock', Title::makeTitle( NS_USER, $ip ), $reason );
242 return array();
243 }
244
245 function doSubmit() {
246 global $wgOut, $wgUser;
247
248 $retval = self::doUnblock( $this->id, $this->ip, $this->reason, $range, $wgUser );
249 if( !empty( $retval ) ) {
250 $key = array_shift( $retval );
251 $this->showForm( wfMsgReal( $key, $retval ) );
252 return;
253 }
254
255 # Report to the user
256 $success = $this->getTitle()->getFullURL( 'action=success&successip=' . urlencode( $this->ip ) );
257 $wgOut->redirect( $success );
258 }
259
260 function showList( $msg ) {
261 global $wgOut, $wgUser;
262
263 if ( $msg != '' ) {
264 $wgOut->setSubtitle( $msg );
265 }
266
267 // Purge expired entries on one in every 10 queries
268 if ( !mt_rand( 0, 10 ) ) {
269 Block::purgeExpired();
270 }
271
272 $conds = array();
273 // Is user allowed to see all the blocks?
274 if ( !$wgUser->isAllowed( 'hideuser' ) )
275 $conds['ipb_deleted'] = 0;
276 if ( $this->ip == '' ) {
277 // No extra conditions
278 } elseif ( substr( $this->ip, 0, 1 ) == '#' ) {
279 $conds['ipb_id'] = substr( $this->ip, 1 );
280 // Single IPs
281 } elseif ( IP::isIPAddress( $this->ip ) && strpos( $this->ip, '/' ) === false ) {
282 if( $iaddr = IP::toHex( $this->ip ) ) {
283 # Only scan ranges which start in this /16, this improves search speed
284 # Blocks should not cross a /16 boundary.
285 $range = substr( $iaddr, 0, 4 );
286 // Fixme -- encapsulate this sort of query-building.
287 $dbr = wfGetDB( DB_SLAVE );
288 $encIp = $dbr->addQuotes( IP::sanitizeIP( $this->ip ) );
289 $encAddr = $dbr->addQuotes( $iaddr );
290 $conds[] = "(ipb_address = $encIp) OR
291 (ipb_range_start" . $dbr->buildLike( $range, $dbr->anyString() ) . " AND
292 ipb_range_start <= $encAddr
293 AND ipb_range_end >= $encAddr)";
294 } else {
295 $conds['ipb_address'] = IP::sanitizeIP( $this->ip );
296 }
297 $conds['ipb_auto'] = 0;
298 // IP range
299 } elseif ( IP::isIPAddress( $this->ip ) ) {
300 $conds['ipb_address'] = Block::normaliseRange( $this->ip );
301 $conds['ipb_auto'] = 0;
302 } else {
303 $user = User::newFromName( $this->ip );
304 if ( $user && ( $id = $user->getId() ) != 0 ) {
305 $conds['ipb_user'] = $id;
306 } else {
307 // Uh...?
308 $conds['ipb_address'] = $this->ip;
309 $conds['ipb_auto'] = 0;
310 }
311 }
312 // Apply filters
313 if( $this->hideuserblocks ) {
314 $conds['ipb_user'] = 0;
315 }
316 if( $this->hidetempblocks ) {
317 $conds['ipb_expiry'] = 'infinity';
318 }
319 if( $this->hideaddressblocks ) {
320 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
321 }
322
323 // Search form
324 $wgOut->addHTML( $this->searchForm() );
325
326 // Check for other blocks, i.e. global/tor blocks
327 $otherBlockLink = array();
328 wfRunHooks( 'OtherBlockLogLink', array( &$otherBlockLink, $this->ip ) );
329
330 // Show additional header for the local block only when other blocks exists.
331 // Not necessary in a standard installation without such extensions enabled
332 if( count( $otherBlockLink ) ) {
333 $wgOut->addHTML(
334 Html::rawElement( 'h2', array(), wfMsg( 'ipblocklist-localblock' ) ) . "\n"
335 );
336 }
337 $pager = new IPBlocklistPager( $this, $conds );
338 if ( $pager->getNumRows() ) {
339 $wgOut->addHTML(
340 $pager->getNavigationBar() .
341 Html::rawElement( 'ul', null, $pager->getBody() ) .
342 $pager->getNavigationBar()
343 );
344 } elseif ( $this->ip != '') {
345 $wgOut->addWikiMsg( 'ipblocklist-no-results' );
346 } else {
347 $wgOut->addWikiMsg( 'ipblocklist-empty' );
348 }
349
350 if( count( $otherBlockLink ) ) {
351 $wgOut->addHTML(
352 Html::rawElement( 'h2', array(), wfMsgExt( 'ipblocklist-otherblocks', 'parseinline', count( $otherBlockLink ) ) ) . "\n"
353 );
354 $list = '';
355 foreach( $otherBlockLink as $link ) {
356 $list .= Html::rawElement( 'li', array(), $link ) . "\n";
357 }
358 $wgOut->addHTML( Html::rawElement( 'ul', array( 'class' => 'mw-ipblocklist-otherblocks' ), $list ) . "\n" );
359 }
360
361 }
362
363 function searchForm() {
364 global $wgScript, $wgLang;
365
366 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ) );
367 $nondefaults = array();
368 if( $this->hideuserblocks ) {
369 $nondefaults['hideuserblocks'] = $this->hideuserblocks;
370 }
371 if( $this->hidetempblocks ) {
372 $nondefaults['hidetempblocks'] = $this->hidetempblocks;
373 }
374 if( $this->hideaddressblocks ) {
375 $nondefaults['hideaddressblocks'] = $this->hideaddressblocks;
376 }
377 $ubLink = $this->makeOptionsLink( $showhide[1-$this->hideuserblocks],
378 array( 'hideuserblocks' => 1-$this->hideuserblocks ), $nondefaults);
379 $tbLink = $this->makeOptionsLink( $showhide[1-$this->hidetempblocks],
380 array( 'hidetempblocks' => 1-$this->hidetempblocks ), $nondefaults);
381 $sipbLink = $this->makeOptionsLink( $showhide[1-$this->hideaddressblocks],
382 array( 'hideaddressblocks' => 1-$this->hideaddressblocks ), $nondefaults);
383
384 $links = array();
385 $links[] = wfMsgHtml( 'ipblocklist-sh-userblocks', $ubLink );
386 $links[] = wfMsgHtml( 'ipblocklist-sh-tempblocks', $tbLink );
387 $links[] = wfMsgHtml( 'ipblocklist-sh-addressblocks', $sipbLink );
388 $hl = $wgLang->pipeList( $links );
389
390 return
391 Html::rawElement( 'form', array( 'action' => $wgScript ),
392 Html::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) .
393 Html::openElement( 'fieldset' ) .
394 Html::element( 'legend', null, wfMsg( 'ipblocklist-legend' ) ) .
395 Xml::inputLabel( wfMsg( 'ipblocklist-username' ), 'ip', 'ip', /* size */ false, $this->ip ) .
396 '&#160;' .
397 Xml::submitButton( wfMsg( 'ipblocklist-submit' ) ) . '<br />' .
398 $hl .
399 Html::closeElement( 'fieldset' )
400 );
401 }
402
403 /**
404 * Makes change an option link which carries all the other options
405 * @param $title see Title
406 * @param $override
407 * @param $options
408 */
409 function makeOptionsLink( $title, $override, $options, $active = false ) {
410 global $wgUser;
411 $sk = $wgUser->getSkin();
412 $params = $override + $options;
413 return $sk->link( $this->getTitle(), htmlspecialchars( $title ),
414 ( $active ? array( 'style'=>'font-weight: bold;' ) : array() ), $params, array( 'known' ) );
415 }
416
417 /**
418 * Callback function to output a block
419 */
420 function formatRow( $block ) {
421 global $wgUser, $wgLang, $wgBlockAllowsUTEdit;
422
423 wfProfileIn( __METHOD__ );
424
425 static $sk=null, $msg=null;
426
427 if( is_null( $sk ) )
428 $sk = $wgUser->getSkin();
429 if( is_null( $msg ) ) {
430 $msg = array();
431 $keys = array( 'infiniteblock', 'expiringblock', 'unblocklink', 'change-blocklink',
432 'anononlyblock', 'createaccountblock', 'noautoblockblock', 'emailblock', 'blocklist-nousertalk', 'blocklistline' );
433 foreach( $keys as $key ) {
434 $msg[$key] = wfMsgHtml( $key );
435 }
436 }
437
438 # Prepare links to the blocker's user and talk pages
439 $blocker_id = $block->getBy();
440 $blocker_name = $block->getByName();
441 $blocker = $sk->userLink( $blocker_id, $blocker_name );
442 $blocker .= $sk->userToolLinks( $blocker_id, $blocker_name );
443
444 # Prepare links to the block target's user and contribs. pages (as applicable, don't do it for autoblocks)
445 if( $block->mAuto ) {
446 $target = $block->getRedactedName(); # Hide the IP addresses of auto-blocks; privacy
447 } else {
448 $target = $sk->userLink( $block->mUser, $block->mAddress )
449 . $sk->userToolLinks( $block->mUser, $block->mAddress, false, Linker::TOOL_LINKS_NOBLOCK );
450 }
451
452 $formattedTime = htmlspecialchars( $wgLang->timeanddate( $block->mTimestamp, true ) );
453
454 $properties = array();
455 $properties[] = Block::formatExpiry( $block->mExpiry );
456 if ( $block->mAnonOnly ) {
457 $properties[] = $msg['anononlyblock'];
458 }
459 if ( $block->mCreateAccount ) {
460 $properties[] = $msg['createaccountblock'];
461 }
462 if (!$block->mEnableAutoblock && $block->mUser ) {
463 $properties[] = $msg['noautoblockblock'];
464 }
465
466 if ( $block->mBlockEmail && $block->mUser ) {
467 $properties[] = $msg['emailblock'];
468 }
469
470 if ( !$block->mAllowUsertalk && $wgBlockAllowsUTEdit ) {
471 $properties[] = $msg['blocklist-nousertalk'];
472 }
473
474 $properties = $wgLang->commaList( $properties );
475
476 $line = wfMsgReplaceArgs( $msg['blocklistline'], array( $formattedTime, $blocker, $target, $properties ) );
477
478 $unblocklink = '';
479 $changeblocklink = '';
480 $toolLinks = '';
481 if ( $wgUser->isAllowed( 'block' ) ) {
482 $unblocklink = $sk->link( $this->getTitle(),
483 $msg['unblocklink'],
484 array(),
485 array( 'action' => 'unblock', 'id' => $block->mId ),
486 'known' );
487
488 # Create changeblocklink for all blocks with exception of autoblocks
489 if( !$block->mAuto ) {
490 $changeblocklink = wfMsgExt( 'pipe-separator', 'escapenoentities' ) .
491 $sk->link( SpecialPage::getTitleFor( 'Blockip', $block->mAddress ),
492 $msg['change-blocklink'],
493 array(), array(), 'known' );
494 }
495 $toolLinks = "($unblocklink$changeblocklink)";
496 }
497
498 $comment = $sk->commentBlock( htmlspecialchars($block->mReason) );
499
500 $s = "{$line} $comment";
501 if ( $block->mHideName )
502 $s = '<span class="history-deleted">' . $s . '</span>';
503
504 wfProfileOut( __METHOD__ );
505 return "<li>$s $toolLinks</li>\n";
506 }
507 }
508
509 /**
510 * @todo document
511 * @ingroup Pager
512 */
513 class IPBlocklistPager extends ReverseChronologicalPager {
514 public $mForm, $mConds;
515
516 function __construct( $form, $conds = array() ) {
517 $this->mForm = $form;
518 $this->mConds = $conds;
519 parent::__construct();
520 }
521
522 function getStartBody() {
523 wfProfileIn( __METHOD__ );
524 # Do a link batch query
525 $this->mResult->seek( 0 );
526 $lb = new LinkBatch;
527
528 /*
529 while ( $row = $this->mResult->fetchObject() ) {
530 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
531 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
532 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->ipb_address ) );
533 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ipb_address ) );
534 }*/
535 # Faster way
536 # Usernames and titles are in fact related by a simple substitution of space -> underscore
537 # The last few lines of Title::secureAndSplit() tell the story.
538 while ( $row = $this->mResult->fetchObject() ) {
539 $name = str_replace( ' ', '_', $row->ipb_by_text );
540 $lb->add( NS_USER, $name );
541 $lb->add( NS_USER_TALK, $name );
542 $name = str_replace( ' ', '_', $row->ipb_address );
543 $lb->add( NS_USER, $name );
544 $lb->add( NS_USER_TALK, $name );
545 }
546 $lb->execute();
547 wfProfileOut( __METHOD__ );
548 return '';
549 }
550
551 function formatRow( $row ) {
552 $block = new Block;
553 $block->initFromRow( $row );
554 return $this->mForm->formatRow( $block );
555 }
556
557 function getQueryInfo() {
558 $conds = $this->mConds;
559 $conds[] = 'ipb_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() );
560 return array(
561 'tables' => 'ipblocks',
562 'fields' => '*',
563 'conds' => $conds,
564 );
565 }
566
567 function getIndexField() {
568 return 'ipb_timestamp';
569 }
570 }