*Automatic address expansion, remove redundant check
[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, usernames are left as is
203 $this->BlockAddress = IP::expandIP( $this->BlockAddress );
204 # isIPv4() and IPv6() are used for final validation
205 $rxIP4 = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
206 $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}';
207 $rxIP = "($rxIP4|$rxIP6)";
208
209 # Check for invalid specifications
210 if ( !preg_match( "/^$rxIP$/", $this->BlockAddress ) ) {
211 $matches = array();
212 if ( preg_match( "/^($rxIP4)\\/(\\d{1,2})$/", $this->BlockAddress, $matches ) ) {
213 # IPv4
214 if ( $wgSysopRangeBans ) {
215 if ( !IP::isIPv4( $this->BlockAddress ) || $matches[2] > 31 || $matches[2] < 16 ) {
216 $this->showForm( wfMsg( 'ip_range_invalid' ) );
217 return;
218 }
219 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
220 } else {
221 # Range block illegal
222 $this->showForm( wfMsg( 'range_block_disabled' ) );
223 return;
224 }
225 } else if ( preg_match( "/^($rxIP6)\\/(\\d{1,3})$/", $this->BlockAddress, $matches ) ) {
226 # IPv6
227 if ( $wgSysopRangeBans ) {
228 if ( !IP::isIPv6( $this->BlockAddress ) || $matches[2] > 127 || $matches[2] < 64 ) {
229 $this->showForm( wfMsg( 'ip_range_invalid' ) );
230 return;
231 }
232 $this->BlockAddress = Block::normaliseRange6( $this->BlockAddress );
233 } else {
234 # Range block illegal
235 $this->showForm( wfMsg( 'range_block_disabled' ) );
236 return;
237 }
238 } else {
239 # Username block
240 if ( $wgSysopUserBans ) {
241 $user = User::newFromName( $this->BlockAddress );
242 if( !is_null( $user ) && $user->getID() ) {
243 # Use canonical name
244 $this->BlockAddress = $user->getName();
245 $userId = $user->getID();
246 } else {
247 $this->showForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->BlockAddress ) ) );
248 return;
249 }
250 } else {
251 $this->showForm( wfMsg( 'badipaddress' ) );
252 return;
253 }
254 }
255 }
256
257 $expirestr = $this->BlockExpiry;
258 if( $expirestr == 'other' )
259 $expirestr = $this->BlockOther;
260
261 if (strlen($expirestr) == 0) {
262 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
263 return;
264 }
265
266 if ( $expirestr == 'infinite' || $expirestr == 'indefinite' ) {
267 $expiry = Block::infinity();
268 } else {
269 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
270 $expiry = strtotime( $expirestr );
271
272 if ( $expiry < 0 || $expiry === false ) {
273 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
274 return;
275 }
276
277 $expiry = wfTimestamp( TS_MW, $expiry );
278 }
279
280 # Create block
281 # Note: for a user block, ipb_address is only for display purposes
282
283 $block = new Block( $this->BlockAddress, $userId, $wgUser->getID(),
284 $this->BlockReason, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly,
285 $this->BlockCreateAccount, $this->BlockEnableAutoblock );
286
287 if (wfRunHooks('BlockIp', array(&$block, &$wgUser))) {
288
289 if ( !$block->insert() ) {
290 $this->showForm( wfMsg( 'ipb_already_blocked',
291 htmlspecialchars( $this->BlockAddress ) ) );
292 return;
293 }
294
295 wfRunHooks('BlockIpComplete', array($block, $wgUser));
296
297 # Prepare log parameters
298 $logParams = array();
299 $logParams[] = $expirestr;
300 $logParams[] = $this->blockLogFlags();
301
302 # Make log entry
303 $log = new LogPage( 'block' );
304 $log->addEntry( 'block', Title::makeTitle( NS_USER, $this->BlockAddress ),
305 $this->BlockReason, $logParams );
306
307 # Report to the user
308 $titleObj = SpecialPage::getTitleFor( 'Blockip' );
309 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
310 urlencode( $this->BlockAddress ) ) );
311 }
312 }
313
314 function showSuccess() {
315 global $wgOut;
316
317 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
318 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
319 $text = wfMsg( 'blockipsuccesstext', $this->BlockAddress );
320 $wgOut->addWikiText( $text );
321 }
322
323 function showLogFragment( $out, $title ) {
324 $out->addHtml( wfElement( 'h2', NULL, LogPage::logName( 'block' ) ) );
325 $request = new FauxRequest( array( 'page' => $title->getPrefixedText(), 'type' => 'block' ) );
326 $viewer = new LogViewer( new LogReader( $request ) );
327 $viewer->showList( $out );
328 }
329
330 /**
331 * Return a comma-delimited list of "flags" to be passed to the log
332 * reader for this block, to provide more information in the logs
333 *
334 * @return array
335 */
336 private function blockLogFlags() {
337 $flags = array();
338 if( $this->BlockAnonOnly )
339 $flags[] = 'anononly';
340 if( $this->BlockCreateAccount )
341 $flags[] = 'nocreate';
342 if( !$this->BlockEnableAutoblock )
343 $flags[] = 'noautoblock';
344 return implode( ',', $flags );
345 }
346
347 /**
348 * Builds unblock and block list links
349 *
350 * @return string
351 */
352 private function getConvenienceLinks() {
353 global $wgUser;
354 $skin = $wgUser->getSkin();
355 $links[] = $this->getUnblockLink( $skin );
356 $links[] = $this->getBlockListLink( $skin );
357 return '<p class="mw-ipb-conveniencelinks">' . implode( ' | ', $links ) . '</p>';
358 }
359
360 /**
361 * Build a convenient link to unblock the given username or IP
362 * address, if available; otherwise link to a blank unblock
363 * form
364 *
365 * @param $skin Skin to use
366 * @return string
367 */
368 private function getUnblockLink( $skin ) {
369 $list = SpecialPage::getTitleFor( 'Ipblocklist' );
370 if( $this->BlockAddress ) {
371 $addr = htmlspecialchars( strtr( $this->BlockAddress, '_', ' ' ) );
372 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock-addr', $addr ),
373 'action=unblock&ip=' . urlencode( $this->BlockAddress ) );
374 } else {
375 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock' ), 'action=unblock' );
376 }
377 }
378
379 /**
380 * Build a convenience link to the block list
381 *
382 * @param $skin Skin to use
383 * @return string
384 */
385 private function getBlockListLink( $skin ) {
386 $list = SpecialPage::getTitleFor( 'Ipblocklist' );
387 if( $this->BlockAddress ) {
388 $addr = htmlspecialchars( strtr( $this->BlockAddress, '_', ' ' ) );
389 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist-addr', $addr ),
390 'ip=' . urlencode( $this->BlockAddress ) );
391 } else {
392 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist' ) );
393 }
394 }
395 }
396 ?>