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