*Add year/month selector to user contribs (bug 516)
[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 // Allow some users to hide name from block log, blocklist and listusers
231 if ( $wgUser->isAllowed( 'hideuser' ) ) {
232 $wgOut->addHTML("
233 <tr>
234 <td>&nbsp;</td>
235 <td>
236 " . wfCheckLabel( wfMsgHtml( 'ipbhidename' ),
237 'wpHideName', 'wpHideName', $this->BlockHideName,
238 array( 'tabindex' => '9' ) ) . "
239 </td>
240 </tr>
241 ");
242 }
243
244 global $wgSysopEmailBans;
245
246 if ( $wgSysopEmailBans && $wgUser->isAllowed( 'blockemail' ) ) {
247 $wgOut->addHTML("
248 <tr id='wpEnableEmailBan'>
249 <td>&nbsp;</td>
250 <td>
251 " . wfCheckLabel( wfMsgHtml( 'ipbemailban' ),
252 'wpEmailBan', 'wpEmailBan', $this->BlockEmail,
253 array( 'tabindex' => '10' )) . "
254 </td>
255 </tr>
256 ");
257 }
258 $wgOut->addHTML("
259 <tr>
260 <td style='padding-top: 1em'>&nbsp;</td>
261 <td style='padding-top: 1em'>
262 " . Xml::submitButton( wfMsg( 'ipbsubmit' ),
263 array( 'name' => 'wpBlock', 'tabindex' => '11' ) ) . "
264 </td>
265 </tr>
266 </table>" .
267 Xml::hidden( 'wpEditToken', $token ) .
268 "</form>
269 <script type=\"text/javascript\">updateBlockOptions()</script>
270 \n" );
271
272 $wgOut->addHtml( $this->getConvenienceLinks() );
273
274 $user = User::newFromName( $this->BlockAddress );
275 if( is_object( $user ) ) {
276 $this->showLogFragment( $wgOut, $user->getUserPage() );
277 } elseif( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->BlockAddress ) ) {
278 $this->showLogFragment( $wgOut, Title::makeTitle( NS_USER, $this->BlockAddress ) );
279 } 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 ) ) {
280 $this->showLogFragment( $wgOut, Title::makeTitle( NS_USER, $this->BlockAddress ) );
281 }
282 }
283
284 function doSubmit() {
285 global $wgOut, $wgUser, $wgSysopUserBans, $wgSysopRangeBans;
286
287 $userId = 0;
288 # Expand valid IPv6 addresses, usernames are left as is
289 $this->BlockAddress = IP::sanitizeIP( $this->BlockAddress );
290 # isIPv4() and IPv6() are used for final validation
291 $rxIP4 = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
292 $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}';
293 $rxIP = "($rxIP4|$rxIP6)";
294
295 # Check for invalid specifications
296 if ( !preg_match( "/^$rxIP$/", $this->BlockAddress ) ) {
297 $matches = array();
298 if ( preg_match( "/^($rxIP4)\\/(\\d{1,2})$/", $this->BlockAddress, $matches ) ) {
299 # IPv4
300 if ( $wgSysopRangeBans ) {
301 if ( !IP::isIPv4( $this->BlockAddress ) || $matches[2] < 16 || $matches[2] > 32 ) {
302 $this->showForm( wfMsg( 'ip_range_invalid' ) );
303 return;
304 }
305 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
306 } else {
307 # Range block illegal
308 $this->showForm( wfMsg( 'range_block_disabled' ) );
309 return;
310 }
311 } else if ( preg_match( "/^($rxIP6)\\/(\\d{1,3})$/", $this->BlockAddress, $matches ) ) {
312 # IPv6
313 if ( $wgSysopRangeBans ) {
314 if ( !IP::isIPv6( $this->BlockAddress ) || $matches[2] < 64 || $matches[2] > 128 ) {
315 $this->showForm( wfMsg( 'ip_range_invalid' ) );
316 return;
317 }
318 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
319 } else {
320 # Range block illegal
321 $this->showForm( wfMsg( 'range_block_disabled' ) );
322 return;
323 }
324 } else {
325 # Username block
326 if ( $wgSysopUserBans ) {
327 $user = User::newFromName( $this->BlockAddress );
328 if( !is_null( $user ) && $user->getID() ) {
329 # Use canonical name
330 $this->BlockAddress = $user->getName();
331 $userId = $user->getID();
332 } else {
333 $this->showForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->BlockAddress ) ) );
334 return;
335 }
336 } else {
337 $this->showForm( wfMsg( 'badipaddress' ) );
338 return;
339 }
340 }
341 }
342
343 $reasonstr = $this->BlockReasonList;
344 if ( $reasonstr != 'other' && $this->BlockReason != '') {
345 // Entry from drop down menu + additional comment
346 $reasonstr .= ': ' . $this->BlockReason;
347 } elseif ( $reasonstr == 'other' ) {
348 $reasonstr = $this->BlockReason;
349 }
350
351 $expirestr = $this->BlockExpiry;
352 if( $expirestr == 'other' )
353 $expirestr = $this->BlockOther;
354
355 if (strlen($expirestr) == 0) {
356 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
357 return;
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 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
368 return;
369 }
370
371 $expiry = wfTimestamp( TS_MW, $expiry );
372 }
373
374 # Create block
375 # Note: for a user block, ipb_address is only for display purposes
376 $block = new Block( $this->BlockAddress, $userId, $wgUser->getID(),
377 $reasonstr, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly,
378 $this->BlockCreateAccount, $this->BlockEnableAutoblock, $this->BlockHideName,
379 $this->BlockEmail);
380
381 if (wfRunHooks('BlockIp', array(&$block, &$wgUser))) {
382
383 if ( !$block->insert() ) {
384 $this->showForm( wfMsg( 'ipb_already_blocked',
385 htmlspecialchars( $this->BlockAddress ) ) );
386 return;
387 }
388
389 wfRunHooks('BlockIpComplete', array($block, $wgUser));
390
391 # Prepare log parameters
392 $logParams = array();
393 $logParams[] = $expirestr;
394 $logParams[] = $this->blockLogFlags();
395
396 # Make log entry, if the name is hidden, put it in the oversight log
397 $log_type = ($this->BlockHideName) ? 'oversight' : 'block';
398 $log = new LogPage( $log_type );
399 $log->addEntry( 'block', Title::makeTitle( NS_USER, $this->BlockAddress ),
400 $reasonstr, $logParams );
401
402 # Report to the user
403 $titleObj = SpecialPage::getTitleFor( 'Blockip' );
404 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
405 urlencode( $this->BlockAddress ) ) );
406 }
407 }
408
409 function showSuccess() {
410 global $wgOut;
411
412 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
413 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
414 $text = wfMsg( 'blockipsuccesstext', $this->BlockAddress );
415 $wgOut->addWikiText( $text );
416 }
417
418 function showLogFragment( $out, $title ) {
419 $out->addHtml( wfElement( 'h2', NULL, LogPage::logName( 'block' ) ) );
420 $request = new FauxRequest( array( 'page' => $title->getPrefixedText(), 'type' => 'block' ) );
421 $viewer = new LogViewer( new LogReader( $request ) );
422 $viewer->showList( $out );
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 }
495 ?>