Core changes for GlobalBlocking and TorBlock extensions, plus some core refactoring...
[lhc/web/wiklou.git] / includes / 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
49 function IPBlockForm( $par ) {
50 global $wgRequest, $wgUser;
51
52 $this->BlockAddress = $wgRequest->getVal( 'wpBlockAddress', $wgRequest->getVal( 'ip', $par ) );
53 $this->BlockAddress = strtr( $this->BlockAddress, '_', ' ' );
54 $this->BlockReason = $wgRequest->getText( 'wpBlockReason' );
55 $this->BlockReasonList = $wgRequest->getText( 'wpBlockReasonList' );
56 $this->BlockExpiry = $wgRequest->getVal( 'wpBlockExpiry', wfMsg('ipbotheroption') );
57 $this->BlockOther = $wgRequest->getVal( 'wpBlockOther', '' );
58
59 # Unchecked checkboxes are not included in the form data at all, so having one
60 # that is true by default is a bit tricky
61 $byDefault = !$wgRequest->wasPosted();
62 $this->BlockAnonOnly = $wgRequest->getBool( 'wpAnonOnly', $byDefault );
63 $this->BlockCreateAccount = $wgRequest->getBool( 'wpCreateAccount', $byDefault );
64 $this->BlockEnableAutoblock = $wgRequest->getBool( 'wpEnableAutoblock', $byDefault );
65 $this->BlockEmail = $wgRequest->getBool( 'wpEmailBan', false );
66 $this->BlockWatchUser = $wgRequest->getBool( 'wpWatchUser', false );
67 # Re-check user's rights to hide names, very serious, defaults to 0
68 $this->BlockHideName = ( $wgRequest->getBool( 'wpHideName', 0 ) && $wgUser->isAllowed( 'hideuser' ) ) ? 1 : 0;
69 }
70
71 function showForm( $err ) {
72 global $wgOut, $wgUser, $wgSysopUserBans;
73
74 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
75 $wgOut->addWikiMsg( 'blockiptext' );
76
77 if($wgSysopUserBans) {
78 $mIpaddress = Xml::label( wfMsg( 'ipadressorusername' ), 'mw-bi-target' );
79 } else {
80 $mIpaddress = Xml::label( wfMsg( 'ipaddress' ), 'mw-bi-target' );
81 }
82 $mIpbexpiry = Xml::label( wfMsg( 'ipbexpiry' ), 'wpBlockExpiry' );
83 $mIpbother = Xml::label( wfMsg( 'ipbother' ), 'mw-bi-other' );
84 $mIpbreasonother = Xml::label( wfMsg( 'ipbreason' ), 'wpBlockReasonList' );
85 $mIpbreason = Xml::label( wfMsg( 'ipbotherreason' ), 'mw-bi-reason' );
86
87 $titleObj = SpecialPage::getTitleFor( 'Blockip' );
88
89 if ( "" != $err ) {
90 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
91 $wgOut->addHTML( Xml::tags( 'p', array( 'class' => 'error' ), $err ) );
92 }
93
94 $scBlockExpiryOptions = wfMsgForContent( 'ipboptions' );
95
96 $showblockoptions = $scBlockExpiryOptions != '-';
97 if (!$showblockoptions)
98 $mIpbother = $mIpbexpiry;
99
100 $blockExpiryFormOptions = Xml::option( wfMsg( 'ipbotheroption' ), 'other' );
101 foreach (explode(',', $scBlockExpiryOptions) as $option) {
102 if ( strpos($option, ":") === false ) $option = "$option:$option";
103 list($show, $value) = explode(":", $option);
104 $show = htmlspecialchars($show);
105 $value = htmlspecialchars($value);
106 $blockExpiryFormOptions .= Xml::option( $show, $value, $this->BlockExpiry === $value ? true : false ) . "\n";
107 }
108
109 $reasonDropDown = Xml::listDropDown( 'wpBlockReasonList',
110 wfMsgForContent( 'ipbreason-dropdown' ),
111 wfMsgForContent( 'ipbreasonotherlist' ), '', 'wpBlockDropDown', 4 );
112
113 global $wgStylePath, $wgStyleVersion;
114 $wgOut->addHTML(
115 Xml::tags( 'script', array( 'type' => 'text/javascript', 'src' => "$wgStylePath/common/block.js?$wgStyleVersion" ), '' ) .
116 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalURL( "action=submit" ), 'id' => 'blockip' ) ) .
117 Xml::openElement( 'fieldset' ) .
118 Xml::element( 'legend', null, wfMsg( 'blockip-legend' ) ) .
119 Xml::openElement( 'table', array ( 'border' => '0', 'id' => 'mw-blockip-table' ) ) .
120 "<tr>
121 <td class='mw-label'>
122 {$mIpaddress}
123 </td>
124 <td class='mw-input'>" .
125 Xml::input( 'wpBlockAddress', 45, $this->BlockAddress,
126 array(
127 'tabindex' => '1',
128 'id' => 'mw-bi-target',
129 'onchange' => 'updateBlockOptions()' ) ). "
130 </td>
131 </tr>
132 <tr>"
133 );
134 if ( $showblockoptions ) {
135 $wgOut->addHTML("
136 <td class='mw-label'>
137 {$mIpbexpiry}
138 </td>
139 <td class='mw-input'>" .
140 Xml::tags( 'select',
141 array(
142 'id' => 'wpBlockExpiry',
143 'name' => 'wpBlockExpiry',
144 'onchange' => 'considerChangingExpiryFocus()',
145 'tabindex' => '2' ),
146 $blockExpiryFormOptions ) .
147 "</td>"
148 );
149 }
150 $wgOut->addHTML("
151 </tr>
152 <tr id='wpBlockOther'>
153 <td class='mw-label'>
154 {$mIpbother}
155 </td>
156 <td class='mw-input'>" .
157 Xml::input( 'wpBlockOther', 45, $this->BlockOther,
158 array( 'tabindex' => '3', 'id' => 'mw-bi-other' ) ) . "
159 </td>
160 </tr>
161 <tr>
162 <td class='mw-label'>
163 {$mIpbreasonother}
164 </td>
165 <td class='mw-input'>
166 {$reasonDropDown}
167 </td>
168 </tr>
169 <tr id=\"wpBlockReason\">
170 <td class='mw-label'>
171 {$mIpbreason}
172 </td>
173 <td class='mw-input'>" .
174 Xml::input( 'wpBlockReason', 45, $this->BlockReason,
175 array( 'tabindex' => '5', 'id' => 'mw-bi-reason', 'maxlength'=> '200' ) ) . "
176 </td>
177 </tr>
178 <tr id='wpAnonOnlyRow'>
179 <td>&nbsp;</td>
180 <td class='mw-input'>" .
181 Xml::checkLabel( wfMsg( 'ipbanononly' ),
182 'wpAnonOnly', 'wpAnonOnly', $this->BlockAnonOnly,
183 array( 'tabindex' => '6' ) ) . "
184 </td>
185 </tr>
186 <tr id='wpCreateAccountRow'>
187 <td>&nbsp;</td>
188 <td class='mw-input'>" .
189 Xml::checkLabel( wfMsg( 'ipbcreateaccount' ),
190 'wpCreateAccount', 'wpCreateAccount', $this->BlockCreateAccount,
191 array( 'tabindex' => '7' ) ) . "
192 </td>
193 </tr>
194 <tr id='wpEnableAutoblockRow'>
195 <td>&nbsp;</td>
196 <td class='mw-input'>" .
197 Xml::checkLabel( wfMsg( 'ipbenableautoblock' ),
198 'wpEnableAutoblock', 'wpEnableAutoblock', $this->BlockEnableAutoblock,
199 array( 'tabindex' => '8' ) ) . "
200 </td>
201 </tr>"
202 );
203
204 global $wgSysopEmailBans;
205 if ( $wgSysopEmailBans && $wgUser->isAllowed( 'blockemail' ) ) {
206 $wgOut->addHTML("
207 <tr id='wpEnableEmailBan'>
208 <td>&nbsp;</td>
209 <td class='mw-input'>" .
210 Xml::checkLabel( wfMsg( 'ipbemailban' ),
211 'wpEmailBan', 'wpEmailBan', $this->BlockEmail,
212 array( 'tabindex' => '9' )) . "
213 </td>
214 </tr>"
215 );
216 }
217
218 // Allow some users to hide name from block log, blocklist and listusers
219 if ( $wgUser->isAllowed( 'hideuser' ) ) {
220 $wgOut->addHTML("
221 <tr id='wpEnableHideUser'>
222 <td>&nbsp;</td>
223 <td class='mw-input'>" .
224 Xml::checkLabel( wfMsg( 'ipbhidename' ),
225 'wpHideName', 'wpHideName', $this->BlockHideName,
226 array( 'tabindex' => '10' ) ) . "
227 </td>
228 </tr>"
229 );
230 }
231
232 # Watchlist their user page?
233 $wgOut->addHTML("
234 <tr id='wpEnableWatchUser'>
235 <td>&nbsp;</td>
236 <td class='mw-input'>" .
237 Xml::checkLabel( wfMsg( 'ipbwatchuser' ),
238 'wpWatchUser', 'wpWatchUser', $this->BlockWatchUser,
239 array( 'tabindex' => '11' ) ) . "
240 </td>
241 </tr>"
242 );
243
244 $wgOut->addHTML("
245 <tr>
246 <td style='padding-top: 1em'>&nbsp;</td>
247 <td class='mw-submit' style='padding-top: 1em'>" .
248 Xml::submitButton( wfMsg( 'ipbsubmit' ),
249 array( 'name' => 'wpBlock', 'tabindex' => '12' ) ) . "
250 </td>
251 </tr>" .
252 Xml::closeElement( 'table' ) .
253 Xml::hidden( 'wpEditToken', $wgUser->editToken() ) .
254 Xml::closeElement( 'fieldset' ) .
255 Xml::closeElement( 'form' ) .
256 Xml::tags( 'script', array( 'type' => 'text/javascript' ), 'updateBlockOptions()' ) . "\n"
257 );
258
259 $wgOut->addHtml( $this->getConvenienceLinks() );
260
261 $user = User::newFromName( $this->BlockAddress );
262 if( is_object( $user ) ) {
263 $this->showLogFragment( $wgOut, $user->getUserPage() );
264 } elseif( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->BlockAddress ) ) {
265 $this->showLogFragment( $wgOut, Title::makeTitle( NS_USER, $this->BlockAddress ) );
266 } 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 ) ) {
267 $this->showLogFragment( $wgOut, Title::makeTitle( NS_USER, $this->BlockAddress ) );
268 }
269 }
270
271 /**
272 * Backend block code.
273 * $userID and $expiry will be filled accordingly
274 * @return array(message key, arguments) on failure, empty array on success
275 */
276 function doBlock(&$userId = null, &$expiry = null)
277 {
278 global $wgUser, $wgSysopUserBans, $wgSysopRangeBans;
279
280 $userId = 0;
281 # Expand valid IPv6 addresses, usernames are left as is
282 $this->BlockAddress = IP::sanitizeIP( $this->BlockAddress );
283 # isIPv4() and IPv6() are used for final validation
284 $rxIP4 = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
285 $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}';
286 $rxIP = "($rxIP4|$rxIP6)";
287
288 # Check for invalid specifications
289 if ( !preg_match( "/^$rxIP$/", $this->BlockAddress ) ) {
290 $matches = array();
291 if ( preg_match( "/^($rxIP4)\\/(\\d{1,2})$/", $this->BlockAddress, $matches ) ) {
292 # IPv4
293 if ( $wgSysopRangeBans ) {
294 if ( !IP::isIPv4( $this->BlockAddress ) || $matches[2] < 16 || $matches[2] > 32 ) {
295 return array('ip_range_invalid');
296 }
297 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
298 } else {
299 # Range block illegal
300 return array('range_block_disabled');
301 }
302 } else if ( preg_match( "/^($rxIP6)\\/(\\d{1,3})$/", $this->BlockAddress, $matches ) ) {
303 # IPv6
304 if ( $wgSysopRangeBans ) {
305 if ( !IP::isIPv6( $this->BlockAddress ) || $matches[2] < 64 || $matches[2] > 128 ) {
306 return array('ip_range_invalid');
307 }
308 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
309 } else {
310 # Range block illegal
311 return array('range_block_disabled');
312 }
313 } else {
314 # Username block
315 if ( $wgSysopUserBans ) {
316 $user = User::newFromName( $this->BlockAddress );
317 if( !is_null( $user ) && $user->getId() ) {
318 # Use canonical name
319 $userId = $user->getId();
320 $this->BlockAddress = $user->getName();
321 } else {
322 return array('nosuchusershort', htmlspecialchars( $user ? $user->getName() : $this->BlockAddress ) );
323 }
324 } else {
325 return array('badipaddress');
326 }
327 }
328 }
329
330 $reasonstr = $this->BlockReasonList;
331 if ( $reasonstr != 'other' && $this->BlockReason != '') {
332 // Entry from drop down menu + additional comment
333 $reasonstr .= ': ' . $this->BlockReason;
334 } elseif ( $reasonstr == 'other' ) {
335 $reasonstr = $this->BlockReason;
336 }
337
338 $expirestr = $this->BlockExpiry;
339 if( $expirestr == 'other' )
340 $expirestr = $this->BlockOther;
341
342 if (strlen($expirestr) == 0) {
343 return array('ipb_expiry_invalid');
344 }
345
346 if ( false === ($expiry = Block::parseExpiryInput( $expirestr )) ) {
347 // Bad expiry.
348 return array('ipb_expiry_invalid');
349 }
350
351 # Create block
352 # Note: for a user block, ipb_address is only for display purposes
353 $block = new Block( $this->BlockAddress, $userId, $wgUser->getId(),
354 $reasonstr, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly,
355 $this->BlockCreateAccount, $this->BlockEnableAutoblock, $this->BlockHideName,
356 $this->BlockEmail);
357
358 if ( $this->BlockWatchUser ) {
359 $wgUser->addWatch ( Title::makeTitle( NS_USER, $this->BlockAddress ) );
360 }
361
362 if (wfRunHooks('BlockIp', array(&$block, &$wgUser))) {
363
364 if ( !$block->insert() ) {
365 return array('ipb_already_blocked', htmlspecialchars($this->BlockAddress));
366 }
367
368 wfRunHooks('BlockIpComplete', array($block, $wgUser));
369
370 # Prepare log parameters
371 $logParams = array();
372 $logParams[] = $expirestr;
373 $logParams[] = $this->blockLogFlags();
374
375 # Make log entry, if the name is hidden, put it in the oversight log
376 $log_type = ($this->BlockHideName) ? 'suppress' : 'block';
377 $log = new LogPage( $log_type );
378 $log->addEntry( 'block', Title::makeTitle( NS_USER, $this->BlockAddress ),
379 $reasonstr, $logParams );
380
381 # Report to the user
382 return array();
383 }
384 else
385 return array('hookaborted');
386 }
387
388 /**
389 * UI entry point for blocking
390 * Wraps around doBlock()
391 */
392 function doSubmit()
393 {
394 global $wgOut;
395 $retval = $this->doBlock();
396 if(empty($retval)) {
397 $titleObj = SpecialPage::getTitleFor( 'Blockip' );
398 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
399 urlencode( $this->BlockAddress ) ) );
400 return;
401 }
402 $key = array_shift($retval);
403 $this->showForm(wfMsgReal($key, $retval));
404 }
405
406 function showSuccess() {
407 global $wgOut;
408
409 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
410 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
411 $text = wfMsgExt( 'blockipsuccesstext', array( 'parse' ), $this->BlockAddress );
412 $wgOut->addHtml( $text );
413 }
414
415 function showLogFragment( $out, $title ) {
416 $out->addHtml( Xml::element( 'h2', NULL, LogPage::logName( 'block' ) ) );
417 LogEventsList::showLogExtract( $out, 'block', $title->getPrefixedText() );
418 }
419
420 /**
421 * Return a comma-delimited list of "flags" to be passed to the log
422 * reader for this block, to provide more information in the logs
423 *
424 * @return array
425 */
426 private function blockLogFlags() {
427 $flags = array();
428 if( $this->BlockAnonOnly && IP::isIPAddress( $this->BlockAddress ) )
429 // when blocking a user the option 'anononly' is not available/has no effect -> do not write this into log
430 $flags[] = 'anononly';
431 if( $this->BlockCreateAccount )
432 $flags[] = 'nocreate';
433 if( !$this->BlockEnableAutoblock )
434 $flags[] = 'noautoblock';
435 if ( $this->BlockEmail )
436 $flags[] = 'noemail';
437 return implode( ',', $flags );
438 }
439
440 /**
441 * Builds unblock and block list links
442 *
443 * @return string
444 */
445 private function getConvenienceLinks() {
446 global $wgUser;
447 $skin = $wgUser->getSkin();
448 $links[] = $skin->makeLink ( 'MediaWiki:Ipbreason-dropdown', wfMsgHtml( 'ipb-edit-dropdown' ) );
449 $links[] = $this->getUnblockLink( $skin );
450 $links[] = $this->getBlockListLink( $skin );
451 return '<p class="mw-ipb-conveniencelinks">' . implode( ' | ', $links ) . '</p>';
452 }
453
454 /**
455 * Build a convenient link to unblock the given username or IP
456 * address, if available; otherwise link to a blank unblock
457 * form
458 *
459 * @param $skin Skin to use
460 * @return string
461 */
462 private function getUnblockLink( $skin ) {
463 $list = SpecialPage::getTitleFor( 'Ipblocklist' );
464 if( $this->BlockAddress ) {
465 $addr = htmlspecialchars( strtr( $this->BlockAddress, '_', ' ' ) );
466 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock-addr', $addr ),
467 'action=unblock&ip=' . urlencode( $this->BlockAddress ) );
468 } else {
469 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock' ), 'action=unblock' );
470 }
471 }
472
473 /**
474 * Build a convenience link to the block list
475 *
476 * @param $skin Skin to use
477 * @return string
478 */
479 private function getBlockListLink( $skin ) {
480 $list = SpecialPage::getTitleFor( 'Ipblocklist' );
481 if( $this->BlockAddress ) {
482 $addr = htmlspecialchars( strtr( $this->BlockAddress, '_', ' ' ) );
483 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist-addr', $addr ),
484 'ip=' . urlencode( $this->BlockAddress ) );
485 } else {
486 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist' ) );
487 }
488 }
489 }