27729f2b825c53b3cf35223e9581dfb9ff7903fe
[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', 45, $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', 45, $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', 45, $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 } 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 ) ) {
195 $this->showLogFragment( $wgOut, Title::makeTitle( NS_USER, $this->BlockAddress ) );
196 }
197 }
198
199 function doSubmit() {
200 global $wgOut, $wgUser, $wgSysopUserBans, $wgSysopRangeBans;
201
202 $userId = 0;
203 # Expand valid IPv6 addresses, usernames are left as is
204 $this->BlockAddress = IP::sanitizeIP( $this->BlockAddress );
205 # isIPv4() and IPv6() are used for final validation
206 $rxIP4 = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
207 $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}';
208 $rxIP = "($rxIP4|$rxIP6)";
209
210 # Check for invalid specifications
211 if ( !preg_match( "/^$rxIP$/", $this->BlockAddress ) ) {
212 $matches = array();
213 if ( preg_match( "/^($rxIP4)\\/(\\d{1,2})$/", $this->BlockAddress, $matches ) ) {
214 # IPv4
215 if ( $wgSysopRangeBans ) {
216 if ( !IP::isIPv4( $this->BlockAddress ) || $matches[2] > 31 || $matches[2] < 16 ) {
217 $this->showForm( wfMsg( 'ip_range_invalid' ) );
218 return;
219 }
220 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
221 } else {
222 # Range block illegal
223 $this->showForm( wfMsg( 'range_block_disabled' ) );
224 return;
225 }
226 } else if ( preg_match( "/^($rxIP6)\\/(\\d{1,3})$/", $this->BlockAddress, $matches ) ) {
227 # IPv6
228 if ( $wgSysopRangeBans ) {
229 if ( !IP::isIPv6( $this->BlockAddress ) || $matches[2] > 127 || $matches[2] < 64 ) {
230 $this->showForm( wfMsg( 'ip_range_invalid' ) );
231 return;
232 }
233 $this->BlockAddress = Block::normaliseRange6( $this->BlockAddress );
234 } else {
235 # Range block illegal
236 $this->showForm( wfMsg( 'range_block_disabled' ) );
237 return;
238 }
239 } else {
240 # Username block
241 if ( $wgSysopUserBans ) {
242 $user = User::newFromName( $this->BlockAddress );
243 if( !is_null( $user ) && $user->getID() ) {
244 # Use canonical name
245 $this->BlockAddress = $user->getName();
246 $userId = $user->getID();
247 } else {
248 $this->showForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->BlockAddress ) ) );
249 return;
250 }
251 } else {
252 $this->showForm( wfMsg( 'badipaddress' ) );
253 return;
254 }
255 }
256 }
257
258 $expirestr = $this->BlockExpiry;
259 if( $expirestr == 'other' )
260 $expirestr = $this->BlockOther;
261
262 if (strlen($expirestr) == 0) {
263 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
264 return;
265 }
266
267 if ( $expirestr == 'infinite' || $expirestr == 'indefinite' ) {
268 $expiry = Block::infinity();
269 } else {
270 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
271 $expiry = strtotime( $expirestr );
272
273 if ( $expiry < 0 || $expiry === false ) {
274 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
275 return;
276 }
277
278 $expiry = wfTimestamp( TS_MW, $expiry );
279 }
280
281 # Create block
282 # Note: for a user block, ipb_address is only for display purposes
283
284 $block = new Block( $this->BlockAddress, $userId, $wgUser->getID(),
285 $this->BlockReason, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly,
286 $this->BlockCreateAccount, $this->BlockEnableAutoblock );
287
288 if (wfRunHooks('BlockIp', array(&$block, &$wgUser))) {
289
290 if ( !$block->insert() ) {
291 $this->showForm( wfMsg( 'ipb_already_blocked',
292 htmlspecialchars( $this->BlockAddress ) ) );
293 return;
294 }
295
296 wfRunHooks('BlockIpComplete', array($block, $wgUser));
297
298 # Prepare log parameters
299 $logParams = array();
300 $logParams[] = $expirestr;
301 $logParams[] = $this->blockLogFlags();
302
303 # Make log entry
304 $log = new LogPage( 'block' );
305 $log->addEntry( 'block', Title::makeTitle( NS_USER, $this->BlockAddress ),
306 $this->BlockReason, $logParams );
307
308 # Report to the user
309 $titleObj = SpecialPage::getTitleFor( 'Blockip' );
310 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
311 urlencode( $this->BlockAddress ) ) );
312 }
313 }
314
315 function showSuccess() {
316 global $wgOut;
317
318 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
319 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
320 $text = wfMsg( 'blockipsuccesstext', $this->BlockAddress );
321 $wgOut->addWikiText( $text );
322 }
323
324 function showLogFragment( $out, $title ) {
325 $out->addHtml( wfElement( 'h2', NULL, LogPage::logName( 'block' ) ) );
326 $request = new FauxRequest( array( 'page' => $title->getPrefixedText(), 'type' => 'block' ) );
327 $viewer = new LogViewer( new LogReader( $request ) );
328 $viewer->showList( $out );
329 }
330
331 /**
332 * Return a comma-delimited list of "flags" to be passed to the log
333 * reader for this block, to provide more information in the logs
334 *
335 * @return array
336 */
337 private function blockLogFlags() {
338 $flags = array();
339 if( $this->BlockAnonOnly && IP::isIPAddress( $this->BlockAddress ) )
340 // when blocking a user the option 'anononly' is not available/has no effect -> do not write this into log
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 ?>