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