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