* Add IPv6 support
[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
41 *
42 * @addtogroup SpecialPage
43 */
44 class IPBlockForm {
45 var $BlockAddress, $BlockExpiry, $BlockReason;
46
47 function IPBlockForm( $par ) {
48 global $wgRequest;
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->BlockExpiry = $wgRequest->getVal( 'wpBlockExpiry', wfMsg('ipbotheroption') );
54 $this->BlockOther = $wgRequest->getVal( 'wpBlockOther', '' );
55
56 # Unchecked checkboxes are not included in the form data at all, so having one
57 # that is true by default is a bit tricky
58 $byDefault = !$wgRequest->wasPosted();
59 $this->BlockAnonOnly = $wgRequest->getBool( 'wpAnonOnly', $byDefault );
60 $this->BlockCreateAccount = $wgRequest->getBool( 'wpCreateAccount', $byDefault );
61 $this->BlockEnableAutoblock = $wgRequest->getBool( 'wpEnableAutoblock', $byDefault );
62 }
63
64 function showForm( $err ) {
65 global $wgOut, $wgUser, $wgSysopUserBans;
66
67 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
68 $wgOut->addWikiText( wfMsg( 'blockiptext' ) );
69
70 if($wgSysopUserBans) {
71 $mIpaddress = Xml::label( wfMsg( 'ipadressorusername' ), 'mw-bi-target' );
72 } else {
73 $mIpaddress = Xml::label( wfMsg( 'ipadress' ), 'mw-bi-target' );
74 }
75 $mIpbexpiry = Xml::label( wfMsg( 'ipbexpiry' ), 'wpBlockExpiry' );
76 $mIpbother = Xml::label( wfMsg( 'ipbother' ), 'mw-bi-other' );
77 $mIpbothertime = wfMsgHtml( 'ipbotheroption' );
78 $mIpbreason = Xml::label( wfMsg( 'ipbreason' ), 'mw-bi-reason' );
79 $titleObj = SpecialPage::getTitleFor( 'Blockip' );
80 $action = $titleObj->escapeLocalURL( "action=submit" );
81
82 if ( "" != $err ) {
83 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
84 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
85 }
86
87 $scBlockExpiryOptions = wfMsgForContent( 'ipboptions' );
88
89 $showblockoptions = $scBlockExpiryOptions != '-';
90 if (!$showblockoptions)
91 $mIpbother = $mIpbexpiry;
92
93 $blockExpiryFormOptions = "<option value=\"other\">$mIpbothertime</option>";
94 foreach (explode(',', $scBlockExpiryOptions) as $option) {
95 if ( strpos($option, ":") === false ) $option = "$option:$option";
96 list($show, $value) = explode(":", $option);
97 $show = htmlspecialchars($show);
98 $value = htmlspecialchars($value);
99 $selected = "";
100 if ($this->BlockExpiry === $value)
101 $selected = ' selected="selected"';
102 $blockExpiryFormOptions .= "<option value=\"$value\"$selected>$show</option>";
103 }
104
105 $token = htmlspecialchars( $wgUser->editToken() );
106
107 global $wgStylePath, $wgStyleVersion;
108 $wgOut->addHTML( "
109 <script type=\"text/javascript\" src=\"$wgStylePath/common/block.js?$wgStyleVersion\">
110 </script>
111 <form id=\"blockip\" method=\"post\" action=\"{$action}\">
112 <table border='0'>
113 <tr>
114 <td align=\"right\">{$mIpaddress}:</td>
115 <td align=\"left\">
116 " . Xml::input( 'wpBlockAddress', 40, $this->BlockAddress,
117 array(
118 'tabindex' => '1',
119 'id' => 'mw-bi-target',
120 'onchange' => 'updateBlockOptions()' ) ) . "
121 </td>
122 </tr>
123 <tr>");
124 if ($showblockoptions) {
125 $wgOut->addHTML("
126 <td align=\"right\">{$mIpbexpiry}:</td>
127 <td align=\"left\">
128 <select tabindex='2' id='wpBlockExpiry' name=\"wpBlockExpiry\" onchange=\"considerChangingExpiryFocus()\">
129 $blockExpiryFormOptions
130 </select>
131 </td>
132 ");
133 }
134 $wgOut->addHTML("
135 </tr>
136 <tr id='wpBlockOther'>
137 <td align=\"right\">{$mIpbother}:</td>
138 <td align=\"left\">
139 " . Xml::input( 'wpBlockOther', 40, $this->BlockOther,
140 array( 'tabindex' => '3', 'id' => 'mw-bi-other' ) ) . "
141 </td>
142 </tr>
143 <tr>
144 <td align=\"right\">{$mIpbreason}:</td>
145 <td align=\"left\">
146 " . Xml::input( 'wpBlockReason', 40, $this->BlockReason,
147 array( 'tabindex' => '3', 'id' => 'mw-bi-reason' ) ) . "
148 </td>
149 </tr>
150 <tr id='wpAnonOnlyRow'>
151 <td>&nbsp;</td>
152 <td align=\"left\">
153 " . wfCheckLabel( wfMsg( 'ipbanononly' ),
154 'wpAnonOnly', 'wpAnonOnly', $this->BlockAnonOnly,
155 array( 'tabindex' => 4 ) ) . "
156 </td>
157 </tr>
158 <tr id='wpCreateAccountRow'>
159 <td>&nbsp;</td>
160 <td align=\"left\">
161 " . wfCheckLabel( wfMsg( 'ipbcreateaccount' ),
162 'wpCreateAccount', 'wpCreateAccount', $this->BlockCreateAccount,
163 array( 'tabindex' => 5 ) ) . "
164 </td>
165 </tr>
166 <tr id='wpEnableAutoblockRow'>
167 <td>&nbsp;</td>
168 <td align=\"left\">
169 " . wfCheckLabel( wfMsg( 'ipbenableautoblock' ),
170 'wpEnableAutoblock', 'wpEnableAutoblock', $this->BlockEnableAutoblock,
171 array( 'tabindex' => 6 ) ) . "
172 </td>
173 </tr>
174 <tr>
175 <td style='padding-top: 1em'>&nbsp;</td>
176 <td style='padding-top: 1em' align=\"left\">
177 " . Xml::submitButton( wfMsg( 'ipbsubmit' ),
178 array( 'name' => 'wpBlock', 'tabindex' => '7' ) ) . "
179 </td>
180 </tr>
181 </table>" .
182 Xml::hidden( 'wpEditToken', $token ) .
183 "</form>
184 <script type=\"text/javascript\">updateBlockOptions()</script>
185 \n" );
186
187 $wgOut->addHtml( $this->getConvenienceLinks() );
188
189 $user = User::newFromName( $this->BlockAddress );
190 if( is_object( $user ) ) {
191 $this->showLogFragment( $wgOut, $user->getUserPage() );
192 } elseif( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->BlockAddress ) ) {
193 $this->showLogFragment( $wgOut, Title::makeTitle( NS_USER, $this->BlockAddress ) );
194 }
195 }
196
197 function doSubmit() {
198 global $wgOut, $wgUser, $wgSysopUserBans, $wgSysopRangeBans;
199
200 $userId = 0;
201 $this->BlockAddress = trim( $this->BlockAddress );
202 # Expand valid IPv6 addresses
203 if ( IP::isIPAddress( $this->BlockAddress ) ) {
204 $this->BlockAddress = IP::expandIP( $this->BlockAddress );
205 }
206 # The above validation is good enough that those below will suffice from here
207 $rxIP4 = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
208 $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}';
209 $rxIP = "($rxIP4|$rxIP6)";
210
211 # Check for invalid specifications
212 if ( !preg_match( "/^$rxIP$/", $this->BlockAddress ) ) {
213 $matches = array();
214 if ( preg_match( "/^($rxIP4)\\/(\\d{1,2})$/", $this->BlockAddress, $matches ) ) {
215 # IPv4
216 if ( $wgSysopRangeBans ) {
217 if ( !IP::isIPv4( $this->BlockAddress ) || $matches[2] > 31 || $matches[2] < 16 ) {
218 $this->showForm( wfMsg( 'ip_range_invalid' ) );
219 return;
220 }
221 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
222 } else {
223 # Range block illegal
224 $this->showForm( wfMsg( 'range_block_disabled' ) );
225 return;
226 }
227 } else if ( preg_match( "/^($rxIP6)\\/(\\d{1,3})$/", $this->BlockAddress, $matches ) ) {
228 # IPv6
229 if ( $wgSysopRangeBans ) {
230 if ( !IP::isIPv6( $this->BlockAddress ) || $matches[2] > 127 || $matches[2] < 64 ) {
231 $this->showForm( wfMsg( 'ip_range_invalid' ) );
232 return;
233 }
234 $this->BlockAddress = Block::normaliseRange6( $this->BlockAddress );
235 } else {
236 # Range block illegal
237 $this->showForm( wfMsg( 'range_block_disabled' ) );
238 return;
239 }
240 } else {
241 # Username block
242 if ( $wgSysopUserBans ) {
243 $user = User::newFromName( $this->BlockAddress );
244 if( !is_null( $user ) && $user->getID() ) {
245 # Use canonical name
246 $this->BlockAddress = $user->getName();
247 $userId = $user->getID();
248 } else {
249 $this->showForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->BlockAddress ) ) );
250 return;
251 }
252 } else {
253 $this->showForm( wfMsg( 'badipaddress' ) );
254 return;
255 }
256 }
257 }
258
259 $expirestr = $this->BlockExpiry;
260 if( $expirestr == 'other' )
261 $expirestr = $this->BlockOther;
262
263 if (strlen($expirestr) == 0) {
264 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
265 return;
266 }
267
268 if ( $expirestr == 'infinite' || $expirestr == 'indefinite' ) {
269 $expiry = Block::infinity();
270 } else {
271 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
272 $expiry = strtotime( $expirestr );
273
274 if ( $expiry < 0 || $expiry === false ) {
275 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
276 return;
277 }
278
279 $expiry = wfTimestamp( TS_MW, $expiry );
280 }
281
282 # Create block
283 # Note: for a user block, ipb_address is only for display purposes
284
285 $block = new Block( $this->BlockAddress, $userId, $wgUser->getID(),
286 $this->BlockReason, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly,
287 $this->BlockCreateAccount, $this->BlockEnableAutoblock );
288
289 if (wfRunHooks('BlockIp', array(&$block, &$wgUser))) {
290
291 if ( !$block->insert() ) {
292 $this->showForm( wfMsg( 'ipb_already_blocked',
293 htmlspecialchars( $this->BlockAddress ) ) );
294 return;
295 }
296
297 wfRunHooks('BlockIpComplete', array($block, $wgUser));
298
299 # Prepare log parameters
300 $logParams = array();
301 $logParams[] = $expirestr;
302 $logParams[] = $this->blockLogFlags();
303
304 # Make log entry
305 $log = new LogPage( 'block' );
306 $log->addEntry( 'block', Title::makeTitle( NS_USER, $this->BlockAddress ),
307 $this->BlockReason, $logParams );
308
309 # Report to the user
310 $titleObj = SpecialPage::getTitleFor( 'Blockip' );
311 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
312 urlencode( $this->BlockAddress ) ) );
313 }
314 }
315
316 function showSuccess() {
317 global $wgOut;
318
319 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
320 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
321 $text = wfMsg( 'blockipsuccesstext', $this->BlockAddress );
322 $wgOut->addWikiText( $text );
323 }
324
325 function showLogFragment( $out, $title ) {
326 $out->addHtml( wfElement( 'h2', NULL, LogPage::logName( 'block' ) ) );
327 $request = new FauxRequest( array( 'page' => $title->getPrefixedText(), 'type' => 'block' ) );
328 $viewer = new LogViewer( new LogReader( $request ) );
329 $viewer->showList( $out );
330 }
331
332 /**
333 * Return a comma-delimited list of "flags" to be passed to the log
334 * reader for this block, to provide more information in the logs
335 *
336 * @return array
337 */
338 private function blockLogFlags() {
339 $flags = array();
340 if( $this->BlockAnonOnly )
341 $flags[] = 'anononly';
342 if( $this->BlockCreateAccount )
343 $flags[] = 'nocreate';
344 if( !$this->BlockEnableAutoblock )
345 $flags[] = 'noautoblock';
346 return implode( ',', $flags );
347 }
348
349 /**
350 * Builds unblock and block list links
351 *
352 * @return string
353 */
354 private function getConvenienceLinks() {
355 global $wgUser;
356 $skin = $wgUser->getSkin();
357 $links[] = $this->getUnblockLink( $skin );
358 $links[] = $this->getBlockListLink( $skin );
359 return '<p class="mw-ipb-conveniencelinks">' . implode( ' | ', $links ) . '</p>';
360 }
361
362 /**
363 * Build a convenient link to unblock the given username or IP
364 * address, if available; otherwise link to a blank unblock
365 * form
366 *
367 * @param $skin Skin to use
368 * @return string
369 */
370 private function getUnblockLink( $skin ) {
371 $list = SpecialPage::getTitleFor( 'Ipblocklist' );
372 if( $this->BlockAddress ) {
373 $addr = htmlspecialchars( strtr( $this->BlockAddress, '_', ' ' ) );
374 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock-addr', $addr ),
375 'action=unblock&ip=' . urlencode( $this->BlockAddress ) );
376 } else {
377 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock' ), 'action=unblock' );
378 }
379 }
380
381 /**
382 * Build a convenience link to the block list
383 *
384 * @param $skin Skin to use
385 * @return string
386 */
387 private function getBlockListLink( $skin ) {
388 $list = SpecialPage::getTitleFor( 'Ipblocklist' );
389 if( $this->BlockAddress ) {
390 $addr = htmlspecialchars( strtr( $this->BlockAddress, '_', ' ' ) );
391 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist-addr', $addr ),
392 'ip=' . urlencode( $this->BlockAddress ) );
393 } else {
394 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist' ) );
395 }
396 }
397 }
398 ?>