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