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