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