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