]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/specials/SpecialBlockip.php
MediaWiki 1.15.1
[autoinstallsdev/mediawiki.git] / includes / specials / SpecialBlockip.php
1 <?php
2 /**
3  * Constructor for Special:Blockip page
4  *
5  * @file
6  * @ingroup SpecialPage
7  */
8
9 /**
10  * Constructor
11  */
12 function wfSpecialBlockip( $par ) {
13         global $wgUser, $wgOut, $wgRequest;
14
15         # Can't block when the database is locked
16         if( wfReadOnly() ) {
17                 $wgOut->readOnlyPage();
18                 return;
19         }
20
21         # Permission check
22         if( !$wgUser->isAllowed( 'block' ) ) {
23                 $wgOut->permissionRequired( 'block' );
24                 return;
25         }
26
27         $ipb = new IPBlockForm( $par );
28
29         $action = $wgRequest->getVal( 'action' );
30         if ( 'success' == $action ) {
31                 $ipb->showSuccess();
32         } else if ( $wgRequest->wasPosted() && 'submit' == $action &&
33                 $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
34                 $ipb->doSubmit();
35         } else {
36                 $ipb->showForm( '' );
37         }
38 }
39
40 /**
41  * Form object for the Special:Blockip page.
42  *
43  * @ingroup SpecialPage
44  */
45 class IPBlockForm {
46         var $BlockAddress, $BlockExpiry, $BlockReason;
47 #       var $BlockEmail;
48         // The maximum number of edits a user can have and still be hidden
49         const HIDEUSER_CONTRIBLIMIT = 1000;
50
51         function IPBlockForm( $par ) {
52                 global $wgRequest, $wgUser, $wgBlockAllowsUTEdit;
53
54                 $this->BlockAddress = $wgRequest->getVal( 'wpBlockAddress', $wgRequest->getVal( 'ip', $par ) );
55                 $this->BlockAddress = strtr( $this->BlockAddress, '_', ' ' );
56                 $this->BlockReason = $wgRequest->getText( 'wpBlockReason' );
57                 $this->BlockReasonList = $wgRequest->getText( 'wpBlockReasonList' );
58                 $this->BlockExpiry = $wgRequest->getVal( 'wpBlockExpiry', wfMsg('ipbotheroption') );
59                 $this->BlockOther = $wgRequest->getVal( 'wpBlockOther', '' );
60
61                 # Unchecked checkboxes are not included in the form data at all, so having one
62                 # that is true by default is a bit tricky
63                 $byDefault = !$wgRequest->wasPosted();
64                 $this->BlockAnonOnly = $wgRequest->getBool( 'wpAnonOnly', $byDefault );
65                 $this->BlockCreateAccount = $wgRequest->getBool( 'wpCreateAccount', $byDefault );
66                 $this->BlockEnableAutoblock = $wgRequest->getBool( 'wpEnableAutoblock', $byDefault );
67                 $this->BlockEmail = $wgRequest->getBool( 'wpEmailBan', false );
68                 $this->BlockWatchUser = $wgRequest->getBool( 'wpWatchUser', false );
69                 # Re-check user's rights to hide names, very serious, defaults to 0
70                 $this->BlockHideName = ( $wgRequest->getBool( 'wpHideName', 0 ) && $wgUser->isAllowed( 'hideuser' ) ) ? 1 : 0;
71                 $this->BlockAllowUsertalk = ( $wgRequest->getBool( 'wpAllowUsertalk', $byDefault ) && $wgBlockAllowsUTEdit );
72                 $this->BlockReblock = $wgRequest->getBool( 'wpChangeBlock', false );
73         }
74
75         function showForm( $err ) {
76                 global $wgOut, $wgUser, $wgSysopUserBans;
77
78                 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
79                 $wgOut->addWikiMsg( 'blockiptext' );
80
81                 if($wgSysopUserBans) {
82                         $mIpaddress = Xml::label( wfMsg( 'ipadressorusername' ), 'mw-bi-target' );
83                 } else {
84                         $mIpaddress = Xml::label( wfMsg( 'ipaddress' ), 'mw-bi-target' );
85                 }
86                 $mIpbexpiry = Xml::label( wfMsg( 'ipbexpiry' ), 'wpBlockExpiry' );
87                 $mIpbother = Xml::label( wfMsg( 'ipbother' ), 'mw-bi-other' );
88                 $mIpbreasonother = Xml::label( wfMsg( 'ipbreason' ), 'wpBlockReasonList' );
89                 $mIpbreason = Xml::label( wfMsg( 'ipbotherreason' ), 'mw-bi-reason' );
90
91                 $titleObj = SpecialPage::getTitleFor( 'Blockip' );
92                 $user = User::newFromName( $this->BlockAddress );
93                 
94                 $alreadyBlocked = false;
95                 if ( $err && $err[0] != 'ipb_already_blocked' ) {
96                         $key = array_shift($err);
97                         $msg = wfMsgReal($key, $err);
98                         $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
99                         $wgOut->addHTML( Xml::tags( 'p', array( 'class' => 'error' ), $msg ) );
100                 } elseif ( $this->BlockAddress ) {
101                         $userId = 0;
102                         if ( is_object( $user ) )
103                                 $userId = $user->getId();
104                         $currentBlock = Block::newFromDB( $this->BlockAddress, $userId );
105                         if ( !is_null($currentBlock) && !$currentBlock->mAuto && # The block exists and isn't an autoblock
106                                 ( $currentBlock->mRangeStart == $currentBlock->mRangeEnd || # The block isn't a rangeblock
107                                 # or if it is, the range is what we're about to block
108                                 ( $currentBlock->mAddress == $this->BlockAddress ) ) ) {
109                                         $wgOut->addWikiMsg( 'ipb-needreblock', $this->BlockAddress );
110                                         $alreadyBlocked = true;
111                                         # Set the block form settings to the existing block
112                                         $this->BlockAnonOnly = $currentBlock->mAnonOnly;
113                                         $this->BlockCreateAccount = $currentBlock->mCreateAccount;
114                                         $this->BlockEnableAutoblock = $currentBlock->mEnableAutoblock;
115                                         $this->BlockEmail = $currentBlock->mBlockEmail;
116                                         $this->BlockHideName = $currentBlock->mHideName;
117                                         $this->BlockAllowUsertalk = $currentBlock->mAllowUsertalk;
118                                         if( $currentBlock->mExpiry == 'infinity' ) {
119                                                 $this->BlockOther = 'indefinite';
120                                         } else {
121                                                 $this->BlockOther = wfTimestamp( TS_ISO_8601, $currentBlock->mExpiry );
122                                         }
123                                         $this->BlockReason = $currentBlock->mReason;
124                         }
125                 }
126
127                 $scBlockExpiryOptions = wfMsgForContent( 'ipboptions' );
128
129                 $showblockoptions = $scBlockExpiryOptions != '-';
130                 if (!$showblockoptions)
131                         $mIpbother = $mIpbexpiry;
132
133                 $blockExpiryFormOptions = Xml::option( wfMsg( 'ipbotheroption' ), 'other' );
134                 foreach (explode(',', $scBlockExpiryOptions) as $option) {
135                         if ( strpos($option, ":") === false ) $option = "$option:$option";
136                         list($show, $value) = explode(":", $option);
137                         $show = htmlspecialchars($show);
138                         $value = htmlspecialchars($value);
139                         $blockExpiryFormOptions .= Xml::option( $show, $value, $this->BlockExpiry === $value ? true : false ) . "\n";
140                 }
141
142                 $reasonDropDown = Xml::listDropDown( 'wpBlockReasonList',
143                         wfMsgForContent( 'ipbreason-dropdown' ),
144                         wfMsgForContent( 'ipbreasonotherlist' ), $this->BlockReasonList, 'wpBlockDropDown', 4 );
145
146                 global $wgStylePath, $wgStyleVersion;
147                 $wgOut->addHTML(
148                         Xml::tags( 'script', array( 'type' => 'text/javascript', 'src' => "$wgStylePath/common/block.js?$wgStyleVersion" ), '' ) .
149                         Xml::openElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalURL( "action=submit" ), 'id' => 'blockip' ) ) .
150                         Xml::openElement( 'fieldset' ) .
151                         Xml::element( 'legend', null, wfMsg( 'blockip-legend' ) ) .
152                         Xml::openElement( 'table', array ( 'border' => '0', 'id' => 'mw-blockip-table' ) ) .
153                         "<tr>
154                                 <td class='mw-label'>
155                                         {$mIpaddress}
156                                 </td>
157                                 <td class='mw-input'>" .
158                                         Xml::input( 'wpBlockAddress', 45, $this->BlockAddress,
159                                                 array(
160                                                         'tabindex' => '1',
161                                                         'id' => 'mw-bi-target',
162                                                         'onchange' => 'updateBlockOptions()' ) ). "
163                                 </td>
164                         </tr>
165                         <tr>"
166                 );
167                 if ( $showblockoptions ) {
168                         $wgOut->addHTML("
169                                 <td class='mw-label'>
170                                         {$mIpbexpiry}
171                                 </td>
172                                 <td class='mw-input'>" .
173                                         Xml::tags( 'select',
174                                                 array(
175                                                         'id' => 'wpBlockExpiry',
176                                                         'name' => 'wpBlockExpiry',
177                                                         'onchange' => 'considerChangingExpiryFocus()',
178                                                         'tabindex' => '2' ),
179                                                 $blockExpiryFormOptions ) .
180                                 "</td>"
181                         );
182                 }
183                 $wgOut->addHTML("
184                         </tr>
185                         <tr id='wpBlockOther'>
186                                 <td class='mw-label'>
187                                         {$mIpbother}
188                                 </td>
189                                 <td class='mw-input'>" .
190                                         Xml::input( 'wpBlockOther', 45, $this->BlockOther,
191                                                 array( 'tabindex' => '3', 'id' => 'mw-bi-other' ) ) . "
192                                 </td>
193                         </tr>
194                         <tr>
195                                 <td class='mw-label'>
196                                         {$mIpbreasonother}
197                                 </td>
198                                 <td class='mw-input'>
199                                         {$reasonDropDown}
200                                 </td>
201                         </tr>
202                         <tr id=\"wpBlockReason\">
203                                 <td class='mw-label'>
204                                         {$mIpbreason}
205                                 </td>
206                                 <td class='mw-input'>" .
207                                         Xml::input( 'wpBlockReason', 45, $this->BlockReason,
208                                                 array( 'tabindex' => '5', 'id' => 'mw-bi-reason', 'maxlength'=> '200' ) ) . "
209                                 </td>
210                         </tr>
211                         <tr id='wpAnonOnlyRow'>
212                                 <td>&nbsp;</td>
213                                 <td class='mw-input'>" .
214                                 Xml::checkLabel( wfMsg( 'ipbanononly' ),
215                                                 'wpAnonOnly', 'wpAnonOnly', $this->BlockAnonOnly,
216                                                 array( 'tabindex' => '6' ) ) . "
217                                 </td>
218                         </tr>
219                         <tr id='wpCreateAccountRow'>
220                                 <td>&nbsp;</td>
221                                 <td class='mw-input'>" .
222                                         Xml::checkLabel( wfMsg( 'ipbcreateaccount' ),
223                                                 'wpCreateAccount', 'wpCreateAccount', $this->BlockCreateAccount,
224                                                 array( 'tabindex' => '7' ) ) . "
225                                 </td>
226                         </tr>
227                         <tr id='wpEnableAutoblockRow'>
228                                 <td>&nbsp;</td>
229                                 <td class='mw-input'>" .
230                                         Xml::checkLabel( wfMsg( 'ipbenableautoblock' ),
231                                                 'wpEnableAutoblock', 'wpEnableAutoblock', $this->BlockEnableAutoblock,
232                                                 array( 'tabindex' => '8' ) ) . "
233                                 </td>
234                         </tr>"
235                 );
236
237                 global $wgSysopEmailBans, $wgBlockAllowsUTEdit;
238                 if ( $wgSysopEmailBans && $wgUser->isAllowed( 'blockemail' ) ) {
239                         $wgOut->addHTML("
240                                 <tr id='wpEnableEmailBan'>
241                                         <td>&nbsp;</td>
242                                         <td class='mw-input'>" .
243                                                 Xml::checkLabel( wfMsg( 'ipbemailban' ),
244                                                         'wpEmailBan', 'wpEmailBan', $this->BlockEmail,
245                                                         array( 'tabindex' => '9' )) . "
246                                         </td>
247                                 </tr>"
248                         );
249                 }
250
251                 // Allow some users to hide name from block log, blocklist and listusers
252                 if ( $wgUser->isAllowed( 'hideuser' ) ) {
253                         $wgOut->addHTML("
254                                 <tr id='wpEnableHideUser'>
255                                         <td>&nbsp;</td>
256                                         <td class='mw-input'><strong>" .
257                                                 Xml::checkLabel( wfMsg( 'ipbhidename' ),
258                                                         'wpHideName', 'wpHideName', $this->BlockHideName,
259                                                         array( 'tabindex' => '10' ) ) . "
260                                         </strong></td>
261                                 </tr>"
262                         );
263                 }
264                 
265                 # Watchlist their user page?
266                 $wgOut->addHTML("
267                         <tr id='wpEnableWatchUser'>
268                                 <td>&nbsp;</td>
269                                 <td class='mw-input'>" .
270                                         Xml::checkLabel( wfMsg( 'ipbwatchuser' ),
271                                                 'wpWatchUser', 'wpWatchUser', $this->BlockWatchUser,
272                                                 array( 'tabindex' => '11' ) ) . "
273                                 </td>
274                         </tr>"
275                 );
276                 if( $wgBlockAllowsUTEdit ){
277                         $wgOut->addHTML("
278                                 <tr id='wpAllowUsertalkRow'>
279                                         <td>&nbsp;</td>
280                                         <td class='mw-input'>" .
281                                                 Xml::checkLabel( wfMsg( 'ipballowusertalk' ),
282                                                         'wpAllowUsertalk', 'wpAllowUsertalk', $this->BlockAllowUsertalk,
283                                                         array( 'tabindex' => '12' ) ) . "
284                                         </td>
285                                 </tr>"
286                         );
287                 }
288
289                 $wgOut->addHTML("
290                         <tr>
291                                 <td style='padding-top: 1em'>&nbsp;</td>
292                                 <td  class='mw-submit' style='padding-top: 1em'>" .
293                                         Xml::submitButton( wfMsg( $alreadyBlocked ? 'ipb-change-block' : 'ipbsubmit' ),
294                                                 array( 'name' => 'wpBlock', 'tabindex' => '13', 'accesskey' => 's' ) ) . "
295                                 </td>
296                         </tr>" .
297                         Xml::closeElement( 'table' ) .
298                         Xml::hidden( 'wpEditToken', $wgUser->editToken() ) .
299                         ( $alreadyBlocked ? Xml::hidden( 'wpChangeBlock', 1 ) : "" ) .
300                         Xml::closeElement( 'fieldset' ) .
301                         Xml::closeElement( 'form' ) .
302                         Xml::tags( 'script', array( 'type' => 'text/javascript' ), 'updateBlockOptions()' ) . "\n"
303                 );
304
305                 $wgOut->addHTML( $this->getConvenienceLinks() );
306
307                 if( is_object( $user ) ) {
308                         $this->showLogFragment( $wgOut, $user->getUserPage() );
309                 } elseif( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->BlockAddress ) ) {
310                         $this->showLogFragment( $wgOut, Title::makeTitle( NS_USER, $this->BlockAddress ) );
311                 } elseif( preg_match( '/^\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}/', $this->BlockAddress ) ) {
312                         $this->showLogFragment( $wgOut, Title::makeTitle( NS_USER, $this->BlockAddress ) );
313                 }
314         }
315
316         /**
317          * Backend block code.
318          * $userID and $expiry will be filled accordingly
319          * @return array(message key, arguments) on failure, empty array on success
320          */
321         function doBlock( &$userId = null, &$expiry = null ) {
322                 global $wgUser, $wgSysopUserBans, $wgSysopRangeBans, $wgBlockAllowsUTEdit;
323
324                 $userId = 0;
325                 # Expand valid IPv6 addresses, usernames are left as is
326                 $this->BlockAddress = IP::sanitizeIP( $this->BlockAddress );
327                 # isIPv4() and IPv6() are used for final validation
328                 $rxIP4 = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
329                 $rxIP6 = '\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}';
330                 $rxIP = "($rxIP4|$rxIP6)";
331
332                 # Check for invalid specifications
333                 if ( !preg_match( "/^$rxIP$/", $this->BlockAddress ) ) {
334                         $matches = array();
335                         if ( preg_match( "/^($rxIP4)\\/(\\d{1,2})$/", $this->BlockAddress, $matches ) ) {
336                                 # IPv4
337                                 if ( $wgSysopRangeBans ) {
338                                         if ( !IP::isIPv4( $this->BlockAddress ) || $matches[2] < 16 || $matches[2] > 32 ) {
339                                                 return array('ip_range_invalid');
340                                         }
341                                         $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
342                                 } else {
343                                         # Range block illegal
344                                         return array('range_block_disabled');
345                                 }
346                         } else if ( preg_match( "/^($rxIP6)\\/(\\d{1,3})$/", $this->BlockAddress, $matches ) ) {
347                                 # IPv6
348                                 if ( $wgSysopRangeBans ) {
349                                         if ( !IP::isIPv6( $this->BlockAddress ) || $matches[2] < 64 || $matches[2] > 128 ) {
350                                                 return array('ip_range_invalid');
351                                         }
352                                         $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
353                                 } else {
354                                         # Range block illegal
355                                         return array('range_block_disabled');
356                                 }
357                         } else {
358                                 # Username block
359                                 if ( $wgSysopUserBans ) {
360                                         $user = User::newFromName( $this->BlockAddress );
361                                         if( !is_null( $user ) && $user->getId() ) {
362                                                 # Use canonical name
363                                                 $userId = $user->getId();
364                                                 $this->BlockAddress = $user->getName();
365                                         } else {
366                                                 return array('nosuchusershort', htmlspecialchars( $user ? $user->getName() : $this->BlockAddress ) );
367                                         }
368                                 } else {
369                                         return array('badipaddress');
370                                 }
371                         }
372                 }
373
374                 if ( $wgUser->isBlocked() && ( $wgUser->getId() !== $userId ) ) {
375                         return array( 'cant-block-while-blocked' );
376                 }
377
378                 $reasonstr = $this->BlockReasonList;
379                 if ( $reasonstr != 'other' && $this->BlockReason != '' ) {
380                         // Entry from drop down menu + additional comment
381                         $reasonstr .= wfMsgForContent( 'colon-separator' ) . $this->BlockReason;
382                 } elseif ( $reasonstr == 'other' ) {
383                         $reasonstr = $this->BlockReason;
384                 }
385
386                 $expirestr = $this->BlockExpiry;
387                 if( $expirestr == 'other' )
388                         $expirestr = $this->BlockOther;
389
390                 if ( ( strlen( $expirestr ) == 0) || ( strlen( $expirestr ) > 50) ) {
391                         return array('ipb_expiry_invalid');
392                 }
393                 
394                 if ( false === ($expiry = Block::parseExpiryInput( $expirestr )) ) {
395                         // Bad expiry.
396                         return array('ipb_expiry_invalid');
397                 }
398                 
399                 if( $this->BlockHideName ) {
400                         if( !$userId ) {
401                                 // IP users should not be hidden
402                                 $this->BlockHideName = false;
403                         } else if( $expiry !== 'infinity' ) {
404                                 // Bad expiry.
405                                 return array('ipb_expiry_temp');
406                         } else if( User::edits($userId) > self::HIDEUSER_CONTRIBLIMIT ) {
407                                 // Typically, the user should have a handful of edits.
408                                 // Disallow hiding users with many edits for performance.
409                                 return array('ipb_hide_invalid');
410                         }
411                 }
412
413                 # Create block
414                 # Note: for a user block, ipb_address is only for display purposes
415                 $block = new Block( $this->BlockAddress, $userId, $wgUser->getId(),
416                         $reasonstr, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly,
417                         $this->BlockCreateAccount, $this->BlockEnableAutoblock, $this->BlockHideName,
418                         $this->BlockEmail, isset( $this->BlockAllowUsertalk ) ? $this->BlockAllowUsertalk : $wgBlockAllowsUTEdit
419                 );
420
421                 # Should this be privately logged?
422                 $suppressLog = (bool)$this->BlockHideName;
423                 if ( wfRunHooks('BlockIp', array(&$block, &$wgUser)) ) {
424                         # Try to insert block. Is there a conflicting block?
425                         if ( !$block->insert() ) {
426                                 # Show form unless the user is already aware of this...
427                                 if ( !$this->BlockReblock ) {
428                                         return array( 'ipb_already_blocked' );
429                                 # Otherwise, try to update the block...
430                                 } else {
431                                         # This returns direct blocks before autoblocks/rangeblocks, since we should
432                                         # be sure the user is blocked by now it should work for our purposes
433                                         $currentBlock = Block::newFromDB( $this->BlockAddress, $userId );
434                                         if( $block->equals( $currentBlock ) ) {
435                                                 return array( 'ipb_already_blocked' );
436                                         }
437                                         # If the name was hidden and the blocking user cannot hide
438                                         # names, then don't allow any block changes...
439                                         if( $currentBlock->mHideName && !$wgUser->isAllowed('hideuser') ) {
440                                                 return array( 'hookaborted' );
441                                         }
442                                         $currentBlock->delete();
443                                         $block->insert();
444                                         # If hiding/unhiding a name, this should go in the private logs
445                                         $suppressLog = $suppressLog || (bool)$currentBlock->mHideName;
446                                         $log_action = 'reblock';
447                                         # Unset _deleted fields if requested
448                                         if( $currentBlock->mHideName && !$this->BlockHideName ) {
449                                                 self::unsuppressUserName( $this->BlockAddress, $userId );
450                                         }
451                                 }
452                         } else {
453                                 $log_action = 'block';
454                         }
455                         wfRunHooks('BlockIpComplete', array($block, $wgUser));
456
457                         # Set *_deleted fields if requested
458                         if( $this->BlockHideName ) {
459                                 self::suppressUserName( $this->BlockAddress, $userId );
460                         }
461
462                         if ( $this->BlockWatchUser &&
463                                 # Only show watch link when this is no range block
464                                 $block->mRangeStart == $block->mRangeEnd) {
465                                 $wgUser->addWatch ( Title::makeTitle( NS_USER, $this->BlockAddress ) );
466                         }
467                         
468                         # Block constructor sanitizes certain block options on insert
469                         $this->BlockEmail = $block->mBlockEmail;
470                         $this->BlockEnableAutoblock = $block->mEnableAutoblock;
471
472                         # Prepare log parameters
473                         $logParams = array();
474                         $logParams[] = $expirestr;
475                         $logParams[] = $this->blockLogFlags();
476
477                         # Make log entry, if the name is hidden, put it in the oversight log
478                         $log_type = $suppressLog ? 'suppress' : 'block';
479                         $log = new LogPage( $log_type );
480                         $log->addEntry( $log_action, Title::makeTitle( NS_USER, $this->BlockAddress ),
481                           $reasonstr, $logParams );
482
483                         # Report to the user
484                         return array();
485                 } else {
486                         return array('hookaborted');
487                 }
488         }
489         
490         public static function suppressUserName( $name, $userId ) {
491                 $op = '|'; // bitwise OR
492                 return self::setUsernameBitfields( $name, $userId, $op );
493         }
494         
495         public static function unsuppressUserName( $name, $userId ) {
496                 $op = '&'; // bitwise AND
497                 return self::setUsernameBitfields( $name, $userId, $op );
498         }
499         
500         private static function setUsernameBitfields( $name, $userId, $op ) {
501                 if( $op !== '|' && $op !== '&' )
502                         return false; // sanity check
503                 $dbw = wfGetDB( DB_MASTER );
504                 $delUser = Revision::DELETED_USER | Revision::DELETED_RESTRICTED;
505                 $delAction = LogPage::DELETED_ACTION | Revision::DELETED_RESTRICTED;
506                 # Normalize user name
507                 $userTitle = Title::makeTitleSafe( NS_USER, $name );
508                 $userDbKey = $userTitle->getDBKey();
509                 # To suppress, we OR the current bitfields with Revision::DELETED_USER
510                 # to put a 1 in the username *_deleted bit. To unsuppress we AND the
511                 # current bitfields with the inverse of Revision::DELETED_USER. The
512                 # username bit is made to 0 (x & 0 = 0), while others are unchanged (x & 1 = x).
513                 # The same goes for the sysop-restricted *_deleted bit.
514                 if( $op == '&' ) {
515                         $delUser = "~{$delUser}";
516                         $delAction = "~{$delAction}";
517                 }
518                 # Hide name from live edits
519                 $dbw->update( 'revision', array("rev_deleted = rev_deleted $op $delUser"),
520                         array('rev_user' => $userId), __METHOD__ );
521                 # Hide name from deleted edits
522                 $dbw->update( 'archive', array("ar_deleted = ar_deleted $op $delUser"),
523                         array('ar_user_text' => $name), __METHOD__ );
524                 # Hide name from logs
525                 $dbw->update( 'logging', array("log_deleted = log_deleted $op $delUser"),
526                         array('log_user' => $userId, "log_type != 'suppress'"), __METHOD__ );
527                 $dbw->update( 'logging', array("log_deleted = log_deleted $op $delAction"),
528                         array('log_namespace' => NS_USER, 'log_title' => $userDbKey,
529                                 "log_type != 'suppress'"), __METHOD__ );
530                 # Hide name from RC
531                 $dbw->update( 'recentchanges', array("rc_deleted = rc_deleted $op $delUser"),
532                         array('rc_user_text' => $name), __METHOD__ );
533                 # Hide name from live images
534                 $dbw->update( 'oldimage', array("oi_deleted = oi_deleted $op $delUser"),
535                         array('oi_user_text' => $name), __METHOD__ );
536                 # Hide name from deleted images
537                 # WMF - schema change pending
538                 # $dbw->update( 'filearchive', array("fa_deleted = fa_deleted $op $delUser"),
539                 #       array('fa_user_text' => $name), __METHOD__ );
540                 # Done!
541                 return true;
542         }
543
544         /**
545          * UI entry point for blocking
546          * Wraps around doBlock()
547          */
548         function doSubmit()
549         {
550                 global $wgOut;
551                 $retval = $this->doBlock();
552                 if(empty($retval)) {
553                         $titleObj = SpecialPage::getTitleFor( 'Blockip' );
554                         $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
555                                 urlencode( $this->BlockAddress ) ) );
556                         return;
557                 }
558                 $this->showForm( $retval );
559         }
560
561         function showSuccess() {
562                 global $wgOut;
563
564                 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
565                 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
566                 $text = wfMsgExt( 'blockipsuccesstext', array( 'parse' ), $this->BlockAddress );
567                 $wgOut->addHTML( $text );
568         }
569
570         function showLogFragment( $out, $title ) {
571                 global $wgUser;
572                 $out->addHTML( Xml::element( 'h2', NULL, LogPage::logName( 'block' ) ) );
573                 $count = LogEventsList::showLogExtract( $out, 'block', $title->getPrefixedText(), '', 10 );
574                 if($count > 10){
575                         $out->addHTML( $wgUser->getSkin()->link(
576                                 SpecialPage::getTitleFor( 'Log' ),
577                                 wfMsgHtml( 'blocklog-fulllog' ),
578                                 array(),
579                                 array(
580                                         'type' => 'block',
581                                         'page' => $title->getPrefixedText() ) ) );
582                 }
583         }
584
585         /**
586          * Return a comma-delimited list of "flags" to be passed to the log
587          * reader for this block, to provide more information in the logs
588          *
589          * @return array
590          */
591         private function blockLogFlags() {
592                 global $wgBlockAllowsUTEdit;
593                 $flags = array();
594                 if( $this->BlockAnonOnly && IP::isIPAddress( $this->BlockAddress ) )
595                         // when blocking a user the option 'anononly' is not available/has no effect -> do not write this into log
596                         $flags[] = 'anononly';
597                 if( $this->BlockCreateAccount )
598                         $flags[] = 'nocreate';
599                 if( !$this->BlockEnableAutoblock )
600                         $flags[] = 'noautoblock';
601                 if ( $this->BlockEmail )
602                         $flags[] = 'noemail';
603                 if ( !$this->BlockAllowUsertalk && $wgBlockAllowsUTEdit )
604                         $flags[] = 'nousertalk';
605                 if ( $this->BlockHideName )
606                         $flags[] = 'hiddenname';
607                 return implode( ',', $flags );
608         }
609
610         /**
611          * Builds unblock and block list links
612          *
613          * @return string
614          */
615         private function getConvenienceLinks() {
616                 global $wgUser, $wgLang;
617                 $skin = $wgUser->getSkin();
618                 if( $this->BlockAddress )
619                         $links[] = $this->getContribsLink( $skin );
620                 $links[] = $this->getUnblockLink( $skin );
621                 $links[] = $this->getBlockListLink( $skin );
622                 $links[] = $skin->makeLink ( 'MediaWiki:Ipbreason-dropdown', wfMsgHtml( 'ipb-edit-dropdown' ) );
623                 return '<p class="mw-ipb-conveniencelinks">' . $wgLang->pipeList( $links ) . '</p>';
624         }
625         
626         /**
627          * Build a convenient link to a user or IP's contribs
628          * form
629          *
630          * @param $skin Skin to use
631          * @return string
632          */
633         private function getContribsLink( $skin ) {
634                 $contribsPage = SpecialPage::getTitleFor( 'Contributions', $this->BlockAddress );
635                 return $skin->link( $contribsPage, wfMsgExt( 'ipb-blocklist-contribs', 'escape', $this->BlockAddress ) );
636         }
637
638         /**
639          * Build a convenient link to unblock the given username or IP
640          * address, if available; otherwise link to a blank unblock
641          * form
642          *
643          * @param $skin Skin to use
644          * @return string
645          */
646         private function getUnblockLink( $skin ) {
647                 $list = SpecialPage::getTitleFor( 'Ipblocklist' );
648                 if( $this->BlockAddress ) {
649                         $addr = htmlspecialchars( strtr( $this->BlockAddress, '_', ' ' ) );
650                         return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock-addr', $addr ),
651                                 'action=unblock&ip=' . urlencode( $this->BlockAddress ) );
652                 } else {
653                         return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock' ),      'action=unblock' );
654                 }
655         }
656
657         /**
658          * Build a convenience link to the block list
659          *
660          * @param $skin Skin to use
661          * @return string
662          */
663         private function getBlockListLink( $skin ) {
664                 $list = SpecialPage::getTitleFor( 'Ipblocklist' );
665                 if( $this->BlockAddress ) {
666                         $addr = htmlspecialchars( strtr( $this->BlockAddress, '_', ' ' ) );
667                         return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist-addr', $addr ),
668                                 'ip=' . urlencode( $this->BlockAddress ) );
669                 } else {
670                         return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist' ) );
671                 }
672         }
673         
674         /**
675         * Block a list of selected users
676         * @param array $users
677         * @param string $reason
678         * @param string $tag replaces user pages
679         * @param string $talkTag replaces user talk pages
680         * @returns array, list of html-safe usernames
681         */
682         public static function doMassUserBlock( $users, $reason = '', $tag = '', $talkTag = '' ) {
683                 global $wgUser;
684                 $counter = $blockSize = 0;
685                 $safeUsers = array();
686                 $log = new LogPage( 'block' );
687                 foreach( $users as $name ) {
688                         # Enforce limits
689                         $counter++;
690                         $blockSize++;
691                         # Lets not go *too* fast
692                         if( $blockSize >= 20 ) {
693                                 $blockSize = 0;
694                                 wfWaitForSlaves( 5 );
695                         }
696                         $u = User::newFromName( $name, false );
697                         // If user doesn't exist, it ought to be an IP then
698                         if( is_null($u) || (!$u->getId() && !IP::isIPAddress( $u->getName() )) ) {
699                                 continue;
700                         }
701                         $userTitle = $u->getUserPage();
702                         $userTalkTitle = $u->getTalkPage();
703                         $userpage = new Article( $userTitle );
704                         $usertalk = new Article( $userTalkTitle );
705                         $safeUsers[] = '[[' . $userTitle->getPrefixedText() . '|' . $userTitle->getText() . ']]';
706                         $expirestr = $u->getId() ? 'indefinite' : '1 week';
707                         $expiry = Block::parseExpiryInput( $expirestr );
708                         $anonOnly = IP::isIPAddress( $u->getName() ) ? 1 : 0;
709                         // Create the block
710                         $block = new Block( $u->getName(), // victim
711                                 $u->getId(), // uid
712                                 $wgUser->getId(), // blocker
713                                 $reason, // comment
714                                 wfTimestampNow(), // block time
715                                 0, // auto ?
716                                 $expiry, // duration
717                                 $anonOnly, // anononly?
718                                 1, // block account creation?
719                                 1, // autoblocking?
720                                 0, // suppress name?
721                                 0 // block from sending email?
722                         );
723                         $oldblock = Block::newFromDB( $u->getName(), $u->getId() );
724                         if( !$oldblock ) {
725                                 $block->insert();
726                                 # Prepare log parameters
727                                 $logParams = array();
728                                 $logParams[] = $expirestr;
729                                 if( $anonOnly ) {
730                                         $logParams[] = 'anononly';
731                                 }
732                                 $logParams[] = 'nocreate';
733                                 # Add log entry
734                                 $log->addEntry( 'block', $userTitle, $reason, $logParams );
735                         }
736                         # Tag userpage! (check length to avoid mistakes)
737                         if( strlen($tag) > 2 ) {
738                                 $userpage->doEdit( $tag, $reason, EDIT_MINOR );
739                         }
740                         if( strlen($talkTag) > 2 ) {
741                                 $usertalk->doEdit( $talkTag, $reason, EDIT_MINOR );
742                         }
743                 }
744                 return $safeUsers;
745         }
746 }