Add the other existing $skin.css/.js to the message files too to be consistent
[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 if( $this->BlockHideName && $expiry != 'infinity' ) {
352 // Bad expiry.
353 return array('ipb_expiry_temp');
354 }
355
356 # Create block
357 # Note: for a user block, ipb_address is only for display purposes
358 $block = new Block( $this->BlockAddress, $userId, $wgUser->getId(),
359 $reasonstr, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly,
360 $this->BlockCreateAccount, $this->BlockEnableAutoblock, $this->BlockHideName,
361 $this->BlockEmail );
362
363 if ( wfRunHooks('BlockIp', array(&$block, &$wgUser)) ) {
364
365 if ( !$block->insert() ) {
366 return array('ipb_already_blocked', htmlspecialchars($this->BlockAddress));
367 }
368
369 wfRunHooks('BlockIpComplete', array($block, $wgUser));
370
371 if ( $this->BlockWatchUser ) {
372 $wgUser->addWatch ( Title::makeTitle( NS_USER, $this->BlockAddress ) );
373 }
374
375 # Prepare log parameters
376 $logParams = array();
377 $logParams[] = $expirestr;
378 $logParams[] = $this->blockLogFlags();
379
380 # Make log entry, if the name is hidden, put it in the oversight log
381 $log_type = ($this->BlockHideName) ? 'suppress' : 'block';
382 $log = new LogPage( $log_type );
383 $log->addEntry( 'block', Title::makeTitle( NS_USER, $this->BlockAddress ),
384 $reasonstr, $logParams );
385
386 # Report to the user
387 return array();
388 }
389 else
390 return array('hookaborted');
391 }
392
393 /**
394 * UI entry point for blocking
395 * Wraps around doBlock()
396 */
397 function doSubmit()
398 {
399 global $wgOut;
400 $retval = $this->doBlock();
401 if(empty($retval)) {
402 $titleObj = SpecialPage::getTitleFor( 'Blockip' );
403 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
404 urlencode( $this->BlockAddress ) ) );
405 return;
406 }
407 $key = array_shift($retval);
408 $this->showForm(wfMsgReal($key, $retval));
409 }
410
411 function showSuccess() {
412 global $wgOut;
413
414 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
415 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
416 $text = wfMsgExt( 'blockipsuccesstext', array( 'parse' ), $this->BlockAddress );
417 $wgOut->addHtml( $text );
418 }
419
420 function showLogFragment( $out, $title ) {
421 $out->addHtml( Xml::element( 'h2', NULL, LogPage::logName( 'block' ) ) );
422 LogEventsList::showLogExtract( $out, 'block', $title->getPrefixedText() );
423 }
424
425 /**
426 * Return a comma-delimited list of "flags" to be passed to the log
427 * reader for this block, to provide more information in the logs
428 *
429 * @return array
430 */
431 private function blockLogFlags() {
432 $flags = array();
433 if( $this->BlockAnonOnly && IP::isIPAddress( $this->BlockAddress ) )
434 // when blocking a user the option 'anononly' is not available/has no effect -> do not write this into log
435 $flags[] = 'anononly';
436 if( $this->BlockCreateAccount )
437 $flags[] = 'nocreate';
438 if( !$this->BlockEnableAutoblock )
439 $flags[] = 'noautoblock';
440 if ( $this->BlockEmail )
441 $flags[] = 'noemail';
442 return implode( ',', $flags );
443 }
444
445 /**
446 * Builds unblock and block list links
447 *
448 * @return string
449 */
450 private function getConvenienceLinks() {
451 global $wgUser;
452 $skin = $wgUser->getSkin();
453 $links[] = $skin->makeLink ( 'MediaWiki:Ipbreason-dropdown', wfMsgHtml( 'ipb-edit-dropdown' ) );
454 $links[] = $this->getUnblockLink( $skin );
455 $links[] = $this->getBlockListLink( $skin );
456 return '<p class="mw-ipb-conveniencelinks">' . implode( ' | ', $links ) . '</p>';
457 }
458
459 /**
460 * Build a convenient link to unblock the given username or IP
461 * address, if available; otherwise link to a blank unblock
462 * form
463 *
464 * @param $skin Skin to use
465 * @return string
466 */
467 private function getUnblockLink( $skin ) {
468 $list = SpecialPage::getTitleFor( 'Ipblocklist' );
469 if( $this->BlockAddress ) {
470 $addr = htmlspecialchars( strtr( $this->BlockAddress, '_', ' ' ) );
471 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock-addr', $addr ),
472 'action=unblock&ip=' . urlencode( $this->BlockAddress ) );
473 } else {
474 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock' ), 'action=unblock' );
475 }
476 }
477
478 /**
479 * Build a convenience link to the block list
480 *
481 * @param $skin Skin to use
482 * @return string
483 */
484 private function getBlockListLink( $skin ) {
485 $list = SpecialPage::getTitleFor( 'Ipblocklist' );
486 if( $this->BlockAddress ) {
487 $addr = htmlspecialchars( strtr( $this->BlockAddress, '_', ' ' ) );
488 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist-addr', $addr ),
489 'ip=' . urlencode( $this->BlockAddress ) );
490 } else {
491 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist' ) );
492 }
493 }
494 }