* switched 'anon-only' block mode to default for IP blocks
[lhc/web/wiklou.git] / includes / SpecialBlockip.php
1 <?php
2 /**
3 * Constructor for Special:Blockip page
4 *
5 * @package MediaWiki
6 * @subpackage SpecialPage
7 */
8
9 /**
10 * Constructor
11 */
12 function wfSpecialBlockip( $par ) {
13 global $wgUser, $wgOut, $wgRequest;
14
15 if( !$wgUser->isAllowed( 'block' ) ) {
16 $wgOut->permissionRequired( 'block' );
17 return;
18 }
19
20 $ipb = new IPBlockForm( $par );
21
22 $action = $wgRequest->getVal( 'action' );
23 if ( 'success' == $action ) {
24 $ipb->showSuccess();
25 } else if ( $wgRequest->wasPosted() && 'submit' == $action &&
26 $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
27 $ipb->doSubmit();
28 } else {
29 $ipb->showForm( '' );
30 }
31 }
32
33 /**
34 * Form object
35 *
36 * @package MediaWiki
37 * @subpackage SpecialPage
38 */
39 class IPBlockForm {
40 var $BlockAddress, $BlockExpiry, $BlockReason;
41
42 function IPBlockForm( $par ) {
43 global $wgRequest;
44
45 $this->BlockAddress = $wgRequest->getVal( 'wpBlockAddress', $wgRequest->getVal( 'ip', $par ) );
46 $this->BlockReason = $wgRequest->getText( 'wpBlockReason' );
47 $this->BlockExpiry = $wgRequest->getVal( 'wpBlockExpiry', wfMsg('ipbotheroption') );
48 $this->BlockOther = $wgRequest->getVal( 'wpBlockOther', '' );
49
50 # Unchecked checkboxes are not included in the form data at all, so having one
51 # that is true by default is a bit tricky
52 $byDefault = !$wgRequest->wasPosted();
53 $this->BlockAnonOnly = $wgRequest->getBool( 'wpAnonOnly', $byDefault );
54 $this->BlockCreateAccount = $wgRequest->getBool( 'wpCreateAccount', $byDefault );
55 $this->BlockEnableAutoblock = $wgRequest->getBool( 'wpEnableAutoblock', $byDefault );
56 }
57
58 function showForm( $err ) {
59 global $wgOut, $wgUser, $wgSysopUserBans;
60
61 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
62 $wgOut->addWikiText( wfMsg( 'blockiptext' ) );
63
64 if($wgSysopUserBans) {
65 $mIpaddress = wfMsgHtml( 'ipadressorusername' );
66 } else {
67 $mIpaddress = wfMsgHtml( 'ipaddress' );
68 }
69 $mIpbexpiry = wfMsgHtml( 'ipbexpiry' );
70 $mIpbother = wfMsgHtml( 'ipbother' );
71 $mIpbothertime = wfMsgHtml( 'ipbotheroption' );
72 $mIpbreason = wfMsgHtml( 'ipbreason' );
73 $mIpbsubmit = wfMsgHtml( 'ipbsubmit' );
74 $titleObj = SpecialPage::getTitleFor( 'Blockip' );
75 $action = $titleObj->escapeLocalURL( "action=submit" );
76
77 if ( "" != $err ) {
78 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
79 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
80 }
81
82 $scBlockAddress = htmlspecialchars( $this->BlockAddress );
83 $scBlockExpiry = htmlspecialchars( $this->BlockExpiry );
84 $scBlockReason = htmlspecialchars( $this->BlockReason );
85 $scBlockOtherTime = htmlspecialchars( $this->BlockOther );
86 $scBlockExpiryOptions = htmlspecialchars( wfMsgForContent( 'ipboptions' ) );
87
88 $showblockoptions = $scBlockExpiryOptions != '-';
89 if (!$showblockoptions)
90 $mIpbother = $mIpbexpiry;
91
92 $blockExpiryFormOptions = "<option value=\"other\">$mIpbothertime</option>";
93 foreach (explode(',', $scBlockExpiryOptions) as $option) {
94 if ( strpos($option, ":") === false ) $option = "$option:$option";
95 list($show, $value) = explode(":", $option);
96 $show = htmlspecialchars($show);
97 $value = htmlspecialchars($value);
98 $selected = "";
99 if ($this->BlockExpiry === $value)
100 $selected = ' selected="selected"';
101 $blockExpiryFormOptions .= "<option value=\"$value\"$selected>$show</option>";
102 }
103
104 $token = htmlspecialchars( $wgUser->editToken() );
105
106 $wgOut->addHTML( "
107 <form id=\"blockip\" method=\"post\" action=\"{$action}\">
108 <table border='0'>
109 <tr>
110 <td align=\"right\">{$mIpaddress}:</td>
111 <td align=\"left\">
112 <input tabindex='1' type='text' size='40' name=\"wpBlockAddress\" value=\"{$scBlockAddress}\" />
113 </td>
114 </tr>
115 <tr>");
116 if ($showblockoptions) {
117 $wgOut->addHTML("
118 <td align=\"right\">{$mIpbexpiry}:</td>
119 <td align=\"left\">
120 <select tabindex='2' id='wpBlockExpiry' name=\"wpBlockExpiry\" onchange=\"considerChangingExpiryFocus()\">
121 $blockExpiryFormOptions
122 </select>
123 </td>
124 ");
125 }
126 $wgOut->addHTML("
127 </tr>
128 <tr id='wpBlockOther'>
129 <td align=\"right\">{$mIpbother}:</td>
130 <td align=\"left\">
131 <input tabindex='3' type='text' size='40' name=\"wpBlockOther\" value=\"{$scBlockOtherTime}\" />
132 </td>
133 </tr>
134 <tr>
135 <td align=\"right\">{$mIpbreason}:</td>
136 <td align=\"left\">
137 <input tabindex='3' type='text' size='40' name=\"wpBlockReason\" value=\"{$scBlockReason}\" />
138 </td>
139 </tr>
140 <tr>
141 <td>&nbsp;</td>
142 <td align=\"left\">
143 " . wfCheckLabel( wfMsg( 'ipbanononly' ),
144 'wpAnonOnly', 'wpAnonOnly', $this->BlockAnonOnly,
145 array( 'tabindex' => 4 ) ) . "
146 </td>
147 </tr>
148 <tr>
149 <td>&nbsp;</td>
150 <td align=\"left\">
151 " . wfCheckLabel( wfMsg( 'ipbcreateaccount' ),
152 'wpCreateAccount', 'wpCreateAccount', $this->BlockCreateAccount,
153 array( 'tabindex' => 5 ) ) . "
154 </td>
155 </tr>
156 <tr>
157 <td>&nbsp;</td>
158 <td align=\"left\">
159 " . wfCheckLabel( wfMsg( 'ipbenableautoblock' ),
160 'wpEnableAutoblock', 'wpEnableAutoblock', $this->BlockEnableAutoblock,
161 array( 'tabindex' => 6 ) ) . "
162 </td>
163 </tr>
164 <tr>
165 <td style='padding-top: 1em'>&nbsp;</td>
166 <td style='padding-top: 1em' align=\"left\">
167 <input tabindex='7' type='submit' name=\"wpBlock\" value=\"{$mIpbsubmit}\" />
168 </td>
169 </tr>
170 </table>
171 <input type='hidden' name='wpEditToken' value=\"{$token}\" />
172 </form>\n" );
173
174 $user = User::newFromName( $this->BlockAddress );
175 if( is_object( $user ) ) {
176 $this->showLogFragment( $wgOut, $user->getUserPage() );
177 } elseif( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->BlockAddress ) ) {
178 $this->showLogFragment( $wgOut, Title::makeTitle( NS_USER, $this->BlockAddress ) );
179 }
180
181 }
182
183 function doSubmit() {
184 global $wgOut, $wgUser, $wgSysopUserBans, $wgSysopRangeBans;
185
186 $userId = 0;
187 $this->BlockAddress = trim( $this->BlockAddress );
188 $rxIP = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
189
190 # Check for invalid specifications
191 if ( ! preg_match( "/^$rxIP$/", $this->BlockAddress ) ) {
192 if ( preg_match( "/^($rxIP)\\/(\\d{1,2})$/", $this->BlockAddress, $matches ) ) {
193 if ( $wgSysopRangeBans ) {
194 if ( $matches[2] > 31 || $matches[2] < 16 ) {
195 $this->showForm( wfMsg( 'ip_range_invalid' ) );
196 return;
197 }
198 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
199 } else {
200 # Range block illegal
201 $this->showForm( wfMsg( 'range_block_disabled' ) );
202 return;
203 }
204 } else {
205 # Username block
206 if ( $wgSysopUserBans ) {
207 $user = User::newFromName( $this->BlockAddress );
208 if( !is_null( $user ) && $user->getID() ) {
209 # Use canonical name
210 $this->BlockAddress = $user->getName();
211 $userId = $user->getID();
212 } else {
213 $this->showForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->BlockAddress ) ) );
214 return;
215 }
216 } else {
217 $this->showForm( wfMsg( 'badipaddress' ) );
218 return;
219 }
220 }
221 }
222
223 $expirestr = $this->BlockExpiry;
224 if( $expirestr == 'other' )
225 $expirestr = $this->BlockOther;
226
227 if (strlen($expirestr) == 0) {
228 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
229 return;
230 }
231
232 if ( $expirestr == 'infinite' || $expirestr == 'indefinite' ) {
233 $expiry = Block::infinity();
234 } else {
235 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
236 $expiry = strtotime( $expirestr );
237
238 if ( $expiry < 0 || $expiry === false ) {
239 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
240 return;
241 }
242
243 $expiry = wfTimestamp( TS_MW, $expiry );
244 }
245
246 # Create block
247 # Note: for a user block, ipb_address is only for display purposes
248
249 $block = new Block( $this->BlockAddress, $userId, $wgUser->getID(),
250 $this->BlockReason, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly,
251 $this->BlockCreateAccount, $this->BlockEnableAutoblock );
252
253 if (wfRunHooks('BlockIp', array(&$block, &$wgUser))) {
254
255 if ( !$block->insert() ) {
256 $this->showForm( wfMsg( 'ipb_already_blocked',
257 htmlspecialchars( $this->BlockAddress ) ) );
258 return;
259 }
260
261 wfRunHooks('BlockIpComplete', array($block, $wgUser));
262
263 # Make log entry
264 $log = new LogPage( 'block' );
265 $log->addEntry( 'block', Title::makeTitle( NS_USER, $this->BlockAddress ),
266 $this->BlockReason, $expirestr );
267
268 # Report to the user
269 $titleObj = SpecialPage::getTitleFor( 'Blockip' );
270 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
271 urlencode( $this->BlockAddress ) ) );
272 }
273 }
274
275 function showSuccess() {
276 global $wgOut;
277
278 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
279 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
280 $text = wfMsg( 'blockipsuccesstext', $this->BlockAddress );
281 $wgOut->addWikiText( $text );
282 }
283
284 function showLogFragment( &$out, &$title ) {
285 $out->addHtml( wfElement( 'h2', NULL, LogPage::logName( 'block' ) ) );
286 $request = new FauxRequest( array( 'page' => $title->getPrefixedText(), 'type' => 'block' ) );
287 $viewer = new LogViewer( new LogReader( $request ) );
288 $viewer->showList( $out );
289 }
290
291 }
292
293 ?>