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