* ( bug 18264 ) upload-by-url form was still disabled in non-js2 form output
[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 = trim( $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, $blocker=null) {
166 if ( $id ) {
167 $block = Block::newFromID( $id );
168 if ( !$block ) {
169 return array('ipb_cant_unblock', htmlspecialchars($id));
170 }
171 $ip = $block->getRedactedName();
172 } else {
173 $block = new Block();
174 $ip = trim( $ip );
175 if ( substr( $ip, 0, 1 ) == "#" ) {
176 $id = substr( $ip, 1 );
177 $block = Block::newFromID( $id );
178 if( !$block ) {
179 return array('ipb_cant_unblock', htmlspecialchars($id));
180 }
181 $ip = $block->getRedactedName();
182 } else {
183 $block = Block::newFromDB( $ip );
184 if ( !$block ) {
185 return array('ipb_cant_unblock', htmlspecialchars($id));
186 }
187 if( $block->mRangeStart != $block->mRangeEnd && !strstr( $ip, "/" ) ) {
188 /* If the specified IP is a single address, and the block is
189 * a range block, don't unblock the range. */
190 $range = $block->mAddress;
191 return array('ipb_blocked_as_range', $ip, $range);
192 }
193 }
194 }
195 // Yes, this is really necessary
196 $id = $block->mId;
197
198 # If the name was hidden and the blocking user cannot hide
199 # names, then don't allow any block removals...
200 if( $blocker && $block->mHideName && !$blocker->isAllowed('hideuser') ) {
201 return array('ipb_cant_unblock', htmlspecialchars($id));
202 }
203
204 # Delete block
205 if ( !$block->delete() ) {
206 return array('ipb_cant_unblock', htmlspecialchars($id));
207 }
208
209 # Unset _deleted fields as needed
210 if( $block->mHideName ) {
211 IPBlockForm::unsuppressUserName( $block->mAddress, $block->mUser );
212 }
213
214 # Make log entry
215 $log = new LogPage( 'block' );
216 $log->addEntry( 'unblock', Title::makeTitle( NS_USER, $ip ), $reason );
217 return array();
218 }
219
220 function doSubmit() {
221 global $wgOut, $wgUser;
222 $retval = self::doUnblock($this->id, $this->ip, $this->reason, $range, $wgUser);
223 if( !empty($retval) ) {
224 $key = array_shift($retval);
225 $this->showForm(wfMsgReal($key, $retval));
226 return;
227 }
228 # Report to the user
229 $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
230 $success = $titleObj->getFullURL( "action=success&successip=" . urlencode( $this->ip ) );
231 $wgOut->redirect( $success );
232 }
233
234 function showList( $msg ) {
235 global $wgOut, $wgUser;
236
237 $wgOut->setPagetitle( wfMsg( "ipblocklist" ) );
238 if ( "" != $msg ) {
239 $wgOut->setSubtitle( $msg );
240 }
241
242 // Purge expired entries on one in every 10 queries
243 if ( !mt_rand( 0, 10 ) ) {
244 Block::purgeExpired();
245 }
246
247 $conds = array();
248 $matches = array();
249 // Is user allowed to see all the blocks?
250 if ( !$wgUser->isAllowed( 'hideuser' ) )
251 $conds['ipb_deleted'] = 0;
252 if ( $this->ip == '' ) {
253 // No extra conditions
254 } elseif ( substr( $this->ip, 0, 1 ) == '#' ) {
255 $conds['ipb_id'] = substr( $this->ip, 1 );
256 // Single IPs
257 } elseif ( IP::isIPAddress($this->ip) && strpos($this->ip,'/') === false ) {
258 if( $iaddr = IP::toHex($this->ip) ) {
259 # Only scan ranges which start in this /16, this improves search speed
260 # Blocks should not cross a /16 boundary.
261 $range = substr( $iaddr, 0, 4 );
262 // Fixme -- encapsulate this sort of query-building.
263 $dbr = wfGetDB( DB_SLAVE );
264 $encIp = $dbr->addQuotes( IP::sanitizeIP($this->ip) );
265 $encRange = $dbr->addQuotes( "$range%" );
266 $encAddr = $dbr->addQuotes( $iaddr );
267 $conds[] = "(ipb_address = $encIp) OR
268 (ipb_range_start LIKE $encRange AND
269 ipb_range_start <= $encAddr
270 AND ipb_range_end >= $encAddr)";
271 } else {
272 $conds['ipb_address'] = IP::sanitizeIP($this->ip);
273 }
274 $conds['ipb_auto'] = 0;
275 // IP range
276 } elseif ( IP::isIPAddress($this->ip) ) {
277 $conds['ipb_address'] = Block::normaliseRange( $this->ip );
278 $conds['ipb_auto'] = 0;
279 } else {
280 $user = User::newFromName( $this->ip );
281 if ( $user && ( $id = $user->getId() ) != 0 ) {
282 $conds['ipb_user'] = $id;
283 } else {
284 // Uh...?
285 $conds['ipb_address'] = $this->ip;
286 $conds['ipb_auto'] = 0;
287 }
288 }
289 // Apply filters
290 if( $this->hideuserblocks ) {
291 $conds['ipb_user'] = 0;
292 }
293 if( $this->hidetempblocks ) {
294 $conds['ipb_expiry'] = 'infinity';
295 }
296 if( $this->hideaddressblocks ) {
297 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
298 }
299
300 $pager = new IPBlocklistPager( $this, $conds );
301 if ( $pager->getNumRows() ) {
302 $wgOut->addHTML(
303 $this->searchForm() .
304 $pager->getNavigationBar() .
305 Xml::tags( 'ul', null, $pager->getBody() ) .
306 $pager->getNavigationBar()
307 );
308 } elseif ( $this->ip != '') {
309 $wgOut->addHTML( $this->searchForm() );
310 $wgOut->addWikiMsg( 'ipblocklist-no-results' );
311 } else {
312 $wgOut->addHTML( $this->searchForm() );
313 $wgOut->addWikiMsg( 'ipblocklist-empty' );
314 }
315 }
316
317 function searchForm() {
318 global $wgScript, $wgRequest, $wgLang;
319
320 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ) );
321 $nondefaults = array();
322 if( $this->hideuserblocks ) {
323 $nondefaults['hideuserblocks'] = $this->hideuserblocks;
324 }
325 if( $this->hidetempblocks ) {
326 $nondefaults['hidetempblocks'] = $this->hidetempblocks;
327 }
328 if( $this->hideaddressblocks ) {
329 $nondefaults['hideaddressblocks'] = $this->hideaddressblocks;
330 }
331 $ubLink = $this->makeOptionsLink( $showhide[1-$this->hideuserblocks],
332 array( 'hideuserblocks' => 1-$this->hideuserblocks ), $nondefaults);
333 $tbLink = $this->makeOptionsLink( $showhide[1-$this->hidetempblocks],
334 array( 'hidetempblocks' => 1-$this->hidetempblocks ), $nondefaults);
335 $sipbLink = $this->makeOptionsLink( $showhide[1-$this->hideaddressblocks],
336 array( 'hideaddressblocks' => 1-$this->hideaddressblocks ), $nondefaults);
337
338 $links = array();
339 $links[] = wfMsgHtml( 'ipblocklist-sh-userblocks', $ubLink );
340 $links[] = wfMsgHtml( 'ipblocklist-sh-tempblocks', $tbLink );
341 $links[] = wfMsgHtml( 'ipblocklist-sh-addressblocks', $sipbLink );
342 $hl = $wgLang->pipeList( $links );
343
344 return
345 Xml::tags( 'form', array( 'action' => $wgScript ),
346 Xml::hidden( 'title', SpecialPage::getTitleFor( 'Ipblocklist' )->getPrefixedDbKey() ) .
347 Xml::openElement( 'fieldset' ) .
348 Xml::element( 'legend', null, wfMsg( 'ipblocklist-legend' ) ) .
349 Xml::inputLabel( wfMsg( 'ipblocklist-username' ), 'ip', 'ip', /* size */ false, $this->ip ) .
350 '&nbsp;' .
351 Xml::submitButton( wfMsg( 'ipblocklist-submit' ) ) . '<br />' .
352 $hl .
353 Xml::closeElement( 'fieldset' )
354 );
355 }
356
357 /**
358 * Makes change an option link which carries all the other options
359 * @param $title see Title
360 * @param $override
361 * @param $options
362 */
363 function makeOptionsLink( $title, $override, $options, $active = false ) {
364 global $wgUser;
365 $sk = $wgUser->getSkin();
366 $params = $override + $options;
367 $ipblocklist = SpecialPage::getTitleFor( 'IPBlockList' );
368 return $sk->link( $ipblocklist, htmlspecialchars( $title ),
369 ( $active ? array( 'style'=>'font-weight: bold;' ) : array() ), $params, array( 'known' ) );
370 }
371
372 /**
373 * Callback function to output a block
374 */
375 function formatRow( $block ) {
376 global $wgUser, $wgLang, $wgBlockAllowsUTEdit;
377
378 wfProfileIn( __METHOD__ );
379
380 static $sk=null, $msg=null;
381
382 if( is_null( $sk ) )
383 $sk = $wgUser->getSkin();
384 if( is_null( $msg ) ) {
385 $msg = array();
386 $keys = array( 'infiniteblock', 'expiringblock', 'unblocklink', 'change-blocklink',
387 'anononlyblock', 'createaccountblock', 'noautoblockblock', 'emailblock', 'blocklist-nousertalk', 'blocklistline' );
388 foreach( $keys as $key ) {
389 $msg[$key] = wfMsgHtml( $key );
390 }
391 }
392
393 # Prepare links to the blocker's user and talk pages
394 $blocker_id = $block->getBy();
395 $blocker_name = $block->getByName();
396 $blocker = $sk->userLink( $blocker_id, $blocker_name );
397 $blocker .= $sk->userToolLinks( $blocker_id, $blocker_name );
398
399 # Prepare links to the block target's user and contribs. pages (as applicable, don't do it for autoblocks)
400 if( $block->mAuto ) {
401 $target = $block->getRedactedName(); # Hide the IP addresses of auto-blocks; privacy
402 } else {
403 $target = $sk->userLink( $block->mUser, $block->mAddress )
404 . $sk->userToolLinks( $block->mUser, $block->mAddress, false, Linker::TOOL_LINKS_NOBLOCK );
405 }
406
407 $formattedTime = htmlspecialchars( $wgLang->timeanddate( $block->mTimestamp, true ) );
408
409 $properties = array();
410 $properties[] = Block::formatExpiry( $block->mExpiry );
411 if ( $block->mAnonOnly ) {
412 $properties[] = $msg['anononlyblock'];
413 }
414 if ( $block->mCreateAccount ) {
415 $properties[] = $msg['createaccountblock'];
416 }
417 if (!$block->mEnableAutoblock && $block->mUser ) {
418 $properties[] = $msg['noautoblockblock'];
419 }
420
421 if ( $block->mBlockEmail && $block->mUser ) {
422 $properties[] = $msg['emailblock'];
423 }
424
425 if ( !$block->mAllowUsertalk && $wgBlockAllowsUTEdit ) {
426 $properties[] = $msg['blocklist-nousertalk'];
427 }
428
429 $properties = $wgLang->commaList( $properties );
430
431 $line = wfMsgReplaceArgs( $msg['blocklistline'], array( $formattedTime, $blocker, $target, $properties ) );
432
433 $unblocklink = '';
434 $changeblocklink = '';
435 $toolLinks = '';
436 if ( $wgUser->isAllowed( 'block' ) ) {
437 $unblocklink = $sk->link( SpecialPage::getTitleFor( 'Ipblocklist' ),
438 $msg['unblocklink'],
439 array(),
440 array( 'action' => 'unblock', 'id' => $block->mId ),
441 'known' );
442
443 # Create changeblocklink for all blocks with exception of autoblocks
444 if( !$block->mAuto ) {
445 $changeblocklink = wfMsgExt( 'pipe-separator', 'escapenoentities' ) .
446 $sk->link( SpecialPage::getTitleFor( 'Blockip', $block->mAddress ),
447 $msg['change-blocklink'],
448 array(), array(), 'known' );
449 }
450 $toolLinks = "($unblocklink$changeblocklink)";
451 }
452
453 $comment = $sk->commentBlock( htmlspecialchars($block->mReason) );
454
455 $s = "{$line} $comment";
456 if ( $block->mHideName )
457 $s = '<span class="history-deleted">' . $s . '</span>';
458
459 wfProfileOut( __METHOD__ );
460 return "<li>$s $toolLinks</li>\n";
461 }
462 }
463
464 /**
465 * @todo document
466 * @ingroup Pager
467 */
468 class IPBlocklistPager extends ReverseChronologicalPager {
469 public $mForm, $mConds;
470
471 function __construct( $form, $conds = array() ) {
472 $this->mForm = $form;
473 $this->mConds = $conds;
474 parent::__construct();
475 }
476
477 function getStartBody() {
478 wfProfileIn( __METHOD__ );
479 # Do a link batch query
480 $this->mResult->seek( 0 );
481 $lb = new LinkBatch;
482
483 /*
484 while ( $row = $this->mResult->fetchObject() ) {
485 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
486 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
487 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->ipb_address ) );
488 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ipb_address ) );
489 }*/
490 # Faster way
491 # Usernames and titles are in fact related by a simple substitution of space -> underscore
492 # The last few lines of Title::secureAndSplit() tell the story.
493 while ( $row = $this->mResult->fetchObject() ) {
494 $name = str_replace( ' ', '_', $row->ipb_by_text );
495 $lb->add( NS_USER, $name );
496 $lb->add( NS_USER_TALK, $name );
497 $name = str_replace( ' ', '_', $row->ipb_address );
498 $lb->add( NS_USER, $name );
499 $lb->add( NS_USER_TALK, $name );
500 }
501 $lb->execute();
502 wfProfileOut( __METHOD__ );
503 return '';
504 }
505
506 function formatRow( $row ) {
507 $block = new Block;
508 $block->initFromRow( $row );
509 return $this->mForm->formatRow( $block );
510 }
511
512 function getQueryInfo() {
513 $conds = $this->mConds;
514 $conds[] = 'ipb_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() );
515 return array(
516 'tables' => 'ipblocks',
517 'fields' => '*',
518 'conds' => $conds,
519 );
520 }
521
522 function getIndexField() {
523 return 'ipb_timestamp';
524 }
525 }