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