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