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