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