This is giving me a syntax error. It looks gross this way, but I can't think of any...
[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 const BLOCK_SUCCESS = 0; // Success
285 const BLOCK_RANGE_INVALID = 1; // Invalid IP range
286 const BLOCK_RANGE_DISABLED = 2; // Sysops can't block ranges
287 const BLOCK_NONEXISTENT_USER = 3; // No such user
288 const BLOCK_IP_INVALID = 4; // Invalid IP address
289 const BLOCK_EXPIRY_INVALID = 5; // Invalid expiry time
290 const BLOCK_ALREADY_BLOCKED = 6; // User is already blocked
291 /**
292 * Backend block code.
293 * $userID and $expiry will be filled accordingly
294 * Returns one of the BLOCK_* constants
295 */
296 function doBlock(&$userId = null, &$expiry = null)
297 {
298 global $wgUser, $wgSysopUserBans, $wgSysopRangeBans;
299
300 $userId = 0;
301 # Expand valid IPv6 addresses, usernames are left as is
302 $this->BlockAddress = IP::sanitizeIP( $this->BlockAddress );
303 # isIPv4() and IPv6() are used for final validation
304 $rxIP4 = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
305 $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}';
306 $rxIP = "($rxIP4|$rxIP6)";
307
308 # Check for invalid specifications
309 if ( !preg_match( "/^$rxIP$/", $this->BlockAddress ) ) {
310 $matches = array();
311 if ( preg_match( "/^($rxIP4)\\/(\\d{1,2})$/", $this->BlockAddress, $matches ) ) {
312 # IPv4
313 if ( $wgSysopRangeBans ) {
314 if ( !IP::isIPv4( $this->BlockAddress ) || $matches[2] < 16 || $matches[2] > 32 ) {
315 return self::BLOCK_RANGE_INVALID;
316 }
317 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
318 } else {
319 # Range block illegal
320 return self::BLOCK_RANGE_DISABLED;
321 }
322 } else if ( preg_match( "/^($rxIP6)\\/(\\d{1,3})$/", $this->BlockAddress, $matches ) ) {
323 # IPv6
324 if ( $wgSysopRangeBans ) {
325 if ( !IP::isIPv6( $this->BlockAddress ) || $matches[2] < 64 || $matches[2] > 128 ) {
326 return self::BLOCK_RANGE_INVALID;
327 }
328 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
329 } else {
330 # Range block illegal
331 return self::BLOCK_RANGE_DISABLED;
332 }
333 } else {
334 # Username block
335 if ( $wgSysopUserBans ) {
336 $user = User::newFromName( $this->BlockAddress );
337 if( !is_null( $user ) && $user->getID() ) {
338 # Use canonical name
339 $this->BlockAddress = $user->getName();
340 $userId = $user->getID();
341 } else {
342 return self::BLOCK_NONEXISTENT_USER;
343 }
344 } else {
345 return self::BLOCK_IP_INVALID;
346 }
347 }
348 }
349
350 $reasonstr = $this->BlockReasonList;
351 if ( $reasonstr != 'other' && $this->BlockReason != '') {
352 // Entry from drop down menu + additional comment
353 $reasonstr .= ': ' . $this->BlockReason;
354 } elseif ( $reasonstr == 'other' ) {
355 $reasonstr = $this->BlockReason;
356 }
357
358 $expirestr = $this->BlockExpiry;
359 if( $expirestr == 'other' )
360 $expirestr = $this->BlockOther;
361
362 if (strlen($expirestr) == 0) {
363 return self::BLOCK_EXPIRY_INVALID;
364 }
365
366 if ( $expirestr == 'infinite' || $expirestr == 'indefinite' ) {
367 $expiry = Block::infinity();
368 } else {
369 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
370 $expiry = strtotime( $expirestr );
371
372 if ( $expiry < 0 || $expiry === false ) {
373 return self::BLOCK_EXPIRY_INVALID;
374 }
375
376 $expiry = wfTimestamp( TS_MW, $expiry );
377 }
378
379 # Create block
380 # Note: for a user block, ipb_address is only for display purposes
381 $block = new Block( $this->BlockAddress, $userId, $wgUser->getID(),
382 $reasonstr, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly,
383 $this->BlockCreateAccount, $this->BlockEnableAutoblock, $this->BlockHideName,
384 $this->BlockEmail);
385
386 if (wfRunHooks('BlockIp', array(&$block, &$wgUser))) {
387
388 if ( !$block->insert() ) {
389 return self::BLOCK_ALREADY_BLOCKED;
390 }
391
392 wfRunHooks('BlockIpComplete', array($block, $wgUser));
393
394 # Prepare log parameters
395 $logParams = array();
396 $logParams[] = $expirestr;
397 $logParams[] = $this->blockLogFlags();
398
399 # Make log entry, if the name is hidden, put it in the oversight log
400 $log_type = ($this->BlockHideName) ? 'oversight' : 'block';
401 $log = new LogPage( $log_type );
402 $log->addEntry( 'block', Title::makeTitle( NS_USER, $this->BlockAddress ),
403 $reasonstr, $logParams );
404
405 # Report to the user
406 return self::BLOCK_SUCCESS;
407 }
408 }
409
410 /**
411 * UI entry point for blocking
412 * Wraps around doBlock()
413 */
414 function doSubmit()
415 {
416 global $wgOut;
417 $retval = $this->doBlock();
418 switch($retval)
419 {
420 case self::BLOCK_RANGE_INVALID:
421 $this->showForm( wfMsg( 'ip_range_invalid' ) );
422 return;
423 case self::BLOCK_RANGE_DISABLED:
424 $this->showForm( wfMsg( 'range_block_disabled' ) );
425 return;
426 case self::BLOCK_NONEXISTENT_USER:
427 $this->showForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->BlockAddress ) ) );
428 return;
429 case self::BLOCK_IP_INVALID:
430 $this->showForm( wfMsg( 'badipaddress' ) );
431 return;
432 case self::BLOCK_EXPIRY_INVALID:
433 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
434 return;
435 case self::BLOCK_ALREADY_BLOCKED:
436 $this->showForm( wfMsg( 'ipb_already_blocked', htmlspecialchars( $this->BlockAddress ) ) );
437 return;
438 case self::BLOCK_SUCCESS:
439 $titleObj = SpecialPage::getTitleFor( 'Blockip' );
440 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
441 urlencode( $this->BlockAddress ) ) );
442 return;
443 default:
444 throw new MWException( __METHOD__ . ": Unknown return value ``{$retval}''" );
445 }
446 }
447
448 function showSuccess() {
449 global $wgOut;
450
451 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
452 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
453 $text = wfMsg( 'blockipsuccesstext', $this->BlockAddress );
454 $wgOut->addWikiText( $text );
455 }
456
457 function showLogFragment( $out, $title ) {
458 $out->addHtml( wfElement( 'h2', NULL, LogPage::logName( 'block' ) ) );
459 $request = new FauxRequest( array( 'page' => $title->getPrefixedText(), 'type' => 'block' ) );
460 $viewer = new LogViewer( new LogReader( $request ) );
461 $viewer->showList( $out );
462 }
463
464 /**
465 * Return a comma-delimited list of "flags" to be passed to the log
466 * reader for this block, to provide more information in the logs
467 *
468 * @return array
469 */
470 private function blockLogFlags() {
471 $flags = array();
472 if( $this->BlockAnonOnly && IP::isIPAddress( $this->BlockAddress ) )
473 // when blocking a user the option 'anononly' is not available/has no effect -> do not write this into log
474 $flags[] = 'anononly';
475 if( $this->BlockCreateAccount )
476 $flags[] = 'nocreate';
477 if( !$this->BlockEnableAutoblock )
478 $flags[] = 'noautoblock';
479 if ( $this->BlockEmail )
480 $flags[] = 'noemail';
481 return implode( ',', $flags );
482 }
483
484 /**
485 * Builds unblock and block list links
486 *
487 * @return string
488 */
489 private function getConvenienceLinks() {
490 global $wgUser;
491 $skin = $wgUser->getSkin();
492 $links[] = $skin->makeLink ( 'MediaWiki:ipbreason-dropdown', wfMsgHtml( 'ipb-edit-dropdown' ) );
493 $links[] = $this->getUnblockLink( $skin );
494 $links[] = $this->getBlockListLink( $skin );
495 return '<p class="mw-ipb-conveniencelinks">' . implode( ' | ', $links ) . '</p>';
496 }
497
498 /**
499 * Build a convenient link to unblock the given username or IP
500 * address, if available; otherwise link to a blank unblock
501 * form
502 *
503 * @param $skin Skin to use
504 * @return string
505 */
506 private function getUnblockLink( $skin ) {
507 $list = SpecialPage::getTitleFor( 'Ipblocklist' );
508 if( $this->BlockAddress ) {
509 $addr = htmlspecialchars( strtr( $this->BlockAddress, '_', ' ' ) );
510 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock-addr', $addr ),
511 'action=unblock&ip=' . urlencode( $this->BlockAddress ) );
512 } else {
513 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock' ), 'action=unblock' );
514 }
515 }
516
517 /**
518 * Build a convenience link to the block list
519 *
520 * @param $skin Skin to use
521 * @return string
522 */
523 private function getBlockListLink( $skin ) {
524 $list = SpecialPage::getTitleFor( 'Ipblocklist' );
525 if( $this->BlockAddress ) {
526 $addr = htmlspecialchars( strtr( $this->BlockAddress, '_', ' ' ) );
527 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist-addr', $addr ),
528 'ip=' . urlencode( $this->BlockAddress ) );
529 } else {
530 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist' ) );
531 }
532 }
533 }
534