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