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