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